Compare commits
2 Commits
bed93d1de4
...
2dea06f8c7
| Author | SHA1 | Date | |
|---|---|---|---|
| 2dea06f8c7 | |||
| 479ff952cf |
@@ -11,7 +11,8 @@ local Sprite = require 'src.graphics.sprite'
|
||||
local Cursor = require 'src.ui.cursor'
|
||||
local Font = require 'src.ui.font'
|
||||
local SoundEffect = require 'src.sound.sfx'
|
||||
local ButtonGroup = require 'src.ui.btngrp'
|
||||
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.
|
||||
self.background = Sprite('imgs/concb03.png')
|
||||
self.btn_font = Font('fonts/Concrete.ttf')
|
||||
self.title_font = Font('fonts/BBrick.ttf', 35)
|
||||
|
||||
-- Create UI elements.
|
||||
self.btns = ButtonGroup(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('Load Game', self.btn_font)
|
||||
self.btns:add_text_button('Options', self.btn_font)
|
||||
@@ -51,6 +54,7 @@ function MainMenu:_init(name, index)
|
||||
|
||||
-- Register all assets.
|
||||
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.cursor)
|
||||
assets:register(self.name, self.bgm)
|
||||
@@ -75,6 +79,7 @@ end
|
||||
function MainMenu:draw()
|
||||
if assets:done(self.name) then
|
||||
self.background:draw()
|
||||
|
||||
self.btns:draw()
|
||||
self.cursor:draw()
|
||||
self.fade:draw()
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local make_class = require 'src.utils.classes'
|
||||
local Asset = require 'src.utils.asset'
|
||||
local Drawable = require 'src.graphics.drawable'
|
||||
local TextButton = require 'src.ui.textbtn'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local ButtonGroup = make_class(Drawable, Asset)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function ButtonGroup:_init(x, y, spacing)
|
||||
self.x = x ~= nil and x or 0
|
||||
self.y = y ~= nil and y or 0
|
||||
self.s = spacing ~= nil and spacing or 10
|
||||
self.btns = {}
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:add_text_button(text, font, callback, base_col, sel_color, press_col)
|
||||
table.insert(self.btns, TextButton(text, font, 0, 0, callback, base_col, sel_color, press_col))
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:load()
|
||||
local y = self.y
|
||||
|
||||
-- Load required assets if needed and then compute the coordinates of each button.
|
||||
for i, v in pairs(self.btns) do
|
||||
if v.is_a[Asset] then
|
||||
v:load()
|
||||
end
|
||||
|
||||
y = y + ((i > 1 and (self.btns[i - 1].h + self.s)) or 0)
|
||||
|
||||
v.x = self.x
|
||||
v.y = y
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:unload()
|
||||
for _, v in pairs(self.btns) do
|
||||
if v.is_a[Asset] then
|
||||
v:unload()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:update(dt)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:update(dt)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:draw()
|
||||
for _, v in pairs(self.btns) do
|
||||
v:draw()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:mousemoved(x, y, dx, dy)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:mousemoved(x, y, dx, dy)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:mousepressed(x, y, btn)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:mousepressed(x, y, btn)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:mousereleased(x, y, btn)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:mousereleased(x, y, btn)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return ButtonGroup
|
||||
@@ -4,26 +4,22 @@
|
||||
|
||||
local make_class = require 'src.utils.classes'
|
||||
local collisions = require 'src.utils.colls'
|
||||
local Asset = require 'src.utils.asset'
|
||||
local Drawable = require 'src.graphics.drawable'
|
||||
local UIElement = require 'src.ui.element'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local Button = make_class(Drawable, Asset)
|
||||
local Button = make_class(UIElement)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Button:_init(x, y, callback)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.w = nil
|
||||
self.h = nil
|
||||
function Button:_init(x, y, callback, float)
|
||||
UIElement._init(self, x, y, float)
|
||||
self.selected = false
|
||||
self.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
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function TextButton:_init(text, font, x, y, callback, base_col, sel_color, press_col)
|
||||
Button._init(self, x, y, callback)
|
||||
function TextButton:_init(text, font, x, y, callback, float, base_col, sel_color, press_col)
|
||||
Button._init(self, x, y, callback, float)
|
||||
self.font = font
|
||||
self.text = text
|
||||
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.press_col = press_col ~= nil and press_col or Color(99, 99, 139)
|
||||
self.disbl_col = Color(95, 63, 75)
|
||||
end
|
||||
|
||||
|
||||
@@ -56,7 +57,7 @@ end
|
||||
|
||||
|
||||
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)
|
||||
self.font:set()
|
||||
|
||||
110
src/ui/vbox.lua
Normal file
110
src/ui/vbox.lua
Normal file
@@ -0,0 +1,110 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local make_class = require 'src.utils.classes'
|
||||
local Asset = require 'src.utils.asset'
|
||||
local Drawable = require 'src.graphics.drawable'
|
||||
local Label = require 'src.ui.label'
|
||||
local TextButton = require 'src.ui.textbtn'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local VBox = make_class(Drawable, Asset)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function VBox:_init(x, y, spacing)
|
||||
self.x = x ~= nil and x or 0
|
||||
self.y = y ~= nil and y or 0
|
||||
self.s = spacing ~= nil and spacing or 10
|
||||
self.elements = {}
|
||||
end
|
||||
|
||||
|
||||
function VBox:add_text_button(text, font, callback, float, 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
|
||||
|
||||
|
||||
function VBox:load()
|
||||
local _y = self.y
|
||||
|
||||
-- Load required assets if needed and then compute the coordinates of each button.
|
||||
for _, v in pairs(self.elements) do
|
||||
if v.is_a[Asset] then
|
||||
v:load()
|
||||
end
|
||||
|
||||
if v.float_right then
|
||||
v.x = 320 - self.x - v.w
|
||||
else
|
||||
v.x = self.x
|
||||
end
|
||||
|
||||
v.y = _y
|
||||
_y = _y + v.h + self.s
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function VBox:unload()
|
||||
for _, v in pairs(self.elements) do
|
||||
if v.is_a[Asset] then
|
||||
v:unload()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function VBox:update(dt)
|
||||
for _, v in pairs(self.elements) do
|
||||
v:update(dt)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function VBox:draw()
|
||||
for _, v in pairs(self.elements) do
|
||||
v:draw()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function VBox:mousemoved(x, y, dx, dy)
|
||||
for _, v in pairs(self.elements) do
|
||||
v:mousemoved(x, y, dx, dy)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function VBox:mousepressed(x, y, btn)
|
||||
for _, v in pairs(self.elements) do
|
||||
v:mousepressed(x, y, btn)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function VBox:mousereleased(x, y, btn)
|
||||
for _, v in pairs(self.elements) do
|
||||
v:mousereleased(x, y, btn)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return VBox
|
||||
Reference in New Issue
Block a user