create buttons

This commit is contained in:
jill 2021-09-19 13:32:12 +03:00
parent fbd5277cdb
commit 57ac1247fa
2 changed files with 86 additions and 0 deletions

81
button.lua Normal file
View file

@ -0,0 +1,81 @@
local self = {}
local buttons = {}
function self.get(index)
return buttons[index]
end
function self.kget(key)
for _, v in ipairs(buttons) do
if v.name == key then
return v
end
end
end
local buttonId
local function insertButton(tab, f)
buttonId = buttonId + 1
f.press = (self.kget(f.name) or {press = 1}).press
return table.insert(tab, f)
end
function self.createButtons()
local s = {}
buttonId = 0
--[[
insertButton(s, {
x = outerpadding,
y = love.graphics.getHeight() - outerpadding - fontHeight * 4 - padding * 2 - 32,
size = 32,
name = 'clipboard',
displayname = 'Copy to Clipboard'
})
]]
buttons = s
end
function self.update(dt)
for i, v in ipairs(buttons) do
local mx, my = love.mouse.getPosition()
local targetsize = 1
if mx > v.x and mx < v.x + v.size and my > v.y and my < v.y + v.size then
if love.mouse.isDown(1) then
targetsize = 0.8
else
targetsize = 0.95
end
end
v.press = mix(v.press, targetsize, dt * 12)
end
end
function self.render()
local mx, my = love.mouse.getPosition()
for i, v in ipairs(buttons) do
local x, y, w, h = v.x, v.y, v.size, v.size
w = w * v.press
h = h * v.press
x = x + (v.size - w) / 2
y = y + (v.size - h) / 2
local hovering = mx > x and mx < x + w and my > y and my < y + h and dropdown.openDropdown == 0
local clicking = hovering and love.mouse.isDown(1)
love.graphics.setColor(0, 0, 0, 1)
if hovering or dragging then
love.graphics.setColor(0.2, 0.2, 0.3, 1)
end
love.graphics.rectangle('fill', x, y, w, h)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('line', x, y, w, h)
end
end
return self

View file

@ -11,6 +11,7 @@ ease = require 'ease'
slider = require 'slider' slider = require 'slider'
dropdown = require 'dropdown' dropdown = require 'dropdown'
graph = require 'graph' graph = require 'graph'
button = require 'button'
modes = { modes = {
preview = 1, preview = 1,
@ -22,6 +23,7 @@ modes = {
function createUI() function createUI()
dropdown.createDropdowns() dropdown.createDropdowns()
slider.createSliders() slider.createSliders()
button.createButtons()
end end
require 'util' -- exports into global table require 'util' -- exports into global table
@ -49,6 +51,7 @@ function love.update(dt)
graph.update(dt) graph.update(dt)
slider.update(dt) slider.update(dt)
dropdown.update(dt) dropdown.update(dt)
button.update(dt)
end end
function love.draw() function love.draw()
@ -65,6 +68,8 @@ function love.draw()
love.graphics.setColor(0.2, 0.2, 0.3, 1) love.graphics.setColor(0.2, 0.2, 0.3, 1)
love.graphics.print('Box of Eases by oatmealine', outerpadding, sh - fontHeight - outerpadding) love.graphics.print('Box of Eases by oatmealine', outerpadding, sh - fontHeight - outerpadding)
button.render()
slider.render() slider.render()
dropdown.render() dropdown.render()