Hover offset issue
I created windows to upgrade and modify my generators in one of my projects, but I seem to have a mouse offset issue. I linked generateurFenetre.y to the button class's Y-coordinate so they would be directly connected; to my surprise, it works, but the button's visual rendering is offset from its physical position. If anyone can help, here is the code and the hover function I'm using—thanks!
generateurFenetre = object
x = 0
y = -300
vitesse = 12
targetY = 0
active = false
alpha = 0
end
checkRectMouseHover = function(x, y, width, height)
return mouse.x >= (x - width/2) and mouse.x <= (x + width/2) and mouse.y >= (y - height/2) and mouse.y <= (y + height/2)
end
BoutonGenerateur = class
constructor = function(x,y,w,h,id)
this.x = x
this.y = y
this.w = w
this.h = h
this.id = id
this.oldW = this.w
this.oldH = this.h
this.active = false
this.survol = false
this.color = "rgb(105,66,46)"
this.update = function()
for ge in generateurs
if ge.interaction == true then
if generateurFenetre.y > -300 then
this.active = true
local posXReelle = generateurFenetre.x + this.x
local posYReelle = generateurFenetre.y + this.y
this.survol = checkRectMouseHover(posXReelle, posYReelle, this.w, this.h)
if this.survol then
if this.w < this.oldW + 5 then
this.w = this.w + 0.5
end
if this.h < this.oldH + 5 then
this.h = this.h + 0.5
end
if this.id != 1 and this.id != 2 then
this.color = "rgb(255,161,113)"
else
this.color = "rgb(255,113,113)"
end
if mouse.press or mouse.left then
if generateurFenetre.active then
if this.id == 1 then
generateurFenetre.targetY = -300
generateurFenetre.active = false
end
end
end
else
if this.w > this.oldW then
this.w = this.w - 1
end
if this.h > this.oldH then
this.h = this.h - 1
end
if this.id != 1 and this.id != 2 then
this.color = "rgb(105,66,46)"
else
this.color = "rgb(147,65,65)"
end
end
else
this.active = false
end
end
end
end
this.draw = function()
if this.active then
local posXReelle = generateurFenetre.x + this.x
local posYReelle = generateurFenetre.y + this.y
screen.fillRect(posXReelle , posYReelle , this.w, this.h, this.color)
screen.drawRect(posXReelle , posYReelle, this.w, this.h, "white")
end
end
end
end
BGenerateur = [ new BoutonGenerateur(110, 60, 20, 20, 1) ]
updateActionGenerateur = function()
if mode == "game" and lieu == "nature" then
for ge in generateurs
for bge in BGenerateur
bge.update()
end
if collision(player.hitboxI, ge.hitboxI) then
if ge.hitboxI.active and player.interaction == true and keyboard.press.E then
ge.interaction = true
player.etat = "interaction"
player.interaction = false
generateurFenetre.targetY = 0
generateurFenetre.active = false
generateurFenetre.alpha = 0.5
end
end
if ge.interaction then
if generateurFenetre.active and keyboard.press.E then
generateurFenetre.targetY = -300
generateurFenetre.active = false
generateurFenetre.alpha = 0.5
end
if generateurFenetre.y < generateurFenetre.targetY then
generateurFenetre.y = generateurFenetre.y + generateurFenetre.vitesse
if generateurFenetre.y >= generateurFenetre.targetY then
generateurFenetre.y = generateurFenetre.targetY
generateurFenetre.active = true
end
elseif generateurFenetre.y > generateurFenetre.targetY then
generateurFenetre.y = generateurFenetre.y - generateurFenetre.vitesse
if generateurFenetre.y <= generateurFenetre.targetY then
generateurFenetre.y = generateurFenetre.targetY
if generateurFenetre.targetY == -300 then
ge.interaction = false
player.etat = "static"
player.interaction = true
generateurFenetre.alpha = 0
end
end
end
end
end
end
end
drawActionGenerateur = function()
if mode == "game" and lieu == "nature" then
if generateurFenetre.y > -300 then
screen.setAlpha(generateurFenetre.alpha)
screen.fillRect(0,0,screen.width*2,screen.height*2,"black")
screen.setAlpha(1)
screen.fillRect(generateurFenetre.x, generateurFenetre.y, 270, 150,"rgb(197,124,87)")
screen.fillRect(generateurFenetre.x, generateurFenetre.y - 72, 270, 5,"rgb(105,66,46)")
screen.drawRect(generateurFenetre.x, generateurFenetre.y, 270, 150, "white")
for bge in BGenerateur
bge.draw()
end
if generateurFenetre.active then
screen.drawText("Parts generator",generateurFenetre.x,generateurFenetre.y+58,10,"rgb(0,0,9)")
screen.drawText("Parts generator",generateurFenetre.x,generateurFenetre.y+60,10,"white")
end
end
end
end
