Skip to content

Commit e209d06

Browse files
committed
Add horizontal scrolling
Several variables are now Vec2 instead of float (eg Mouse.wheelDelta), or tuple of boolean instead of boolean (eg Node.scrollable) to store both horizontal and vertical attributes
1 parent 9d9452c commit e209d06

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

examples/minimal_scroll/minimal_scroll.nim

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ proc drawMain() =
1010
stroke "#000"
1111
strokeWeight 1
1212
clipContent true
13-
scrollable true
13+
scrollable true, true
1414
fill "#FFF"
1515
for i in 0 .. 4:
1616
group "blockA" & $i:
1717
scrollSpeed (i + 1) * 2 - 6
1818
box 15 + (float i) * 120, 15, 100, 100
1919
fill "#2B9FEA"
20+
stroke "#000"
21+
strokeWeight 1
2022

2123
startFidget(drawMain, w = 620, h = 140)

src/fidget.nim

+27-13
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,12 @@ proc box*(x, y, w, h: float32) =
280280
current.box.w = w
281281
current.box.h = h
282282

283-
## Apply scrolling
283+
## Apply scroll
284284
if not parent.isNil:
285-
if parent.scrollable:
286-
current.box.y += parent.scroll.y * current.scrollSpeed
285+
if parent.scrollable.x:
286+
current.box.x += parent.scroll.x * current.scrollSpeed.x
287+
if parent.scrollable.y:
288+
current.box.y += parent.scroll.y * current.scrollSpeed.y
287289

288290
proc box*(
289291
x: int|float32|float64,
@@ -363,22 +365,34 @@ proc scrollBars*(scrollBars: bool) =
363365
## Causes the parent to clip the children and draw scroll bars.
364366
current.scrollBars = scrollBars
365367

366-
proc scrollable*(scrollable: bool) =
368+
proc scrollable*(value: tuple[x: bool, y:bool]) =
367369
## Causes the parent to let scroll its children.
368-
current.scrollable = scrollable
370+
current.scrollable = value
369371

370372
## Accumulate scroll value
371-
if current.scrollable:
373+
if current.scrollable.x or current.scrollable.y:
372374
onHover:
373-
current.scroll.y += mouse.wheelDelta
375+
current.scroll += mouse.wheelDelta
374376

375-
proc scrollSpeed*(scrollSpeed: float) =
376-
## Sets the speed at which a child is scolled inside its parent's box
377-
current.scrollSpeed = scrollSpeed
377+
proc scrollable*(xValue: bool, yValue: bool) =
378+
## Causes the parent to let scroll its children.
379+
scrollable((x: xValue, y: yValue))
380+
381+
proc scrollable*(yValue: bool) =
382+
## Causes the parent to let scroll its children.
383+
scrollable((x: false, y: yValue))
384+
385+
proc scrollSpeed*(value: Vec2) =
386+
## Sets the speed at which a child is scrolled inside its parent's box
387+
current.scrollSpeed = value
388+
389+
proc scrollSpeed*(value: int|float32|float64) =
390+
## Sets the speed at which a child is scrolled inside its parent's box
391+
scrollSpeed(vec2(float32 value, float32 value))
378392

379-
proc scrollSpeed*(scrollSpeed: int) =
380-
## Sets the speed at which a child is scolled inside its parent's box
381-
current.scrollSpeed = float scrollSpeed
393+
proc scrollSpeed*(xValue, yValue: int|float32|float64) =
394+
## Sets the speed at which a child is scrolled inside its parent's box
395+
scrollSpeed(vec2(float32 xValue, float32 yValue))
382396

383397
proc cursorColor*(color: Color) =
384398
## Sets the color of the text cursor.

src/fidget/common.nim

+5-5
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ type
142142
textLayoutWidth*: float32
143143
## Can the text be selected.
144144
selectable*: bool
145-
scrollable*: bool
145+
scrollable*: tuple[x: bool, y: bool]
146146
scroll*: Vec2
147-
scrollSpeed*: float
147+
scrollSpeed*: Vec2
148148
scrollBars*: bool ## Should it have scroll bars if children are clipped.
149149

150150
KeyState* = enum
@@ -163,7 +163,7 @@ type
163163
Mouse* = ref object
164164
pos*, delta*, prevPos*: Vec2
165165
pixelScale*: float32
166-
wheelDelta*: float32
166+
wheelDelta*: Vec2
167167
cursorStyle*: MouseCursorStyle ## Sets the mouse cursor icon
168168
prevCursorStyle*: MouseCursorStyle
169169

@@ -323,7 +323,7 @@ proc resetToDefault*(node: Node)=
323323
node.clipContent = false
324324
node.diffIndex = 0
325325
node.selectable = false
326-
node.scrollSpeed = 1
326+
node.scrollSpeed = vec2(1, 1)
327327

328328
proc setupRoot*() =
329329
if root == nil:
@@ -339,7 +339,7 @@ proc setupRoot*() =
339339

340340
proc clearInputs*() =
341341

342-
mouse.wheelDelta = 0
342+
mouse.wheelDelta = vec2(0, 0)
343343

344344
# Reset key and mouse press to default state
345345
for i in 0 ..< buttonPress.len:

src/fidget/opengl/base.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ proc onScroll(window: staticglfw.Window, xoffset, yoffset: float64) {.cdecl.} =
274274
if keyboard.focusNode != nil:
275275
textBox.scrollBy(-yoffset * 50)
276276
else:
277-
mouse.wheelDelta += yoffset
277+
mouse.wheelDelta += vec2(xoffset, yoffset)
278278

279279
proc onMouseButton(
280280
window: staticglfw.Window, button, action, modifiers: cint

0 commit comments

Comments
 (0)