diff --git a/API.md b/API.md index e5693dd..258d1a7 100644 --- a/API.md +++ b/API.md @@ -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 getChildren() diff --git a/JUIlib.lua b/JUIlib.lua index cd2ccf5..24e63e0 100644 --- a/JUIlib.lua +++ b/JUIlib.lua @@ -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"), diff --git a/README.md b/README.md index 2d77863..21179a6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ -# JUI Library +# Josh's UI Library + +Of course, yet another Love2d GUI library. + A library that adds simple GUI elements for you to utilize in your Love2D games. ## Features diff --git a/lib/classes/Button.lua b/lib/classes/Button.lua index 9fb2c34..68eacb7 100644 --- a/lib/classes/Button.lua +++ b/lib/classes/Button.lua @@ -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 \ No newline at end of file diff --git a/lib/classes/InputForm.lua b/lib/classes/InputForm.lua new file mode 100644 index 0000000..9cab233 --- /dev/null +++ b/lib/classes/InputForm.lua @@ -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 \ No newline at end of file diff --git a/lib/classes/JUIScene.lua b/lib/classes/JUIScene.lua index 5f9f146..47091f6 100644 --- a/lib/classes/JUIScene.lua +++ b/lib/classes/JUIScene.lua @@ -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() diff --git a/lib/classes/UIBase.lua b/lib/classes/UIBase.lua index a95f51b..f3ac428 100644 --- a/lib/classes/UIBase.lua +++ b/lib/classes/UIBase.lua @@ -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 diff --git a/lib/classes/UIButton.lua b/lib/classes/UIButton.lua index e500b62..0f77448 100644 --- a/lib/classes/UIButton.lua +++ b/lib/classes/UIButton.lua @@ -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() diff --git a/main.lua b/main.lua index a74ecd7..8574a6a 100644 --- a/main.lua +++ b/main.lua @@ -38,6 +38,8 @@ end JUI:parent(mainmenu, testSlider) JUI:parent(mainmenu, text) +JUI:parent(mainmenu, bigBalls) + local windowCreationSuccess local debugInfoString = ""