Skip to content

Commit

Permalink
Update FensterB with new mouse function
Browse files Browse the repository at this point in the history
Fix mouse examples
  • Loading branch information
CardealRusso committed Oct 15, 2024
1 parent b4e0504 commit e9d94b0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ while app.loop:
break
# Get mouse position and click state
let (mouseX, mouseY, mouseClick) = app.mouse
if mouseClick == 1:
let (mouseX, mouseY, clickStates, holdStates) = app.mouse
if clickStates[0] == 1:
echo "Clicked at: ", mouseX, "x", mouseY
# Adjust FPS
Expand Down Expand Up @@ -101,11 +101,11 @@ clear*(self: Fenster)
### Input Handling
```nim
keys*(self: Fenster): array[256, cint]
mouse*(self: Fenster): tuple[x, y, click: int]
mouse*(self: Fenster): tuple[x, y: int, click: array[5, int], hold: array[3, int]]
modkey*(self: Fenster): int
```
keys = Array of key states. Index corresponds to ASCII value (0-255), but arrows are 17..20.
mouse = Get mouse position (x, y) and click state.
mouse = Get mouse position (x, y), clicked (left, right, middle, scroll up, scroll down) and holding buttons (left, right, middle)
modkey = 4 bits mask, ctrl=1, shift=2, alt=4, meta=8

# Examples
Expand Down
2 changes: 1 addition & 1 deletion examples/interactive_julia_set.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ proc julia(x, y, cx, cy: float32, maxIter: int): int =
return 0

while app.loop and app.keys[27] == 0:
let (mouseX, mouseY, _) = app.mouse
let (mouseX, mouseY, _, _) = app.mouse
if (mouseX, mouseY) != oldpos:
oldpos = (mouseX, mouseY)
cx = mouseX.float32 / app.width.float32 * 4 - 2
Expand Down
28 changes: 28 additions & 0 deletions examples/mouse_buttons.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fenstim

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

while app.loop and app.keys[27] == 0:
let (mouseX, mouseY, clickStates, holdStates) = app.mouse

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
8 changes: 4 additions & 4 deletions examples/simple_mouse_drawing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ while app.loop and app.keys[27] == 0:
if app.keys[ord('W')] == 1:
currentColor = rand(uint32)

let (mouseX, mouseY, mouseDown) = app.mouse
let (mouseX, mouseY, clickStates, holdStates) = app.mouse

if app.mouse[2] == 1:
let x = clamp(app.mouse[0], 0, app.width)
let y = clamp(app.mouse[1], 0, app.height)
if clickStates[0] == 1:
let x = clamp(mouseX, 0, app.width)
let y = clamp(mouseY, 0, app.height)
app.pixel(x, y) = currentColor
13 changes: 11 additions & 2 deletions src/fenstim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type
modkey*: cint
x*: cint
y*: cint
mouse*: cint

Fenster* = object
raw: ptr FensterStruct
Expand All @@ -32,6 +31,7 @@ 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 @@ -79,10 +79,19 @@ template pixel*(self: Fenster, x, y: int): uint32 = self.raw.buf[y * self.raw.wi
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 mouse*(self: Fenster): tuple[x, y, click: int] = (self.raw.x.int, self.raw.y.int, self.raw.mouse.int)
template modkey*(self: Fenster): int = self.raw.modkey.int
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 e9d94b0

Please sign in to comment.