Added a few events to UIRect.
Created event object, did some cleanup to main.lua example
This commit is contained in:
@@ -85,6 +85,7 @@ Dimension getPosition()
|
||||
Dimension getSize()
|
||||
Vector2D getAbsolutePosition() -- returns the real position of the object, in pixels
|
||||
Vector2D getAbsoluteSize() -- returns the real size of the object, in pixels
|
||||
boolean isMouseInside()
|
||||
setBackgroundColor(RGBColor color)
|
||||
setBorderColor(RGBColor color)
|
||||
setCornerRounding(number rounding)
|
||||
@@ -94,6 +95,12 @@ setBackgroundTransparency(number alpha)
|
||||
setBorderTransparency(number alpha)
|
||||
```
|
||||
|
||||
Events:
|
||||
```lua
|
||||
mouseEnter
|
||||
mouseExit
|
||||
```
|
||||
|
||||
### Frame class
|
||||
|
||||
*Inherits UIRect*
|
||||
|
28
classes/Event.lua
Normal file
28
classes/Event.lua
Normal file
@@ -0,0 +1,28 @@
|
||||
local DataType = require("classes/DataType")
|
||||
|
||||
local Event = DataType:subclass("Event")
|
||||
|
||||
function Event:init()
|
||||
self.super:init()
|
||||
self.callbacks = {}
|
||||
end
|
||||
|
||||
function Event:connect(newFunction)
|
||||
local num = #self.callbacks
|
||||
|
||||
self.callbacks[num] = newFunction
|
||||
|
||||
return num
|
||||
end
|
||||
|
||||
function Event:disconnect(functionID)
|
||||
self.callbacks[functionID] = nil
|
||||
end
|
||||
|
||||
function Event:call(...)
|
||||
for _, func in pairs(self.callbacks) do
|
||||
func(...)
|
||||
end
|
||||
end
|
||||
|
||||
return Event
|
4
classes/Image.lua
Normal file
4
classes/Image.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
local UIRect = require("classes/UIRect")
|
||||
|
||||
local Image = UIRect:subclass("Image")
|
||||
|
@@ -22,6 +22,10 @@ function TextBox:recalculateInternalFont()
|
||||
end
|
||||
end
|
||||
|
||||
function TextBox:getText()
|
||||
return self.text
|
||||
end
|
||||
|
||||
function TextBox:getFontFace()
|
||||
return self.fontFace
|
||||
end
|
||||
@@ -30,6 +34,10 @@ function TextBox:getTextSize()
|
||||
return self.textSize
|
||||
end
|
||||
|
||||
function TextBox:setText(text)
|
||||
self.text = text
|
||||
end
|
||||
|
||||
function TextBox:setFontFace(fontface)
|
||||
self.fontFace = fontface
|
||||
self:recalculateInternalFont()
|
||||
|
@@ -33,13 +33,13 @@ end
|
||||
function UIBase:render()
|
||||
--if not self.active then return end
|
||||
|
||||
--self:renderChildren()
|
||||
self:renderChildren()
|
||||
end
|
||||
|
||||
function UIBase:update(delta)
|
||||
--if not self.active then return end
|
||||
|
||||
--self:updateChildren(delta)
|
||||
self:updateChildren(delta)
|
||||
end
|
||||
|
||||
|
||||
|
@@ -2,6 +2,7 @@ local UIBase = require("classes/UIBase")
|
||||
local Dimension = require("classes/Dimension")
|
||||
local Vector2D = require("classes/Vector2D")
|
||||
local RGBColor = require("classes/RGBColor")
|
||||
local Event = require("classes/Event")
|
||||
|
||||
local UIRect = UIBase:subclass("UIRect")
|
||||
|
||||
@@ -15,6 +16,10 @@ function UIRect:init()
|
||||
self.cornerRounding = 1
|
||||
self.size = Dimension:new(0.2, 0.12)
|
||||
self.position = Dimension:new(0, 0)
|
||||
|
||||
self.mouseOver = false
|
||||
self.mouseEnter = Event:new()
|
||||
self.mouseExit = Event:new()
|
||||
end
|
||||
|
||||
function UIRect:getBackgroundColor()
|
||||
@@ -45,6 +50,10 @@ function UIRect:getBackgroundTransparency()
|
||||
return self.backgroundTransparency
|
||||
end
|
||||
|
||||
function UIRect:isMouseInside()
|
||||
return self.mouseOver
|
||||
end
|
||||
|
||||
function UIRect:setBackgroundTransparency(alpha)
|
||||
self.backgroundTransparency = alpha
|
||||
end
|
||||
@@ -69,6 +78,37 @@ function UIRect:getSize()
|
||||
return self.size
|
||||
end
|
||||
|
||||
function UIRect:update(delta)
|
||||
self.super:update(delta)
|
||||
|
||||
local mousePos = Vector2D:new(love.mouse.getX(), love.mouse.getY())
|
||||
|
||||
local pos = self:getAbsolutePosition()
|
||||
|
||||
local size = self:getAbsolutePosition()
|
||||
|
||||
local mouseWithin = true
|
||||
if not (mousePos.x > pos.x) then mouseWithin = false end
|
||||
if not (mousePos.x < (pos.x + size.x)) then mouseWithin = false end
|
||||
|
||||
if not (mousePos.y > pos.y) then mouseWithin = false end
|
||||
if not (mousePos.y < (pos.y + size.y)) then mouseWithin = false end
|
||||
|
||||
if mouseWithin then
|
||||
|
||||
if (self.mouseOver == false) then
|
||||
self.mouseOver = true
|
||||
self.mouseEnter:call()
|
||||
|
||||
end
|
||||
else
|
||||
if (self.mouseOver == true) then
|
||||
self.mouseOver = false
|
||||
self.mouseExit:call()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function UIRect:getAbsoluteSize()
|
||||
local pos = self:getPosition()
|
||||
local size = self:getSize()
|
||||
|
48
main.lua
48
main.lua
@@ -1,4 +1,7 @@
|
||||
_G.stringList = {}
|
||||
--[[
|
||||
This is an example project, demonstrating the ways that JUI can be utilized.
|
||||
|
||||
]]--
|
||||
|
||||
local JUI = require("JUIlib")
|
||||
|
||||
@@ -12,15 +15,22 @@ background:setPosition(bigboy)
|
||||
background:setCornerRounding(0)
|
||||
|
||||
local emptyColor = JUI.TextBox:new()
|
||||
emptyColor:setSize(JUI.Dimension:new(0.7, 0.4))
|
||||
emptyColor:setSize(JUI.Dimension:new(0.3, 0.1))
|
||||
emptyColor:setPosition(JUI.Dimension:new(0.2, 0.8))
|
||||
emptyColor:setBackgroundColor(JUI.RGBColor:new(200, 69, 128))
|
||||
emptyColor:setTextSize(36)
|
||||
emptyColor:setText("Bottom Text")
|
||||
emptyColor.mouseEnter:connect(function()
|
||||
emptyColor:setText("Inside")
|
||||
end)
|
||||
emptyColor.mouseExit:connect(function()
|
||||
emptyColor:setText("Outside")
|
||||
end)
|
||||
|
||||
JUI.parent(mainmenu, background)
|
||||
JUI.parent(background, emptyColor)
|
||||
JUI.parent(mainmenu, emptyColor)
|
||||
|
||||
local windowCreationSuccess = nil
|
||||
local tickTimer = 0
|
||||
local windowCreationSuccess
|
||||
|
||||
function love.load()
|
||||
windowCreationSuccess = love.window.setMode(1024, 600, {
|
||||
@@ -32,34 +42,14 @@ function love.load()
|
||||
})
|
||||
end
|
||||
|
||||
local secondsPassing = 0
|
||||
|
||||
local function updateLimiter(delta)
|
||||
tickTimer = tickTimer + delta
|
||||
|
||||
|
||||
if not (tickTimer > 1/60) then return false end
|
||||
tickTimer = 0
|
||||
end
|
||||
|
||||
function love.update(delta)
|
||||
updateLimiter(delta)
|
||||
secondsPassing = secondsPassing + delta
|
||||
mainmenu:update(delta)
|
||||
end
|
||||
|
||||
|
||||
function love.draw()
|
||||
love.graphics.setColor(JUI.RGBColor:new(100, 100, 100):dump())
|
||||
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
|
||||
|
||||
|
||||
mainmenu:render()
|
||||
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
for index, str in pairs(_G.stringList) do
|
||||
love.graphics.print(str, 500, (index*20))
|
||||
end
|
||||
|
||||
love.graphics.print("fps: "..tostring(love.timer.getFPS( )), 4, 4)
|
||||
mainmenu:render()
|
||||
|
||||
love.graphics.setColor((JUI.RGBColor:new(255, 255, 255)):dump())
|
||||
love.graphics.print("fps: "..tostring(love.timer.getFPS()), 4, 4)
|
||||
end
|
Reference in New Issue
Block a user