From 9f2b6c4996ccf764d4e2069fb8b0ab254e180995 Mon Sep 17 00:00:00 2001 From: CardealRusso Date: Tue, 15 Oct 2024 22:52:20 -0300 Subject: [PATCH] Better mouse --- examples/mouse_buttons.nim | 33 +++++++++++---------------------- src/fensterb | 2 +- src/fenstim.nim | 15 ++++----------- 3 files changed, 16 insertions(+), 34 deletions(-) diff --git a/examples/mouse_buttons.nim b/examples/mouse_buttons.nim index 8eaf18b..748f887 100644 --- a/examples/mouse_buttons.nim +++ b/examples/mouse_buttons.nim @@ -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 diff --git a/src/fensterb b/src/fensterb index 09f1838..f522473 160000 --- a/src/fensterb +++ b/src/fensterb @@ -1 +1 @@ -Subproject commit 09f1838579eff1dcbefd55e9907ab0b60f50e6a0 +Subproject commit f5224735a2ff8e7e41b4cc3442010a0899a6d106 diff --git a/src/fenstim.nim b/src/fenstim.nim index 8bc269a..f547cf1 100644 --- a/src/fenstim.nim +++ b/src/fenstim.nim @@ -18,6 +18,8 @@ type modkey*: cint x*: cint y*: cint + mclick: array[5, cint] + mhold: array[3, cint] Fenster* = object raw: ptr FensterStruct @@ -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) = @@ -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))