This commit is contained in:
SweetSea-ButImNotSweet
2024-05-21 20:07:53 +07:00
parent 7c39c1ada8
commit 5cf9deb9eb
7 changed files with 149 additions and 61 deletions

View File

@@ -61,7 +61,8 @@ local button = {
codeWhenPressed = NULL, codeWhenPressed = NULL,
codeWhenReleased = NULL, codeWhenReleased = NULL,
_hovering = false _hovering = false,
_pressed = false,
}; button.__index = button }; button.__index = button
function button:draw() function button:draw()
love.graphics.setLineWidth(self.borderWidth) love.graphics.setLineWidth(self.borderWidth)
@@ -107,14 +108,23 @@ function button:isHovering(x,y)
return self._hovering return self._hovering
end end
---Trigger press action, only when ``self._hovering`` is true ---Trigger press action, only when ``self._hovering`` is true
function button:press() function button:press(x, y)
if self._hovering then self.codeWhenPressed() end if self:isHovering(x, y) and not self._pressed then
self.codeWhenPressed()
self._pressed = true
end
end end
---Trigger release action, don't need ``self._hovering`` to ``true`` ---Trigger release action, don't need ``self._hovering`` to ``true``
function button:release() ---@param isMouse? boolean Button just released by mouse?
if self._hovering then function button:release(x, y, isTouch)
if self:isHovering(x, y) and self._pressed then
self.codeWhenReleased() self.codeWhenReleased()
self._hovering = false self._pressed = false
if isTouch then
self._hovering = false
else
self:isHovering(x, y)
end
end end
end end
@@ -204,4 +214,38 @@ function BUTTON.setDefaultOption(D)
end end
end end
-- < EXTRA GENERAL OPTIONS >
---Draw all buttons in provided list
---@param list table<BUTTON.button>
function BUTTON.draw(list)
for _, v in pairs(list) do v:draw() end
end
---Check if current mouse position is inside button<br>
---Calling BUTTON.press will trigger this, but you can call it when moving mouse so button can be highlighted when being hovered
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y nunber # Mouse position
function BUTTON.checkHovering(list, x, y)
for _, v in pairs(list) do v:isHovering(x, y) end
end
--- Trigger the press action, only if ``button._hovering == true``
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y nunber # Mouse position
function BUTTON.press(list, x, y)
for _, v in pairs(list) do v:press(x, y) end
end
---Trigger the release action
---@param list table<BUTTON.button>
---@param x number # Mouse position
---@param y nunber # Mouse position
---@param isMouse? boolean # Is mouse just released a button?
function BUTTON.release(list, x, y, isTouch)
for _, v in pairs(list) do v:release(x, y, isTouch) end
end
return BUTTON return BUTTON

View File

@@ -7,10 +7,13 @@ FONT_tromi = love.graphics.newFont('res/fonts/monofonto rg.otf', 28)
FONT_big = love.graphics.newFont('res/fonts/monofonto rg.otf', 56) FONT_big = love.graphics.newFont('res/fonts/monofonto rg.otf', 56)
local font_height = FONT_tromi:getHeight() * 0.5 local font_height = FONT_tromi:getHeight() * 0.5
local font_big_height = FONT_big:getHeight() * 0.5
-- BUTTON library -- BUTTON library
BUTTON = require "libs.simple-button" BUTTON = require "libs.simple-button"
BUTTON.setDefaultOption{ BUTTON.setDefaultOption{
draw = function(self) draw = function(self)
local need_big_font = (self.font == FONT_big)
love.graphics.setColor(0, 0, 0, 0.8) love.graphics.setColor(0, 0, 0, 0.8)
love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r) love.graphics.rectangle('fill', self.x, self.y, self.w, self.h, self.r)
@@ -21,14 +24,25 @@ BUTTON.setDefaultOption{
local lineAmount local lineAmount
do do
local _, t = FONT_tromi:getWrap(self.text, self.w * 2) local _, t
if need_big_font then
_, t = FONT_big:getWrap(self.text, self.w * 2)
else
_, t = FONT_tromi:getWrap(self.text, self.w * 2)
end
lineAmount = #t lineAmount = #t
end end
local textHeight = font_height * (lineAmount * 0.5) local _font_height = need_big_font and font_big_height or font_height
local textHeight = _font_height * (lineAmount * 0.5)
local textPos = self.y + (self.h * 0.5) - textHeight local textPos = self.y + (self.h * 0.5) - textHeight
drawText(self.text, self.x, textPos, self.w, 'center') if need_big_font then
drawBigText(self.text, self.x, textPos, self.w, 'center')
else
drawText(self.text, self.x, textPos, self.w, 'center')
end
love.graphics.setColor(1, 1, 1, 0.8) love.graphics.setColor(1, 1, 1, 0.8)
love.graphics.setLineWidth(1) love.graphics.setLineWidth(1)

View File

@@ -51,7 +51,10 @@ function love.load()
require "scene" require "scene"
require "game.vctrl" -- VCTRL require "game.vctrl" -- VCTRL
SCENE = SETTINGS.firstTime and InputConfigScene(true) or TitleScene() SCENE.update = function()
SCENE = SETTINGS.firstTime and InputConfigScene(true) or TitleScene()
end
-- VCTRL.toggle(love.system.getOS()=='Android' or true) -- VCTRL.toggle(love.system.getOS()=='Android' or true)
VCTRL.new{ -- up down left right --- right left down up VCTRL.new{ -- up down left right --- right left down up
-- {type='button',x= 100,y=320,key= 'up',r=35,iconSize=60,alpha=0.75}, -- {type='button',x= 100,y=320,key= 'up',r=35,iconSize=60,alpha=0.75},
@@ -116,11 +119,11 @@ function love.draw()
love.graphics.pop() love.graphics.pop()
end end
function love.mousepressed(x, y, b, _, presses) function love.mousepressed(x, y, b, isTouch, presses)
local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y) local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y)
SCENE:onInputPress{type = "mouse", x = x, y = y, button = b, presses = presses} SCENE:onInputPress{type = "mouse", x = x, y = y, button = b, presses = presses}
end end
function love.mousereleased(x,y, b, _, presses) function love.mousereleased(x, y, b, isTouch, presses)
local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y) local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y)
SCENE:onInputRelease{type = "mouse", x = x, y = y, button = b, presses = presses} SCENE:onInputRelease{type = "mouse", x = x, y = y, button = b, presses = presses}
end end
@@ -370,11 +373,10 @@ function love.run()
end end
function love.errorhandler(msg) function love.errorhandler(msg)
local msg = msg or "REV! BÀ ĂN CÁI TỜ TIN NHẮN LỖI RỒI À?!"
local showScreenshot = false local showScreenshot = false
local errorCopied = false local errorCopied = false
local enter_fullscreen = SETTINGS and SETTINGS["fullscreen"] or false
-- Reset audio.
if love.audio then love.audio.stop() end
-- Render everything again in a canva -- Render everything again in a canva
love.graphics.origin() love.graphics.origin()
@@ -403,13 +405,15 @@ function love.errorhandler(msg)
) )
love.graphics.setCanvas() love.graphics.setCanvas()
love.audio.stop()
-- Handling the error -- Handling the error
local err={"Error:"..msg} local err={"Error:"..msg}
local c=2 local c=2
for l in debug.traceback("",2):gmatch("(.-)\n") do for l in debug.traceback("",2):gmatch("(.-)\n") do
if c>2 then if c>2 then
if not l:find("boot") then if not l:find("boot") then
err[c]=l:gsub("^\t*","") err[c]=l:gsub("^\t*","\t")
c=c+1 c=c+1
end end
else else
@@ -434,17 +438,20 @@ function love.errorhandler(msg)
drawText([[ drawText([[
OH NO! Tromi has crashed. OH NO! Tromi has crashed.
Since this is not the official port, please do not report any bugs to mycophobia. Since this is not the official port, please do not report any bugs to mycophobia.
Instead, report this to me via my Discord ``sweetsea`` with a screenshot of this. Instead, report this to me via my Discord ``sweetsea'' with a screenshot of this.
REMEMBER TO SCREENSHOT BECAUSE ERROR INFO IS NOT SAVED! REMEMBER TO SCREENSHOT ERROR INFO BEFORE QUITTING BECAUSE THEY ARE NOT SAVED!
Ctrl + C: copy the error info | If you click or tap, a window appear with 4 options: Ctrl + C: copy the error info | If you click / tap, a window appears with 4 options:
Space : show/hide screenshot | OK: Quit Copy: copy error info Space : show/hide screenshot | OK : Quit Copy: copy error info
Escape : Quit | Cancel: Go back Show: show/hide screenshot Escape : Quit | Cancel: Go back Show: show/hide screenshot
]], 20, 10, 620, "left")
Traceback:]]..(errorCopied and " (Copied to clipboard)" or ""), drawText(err[1]:sub(7).."\nTraceback:"..(errorCopied and " (Copied to clipboard)\n" or "\n")..p, 20, 180, 620, "left")
20, 10, 620, "left") else
drawText(p, 40, 200, 600, "left") love.graphics.setColor(0, 0, 0, 0.8)
love.graphics.rectangle("fill", 15, 450, 400, 25, 5, 5)
drawText("Tromi has crashed! Press Space or tap to show error info", 15, 455, 400, "left")
end end
love.graphics.present() love.graphics.present()
@@ -471,10 +478,10 @@ Traceback:]]..(errorCopied and " (Copied to clipboard)" or ""),
elseif e == "keypressed" and a == "space" then elseif e == "keypressed" and a == "space" then
showScreenshot = not showScreenshot showScreenshot = not showScreenshot
elseif e == "keypressed" and a == "f4" then elseif e == "keypressed" and a == "f4" then
SETTINGS["fullscreen"] = not SETTINGS["fullscreen"] enter_fullscreen = not enter_fullscreen
love.window.setFullscreen(SETTINGS["fullscreen"]) love.window.setFullscreen(enter_fullscreen)
elseif e == "touchpressed" or e == "mousepressed" then elseif e == "mousepressed" then
local pressed = love.window.showMessageBox("Quit Tromi?", "Remember to save a copy of screenshot, since they are not saved!", buttons) local pressed = love.window.showMessageBox("Quit? Screenshot? Copy?", "Remember to screenshot error info before quitting because they are not saved!", buttons)
if pressed == 1 then if pressed == 1 then
return 1 return 1
elseif pressed == 3 then elseif pressed == 3 then

View File

@@ -4,7 +4,9 @@ SCENE = Object:extend()
function SCENE:new() end function SCENE:new() end
function SCENE:update() end function SCENE:update() end
function SCENE:render() end function SCENE:render()
love.graphics.draw(LOADING_IMAGE_FILE,0,0,0,0.5)
end
-- You can use the class SCENE_onInput to show suggestions for `e` table -- You can use the class SCENE_onInput to show suggestions for `e` table

View File

@@ -1,11 +1,22 @@
local NameEntryScene = SCENE:extend() local NameEntryScene = SCENE:extend()
NameEntryScene.title = "Game Start" NameEntryScene.title = "Game Start"
local buttonList = {
BUTTON.new{
text = "<\nCHAR", font = FONT_big,
x = 40, y = 160, w = 70, h = 70,
},
BUTTON.new{
text = ">\nCHAR", font = FONT_big,
x = 130, y = 160, w = 70, h = 70,
}
}
local Grid = require 'game.grid' local Grid = require 'game.grid'
local bitser = require 'libs.bitser' local bitser = require 'libs.bitser'
function NameEntryScene:new() function NameEntryScene:new()
self.chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890." self.hars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890."
self.char_pos = 1 self.char_pos = 1
self.name_entry = {'A','A','A'} self.name_entry = {'A','A','A'}
self.entry_pos = 1 self.entry_pos = 1
@@ -48,6 +59,8 @@ end
function NameEntryScene:render() function NameEntryScene:render()
MainBackground() MainBackground()
BUTTON.draw(buttonList)
love.graphics.setColor(1, 1, 1, 1) love.graphics.setColor(1, 1, 1, 1)
love.graphics.line(216,80,216,80+(16*self.grid.height)) love.graphics.line(216,80,216,80+(16*self.grid.height))
love.graphics.line(216+(16*self.grid.width),80,216+(16*self.grid.width),80+(16*self.grid.height)) love.graphics.line(216+(16*self.grid.width),80,216+(16*self.grid.width),80+(16*self.grid.height))
@@ -108,8 +121,16 @@ function NameEntryScene:update()
self.entry_chars = self.name_entry[1]..self.name_entry[2]..self.name_entry[3] self.entry_chars = self.name_entry[1]..self.name_entry[2]..self.name_entry[3]
end end
function NameEntryScene:onInputMove(e)
if e.type == "mouse" then
BUTTON.checkHovering(buttonList, e.x, e.y)
end
end
function NameEntryScene:onInputPress(e) function NameEntryScene:onInputPress(e)
if e.input == "menu_decide" or e.input == "rotate_left" or e.scancode == "return" then if e.type == "mouse" or e.type == "touch" then
BUTTON.press(buttonList, e.x, e.y)
elseif e.input == "menu_decide" or e.input == "rotate_left" or e.scancode == "return" then
self.delete_confirm = false self.delete_confirm = false
self.delete_input_count = 0 self.delete_input_count = 0
if self.entry_pos == 4 then if self.entry_pos == 4 then
@@ -177,7 +198,11 @@ function NameEntryScene:onInputPress(e)
end end
function NameEntryScene:onInputRelease(e) function NameEntryScene:onInputRelease(e)
if e.input == "left" or e.scancode == "left" or e.input == "right" or e.scancode == "right" then if e.type == "mouse" then
BUTTON.release(buttonList, e.x, e.y)
elseif e.type == "touch" then
BUTTON.release(buttonList, e.x, e.y, true)
elseif e.input == "left" or e.scancode == "left" or e.input == "right" or e.scancode == "right" then
self.direction = nil self.direction = nil
self.repeat_counter = self.repeat_limit-1 self.repeat_counter = self.repeat_limit-1
end end

View File

@@ -94,15 +94,29 @@ end
function TitleScene:onInputPress(e) function TitleScene:onInputPress(e)
if e.type == "touch" then if e.type == "touch" then
local selecting = math.floor((e.y - 198) / 20) local selecting = math.floor((e.y - 198) / 20)
if (e.x >= 20 and e.x <= 260) and (selecting > 0 and selecting <= #main_menu_screens) then if
(e.x >= 20 and e.x <= 260) and
(selecting > 0 and selecting <= #main_menu_screens)
then
if self.main_menu_state ~= selecting then if self.main_menu_state ~= selecting then
self.main_menu_state = selecting self.main_menu_state = selecting
else else
VCTRL.toggle(true) VCTRL.toggle(true)
SCENE = main_menu_screens[selecting]() SCENE = main_menu_screens[selecting]()
end end
elseif e.x >= 14 and e.y >= 40 and e.x <= 274 and e.y <= 170 then elseif
e.x >= 14 and
e.y >= 40 and
e.x <= 274 and
e.y <= 160
then
if e.x >= 137 then -- Right
table.remove(self.code, 8)
table.insert(self.code, 1, 1)
else -- Left
table.remove(self.code, 8)
table.insert(self.code, 1, -1)
end
end end
else else
if e.input == "menu_decide" or e.input == "rotate_left" or e.scancode == "return" then if e.input == "menu_decide" or e.input == "rotate_left" or e.scancode == "return" then

View File

@@ -7,7 +7,7 @@ TouchConfigScene.title = "Touchscreen config\n(you can tap anywhere on touch scr
2. Add behaviors 2. Add behaviors
]] ]]
local widgetList = { local buttonList = {
select_widget = BUTTON.new{ select_widget = BUTTON.new{
text = "Select key\n[Rotate right 2]", text = "Select key\n[Rotate right 2]",
x = 10, y = 10, w = 120, h = 110 x = 10, y = 10, w = 120, h = 110
@@ -33,6 +33,7 @@ local widgetList = {
BUTTON.new{ BUTTON.new{
text = "Opacity: 50%\nTap to reset", text = "Opacity: 50%\nTap to reset",
x = 255, y = 70, w = 100, h = 50, x = 255, y = 70, w = 100, h = 50,
codeWhenReleased = function() PlaySE("autopromote") end
}, },
BUTTON.new{ BUTTON.new{
text = "[+]\nTotally see", text = "[+]\nTotally see",
@@ -52,23 +53,7 @@ local widgetList = {
text = "RESET to\nDEFAULT", text = "RESET to\nDEFAULT",
x = 560, y = 70, w = 70, h = 50, x = 560, y = 70, w = 70, h = 50,
}, },
} }
local function widgetList_update()
for _, v in pairs(widgetList) do v:update() end
end
local function widgetList_draw()
for _, v in pairs(widgetList) do v:draw() end
end
local function widgetList_isHovering(x, y)
for _, v in pairs(widgetList) do v:isHovering(x, y) end
end
local function widgetList_press()
for _, v in pairs(widgetList) do v:press() end
end
local function widgetList_release()
for _, v in pairs(widgetList) do v:release() end
end
function TouchConfigScene:new() function TouchConfigScene:new()
-- TODO -- TODO
@@ -79,31 +64,28 @@ end
function TouchConfigScene:render() function TouchConfigScene:render()
MainBackground() MainBackground()
widgetList_draw() BUTTON.draw(buttonList)
end end
---@param e SCENE_onInput ---@param e SCENE_onInput
function TouchConfigScene:onInputMove(e) function TouchConfigScene:onInputMove(e)
if e.type == "mouse" then if e.type == "mouse" then
widgetList_isHovering(e.x, e.y) BUTTON.checkHovering(buttonList, e.x, e.y)
end end
end end
---@param e SCENE_onInput ---@param e SCENE_onInput
function TouchConfigScene:onInputPress(e) function TouchConfigScene:onInputPress(e)
if e.input == 'menu_back' then SCENE = InputConfigScene() end if e.input == 'menu_back' then SCENE = InputConfigScene() end
if e.type == "mouse" then widgetList_press() end if e.type == "mouse" or e.type == "touch" then
if e.type == "touch" then BUTTON.press(buttonList, e.x, e.y)
widgetList_isHovering(e.x,e.y)
widgetList_press()
end end
end end
---@param e SCENE_onInput ---@param e SCENE_onInput
function TouchConfigScene:onInputRelease(e) function TouchConfigScene:onInputRelease(e)
if e.type == "mouse" then if e.type == "mouse" then
widgetList_release() BUTTON.release(buttonList, e.x, e.y)
widgetList_isHovering(x, y)
elseif e.type == "touch" then elseif e.type == "touch" then
widgetList_release() BUTTON.release(buttonList, e.x, e.y, true)
end end
end end