Update in vctrl system

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-04-14 12:14:34 +07:00
parent 9cfeb04176
commit 7e7c5301c5
4 changed files with 80 additions and 24 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

View File

@@ -15,52 +15,55 @@ local function mDrawQ(obj,quad,x,y,a,k)
love.graphics.draw(obj,quad,x,y,a,k,nil,w*.5,h*.5)
end
local empty_quad=gc_newQuad(0,0,1,1,1,1)
local empty_quad
-- A table containing quads used to draw icons for virtual control system.
local virtual_quad=setmetatable((function()
local t={}
local w=180
empty_quad=gc_newQuad(0,0,1,1,4*w,3*w)
for i,name in next,{
'left','right','up','down',
'rotate_left','rotate_right','rotate_left2','rotate_right2',
} do if #name>0 then t[name]=gc_newQuad((i-1)%4*w,math.floor((i-1)/4)*w,w,w,4*w,2*w) end end -- 4x2 is the size of entire texture
'menu_deicde','','menu_back'
} do if #name>0 then t[name]=gc_newQuad((i-1)%4*w,math.floor((i-1)/4)*w,w,w,4*w,3*w) end end -- 4x2 is the size of entire texture
return t
end)(),{
__index=function() return empty_quad end
})
local virtual_texture=love.graphics.newImage('game/VirtualControlTexture.png')
local virtual_texture=love.graphics.newImage('game/vctrlTexture.png')
local control_type={}
local button={}
button.__index=button
function button:new(data)
control_type.button={}
control_type.button.__index=control_type.button
function control_type.button:new(data)
local data=data or {}
return setmetatable({
show=data.show or true,
x=data.x or 320,
y=data.y or 180,
y=data.y or 240,
r=data.r or 80, -- size
shape=data.shape or 'circle',
key=data.key or 'X',
iconSize=data.iconSize or 80,
alpha=data.alpha or 0.75,
quad=virtual_quad[data.key]
quad=virtual_quad[data.icon]
},self)
end
function button:press()
function control_type.button:press()
self.pressed=true
self.lastPressTime=love.timer.getTime()
love.keypressed(self.key)
love.keypressed(self.key, love.keyboard.getScancodeFromKey(self.key))
end
function button:release()
function control_type.button:release()
self.pressed=false
love.keyreleased(self.key)
love.keyreleased(self.key,love.keyboard.getScancodeFromKey(self.key))
end
function button:drag(dx,dy)
function control_type.button:drag(dx,dy)
self.x,self.y=self.x+dx,self.y+dy
end
function button:draw(forceLight)
function control_type.button:draw(forceLight)
love.graphics.setLineWidth(4)
if self.shape=='circle' then
love.graphics.setColor(1,1,1,self.pressed and .5 or .05)
@@ -86,7 +89,7 @@ function button:draw(forceLight)
)
end
end
function button:getDistance(x,y)
function control_type.button:getDistance(x,y)
if self.shape=='circle' then
return math_distance(x,y,self.x,self.y)/self.r
elseif self.shape=='square' then
@@ -95,11 +98,27 @@ function button:getDistance(x,y)
end
local touches={}
VCTRL={
button:new{x=50,y=50,key='left'},
}
VCTRL={}
VCTRL.focus=nil -- Focusing buttons
---@param ... table
---@class data
---@field type string
---@field x number
---@field y number
---@field shape string
---@field key string
---@field iconSize number
---@field alpha number
---Adding (multiple) virtual button(s)
function VCTRL.new(...)
for _,d in pairs(...) do
local t=d.type
d.type=nil
table.insert(VCTRL,control_type[t]:new(d))
end
end
function VCTRL.press(x,y,id)
local obj,closestDist=false,1e99
for i=1,#VCTRL do
@@ -117,17 +136,20 @@ function VCTRL.press(x,y,id)
VCTRL.focus=obj
end
end
function VCTRL.release(id)
if touches[id] then
touches[id]:release()
end
touches[id]=nil
end
function VCTRL.drag(dx,dy,id)
if touches[id] then
touches[id]:drag(dx,dy)
end
end
function VCTRL.draw(forceLight)
for i=1,#VCTRL do
if VCTRL[i].show then

BIN
game/vctrlTexture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@@ -29,9 +29,9 @@ function love.load()
require "load.bigint"
loadSave()
require "scene"
require "game.VirtualControl" -- VCTRL
require "game.vctrl" -- VCTRL
love.mouse.setVisible(false)
-- love.mouse.setVisible(false)
love.window.setMode(love.graphics.getWidth(), love.graphics.getHeight(), {resizable = true});
GLOBAL_TRANSFORM = love.math.newTransform()
@@ -39,17 +39,26 @@ function love.load()
-- init config
initConfig()
love.window.setFullscreen(config["fullscreen"])
VCTRL.new{
{type='button',x= 100,y=320,icon= 'up',r=30,iconSize=60,alpha=1},
{type='button',x= 160,y=380,icon= 'right',r=30,iconSize=60,alpha=1},
{type='button',x= 100,y=440,icon= 'down',r=30,iconSize=60,alpha=1},
{type='button',x= 40,y=380,icon= 'left',r=30,iconSize=60,alpha=1},
{type='button',x=640-100,y=320,icon='rotate_right2',r=30,iconSize=60,alpha=1},
{type='button',x=640-160,y=380,icon= 'rotate_left2',r=30,iconSize=60,alpha=1},
{type='button',x=640-100,y=440,icon= 'rotate_right',r=30,iconSize=60,alpha=1},
{type='button',x=640- 40,y=380,icon= 'rotate_left',r=30,iconSize=60,alpha=1},
}
end
function love.resize(w, h)
local scale_factor = math.min(w / 640, h / 480)
GLOBAL_TRANSFORM:setTransformation(
(w - scale_factor * 640) / 2, -- x
(h - scale_factor * 480) / 2, -- y
0, -- orientation
(w - scale_factor * 640) / 2,
(h - scale_factor * 480) / 2,
0,
scale_factor
)
end
@@ -62,6 +71,23 @@ function love.draw()
scene:render()
VCTRL.draw()
love.graphics.setLineWidth(3)
love.graphics.rectangle('line',0,0,640,480)
-- -- Grid system
-- love.graphics.replaceTransform(GLOBAL_TRANSFORM)
-- love.graphics.setColor(1,1,1,0.5)
-- love.graphics.setLineWidth(1)
-- -- From 0 to X
-- for ix=1,math.floor(64) do
-- love.graphics.line(10*ix,0,10*ix,480)
-- end
-- -- From 0 to Y
-- for iy=1,math.floor(48) do
-- love.graphics.line(0,10*iy,640,10*iy)
-- end
love.graphics.pop()
if DEBUG_showKey then
@@ -69,6 +95,14 @@ function love.draw()
end
end
function love.mousepressed(x,y,id)
local x,y=GLOBAL_TRANSFORM:inverseTransformPoint(x,y)
VCTRL.press(x,y,id)
end
function love.mousereleased(x,y,id)
VCTRL.release(id)
end
function love.keypressed(key, scancode)
local input_pressed=nil