Skip to content

Commit

Permalink
Solve #178 Update get_text_width -> get_text_size
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Mar 11, 2022
1 parent 007e715 commit ea5b3dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs_md/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ Have a nice day!
- **#44** [Slider] Click zone on all slider node, not only pin node
- Finally! Added the `slider:set_input_node(node)` function. Now slider can be interacted not only with slider pin node, but with any zone you will define.
- It will work only from Defold 1.3.0. If you use earlier version, nothing is happened. It using new `gui.local_to_node` and `gui.set_screen_position` functions.
- **#178** **[BREAKING][Text]** Rename `text:get_text_width` to `text:get_text_size`. Now it return two numbers: width and height
- **#114** Add default component templates
- Added templates for fast prototyping. Use GUI templates to place buttons, checkbox, input and sliders on your GUI scene. You still have to create component with `druid:new` functions inside gui_script.
- **#168** Add button to open code of current example
Expand Down
18 changes: 11 additions & 7 deletions druid/base/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ end

local function update_text_with_trim(self, trim_postfix)
local max_width = self.text_area.x
local text_width = self:get_text_width()
local text_width = self:get_text_size()

if text_width > max_width then
local text_length = utf8.len(self.last_value)
local new_text = self.last_value
while text_width > max_width do
text_length = text_length - 1
new_text = utf8.sub(self.last_value, 1, text_length)
text_width = self:get_text_width(new_text .. trim_postfix)
text_width = self:get_text_size(new_text .. trim_postfix)
end

gui.set_text(self.node, new_text .. trim_postfix)
Expand All @@ -122,7 +122,7 @@ end


local function update_text_with_anchor_shift(self)
if self:get_text_width() >= self.text_area.x then
if self:get_text_size() >= self.text_area.x then
self:set_pivot(const.REVERSE_PIVOTS[self.start_pivot])
else
self:set_pivot(self.start_pivot)
Expand Down Expand Up @@ -236,21 +236,25 @@ end
--- Calculate text width with font with respect to trailing space
-- @tparam Text self @{Text}
-- @tparam[opt] string text
function Text.get_text_width(self, text)
-- @treturn number Width
-- @treturn number Height
function Text.get_text_size(self, text)
text = text or self.last_value
local font = gui.get_font(self.node)
local scale = gui.get_scale(self.node)
local result = gui.get_text_metrics(font, text, 0, false, 0, 0).width
local linebreak = gui.get_line_break(self.node)
local metrics = gui.get_text_metrics(font, text, 0, linebreak, 0, 0)
local width = metrics.width
for i = #text, 1, -1 do
local c = string.sub(text, i, i)
if c ~= ' ' then
break
end

result = result + get_space_width(self, font)
width = width + get_space_width(self, font)
end

return result * scale.x
return width * scale.x, metrics.height * scale.y
end


Expand Down

0 comments on commit ea5b3dc

Please sign in to comment.