Repository navigation
feat: Width and Height, an object's drawn size - #22
Merged
Merged
Conversation
o.Width() and o.Height() read the size an object is drawn and clicked in: its Size, a Rect's size, a sprite's image once it is in a scene, or a text as measured with its fonts (fallbacks included), TextSize, the Wrap column and LineHeight, plus a button's plate. Never the Hitbox. Text is already measured on every change, so both are current right after SetText, Font, TextSize, Wrap or LineHeight, in the same frame, headless and in go test with no window. Games can lay labels out by their edges and test that every translation fits its box instead of guessing len(s)*size, which is wrong for Cyrillic, CJK and fallbacks. Restart now re-measures a restored text with the font, size and wrap it has, so a text restyled after the scene's setup reports (and draws in) the right box after Restart. The dialog example anchors the speaker's name at the speech's left edge and the CLICK hint at the frame's right edge by their Width. The README gains a cheat-sheet row and contract item 13.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
o.Width() float64ando.Height() float64: the size an object is drawn and clicked in.Sizewhen set; else a Rect's size, a sprite's image size (once the object is in a scene), or a text's measured size: its fonts with fallbacks,TextSize, theWrapcolumn, linesLineHeightapart, plus a button's plate margins.Hitbox, which only affects collisions.measureTextruns inText,SetText,Font,TextSize,Wrap,LineHeight), so both getters are current in the same frame, headless and ingo test, with no window and nothing drawn.Games no longer have to guess
len(s)*size, which is wrong for multi-byte and fallback-font text. They can lay out labels by their edges and test that every translation fits its box.Design choices
.Size(w, h)is already the setter.Width()/Height()match the existingg.Width()/g.Height(), and a single value can be used inline:label.X = left + label.Width()/2. A(w, h)pair can't be used that way.bounds()), not the collisionbox(). Layout and fit checks need what the player sees. Agents keep getting the hitbox in observations.Measure(s)helper.engine.Text(s).Font(...).TextSize(n).Wrap(w).Height()already measures a string with no game or scene. A text object is cheap, and this form takes exactly the same styling calls the real label uses. A separate helper would copy that styling as parameters.restoreStatenow re-measures a restored text with the font, size and wrap it has now. Before, a text restyled after setup came back with its old box and was drawn in it. The new test fails without this fix.Proof
size_test.go:SizeandHitbox, and a sized text area.SetText(measured per glyph, not per byte),TextSize,Font, and a button.testdata/fallback.ttf: Latin-only text, all-CJK text, and a mixed line.LineHeight(40), and"\n"lines.SetTextthenWidthinsideOnUpdate, anchoring by the left edge in that same frame, checked against the observation, then Restart.Add, with and withoutSize.examples/dialog: the speaker's name is placed at the speech's left edge usingWidthafter eachSetText, which replaces the Wrap-column workaround. The CLICK hint's right edge is placed at x 770. Over MCP, English lays out pixel-identically to before (ELDER at x 250 w 120, CLICK at 740 w 60), so demo.gif stays valid. zh-CN gives 长老 with its left edge at 190 and 点击 with its right edge at 770.