gamnit :: RoundControl :: hit
redef fun hit(event, ui_pos)
do
var display = app.display
if display == null then return false
var dx = ui_pos.x - center.x
var dy = ui_pos.y - center.y
if show_down then dy -= 100.0
# Angle (with 0.0 on the right) to index in WASD (0 -> W/up)
var indexes = new Set[Int]
var ao = atan2(dy, dx)
ao -= pi/4.0
# Look for 2 angles so 2 buttons can be pressed at the same time
for da in once [-pi/8.0, pi/8.0] do
var a = ao+da
while a < 0.0 do a += pi*2.0
while a > 2.0*pi do a -= pi*2.0
var index = (a * 2.0 / pi).floor.to_i
if index < 0 then index += 4
indexes.add index
end
var shows = [show_up, show_left, show_down, show_right]
var new_down_names = new Set[String]
for index in indexes do
# Don't trigger events for hidden buttons/directions
if not shows[index] then continue
var name = names[index]
# Simulate event
var e = new VirtualGamepadEvent(name)
e.is_down = event.pressed
app.accept_event e
if event.pressed then new_down_names.add name
end
# Depress released directions
for name in down_names do
if not new_down_names.has(name) then
var e = new VirtualGamepadEvent(name)
e.is_down = false
app.accept_event e
end
end
down_names = new_down_names
return true
end
lib/gamnit/virtual_gamepad/virtual_gamepad.nit:343,2--394,4