Skip to content

Commit aa3cc7a

Browse files
committed
Merge branch 'master' of github.com:treeform/fidget
2 parents 673bc41 + 5ecc517 commit aa3cc7a

File tree

12 files changed

+151
-19
lines changed

12 files changed

+151
-19
lines changed

fidget.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ srcDir = "src"
1010

1111
requires "nim >= 1.0.0"
1212
requires "chroma >= 0.1.3"
13-
requires "typography >= 0.3.3"
13+
requires "typography >= 0.4.0"
1414
requires "flippy >= 0.4.4"
1515
requires "vmath >= 0.3.1"
1616
requires "print >= 0.1.0"

src/fidget.nim

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

269+
proc orgBox*(x, y, w, h: int|float32|float32) =
270+
## Sets the box dimensions of the original element for constraints.
271+
current.orgBox.x = float32 x
272+
current.orgBox.y = float32 y
273+
current.orgBox.w = float32 w
274+
current.orgBox.h = float32 h
275+
269276
proc box*(x, y, w, h: float32) =
270277
## Sets the box dimensions.
271278
current.box.x = x
@@ -280,19 +287,14 @@ proc box*(
280287
h: int|float32|float64
281288
) =
282289
## Sets the box dimensions with integers
290+
## Always set box before orgBox when doing constraints.
283291
box(float32 x, float32 y, float32 w, float32 h)
292+
orgBox(float32 x, float32 y, float32 w, float32 h)
284293

285294
proc box*(rect: Rect) =
286295
## Sets the box dimensions with integers
287296
box(rect.x, rect.y, rect.w, rect.h)
288297

289-
proc orgBox*(x, y, w, h: int|float32|float32) =
290-
## Sets the box dimensions of the original element for constraints.
291-
current.orgBox.x = float32 x
292-
current.orgBox.y = float32 y
293-
current.orgBox.w = float32 w
294-
current.orgBox.h = float32 h
295-
296298
proc rotation*(rotationInDeg: float32) =
297299
## Sets rotation in degrees.
298300
current.rotation = rotationInDeg

src/fidget/common.nim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ proc computeLayout*(parent, node: Node) =
385385
let xDiff = parent.box.w - parent.orgBox.w
386386
node.box.w += xDiff
387387
of cCenter:
388-
node.box.x = floor((parent.box.w - node.box.w) / 2.0)
388+
let offset = floor((node.orgBox.w - parent.orgBox.w) / 2.0 + node.orgBox.x)
389+
node.box.x = floor((parent.box.w - node.box.w) / 2.0) + offset
389390
390391
case node.constraintsHorizontal:
391392
of cMin: discard
@@ -400,12 +401,12 @@ proc computeLayout*(parent, node: Node) =
400401
let yDiff = parent.box.h - parent.orgBox.h
401402
node.box.h += yDiff
402403
of cCenter:
403-
node.box.y = floor((parent.box.h - node.box.h) / 2.0)
404+
let offset = floor((node.orgBox.h - parent.orgBox.h) / 2.0 + node.orgBox.y)
405+
node.box.y = floor((parent.box.h - node.box.h) / 2.0) + offset
404406
405407
# Typeset text
406408
if node.kind == nkText:
407409
computeTextLayout(node)
408-
409410
case node.textStyle.autoResize:
410411
of tsNone:
411412
# Fixed sized text node.

src/fidget/opengl/context.nim

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,35 @@ proc setVertColor(buf: var seq[uint8], i: int, color: ColorRGBA) =
335335
buf[i * 4 + 2] = color.b
336336
buf[i * 4 + 3] = color.a
337337

338-
func `*`(m: Mat4, v: Vec2): Vec2 =
338+
func `*`*(m: Mat4, v: Vec2): Vec2 =
339339
(m * vec3(v, 0.0)).xy
340340

341+
proc drawQuad*(
342+
ctx: Context,
343+
verts: array[4, Vec2],
344+
uvs: array[4, Vec2],
345+
colors: array[4, ColorRGBA],
346+
) =
347+
ctx.checkBatch()
348+
349+
let offset = ctx.quadCount * 4
350+
ctx.positions.data.setVert2(offset + 0, verts[0])
351+
ctx.positions.data.setVert2(offset + 1, verts[1])
352+
ctx.positions.data.setVert2(offset + 2, verts[2])
353+
ctx.positions.data.setVert2(offset + 3, verts[3])
354+
355+
ctx.uvs.data.setVert2(offset + 0, uvs[0])
356+
ctx.uvs.data.setVert2(offset + 1, uvs[1])
357+
ctx.uvs.data.setVert2(offset + 2, uvs[2])
358+
ctx.uvs.data.setVert2(offset + 3, uvs[3])
359+
360+
ctx.colors.data.setVertColor(offset + 0, colors[0])
361+
ctx.colors.data.setVertColor(offset + 1, colors[1])
362+
ctx.colors.data.setVertColor(offset + 2, colors[2])
363+
ctx.colors.data.setVertColor(offset + 3, colors[3])
364+
365+
inc ctx.quadCount
366+
341367
proc drawUvRect(ctx: Context, at, to: Vec2, uvAt, uvTo: Vec2, color: Color) =
342368
## Adds an image rect with a path to an ctx
343369
ctx.checkBatch()
@@ -558,6 +584,45 @@ proc strokeRoundedRect*(
558584
color
559585
)
560586

587+
proc line*(
588+
ctx: Context, a: Vec2, b: Vec2, color: Color
589+
) =
590+
let hash = hash((
591+
2345,
592+
a,
593+
b
594+
))
595+
596+
let
597+
w = ceil(abs(b.x - a.x)).int
598+
h = ceil(abs(a.y - b.y)).int
599+
pos = vec2(min(a.x, b.x), min(a.y, b.y))
600+
601+
if hash notin ctx.entries:
602+
var image = newImage(w, h, 4)
603+
image.fill(rgba(255, 255, 255, 0))
604+
image.line(
605+
a-pos, b-pos,
606+
rgba(255, 255, 255, 255)
607+
)
608+
ctx.putImage(hash, image)
609+
let
610+
uvRect = ctx.entries[hash]
611+
wh = vec2(w.float32, h.float32) * ctx.atlasSize.float32
612+
ctx.drawUvRect(
613+
pos,
614+
pos + vec2(w.float32, h.float32),
615+
uvRect.xy,
616+
uvRect.xy + uvRect.wh,
617+
color
618+
)
619+
620+
proc linePolygon*(
621+
ctx: Context, poly: seq[Vec2], color: Color
622+
) =
623+
for i in 0 ..< poly.len:
624+
ctx.line(poly[i], poly[(i+1) mod poly.len], color)
625+
561626
proc clearMask*(ctx: Context) =
562627
## Sets mask off (actually fills the mask with white).
563628
assert ctx.frameBegun == true, "ctx.beginFrame has not been called."

src/fidget/openglbackend.nim

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ computeTextLayout = proc(node: Node) =
2727
boundsMax: Vec2
2828
size = node.box.wh
2929
if node.textStyle.autoResize == tsWidthAndHeight:
30-
size = vec2(0, 0)
30+
size.x = 0
3131
node.textLayout = font.typeset(
3232
node.text.toRunes(),
3333
pos = vec2(0, 0),
@@ -51,9 +51,12 @@ proc focus*(keyboard: Keyboard, node: Node) =
5151
keyboard.onFocusNode = node
5252
keyboard.focusNode = node
5353

54+
var font = fonts[node.textStyle.fontFamily]
55+
font.size = node.textStyle.fontSize
56+
font.lineHeight = node.textStyle.lineHeight
5457
keyboard.input = node.text
5558
textBox = newTextBox(
56-
fonts[node.textStyle.fontFamily],
59+
font,
5760
int node.screenBox.w,
5861
int node.screenBox.h,
5962
node.text,
@@ -116,8 +119,8 @@ proc drawText(node: Node) =
116119
let editing = keyboard.focusNode == node
117120

118121
if editing:
119-
if textBox.size != node.box.wh:
120-
textBox.resize(node.box.wh)
122+
if textBox.size != node.screenBox.wh:
123+
textBox.resize(node.screenBox.wh)
121124
node.textLayout = textBox.layout
122125
ctx.saveTransform()
123126
ctx.translate(-textBox.scroll)
@@ -128,7 +131,7 @@ proc drawText(node: Node) =
128131

129132
# draw characters
130133
for glyphIdx, pos in node.textLayout:
131-
if pos.character notin font.glyphs:
134+
if pos.character notin font.typeface.glyphs:
132135
continue
133136
if pos.rune == Rune(32):
134137
# Don't draw space, even if font has a char for it.
@@ -162,7 +165,7 @@ proc drawText(node: Node) =
162165

163166
if hashFill notin ctx.entries:
164167
var
165-
glyph = font.glyphs[pos.character]
168+
glyph = font.typeface.glyphs[pos.character]
166169
glyphOffset: Vec2
167170
let glyphFill = font.getGlyphImage(
168171
glyph,
@@ -174,7 +177,7 @@ proc drawText(node: Node) =
174177

175178
if node.strokeWeight > 0 and hashStroke notin ctx.entries:
176179
var
177-
glyph = font.glyphs[pos.character]
180+
glyph = font.typeface.glyphs[pos.character]
178181
glyphOffset: Vec2
179182
let glyphFill = font.getGlyphImage(
180183
glyph,
@@ -323,6 +326,8 @@ proc setupFidget(
323326

324327
drawMain()
325328

329+
root.removeExtraChildren()
330+
326331
computeLayout(nil, root)
327332
computeScreenBox(nil, root)
328333

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

3.24 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

0 commit comments

Comments
 (0)