Debian Codegraph @

gui/dialogs/chat-menu.xml

raw symbols SCIP
1 <?xml version="1.0"?>
2
3 <PropertyList>
4 <name>chat-menu</name>
5 <x>10</x>
6 <y>-30</y>
7 <layout>vbox</layout>
8 <draggable>false</draggable>
9 <default-padding>1</default-padding>
10 <font>
11 <property>sim/gui/selected-style/fonts/gui-small</property>
12 </font>
13 <color>
14 <red>0</red>
15 <green>0</green>
16 <blue>0</blue>
17 <alpha>0</alpha>
18 </color>
19 <text>
20 <label>0. Back</label>
21 <halign>left</halign>
22 <color>
23 <red>0.9</red>
24 <green>0.2</green>
25 <blue>0.2</blue>
26 <alpha>1</alpha>
27 </color>
28 </text>
29 <text>
30 <label>1. Edit</label>
31 <halign>left</halign>
32 <color>
33 <red>0.9</red>
34 <green>0.2</green>
35 <blue>0.2</blue>
36 <alpha>1</alpha>
37 </color>
38 </text>
39 <text>
40 <halign>left</halign>
41 <property>/sim/multiplay/chat-menu/entry[0]</property>
42 <live>true</live>
43 <color>
44 <red>0.9</red>
45 <green>0.2</green>
46 <blue>0.2</blue>
47 <alpha>1</alpha>
48 </color>
49 </text>
50 <text>
51 <halign>left</halign>
52 <property>/sim/multiplay/chat-menu/entry[1]</property>
53 <live>true</live>
54 <color>
55 <red>0.9</red>
56 <green>0.2</green>
57 <blue>0.2</blue>
58 <alpha>1</alpha>
59 </color>
60 </text>
61 <text>
62 <halign>left</halign>
63 <property>/sim/multiplay/chat-menu/entry[2]</property>
64 <live>true</live>
65 <color>
66 <red>0.9</red>
67 <green>0.2</green>
68 <blue>0.2</blue>
69 <alpha>1</alpha>
70 </color>
71 </text>
72 <text>
73 <halign>left</halign>
74 <property>/sim/multiplay/chat-menu/entry[3]</property>
75 <live>true</live>
76 <color>
77 <red>0.9</red>
78 <green>0.2</green>
79 <blue>0.2</blue>
80 <alpha>1</alpha>
81 </color>
82 </text>
83 <text>
84 <halign>left</halign>
85 <property>/sim/multiplay/chat-menu/entry[4]</property>
86 <live>true</live>
87 <color>
88 <red>0.9</red>
89 <green>0.2</green>
90 <blue>0.2</blue>
91 <alpha>1</alpha>
92 </color>
93 </text>
94 <text>
95 <halign>left</halign>
96 <property>/sim/multiplay/chat-menu/entry[5]</property>
97 <live>true</live>
98 <color>
99 <red>0.9</red>
100 <green>0.2</green>
101 <blue>0.2</blue>
102 <alpha>1</alpha>
103 </color>
104 </text>
105 <text>
106 <halign>left</halign>
107 <property>/sim/multiplay/chat-menu/entry[6]</property>
108 <live>true</live>
109 <color>
110 <red>0.9</red>
111 <green>0.2</green>
112 <blue>0.2</blue>
113 <alpha>1</alpha>
114 </color>
115 </text>
116 <text>
117 <halign>left</halign>
118 <property>/sim/multiplay/chat-menu/entry[7]</property>
119 <live>true</live>
120 <color>
121 <red>0.9</red>
122 <green>0.2</green>
123 <blue>0.2</blue>
124 <alpha>1</alpha>
125 </color>
126 </text>
127
128 <nasal>
129 <open>
130 <![CDATA[
131 var text = "";
132 var topNode = "/sim/multiplay/chat-menu/config";
133
134 setprop("/sim/multiplay/chat-menu/entry", "1");
135 setprop("/sim/multiplay/chat-menu/entry[1]", "2");
136 setprop("/sim/multiplay/chat-menu/entry[2]", "3");
137 setprop("/sim/multiplay/chat-menu/entry[3]", "4");
138 setprop("/sim/multiplay/chat-menu/entry[4]", "5");
139 setprop("/sim/multiplay/chat-menu/entry[5]", "6");
140 setprop("/sim/multiplay/chat-menu/entry[6]", "7");
141 setprop("/sim/multiplay/chat-menu/entry[7]", "8");
142 setprop("/sim/multiplay/chat-menu/entry[8]", "9");
143 setprop("/sim/multiplay/chat-menu/entry[9]", "0");
144
145 # Get the conversation tree.
146 var entryNodes = props.globals.getNode("/sim/multiplay/chat-menu").getChildren("entry");
147 var pos = props.globals.getNode(topNode);
148
149 # Get various substitution values.
150 var callsign = getprop("/sim/multiplay/callsign");
151
152 # Simple rounding of altitude to 100 ft.
153 var altitude = int(getprop("/position/altitude-ft")/100) * 100;
154
155 var type = getprop("/sim/description");
156 if (type != "")
157 {
158 # Get the first word of the description, which will (hopefully) be
159 # something appropriate, such as "Cessna", "Boeing".
160 type = split(" ", type)[0];
161 }
162
163 # Get the nearest airport.
164 var airport = airportinfo();
165
166 # Determine the active runway. We have two ways to do this:
167 # - If the aircraft is on the ground (or very close to it), we'll try to determine
168 # the runway it is closest to, and facing the correct direction for.
169 # - If the aircraft is in the air, we'll work out the active runway based on the wind.
170
171 var active_runway = "";
172 var on_ground = (getprop("/position/altitude-agl-ft") < 100);
173
174 if (on_ground)
175 {
176 # To find out the closest runway to the aircrafts position, we'll look at the heading
177 # required to go from the aircraft's current position to the center of each runway.
178 # The closer this is to the runways real heading, the more likely this is the runway
179 # we're on. Note that we can't rely on /sim/atc/runway, as this is only set on
180 # initialization.
181 var max = 360;
182 var loc = geo.aircraft_position();
183
184 foreach (var r; keys(airport.runways)) {
185 var curr = airport.runways[r];
186 var p = geo.Coord.new();
187 p.set_latlon(curr.lat, curr.lon, airport.elevation);
188
189 var course = loc.course_to(p) - curr.heading;
190 while (course >= 180) course -= 360;
191 while (course < -180) course += 360;
192
193 var deviation = math.abs(course);
194
195 if (deviation < max)
196 {
197 active_runway = r;
198 max = deviation;
199 }
200 }
201
202 # It may be the case that we're taxiing down the runway, past the mid-point.
203 # If the aircraft is facing the "wrong" way for this runway, then take the
204 # reciprocal runway.
205 var heading = getprop("/orientation/heading-magnetic-deg");
206 var deviation = heading - airport.runways[active_runway].heading;
207
208 while (deviation >= 180) deviation -= 360;
209 while (deviation < -180) deviation += 360;
210 deviation = math.abs(deviation);
211
212 if (deviation > 90)
213 {
214 var number = math.mod(num(substr(active_runway, 0, 2)) + 18, 36);
215 var side = substr(active_runway, 2, 1);
216 active_runway = sprintf("%02d%s", number, side == "R" ? "L" : side == "L" ? "R" : side);
217 }
218 }
219 else
220 {
221 var wind_speed = getprop("/environment/wind-speed-kt");
222 var wind_from = wind_speed ? getprop("/environment/wind-from-heading-deg") : 270;
223 var max = -1;
224
225 foreach (var r; keys(airport.runways)) {
226 var curr = airport.runways[r];
227
228 var wind = wind_from - curr.heading;
229 while (wind >= 180) wind -= 360;
230 while (wind < -180) wind += 360;
231
232 var deviation = math.abs(wind) + 1e-20;
233 var v = (0.01 * curr.length + 0.01 * curr.width) / deviation;
234
235 if (v > max) {
236 max = v;
237 active_runway = r;
238 }
239 }
240 }
241
242 # Find our distance and cardinal direction to the airport.
243 var directions = split(",", "North,North East,East,South East,South,South West,West,North West");
244 var loc = geo.aircraft_position();
245 var airportloc = geo.Coord.new();
246 airportloc.set_latlon(airport.lat, airport.lon, airport.elevation);
247
248 # We want the course _from_ the airport to the aircraft - for reporting ".. approaching from the SW"
249 var airport_course = airportloc.course_to(loc);
250 var airport_distance = int((airportloc.distance_to(loc) / 1609) + 0.5);
251
252 # Get an index into our array of directions.
253 var dir = int(math.mod((airport_course + 22.5), 360) / 45);
254 var airport_direction = directions[dir];
255
256 # Now launch the keyboard listener.
257 var kbdevent = setlistener("/devices/status/keyboard/event", func (event) {
258
259 # Only check the key when pressed.
260 if (!event.getNode("pressed").getValue())
261 return;
262
263 var key = event.getNode("key");
264
265 if (handle_key(key.getValue()))
266 key.setValue(-1); # drop key event
267 });
268
269 var handle_key = func (key) {
270 # We only handle keys 0-9 and Esc
271
272 if (key == 27)
273 {
274 # escape -> cancel
275 removelistener(kbdevent);
276 gui.popdown();
277 fgcommand("dialog-close", props.Node.new({"dialog-name": "chat-menu"}));
278 return 1;
279 }
280
281 if ((key < `0`) or (key > `9`))
282 {
283 # pass the event back.
284 return 0;
285 }
286
287 if (key == `0`)
288 {
289 # Go back one level.
290 text = "";
291
292 if (pos.getName() != "config")
293 {
294 # Build up the chat string again.
295 pos = pos.getParent();
296 var p = pos;
297
298 while ((p.getName() != "config") and
299 (p.getChild("name").getValue() != nil))
300 {
301 var t = string.trim(p.getChild("name").getValue());
302
303 # Entries that begin with "[" are silent.
304 if (t[0] != `[`)
305 {
306 text = t ~ " " ~ text;
307 }
308
309 p = p.getParent();
310 }
311 }
312
313 updateDialog();
314 }
315
316 if (key == `1`)
317 {
318 # Go to edit mode using the inline editor.
319 removelistener(kbdevent);
320 gui.popdown();
321 multiplayer.compose_message(text);
322 fgcommand("dialog-close", props.Node.new({"dialog-name": "chat-menu"}));
323 }
324
325 if ((key > `1`) and (key <= `9`))
326 {
327 # Select the appropriate new node and update.
328 # The index starts from position 2.
329 var i = key - `2`;
330
331 if (i > (size(pos.getChildren("menu")) -1))
332 {
333 # Drop out if the user has entered a too large value.
334 return 0;
335 }
336
337 var t = entryNodes[i].getValue();
338
339 t = string.trim(substr(t, 3, size(t) -3));
340
341 # Entries that begin with "[" are silent.
342 if (t[0] != `[`)
343 {
344 text = text ~ " " ~ t;
345 }
346
347 pos = pos.getChildren("menu")[i];
348
349 if (size(pos.getChildren("menu")) == 0)
350 {
351 # We've come to the end of the tree - send the message and close
352 setprop("/sim/multiplay/chat", text);
353 removelistener(kbdevent);
354 gui.popdown();
355 fgcommand("dialog-close", props.Node.new({"dialog-name": "chat-menu"}));
356 }
357 else
358 {
359 # We've got more tree to traverse.
360 updateDialog();
361 }
362 }
363
364 # If we got here, we consumed the event
365 return 1;
366 }
367
368 # Substitute simple values into the string.
369 # The values we handle are
370 #
371 # % - Type (first word of /sim/description)
372 # # - callsign
373 # $ - altitude
374 # * - airport ID.
375 # & - course from airport to aircraft - "North West"
376 # ! - distance from aircraft to airport in miles.
377 #
378 var subvals = func (str) {
379 var t = "";
380
381 for (var p = 0; p < size(str); p += 1)
382 {
383 if (str[p] == `!`) t ~= airport_direction;
384 elsif (str[p] == `^`) t ~= airport_distance;
385 elsif (str[p] == `(`) t ~= active_runway;
386 elsif (str[p] == `*`) t ~= airport.name;
387 elsif (str[p] == `%`) t ~= type;
388 elsif (str[p] == `#`) t ~= callsign;
389 elsif (str[p] == `$`) t ~= altitude;
390 else t ~= chr(str[p]);
391 }
392
393 return t;
394 }
395
396 var updateDialog = func {
397
398 var children = pos.getChildren("menu");
399 var i = 0;
400 foreach(var c; children)
401 {
402 var p = i + 2;
403 var txt = p ~ ': ' ~ subvals(c.getChild("name").getValue());
404
405 if (i < 9)
406 {
407 entryNodes[i].setValue(txt);
408 }
409 i = i + 1;
410 }
411
412 # Set the rest of the dialog to blank.
413 while (i <= 9)
414 {
415 entryNodes[i].setValue("");
416 i = i + 1;
417 }
418
419 # Write the popup.
420 gui.popupTip(text, 1000000);
421 }
422
423 # Start by updating the dialog.
424 updateDialog();
425 ]]>
426 </open>
427 </nasal>
428 </PropertyList>