Skip to content

Commit

Permalink
Better mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
CardealRusso committed Oct 16, 2024
1 parent e9d94b0 commit 9f2b6c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
33 changes: 11 additions & 22 deletions examples/mouse_buttons.nim
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import fenstim
import fenstim, strutils

var
app = init(Fenster, "Mouse Buttons test", 800, 600)
lastMouseX, lastMouseY = -1
lastClickStates: array[5, int]
lastHoldStates: array[3, int]
prevMClick = app.mclick
prevMHold = app.mhold

while app.loop and app.keys[27] == 0:
let (mouseX, mouseY, clickStates, holdStates) = app.mouse
#It doesn't matter if you call app.m* several times in the same loop, since the value will only change in the next loop
if app.mclick != prevMClick or app.mhold != prevMHold:
echo "Clicks: ", app.mclick
echo "HOlding: ", app.mhold

let positionChanged = [mouseX, mouseY] != [lastMouseX, lastMouseY]
let clickChanged = clickStates != lastClickStates
let holdChanged = holdStates != lastHoldStates

if positionChanged or clickChanged or holdChanged:
echo "Mouse position: (", mouseX, ", ", mouseY, ")"
echo "Click states: ", clickStates
echo "Hold states: ", holdStates

echo "--------------------"

lastMouseX = mouseX
lastMouseY = mouseY
lastClickStates = clickStates
lastHoldStates = holdStates

app.close
echo "-".repeat(15)

prevMClick = app.mclick
prevMHold = app.mhold
15 changes: 4 additions & 11 deletions src/fenstim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type
modkey*: cint
x*: cint
y*: cint
mclick: array[5, cint]
mhold: array[3, cint]

Fenster* = object
raw: ptr FensterStruct
Expand All @@ -31,7 +33,6 @@ proc fenster_loop(fenster: ptr FensterStruct): cint
proc fenster_close(fenster: ptr FensterStruct)
proc fenster_sleep(ms: cint)
proc fenster_time(): int64
proc fenster_mouse(fenster: ptr FensterStruct, click_state: ptr array[5, cint], hold_state: ptr array[3, cint])
{.pop.}

proc close*(self: var Fenster) =
Expand Down Expand Up @@ -80,18 +81,10 @@ template width*(self: Fenster): int = self.raw.width.int
template height*(self: Fenster): int = self.raw.height.int
template keys*(self: Fenster): array[256, cint] = self.raw.keys
template modkey*(self: Fenster): int = self.raw.modkey.int
template mclick*(self: Fenster): array[5, cint] = self.raw.mclick
template mhold*(self: Fenster): array[3, cint] = self.raw.mhold
proc sleep*(self: Fenster, ms: int) = fenster_sleep(ms.cint)
proc time*(self: Fenster): int64 = fenster_time()
proc mouse*(self: Fenster): tuple[x, y: int, click: array[5, int], hold: array[3, int]] =
var click_state: array[5, cint]
var hold_state: array[3, cint]
fenster_mouse(self.raw, addr click_state, addr hold_state)
(
x: self.raw.x.int,
y: self.raw.y.int,
click: [click_state[0].int, click_state[1].int, click_state[2].int, click_state[3].int, click_state[4].int],
hold: [hold_state[0].int, hold_state[1].int, hold_state[2].int]
)

#Below are functions that are not part of Fenster
template clear*(self: Fenster) = zeroMem(self.raw.buf, self.raw.width.int * self.raw.height.int * sizeof(uint32))
Expand Down

0 comments on commit 9f2b6c4

Please sign in to comment.