Skip to content

Commit

Permalink
draw2D, easy: use Origin for origin fields for text also
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon committed Dec 16, 2023
1 parent a011e0f commit bf3c6d7
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 54 deletions.
8 changes: 4 additions & 4 deletions easy/easy.api.v
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub mut:
rotation f32
scale f32 = 1.0
text string
size f32 = shy.defaults.font.size
origin shy.Anchor
size f32 = shy.defaults.font.size
origin shy.Origin = shy.Anchor.top_left
align shy.TextAlign = .baseline | .left
offset vec.Vec2[f32]
color shy.Color = shy.rgba(255, 255, 255, 255) // BUG shy.defaults.font.color
Expand All @@ -74,8 +74,8 @@ pub mut:
rotation f32
scale f32 = 1.0
text string
size f32 = shy.defaults.font.size
origin shy.Anchor
size f32 = shy.defaults.font.size
origin shy.Origin = shy.Anchor.top_left
align shy.TextAlign = .baseline | .left
offset vec.Vec2[f32]
color shy.Color = shy.defaults.font.color
Expand Down
2 changes: 1 addition & 1 deletion examples/anchors/anchors.v
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn (mut a App) frame(dt f64) {
a.quick.text(
x: shy.half * a.window.width
y: shy.half * a.window.height
origin: .bottom_center
origin: shy.Anchor.bottom_center
// align: .left | .bottom
offset: vec.vec2[f32](0, -50 - 20)
text: 'Current draw anchor:\n${a.anchor}'
Expand Down
2 changes: 1 addition & 1 deletion examples/anchors/triangle_anchors.v
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn (mut a App) frame(dt f64) {
a.quick.text(
x: a.window.width * 0.01
y: a.window.height * (1.0 - 0.01)
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: 'Click mouse left/right to change transform origin
Origin: ${a.origin}
Rotation: ${rotation * shy.rad2deg:.1f}°'
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/animators.v
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn (mut a App) frame(dt f64) {
a.quick.text(
x: a.window.width * 0.01
y: a.window.height * (1.0 - 0.01)
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: 'Animation running for ${es:.1f} seconds
Frame: ${win.state.frame}
follow at ${a.fa_x.value():.1f},${a.fa_y.value():.1f} running: ${a.fa_x.running}
Expand Down
2 changes: 1 addition & 1 deletion examples/mouse/mouse.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ pub fn (mut a App) frame(dt f64) {
x: mx + 20
y: my + 20
text: 'Shy Mouse at ${mx},${my}\nButtons pressed: ${buttons}'
origin: .top_left
origin: shy.Anchor.top_left
)
}
2 changes: 1 addition & 1 deletion examples/record_and_replay/record_and_replay.v
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ rect.y ${a.fa_y.value}'
a.quick.text(
x: a.window.width * 0.01
y: a.window.height * 0.01
origin: .top_left
origin: shy.Anchor.top_left
text: text
)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/render_step/render_step.v
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn (mut a App) frame(dt f64) {
a.quick.text(
x: a.window.width * 0.01
y: a.window.height * (1.0 - 0.01)
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: 'Press "s" to step 135 frames, hold "s+shift" to go out of step mode.
Mode: ${win.mode}
Frame: ${win.state.frame}
Expand Down
2 changes: 1 addition & 1 deletion examples/render_step/ui_and_manual_mode.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn (mut a App) frame(dt f64) {
a.quick.text(
x: a.window.width * 0.01
y: a.window.height * (1.0 - 0.01)
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: '${text_manual_render}
Press "m" to toggle window mode.
Press "a" to set new animator.
Expand Down
6 changes: 3 additions & 3 deletions examples/timers/timers.v
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ pub fn (mut a App) init() ! {
x: shy.half * size.width
y: shy.half * size.height
size: 50
origin: .center
origin: shy.Anchor.center
align: .center
)

a.info_text = a.easy.new_text(
x: shy.half * size.width
y: 10
origin: .top_center
origin: shy.Anchor.top_center
align: .center
text: info
)

a.clock_text = a.easy.new_text(
y: size.height
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: 'Local time: ${time.now().format_ss()}'
)

Expand Down
82 changes: 48 additions & 34 deletions lib/draw2d.text.b.v
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ mut:
pub mut:
text string
rotation f32
font string = defaults.font.name
color Color = defaults.font.color
origin Anchor
font string = defaults.font.name
color Color = defaults.font.color
origin Origin = Anchor.top_left
align TextAlign = .baseline | .left // TODO V BUG lib.defaults.font.align
size f32 = defaults.font.size
scale f32 = 1.0
Expand All @@ -140,6 +140,45 @@ pub mut:
blur f32
}

/*
fn calculate_text_offset_from_anchor(anchor Anchor, max_w f32, max_h f32) ( f32, f32) {
mut off_x := f32(0)
mut off_y := f32(0)
match anchor {
.top_left {}
.top_center {
off_x = -(max_w / 2)
}
.top_right {
off_x = -max_w
}
.center_left {
off_y = -(max_h / 2)
}
.center {
off_x = -(max_w / 2)
off_y = -(max_h / 2)
}
.center_right {
off_x = -max_w
off_y = -(max_h / 2)
}
.bottom_left {
off_y = -max_h
}
.bottom_center {
off_x = -(max_w / 2)
off_y = -max_h
}
.bottom_right {
off_x = -max_w
off_y = -max_h
}
}
return off_x, off_y
}
*/

@[inline]
pub fn (t Draw2DText) draw() {
// http://www.freetype.org/freetype2/docs/tutorial/metrics.png
Expand Down Expand Up @@ -243,37 +282,12 @@ pub fn (t Draw2DText) draw() {
0
}
off_x += align_off_x
match t.origin {
.top_left {}
.top_center {
off_x += -(max_w / 2)
}
.top_right {
off_x += -max_w
}
.center_left {
off_y += -(max_h / 2)
}
.center {
off_x += -(max_w / 2)
off_y += -(max_h / 2)
}
.center_right {
off_x += -max_w
off_y += -(max_h / 2)
}
.bottom_left {
off_y += -max_h
}
.bottom_center {
off_x += -(max_w / 2)
off_y += -max_h
}
.bottom_right {
off_x += -max_w
off_y += -max_h
}
}

o_off_x, o_off_y := t.origin.pos_wh(max_w, max_h)

off_x += -o_off_x
off_y += -o_off_y

y_accu += prev_line_height
prev_line_height = line_height - 2 // lines.len

Expand Down
4 changes: 2 additions & 2 deletions tests/visual/easy_mixed_drawing.v
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn (mut a App) frame(dt f64) {
x: win_w
y: win_h
text: 'Shy Test!'
origin: .bottom_right
origin: shy.Anchor.bottom_right
)

a.quick.line_segment(
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn (mut a App) frame(dt f64) {
x: (win_w * 0.25)
y: (win_h * 0.75)
text: 'Shy'
origin: .center
origin: shy.Anchor.center
)

a.quick.rect(
Expand Down
2 changes: 1 addition & 1 deletion tests/visual/image_fill_modes.v
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn (mut a App) live_frame(dt f64) {
y: a.canvas().height
// width: rw
// height: rh
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: 'Image (n/m): ${images[a.image]}
Origin (up/down): ${a.origin}
FillMode (left/right): ${a.fill_mode}'
Expand Down
2 changes: 1 addition & 1 deletion tests/visual/rectangle_rounded.v
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn (mut a App) live_frame(dt f64) {

a.quick.text(
y: a.canvas().height
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: 'Press "D" to change dimensions
Origin (up/down): ${a.origin}
Dimensions ${a.dimension}x${a.dimension}'
Expand Down
4 changes: 2 additions & 2 deletions tests/visual/triangles.v
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn (mut a App) frame(dt f64) {
a: center
b: center + shy.vec2(f32(50), -50) // TODO origin bug
c: center + shy.vec2(f32(100), 0)
origin: .center
origin: shy.Anchor.center
)*/

origin := a.origin // shy.Anchor.top_center
Expand Down Expand Up @@ -131,7 +131,7 @@ pub fn (mut a App) frame(dt f64) {
a.quick.text(
x: a.canvas().width * 0.01
y: a.canvas().height * (1.0 - 0.01)
origin: .bottom_left
origin: shy.Anchor.bottom_left
text: 'Click mouse left/right to change transform origin
Origin: ${a.origin}'
)
Expand Down

0 comments on commit bf3c6d7

Please sign in to comment.