Skip to content

Commit 00b8ab6

Browse files
committed
Add Input element
1 parent df7c0f8 commit 00b8ab6

File tree

7 files changed

+81
-6
lines changed

7 files changed

+81
-6
lines changed

EZGUI.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ local function make_observable(t, key, prev_keys)
4848
end
4949
path = path .. key
5050

51-
print(path .. " changed!")
51+
-- print(path .. " changed!")
5252
end
5353
})
5454
end
@@ -65,12 +65,13 @@ return {
6565
("%sparsing_functions.lua"):format(self_path),
6666
("%sstring_buffer.lua"):format(self_path),
6767
("%sutils.lua"):format(self_path),
68-
("%selements/Button.lua"):format(self_path),
6968
("%selements/DOMElement.lua"):format(self_path),
70-
("%selements/Image.lua"):format(self_path),
7169
("%selements/Layout.lua"):format(self_path),
72-
("%selements/Slider.lua"):format(self_path),
7370
("%selements/Text.lua"):format(self_path),
71+
("%selements/Input.lua"):format(self_path),
72+
("%selements/Button.lua"):format(self_path),
73+
("%selements/Image.lua"):format(self_path),
74+
("%selements/Slider.lua"):format(self_path),
7475
}
7576
for i, filepath in ipairs(files) do
7677
local content = ModTextFileGetContent(filepath)
@@ -88,6 +89,7 @@ return {
8889
local pretty = dofile_once(self_path .. "lib/pretty.lua")
8990
local Layout = dofile_once(self_path .. "elements/Layout.lua")
9091
local Text = dofile_once(self_path .. "elements/Text.lua")
92+
local Input = dofile_once(self_path .. "elements/Input.lua")
9193
local Button = dofile_once(self_path .. "elements/Button.lua")
9294
local Image = dofile_once(self_path .. "elements/Image.lua")
9395
local Slider = dofile_once(self_path .. "elements/Slider.lua")
@@ -102,6 +104,7 @@ return {
102104
Button = Button,
103105
Image = Image,
104106
Slider = Slider,
107+
Input = Input,
105108
}
106109

107110
local dom_cache = {}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ Render a slider.
6464
## Text
6565
Render some text. Example `<Text>Hello</Text>`
6666

67+
## Input
68+
For getting user input. Example `<Input bind="name"></Input>`
69+
70+
### Properties:
71+
- `bind` - The data context variable to bind to
72+
6773
All element support the `padding`, `margin` properties.
6874

6975
# Styling / Pseudo-CSS

build.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ local files_to_bundle = {
3232
"lib/pretty.lua",
3333
"lib/nxml.lua",
3434
"elements/Button.lua",
35+
"elements/Input.lua",
3536
"elements/DOMElement.lua",
3637
"elements/Image.lua",
3738
"elements/Layout.lua",

elements/Input.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
dofile_once("%PATH%oop.lua")
2+
local parser = dofile_once("%PATH%parsing_functions.lua")
3+
local utils = dofile_once("%PATH%utils.lua")
4+
local DOMElement = dofile_once("%PATH%elements/DOMElement.lua")
5+
6+
local Input = new_class("Input", function(self, xml_element, data_context)
7+
super(xml_element, data_context)
8+
if not xml_element.attr.bind then
9+
error("'bind' attribute is required on Input field", 4)
10+
end
11+
self.binding_target = { type = "binding", target_chain = parser.read_binding_target(xml_element.attr.bind) }
12+
self.min_width = 30
13+
end, DOMElement)
14+
15+
Input.default_style = {
16+
width = 100,
17+
}
18+
19+
function Input:GetInnerAndOuterDimensions(gui, data_context)
20+
if not gui then error("Required parameter #1: GuiObject", 2) end
21+
if not data_context then error("Required parameter #2: data_context:table", 2) end
22+
local inner_width, inner_height = math.max(self.min_width, self.style.width), 11
23+
local outer_width = inner_width + self.style.padding_left + self.style.padding_right
24+
local outer_height = inner_height + self.style.padding_top + self.style.padding_bottom
25+
outer_width = math.max(outer_width, self.min_width, self.style.width or 0)
26+
outer_height = math.max(outer_height, self.style.height or 0)
27+
return inner_width, inner_height, outer_width, outer_height
28+
end
29+
30+
function Input:Render(gui, new_id, data_context, layout)
31+
if not gui then error("Required parameter #1: GuiObject", 2) end
32+
if not data_context then error("Required parameter #2: data_context", 2) end
33+
local width, height = self:GetDimensions(gui, data_context)
34+
local value = get_value_from_chain_or_not(data_context, self.binding_target)
35+
local x, y = self.style.margin_left, self.style.margin_top
36+
if layout then
37+
x, y = layout:GetPositionForWidget(gui, data_context, self, width, height)
38+
end
39+
local z
40+
if layout then
41+
z = layout:GetZ()
42+
else
43+
z = self:GetZ()
44+
end
45+
GuiZSetForNextWidget(gui, z)
46+
local new_text = GuiTextInput(gui, new_id(), x + self.style.padding_left, y + self.style.padding_top, value, width, 50)
47+
if new_text ~= value then
48+
-- TODO: Refactor this
49+
local context = data_context
50+
for i=1, #self.binding_target.target_chain-1 do
51+
context = context[self.binding_target.target_chain[i]]
52+
end
53+
context[self.binding_target.target_chain[#self.binding_target.target_chain]] = new_text
54+
end
55+
end
56+
57+
return Input

env.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function GuiGetTextDimensions(gui, text) return #text * 6, 10 end
5151
function GuiGetImageDimensions(gui, image_path) return 10, 10 end
5252
function GuiLayoutEnd() end
5353
function GuiSlider() return 1 end
54+
function GuiTextInput() return "" end
5455
function GuiBeginScrollContainer() end
5556
function GuiEndScrollContainer() end
5657
function GuiOptionsAddForNextWidget() end

parsing_functions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local utils = dofile_once("%PATH%utils.lua")
22
local string_buffer = dofile_once("%PATH%string_buffer.lua")
33

44
local dom_element_names = {
5-
"Button", "Image", "Layout", "Slider", "Text", "*"
5+
"Layout", "Text", "Input", "Button", "Image", "Slider", "*"
66
}
77

88
local function _throw_error(str, msg, pos, level)

xxx.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local data = {
77
},
88
align_items_horizontal = "center",
99
align_items_vertical = "top",
10+
align_self_horizontal = "center",
11+
align_self_vertical = "top",
1012
direction = "vertical",
1113
pleft = {
1214
val = 22
@@ -17,6 +19,11 @@ local data = {
1719
margin_bottom = 0,
1820
padding_left = 0,
1921
padding_top = 0,
22+
padding = 0,
23+
margin = 0,
24+
width = 0,
25+
height = 0,
26+
color = { r = 1, g = 1, b = 1, a = 1 },
2027
padding_right = 0,
2128
padding_bottom = 0,
2229
increase_counter = function(data, element, amount)
@@ -48,4 +55,4 @@ local parser = dofile_once("parsing_functions.lua")
4855
local pretty = dofile_once("lib/pretty.lua")
4956
local css = dofile_once("css.lua")
5057

51-
local a = d(0, 0, "../../gui.xml", data)
58+
local a = d(0, 0, "../../gui2.xml", data)

0 commit comments

Comments
 (0)