Skip to content

Commit a29ca4f

Browse files
committed
Fix center center offset.
1 parent b4e0137 commit a29ca4f

File tree

10 files changed

+129
-26
lines changed

10 files changed

+129
-26
lines changed

src/fidget.nim

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ proc image*(imageName: string) =
261261
## Sets image fill.
262262
current.imageName = imageName
263263

264+
proc orgBox*(x, y, w, h: int|float32|float32) =
265+
## Sets the box dimensions of the original element for constraints.
266+
current.orgBox.x = float32 x
267+
current.orgBox.y = float32 y
268+
current.orgBox.w = float32 w
269+
current.orgBox.h = float32 h
270+
264271
proc box*(x, y, w, h: float32) =
265272
## Sets the box dimensions.
266273
current.box.x = x
@@ -275,19 +282,14 @@ proc box*(
275282
h: int|float32|float64
276283
) =
277284
## Sets the box dimensions with integers
285+
## Always set box before orgBox when doing constraints.
278286
box(float32 x, float32 y, float32 w, float32 h)
287+
orgBox(float32 x, float32 y, float32 w, float32 h)
279288

280289
proc box*(rect: Rect) =
281290
## Sets the box dimensions with integers
282291
box(rect.x, rect.y, rect.w, rect.h)
283292

284-
proc orgBox*(x, y, w, h: int|float32|float32) =
285-
## Sets the box dimensions of the original element for constraints.
286-
current.orgBox.x = float32 x
287-
current.orgBox.y = float32 y
288-
current.orgBox.w = float32 w
289-
current.orgBox.h = float32 h
290-
291293
proc rotation*(rotationInDeg: float32) =
292294
## Sets rotation in degrees.
293295
current.rotation = rotationInDeg

src/fidget/common.nim

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,29 @@ proc consume*(keyboard: Keyboard) =
365365
proc consume*(mouse: Mouse) =
366366
## Reset the mouse state consuming any event information.
367367
buttonPress[MOUSE_LEFT] = false
368-
368+
import print
369369
proc computeLayout*(parent, node: Node) =
370370
## Computes constraints and auto-layout.
371371
for n in node.nodes:
372372
computeLayout(node, n)
373373
374+
# Typeset text
375+
if node.kind == nkText:
376+
computeTextLayout(node)
377+
print node.textLayoutWidth
378+
print node.textLayoutHeight
379+
case node.textStyle.autoResize:
380+
of tsNone:
381+
# Fixed sized text node.
382+
discard
383+
of tsHeight:
384+
# Text will grow down.
385+
node.box.h = node.textLayoutHeight
386+
of tsWidthAndHeight:
387+
# Text will grow down and wide.
388+
node.box.w = node.textLayoutWidth
389+
node.box.h = node.textLayoutHeight
390+
374391
# Constraints code.
375392
case node.constraintsVertical:
376393
of cMin: discard
@@ -385,7 +402,8 @@ proc computeLayout*(parent, node: Node) =
385402
let xDiff = parent.box.w - parent.orgBox.w
386403
node.box.w += xDiff
387404
of cCenter:
388-
node.box.x = floor((parent.box.w - node.box.w) / 2.0)
405+
let offset = floor((node.orgBox.w - parent.orgBox.w) / 2.0 + node.orgBox.x)
406+
node.box.x = floor((parent.box.w - node.box.w) / 2.0) + offset
389407
390408
case node.constraintsHorizontal:
391409
of cMin: discard
@@ -400,23 +418,8 @@ proc computeLayout*(parent, node: Node) =
400418
let yDiff = parent.box.h - parent.orgBox.h
401419
node.box.h += yDiff
402420
of cCenter:
403-
node.box.y = floor((parent.box.h - node.box.h) / 2.0)
404-
405-
# Typeset text
406-
if node.kind == nkText:
407-
computeTextLayout(node)
408-
409-
case node.textStyle.autoResize:
410-
of tsNone:
411-
# Fixed sized text node.
412-
discard
413-
of tsHeight:
414-
# Text will grow down.
415-
node.box.h = node.textLayoutHeight
416-
of tsWidthAndHeight:
417-
# Text will grow down and wide.
418-
node.box.w = node.textLayoutWidth
419-
node.box.h = node.textLayoutHeight
421+
let offset = floor((node.orgBox.h - parent.orgBox.h) / 2.0 + node.orgBox.y)
422+
node.box.y = floor((parent.box.h - node.box.h) / 2.0) + offset
420423
421424
# Auto-layout code.
422425
if node.layoutMode == lmVertical:

src/fidget/opengl/context.nim

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,45 @@ proc strokeRoundedRect*(
558558
color
559559
)
560560

561+
proc line*(
562+
ctx: Context, a: Vec2, b: Vec2, color: Color
563+
) =
564+
let hash = hash((
565+
2345,
566+
a,
567+
b
568+
))
569+
570+
let
571+
w = ceil(abs(b.x - a.x)).int
572+
h = ceil(abs(a.y - b.y)).int
573+
pos = vec2(min(a.x, b.x), min(a.y, b.y))
574+
575+
if hash notin ctx.entries:
576+
var image = newImage(w, h, 4)
577+
image.fill(rgba(255, 255, 255, 0))
578+
image.line(
579+
a-pos, b-pos,
580+
rgba(255, 255, 255, 255)
581+
)
582+
ctx.putImage(hash, image)
583+
let
584+
uvRect = ctx.entries[hash]
585+
wh = vec2(w.float32, h.float32) * ctx.atlasSize.float32
586+
ctx.drawUvRect(
587+
pos,
588+
pos + vec2(w.float32, h.float32),
589+
uvRect.xy,
590+
uvRect.xy + uvRect.wh,
591+
color
592+
)
593+
594+
proc linePolygon*(
595+
ctx: Context, poly: seq[Vec2], color: Color
596+
) =
597+
for i in 0 ..< poly.len:
598+
ctx.line(poly[i], poly[(i+1) mod poly.len], color)
599+
561600
proc clearMask*(ctx: Context) =
562601
## Sets mask off (actually fills the mask with white).
563602
assert ctx.frameBegun == true, "ctx.beginFrame has not been called."
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Shows a text pad like program.
2+
3+
import fidget
4+
5+
loadFont("IBM Plex Sans", "IBMPlexSans-Regular.ttf")
6+
7+
setTitle("cCenter and vCenter text")
8+
9+
proc drawMain() =
10+
11+
frame "phoneLike":
12+
box 0, 0, parent.box.w, parent.box.h
13+
orgBox 0, 0, 375, 667
14+
15+
fill "#ffffff"
16+
17+
text "Some Test X":
18+
box 55, 81, 266, 62
19+
constraints cCenter, cCenter
20+
fill "#000000"
21+
font "IBM Plex Sans", 48, 400, 48, hLeft, vCenter
22+
characters "Some Test X"
23+
textAutoResize tsWidthAndHeight
24+
25+
rectangle "box":
26+
box 55, 81, 266, 62
27+
constraints cCenter, cCenter
28+
fill "#ff7b7b"
29+
30+
startFidget(drawMain, w = 375, h = 667)
Binary file not shown.

tests/centercentertext/screenshot.png

7.53 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fidget
2+
3+
setTitle("Constraints Center Offset")
4+
5+
proc drawMain() =
6+
frame "constraints":
7+
orgBox 0, 0, 500, 500
8+
box 0, 0, parent.box.w, parent.box.h
9+
fill "#ffffff"
10+
rectangle "Rectangle 5":
11+
box 300, 100, 100, 100
12+
constraints cCenter, cCenter
13+
fill "#d8fbbd"
14+
rectangle "Rectangle 4":
15+
box 300, 300, 100, 100
16+
constraints cCenter, cCenter
17+
fill "#ff7b7b"
18+
rectangle "Rectangle 3":
19+
box 100, 300, 100, 100
20+
constraints cCenter, cCenter
21+
fill "#ffb48a"
22+
rectangle "Rectangle 2":
23+
box 100, 100, 100, 100
24+
constraints cCenter, cCenter
25+
fill "#abf1e5"
26+
27+
startFidget(drawMain, w = 500, h = 500)
443 Bytes
Loading

tests/pixelscaling/screenshot.png

62 Bytes
Loading

tests/run.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ proc main(
126126
# runList.add "tests/imagegen"
127127
# runList.add "tests/imagestatic"
128128

129+
runList.add "tests/centercentertext"
130+
runList.add "tests/constrantscenteroffset"
129131
runList.add "tests/autolayouttext"
130132
runList.add "tests/autolayoutcomplex"
131133
runList.add "tests/autolayouthorizontal"

0 commit comments

Comments
 (0)