diff --git a/src/gstates/menus/base.lua b/src/gstates/menus/base.lua index a82e576..68e1c59 100644 --- a/src/gstates/menus/base.lua +++ b/src/gstates/menus/base.lua @@ -28,7 +28,7 @@ function BaseMenu:_init(parent, name, index, bckg) -- Create background sprite and container. self.background = Sprite(bckg) - self.container = VBox(15, 5, love.graphics.getWidth()) + self.container = VBox(15, 5, love.graphics.getWidth() - 15) -- Register the background and container into the asset manager. assets:register(self.name, self.background) diff --git a/src/gstates/menus/mainmenu.lua b/src/gstates/menus/mainmenu.lua index 07667c7..b3568dd 100644 --- a/src/gstates/menus/mainmenu.lua +++ b/src/gstates/menus/mainmenu.lua @@ -6,6 +6,8 @@ local make_class = require 'src.utils.classes' local constants = require 'src.gstates.menus.const' local BaseMenu = require 'src.gstates.menus.base' local Color = require 'src.utils.color' +local Label = require 'src.ui.label' +local TextButton = require 'src.ui.textbtn' ------------------------------------------------------------------------------ @@ -23,12 +25,12 @@ function MainMenu:_init(parent, title_font, button_font) BaseMenu._init(self, parent, "Main Menu", constants.MAIN_MENU, 'imgs/concb03.png') -- Create UI elements of the main menu. - self.container:add_label('LoveDOS', title_font, Color(215, 0, 0), true) - self.container:add_text_button('New Game', button_font) - self.container:add_text_button('Load Game', button_font) - self.container:add_text_button('Options', button_font, function() parent.next_menu = constants.OPTIONS_MENU end) - self.container:add_text_button('Help & Story', button_font) - self.container:add_text_button('Quit', button_font, function() parent.next_state = -1 end) + self.container:add(Label(nil, nil, 'LoveDOS', title_font, Color(215, 0, 0), true)) + self.container:add(TextButton('New Game', button_font)) + self.container:add(TextButton('Load Game', button_font)) + self.container:add(TextButton('Options', button_font, nil, nil, function() parent.next_menu = constants.OPTIONS_MENU end)) + self.container:add(TextButton('Help & Story', button_font)) + self.container:add(TextButton('Quit', button_font, nil, nil, function() parent.next_state = -1 end)) end diff --git a/src/gstates/menus/options.lua b/src/gstates/menus/options.lua index ce8a12c..cbe2652 100644 --- a/src/gstates/menus/options.lua +++ b/src/gstates/menus/options.lua @@ -4,8 +4,12 @@ local make_class = require 'src.utils.classes' local constants = require 'src.gstates.menus.const' +local settings = require 'src.utils.settings' local BaseMenu = require 'src.gstates.menus.base' local Color = require 'src.utils.color' +local HBox = require 'src.ui.hbox' +local Label = require 'src.ui.label' +local TextButton = require 'src.ui.textbtn' ------------------------------------------------------------------------------ @@ -23,12 +27,28 @@ function OptionsMenu:_init(parent, title_font, button_font) BaseMenu._init(self, parent, "Options Menu", constants.OPTIONS_MENU, 'imgs/cpu.png') -- Create UI elements of the main menu. - self.container:add_label('Options', title_font, Color(215, 0, 0), true) - self.container:add_label('Enable Permadeath', button_font) - self.container:add_label('Controls', button_font) - self.container:add_label('Sound Volume', button_font) - self.container:add_label('Music Volume', button_font) - self.container:add_text_button('Go Back', button_font, function() parent.next_menu = constants.MAIN_MENU end) + self.container:add(Label(nil, nil, 'Options', title_font, Color(215, 0, 0), true)) + + local box = HBox() + box:add(Label(nil, nil, 'Enable Permadeath', button_font)) + box:add(Label(nil, nil, 'X', button_font)) + self.container:add(box) + + box = HBox() + box:add(Label(nil, nil, 'Controls', button_font)) + self.container:add(box) + + box = HBox() + box:add(Label(nil, nil, 'Sound Volume', button_font)) + self.container:add(box) + + box = HBox() + box:add(Label(nil, nil, 'Music Volume', button_font)) + self.container:add(box) + + box = HBox() + box:add(TextButton('Go Back', button_font, nil, nil, function() parent.next_menu = constants.MAIN_MENU end)) + self.container:add(box) end diff --git a/src/ui/hbox.lua b/src/ui/hbox.lua new file mode 100644 index 0000000..cd6c8f3 --- /dev/null +++ b/src/ui/hbox.lua @@ -0,0 +1,62 @@ +------------------------------------------------------------------------------ +-- Imports +------------------------------------------------------------------------------ + +local make_class = require 'src.utils.classes' +local Layout = require 'src.ui.layout' + + +------------------------------------------------------------------------------ +-- Class definitions +------------------------------------------------------------------------------ + +local HBox = make_class(Layout) + + +------------------------------------------------------------------------------ +-- Class methods +------------------------------------------------------------------------------ + +function HBox:_init(x, y, w, h, spacing) + Layout._init(self, x, y, w, h, spacing) +end + + +function HBox:load() + Layout.load(self) + + local _x = self.x + + -- Load required assets if needed and then compute the coordinates of each button. + for _, v in pairs(self.elements) do + v.y = self.y + + v.x = _x + _x = _x + v.w + self.s + end +end + + +function HBox:set_dimensions() + if self.w == nil then + self.w = 0 + for i, v in pairs(self.elements) do + self.w = self.w + v.w + if i < #self.elements then self.w = self.w + self.s end + end + end + + if self.h == nil then + self.h = 0 + for _, v in pairs(self.elements) do + if v.h > self.h then self.h = v.h end + end + end +end + + +------------------------------------------------------------------------------ +-- Module return +------------------------------------------------------------------------------ + +return HBox diff --git a/src/ui/layout.lua b/src/ui/layout.lua new file mode 100644 index 0000000..3207c65 --- /dev/null +++ b/src/ui/layout.lua @@ -0,0 +1,96 @@ +------------------------------------------------------------------------------ +-- Imports +------------------------------------------------------------------------------ + +local make_class = require 'src.utils.classes' +local UIElement = require 'src.ui.element' + + +------------------------------------------------------------------------------ +-- Class definitions +------------------------------------------------------------------------------ + +local Layout = make_class(UIElement) + + +------------------------------------------------------------------------------ +-- Class methods +------------------------------------------------------------------------------ + +function Layout:_init(x, y, w, h, spacing, float) + UIElement._init(self, x, y, float) + self.x = x ~= nil and x or 0 + self.y = y ~= nil and y or 0 + self.w = w + self.h = h + self.s = spacing ~= nil and spacing or 10 + self.elements = {} +end + + +function Layout:add(element) + table.insert(self.elements, element) +end + + +function Layout:load() + -- Load required assets if needed and then compute the coordinates of each button. + for _, v in pairs(self.elements) do + if v.is_a[UIElement] then + v:load() + end + end + + self:set_dimensions() +end + + +function Layout:unload() + for _, v in pairs(self.elements) do + if v.is_a[UIElement] then + v:unload() + end + end +end + + +function Layout:update(dt) + for _, v in pairs(self.elements) do + v:update(dt) + end +end + + +function Layout:draw() + for _, v in pairs(self.elements) do + v:draw() + end +end + + +function Layout:mousemoved(x, y, dx, dy) + for _, v in pairs(self.elements) do + v:mousemoved(x, y, dx, dy) + end +end + + +function Layout:mousepressed(x, y, btn) + for _, v in pairs(self.elements) do + v:mousepressed(x, y, btn) + end +end + + +function Layout:mousereleased(x, y, btn) + for _, v in pairs(self.elements) do + v:mousereleased(x, y, btn) + end +end + + +------------------------------------------------------------------------------ +-- Module return +------------------------------------------------------------------------------ + +return Layout diff --git a/src/ui/vbox.lua b/src/ui/vbox.lua index bdb6612..1424df6 100644 --- a/src/ui/vbox.lua +++ b/src/ui/vbox.lua @@ -3,17 +3,14 @@ ------------------------------------------------------------------------------ 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' +local Layout = require 'src.ui.layout' ------------------------------------------------------------------------------ -- Class definitions ------------------------------------------------------------------------------ -local VBox = make_class(Drawable, Asset) +local VBox = make_class(Layout) ------------------------------------------------------------------------------ @@ -21,36 +18,19 @@ local VBox = make_class(Drawable, Asset) ------------------------------------------------------------------------------ function VBox:_init(x, y, w, h, spacing) - self.x = x ~= nil and x or 0 - self.y = y ~= nil and y or 0 - self.w = w ~= nil and w or 0 - self.h = h ~= nil and h 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)) + Layout._init(self, x, y, w, h, spacing) end function VBox:load() + Layout.load(self) + 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 = self.w - self.x - v.w + v.x = self.w - v.w else v.x = self.x end @@ -61,50 +41,23 @@ function VBox:load() end -function VBox:unload() - for _, v in pairs(self.elements) do - if v.is_a[Asset] then - v:unload() +function VBox:set_dimensions() + if self.w == nil then + self.w = 0 + for _, v in pairs(self.elements) do + if v.w > self.w then self.w = v.w end + end + end + + if self.h == nil then + self.h = 0 + for i, v in pairs(self.elements) do + self.h = self.h + v.h + if i < #self.elements then self.h = self.h + self.s end 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 ------------------------------------------------------------------------------