diff options
-rw-r--r-- | gui.lua | 21 | ||||
-rw-r--r-- | util.lua | 17 |
2 files changed, 30 insertions, 8 deletions
@@ -202,10 +202,6 @@ function GuiComponent:textinput(c) end -function GuiComponent:_halfheight() - return self:h() / 2 -end - Button = class(GuiComponent) function Button:__init(x, y, w, h, bg, fg) @@ -233,9 +229,7 @@ function Button:draw() self:drawbox() love.graphics.setFont(self:font()) - love.graphics.printf(self.text, self:x(), - (self:y() + (self:h() / 2)) - self:_halfheight(), - self:w(), 'center') + love.graphics.printf(self.text, self:x(), self:y(), self:w(), 'center') love.graphics.setFont(defaultfont) return true @@ -246,7 +240,6 @@ TextField = class(GuiComponent) function TextField:__init(x, y, w, h, bg, fg) self._base.__init(self, x, y, w, h, bg, fg) self.texteditable = true - self._placeholdertext = '' self._scrollindex = 0 end @@ -256,5 +249,17 @@ function TextField:draw() return false end + self:drawbox() + + local disptext = self.text:sub(self._scrollindex, self.text:len()) + disptext = disptext:fitwidth(self:w() * 0.95, self.font()) + + local textx = self:x() + self:w()*0.05 + local texty = self:y() + self:h()/2 - self:font():getHeight()/2 + + love.graphics.setFont(self:font()) + love.graphics.print(self.text, textx, texty) + love.graphics.setFont(defaultfont) + return true end @@ -62,3 +62,20 @@ function string:split(delim) return out end + +function string:fitwidth(w, font) + local i = 0 + local s + + repeat + i = i + 1 + s = self:sub(1, i) + until i > self:len() or font:getWidth(s) > w + + if i == 1 then + return '' + end + + i = i - 1 + return self:sub(1, i) +end |