#include "ui_defs.lua" function Ui.TextInput(obj) local textInput textInput = Ui.Frame { focus = false, isGamepad = false, background = Ui.ImageBox { id = "background", path = "ui/common/box-outline-6.png", border = {width = 6, height = 6}, color = {1,1,1,0.5}, onPreDraw = function(self) self:fromParent() end, }, placeholder = Ui.Text { margin = {left = 8}, vAlign = Ui.ContentLayout.Vertical.Middle, text = "Input text", colorFilter = {1,1,1,0.5}, }, data = { text = "" }, clearButton = Ui.Text { vAlign = Ui.ContentLayout.Vertical.Middle, hAlign = Ui.ContentLayout.Horizontal.Right, margin = {right = 10}, text = "x", textOnly = true, onPreDraw = function(self) if not self.parent.isGamepad then Ui.ProcessFocus(self) end end, onMouseEnter = function(self) self.color = 0xFFFF7F end, onMouseLeave = function(self) self.color = 0xFFFFFF end, onPressed = function(self) self.parent.input:clear() end, }, input = Ui.Object:new { visible = true, maxLength = 0, color = {1,1,1}, onPreDraw = function(self) self:fromParent() self.parent.placeholder.visible = self:empty() end, empty = function(self) return UiGetSymbolsCount(self.parent.data.text) == 0 end, clear = function(self) self.parent.data.text = "" end, draw = function(self) UiColor(unpack(self.color)) Ui:Font("regular.ttf", 22) self.parent.data.text = UiTextInput(self.parent.data.text, self.w, self.h, self.parent.focus) if UiGetSymbolsCount(self.parent.data.text) > self.maxLength then self.parent.data.text = UiTextSymbolsSub(self.parent.data.text, 1, self.maxLength) end end, }, onPostDraw = function(self) self.isGamepad = LastInputDevice() == UI_DEVICE_GAMEPAD self.clearButton.visible = not self.isGamepad end, setText = function(self, newText) self.data.text = newText end, getText = function(self) return tostring(self.data.text) end, empty = function(self) return self.input:empty() end, deleteLastCharacter = function(self) local len = UiGetSymbolsCount(self.data.text) if len > 0 then self.data.text = UiTextSymbolsSub(self.data.text, 1, len - 1) end end, onFocused = function(self) end, } return textInput:new(obj) end