summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid McMackins II <contact@mcmackins.org>2018-05-30 06:42:55 -0500
committerDavid McMackins II <contact@mcmackins.org>2018-05-30 06:42:55 -0500
commitfef04662c0962d01c49840762d5190aade03b562 (patch)
treeff6ccb145749c627cb5b6e70bac7a04cdc0470d1
parent04812b5a154ff2702820c8f77213b0380ed1e027 (diff)
Implement basic draw for text fieldsHEADmaster
-rw-r--r--gui.lua21
-rw-r--r--util.lua17
2 files changed, 30 insertions, 8 deletions
diff --git a/gui.lua b/gui.lua
index 99a90d2..bb497ae 100644
--- a/gui.lua
+++ b/gui.lua
@@ -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
diff --git a/util.lua b/util.lua
index 5f4b700..497adb7 100644
--- a/util.lua
+++ b/util.lua
@@ -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