Added InputForm, mega bugfixes lol

This commit is contained in:
Josh
2019-05-16 15:16:21 -05:00
parent 569a5de51a
commit 6f5e0455d4
8 changed files with 158 additions and 6 deletions

2
API.md
View File

@@ -34,6 +34,8 @@ UIBase:new() -- Unless otherwise specified, all UIBase object constructors take
Methods:
```lua
keypressPass(string key) -- use with love.keypressed function
textinputPass(string t) -- use with love.textinput function
render()
update(number deltatime) -- deltatime should be passed from love.update function
table<UIBase> getChildren()

View File

@@ -7,7 +7,8 @@ return {
Frame = require("lib.classes.Frame"),
Label = require("lib.classes.Label"),
Button = require("lib.classes.Button"),
Slider = require("lib.classes.Slider"),
Slider = require("lib.classes.Slider"),
InputForm = require("lib.classes.InputForm"),
-- Datatypes
Vector2D = require("lib.datatypes.Vector2D"),

View File

@@ -2,7 +2,7 @@ local UIButton = require("lib.classes.UIButton")
local Color = require("lib.datatypes.Color")
local Button = UIButton:subclass("Button")
--[[
function Button:init()
self.super:init()
self.text = "Button"
@@ -78,5 +78,5 @@ function Button:render()
self:renderChildren()
end
]]
return Button

123
lib/classes/InputForm.lua Normal file
View File

@@ -0,0 +1,123 @@
local Label = require("lib.classes.Label")
local Event = require("lib.classes.Event")
local InputForm = Label:subclass("InputForm")
function InputForm:init()
self.super:init()
self.focusGrabbed = Event:new()
self.focusDropped = Event:new()
self.inputEvent = Event:new()
self.isFocused = false
self.defaultText = ""
self.text = ""
self.internalText = ""
self.defaultText = ""
self.hasBeenModified = false
self.clearOnReturn = false
self.overrideDefaultTextOnFocus = true
self.time = 0.0
self.cursor = "|"
self.cursorPos = 0
self.shift = false
end
function InputForm:reset()
self.cursorPos = 0
self.time = 0.0
self.internalText = ""
self.hasBeenModified = false
end
function InputForm:keypressed(key)
if key == "backspace" and self.cursorPos > 0 then
self.internalText = string.sub(self.internalText, 1, self.cursorPos-1)..string.sub(self.internalText, self.cursorPos+1)
self.cursorPos = self.cursorPos - 1
elseif key == "left" then
self.cursorPos = math.max(0, self.cursorPos-1)
elseif key == "right" then
self.cursorPos = math.min(self.internalText:len(), self.cursorPos+1)
elseif key == "delete" then
-- omitted for now
elseif key == "return" then
--print(self.internalText)
self.inputEvent:call(self.internalText)
self:dropFocus(true)
if self.clearOnReturnKey then
self:reset()
end
end
end
function InputForm:textinput(t)
self.internalText = string.sub(self.internalText, 1, self.cursorPos)..t..string.sub(self.internalText, self.cursorPos+1)
self.cursorPos = self.cursorPos + 1
self.hasBeenModified = true
end
function InputForm:setDefaultText(text)
self.defaultText = text
end
function InputForm:grabFocus()
self.isFocused = true
self.focusGrabbed:call()
if self.overrideDefaultTextOnFocus then
self.hasBeenModified = true
end
end
function InputForm:dropFocus(enter)
self.isFocused = false
self.focusDropped:call(enter)
end
function InputForm:update(dt)
self.super:update(dt)
-- invert the cursor every second
self.time = self.time + dt
if self.time > 0.25 then
self.cursor = (self.cursor == "|") and "" or "|"
self.time = 0.0
end
if self.hasBeenModified == true then
if self.isFocused then
self.text = string.sub(self.internalText, 1, self.cursorPos)..self.cursor..string.sub(self.internalText, self.cursorPos+1)
else
self.text = self.internalText
end
else
self.text = self.defaultText
end
if love.mouse.isDown(1) then
if self.mouseOver then
self:grabFocus()
else
self:dropFocus(false)
end
end
end
function InputForm:render()
self.super:render()
self:renderChildren()
end
return InputForm

View File

@@ -35,6 +35,7 @@ function JUIScene:getAbsoluteSize()
return Vector2D:new(love.graphics.getWidth(), love.graphics.getHeight())
end
function JUIScene:render()
self.super:render()
self:renderChildren()

View File

@@ -45,10 +45,31 @@ function UIBase:init()
self.isUIObject = true
self.active = true
self.children = {}
self.parent = nil
self.parent = nil
self.textInput = false
self.baseReturnFont = love.graphics.newFont(12)
end
function UIBase:keypressPass(key)
for _, child in pairs(self.children) do
if child.isFocused and child.isFocused == true then
child:keypressed(key)
return true
end
child:keypressPass(key)
end
end
function UIBase:textinputPass(t)
for _, child in pairs(self.children) do
if child.isFocused and child.isFocused == true then
child:textinput(t)
return true
end
child:textinputPass(t)
end
end
function UIBase:renderChildren()
for _, child in pairs(self.children) do
if child.isUIObject then
@@ -66,7 +87,9 @@ function UIBase:updateChildren(delta)
end
function UIBase:render()
end
function UIBase:update(delta)
self:updateChildren(delta)
end

View File

@@ -1,7 +1,7 @@
local UIRect = require("lib.classes.UIRect")
local Label = require("lib.classes.Label")
local Event = require("lib.classes.Event")
local UIButton = UIRect:subclass("UIButton")
local UIButton = Label:subclass("UIButton")
function UIButton:init()
self.super:init()

View File

@@ -38,6 +38,8 @@ end
JUI:parent(mainmenu, testSlider)
JUI:parent(mainmenu, text)
JUI:parent(mainmenu, bigBalls)
local windowCreationSuccess
local debugInfoString = ""