From 40fdba2588784560ad2c26c6e60ecb471a9ecf51 Mon Sep 17 00:00:00 2001 From: Hyeonung Baek Date: Mon, 30 Dec 2024 19:32:56 +0900 Subject: [PATCH] Add basic functions --- .gitignore | 1 + TODO.md | 23 +++++++++++++++++++ bordered_window.lua | 19 --------------- button.lua | 32 ++++++++++++++++++++++++++ conf.lua | 8 +++++++ export.sh | 5 ++++ main.lua | 56 ++++++++++++++++++++++++++++++++++++++------- util.lua | 10 ++++++++ window.lua | 45 +++++++++++++++++------------------- 9 files changed, 148 insertions(+), 51 deletions(-) create mode 100644 .gitignore create mode 100644 TODO.md delete mode 100644 bordered_window.lua create mode 100644 button.lua create mode 100644 conf.lua create mode 100755 export.sh create mode 100644 util.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3159fbe --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.love diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..36498e0 --- /dev/null +++ b/TODO.md @@ -0,0 +1,23 @@ +ui system +game system + +seperated + +simple, minimalistic solution + +mousepressed = window.mousepressed(self, function(self,x, y, button) +end) + +draw = window.draw(self, function (self) +end) + +update + +- game + + screen + + items + + log + +- node + + x, y, width, height, + + draw, update, mousepressed diff --git a/bordered_window.lua b/bordered_window.lua deleted file mode 100644 index c80fe97..0000000 --- a/bordered_window.lua +++ /dev/null @@ -1,19 +0,0 @@ -local window = require("window") - -local w = {} - -function w.new(inner, x, y, w, h) - local mt = { __index = inner } - local a = {} - setmetatable(a, mt) - - function a:draw() - inner:draw() - love.graphics.rectangle("line", 0, 0, w, h) - end - - local r = window.new(a, x, y, h, w) - return r -end - -return w diff --git a/button.lua b/button.lua new file mode 100644 index 0000000..87baba0 --- /dev/null +++ b/button.lua @@ -0,0 +1,32 @@ +local window = require('window') + +local font = love.graphics.getFont() +local m = {} + +function m.new(text, callback, x, y, width, height) + local tw = font:getWidth(text) + local th = font:getHeight(text) + + local btn = { + text = text, + x = x, + y = y, + width = width, + height = height, + tx = (width - tw) / 2, + ty = (height - th) / 2 + } + + btn.mousepressed = window.mousepressed(function(self, x, y, button) + callback(x, y, button) + end) + + btn.draw = window.draw(function(self) + love.graphics.printf(self.text, self.tx, self.ty, self.width) + window.draw_border(self.width, self.height) + end) + + return btn +end + +return m diff --git a/conf.lua b/conf.lua new file mode 100644 index 0000000..02abbaa --- /dev/null +++ b/conf.lua @@ -0,0 +1,8 @@ +WIDTH = 320 +HEIGHT = 240 +MULTIPLIER = 1.5 + +function love.conf(t) + t.window.width = MULTIPLIER * WIDTH + t.window.height = MULTIPLIER * HEIGHT +end diff --git a/export.sh b/export.sh new file mode 100755 index 0000000..ddea6f1 --- /dev/null +++ b/export.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +rm garden.love +zip garden.love *.lua +cp garden.love ~/storage/downloads/ diff --git a/main.lua b/main.lua index 77987de..dc0030b 100644 --- a/main.lua +++ b/main.lua @@ -1,20 +1,60 @@ local window = require("window") -local bwindow = require("bordered_window") ---local button = require("button") +local button = require("button") function love.load() - b = {} - function b.draw() - love.graphics.print("A", 10, 10) + timer = 0 + bx, by = 0, 0 + cx, cy = 0, 0 + + main = { + x = 50, + y = 50, + rx = 50, ry = 50, width = 300, height = 200 +} + + function main:moverect() + self.rx = self.rx + 50 + self.ry = self.ry + 50 end - ba = bwindow.new(b, 200, 200, 50, 50) - -- canvas = love.graphics.newCanvas() + local m = main + cback = function (x, y) + cx, cy = x, y + m:moverect() + end + + btn = button.new('Move', cback, 10, 10, 50, 50) + + main.draw = window.draw(function(self) + love.graphics.rectangle('fill', self.rx, self.ry, 50, 50) + love.graphics.print(string.format("x %d", self.rx), 50, 100) + btn:draw() + end) + + main.mousepressed = window.mousepressed(function(self, x, y, button) + btn:mousepressed(x, y, button) + end) +end + +function love.mousepressed(x, y, button) + local x, y = x / MULTIPLIER, y / MULTIPLIER + bx, by = x, y + main:mousepressed(x, y, button) end function love.update(dt) + timer = timer + dt end function love.draw() - ba:draw() + love.graphics.push() + love.graphics.scale(MULTIPLIER) + + love.graphics.print(string.format("t: %d", timer), 0, 0) + love.graphics.print(string.format("%d %d", bx, by), 0, 20) + love.graphics.print(string.format("%d %d", cx, cy), 0, 40) + + main:draw() + + love.graphics.pop() end diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..16ee1e3 --- /dev/null +++ b/util.lua @@ -0,0 +1,10 @@ +local m = {} + +function m.between(x, a, b) + return a <= x and x <= b +end + +function m.inside(px, py, x, y, w, h) + return m.between(px, x, x+w) and + m.between(py, y, y+h) +end diff --git a/window.lua b/window.lua index 4aa7ea8..c1f6524 100644 --- a/window.lua +++ b/window.lua @@ -1,33 +1,30 @@ +-- primitive of drawing objects local w = {} -function w.new(inner, x, y, w, h) - local r = { - transform = love.math.newTransform(x, y), - width = w, - height = h - } - - function r:transformPoint(x, y) - local x, y = transform:transformPoint(x, y) - if 0 <= x and x <= width and 0 <= y and y <= height then - return x, y - else - return nil - end - end - - function r:mousepressed(m, x, y) - return inner:mousepressed(m, x, y) - end - - function r:draw() +function w.draw(f) + return function(self) love.graphics.push() - love.graphics.applyTransform(self.transform) - inner:draw() + love.graphics.translate(self.x, self.y) + f(self) love.graphics.pop() end +end - return r +function w.mousepressed(f) + return function(self, x, y, button) + local x, y = x - self.x, y - self.y + + if 0 <= x and x <= self.width and 0 <= y and y <= self.height then + f(self, x, y, button) + end + end +end + +function w.draw_border(width, height) + love.graphics.push('all') + love.graphics.setLineWidth(5) + love.graphics.rectangle('line', 0, 0, width, height, 5) + love.graphics.pop() end return w