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 16, 2024
1 parent b4e0504 commit 50a0f58
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ while app.loop:
break
# Get mouse position and click state
let (mouseX, mouseY, mouseClick) = app.mouse
if mouseClick == 1:
echo "Clicked at: ", mouseX, "x", mouseY
if app.mouse.mclick[0] == 1:
echo "Clicked at: ", app.mouse.pos.x, "x", app.mouse.pos.y
# Adjust FPS
app.targetFps = 30
Expand Down Expand Up @@ -101,11 +100,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[pos: tuple[x, y: int], mclick: array[5, cint], mhold: array[3, cint]]
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.pos
if (mouseX, mouseY) != oldpos:
oldpos = (mouseX, mouseY)
cx = mouseX.float32 / app.width.float32 * 4 - 2
Expand Down
10 changes: 10 additions & 0 deletions examples/mouse_buttons.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fenstim, strutils

var
app = init(Fenster, "Mouse Buttons test", 800, 600)
oldmouse = app.mouse

while app.loop and app.keys[27] == 0:
if oldmouse != app.mouse:
oldmouse = app.mouse
echo app.mouse
8 changes: 3 additions & 5 deletions examples/simple_mouse_drawing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ while app.loop and app.keys[27] == 0:
if app.keys[ord('W')] == 1:
currentColor = rand(uint32)

let (mouseX, mouseY, mouseDown) = 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 app.mouse.mhold[0] == 1:
let x = clamp(app.mouse.pos.x, 0, app.width)
let y = clamp(app.mouse.pos.y, 0, app.height)
app.pixel(x, y) = currentColor
10 changes: 8 additions & 2 deletions src/fenstim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type
modkey*: cint
x*: cint
y*: cint
mouse*: cint
mclick: array[5, cint]
mhold: array[3, cint]

Fenster* = object
raw: ptr FensterStruct
Expand Down Expand Up @@ -79,8 +80,13 @@ 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
template mouse*(self: Fenster): tuple[pos: tuple[x, y: int], mclick: array[5, cint], mhold: array[3, cint]] =
(
pos: (x: self.raw.x.int, y: self.raw.y.int),
mclick: self.raw.mclick,
mhold: self.raw.mhold
)
proc sleep*(self: Fenster, ms: int) = fenster_sleep(ms.cint)
proc time*(self: Fenster): int64 = fenster_time()

Expand Down

0 comments on commit 50a0f58

Please sign in to comment.