Skip to content

Commit 93b6bdf

Browse files
committed
Split Text content into multiple lines
1 parent ca9e157 commit 93b6bdf

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

EZGUI.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--[[
2-
EZGUI v0.1.0
2+
EZGUI v0.1.1
33
]]
44

55
local _ModTextFileGetContent = ModTextFileGetContent

changelog.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
v0.1.1:
2+
- FIX: Layout background rendering z-index
3+
- FIX: Attribute binding didn't work (e.g. :debug="var")
4+
- FIX: CSS property defaults were not being applied correctly
5+
- Rework <Text> multiline/newline rendering
6+
- Use images from data/ to render debug visualizations
7+
18
v0.1.0:
29
- First preview release

elements/Text.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
dofile_once("%PATH%/oop.lua")
22
local parsers = dofile_once("%PATH%/parsing_functions.lua")
3+
local utils = dofile_once("%PATH%/utils.lua")
34
local DOMElement = dofile_once("%PATH%/elements/DOMElement.lua")
45

56
-- trim7 from http://lua-users.org/wiki/StringTrim
@@ -9,14 +10,23 @@ end
910

1011
local Text = new_class("Text", function(self, xml_element, data_context)
1112
super(xml_element, data_context)
13+
local text = trim(xml_element:text())
1214
self.value = parsers.parse_text(trim(xml_element:text()))
1315
end, DOMElement)
1416

1517
function Text:GetDimensions(gui, data_context)
1618
if not gui then error("Required parameter #1: GuiObject", 2) end
1719
if not data_context then error("Required parameter #2: data_context:table", 2) end
1820
local text = inflate(self.value, data_context)
19-
local width, height = GuiGetTextDimensions(gui, text)
21+
-- split text into lines
22+
local lines = utils.split_lines(text)
23+
local width, height = 0, 0
24+
for i, line in ipairs(lines) do
25+
line = trim(line)
26+
local w, h = GuiGetTextDimensions(gui, line)
27+
width = math.max(width, w)
28+
height = height + h
29+
end
2030
width = width + self.style.padding_left + self.style.padding_right
2131
height = height + self.style.padding_top + self.style.padding_bottom
2232
return width, height
@@ -26,7 +36,9 @@ function Text:Render(gui, new_id, data_context, layout)
2636
if not gui then error("Required parameter #1: GuiObject", 2) end
2737
if not data_context then error("Required parameter #2: data_context", 2) end
2838
local text = inflate(self.value, data_context)
39+
local lines = utils.split_lines(text)
2940
local width, height = self:GetDimensions(gui, data_context)
41+
local line_height = (height - self.style.padding_top - self.style.padding_bottom) / #lines
3042
local x, y = self.style.margin_left, self.style.margin_top
3143
if layout then
3244
x, y = layout:GetPositionForWidget(self, width, height)
@@ -37,8 +49,11 @@ function Text:Render(gui, new_id, data_context, layout)
3749
else
3850
z = self:GetZ()
3951
end
40-
GuiZSetForNextWidget(gui, z)
41-
GuiText(gui, x + self.style.padding_left, y + self.style.padding_top, text)
52+
for i, line in ipairs(lines) do
53+
line = trim(line)
54+
GuiZSetForNextWidget(gui, z)
55+
GuiText(gui, x + self.style.padding_left, y + self.style.padding_top + (i-1) * line_height, line)
56+
end
4257

4358
if self.attr.debug then
4459
-- Debug rendering

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ezgui",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "You make an XML file that contains your GUI definition, just like single file components in Vue.js.\r A root Layout element is mandatory.\r ```xml\r <Layout>\r <Button @click=\"a_method('with a string arg')\">Click me!</Button>\r <Text forEach=\"element in collection\">{{ element }}</Text>\r </Layout>\r <Style>\r Layout {\r direction: vertical;\r padding: 2;\r }\r Layout > Button {\r margin: [button_margin]; // Can also databind to CSS properties!\r }\r </Style>\r ```\r Then in your init.lua you can render this GUI:\r ```lua\r -- Dofiling EZGUI.lua returns a table with an init function that you need to call and pass in the path to the library, which in turn will return a render function you can call to render a GUI\r local render_gui = dofile_once(\"mods/your_mod_id/lib/EZGUI/EZGUI.lua\").init(\"mods/your_mod_id/lib/EZGUI\")\r -- This is the data context table, here lives your data that you can bind to\r local data = {\r collection = { \"Bloo\", \"Blaa\", \"Blee\" },\r button_margin = 5,\r -- Methods defined here can be used in @click, arg1 is the data_context itself, arg2 the element that was clicked, arg3 the first custom arg\r a_method = function(data, element, arg1)\r print(arg1)\r end,\r }",
55
"main": "build.js",
66
"directories": {

utils.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ local function get_data_from_binding_chain(data_context, binding_target_chain)
6565
end
6666

6767
return {
68+
split_lines = split_lines,
6869
throw_error = throw_error,
6970
get_line_by_pos = get_line_by_pos,
7071
get_data_from_binding_chain = get_data_from_binding_chain

0 commit comments

Comments
 (0)