-
Notifications
You must be signed in to change notification settings - Fork 258
Add custom text truncation support, and improve scoreboard legibility #7105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
31ad501
f002a2a
16066fb
5eb1c4d
3653d26
eb39955
7e707b6
a6c3eca
a49fc59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| - (#7105) Improve scoreboard legibility and internal text functions for mods/devs | ||
|
|
||
| #### UI | ||
|
|
||
| Improve scoreboard legibility with better alignment, tooltips, dropshadows, and text cropping. | ||
|
|
||
| #### Modding/development | ||
|
|
||
| added SetTruncationText() to assign custom trailing characters like "..." when text is cropped | ||
|
|
||
| split SetText() into **SetText()** and its implicitly called **SetDisplayText()**. This allows for fancier text implementations like custom truncation. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,38 @@ Text = ClassUI(moho.text_methods, Control) { | |
| self._color.OnDirty = function(var) | ||
| self:SetNewColor(var()) | ||
| end | ||
| self._truncationText = "" | ||
| self._fullText = nil | ||
|
|
||
|
|
||
| --- Direct Engine SetText() that changes what text is displayed | ||
| ---@type function | ||
| ---@param str string | number | ||
| self.SetDisplayText = self.SetText | ||
|
|
||
|
|
||
| --- FAF extensible SetText() that uses SetDisplayText() but can retain it's original text for fancy text display setups like truncation | ||
| ---@param text string | number | ||
| self.SetText = function (self, text) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each Text instance has this field, not its meta. |
||
| self.SetDisplayText(self,text) | ||
| self._fullText = text | ||
| if self._truncationText ~= nil and self._truncationText ~= "" then | ||
| if self._initialized then | ||
| self:_applyTruncation() | ||
| end | ||
| end | ||
| end | ||
|
|
||
| --- Direct Engine GetText() for getting the current displayed value | ||
| ---@type function | ||
| ---@return string | ||
| self.GetDisplayText = self.GetText | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each Text instance has this field, not its meta. |
||
|
|
||
| --- FAF extensible GetText() that retrieves raw original text that isn't modified for display | ||
| ---@return string | ||
| self.GetText = function (self) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each Text instance has this field, not its meta. |
||
| return self._fullText | ||
| end | ||
|
Comment on lines
+36
to
+67
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make a separate class for Truncatable text. You are polluting Text class with functionality used by scoreboard only.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are really 2 changes here. A text rendering feature (better truncation), and a text architecture change (display text) which the truncation relies on. 1. Better Text TruncationIt's only used by the scoreboard because it didn't exist to be used for other things yet. Text can already be truncated, but this simply allows the option for setting how it truncates it. You may want a dash, dots, or warning text (for texts that shouldn't be truncated but would be hard to notice if cut off at the right spot). It's more of a small extension to SetClipToWidth() than a new special text type. SetTruncationText() is just setting the trailing characters, if there was any confusion there. Maybe SetTrailingCharacters() would be better? 2. SetText() and SetDisplayText()The separation of the full text and the "displayed text" sent to the engine, is much more useful than just supporting truncation text. It allows much more elaborate text animation/visual effects for modding and development because it's no longer on you to keep track of the old state. Plus anything that tries to GetText() will get it's real value instead of the partial text effect value that may have been setup. GetDisplayText() would be used for getting what the engine sees. Hopefully that makes sense. Thanks for the review!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I get a response by the Human being? Still i insist on creating a separate class.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I typed all that myself, dome to keyboard :( The last line "Hopefully that makes sense." was about my writing, not some AI garbage. I try to include a thanks when I can because this is volunteer work, and programming feedback tends to be heavy on critique so the "thanks" is as much for you as it is for me (to not take it personally). Aaaanyway, yeah, you're right about the function instancing. That modding override trick is great for single function rewrites, but I understand avoiding it here. How about this solution in the body rather than inside __init? That would prevent all the instancing right? ---@class Text : moho.text_methods, Control, InternalObject
Text = ClassUI(...)
__init()..
--- Direct Engine SetText() that changes what text is displayed
---@type function
---@type fun(self: Text, str: string | number)
SetDisplayText = moho.text_methods.SetText,
--- Direct Engine GetText() for getting the current displayed value
---@type function
---@return string
GetDisplayText = moho.text_methods.GetText,
--- FAF extensible SetText() that uses SetDisplayText() but can retain it's original text for fancy text display setups like truncation
---@param text string | number
SetText = function(self, text)
self:SetDisplayText(text)
self._fullText = text
if self._truncationText ~= nil and self._truncationText ~= "" then
if self._initialized then
self:_applyTruncation()
end
end
end,
--- FAF extensible GetText() that retrieves raw original text that isn't modified for display
---@return string
GetText = function(self)
return self._fullText
end,
--- Sets custom truncation trailing characters like "..." or "-". Set to "" to disable.
---@param text string | number
SetTruncationText = function(...)...
Also, should I keep it as "truncation text" or call it "trailing characters" instead, for clarity?
Claude.ai showed me I could use "moho.text_methods.SetText" instead of "self.SetText()" Although it defined local variables outside the class to reference it first and then assign the Display functions in the __init(), but I found that wasn't necessary. (Hand written reply)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry if I offended you. I just don't like when people drop a lot of text especially made with ai not even understanding what's it about. As I said, you are modifying class that is used everywhere in the code and any side effects are very unwanted. Please just create a separate class that inherits from Text. Like TruncatableText and instantiate it in scoreboard. It will make it easier to control scope of the change and will be easier to maintain. |
||
| end, | ||
|
|
||
| OnInit = function(self) | ||
|
|
@@ -41,15 +73,58 @@ Text = ClassUI(moho.text_methods, Control) { | |
| self:SetClipToWidth(false) | ||
| end, | ||
|
|
||
| --- Sets custom truncation trailing characters like "..." or "-". Set to "" to disable. | ||
| ---@param text string | number | ||
| SetTruncationText = function (self, text) | ||
| self._truncationText = tostring(text) | ||
| if self._fullText ~= nil and self._initialized then | ||
| self:_applyTruncation() | ||
| end | ||
| end, | ||
|
|
||
| SetClipToWidth = function(self, clipToWidth) | ||
| if clipToWidth then | ||
| self.Width:Set(function() return self.Right() - self.Left() end) | ||
|
|
||
| self.Width.OnDirty = function() | ||
| if self._truncationText ~= nil and self._truncationText ~= "" then | ||
| self:_applyTruncation() | ||
| end | ||
| end | ||
| else | ||
| self.Width:Set(function() return math.floor(self.TextAdvance()) end) | ||
| self.Width.OnDirty = nil | ||
| end | ||
| self:SetNewClipToWidth(clipToWidth) | ||
| end, | ||
|
|
||
| --- Internal function to fit the truncation string inside the max width | ||
| _applyTruncation = function(self) | ||
| local maxWidth = self.Width() | ||
| if maxWidth <= 0 then | ||
| return | ||
| end | ||
|
|
||
| -- ellipsis is the trailing '...' on truncated text | ||
| local ellipsis = self._truncationText | ||
| local str = self._fullText | ||
| if str == nil then return end | ||
| -- restore full text if it now fits | ||
| if self:GetStringAdvance(str) <= maxWidth then | ||
| self.SetDisplayText(self, str) | ||
| return | ||
| end | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| --iterate until string + ellipsis fit | ||
| local i = STR_Utf8Len(str) | ||
| while i > 0 and self:GetStringAdvance(str .. ellipsis) > maxWidth do | ||
| str = STR_Utf8SubString(str, 1, i - 1) | ||
| i = i - 1 | ||
| end | ||
|
|
||
| self.SetDisplayText(self, str .. ellipsis) | ||
| end, | ||
|
|
||
| -- lazy var support | ||
| SetFont = function(self, family, pointsize) | ||
| if self._font then | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each Text instance has this field, not its meta.