Added labels and flushed-right UI elements.
This commit is contained in:
@@ -12,6 +12,7 @@ local Cursor = require 'src.ui.cursor'
|
|||||||
local Font = require 'src.ui.font'
|
local Font = require 'src.ui.font'
|
||||||
local SoundEffect = require 'src.sound.sfx'
|
local SoundEffect = require 'src.sound.sfx'
|
||||||
local VBox = require 'src.ui.vbox'
|
local VBox = require 'src.ui.vbox'
|
||||||
|
local Color = require 'src.utils.color'
|
||||||
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
@@ -33,9 +34,11 @@ function MainMenu:_init(name, index)
|
|||||||
-- Create sprites and buttons.
|
-- Create sprites and buttons.
|
||||||
self.background = Sprite('imgs/concb03.png')
|
self.background = Sprite('imgs/concb03.png')
|
||||||
self.btn_font = Font('fonts/Concrete.ttf')
|
self.btn_font = Font('fonts/Concrete.ttf')
|
||||||
|
self.title_font = Font('fonts/BBrick.ttf', 35)
|
||||||
|
|
||||||
-- Create UI elements.
|
-- Create UI elements.
|
||||||
self.btns = VBox(15, 15)
|
self.btns = VBox(15, 5)
|
||||||
|
self.btns:add_label('LoveDOS', self.title_font, Color(215, 0, 0), true)
|
||||||
self.btns:add_text_button('New Game', self.btn_font)
|
self.btns:add_text_button('New Game', self.btn_font)
|
||||||
self.btns:add_text_button('Load Game', self.btn_font)
|
self.btns:add_text_button('Load Game', self.btn_font)
|
||||||
self.btns:add_text_button('Options', self.btn_font)
|
self.btns:add_text_button('Options', self.btn_font)
|
||||||
@@ -51,6 +54,7 @@ function MainMenu:_init(name, index)
|
|||||||
|
|
||||||
-- Register all assets.
|
-- Register all assets.
|
||||||
assets:register(self.name, self.btn_font)
|
assets:register(self.name, self.btn_font)
|
||||||
|
assets:register(self.name, self.title_font)
|
||||||
assets:register(self.name, self.background)
|
assets:register(self.name, self.background)
|
||||||
assets:register(self.name, self.cursor)
|
assets:register(self.name, self.cursor)
|
||||||
assets:register(self.name, self.bgm)
|
assets:register(self.name, self.bgm)
|
||||||
@@ -75,6 +79,7 @@ end
|
|||||||
function MainMenu:draw()
|
function MainMenu:draw()
|
||||||
if assets:done(self.name) then
|
if assets:done(self.name) then
|
||||||
self.background:draw()
|
self.background:draw()
|
||||||
|
|
||||||
self.btns:draw()
|
self.btns:draw()
|
||||||
self.cursor:draw()
|
self.cursor:draw()
|
||||||
self.fade:draw()
|
self.fade:draw()
|
||||||
|
|||||||
@@ -4,26 +4,22 @@
|
|||||||
|
|
||||||
local make_class = require 'src.utils.classes'
|
local make_class = require 'src.utils.classes'
|
||||||
local collisions = require 'src.utils.colls'
|
local collisions = require 'src.utils.colls'
|
||||||
local Asset = require 'src.utils.asset'
|
local UIElement = require 'src.ui.element'
|
||||||
local Drawable = require 'src.graphics.drawable'
|
|
||||||
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
-- Class definitions
|
-- Class definitions
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
local Button = make_class(Drawable, Asset)
|
local Button = make_class(UIElement)
|
||||||
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
-- Class methods
|
-- Class methods
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
function Button:_init(x, y, callback)
|
function Button:_init(x, y, callback, float)
|
||||||
self.x = x
|
UIElement._init(self, x, y, float)
|
||||||
self.y = y
|
|
||||||
self.w = nil
|
|
||||||
self.h = nil
|
|
||||||
self.selected = false
|
self.selected = false
|
||||||
self.pressed = false
|
self.pressed = false
|
||||||
self.was_pressed = false
|
self.was_pressed = false
|
||||||
|
|||||||
41
src/ui/element.lua
Normal file
41
src/ui/element.lua
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Imports
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
local make_class = require 'src.utils.classes'
|
||||||
|
local Asset = require 'src.utils.asset'
|
||||||
|
local Drawable = require 'src.graphics.drawable'
|
||||||
|
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Class definitions
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
local UIElement = make_class(Drawable, Asset)
|
||||||
|
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Class methods
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function UIElement:_init(x, y, float)
|
||||||
|
Asset._init(self)
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = nil
|
||||||
|
self.h = nil
|
||||||
|
self.float_right = float and float or false
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function UIElement:set_dimensions()
|
||||||
|
self.w = 0
|
||||||
|
self.h = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Module return
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return UIElement
|
||||||
69
src/ui/label.lua
Normal file
69
src/ui/label.lua
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Imports
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
local love = require 'love'
|
||||||
|
local make_class = require 'src.utils.classes'
|
||||||
|
local UIElement = require 'src.ui.element'
|
||||||
|
local Color = require 'src.utils.color'
|
||||||
|
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Class definitions
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
local Label = make_class(UIElement)
|
||||||
|
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Class methods
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function Label:_init(x, y, text, font, color, float)
|
||||||
|
UIElement._init(self, x, y, float)
|
||||||
|
self.text = text
|
||||||
|
self.font = font
|
||||||
|
self.color = color ~= nil and color or Color(255, 255, 255)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Label:set_dimensions()
|
||||||
|
self.w = self.font:get_width(self.text)
|
||||||
|
self.h = self.font:get_height(self.text)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Label:load()
|
||||||
|
if not self.font:is_loaded() then
|
||||||
|
self.font:load()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If the label's size has not been computed then do it.
|
||||||
|
if self.w == nil or self.h == nil then self:set_dimensions() end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Label:unload()
|
||||||
|
self.font:unload()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Label:is_loaded()
|
||||||
|
return self.font:is_loaded() and self.w ~= nil and self.h ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Label:draw()
|
||||||
|
love.graphics.setColor(self.color.r, self.color.g, self.color.b)
|
||||||
|
self.font:set()
|
||||||
|
love.graphics.print(self.text, self.x, self.y)
|
||||||
|
self.font:unset()
|
||||||
|
love.graphics.setColor()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
-- Module return
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return Label
|
||||||
@@ -19,13 +19,14 @@ local TextButton = make_class(Button)
|
|||||||
-- Class methods
|
-- Class methods
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
function TextButton:_init(text, font, x, y, callback, base_col, sel_color, press_col)
|
function TextButton:_init(text, font, x, y, callback, float, base_col, sel_color, press_col)
|
||||||
Button._init(self, x, y, callback)
|
Button._init(self, x, y, callback, float)
|
||||||
self.font = font
|
self.font = font
|
||||||
self.text = text
|
self.text = text
|
||||||
self.base_col = base_col ~= nil and base_col or Color(255, 255, 255)
|
self.base_col = base_col ~= nil and base_col or Color(255, 255, 255)
|
||||||
self.sel_color = sel_color ~= nil and sel_color or Color(215, 0, 0)
|
self.sel_color = sel_color ~= nil and sel_color or Color(215, 0, 0)
|
||||||
self.press_col = press_col ~= nil and press_col or Color(99, 99, 139)
|
self.press_col = press_col ~= nil and press_col or Color(99, 99, 139)
|
||||||
|
self.disbl_col = Color(95, 63, 75)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function TextButton:draw()
|
function TextButton:draw()
|
||||||
local color = (self.pressed and self.press_col) or ((self.selected and self.sel_color) or self.base_col)
|
local color = self.callback == nil and self.disbl_col or ((self.pressed and self.press_col) or ((self.selected and self.sel_color) or self.base_col))
|
||||||
|
|
||||||
love.graphics.setColor(color.r, color.g, color.b)
|
love.graphics.setColor(color.r, color.g, color.b)
|
||||||
self.font:set()
|
self.font:set()
|
||||||
|
|||||||
@@ -2,10 +2,11 @@
|
|||||||
-- Imports
|
-- Imports
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
local make_class = require 'src.utils.classes'
|
local make_class = require 'src.utils.classes'
|
||||||
local Asset = require 'src.utils.asset'
|
local Asset = require 'src.utils.asset'
|
||||||
local Drawable = require 'src.graphics.drawable'
|
local Drawable = require 'src.graphics.drawable'
|
||||||
local TextButton = require 'src.ui.textbtn'
|
local Label = require 'src.ui.label'
|
||||||
|
local TextButton = require 'src.ui.textbtn'
|
||||||
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
@@ -20,37 +21,46 @@ local VBox = make_class(Drawable, Asset)
|
|||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
function VBox:_init(x, y, spacing)
|
function VBox:_init(x, y, spacing)
|
||||||
self.x = x ~= nil and x or 0
|
self.x = x ~= nil and x or 0
|
||||||
self.y = y ~= nil and y or 0
|
self.y = y ~= nil and y or 0
|
||||||
self.s = spacing ~= nil and spacing or 10
|
self.s = spacing ~= nil and spacing or 10
|
||||||
self.btns = {}
|
self.elements = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function VBox:add_text_button(text, font, callback, base_col, sel_color, press_col)
|
function VBox:add_text_button(text, font, callback, float, base_col, sel_color, press_col)
|
||||||
table.insert(self.btns, TextButton(text, font, 0, 0, callback, base_col, sel_color, press_col))
|
table.insert(self.elements, TextButton(text, font, 0, 0, callback, float, base_col, sel_color, press_col))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function VBox:add_label(text, font, color, float)
|
||||||
|
table.insert(self.elements, Label(0, 0, text, font, color, float))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function VBox:load()
|
function VBox:load()
|
||||||
local y = self.y
|
local _y = self.y
|
||||||
|
|
||||||
-- Load required assets if needed and then compute the coordinates of each button.
|
-- Load required assets if needed and then compute the coordinates of each button.
|
||||||
for i, v in pairs(self.btns) do
|
for _, v in pairs(self.elements) do
|
||||||
if v.is_a[Asset] then
|
if v.is_a[Asset] then
|
||||||
v:load()
|
v:load()
|
||||||
end
|
end
|
||||||
|
|
||||||
y = y + ((i > 1 and (self.btns[i - 1].h + self.s)) or 0)
|
if v.float_right then
|
||||||
|
v.x = 320 - self.x - v.w
|
||||||
|
else
|
||||||
|
v.x = self.x
|
||||||
|
end
|
||||||
|
|
||||||
v.x = self.x
|
v.y = _y
|
||||||
v.y = y
|
_y = _y + v.h + self.s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function VBox:unload()
|
function VBox:unload()
|
||||||
for _, v in pairs(self.btns) do
|
for _, v in pairs(self.elements) do
|
||||||
if v.is_a[Asset] then
|
if v.is_a[Asset] then
|
||||||
v:unload()
|
v:unload()
|
||||||
end
|
end
|
||||||
@@ -59,35 +69,35 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function VBox:update(dt)
|
function VBox:update(dt)
|
||||||
for _, v in pairs(self.btns) do
|
for _, v in pairs(self.elements) do
|
||||||
v:update(dt)
|
v:update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function VBox:draw()
|
function VBox:draw()
|
||||||
for _, v in pairs(self.btns) do
|
for _, v in pairs(self.elements) do
|
||||||
v:draw()
|
v:draw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function VBox:mousemoved(x, y, dx, dy)
|
function VBox:mousemoved(x, y, dx, dy)
|
||||||
for _, v in pairs(self.btns) do
|
for _, v in pairs(self.elements) do
|
||||||
v:mousemoved(x, y, dx, dy)
|
v:mousemoved(x, y, dx, dy)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function VBox:mousepressed(x, y, btn)
|
function VBox:mousepressed(x, y, btn)
|
||||||
for _, v in pairs(self.btns) do
|
for _, v in pairs(self.elements) do
|
||||||
v:mousepressed(x, y, btn)
|
v:mousepressed(x, y, btn)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function VBox:mousereleased(x, y, btn)
|
function VBox:mousereleased(x, y, btn)
|
||||||
for _, v in pairs(self.btns) do
|
for _, v in pairs(self.elements) do
|
||||||
v:mousereleased(x, y, btn)
|
v:mousereleased(x, y, btn)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user