This repository has been archived by the owner on Dec 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleAlt.lua
262 lines (162 loc) · 5.2 KB
/
SimpleAlt.lua
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
-- A simple altimeter setter by a few clicks, in 100 and 1000 ft steps
-- (c) Kolja Zuelsdorf
require "graphics"
dataref("SELECTED_ALT", "sim/cockpit2/autopilot/altitude_dial_ft", "writable")
dataref("VIEW_TYPE", "sim/graphics/view/view_type")
local KazUiUtil = {}
KazUiUtil.__index = KazUiUtil
setmetatable(KazUiUtil, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function KazUiUtil.new()
local self = setmetatable({}, KazUiUtil)
return self
end
-- draws an "empty" rectancle (just the lines)
function KazUiUtil.drawRectangle(x1, y1, x2, y2)
-- top
graphics.draw_line(x1, y2, x2, y2)
-- right
graphics.draw_line(x2, y2, x2, y1)
-- bottom
graphics.draw_line(x1, y1, x2, y1)
-- left
graphics.draw_line(x1, y1, x1, y2)
end
local KazWindow = {}
KazWindow.__index = KazWindow
setmetatable(KazWindow, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function KazWindow.new(x, y, width, height)
local self = setmetatable({}, KazWindow)
self.x = x
self.y = y
self.width = width
self.height = height
self.title = '';
return self
end
function KazWindow:setTitle(title)
self.title = title;
end
function KazWindow:draw()
-- init the graphics system
XPLMSetGraphicsState(0,0,0,1,1,0,0)
-- draw transparent backgroud
graphics.set_color(0, 0, 0, 0.5)
graphics.draw_rectangle(self.x, self.y, self.x + self.width, self.y + self.height)
graphics.set_color(1, 1, 1, 1)
if self.title ~= '' then
draw_string_Helvetica_10(self.x + 2, self.y + self.height - 12, self.title)
end
end
local KazButton = {}
KazButton.__index = KazButton
setmetatable(KazButton, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function KazButton.new(x, y, width, height)
local self = setmetatable({}, KazButton)
self.x = x
self.y = y
self.width = width
self.height = height
self.label = '';
self.onClickEvent = nil;
return self
end
function KazButton:setLabel(label)
self.label = label;
end
function KazButton:draw()
-- init the graphics system
XPLMSetGraphicsState(0,0,0,1,1,0,0)
-- draw transparent backgroud
graphics.set_color(0, 0, 0, 0.5)
graphics.draw_rectangle(self.x, self.y, self.x + self.width, self.y + self.height)
if self:isHovered() then
graphics.set_color(0.12, 0.82, 0.08, 1)
else
graphics.set_color(1, 1, 1, 1)
end
KazUiUtil.drawRectangle(self.x, self.y, self.x + self.width, self.y + self.height)
if self.label ~= '' then
draw_string_Helvetica_10(self.x + 5, self.y + self.height - 15, self.label)
end
end
function KazButton:isHovered()
return ((MOUSE_X >= self.x and MOUSE_X <= self.x + self.width)
and
(MOUSE_Y >= self.y and MOUSE_Y <= self.y + self.height));
end
function KazButton:onClick(callback)
self.onClickEvent = callback
end
function KazButton:handleClick()
if self:isHovered() and self.onClickEvent ~= nil then
self.onClickEvent(self)
RESUME_MOUSE_CLICK = true
end
end
function getNextHighestThousand()
return math.floor(SELECTED_ALT / 1000 + 1) * 1000
end
function getNextLowestThousand()
return math.floor((SELECTED_ALT - 1) / 1000) * 1000
end
function getNextHighestHundred()
return math.floor(SELECTED_ALT / 100 + 1) * 100
end
function getNextLowestHundred()
return math.floor((SELECTED_ALT - 1) / 100) * 100
end
function setAutopilotAltitude(altitude)
SELECTED_ALT = altitude
end
local simpleAltWindow = KazWindow.new(SCREEN_WIDTH - 115, 20, 95, 75)
simpleAltWindow:setTitle('SimpleAlt');
local increaseByHunderedButton = KazButton.new(simpleAltWindow.x + 5, simpleAltWindow.y + 35, 41, 21)
local decreaseByHunderedButton = KazButton.new(simpleAltWindow.x + 5, simpleAltWindow.y + 5, 41, 21)
local increaseByThousandButton = KazButton.new(simpleAltWindow.x + 50, simpleAltWindow.y + 35, 41, 21)
local decreaseByThousandButton = KazButton.new(simpleAltWindow.x + 50, simpleAltWindow.y + 5, 41, 21)
function renderControlButton(button, altitude)
button:setLabel(altitude);
button:onClick(function (callerButton)
setAutopilotAltitude(altitude)
end)
button:draw()
end
function simple_alt_draw_alt_selector()
-- only draw selector in cockpit
if VIEW_TYPE ~= 1026 then
return
end
simpleAltWindow:draw()
renderControlButton(increaseByHunderedButton, getNextHighestHundred());
renderControlButton(decreaseByHunderedButton, getNextLowestHundred());
renderControlButton(increaseByThousandButton, getNextHighestThousand());
renderControlButton(decreaseByThousandButton, getNextLowestThousand());
end
do_every_draw("simple_alt_draw_alt_selector()")
function simple_alt_mouse_click_events()
-- only handle selector events in cockpit
if VIEW_TYPE ~= 1026 then
return
end
-- we will only react once
if MOUSE_STATUS ~= "down" then
return
end
increaseByHunderedButton:handleClick()
decreaseByHunderedButton:handleClick()
increaseByThousandButton:handleClick()
decreaseByThousandButton:handleClick()
end
do_on_mouse_click("simple_alt_mouse_click_events()")