From 9e43b02620a9f52f33052a3c624af398b729f1a9 Mon Sep 17 00:00:00 2001 From: bizcochito Date: Thu, 20 Jan 2022 15:18:40 +0100 Subject: [PATCH] implemented the button thing (badly(im sorry) --- data/scripts.lua | 1 + data/scripts/pause.lua | 49 ++++++++++++++++- data/scripts/ui/button.lua | 106 +++++++++++++++++++++++++++++++++++++ main.lua | 8 ++- 4 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 data/scripts/ui/button.lua diff --git a/data/scripts.lua b/data/scripts.lua index 48cea90..34b42c3 100644 --- a/data/scripts.lua +++ b/data/scripts.lua @@ -18,6 +18,7 @@ require "data/scripts/objects" require "data/scripts/debug" require "data/scripts/keybind" require "data/scripts/pause" +require "data/scripts/ui/button" -- game loop require "data/scripts/game" require "data/scripts/gameworld" diff --git a/data/scripts/pause.lua b/data/scripts/pause.lua index 499a3bb..adae5b1 100644 --- a/data/scripts/pause.lua +++ b/data/scripts/pause.lua @@ -12,8 +12,55 @@ function PauseUI() love.graphics.rectangle("fill", 0, 0, game.width, game.height) love.graphics.setColor(1,1,1,1) love.graphics.rectangle("fill", pauseX, pauseY, pauseWidth, pauseHeight) - + -- Buttons + for _, element in pairs(UIElement) do + element:Draw() + end -- Reset scale love.graphics.scale(2,2) end + +function MenuStep(menu) + -- first get mouse + local mouse_x, mouse_y = love.mouse.getPosition() + for _, element in pairs(UIElement) do + if element.type == "Button" then + element:checkMouse(mouse_x, mouse_y) + end + end + if menu == "pauseMenu" then + if PauseResume:getVariable() == true then + MenuExit(menu,0) + do_pause = false + elseif PauseExit:getVariable() == true then + love.event.quit() + end + else + + end +end + +function MenuExit(from,to) + for _, element in pairs(UIElement) do + element = nil + end + UIElement = {} + if from == "pauseMenu" then + PauseResume = nil + PauseOptions = nil + PauseExit = nil + end + menuPage = to or nil +end + +function MenuInit(menu) + local buttonStandard = {width = 200, height = 30, separation = 10} + -- main menu + if menu == "pauseMenu" then + -- elements + PauseResume = interfaceButton:New(game.width/2, game.height/2-buttonStandard.height-buttonStandard.separation, buttonStandard.width, buttonStandard.height, {false,true}, 1, {text = "Resume", color = {0,0,0.5}, color2 = {1,1,1}}) + PauseOptions = interfaceButton:New(game.width/2, game.height/2, buttonStandard.width, buttonStandard.height, {false,true}, 1, {text = "Options", color = {0,0,0.5}, color2 = {1,1,1}}) + PauseExit = interfaceButton:New(game.width/2, game.height/2+buttonStandard.height+buttonStandard.separation, buttonStandard.width, buttonStandard.height, {false,true}, 1, {text = "Exit", color = {0,0,0.5}, color2 = {1,1,1}}) + end +end diff --git a/data/scripts/ui/button.lua b/data/scripts/ui/button.lua new file mode 100644 index 0000000..2ff478e --- /dev/null +++ b/data/scripts/ui/button.lua @@ -0,0 +1,106 @@ +interfaceButton = {type = "Button"} + +-- centered buttons +function interfaceButton:New(x,y,w,h,table_values,value,style) + + o = {} + o.pos = { + x = x, + y = y + } + o.size = { + w = w, + h = h + + } + + o.values = table_values or {false,true} + o.value = value or 1 + o.target_variable = o.values[o.value] + + o.clicked = false + + o.style = { + text = style.text or nil, + color = style.color or {1,1,1}, + color2 = style.color2 or {0,0,0}, + --color3 = style.color3 or style.color2 or {0,0,0}, + alpha = style.alpha or 1, + scale = style.scale or 1, + scale_x = style.scale_x or 1, + scale_y = style.scale_y or 1, + scale_proportion = 1 + } + + o.style.unselected = { + scale_proportion = o.style.scale_proportion + } + + o.style.selected = { + scale_proportion = 1.5 + } + + + AddElement(o) + + setmetatable(o, self) + self.__index = self + + return o +end + +function interfaceButton:getVariable() + return self.target_variable +end +function AddElement(self) + table.insert(UIElement,self) + + self.id = #UIElement +end + +function interfaceButton:checkMouse(mouse_x, mouse_y) + if self.clicked == false + and mouse_x < self.pos.x + self.size.w/2 + and mouse_x > self.pos.x - self.size.w/2 + and mouse_y < self.pos.y + self.size.h/2 + and mouse_y > self.pos.y - self.size.h/2 then + o.style.scale_proportion = o.style.selected.scale_proportion + if love.mouse.isDown(1) then + self.clicked = true + self.value = self.value + 1 + if self.value > #self.values then + self.value = 1 + end + self.target_variable = self.values[self.value] + end + else + o.style.scale_proportion = o.style.unselected.scale_proportion + self.clicked = false + end +end + +function interfaceButton:Draw() + local c1, c2, c3, a = love.graphics.getColor() + + love.graphics.setColor(self.style.color[1],self.style.color[2],self.style.color[3],self.style.alpha) + love.graphics.rectangle( + "fill", + self.pos.x-(self.size.w/2)*self.style.scale_x*self.style.scale_proportion, + self.pos.y-(self.size.h/2)*self.style.scale_y*self.style.scale_proportion, + self.size.w *self.style.scale_x*self.style.scale_proportion, + self.size.h *self.style.scale_y*self.style.scale_proportion) + love.graphics.setColor(self.style.color2[1],self.style.color2[2],self.style.color2[3],self.style.alpha) + love.graphics.rectangle( + "line", + self.pos.x-(self.size.w/2)*self.style.scale_x*self.style.scale_proportion, + self.pos.y-(self.size.h/2)*self.style.scale_y*self.style.scale_proportion, + self.size.w *self.style.scale_x*self.style.scale_proportion, + self.size.h *self.style.scale_y*self.style.scale_proportion) + + if self.style.text ~= nil then + love.graphics.print(self.style.text,self.pos.x,self.pos.y) + else + love.graphics.print(tostring(self.target_variable),self.pos.x,self.pos.y) + end + love.graphics.setColor(c1,c2,c3,a) +end diff --git a/main.lua b/main.lua index df1769e..f1ba07e 100644 --- a/main.lua +++ b/main.lua @@ -13,6 +13,8 @@ function love.load() fps_draw = 0 fps_total = 0 + UIElement = {} + love.graphics.setColor(1,1,1) love.keyboard.setKeyRepeat(true) love.graphics.setDefaultFilter("nearest") -- good pixel @@ -64,13 +66,15 @@ function love.update(dt) fps_count = fps_count + 1 current_dt = dt - + --MenuStep + MenuStep(menuPage) --keypressed if Keybind:HasPressed(Keybind.menu.pause) then if do_pause then do_pause = false else - pausepage = 1 + menuPage = "pauseMenu" + MenuInit(menuPage) do_pause = true end end