-
Notifications
You must be signed in to change notification settings - Fork 1
/
control_canvas.rb
86 lines (79 loc) · 2.65 KB
/
control_canvas.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class ControlCanvas < FXCanvas
def initialize(parent, window)
@window = window
super(parent, width: 100, height: 100, opts: LAYOUT_FILL)
# canvas.fill(FXRGB(255, 255, 255))
self.connect(SEL_MOUSEWHEEL, method(:scroll_handler))
self.connect(SEL_KEYPRESS, method(:keypress_handler))
self.connect(SEL_KEYRELEASE, method(:keyrelease_handler))
end
#######
private
#######
def scroll_handler(*, data)
if data.code == 120 # scroll up
@window.flight_controls[:throttle].value += 1
@window.writeout('t', @window.flight_controls[:throttle].value)
elsif data.code == -120 # scroll down
@window.flight_controls[:throttle].value -= 1
@window.writeout('t', @window.flight_controls[:throttle].value)
end
end
def keypress_handler(*, data)
case data.code
when 32 # space
update_key = :throttle
value = 0
when 87, 119 # w
update_key = :pitch
value = -10
when 83, 115 # s
update_key = :pitch
value = 10
when 65, 97 # a
update_key = :roll
value = -10
when 68, 100 # d
update_key = :roll
value = 10
when 65362, 65363 # up, or right
update_key = :throttle
value = @window.flight_controls[update_key].value + 1
when 65364, 65361 # down, or left
update_key = :throttle
value = @window.flight_controls[update_key].value - 1
end
unless update_key.nil?
if update_key == :throttle ||
(@window.flight_controls[update_key].value != value)
@window.flight_controls[update_key].value = value
@window.writeout(update_key[0], value)
end
end
end
def keyrelease_handler(*, data)
# This handler is called when the key is released or called
# repeatedly when the key is held down. We only want to reset
# the value to 0 when the key is actually let
# go. FXApp#getKeyState() returns true if the key is currently
# depressed.
if getApp().getKeyState(data.code)
return
end
# if key has been released, reset corresponding value to 0
case data.code
when 87, 119 # w
update_key = :pitch
when 83, 115 # s
update_key = :pitch
when 65, 97 # a
update_key = :roll
when 68, 100 # d
update_key = :roll
end
unless update_key.nil?
@window.flight_controls[update_key].value = 0
@window.writeout(update_key[0], 0)
end
end
end