Debian Codegraph @

engine/ui/script_ui.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name script_ui.cpp - The ui ccl functions. */
11 //
12 // (c) Copyright 1999-2007 by Lutz Sammer, Jimmy Salmon, Martin Renold
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "stratagus.h"
40 #include "script.h"
41 #include "interface.h"
42 #include "ui.h"
43 #include "video.h"
44 #include "map.h"
45 #include "menus.h"
46 #include "font.h"
47 #include "unit.h"
48 #include "unit_manager.h"
49 #include "unittype.h"
50 #include "title.h"
51
52 /*----------------------------------------------------------------------------
53 -- Variables
54 ----------------------------------------------------------------------------*/
55
56 std::string ClickMissile; /// FIXME:docu
57 std::string DamageMissile; /// FIXME:docu
58
59 std::map<std::string, ButtonStyle *> ButtonStyleHash;
60
61 static int HandleCount = 1; /// Lua handler count
62
63 CPreference Preference;
64
65 /*----------------------------------------------------------------------------
66 -- Functions
67 ----------------------------------------------------------------------------*/
68
69 /**
70 ** Set speed of middle-mouse scroll
71 **
72 ** @param l Lua state.
73 */
74 static int CclSetMouseScrollSpeedDefault(lua_State *l)
75 {
76 LuaCheckArgs(l, 1);
77 UI.MouseScrollSpeedDefault = LuaToNumber(l, 1);
78 return 0;
79 }
80
81 /**
82 ** Set speed of ctrl-middle-mouse scroll
83 **
84 ** @param l Lua state.
85 */
86 static int CclSetMouseScrollSpeedControl(lua_State *l)
87 {
88 LuaCheckArgs(l, 1);
89 UI.MouseScrollSpeedControl = LuaToNumber(l, 1);
90 return 0;
91 }
92
93 /**
94 ** Set which missile is used for right click
95 **
96 ** @param l Lua state.
97 */
98 static int CclSetClickMissile(lua_State *l)
99 {
100 int args;
101
102 args = lua_gettop(l);
103 if (args > 1 || (args == 1 && (!lua_isnil(l, 1) && !lua_isstring(l, 1)))) {
104 LuaError(l, "incorrect argument");
105 }
106 ClickMissile.clear();
107 if (args == 1 && !lua_isnil(l, 1)) {
108 ClickMissile = lua_tostring(l, 1);
109 }
110
111 return 0;
112 }
113
114 /**
115 ** Set which missile shows Damage
116 **
117 ** @param l Lua state.
118 */
119 static int CclSetDamageMissile(lua_State *l)
120 {
121 int args;
122
123 args = lua_gettop(l);
124 if (args > 1 || (args == 1 && (!lua_isnil(l, 1) && !lua_isstring(l, 1)))) {
125 LuaError(l, "incorrect argument");
126 }
127 DamageMissile.clear();
128 if (args == 1 && !lua_isnil(l, 1)) {
129 DamageMissile = lua_tostring(l, 1);
130 }
131
132 return 0;
133 }
134
135 static int CclSetMaxOpenGLTexture(lua_State *l)
136 {
137 LuaCheckArgs(l, 1);
138 if (CclInConfigFile) {
139 GLMaxTextureSizeOverride = LuaToNumber(l, 1);
140 }
141
142 return 0;
143 }
144
145 /**
146 ** Set the video resolution.
147 **
148 ** @param l Lua state.
149 */
150 static int CclSetVideoResolution(lua_State *l)
151 {
152 LuaCheckArgs(l, 2);
153 if (CclInConfigFile) {
154 // May have been set from the command line
155 if (!Video.Width || !Video.Height) {
156 Video.Width = LuaToNumber(l, 1);
157 Video.Height = LuaToNumber(l, 2);
158 }
159 }
160 return 0;
161 }
162
163 /**
164 ** Get the video resolution.
165 **
166 ** @param l Lua state.
167 */
168 static int CclGetVideoResolution(lua_State *l)
169 {
170 LuaCheckArgs(l, 0);
171 lua_pushnumber(l, Video.Width);
172 lua_pushnumber(l, Video.Height);
173 return 2;
174 }
175
176 /**
177 ** Set the video fullscreen mode.
178 **
179 ** @param l Lua state.
180 */
181 static int CclSetVideoFullScreen(lua_State *l)
182 {
183 LuaCheckArgs(l, 1);
184 if (CclInConfigFile) {
185 // May have been set from the command line
186 if (!VideoForceFullScreen) {
187 Video.FullScreen = LuaToBoolean(l, 1);
188 }
189 }
190 return 0;
191 }
192
193 /**
194 ** Get the video fullscreen mode.
195 **
196 ** @param l Lua state.
197 */
198 static int CclGetVideoFullScreen(lua_State *l)
199 {
200 LuaCheckArgs(l, 0);
201 lua_pushboolean(l, Video.FullScreen);
202 return 1;
203 }
204
205 /**
206 ** Default title screens.
207 **
208 ** @param l Lua state.
209 */
210 static int CclSetTitleScreens(lua_State *l)
211 {
212 const char *value;
213 int i;
214 int args;
215 int j;
216 int subargs;
217 int k;
218
219 if (TitleScreens) {
220 for (i = 0; TitleScreens[i]; ++i) {
221 delete TitleScreens[i];
222 }
223 delete[] TitleScreens;
224 TitleScreens = NULL;
225 }
226
227 args = lua_gettop(l);
228 TitleScreens = new TitleScreen *[args + 1];
229 memset(TitleScreens, 0, (args + 1) * sizeof(TitleScreen *));
230
231 for (j = 0; j < args; ++j) {
232 LuaCheckTable(l, j + 1);
233 TitleScreens[j] = new TitleScreen;
234 lua_pushnil(l);
235 while (lua_next(l, j + 1)) {
236 value = LuaToString(l, -2);
237 if (!strcmp(value, "Image")) {
238 TitleScreens[j]->File = LuaToString(l, -1);
239 } else if (!strcmp(value, "Music")) {
240 TitleScreens[j]->Music = LuaToString(l, -1);
241 } else if (!strcmp(value, "StretchImage")) {
242 TitleScreens[j]->StretchImage = LuaToBoolean(l, -1);
243 } else if (!strcmp(value, "Timeout")) {
244 TitleScreens[j]->Timeout = LuaToNumber(l, -1);
245 } else if (!strcmp(value, "Labels")) {
246 LuaCheckTable(l, -1);
247 subargs = lua_objlen(l, -1);
248 TitleScreens[j]->Labels = new TitleScreenLabel *[subargs + 1];
249 memset(TitleScreens[j]->Labels, 0, (subargs + 1) * sizeof(TitleScreenLabel *));
250 for (k = 0; k < subargs; ++k) {
251 lua_rawgeti(l, -1, k + 1);
252 LuaCheckTable(l, -1);
253 TitleScreens[j]->Labels[k] = new TitleScreenLabel;
254 lua_pushnil(l);
255 while (lua_next(l, -2)) {
256 value = LuaToString(l, -2);
257 if (!strcmp(value, "Text")) {
258 TitleScreens[j]->Labels[k]->Text = LuaToString(l, -1);
259 } else if (!strcmp(value, "Font")) {
260 TitleScreens[j]->Labels[k]->Font = CFont::Get(LuaToString(l, -1));
261 } else if (!strcmp(value, "Pos")) {
262 LuaCheckTableSize(l, -1, 2);
263 TitleScreens[j]->Labels[k]->Xofs = LuaToNumber(l, -1, 1);
264 TitleScreens[j]->Labels[k]->Yofs = LuaToNumber(l, -1, 2);
265 } else if (!strcmp(value, "Flags")) {
266 int subsubargs;
267 int subk;
268
269 LuaCheckTable(l, -1);
270 subsubargs = lua_objlen(l, -1);
271 for (subk = 0; subk < subsubargs; ++subk) {
272 value = LuaToString(l, -1, subk + 1);
273 if (!strcmp(value, "center")) {
274 TitleScreens[j]->Labels[k]->Flags |= TitleFlagCenter;
275 } else {
276 LuaError(l, "incorrect flag");
277 }
278 }
279 } else {
280 LuaError(l, "Unsupported key: %s" _C_ value);
281 }
282 lua_pop(l, 1);
283 }
284 lua_pop(l, 1);
285 }
286 } else {
287 LuaError(l, "Unsupported key: %s" _C_ value);
288 }
289 lua_pop(l, 1);
290 }
291 }
292
293 return 0;
294 }
295
296 /**
297 ** Define a cursor.
298 **
299 ** @param l Lua state.
300 */
301 static int CclDefineCursor(lua_State *l)
302 {
303 const char *value;
304 std::string name;
305 std::string file;
306 int hotx;
307 int hoty;
308 int w;
309 int h;
310 int rate;
311 int i;
312 CCursor *ct;
313
314 LuaCheckArgs(l, 1);
315 LuaCheckTable(l, 1);
316 hotx = hoty = w = h = rate = 0;
317 lua_pushnil(l);
318 while (lua_next(l, 1)) {
319 value = LuaToString(l, -2);
320 if (!strcmp(value, "Name")) {
321 name = LuaToString(l, -1);
322 } else if (!strcmp(value, "File")) {
323 file = LuaToString(l, -1);
324 } else if (!strcmp(value, "HotSpot")) {
325 LuaCheckTableSize(l, -1, 2);
326 hotx = LuaToNumber(l, -1, 1);
327 hoty = LuaToNumber(l, -1, 2);
328 } else if (!strcmp(value, "Size")) {
329 LuaCheckTableSize(l, -1, 2);
330 w = LuaToNumber(l, -1, 1);
331 h = LuaToNumber(l, -1, 2);
332 } else if (!strcmp(value, "Rate")) {
333 rate = LuaToNumber(l, -1);
334 } else {
335 LuaError(l, "Unsupported tag: %s" _C_ value);
336 }
337 lua_pop(l, 1);
338 }
339
340 Assert(!name.empty() && !file.empty() && w && h);
341
342 //
343 // Look if this kind of cursor already exists.
344 //
345 ct = NULL;
346 i = 0;
347 if (AllCursors.size()) {
348 for (; i < (int)AllCursors.size(); ++i) {
349 if (AllCursors[i].Ident == name) {
350 ct = &AllCursors[i];
351 break;
352 }
353 }
354 }
355 //
356 // Not found, make a new slot.
357 //
358 if (!ct) {
359 CCursor c;
360 AllCursors.push_back(c);
361 ct = &AllCursors.back();
362 ct->Ident = name;
363 }
364
365 ct->G = CGraphic::New(file, w, h);
366 ct->HotX = hotx;
367 ct->HotY = hoty;
368 ct->FrameRate = rate;
369
370 return 0;
371 }
372
373 /**
374 ** Set the current game cursor.
375 **
376 ** @param l Lua state.
377 */
378 static int CclSetGameCursor(lua_State *l)
379 {
380 LuaCheckArgs(l, 1);
381 GameCursor = CursorByIdent(LuaToString(l, 1));
382 return 0;
383 }
384
385 /**
386 ** Define the viewports.
387 **
388 ** @param l Lua state.
389 */
390 static int CclDefineViewports(lua_State *l)
391 {
392 const char *value;
393 int i;
394 int args;
395 int slot;
396
397 i = 0;
398 args = lua_gettop(l);
399 for (int j = 0; j < args; ++j) {
400 value = LuaToString(l, j + 1);
401 ++j;
402 if (!strcmp(value, "mode")) {
403 UI.ViewportMode = (ViewportModeType)(int)LuaToNumber(l, j + 1);
404 } else if (!strcmp(value, "viewport")) {
405 LuaCheckTableSize(l, j + 1, 3);
406 UI.Viewports[i].MapX = LuaToNumber(l, j + 1, 1);
407 UI.Viewports[i].MapY = LuaToNumber(l, j + 1, 2);
408 slot = (int)LuaToNumber(l, j + 1, 3);
409 if (slot != -1) {
410 UI.Viewports[i].Unit = UnitSlots[slot];
411 }
412 ++i;
413 } else {
414 LuaError(l, "Unsupported tag: %s" _C_ value);
415 }
416 }
417 UI.NumViewports = i;
418
419 return 0;
420 }
421
422 /**
423 ** Fighter right button attacks as default.
424 **
425 ** @param l Lua state.
426 */
427 static int CclRightButtonAttacks(lua_State *l)
428 {
429 LuaCheckArgs(l, 0);
430 RightButtonAttacks = true;
431 return 0;
432 }
433
434 /**
435 ** Fighter right button moves as default.
436 **
437 ** @param l Lua state.
438 */
439 static int CclRightButtonMoves(lua_State *l)
440 {
441 LuaCheckArgs(l, 0);
442 RightButtonAttacks = false;
443 return 0;
444 }
445
446 /**
447 ** Find a button style
448 **
449 ** @param style Name of the style to find.
450 **
451 ** @return Button style, NULL if not found.
452 */
453 ButtonStyle *FindButtonStyle(const std::string &style)
454 {
455 return ButtonStyleHash[style];
456 }
457
458 /**
459 ** Parse button style properties
460 **
461 ** @param l Lua state.
462 ** @param p Properties to fill in.
463 */
464 static void ParseButtonStyleProperties(lua_State *l, ButtonStyleProperties *p)
465 {
466 const char *value;
467 std::string file;
468 int w;
469 int h;
470
471 LuaCheckTable(l, -1);
472
473 w = h = 0;
474
475 lua_pushnil(l);
476 while (lua_next(l, -2)) {
477 value = LuaToString(l, -2);
478 if (!strcmp(value, "File")) {
479 file = LuaToString(l, -1);
480 } else if (!strcmp(value, "Size")) {
481 LuaCheckTableSize(l, -1, 2);
482 w = LuaToNumber(l, -1, 1);
483 h = LuaToNumber(l, -1, 2);
484 } else if (!strcmp(value, "Frame")) {
485 p->Frame = LuaToNumber(l, -1);
486 } else if (!strcmp(value, "Border")) {
487 LuaCheckTable(l, -1);
488 lua_pushnil(l);
489 while (lua_next(l, -2)) {
490 value = LuaToString(l, -2);
491 if (!strcmp(value, "Color")) {
492 LuaCheckTableSize(l, -1, 3);
493 p->BorderColorRGB.r = LuaToNumber(l, -1, 1);
494 p->BorderColorRGB.g = LuaToNumber(l, -1, 2);
495 p->BorderColorRGB.b = LuaToNumber(l, -1, 3);
496 } else if (!strcmp(value, "Size")) {
497 p->BorderSize = LuaToNumber(l, -1);
498 } else {
499 LuaError(l, "Unsupported tag: %s" _C_ value);
500 }
501 lua_pop(l, 1);
502 }
503 } else if (!strcmp(value, "TextPos")) {
504 LuaCheckTableSize(l, -1, 2);
505 p->TextX = LuaToNumber(l, -1, 1);
506 p->TextY = LuaToNumber(l, -1, 2);
507 } else if (!strcmp(value, "TextAlign")) {
508 value = LuaToString(l, -1);
509 if (!strcmp(value, "Center")) {
510 p->TextAlign = TextAlignCenter;
511 } else if (!strcmp(value, "Right")) {
512 p->TextAlign = TextAlignRight;
513 } else if (!strcmp(value, "Left")) {
514 p->TextAlign = TextAlignLeft;
515 } else {
516 LuaError(l, "Invalid text alignment: %s" _C_ value);
517 }
518 } else if (!strcmp(value, "TextNormalColor")) {
519 p->TextNormalColor = LuaToString(l, -1);
520 } else if (!strcmp(value, "TextReverseColor")) {
521 p->TextReverseColor = LuaToString(l, -1);
522 } else {
523 LuaError(l, "Unsupported tag: %s" _C_ value);
524 }
525 lua_pop(l, 1);
526 }
527
528 if (!file.empty()) {
529 p->Sprite = CGraphic::New(file, w, h);
530 }
531 }
532
533 /**
534 ** Define a button style
535 **
536 ** @param l Lua state.
537 */
538 static int CclDefineButtonStyle(lua_State *l)
539 {
540 const char *style;
541 const char *value;
542 ButtonStyle *b;
543
544 LuaCheckArgs(l, 2);
545 LuaCheckTable(l, 2);
546
547 style = LuaToString(l, 1);
548 b = ButtonStyleHash[style];
549 if (!b) {
550 b = ButtonStyleHash[style] = new ButtonStyle;
551 // Set to bogus value to see if it was set later
552 b->Default.TextX = b->Hover.TextX = b->Clicked.TextX = 0xFFFFFF;
553 }
554
555 lua_pushnil(l);
556 while (lua_next(l, 2)) {
557 value = LuaToString(l, -2);
558 if (!strcmp(value, "Size")) {
559 LuaCheckTableSize(l, -1, 2);
560 b->Width = LuaToNumber(l, -1, 1);
561 b->Height = LuaToNumber(l, -1, 2);
562 } else if (!strcmp(value, "Font")) {
563 b->Font = CFont::Get(LuaToString(l, -1));
564 } else if (!strcmp(value, "TextNormalColor")) {
565 b->TextNormalColor = LuaToString(l, -1);
566 } else if (!strcmp(value, "TextReverseColor")) {
567 b->TextReverseColor = LuaToString(l, -1);
568 } else if (!strcmp(value, "TextPos")) {
569 LuaCheckTableSize(l, -1, 2);
570 b->TextX = LuaToNumber(l, -1, 1);
571 b->TextY = LuaToNumber(l, -1, 2);
572 } else if (!strcmp(value, "TextAlign")) {
573 value = LuaToString(l, -1);
574 if (!strcmp(value, "Center")) {
575 b->TextAlign = TextAlignCenter;
576 } else if (!strcmp(value, "Right")) {
577 b->TextAlign = TextAlignRight;
578 } else if (!strcmp(value, "Left")) {
579 b->TextAlign = TextAlignLeft;
580 } else {
581 LuaError(l, "Invalid text alignment: %s" _C_ value);
582 }
583 } else if (!strcmp(value, "Default")) {
584 ParseButtonStyleProperties(l, &b->Default);
585 } else if (!strcmp(value, "Hover")) {
586 ParseButtonStyleProperties(l, &b->Hover);
587 } else if (!strcmp(value, "Clicked")) {
588 ParseButtonStyleProperties(l, &b->Clicked);
589 } else {
590 LuaError(l, "Unsupported tag: %s" _C_ value);
591 }
592 lua_pop(l, 1);
593 }
594
595 if (b->Default.TextX == 0xFFFFFF) {
596 b->Default.TextX = b->TextX;
597 b->Default.TextY = b->TextY;
598 }
599 if (b->Hover.TextX == 0xFFFFFF) {
600 b->Hover.TextX = b->TextX;
601 b->Hover.TextY = b->TextY;
602 }
603 if (b->Clicked.TextX == 0xFFFFFF) {
604 b->Clicked.TextX = b->TextX;
605 b->Clicked.TextY = b->TextY;
606 }
607
608 if (b->Default.TextAlign == TextAlignUndefined) {
609 b->Default.TextAlign = b->TextAlign;
610 }
611 if (b->Hover.TextAlign == TextAlignUndefined) {
612 b->Hover.TextAlign = b->TextAlign;
613 }
614 if (b->Clicked.TextAlign == TextAlignUndefined) {
615 b->Clicked.TextAlign = b->TextAlign;
616 }
617
618 return 0;
619 }
620
621 /**
622 ** Add a Lua handler
623 ** FIXME: when should these be freed?
624 */
625 int AddHandler(lua_State *l)
626 {
627 lua_pushstring(l, "_handlers_");
628 lua_gettable(l, LUA_GLOBALSINDEX);
629 if (lua_isnil(l, -1)) {
630 lua_pop(l, 1);
631 lua_pushstring(l, "_handlers_");
632 lua_newtable(l);
633 lua_settable(l, LUA_GLOBALSINDEX);
634 lua_pushstring(l, "_handlers_");
635 lua_gettable(l, LUA_GLOBALSINDEX);
636 }
637 lua_pushvalue(l, -2);
638 lua_rawseti(l, -2, HandleCount);
639 lua_pop(l, 1);
640
641 return HandleCount++;
642 }
643
644 /**
645 ** Call a Lua handler
646 */
647 void CallHandler(unsigned int handle, int value)
648 {
649 lua_pushstring(Lua, "_handlers_");
650 lua_gettable(Lua, LUA_GLOBALSINDEX);
651 lua_rawgeti(Lua, -1, handle);
652 lua_pushnumber(Lua, value);
653 LuaCall(1, 1);
654 lua_pop(Lua, 1);
655 }
656
657 /**
658 ** Define a button.
659 **
660 ** @param l Lua state.
661 */
662 static int CclDefineButton(lua_State *l)
663 {
664 char buf[64];
665 const char *value;
666 const char *s2;
667 ButtonAction ba;
668
669 LuaCheckArgs(l, 1);
670 LuaCheckTable(l, 1);
671
672 //
673 // Parse the arguments
674 //
675 lua_pushnil(l);
676 while (lua_next(l, 1)) {
677 value = LuaToString(l, -2);
678 if (!strcmp(value, "Pos")) {
679 ba.Pos = LuaToNumber(l, -1);
680 } else if (!strcmp(value, "Level")) {
681 ba.Level = LuaToNumber(l, -1);
682 } else if (!strcmp(value, "Icon")) {
683 ba.Icon.Name = LuaToString(l, -1);
684 } else if (!strcmp(value, "Action")) {
685 value = LuaToString(l, -1);
686 if (!strcmp(value, "move")) {
687 ba.Action = ButtonMove;
688 } else if (!strcmp(value, "stop")) {
689 ba.Action = ButtonStop;
690 } else if (!strcmp(value, "attack")) {
691 ba.Action = ButtonAttack;
692 } else if (!strcmp(value, "repair")) {
693 ba.Action = ButtonRepair;
694 } else if (!strcmp(value, "harvest")) {
695 ba.Action = ButtonHarvest;
696 } else if (!strcmp(value, "button")) {
697 ba.Action = ButtonButton;
698 } else if (!strcmp(value, "build")) {
699 ba.Action = ButtonBuild;
700 } else if (!strcmp(value, "train-unit")) {
701 ba.Action = ButtonTrain;
702 } else if (!strcmp(value, "patrol")) {
703 ba.Action = ButtonPatrol;
704 } else if (!strcmp(value, "stand-ground")) {
705 ba.Action = ButtonStandGround;
706 } else if (!strcmp(value, "attack-ground")) {
707 ba.Action = ButtonAttackGround;
708 } else if (!strcmp(value, "cast-spell")) {
709 ba.Action = ButtonSpellCast;
710 } else if (!strcmp(value, "unload")) {
711 ba.Action = ButtonUnload;
712 } else if (!strcmp(value, "cancel")) {
713 ba.Action = ButtonCancel;
714 } else if (!strcmp(value, "cancel-train-unit")) {
715 ba.Action = ButtonCancelTrain;
716 } else if (!strcmp(value, "cancel-build")) {
717 ba.Action = ButtonCancelBuild;
718 } else {
719 LuaError(l, "Unsupported button action: %s" _C_ value);
720 }
721 } else if (!strcmp(value, "Value")) {
722 if (!lua_isnumber(l, -1) && !lua_isstring(l, -1)) {
723 LuaError(l, "incorrect argument");
724 }
725 if (lua_isnumber(l, -1)) {
726 sprintf_s(buf, sizeof(buf), "%ld", (long int)lua_tonumber(l, -1));
727 s2 = buf;
728 } else {
729 s2 = lua_tostring(l, -1);
730 }
731 ba.ValueStr = s2;
732 } else if (!strcmp(value, "Allowed")) {
733 value = LuaToString(l, -1);
734 if (!strcmp(value, "check-true")) {
735 ba.Allowed = ButtonCheckTrue;
736 } else if (!strcmp(value, "check-false")) {
737 ba.Allowed = ButtonCheckFalse;
738 } else if (!strcmp(value, "check-units-or")) {
739 ba.Allowed = ButtonCheckUnitsOr;
740 } else if (!strcmp(value, "check-units-and")) {
741 ba.Allowed = ButtonCheckUnitsAnd;
742 } else if (!strcmp(value, "check-network")) {
743 ba.Allowed = ButtonCheckNetwork;
744 } else if (!strcmp(value, "check-no-network")) {
745 ba.Allowed = ButtonCheckNoNetwork;
746 } else if (!strcmp(value, "check-no-work")) {
747 ba.Allowed = ButtonCheckNoWork;
748 } else if (!strcmp(value, "check-attack")) {
749 ba.Allowed = ButtonCheckAttack;
750 } else {
751 LuaError(l, "Unsupported action: %s" _C_ value);
752 }
753 } else if (!strcmp(value, "AllowArg")) {
754 int subargs;
755 int k;
756 std::string allowstr;
757
758 LuaCheckTable(l, -1);
759 subargs = lua_objlen(l, -1);
760 for (k = 0; k < subargs; ++k) {
761 s2 = LuaToString(l, -1, k + 1);
762 allowstr += s2;
763 if (k != subargs - 1) {
764 allowstr += ",";
765 }
766 }
767 ba.AllowStr = allowstr;
768 } else if (!strcmp(value, "Hint")) {
769 ba.Hint = LuaToString(l, -1);
770 } else if (!strcmp(value, "ForUnit")) {
771 int subargs;
772 int k;
773 std::string umask;
774
775 LuaCheckTable(l, -1);
776 // FIXME: ba.UnitMask shouldn't be a string
777 umask = ",";
778 subargs = lua_objlen(l, -1);
779 for (k = 0; k < subargs; ++k) {
780 s2 = LuaToString(l, -1, k + 1);
781 umask += s2;
782 umask += ",";
783 }
784 ba.UnitMask = umask;
785 if (ba.UnitMask.compare(0, 3, ",*,") == 0) {
786 ba.UnitMask = "*";
787 }
788 } else {
789 LuaError(l, "Unsupported tag: %s" _C_ value);
790 }
791 lua_pop(l, 1);
792 }
793 AddButton(ba.Pos, ba.Level, ba.Icon.Name, ba.Action, ba.ValueStr,
794 ba.Allowed, ba.AllowStr, ba.Hint, ba.UnitMask);
795
796 return 0;
797 }
798
799 /**
800 ** Run the set-selection-changed-hook.
801 */
802 void SelectionChanged(void)
803 {
804 // We Changed out selection, anything pending buttonwise must be cleared
805 UI.StatusLine.Clear();
806 ClearCosts();
807 CurrentButtonLevel = 0;
808 UI.ButtonPanel.Update();
809 GameCursor = UI.Point.Cursor;
810 CursorBuilding = NULL;
811 CursorState = CursorStatePoint;
812 UI.ButtonPanel.Update();
813 }
814
815 /**
816 ** The selected unit has been altered.
817 */
818 void SelectedUnitChanged(void)
819 {
820 UI.ButtonPanel.Update();
821 }
822
823 /**
824 ** Add a new message.
825 **
826 ** @param l Lua state.
827 */
828 static int CclAddMessage(lua_State *l)
829 {
830 LuaCheckArgs(l, 1);
831 SetMessage("%s", LuaToString(l, 1));
832 return 0;
833 }
834
835 /**
836 ** Set the keys which are use for grouping units, helpful for other keyboards
837 **
838 ** @param l Lua state.
839 */
840 static int CclSetGroupKeys(lua_State *l)
841 {
842 LuaCheckArgs(l, 1);
843 UiGroupKeys = LuaToString(l, 1);
844 return 0;
845 }
846
847 /**
848 ** Set basic map caracteristics.
849 **
850 ** @param l Lua state.
851 */
852 static int CclPresentMap(lua_State *l)
853 {
854 LuaCheckArgs(l, 5);
855
856 Map.Info.Description = LuaToString(l, 1);
857 // Number of players in LuaToNumber(l, 2); // Not used yet.
858 Map.Info.MapWidth = LuaToNumber(l, 3);
859 Map.Info.MapHeight = LuaToNumber(l, 4);
860 Map.Info.MapUID = LuaToNumber(l, 5);
861
862 return 0;
863 }
864
865 /**
866 ** Define the lua file that will build the map
867 **
868 ** @param l Lua state.
869 */
870 static int CclDefineMapSetup(lua_State *l)
871 {
872 LuaCheckArgs(l, 1);
873 Map.Info.Filename = LuaToString(l, 1);
874
875 return 0;
876 }
877
878 /**
879 ** Register CCL features for UI.
880 */
881 void UserInterfaceCclRegister(void)
882 {
883 lua_register(Lua, "AddMessage", CclAddMessage);
884
885 lua_register(Lua, "SetMouseScrollSpeedDefault", CclSetMouseScrollSpeedDefault);
886 lua_register(Lua, "SetMouseScrollSpeedControl", CclSetMouseScrollSpeedControl);
887
888 lua_register(Lua, "SetClickMissile", CclSetClickMissile);
889 lua_register(Lua, "SetDamageMissile", CclSetDamageMissile);
890
891 lua_register(Lua, "SetMaxOpenGLTexture", CclSetMaxOpenGLTexture);
892 lua_register(Lua, "SetVideoResolution", CclSetVideoResolution);
893 lua_register(Lua, "GetVideoResolution", CclGetVideoResolution);
894 lua_register(Lua, "SetVideoFullScreen", CclSetVideoFullScreen);
895 lua_register(Lua, "GetVideoFullScreen", CclGetVideoFullScreen);
896
897 lua_register(Lua, "SetTitleScreens", CclSetTitleScreens);
898
899 lua_register(Lua, "DefineCursor", CclDefineCursor);
900 lua_register(Lua, "SetGameCursor", CclSetGameCursor);
901 lua_register(Lua, "DefineViewports", CclDefineViewports);
902
903 lua_register(Lua, "RightButtonAttacks", CclRightButtonAttacks);
904 lua_register(Lua, "RightButtonMoves", CclRightButtonMoves);
905
906 lua_register(Lua, "DefineButton", CclDefineButton);
907
908 lua_register(Lua, "DefineButtonStyle", CclDefineButtonStyle);
909
910 lua_register(Lua, "PresentMap", CclPresentMap);
911 lua_register(Lua, "DefineMapSetup", CclDefineMapSetup);
912
913 lua_register(Lua, "SetGroupKeys", CclSetGroupKeys);
914 }
915
916 //@}