add button

This commit is contained in:
fnicon 2026-03-22 17:53:58 +09:00
parent eef53745f3
commit a7fd31a83f
9 changed files with 213 additions and 9 deletions

View File

@ -0,0 +1,106 @@
local vm = require"lib.vornmath"
local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()
local reap = require("lib.reap")
local BASE = reap.base_path(...)
function world:new()
world.super.new(self, BASE, "button")
end
function default_func()
end
local default = {
pos = vm.vec2(0, 0),
wh = vm.vec2(0, 0),
scale = 1,
origin = vm.vec2(0, 0),
label_origin = vm.vec2(0, 0),
label_color = {255/255, 255/255, 255/255},
bg_color = {0, 0, 0},
line_color = {1, 0, 0},
func = default_func,
label = "skip",
}
function world:load(entity, data)
self.data = data or default
end
-- x1 y1 -- (x1 + w1) y1
-- x2 y2
-- x1 (y1 + h1) -- (x1 + w1) (y1 + h1)
local function is_inside(x1,y1,w1,h1, x2,y2)
return x1 < x2 and
x2 < x1+w1 and
y1 < y2 and
y2 < y1+h1
end
local function draw_text(text, x, y, c)
local font_ix = test.draw_font_start()
test.draw_font_set_base_color(c[1], c[2], c[3])
test.draw_font(font_ix, text, x, y)
end
local function draw_outline(x, y, w, h, c)
test.draw_set_color(c[1], c[2], c[3])
-- 2──1
-- │ │ valid (counter clockwise)
-- 3──4
test.draw_line(
x + w, y,
x, y
)
test.draw_line(
x, y,
x, y + h
)
test.draw_line(
x, y + h,
x + w, y + h
)
test.draw_line(
x + w, y + h,
x + w, y
)
end
local function draw_box(x, y, w, h, c)
test.draw_set_color(c[1], c[2], c[3])
-- 2──1
-- │ │ valid (counter clockwise)
-- 3──4
test.draw_quad(
x + w, y,
x, y,
x, y + h,
x + w, y + h
)
end
local function draw_button(text, x, y, w, h, ox, oy, lox, loy, ct, cl, cb)
x = x + ox
y = y + oy
test.draw_line_quad_start()
draw_box(x, y, w, h, cb)
draw_outline(x, y, w, h, cl)
x = x + lox
y = y + loy
draw_text(text, x, y, ct)
end
function world:ui_draw()
draw_button(self.data.label,
self.data.pos[1], self.data.pos[2], self.data.wh[1], self.data.wh[2],
self.data.origin[1], self.data.origin[2],
self.data.label_origin[1], self.data.label_origin[2],
self.data.label_color, self.data.line_color, self.data.bg_color
)
end
return world

View File

@ -0,0 +1,96 @@
local wm = require"world_map"
local vm = require"lib.vornmath"
local main_wrapper = require "love_src.wrapper.lappy.world"
---@class wrappers.Concord.world : lappy.world
local world = main_wrapper:extend()
local reap = require("lib.reap")
local BASE = reap.base_path(...)
function world:new()
world.super.new(self, BASE, "main_menu")
self.data = {}
self.entities_data = {}
self.entities = {}
end
local default = {
}
local function to_race()
load_world(wm["race"])
end
local lw, lh = love.graphics.getDimensions()
local default_entities = {
{
type = "button",
name = "button_to_race",
components = {
button = require"love_src.src.new.world.main_menu.components.button"
},
data = {
button = {
pos = vm.vec2(lw/2, lh/2),
wh = vm.vec2(120, 60),
scale = 1,
origin = vm.vec2(-60, -30),
label_origin = vm.vec2(25, 12.5),
label_color = {255/255, 255/255, 255/255},
bg_color = {0, 1, 0},
line_color = {1, 0, 0},
func = to_race,
label = "play",
}
},
}
}
function world:load(data, entities_data)
self.data = data or default
self.entities_data = entities_data or default_entities
for _, d in pairs(self.entities_data) do
local e = {
type = d.type,
name = d.name,
components = {}
}
for t, c in pairs(d.components) do
e.components[t] = c()
e.components[t]:load(e, d.data[t])
end
table.insert(self.entities, e)
end
end
function world:update(dt)
for _, e in ipairs(self.entities) do
for t, c in pairs(e.components) do
c:update(dt)
end
end
end
function world:draw()
test.draw()
for _, e in ipairs(self.entities) do
for t, c in pairs(e.components) do
c:draw()
end
end
end
function world:ui_draw()
for _, e in ipairs(self.entities) do
for t, c in pairs(e.components) do
c:ui_draw()
end
end
end
return world

View File

@ -9,15 +9,15 @@ local BASE = reap.base_path(...)
local default = {
racers = {
base = require "love_src.src.new.components.racer"
base = require "love_src.src.new.world.race.components.racer"
}
}
local entities_default = {
{
components = {
racer = require("love_src.src.new.components.racer"),
actor = require("love_src.src.new.components.actor"),
racer = require("love_src.src.new.world.race.components.racer"),
actor = require("love_src.src.new.world.race.components.actor"),
},
type = "racer",
name = "racer 1",
@ -50,7 +50,7 @@ local entities_default = {
},
{
components = {
map = require"love_src.src.new.components.map"
map = require"love_src.src.new.world.race.components.map"
},
type = "map",
name = "map 1",
@ -113,6 +113,7 @@ function world:update(dt)
end
function world:draw()
test.draw()
for _, e in ipairs(self.entities) do
for t, c in pairs(e.components) do
c:draw()

View File

@ -191,7 +191,8 @@ end
local wm = require("world_map")
world = {
["race"] = require("love_src.src.new.world.race")()
["main_menu"] = require("love_src.src.new.world.main_menu.init")(),
["race"] = require("love_src.src.new.world.race.init")(),
-- ["top_down_race"] = require("love_src.src.world.top_down_race")(),
-- ["main_menu"] = require("love_src.src.world.main_menu")(),
-- ["1_intro"] = require("love_src.src.world.1_intro")(),
@ -200,7 +201,7 @@ world = {
-- ["train"] = require("love_src.src.world.train")(),
};
current = wm["race"]
current = wm["main_menu"]
function load_world(world_to_load)
current = world_to_load
@ -277,7 +278,7 @@ function love.run()
love_update(dt)
love_draw()
draw()
-- draw()
local mouse_down = love.mouse.isDown(1)
if mouse_down then

View File

@ -1,8 +1,8 @@
return {
["top_down_race"] = "top_down_race",
-- ["top_down_race"] = "top_down_race",
["main_menu"] = "main_menu",
["1_intro"] = "1_intro",
["2_town_square"] = "2_town_square",
["race"] = "race",
["train"] = "train"
-- ["train"] = "train"
}