From 2b916ca2adca895f33526cfa21b93f8f50258ad5 Mon Sep 17 00:00:00 2001 From: SweetSea-ButImNotSweet <106439598+SweetSea-ButImNotSweet@users.noreply.github.com> Date: Wed, 22 May 2024 19:17:17 +0700 Subject: [PATCH] Moving VCTRL related calls and adding buttons for name entry screen --- game/gamemode.lua | 33 ++++++++----------------- game/vctrl.lua | 56 ++++++++++++++++++++++++++---------------- libs/simple-button.lua | 36 ++++++++++++++++++--------- main.lua | 17 ++----------- scene/game.lua | 9 +++++-- scene/name_entry.lua | 36 +++++++++++++++++++++------ scene/title.lua | 55 ++++++++++++++++++++--------------------- scene/touch_config.lua | 10 ++++++-- scene/training.lua | 7 ++++-- 9 files changed, 146 insertions(+), 113 deletions(-) diff --git a/game/gamemode.lua b/game/gamemode.lua index d7f930d..50ba8dc 100644 --- a/game/gamemode.lua +++ b/game/gamemode.lua @@ -13,6 +13,8 @@ local Piece = require 'game.piece' local GameMode = Object:extend() function GameMode:new(player_name, input_file, replay_grade) + VCTRL.toggle(true) + if player_name == nil then self.training = true else self.training = false end if input_file ~= nil then input_file = love.filesystem.newFile(REPLAY_DIR..input_file, 'r'):read() @@ -308,15 +310,6 @@ function GameMode:getReplayInputs(input_file) local replay_inputs = {} local semicolon_position = string.find(input_file, ';', 1, true) - -- for _=1,1e99 do - -- local c = string.sub(input_file,_,_) - -- if c == ';' then - -- reached_inputs = true - -- i = _ + 1 - -- end - -- if reached_inputs then break end - -- end - if semicolon_position then for i = semicolon_position + 1, #input_file, 3 do local input_list = {right = false, rotate_right2 = false, up = false, rotate_left = false, left = false, down = false, rotate_right = false, rotate_left2 = false} @@ -346,12 +339,6 @@ function GameMode:getInputPieceSeq(input_file) input_file, string_start, string.find(input_file, ';', string_start, true) - 1 ) -- seed - -- for i = string_start, #input_file do - -- local c = string.sub(input_file, i, i) - -- if c == ';' then break - -- else seed = seed..c - -- end - -- end end function GameMode:update(inputs, ruleset) @@ -936,19 +923,19 @@ end function GameMode:drawInputDisplay(left, top) if self.replay_inputs[self.frames] ~= nil then - drawText("+", left+10, top+8, 1000, "left") - drawText("v", left+10, top+18, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['down']),1,1-boolToInt(self.replay_inputs[self.frames]['down']),1}) - drawText("<", left, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['left']),1,1-boolToInt(self.replay_inputs[self.frames]['left']),1}) - drawText(">", left+20, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['right']),1,1-boolToInt(self.replay_inputs[self.frames]['right']),1}) + drawText("·", left+10, top+8, 1000, "left") + drawText("↓", left+10, top+18, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['down']),1,1-boolToInt(self.replay_inputs[self.frames]['down']),1}) + drawText("←", left, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['left']),1,1-boolToInt(self.replay_inputs[self.frames]['left']),1}) + drawText("→", left+20, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['right']),1,1-boolToInt(self.replay_inputs[self.frames]['right']),1}) drawText("L", left+35, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['rotate_left']),1,1-boolToInt(self.replay_inputs[self.frames]['rotate_left']),1}) drawText("R", left+50, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['rotate_right']),1,1-boolToInt(self.replay_inputs[self.frames]['rotate_right']),1}) drawText("L", left+65, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['rotate_left2']),1,1-boolToInt(self.replay_inputs[self.frames]['rotate_left2']),1}) drawText("R", left+80, top+8, 1000, "left",{1-boolToInt(self.replay_inputs[self.frames]['rotate_right2']),1,1-boolToInt(self.replay_inputs[self.frames]['rotate_right2']),1}) else - drawText("+", left+10, top+8, 1000, "left") - drawText("v", left+10, top+18, 1000, "left") - drawText("<", left, top+8, 1000, "left") - drawText(">", left+20, top+8, 1000, "left") + drawText("·", left+10, top+8, 1000, "left") + drawText("↓", left+10, top+18, 1000, "left") + drawText("←", left, top+8, 1000, "left") + drawText("→", left+20, top+8, 1000, "left") drawText("L", left+35, top+8, 1000, "left") drawText("R", left+50, top+8, 1000, "left") drawText("L", left+65, top+8, 1000, "left") diff --git a/game/vctrl.lua b/game/vctrl.lua index 935fa1b..d6fc295 100644 --- a/game/vctrl.lua +++ b/game/vctrl.lua @@ -73,44 +73,48 @@ end function control_type.button:reset() self.pressed=false self.lastPressTime=-1e99 + self.pressingID=false end -function control_type.button:press() +function control_type.button:press(_,_,id) self.pressed=true self.lastPressTime=love.timer.getTime() + self.pressing=id -- love.keypressed(self.key, love.keyboard.getScancodeFromKey(self.key)) SCENE:onInputPress{input=self.key,type="virtual"} end function control_type.button:release() self.pressed=false + self.pressingID=false -- love.keyreleased(self.key,love.keyboard.getScancodeFromKey(self.key)) SCENE:onInputRelease{input=self.key,type="virtual"} end function control_type.button:drag(dx,dy) self.x,self.y=self.x+dx,self.y+dy end -function control_type.button:draw(forceLight) +function control_type.button:draw(forceAlpha) + local alpha = forceAlpha or self.alpha love.graphics.setLineWidth(4) if self.shape=='circle' then - love.graphics.setColor(0,0,0,self.alpha) + love.graphics.setColor(0,0,0,alpha) love.graphics.circle('fill',self.x,self.y,self.r-4) - love.graphics.setColor(1,1,1,self.pressed and .5 or .05) + love.graphics.setColor(1,1,1,self.pressed and .5 or 0) love.graphics.circle('fill',self.x,self.y,self.r-4) - love.graphics.setColor(1,1,1) + love.graphics.setColor(1,1,1,alpha) love.graphics.circle('line',self.x,self.y,self.r-2) elseif self.shape=='square' then - love.graphics.setColor(0,0,0,self.alpha) + love.graphics.setColor(0,0,0,alpha) love.graphics.rectangle('fill',self.x-self.r-4,self.y-self.r-4,self.r*2+8,self.r*2+8) - love.graphics.setColor(1,1,1,self.pressed and .5 or .05) + love.graphics.setColor(1,1,1,self.pressed and .5 or 0) love.graphics.rectangle('fill',self.x-self.r-4,self.y-self.r-4,self.r*2+8,self.r*2+8) - love.graphics.setColor(1,1,1) + love.graphics.setColor(1,1,1,alpha) love.graphics.rectangle('line',self.x-self.r-2,self.y-self.r-2,self.r*2+4,self.r*2+4) end if self.iconSize>0 and self.quad then - love.graphics.setColor(1,1,1,forceLight and 1 or self.alpha) + love.graphics.setColor(1,1,1,alpha) local _,_,w,h=self.quad:getViewport() mDrawQ( virtual_texture, @@ -133,8 +137,7 @@ local global_toggle=false VCTRL={} VCTRL.focus=nil -- Focusing buttons ----@param ... table ----@class data +---@class VCTRL.data ---@field type string ---@field x number ---@field y number @@ -143,6 +146,8 @@ VCTRL.focus=nil -- Focusing buttons ---@field iconSize number ---@field alpha number ---@field show boolean + +---@param ... VCTRL.data ---Adding (multiple) virtual button(s) function VCTRL.new(...) for _,d in pairs(...) do @@ -156,20 +161,28 @@ end ---Enabling virtual control or not function VCTRL.toggle(toggle) if not toggle then - -- Release all buttons to prevent ghost keys + -- Release all buttons to prevent button ghost situation for id, b in pairs(touches) do b:release(id) - touches[id]=nil + touches[id]=nil end end global_toggle=toggle end +function VCTRL.clearAll() + local toggle = global_toggle + VCTRL.toggle(false) + global_toggle = toggle + + for i=#VCTRL,1,-1 do VCTRL[i] = nil end + collectgarbage() +end + function VCTRL.press(x,y,id) if not global_toggle then return end local obj,closestDist=false,1e99 - for i=1,#VCTRL do - local w=VCTRL[i] + for _, w in ipairs(VCTRL) do if w.show then local d=w:getDistance(x,y) if d<=1 and d ---@param x number # Mouse position ---@param y number # Mouse position -function BUTTON.press(list, x, y) - for _, v in pairs(list) do v:press(x, y) end +function BUTTON.press(list, x, y, touchID) + for _, v in pairs(list) do v:press(x, y, touchID) end end ---Trigger the release action ---@param list table ----@param x number # Mouse position ----@param y number # Mouse position -function BUTTON.release(list, x, y) - for _, v in pairs(list) do v:release(x, y) end +function BUTTON.release(list, x, y, touchID) + for _, v in pairs(list) do v:release(x, y, touchID) end end return BUTTON \ No newline at end of file diff --git a/main.lua b/main.lua index 21e245f..0552f09 100644 --- a/main.lua +++ b/main.lua @@ -97,8 +97,6 @@ function love.draw() love.graphics.push() SCENE:render() - VCTRL.draw() - -- -- Grid system -- local grid_width, grid_height = 40, 20 -- love.graphics.replaceTransform(GLOBAL_TRANSFORM) @@ -114,10 +112,6 @@ function love.draw() -- love.graphics.line(0, grid_height * iy, 640, grid_height * iy) -- end - love.graphics.setColor(0, 0, 0, 0.8) - love.graphics.rectangle("fill", 10, 445, 400, 30) - drawText("X: "..(LAST_X_POSITION or "--------").."; Y: "..(LAST_Y_POSITION or "--------"), 10, 455, 400, "left") - love.graphics.pop() end @@ -142,22 +136,15 @@ end function love.touchpressed(id,x,y) local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y) - if not VCTRL.press(x,y,id) then SCENE:onInputPress{type = "touch", x = x, y = y, dx = 0, dy = 0, id = id} - end end function love.touchdragged(id,x,y,dx,dy) local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y) - if not VCTRL.drag(x,y,id) then - SCENE:onInputDrag{type = "touch", x = x, y = y, dx = dx, dy = dy, id = id} - end + SCENE:onInputDrag{type = "touch", x = x, y = y, dx = dx, dy = dy, id = id} end function love.touchreleased(id,x,y) local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y) - if not VCTRL.release(id) then - SCENE:onInputRelease{type = "touch", x = x, y = y, dx = 0, dy = 0, id = id} - LAST_X_POSITION, LAST_Y_POSITION = x, y - end + SCENE:onInputRelease{type = "touch", x = x, y = y, dx = 0, dy = 0, id = id} end function love.keypressed(key, scancode) diff --git a/scene/game.lua b/scene/game.lua index 537463c..6fdb16f 100644 --- a/scene/game.lua +++ b/scene/game.lua @@ -69,10 +69,13 @@ end function GameScene:render() self.game:draw(self.paused) + VCTRL.draw() end function GameScene:onInputPress(e) - if (self.game.game_over or self.game.completed) and (e.input == "menu_decide" or e.input == "menu_back" or e.input == "rotate_right") and self.game.game_over_frames > 50 then + if e.type == "touch" then VCTRL.press(e.x, e.y, e.id) + + elseif (self.game.game_over or self.game.completed) and (e.input == "menu_decide" or e.input == "menu_back" or e.input == "rotate_right") and self.game.game_over_frames > 50 then SCENE = TitleScene() elseif tas and e.input == "menu_decide" then self:update(false, true) @@ -110,7 +113,9 @@ function GameScene:onInputPress(e) end function GameScene:onInputRelease(e) - if e.input and string.sub(e.input, 1, 5) ~= "menu_" then + if e.type == "touch" then + VCTRL.release(e.id) + elseif e.input and string.sub(e.input, 1, 5) ~= "menu_" then self.inputs[e.input] = false end end diff --git a/scene/name_entry.lua b/scene/name_entry.lua index 156017f..c098e09 100644 --- a/scene/name_entry.lua +++ b/scene/name_entry.lua @@ -3,20 +3,38 @@ NameEntryScene.title = "Game Start" local buttonList = { BUTTON.new{ - text = "<\nCHAR", font = FONT_big, - x = 40, y = 160, w = 70, h = 70, + text = "←\nCHAR", font = FONT_big, + x = 25, y = 120, w = 80, h = 80, + codeWhenPressed = function() SCENE:onInputPress {input = "left"} end, + codeWhenReleased = function() SCENE:onInputRelease{input = "left"} end, }, BUTTON.new{ - text = ">\nCHAR", font = FONT_big, - x = 130, y = 160, w = 70, h = 70, - } + text = "→\nCHAR", font = FONT_big, + x = 115, y = 120, w = 80, h = 80, + codeWhenPressed = function() SCENE:onInputPress {input = "right"} end, + codeWhenReleased = function() SCENE:onInputRelease{input = "right"} end, + }, + BUTTON.new{ + text = "↑\nESC", font = FONT_big, + x = 25, y = 210, w = 80, h = 80, + codeWhenPressed = function() SCENE:onInputPress {input = "menu_back"} end, + codeWhenReleased = function() SCENE:onInputRelease{input = "menu_back"} end, + }, + BUTTON.new{ + text = "↓\nENTER", font = FONT_big, + x = 115, y = 210, w = 80, h = 80, + codeWhenPressed = function() SCENE:onInputPress {input = "menu_decide"} end, + codeWhenReleased = function() SCENE:onInputRelease{input = "menu_decide"} end, + }, } local Grid = require 'game.grid' local bitser = require 'libs.bitser' function NameEntryScene:new() - self.hars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890." + VCTRL.toggle(false) + + self.chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890." self.char_pos = 1 self.name_entry = {'A','A','A'} self.entry_pos = 1 @@ -129,11 +147,12 @@ end function NameEntryScene:onInputPress(e) if e.type == "mouse" or e.type == "touch" then - BUTTON.press(buttonList, e.x, e.y) + BUTTON.press(buttonList, e.x, e.y, e.id) elseif e.input == "menu_decide" or e.input == "rotate_left" or e.scancode == "return" then self.delete_confirm = false self.delete_input_count = 0 if self.entry_pos == 4 then + BUTTON.release(buttonList, e.x, e.y, e.id) SETTINGS['last_entry'] = name:upper() SCENE = GameScene(name:lower()) else @@ -164,6 +183,7 @@ function NameEntryScene:onInputPress(e) self.delete_confirm = false self.delete_input_count = 0 if self.entry_pos == 1 then + BUTTON.release(buttonList, true) SCENE = TitleScene() else self.name_entry[self.entry_pos] = 'A' @@ -199,7 +219,7 @@ end function NameEntryScene:onInputRelease(e) if e.type == "mouse" or e.type == "touch" then - BUTTON.release(buttonList, e.x, e.y) + BUTTON.release(buttonList, e.x, e.y, e.id) elseif e.input == "left" or e.scancode == "left" or e.input == "right" or e.scancode == "right" then self.direction = nil self.repeat_counter = self.repeat_limit-1 diff --git a/scene/title.lua b/scene/title.lua index 14e2351..3c2bebb 100644 --- a/scene/title.lua +++ b/scene/title.lua @@ -58,7 +58,6 @@ function TitleScene:render() love.graphics.setColor(0,0,0,0.7) love.graphics.rectangle("fill", 14, 174, 260, 210, 10, 10) love.graphics.setColor(0.4, 1, 1, 0.5) - love.graphics.rectangle("fill", 14, 40, 260, 120, 10, 10) love.graphics.rectangle("fill", 20, 198 + 20 * self.main_menu_state, 240, 22) -- drawBigText('Tromi', 30, 180, 120, "left") @@ -93,7 +92,33 @@ end function TitleScene:onInputPress(e) if e.type == "touch" then - + if -- PENTO mode + 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 + else -- Select option from menu + 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 self.main_menu_state ~= selecting then + self.main_menu_state = selecting + else + VCTRL.toggle(true) + SCENE = main_menu_screens[selecting]() + end + end + end else if e.input == "menu_decide" or e.input == "rotate_left" or e.scancode == "return" then SCENE = main_menu_screens[self.main_menu_state]() @@ -112,32 +137,6 @@ function TitleScene:onInputPress(e) end function TitleScene:onInputRelease(e) - if e.type ~= "touch" then return end - 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 self.main_menu_state ~= selecting then - self.main_menu_state = selecting - else - VCTRL.toggle(true) - SCENE = main_menu_screens[selecting]() - end - 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 return TitleScene diff --git a/scene/touch_config.lua b/scene/touch_config.lua index 7ebad83..4d8f391 100644 --- a/scene/touch_config.lua +++ b/scene/touch_config.lua @@ -7,6 +7,9 @@ TouchConfigScene.title = "Touchscreen config\n(you can tap anywhere on touch scr 2. Add behaviors ]] +local focusingButton +local selectingButton + local buttonList = { select_button = BUTTON.new{ text = "Select key\n[Rotate right 2]", @@ -56,6 +59,9 @@ local buttonList = { } function TouchConfigScene:new() + VCTRL.toggle(true) + focusingButton = false + selectingButton = nil -- TODO end function TouchConfigScene:update() @@ -78,13 +84,13 @@ end function TouchConfigScene:onInputPress(e) if e.input == 'menu_back' then SCENE = InputConfigScene() end if e.type == "mouse" or e.type == "touch" then - BUTTON.press(buttonList, e.x, e.y) + BUTTON.press(buttonList, e.x, e.y, e.type == "touch") end end ---@param e SCENE_onInput function TouchConfigScene:onInputRelease(e) if e.type == "mouse" or e.type == "touch" then - BUTTON.release(buttonList, e.x, e.y) + BUTTON.release(buttonList, e.x, e.y, e.type == "touch") end end diff --git a/scene/training.lua b/scene/training.lua index 1abb98a..2a7044c 100644 --- a/scene/training.lua +++ b/scene/training.lua @@ -38,10 +38,12 @@ end function TrainingScene:render() self.game:draw(self.paused) + VCTRL.draw() end function TrainingScene:onInputPress(e) - if (self.game.game_over or self.game.completed) and (e.input == "menu_decide" or e.input == "menu_back" or e.input == "rotate_right") and self.game.game_over_frames > 50 then + if e.type == "touch" then VCTRL.press(e.x, e.y, e.id) + elseif (self.game.game_over or self.game.completed) and (e.input == "menu_decide" or e.input == "menu_back" or e.input == "rotate_right") and self.game.game_over_frames > 50 then SCENE = TitleScene() elseif (e.input == "menu_back") then SCENE = TitleScene() @@ -51,7 +53,8 @@ function TrainingScene:onInputPress(e) end function TrainingScene:onInputRelease(e) - if e.input and string.sub(e.input, 1, 5) ~= "menu_" then + if e.type == "touch" then VCTRL.release(e.id) + elseif e.input and string.sub(e.input, 1, 5) ~= "menu_" then self.inputs[e.input] = false end end