17cupsofcoffee / tetra

🎮 A simple 2D game framework written in Rust
MIT License
907 stars 63 forks source link

add draw_align for graphics::Text #344

Closed Chen1Plus closed 6 months ago

Chen1Plus commented 7 months ago

resolve #343

17cupsofcoffee commented 6 months ago

Thanks for picking this up!

I've tested this locally and it seems to work - my only concern is that this approach (aligning as part of drawing, rather than as part of layout) gives you some surprising behaviour with multi-line text:

image

The Text as a whole is centered, but each line is still left-aligned.

If possible, I think it would be better to do the centering as part of the layout (which lives in src/graphics/text/cache.rs). The way I've seen this done in other libraries is:

This should make multi-line text work as you'd expect, and it also has the nice side effect of making it so Text::get_bounds gives you bounds that take into account alignment.

The only slightly annoying thing about this approach is that you can't re-use the same Text for multiple alignments (at least not without repeatng the layout, like we do when you change the content/font).

Let me know what you think - maybe I'm overthinking this 😄

Chen1Plus commented 6 months ago

I agree with you. It would be a better solution. However, I've been a bit busy lately, so I might need some more time to open next PR for this.

17cupsofcoffee commented 6 months ago

I agree with you. It would be a better solution. However, I've been a bit busy lately, so I might need some more time to open next PR for this.

That's no problem, appreciate the work you've done up to this point!