Skip to content

Commit 9d9452c

Browse files
committed
Add scrollable
scrollable, scroll and scrollSpeed attributes addded to Node, and corresponding setters minimal_scroll.nim added in examples
1 parent 8aeb078 commit 9d9452c

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## This minimal example shows 5 blue squares in a scroll box
2+
3+
import fidget
4+
5+
proc drawMain() =
6+
frame "main":
7+
box 0, 0, 620, 140
8+
frame "scrollBox":
9+
box 5, 5, 610, 130
10+
stroke "#000"
11+
strokeWeight 1
12+
clipContent true
13+
scrollable true
14+
fill "#FFF"
15+
for i in 0 .. 4:
16+
group "blockA" & $i:
17+
scrollSpeed (i + 1) * 2 - 6
18+
box 15 + (float i) * 120, 15, 100, 100
19+
fill "#2B9FEA"
20+
21+
startFidget(drawMain, w = 620, h = 140)

src/fidget.nim

+22
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ proc box*(x, y, w, h: float32) =
280280
current.box.w = w
281281
current.box.h = h
282282

283+
## Apply scrolling
284+
if not parent.isNil:
285+
if parent.scrollable:
286+
current.box.y += parent.scroll.y * current.scrollSpeed
287+
283288
proc box*(
284289
x: int|float32|float64,
285290
y: int|float32|float64,
@@ -358,6 +363,23 @@ proc scrollBars*(scrollBars: bool) =
358363
## Causes the parent to clip the children and draw scroll bars.
359364
current.scrollBars = scrollBars
360365

366+
proc scrollable*(scrollable: bool) =
367+
## Causes the parent to let scroll its children.
368+
current.scrollable = scrollable
369+
370+
## Accumulate scroll value
371+
if current.scrollable:
372+
onHover:
373+
current.scroll.y += mouse.wheelDelta
374+
375+
proc scrollSpeed*(scrollSpeed: float) =
376+
## Sets the speed at which a child is scolled inside its parent's box
377+
current.scrollSpeed = scrollSpeed
378+
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
382+
361383
proc cursorColor*(color: Color) =
362384
## Sets the color of the text cursor.
363385
current.cursorColor = color

src/fidget/common.nim

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ type
142142
textLayoutWidth*: float32
143143
## Can the text be selected.
144144
selectable*: bool
145+
scrollable*: bool
146+
scroll*: Vec2
147+
scrollSpeed*: float
145148
scrollBars*: bool ## Should it have scroll bars if children are clipped.
146149

147150
KeyState* = enum
@@ -320,6 +323,7 @@ proc resetToDefault*(node: Node)=
320323
node.clipContent = false
321324
node.diffIndex = 0
322325
node.selectable = false
326+
node.scrollSpeed = 1
323327

324328
proc setupRoot*() =
325329
if root == nil:

0 commit comments

Comments
 (0)