|
| 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 |
0 commit comments