Omnimaga
Calculator Community => TI Calculators => Lua => Topic started by: ElementCoder on September 24, 2012, 01:50:13 pm
-
While working on a project I found myself in need of detecting whether the mouse was clicked inside a certain area or not. I thought it would be pretty simple since I only needed square 'hitboxes'. I wrote some simple code and figured to share it in case it might be useful to others.
Hitbox code:
Code for the hitbox:
HitBoxSquare = class()
function HitBoxSquare:init(xIn, yIn, width, height)
self.x = xIn
self.y = yIn
self.width = width
self.height = height
end
function HitBoxSquare:contains(xMouse, yMouse)
local sw = self.width
local sh = self.height
return (xMouse>=(self.x)) and xMouse<=(self.x+sw) and yMouse>=(self.y) and yMouse<=(self.y+sh)
end
Mandatory code:
To track mousepresses:
function on.mouseDown(x, y)
mouseX = x
mouseY = y
--Other code here
end
To track mousemovement:
function on.mouseMove(x, y)
mouseX = x
mouseY = y
--Other code here
end
How to use:
Create the hitbox:
hitBox = HitBoxSquare(x, y, width, height)
Check if the mouse is clicked inside the hitbox:
if hitBox:contains(mouseX, mouseY) then
--Do something here
end
Using multiple hitboxes:
If you want to use more then one hitbox, checking them by hand is a little too much work. Here's where tables come in handy!
-----------------------------------------------
-- Checking multiple hitboxes, the fast way. --
-- ElementCoder, 2012 --
-----------------------------------------------
HitBoxSquare = class()
function HitBoxSquare:init(xIn, yIn, width, height)
self.x = xIn
self.y = yIn
self.width = width
self.height = height
end
function HitBoxSquare:contains(xMouse, yMouse)
local sw = self.width
local sh = self.height
return (xMouse>=(self.x)) and xMouse<=(self.x+sw) and yMouse>=(self.y) and yMouse<=(self.y+sh)
end
hb1 = HitBoxSquare(10, 10, 10, 10)
hb2 = HitBoxSquare(10, 25, 10, 10)
hb3 = HitBoxSquare(10, 40, 10, 10)
hitbox = {hb1, hb2, hb3}
hitboxname = {"Hitbox 1", "Hitbox 2", "Hitbox 3"}
function on.paint(gc)
gc:drawRect(10, 10, 10, 10)
gc:drawRect(10, 25, 10, 10)
gc:drawRect(10, 40, 10, 10)
end
function on.mouseMove(x, y)
mouseX, mouseY = x, y
for i = 1, 3 do
if hitbox[i]:contains(mouseX, mouseY) then
print(hitboxname[i].." contains the mouse!")
end
end
end
-
Nice! I used a system similar to this with my Checkers game for mouse controls.
-
Oh wow this is great! I could see this coming into use for some ideas that I have that involve a mouse pointer.
So theoretically you could have several hitboxes that you could move around quickly and make the mouse click, all doing different actions right?
-
Yep, you would define every hitbox by doing so:
hitBox1 = HitBoxSquare(x, y, widht, height)
hitBox2 = HitBoxSquare(x, y, widht, height)
hitBox3 = HitBoxSquare(x, y, widht, height)
And then you would check for mouse presence by doing this:
hitBox1:contains(mouseX, mouseY)
hitBox2:contains(mouseX, mouseY)
hitBox3:contains(mouseX, mouseY)
But this would not work very well if X hitboxes would be to overlap each other (unless you'd want all X actions to be performed ofc).
P.S. Nice to see your still working on projects :) I'm glad I have some time again for programming and such. I can finally start working on projects that have been on hold for a long time :D
-
I have only ever used lists with single values, and not classes, but would you be able to use lists to define the hitboxes
hitBox={}
hitBox[1] = HitBoxSquare(x, y, widht, height)
hitBox[2] = HitBoxSquare(x, y, widht, height)
hitBox[3] = HitBoxSquare(x, y, widht, height)
That way you could efficiently do something to all of them
for num=1,10 do
something
end
-
I think it is possible. I'll try it tomorrow and if it works I'll post a snippet in the first post to show how to :)
Of course you and others can help too!
[edit]read: jim we need you D:
-
Yes, storing the objects in a table and iterating over them is the way to do it.
I'd love to post some more code, but I'm ill at the moment and can't think straight.
-
I made a little example and added it to the main post :)
-
ElementCoder, I'd suggest you to look through https://github.com/adriweb/EEPro-for-Nspire/blob/master/Global%20Libraries/screen.lua.
It's part of ETK 2, the GUI toolkit I made for EEPro. I don't want to brag about it, but I think there are some interesting things in it, stuff that might give you some ideas. The widgets are defined in widgets.lua, in the same directory as this script.