summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-04-06 23:03:50 +0200
committerAki <nthirtyone@gmail.com>2017-04-06 23:03:50 +0200
commit20770db3aba953585495d21bfe0a2e430485b038 (patch)
tree45a58dc25e58ea44dd0d9bbbc984f0cd5e7ce32e
parente9396447537e0c6dedfb55e9a7ffc1e99575d860 (diff)
downloadroflnauts-20770db3aba953585495d21bfe0a2e430485b038.zip
roflnauts-20770db3aba953585495d21bfe0a2e430485b038.tar.gz
roflnauts-20770db3aba953585495d21bfe0a2e430485b038.tar.bz2
First steps in cleaning-up menu.
Selector, Button and Header extend Element now.
-rw-r--r--button.lua56
-rw-r--r--element.lua50
-rw-r--r--header.lua45
-rw-r--r--selector.lua78
4 files changed, 105 insertions, 124 deletions
diff --git a/button.lua b/button.lua
index c5681b2..7afc8e9 100644
--- a/button.lua
+++ b/button.lua
@@ -1,51 +1,48 @@
--- `Button`
--- Button used in `Menu`
-
+--- `Button`
+-- Menu element that can be activated by user.
Button = {
- text = "",
- focused = false,
+ parent = --[[not.Menu]]nil,
x = 0,
y = 0,
+ text = "",
+ focused = false,
sprite,
quads,
delay = 2,
parent,
}
-function Button:new(parent)
- local o = {}
- setmetatable(o, self)
- self.__index = self
+-- `Button` is a child of `Element`.
+require "element"
+Button.__index = Button
+setmetatable(Button, Element)
+
+function Button:new (parent)
+ local o = setmetatable({}, self)
o.parent = parent
o.sprite, o.quads = parent:getSheet()
return o
end
-function Button:setText(text)
+
+function Button:setText (text)
self.text = text or ""
return self
end
-function Button:setPosition(x, y)
- self.x = x or 0
- self.y = y or 0
- return self
-end
-function Button:getPosition() return self.x,self.y end
+
function Button:focus(next)
self.focused = true
return true
end
-function Button:blur()
+function Button:blur ()
self.focused = false
end
-function Button:active() end
-function Button:isEnabled() return true end
-function Button:set(name, func)
- if type(name) == "string" and type(func) == "function" then
- self[name] = func
- end
- return self
+
+function Button:active () end
+function Button:isEnabled ()
+ return true
end
-function Button:draw(scale)
+
+function Button:draw (scale)
local x,y = self:getPosition()
local quad = self.quads
local sprite = self.sprite
@@ -62,17 +59,18 @@ function Button:draw(scale)
love.graphics.setFont(Font)
love.graphics.printf(self.text, (x+2)*scale, (y+4)*scale, 54, "center", 0, scale, scale)
end
-function Button:update(dt)
+
+function Button:update (dt)
self.delay = self.delay + dt
if self.delay > Button.delay then -- Button.delay is initial
self.delay = self.delay - Button.delay
end
end
-function Button:controlpressed(set, action, key)
+
+function Button:controlpressed (set, action, key)
if action == "attack" and self.focused and self:isEnabled() then
self:active()
end
end
-function Button:controlreleased(set, action, key) end
-return Button \ No newline at end of file
+return Button
diff --git a/element.lua b/element.lua
index f9d1c3d..e6d91da 100644
--- a/element.lua
+++ b/element.lua
@@ -1,42 +1,50 @@
--- `Element`
--- Empty element for `Menu` creation. Can be anything.
+--- `Element`
+-- Empty element used inside `Menu`.
Element = {
+ parent = --[[not.Menu]]nil,
x = 0,
- y = 0,
- parent
+ y = 0
}
-function Element:new(parent)
- local o = {}
- setmetatable(o, self)
- self.__index = self
+
+Element.__index = Element
+
+function Element:new (parent)
+ local o = setmetatable({}, self)
o.parent = parent
return o
end
-function Element:delete() end -- deletes Element
-function Element:getPosition() return self.x, self.y end -- gives x,y of Element
-function Element:setPosition(x,y)
- self.x, self.y = x, y
+
+function Element:delete () end -- deletes Element
+
+function Element:getPosition ()
+ return self.x, self.y
+end
+function Element:setPosition (x, y)
+ self.x = x or 0
+ self.y = y or 0
return self
end
-function Element:set(name, func)
+
+function Element:set (name, func)
if type(name) == "string" and func ~= nil then
self[name] = func
end
return self
end
--- Menu callbacks
-function Element:focus() -- Called when Element gains focus
+-- Called when menu tries to focus on this element.
+-- If it will return false then menu will skip element and go to next in list.
+function Element:focus ()
return false
end
-function Element:blur() end -- Called when Element loses focus
+function Element:blur () end -- Called when Element loses focus.
-- LÖVE2D callbacks
-function Element:draw(scale) end
-function Element:update(dt) end
+function Element:draw (scale) end
+function Element:update (dt) end
-- Controller callbacks
-function Element:controlpressed(set, action, key) end
-function Element:controlreleased(set, action, key) end
+function Element:controlpressed (set, action, key) end
+function Element:controlreleased (set, action, key) end
-return Element \ No newline at end of file
+return Element
diff --git a/header.lua b/header.lua
index d67e582..9c18bf1 100644
--- a/header.lua
+++ b/header.lua
@@ -1,41 +1,36 @@
--- `Header`
--- It dances!
-
+--- `Header`
+-- Swinging title.
Header = {
+ parent = --[[not.Menu]]nil,
x = 0,
y = 0,
text = "",
- parent,
bounce = 2,
}
-function Header:new(parent)
- local o = {}
- setmetatable(o, self)
- self.__index = self
+
+-- `Header` is a child of `Element`.
+require "element"
+Header.__index = Header
+setmetatable(Header, Element)
+
+function Header:new (parent)
+ local o = setmetatable({}, self)
o.parent = parent
return o
end
-function Header:setText(text)
+
+function Header:setText (text)
self.text = text or ""
return self
end
-function Header:setPosition(x, y)
- self.x = x or 0
- self.y = y or 0
- return self
-end
-function Header:getBounce(f)
+
+function Header:getBounce (f)
local f = f or 1
return math.sin(self.bounce*f*math.pi)
end
-function Header:getPosition() return self.x,self.y end -- gives x,y of Element
-function Header:focus()
- return false
-end
-function Header:blur() end -- Called when Element loses focus
-- LÖVE2D callbacks
-function Header:draw(scale)
+function Header:draw (scale)
local angle = self:getBounce(2)
local dy = self:getBounce()*4
local x,y = self:getPosition()
@@ -43,15 +38,11 @@ function Header:draw(scale)
love.graphics.setFont(Bold)
love.graphics.printf(string.upper(self.text),x*scale,(y+dy)*scale,400,"center",(angle*5)*math.pi/180,scale,scale,200,12)
end
-function Header:update(dt)
+function Header:update (dt)
self.bounce = self.bounce + dt*0.7
if self.bounce > Header.bounce then -- Header.bounce is initial
self.bounce = self.bounce - Header.bounce
end
end
--- Controller callbacks
-function Header:controlpressed(set, action, key) end
-function Header:controlreleased(set, action, key) end
-
-return Header \ No newline at end of file
+return Header
diff --git a/selector.lua b/selector.lua
index 03be918..1a449ed 100644
--- a/selector.lua
+++ b/selector.lua
@@ -1,4 +1,4 @@
--- `Selector` (Element)
+--- `Selector`
-- Used in Menu for selecting various things from list. Works for each Controller set or globally.
--[[
How to use `Selector` in `Menu` config file?
@@ -12,9 +12,8 @@ selector:new(menu)
:set("global", false) -- true: single selector; false: selector for each controller set present
:init()
]]
-
Selector = {
- parent,
+ parent = --[[not.Menu]]nil,
x = 0,
y = 0,
width = 0,
@@ -35,53 +34,39 @@ Selector = {
icons_q
}
+-- `Selector` is a child of `Element`.
+require "element"
+Selector.__index = Selector
+setmetatable(Selector, Element)
+
-- Constructor
-function Selector:new(parent)
- local o = {}
- setmetatable(o, self)
- self.__index = self
+function Selector:new (parent)
+ local o = setmetatable({}, self)
o.parent = parent
o.sprite, o.quads = parent:getSheet()
return o
end
--- Position
-function Selector:getPosition()
- return self.x, self.y
-end
-function Selector:setPosition(x,y)
- self.x, self.y = x, y
- return self
-end
-
-- Size of single block
-function Selector:getSize()
+function Selector:getSize ()
return self.width, self.height
end
-function Selector:setSize(width, height)
+function Selector:setSize (width, height)
self.width, self.height = width, height
return self
end
-- Spacing between two blocks
-function Selector:getMargin()
+function Selector:getMargin ()
return self.margin
end
-function Selector:setMargin(margin)
+function Selector:setMargin (margin)
self.margin = margin
return self
end
--- General setter for Menu configuration files
-function Selector:set(name, func)
- if type(name) == "string" and func ~= nil then
- self[name] = func
- end
- return self
-end
-
-- Initialize Selector with current settings.
-function Selector:init()
+function Selector:init ()
-- Make sure that there is list present
if self.list == nil then
self.list = {}
@@ -105,17 +90,17 @@ function Selector:init()
end
-- Cycle through list on given number
-function Selector:next(n)
+function Selector:next (n)
local current = self.selections[n]
self:setSelection(n, current + 1)
end
-function Selector:previous(n)
+function Selector:previous (n)
local current = self.selections[n]
self:setSelection(n, current - 1)
end
-- Get number associated with a given set
-function Selector:checkNumber(set)
+function Selector:checkNumber (set)
if self.global then return 1 end -- For global Selector
for n,check in pairs(self.sets) do
if check == set then return n end
@@ -123,13 +108,13 @@ function Selector:checkNumber(set)
end
-- Check if given number is locked
-function Selector:isLocked(n)
+function Selector:isLocked (n)
local n = n or 1
return self.locks[n]
end
-- Sets value of selection of given number. Returns old.
-function Selector:setSelection(n, new)
+function Selector:setSelection (n, new)
-- Functception. It sounds like fun but it isn't.
local function limit(new, total)
if new > total then
@@ -147,18 +132,18 @@ function Selector:setSelection(n, new)
end
-- Get value of selection of given number
-function Selector:getSelection(n)
+function Selector:getSelection (n)
local n = n or 1
return self.selections[n]
end
-- Get value from list by selection
-function Selector:getListValue(i)
+function Selector:getListValue (i)
return self.list[i]
end
-- Checks if selection of given number is unique within Selector scope.
-function Selector:isUnique(n)
+function Selector:isUnique (n)
local selection = self:getSelection(n)
for fn,v in pairs(self.selections) do
if fn ~= n and self:isLocked(fn) and v == selection then
@@ -169,7 +154,7 @@ function Selector:isUnique(n)
end
-- Get list of selections, checks if not locked are allowed.
-function Selector:getFullSelection(allowed)
+function Selector:getFullSelection (allowed)
local allowed = allowed
if allowed == nil then allowed = false end
local t = {}
@@ -186,7 +171,7 @@ function Selector:getFullSelection(allowed)
end
-- Rolls and returns random selection from list that is not locked.
-function Selector:rollRandom(avoids)
+function Selector:rollRandom (avoids)
-- Me: You should make it simpler.
-- Inner me: Nah, it works. Leave it.
-- Me: Ok, let's leave it as it is.
@@ -209,7 +194,7 @@ function Selector:rollRandom(avoids)
end
-- Draw single block of Selector
-function Selector:drawBlock(n, x, y, scale)
+function Selector:drawBlock (n, x, y, scale)
if self.quads == nil or self.sprite == nil then return end
local x, y = x or 0, y or 0
local name = self:getListValue(self:getSelection(n))
@@ -249,16 +234,16 @@ function Selector:drawBlock(n, x, y, scale)
end
-- Menu callbacks
-function Selector:focus() -- Called when Element gains focus
+function Selector:focus () -- Called when Element gains focus
self.focused = true
return true
end
-function Selector:blur() -- Called when Element loses focus
+function Selector:blur () -- Called when Element loses focus
self.focused = false
end
-- LÖVE2D callbacks
-function Selector:draw(scale)
+function Selector:draw (scale)
local x,y = self:getPosition()
local margin = self:getMargin()
local width = self:getSize()
@@ -267,7 +252,7 @@ function Selector:draw(scale)
self:drawBlock(n, x+(margin+width)*(n-1)+margin*n, y, scale)
end
end
-function Selector:update(dt)
+function Selector:update (dt)
self.delay = self.delay + dt
if self.delay > Selector.delay then -- Selector.delay is initial
self.delay = self.delay - Selector.delay
@@ -276,7 +261,7 @@ end
-- Controller callbacks
-- TODO: Add action to perform when key is pressed and selector is locked in e.g. to move into character selection from map selection.
-function Selector:controlpressed(set, action, key)
+function Selector:controlpressed (set, action, key)
if set and self.focused then
local n = self:checkNumber(set)
local locked = self:isLocked(n)
@@ -301,6 +286,5 @@ function Selector:controlpressed(set, action, key)
end
end
end
-function Selector:controlreleased(set, action, key) end
-return Selector \ No newline at end of file
+return Selector