tie372 11 ani în urmă
părinte
comite
94c29bdad0
3 a modificat fișierele cu 111 adăugiri și 10 ștergeri
  1. 95 0
      lib/gooey/textbox.lua
  2. 13 8
      lib/typo.lua
  3. 3 2
      require.lua

+ 95 - 0
lib/gooey/textbox.lua

@@ -0,0 +1,95 @@
+TextBox = class()
+
+local g = love.graphics
+
+function TextBox:init()
+	self.x = 0
+	self.y = 0
+	self.w = 0
+	self.h = 0
+	self.bgColor = {0, 0, 0, 255}
+	self.borderColor = {0, 0, 0, 255}
+
+	self.font = nil
+	self.fontSize = 0
+	self.fontColor = {255, 255, 255, 255}
+
+	self.hPadding = 0
+	self.vPadding = 0
+
+	self.text = ''
+
+	self.focused = false
+	self.insertAt = 1
+
+	self.onfocus = nil
+	self.onblur = nil
+	self.onchange = nil
+end
+
+function TextBox:focus()
+	if not self.focused then
+		self.focused = true
+		f.exe(self.onfocus, self)
+		love.keyboard.setKeyRepeat(true)
+	end
+end
+
+function TextBox:blur()
+	if self.focused then
+		self.focused = false
+		f.exe(self.onblur, self)
+		love.keyboard.setKeyRepeat(false)
+	end
+end
+
+function TextBox:keypressed(key)
+	if key == 'backspace' and self.focused and self.insertAt > 1 then
+		self.text = self.text:sub(1, self.insertAt - 2) .. self.text:sub(self.insertAt)
+		self.insertAt = self.insertAt - 1
+		f.exe(self.onchange, self)
+	elseif key == 'left' and self.insertAt > 1 then
+		self.insertAt = self.insertAt - 1
+	elseif key == 'right' and self.insertAt < #self.text + 1 then
+		self.insertAt = self.insertAt + 1
+	end
+end
+
+function TextBox:textinput(char)
+	if self.focused then
+		self.text = self.text:sub(1, self.insertAt - 1) .. char .. self.text:sub(self.insertAt)
+		self.insertAt = self.insertAt + 1
+		f.exe(self.onchange, self)
+	end
+end
+
+function TextBox:draw()
+
+	g.setColor(self.bgColor)
+	g.rectangle('fill', self.x, self.y, self.w, self.h)
+	g.setColor(self.borderColor)
+	g.rectangle('line', self.x + .5, self.y + .5, self.w - 1, self.h - 1)
+
+	g.setFontPixel(self.font, self.fontSize)
+	g.setColor(self.fontColor)
+
+	local str = self.text
+	if self.placeholder and #self.text == 0 then
+		local r, g, b, a = g.getColor()
+		a = a / 2
+		love.graphics.setColor(r, g, b, a)
+		str = self.placeholder
+	end
+	
+	local font = g.getFont()
+	local x = self.x + self.hPadding
+	local y = self.y + self.h / 2 - font:getHeight() / 2
+
+	g.print(str, x, y)
+
+	if self.focused then
+		local xx = self.x + self.hPadding + font:getWidth(self.text:sub(1, self.insertAt - 1)) + .5
+		g.setColor(self.fontColor)
+		g.line(xx, y, xx, y + font:getHeight())
+	end
+end

+ 13 - 8
lib/typo.lua

@@ -3,17 +3,22 @@ Typo.fonts = {}
 local oldSetFont = love.graphics.setFont
 
 Typo.setFont = function(name, size)
-  if type(name) == 'string' then
-    Typo.fonts[name] = Typo.fonts[name] or {}
-    Typo.fonts[name][size] = Typo.fonts[name][size] or love.graphics.newFont('media/fonts/' .. name .. '.ttf', love.graphics.height(size / 100))
-    oldSetFont(Typo.fonts[name][size])
-  else
-    oldSetFont(name)
-  end
+	if type(name) == 'string' then
+		Typo.setFontPixel(name, love.graphics.height(size / 100))
+	else
+		oldSetFont(name)
+	end
+end
+
+Typo.setFontPixel = function(name, size)
+	Typo.fonts[name] = Typo.fonts[name] or {}
+	Typo.fonts[name][size] = Typo.fonts[name][size] or love.graphics.newFont('media/fonts/' .. name .. '.ttf', size)
+	oldSetFont(Typo.fonts[name][size])
 end
 
 Typo.resize = function()
   table.clear(Typo.fonts)
 end
 
-love.graphics.setFont = Typo.setFont
+love.graphics.setFont = Typo.setFont
+love.graphics.setFontPixel = Typo.setFontPixel

+ 3 - 2
require.lua

@@ -1,7 +1,7 @@
 local function load(dir)
   for _, file in pairs(love.filesystem.getDirectoryItems(dir)) do
     local path = dir .. '/' .. file
-    if string.find(path, '%.lua') then
+    if string.find(path, '%.lua') and not string.find(path, '%..+%.lua') then
       require(path:gsub('%.lua', ''))
     end
   end
@@ -15,5 +15,6 @@ load 'lib'
 load 'lib/editor'
 load 'lib/hud'
 load 'lib/menu'
+load 'lib/gooey'
 load 'ovw'
-load 'data/loader'
+load 'data/loader'