Debian Codegraph @

engine/ui/widgets.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 widgets.cpp - The stratagus ui widgets. */
11 //
12 // (c) Copyright 2005-2008 by Francois Beerten and Jimmy Salmon
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 "stratagus.h"
36 #include "video.h"
37 #include "font.h"
38 #include "cursor.h"
39 #include "ui.h"
40 #include "widgets.h"
41 #include "network.h"
42 #include "netconnect.h"
43 #include "editor.h"
44 #include "script.h"
45
46 /*----------------------------------------------------------------------------
47 -- Variables
48 ----------------------------------------------------------------------------*/
49
50 // Guichan stuff we need
51 gcn::Gui *Gui; /// A Gui object - binds it all together
52 gcn::SDLInput *Input; /// Input driver
53
54 static EventCallback GuichanCallbacks;
55
56 static std::stack<MenuScreen *> MenuStack;
57
58
59 /*----------------------------------------------------------------------------
60 -- Functions
61 ----------------------------------------------------------------------------*/
62
63
64 static void MenuHandleButtonDown(unsigned buttons)
65 {
66 }
67 static void MenuHandleButtonUp(unsigned buttons)
68 {
69 }
70 static void MenuHandleMouseMove(int x, int y)
71 {
72 HandleCursorMove(&x, &y);
73 }
74 static void MenuHandleKeyDown(unsigned key, unsigned keychar)
75 {
76 HandleKeyModifiersDown(key, keychar);
77 }
78 static void MenuHandleKeyUp(unsigned key, unsigned keychar)
79 {
80 HandleKeyModifiersUp(key, keychar);
81 }
82 static void MenuHandleKeyRepeat(unsigned key, unsigned keychar)
83 {
84 Input->processKeyRepeat();
85 HandleKeyModifiersDown(key, keychar);
86 }
87
88
89 /**
90 ** Initializes the GUI stuff
91 */
92 void initGuichan()
93 {
94 gcn::Graphics *graphics;
95
96 if (UseOpenGL) {
97 graphics = new MyOpenGLGraphics();
98 } else {
99 graphics = new gcn::SDLGraphics();
100
101 // Set the target for the graphics object to be the screen.
102 // In other words, we will draw to the screen.
103 // Note, any surface will do, it doesn't have to be the screen.
104 ((gcn::SDLGraphics *)graphics)->setTarget(TheScreen);
105 }
106
107 Input = new gcn::SDLInput();
108
109 Gui = new gcn::Gui();
110 Gui->setGraphics(graphics);
111 Gui->setInput(Input);
112 Gui->setTop(NULL);
113 Gui->setUseDirtyDrawing(!UseOpenGL);
114
115 GuichanCallbacks.ButtonPressed = &MenuHandleButtonDown;
116 GuichanCallbacks.ButtonReleased = &MenuHandleButtonUp;
117 GuichanCallbacks.MouseMoved = &MenuHandleMouseMove;
118 GuichanCallbacks.MouseExit = &HandleMouseExit;
119 GuichanCallbacks.KeyPressed = &MenuHandleKeyDown;
120 GuichanCallbacks.KeyReleased = &MenuHandleKeyUp;
121 GuichanCallbacks.KeyRepeated = &MenuHandleKeyRepeat;
122 GuichanCallbacks.NetworkEvent = NetworkEvent;
123 }
124
125 /**
126 ** Free all guichan infrastructure
127 */
128 void freeGuichan()
129 {
130 if (Gui) {
131 delete Gui->getGraphics();
132 }
133 delete Gui;
134 delete Input;
135
136 Gui = NULL;
137 Input = NULL;
138 }
139
140 /**
141 ** Handle input events
142 **
143 ** @param event event to handle, null if no more events for this frame
144 */
145 void handleInput(const SDL_Event *event)
146 {
147 if (event) {
148 if (Input) {
149 try {
150 Input->pushInput(*event);
151 } catch (const gcn::Exception &) {
152 // ignore unhandled buttons
153 }
154 }
155 } else {
156 if (Gui) {
157 Gui->logic();
158 }
159 }
160 }
161
162 void DrawGuichanWidgets()
163 {
164 if (Gui) {
165 Gui->setUseDirtyDrawing(!UseOpenGL && !GameRunning && !Editor.Running && !PatchEditorRunning);
166 Gui->draw();
167 }
168 }
169
170
171 /*----------------------------------------------------------------------------
172 -- LuaActionListener
173 ----------------------------------------------------------------------------*/
174
175
176 /**
177 ** LuaActionListener constructor
178 **
179 ** @param l Lua state
180 ** @param f Listener function
181 */
182 LuaActionListener::LuaActionListener(lua_State *l, lua_Object f) :
183 callback(l, f)
184 {
185 }
186
187 /**
188 ** Called when an action is recieved from a Widget. It is used
189 ** to be able to recieve a notification that an action has
190 ** occured.
191 **
192 ** @param eventId the identifier of the Widget
193 */
194 void LuaActionListener::action(const std::string &eventId)
195 {
196 callback.pushPreamble();
197 callback.pushString(eventId.c_str());
198 callback.run();
199 }
200
201 /**
202 ** LuaActionListener destructor
203 */
204 LuaActionListener::~LuaActionListener()
205 {
206 }
207
208
209 /*----------------------------------------------------------------------------
210 -- MyOpenGLGraphics
211 ----------------------------------------------------------------------------*/
212
213
214 void MyOpenGLGraphics::_beginDraw()
215 {
216 gcn::Rectangle area(0, 0, Video.Width, Video.Height);
217 pushClipArea(area);
218 }
219
220 void MyOpenGLGraphics::_endDraw()
221 {
222 popClipArea();
223 }
224
225 void MyOpenGLGraphics::drawImage(const gcn::Image* image, int srcX, int srcY,
226 int dstX, int dstY, int width, int height)
227 {
228 const gcn::ClipRectangle &r = this->getCurrentClipArea();
229 int right = std::min(r.x + r.width - 1, Video.Width - 1);
230 int bottom = std::min(r.y + r.height - 1, Video.Height - 1);
231
232 if (r.x > right || r.y > bottom) {
233 return;
234 }
235
236 PushClipping();
237 SetClipping(r.x, r.y, right, bottom);
238 ((CGraphic *)image)->DrawSubClip(srcX, srcY, width, height,
239 dstX + mClipStack.top().xOffset, dstY + mClipStack.top().yOffset);
240 PopClipping();
241 }
242
243 void MyOpenGLGraphics::drawPoint(int x, int y)
244 {
245 gcn::Color c = this->getColor();
246 Video.DrawPixelClip(Video.MapRGBA(0, c.r, c.g, c.b, c.a),
247 x + mClipStack.top().xOffset, y + mClipStack.top().yOffset);
248 }
249
250 void MyOpenGLGraphics::drawLine(int x1, int y1, int x2, int y2)
251 {
252 gcn::Color c = this->getColor();
253 Video.DrawLineClip(Video.MapRGBA(0, c.r, c.g, c.b, c.a),
254 x1 + mClipStack.top().xOffset, y1 + mClipStack.top().yOffset,
255 x2 + mClipStack.top().xOffset, y2 + mClipStack.top().yOffset);
256 }
257
258 void MyOpenGLGraphics::drawRectangle(const gcn::Rectangle& rectangle)
259 {
260 gcn::Rectangle area = rectangle;
261 gcn::ClipRectangle top = mClipStack.top();
262 gcn::Color c = this->getColor();
263
264 area.x += top.xOffset;
265 area.y += top.yOffset;
266
267 if (!area.intersect(top) || c.a == 0) {
268 return;
269 }
270
271 int x1 = std::max(area.x, top.x);
272 int y1 = std::max(area.y, top.y);
273 int x2 = std::min(area.x + area.width, top.x + top.width);
274 int y2 = std::min(area.y + area.height, top.y + top.height);
275
276 Video.DrawTransRectangle(Video.MapRGB(0, c.r, c.g, c.b),
277 x1, y1, x2 - x1, y2 - y1, mColor.a);
278 }
279
280 void MyOpenGLGraphics::fillRectangle(const gcn::Rectangle& rectangle)
281 {
282 gcn::Rectangle area = rectangle;
283 gcn::ClipRectangle top = mClipStack.top();
284 gcn::Color c = this->getColor();
285
286 area.x += top.xOffset;
287 area.y += top.yOffset;
288
289 if (!area.intersect(top) || c.a == 0) {
290 return;
291 }
292
293 int x1 = std::max(area.x, top.x);
294 int y1 = std::max(area.y, top.y);
295 int x2 = std::min(area.x + area.width, top.x + top.width);
296 int y2 = std::min(area.y + area.height, top.y + top.height);
297
298 Video.FillTransRectangle(Video.MapRGB(0, c.r, c.g, c.b),
299 x1, y1, x2 - x1, y2 - y1, c.a);
300 }
301
302
303 /*----------------------------------------------------------------------------
304 -- ImageButton
305 ----------------------------------------------------------------------------*/
306
307
308 /**
309 ** ImageButton constructor
310 */
311 ImageButton::ImageButton() :
312 Button(), normalImage(NULL), pressedImage(NULL),
313 disabledImage(NULL)
314 {
315 setForegroundColor(0xffffff);
316 }
317
318 /**
319 ** ImageButton constructor
320 **
321 ** @param caption Caption text
322 */
323 ImageButton::ImageButton(const std::string &caption) :
324 Button(caption), normalImage(NULL), pressedImage(NULL),
325 disabledImage(NULL)
326 {
327 setForegroundColor(0xffffff);
328 }
329
330 /**
331 ** Draw the image button
332 **
333 ** @param graphics Graphics object to draw with
334 */
335 void ImageButton::draw(gcn::Graphics *graphics)
336 {
337 if (!normalImage) {
338 Button::draw(graphics);
339 return;
340 }
341
342 gcn::Image *img;
343
344 if (!isEnabled()) {
345 img = disabledImage ? disabledImage : normalImage;
346 } else if (isPressed()) {
347 img = pressedImage ? pressedImage : normalImage;
348 } else if (0 && hasMouse()) {
349 // FIXME: add mouse-over image
350 img = NULL;
351 } else {
352 img = normalImage;
353 }
354 graphics->drawImage(img, 0, 0, 0, 0,
355 img->getWidth(), img->getHeight());
356
357 graphics->setColor(getForegroundColor());
358
359 int textX;
360 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
361
362 switch (getAlignment()) {
363 case gcn::Graphics::LEFT:
364 textX = 4;
365 break;
366 case gcn::Graphics::CENTER:
367 textX = getWidth() / 2;
368 break;
369 case gcn::Graphics::RIGHT:
370 textX = getWidth() - 4;
371 break;
372 default:
373 throw GCN_EXCEPTION("Unknown alignment.");
374 }
375
376 graphics->setFont(getFont());
377 if (isPressed()) {
378 graphics->drawText(getCaption(), textX + 4, textY + 4, getAlignment());
379 } else {
380 graphics->drawText(getCaption(), textX + 2, textY + 2, getAlignment());
381 }
382
383 if (hasFocus()) {
384 graphics->drawRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight()));
385 }
386 }
387
388 /**
389 ** Automatically adjust the size of an image button
390 */
391 void ImageButton::adjustSize()
392 {
393 if (normalImage) {
394 setWidth(normalImage->getWidth());
395 setHeight(normalImage->getHeight());
396 } else {
397 Button::adjustSize();
398 }
399 }
400
401 /*----------------------------------------------------------------------------
402 -- ImageRadioButton
403 ----------------------------------------------------------------------------*/
404
405
406 /**
407 ** ImageRadioButton constructor
408 */
409 ImageRadioButton::ImageRadioButton() : gcn::RadioButton(),
410 uncheckedNormalImage(NULL), uncheckedPressedImage(NULL),
411 checkedNormalImage(NULL), checkedPressedImage(NULL),
412 mMouseDown(false)
413 {
414 }
415
416 /**
417 ** ImageRadioButton constructor
418 */
419 ImageRadioButton::ImageRadioButton(const std::string &caption,
420 const std::string &group, bool marked) :
421 gcn::RadioButton(caption, group, marked),
422 uncheckedNormalImage(NULL), uncheckedPressedImage(NULL),
423 checkedNormalImage(NULL), checkedPressedImage(NULL),
424 mMouseDown(false)
425 {
426 }
427
428 /**
429 ** Draw the image radio button (not the caption)
430 */
431 void ImageRadioButton::drawBox(gcn::Graphics *graphics)
432 {
433 gcn::Image *img = NULL;
434
435 if (isMarked()) {
436 if (mMouseDown) {
437 img = checkedPressedImage;
438 } else {
439 img = checkedNormalImage;
440 }
441 } else {
442 if (mMouseDown) {
443 img = uncheckedPressedImage;
444 } else {
445 img = uncheckedNormalImage;
446 }
447 }
448
449 if (img) {
450 graphics->drawImage(img, 0, 0, 0, (getHeight() - img->getHeight()) / 2,
451 img->getWidth(), img->getHeight());
452 } else {
453 RadioButton::drawBox(graphics);
454 }
455 }
456
457 /**
458 ** Draw the image radio button
459 */
460 void ImageRadioButton::draw(gcn::Graphics *graphics)
461 {
462 drawBox(graphics);
463
464 graphics->setFont(getFont());
465 graphics->setColor(getForegroundColor());
466
467 int width;
468 if (uncheckedNormalImage) {
469 width = uncheckedNormalImage->getWidth();
470 width += width / 2;
471 } else {
472 width = getHeight();
473 width += width / 2;
474 }
475
476 graphics->drawText(getCaption(), width - 2, 0);
477
478 if (hasFocus()) {
479 graphics->drawRectangle(gcn::Rectangle(width - 4, 0, getWidth() - width + 3, getHeight()));
480 }
481 }
482
483 /**
484 ** Mouse button pressed callback
485 */
486 void ImageRadioButton::mousePress(int x, int y, int button)
487 {
488 if (button == gcn::MouseInput::LEFT && hasMouse()) {
489 mMouseDown = true;
490 }
491 }
492
493 /**
494 ** Mouse button released callback
495 */
496 void ImageRadioButton::mouseRelease(int x, int y, int button)
497 {
498 if (button == gcn::MouseInput::LEFT) {
499 mMouseDown = false;
500 }
501 }
502
503 /**
504 ** Mouse clicked callback
505 */
506 void ImageRadioButton::mouseClick(int x, int y, int button, int count)
507 {
508 if (button == gcn::MouseInput::LEFT) {
509 setMarked(true);
510 generateAction();
511 }
512 }
513
514 /**
515 ** Adjusts the size to fit the image and font size
516 */
517 void ImageRadioButton::adjustSize()
518 {
519 int width, height;
520
521 height = getFont()->getHeight();
522 if (uncheckedNormalImage) {
523 width = uncheckedNormalImage->getWidth();
524 width += width / 2;
525 if (uncheckedNormalImage->getHeight() > height) {
526 height = uncheckedNormalImage->getHeight();
527 }
528 } else {
529 width = getFont()->getHeight();
530 width += width / 2;
531 }
532
533 setHeight(height);
534 setWidth(getFont()->getWidth(mCaption) + width);
535 }
536
537
538 /*----------------------------------------------------------------------------
539 -- ImageCheckbox
540 ----------------------------------------------------------------------------*/
541
542
543 /**
544 ** Image checkbox constructor
545 */
546 ImageCheckBox::ImageCheckBox() : gcn::CheckBox(),
547 uncheckedNormalImage(NULL), uncheckedPressedImage(NULL),
548 checkedNormalImage(NULL), checkedPressedImage(NULL),
549 mMouseDown(false)
550 {
551 }
552
553 /**
554 ** Image checkbox constructor
555 */
556 ImageCheckBox::ImageCheckBox(const std::string &caption, bool marked) :
557 gcn::CheckBox(caption, marked),
558 uncheckedNormalImage(NULL), uncheckedPressedImage(NULL),
559 checkedNormalImage(NULL), checkedPressedImage(NULL),
560 mMouseDown(false)
561 {
562 }
563
564 /**
565 ** Draw the image checkbox
566 */
567 void ImageCheckBox::draw(gcn::Graphics *graphics)
568 {
569 drawBox(graphics);
570
571 graphics->setFont(getFont());
572 graphics->setColor(getForegroundColor());
573
574 int width;
575 if (uncheckedNormalImage) {
576 width = uncheckedNormalImage->getWidth();
577 width += width / 2;
578 } else {
579 width = getHeight();
580 width += width / 2;
581 }
582
583 graphics->drawText(getCaption(), width - 2, 0);
584
585 if (hasFocus()) {
586 graphics->drawRectangle(gcn::Rectangle(width - 4, 0, getWidth() - width + 3, getHeight()));
587 }
588 }
589
590 /**
591 ** Draw the checkbox (not the caption)
592 */
593 void ImageCheckBox::drawBox(gcn::Graphics *graphics)
594 {
595 gcn::Image *img = NULL;
596
597 if (mMarked) {
598 if (mMouseDown) {
599 img = checkedPressedImage;
600 } else {
601 img = checkedNormalImage;
602 }
603 } else {
604 if (mMouseDown) {
605 img = uncheckedPressedImage;
606 } else {
607 img = uncheckedNormalImage;
608 }
609 }
610
611 if (img) {
612 graphics->drawImage(img, 0, 0, 0, (getHeight() - img->getHeight()) / 2,
613 img->getWidth(), img->getHeight());
614 } else {
615 CheckBox::drawBox(graphics);
616 }
617 }
618
619 /**
620 ** Mouse button pressed callback
621 */
622 void ImageCheckBox::mousePress(int x, int y, int button)
623 {
624 if (button == gcn::MouseInput::LEFT && hasMouse()) {
625 mMouseDown = true;
626 }
627 }
628
629 /**
630 ** Mouse button released callback
631 */
632 void ImageCheckBox::mouseRelease(int x, int y, int button)
633 {
634 if (button == gcn::MouseInput::LEFT) {
635 mMouseDown = false;
636 }
637 }
638
639 /**
640 ** Mouse clicked callback
641 */
642 void ImageCheckBox::mouseClick(int x, int y, int button, int count)
643 {
644 if (button == gcn::MouseInput::LEFT) {
645 toggle();
646 }
647 }
648
649 /**
650 ** Adjusts the size to fit the image and font size
651 */
652 void ImageCheckBox::adjustSize()
653 {
654 int width, height;
655
656 height = getFont()->getHeight();
657 if (uncheckedNormalImage) {
658 width = uncheckedNormalImage->getWidth();
659 width += width / 2;
660 if (uncheckedNormalImage->getHeight() > height) {
661 height = uncheckedNormalImage->getHeight();
662 }
663 } else {
664 width = getFont()->getHeight();
665 width += width / 2;
666 }
667
668 setHeight(height);
669 setWidth(getFont()->getWidth(mCaption) + width);
670 }
671
672
673 /*----------------------------------------------------------------------------
674 -- ImageSlider
675 ----------------------------------------------------------------------------*/
676
677
678 /**
679 ** Image slider constructor
680 */
681 ImageSlider::ImageSlider(double scaleEnd) :
682 Slider(scaleEnd), markerImage(NULL), backgroundImage(NULL)
683 {
684 }
685
686 /**
687 ** Image slider constructor
688 */
689 ImageSlider::ImageSlider(double scaleStart, double scaleEnd) :
690 Slider(scaleStart, scaleEnd), markerImage(NULL), backgroundImage(NULL)
691 {
692 }
693
694 /**
695 ** Draw the image slider marker
696 */
697 void ImageSlider::drawMarker(gcn::Graphics *graphics)
698 {
699 if (markerImage) {
700 if (getOrientation() == HORIZONTAL) {
701 int v = getMarkerPosition();
702 graphics->drawImage(markerImage, 0, 0, v, 0,
703 markerImage->getWidth(), markerImage->getHeight());
704 } else {
705 int v = (getHeight() - getMarkerLength()) - getMarkerPosition();
706 graphics->drawImage(markerImage, 0, 0, 0, v,
707 markerImage->getWidth(), markerImage->getHeight());
708 }
709 } else {
710 Slider::drawMarker(graphics);
711 }
712 }
713
714 /**
715 ** Draw the image slider
716 */
717 void ImageSlider::draw(gcn::Graphics *graphics)
718 {
719 if (backgroundImage) {
720 graphics->drawImage(backgroundImage, 0, 0, 0, 0,
721 backgroundImage->getWidth(), backgroundImage->getHeight());
722 drawMarker(graphics);
723 } else {
724 Slider::draw(graphics);
725 }
726 }
727
728 /**
729 ** Set the marker image
730 */
731 void ImageSlider::setMarkerImage(gcn::Image *image)
732 {
733 markerImage = image;
734 setMarkerLength(image->getWidth());
735 }
736
737 /**
738 ** Set the background image
739 */
740 void ImageSlider::setBackgroundImage(gcn::Image *image)
741 {
742 backgroundImage = image;
743 }
744
745
746 /*----------------------------------------------------------------------------
747 -- MultiLineLabel
748 ----------------------------------------------------------------------------*/
749
750
751 /**
752 ** MultiLineLabel constructor
753 */
754 MultiLineLabel::MultiLineLabel()
755 {
756 this->mAlignment = LEFT;
757 this->mVerticalAlignment = TOP;
758 this->mLineWidth = 0;
759 }
760
761 /**
762 ** MultiLineLabel constructor
763 */
764 MultiLineLabel::MultiLineLabel(const std::string &caption)
765 {
766 this->mCaption = caption;
767 this->mAlignment = LEFT;
768 this->mVerticalAlignment = TOP;
769
770 this->mLineWidth = 999999;
771 this->wordWrap();
772 this->adjustSize();
773 }
774
775 /**
776 ** Set the caption
777 */
778 void MultiLineLabel::setCaption(const std::string &caption)
779 {
780 this->mCaption = caption;
781 this->wordWrap();
782 this->setDirty(true);
783 }
784
785 /**
786 ** Get the caption
787 */
788 const std::string &MultiLineLabel::getCaption() const
789 {
790 return this->mCaption;
791 }
792
793 /**
794 ** Set the horizontal alignment
795 */
796 void MultiLineLabel::setAlignment(unsigned int alignment)
797 {
798 this->mAlignment = alignment;
799 }
800
801 /**
802 ** Get the horizontal alignment
803 */
804 unsigned int MultiLineLabel::getAlignment()
805 {
806 return this->mAlignment;
807 }
808
809 /**
810 ** Set the vertical alignment
811 */
812 void MultiLineLabel::setVerticalAlignment(unsigned int alignment)
813 {
814 this->mVerticalAlignment = alignment;
815 }
816
817 /**
818 ** Get the vertical alignment
819 */
820 unsigned int MultiLineLabel::getVerticalAlignment()
821 {
822 return this->mVerticalAlignment;
823 }
824
825 /**
826 ** Set the line width
827 */
828 void MultiLineLabel::setLineWidth(int width)
829 {
830 this->mLineWidth = width;
831 this->wordWrap();
832 }
833
834 /**
835 ** Get the line width
836 */
837 int MultiLineLabel::getLineWidth()
838 {
839 return this->mLineWidth;
840 }
841
842 /**
843 ** Adjust the size
844 */
845 void MultiLineLabel::adjustSize()
846 {
847 int width = 0;
848 for (int i = 0; i < (int)this->mTextRows.size(); ++i) {
849 int w = this->getFont()->getWidth(this->mTextRows[i]);
850 if (width < w) {
851 if (w <= this->mLineWidth) {
852 width = w;
853 } else {
854 width = this->mLineWidth;
855 }
856 }
857 }
858 this->setWidth(width);
859 this->setHeight(this->getFont()->getHeight() * this->mTextRows.size());
860 }
861
862 /**
863 ** Draw the label
864 */
865 void MultiLineLabel::draw(gcn::Graphics *graphics)
866 {
867 graphics->setFont(getFont());
868 graphics->setColor(getForegroundColor());
869
870 int textX, textY;
871 switch (this->getAlignment()) {
872 case LEFT:
873 textX = 0;
874 break;
875 case CENTER:
876 textX = this->getWidth() / 2;
877 break;
878 case RIGHT:
879 textX = this->getWidth();
880 break;
881 default:
882 throw GCN_EXCEPTION("Unknown alignment.");
883 }
884 switch (this->getVerticalAlignment()) {
885 case TOP:
886 textY = 0;
887 break;
888 case CENTER:
889 textY = (this->getHeight() - (int)this->mTextRows.size() * this->getFont()->getHeight()) / 2;
890 break;
891 case BOTTOM:
892 textY = this->getHeight() - (int)this->mTextRows.size() * this->getFont()->getHeight();
893 break;
894 default:
895 throw GCN_EXCEPTION("Unknown alignment.");
896 }
897
898 for (int i = 0; i < (int)this->mTextRows.size(); ++i) {
899 graphics->drawText(this->mTextRows[i], textX, textY + i * this->getFont()->getHeight(),
900 this->getAlignment());
901 }
902 }
903
904 /**
905 ** Draw the border
906 */
907 void MultiLineLabel::drawBorder(gcn::Graphics *graphics)
908 {
909 gcn::Color faceColor = getBaseColor();
910 gcn::Color highlightColor, shadowColor;
911 int alpha = getBaseColor().a;
912 int width = getWidth() + getBorderSize() * 2 - 1;
913 int height = getHeight() + getBorderSize() * 2 - 1;
914 highlightColor = faceColor + 0x303030;
915 highlightColor.a = alpha;
916 shadowColor = faceColor - 0x303030;
917 shadowColor.a = alpha;
918
919 for (unsigned int i = 0; i < getBorderSize(); ++i) {
920 graphics->setColor(shadowColor);
921 graphics->drawLine(i, i, width - i, i);
922 graphics->drawLine(i, i + 1, i, height - i - 1);
923 graphics->setColor(highlightColor);
924 graphics->drawLine(width - i, i + 1, width - i, height - i);
925 graphics->drawLine(i, height - i, width - i - 1, height - i);
926 }
927 }
928
929 /**
930 ** Do word wrap
931 */
932 void MultiLineLabel::wordWrap()
933 {
934 gcn::Font *font = this->getFont();
935 int lineWidth = this->getLineWidth();
936 std::string str = this->getCaption();
937 size_t pos, lastPos;
938 std::string substr;
939 bool done = false;
940 bool first = true;
941
942 this->mTextRows.clear();
943
944 while (!done) {
945 if (str.find('\n') != std::string::npos || font->getWidth(str) > lineWidth) {
946 // string too wide or has a newline, split it up
947 first = true;
948 lastPos = 0;
949 while (1) {
950 // look for any whitespace
951 pos = str.find_first_of(" \t\n", first ? 0 : lastPos + 1);
952 if (pos != std::string::npos) {
953 // found space, now check width
954 substr = str.substr(0, pos);
955 if (font->getWidth(substr) > lineWidth) {
956 // sub-string is too big, use last good position
957 if (first) {
958 // didn't find a good last position
959 substr = str.substr(0, pos);
960 this->mTextRows.push_back(substr);
961 str = str.substr(pos + 1);
962 break;
963 } else {
964 substr = str.substr(0, lastPos);
965 this->mTextRows.push_back(substr);
966 // If we stopped at a space then skip any extra spaces but stop at a newline
967 if (str[lastPos] != '\n') {
968 while (str[lastPos + 1] == ' ' || str[lastPos + 1] == '\t' || str[lastPos + 1] == '\n') {
969 ++lastPos;
970 if (str[lastPos] == '\n') {
971 break;
972 }
973 }
974 }
975 str = str.substr(lastPos + 1);
976 break;
977 }
978 } else {
979 // sub-string is small enough
980 // stop if we found a newline, otherwise look for next space
981 if (str[pos] == '\n') {
982 substr = str.substr(0, pos);
983 this->mTextRows.push_back(substr);
984 str = str.substr(pos + 1);
985 break;
986 }
987 }
988 } else {
989 // no space found
990 if (first) {
991 // didn't find a good last position, we're done
992 this->mTextRows.push_back(str);
993 done = true;
994 break;
995 } else {
996 substr = str.substr(0, lastPos);
997 this->mTextRows.push_back(substr);
998 str = str.substr(lastPos + 1);
999 break;
1000 }
1001 }
1002 lastPos = pos;
1003 first = false;
1004 }
1005 } else {
1006 // string small enough
1007 this->mTextRows.push_back(str);
1008 done = true;
1009 }
1010 }
1011 }
1012
1013
1014 /*----------------------------------------------------------------------------
1015 -- ScrollingWidget
1016 ----------------------------------------------------------------------------*/
1017
1018
1019 /**
1020 ** ScrollingWidget constructor.
1021 **
1022 ** @param width Width of the widget.
1023 ** @param height Height of the widget.
1024 */
1025 ScrollingWidget::ScrollingWidget(int width, int height) :
1026 gcn::ScrollArea(NULL, gcn::ScrollArea::SHOW_NEVER, gcn::ScrollArea::SHOW_NEVER),
1027 speedY(1.f), containerY(0.f), finished(false)
1028 {
1029 container.setDimension(gcn::Rectangle(0, 0, width, height));
1030 container.setOpaque(false);
1031 setContent(&container);
1032 setDimension(gcn::Rectangle(0, 0, width, height));
1033 }
1034
1035 /**
1036 ** Add a widget in the window.
1037 **
1038 ** @param widget Widget to add.
1039 ** @param x Position of the widget in the window.
1040 ** @param y Position of the widget in the window.
1041 */
1042 void ScrollingWidget::add(gcn::Widget *widget, int x, int y)
1043 {
1044 container.add(widget, x, y);
1045 if (x + widget->getWidth() > container.getWidth()) {
1046 container.setWidth(x + widget->getWidth());
1047 }
1048 if (y + widget->getHeight() > container.getHeight()) {
1049 container.setHeight(y + widget->getHeight());
1050 }
1051 }
1052
1053 /**
1054 ** Scrolling the content when possible.
1055 */
1056 void ScrollingWidget::logic()
1057 {
1058 setDirty(true);
1059 if (container.getHeight() + containerY - speedY > 0 ) {
1060 // the bottom of the container is lower than the top
1061 // of the widget. It is thus still visible.
1062 containerY -= speedY;
1063 container.setY((int)containerY);
1064 } else if (!finished) {
1065 finished = true;
1066 generateAction();
1067 }
1068 }
1069
1070 /**
1071 ** Restart animation to the beginning.
1072 */
1073 void ScrollingWidget::restart()
1074 {
1075 container.setY(0);
1076 containerY = 0.f;
1077 finished = (container.getHeight() == getHeight());
1078 }
1079
1080
1081 /*----------------------------------------------------------------------------
1082 -- Windows
1083 ----------------------------------------------------------------------------*/
1084
1085
1086 /**
1087 ** Windows constructor.
1088 **
1089 ** @param title Title of the window.
1090 ** @param width Width of the window.
1091 ** @param height Height of the window.
1092 */
1093 Windows::Windows(const std::string &title, int width, int height) :
1094 Window(title), blockwholewindow(true)
1095 {
1096 container.setDimension(gcn::Rectangle(0, 0, width, height));
1097 scroll.setDimension(gcn::Rectangle(0, 0, width, height));
1098 this->setContent(&scroll);
1099 scroll.setContent(&container);
1100 this->resizeToContent();
1101 }
1102
1103 /**
1104 ** Add a widget in the window.
1105 **
1106 ** @param widget Widget to add.
1107 ** @param x Position of the widget in the window.
1108 ** @param y Position of the widget in the window.
1109 */
1110 void Windows::add(gcn::Widget *widget, int x, int y)
1111 {
1112 container.add(widget, x, y);
1113 if (x + widget->getWidth() > container.getWidth()) {
1114 container.setWidth(x + widget->getWidth());
1115 }
1116 if (y + widget->getHeight() > container.getHeight()) {
1117 container.setHeight(y + widget->getHeight());
1118 }
1119 }
1120
1121 /**
1122 ** Move the window when it is dragged.
1123 **
1124 ** @param x X coordinate of the mouse relative to the window.
1125 ** @param y Y coordinate of the mouse relative to the widndow.
1126 **
1127 ** @note Once dragged, without release the mouse,
1128 ** if you go virtualy outside the container then go back,
1129 ** you have to wait the virtual cursor are in the container.
1130 ** It is because x, y argument refer to a virtual cursor :(
1131 ** @note An another thing is strange
1132 ** when the container is a "scrollable" ScrollArea with the cursor.
1133 ** The cursor can go outside the visual area.
1134 */
1135 void Windows::mouseMotion(int x, int y)
1136 {
1137 gcn::BasicContainer *bcontainer = getParent();
1138 int diffx;
1139 int diffy;
1140 int criticalx;
1141 int criticaly;
1142 int absx;
1143 int absy;
1144
1145 if (!mMouseDrag || !isMovable()) {
1146 return;
1147 }
1148
1149 diffx = x - mMouseXOffset;
1150 diffy = y - mMouseYOffset;
1151 if (blockwholewindow) {
1152 criticalx = getX();
1153 criticaly = getY();
1154 } else {
1155 criticalx = getX() + mMouseXOffset;
1156 criticaly = getY() + mMouseYOffset;
1157 }
1158
1159
1160 if (criticalx + diffx < 0) {
1161 diffx = -criticalx;
1162 }
1163 if (criticaly + diffy < 0) {
1164 diffy = -criticaly;
1165 }
1166
1167 if (blockwholewindow) {
1168 criticalx = getX() + getWidth();
1169 criticaly = getY() + getHeight();
1170 }
1171 if (criticalx + diffx >= bcontainer->getWidth()) {
1172 diffx = bcontainer->getWidth() - criticalx;
1173 }
1174 if (criticaly + diffy >= bcontainer->getHeight()) {
1175 diffy = bcontainer->getHeight() - criticaly;
1176 }
1177
1178 // Place the window.
1179 x = getX() + diffx;
1180 y = getY() + diffy;
1181 setPosition(x, y);
1182
1183 // Move the cursor.
1184 // Usefull only when window reachs the limit.
1185 getAbsolutePosition(absx, absy);
1186 CursorX = absx + mMouseXOffset;
1187 CursorY = absy + mMouseYOffset;
1188 }
1189
1190 /**
1191 ** Set background color of the window.
1192 **
1193 ** @param color Color to set.
1194 */
1195 void Windows::setBackgroundColor(const gcn::Color &color)
1196 {
1197 Window::setBackgroundColor(color);
1198 scroll.setBackgroundColor(color);
1199 }
1200
1201 /**
1202 ** Set base color of the windows.
1203 **
1204 ** @param color Color to set.
1205 */
1206 void Windows::setBaseColor(const gcn::Color &color)
1207 {
1208 Window::setBaseColor(color);
1209 container.setBaseColor(color);
1210 }
1211
1212
1213 /*----------------------------------------------------------------------------
1214 -- LuaListModel
1215 ----------------------------------------------------------------------------*/
1216
1217
1218 /**
1219 ** Set the list
1220 */
1221 void LuaListModel::setList(lua_State *lua, lua_Object *lo)
1222 {
1223 clear();
1224
1225 int args = lua_objlen(lua, *lo);
1226 for (int j = 0; j < args; ++j) {
1227 add(std::string(LuaToString(lua, *lo, j + 1)));
1228 }
1229 }
1230
1231
1232 /*----------------------------------------------------------------------------
1233 -- ListBoxWidget
1234 ----------------------------------------------------------------------------*/
1235
1236
1237 /**
1238 ** ListBoxWidget constructor.
1239 **
1240 ** @todo Size should be parametrable, maybe remove default constructor?
1241 */
1242 ListBoxWidget::ListBoxWidget(unsigned int width, unsigned int height)
1243 {
1244 setDimension(gcn::Rectangle(0, 0, width, height));
1245 setContent(&listbox);
1246 setBackgroundColor(gcn::Color(128, 128, 128));
1247 }
1248
1249
1250 /**
1251 ** Set the list
1252 */
1253 void ListBoxWidget::setList(lua_State *lua, lua_Object *lo)
1254 {
1255 lualistmodel.setList(lua, lo);
1256 listbox.setListModel(&lualistmodel);
1257 adjustSize();
1258 }
1259
1260 /**
1261 ** Sets the ListModel index of the selected element.
1262 **
1263 ** @param selected The ListModel index of the selected element.
1264 **
1265 ** @see gcn::ListBox
1266 */
1267 void ListBoxWidget::setSelected(int selected)
1268 {
1269 listbox.setSelected(selected);
1270 }
1271
1272 /**
1273 ** Gets the ListModel index of the selected element.
1274 **
1275 ** @return The ListModel index of the selected element.
1276 **
1277 ** @see gcn::ListBox
1278 */
1279 int ListBoxWidget::getSelected() const
1280 {
1281 return const_cast<gcn::ListBox &> (listbox).getSelected();
1282 }
1283
1284 /**
1285 ** Set background color of the ListBoxWidget.
1286 **
1287 ** @param color Color to set.
1288 */
1289 void ListBoxWidget::setBackgroundColor(const gcn::Color &color)
1290 {
1291 ScrollArea::setBackgroundColor(color);
1292 ScrollArea::setBaseColor(color);
1293 listbox.setBackgroundColor(color);
1294 }
1295
1296 /**
1297 ** Set font of the ListBox.
1298 **
1299 ** @param font Font to set.
1300 */
1301 void ListBoxWidget::setFont(gcn::Font *font)
1302 {
1303 listbox.setFont(font);
1304 listbox.setWidth(getWidth());
1305 adjustSize();
1306 }
1307
1308 /**
1309 ** Adjust size of the listBox.
1310 **
1311 ** @todo Fix width of the scroll area (depend of v-scroll or not).
1312 */
1313 void ListBoxWidget::adjustSize()
1314 {
1315 int i;
1316 int width;
1317 gcn::ListModel *listmodel;
1318
1319 width = listbox.getWidth();
1320 Assert(listbox.getListModel());
1321 listmodel = listbox.getListModel();
1322 for (i = 0; i < listmodel->getNumberOfElements(); ++i) {
1323 if (width < listbox.getFont()->getWidth(listmodel->getElementAt(i))) {
1324 width = listbox.getFont()->getWidth(listmodel->getElementAt(i));
1325 }
1326 }
1327 if (width != listbox.getWidth()) {
1328 listbox.setWidth(width);
1329 }
1330 }
1331
1332 /**
1333 ** Add an action listener
1334 */
1335 void ListBoxWidget::addActionListener(gcn::ActionListener *actionListener)
1336 {
1337 listbox.addActionListener(actionListener);
1338 }
1339
1340 /**
1341 ** Get dirty
1342 */
1343 bool ListBoxWidget::getDirty() const
1344 {
1345 return listbox.getDirty();
1346 }
1347
1348
1349 /*----------------------------------------------------------------------------
1350 -- DropDownWidget
1351 ----------------------------------------------------------------------------*/
1352
1353
1354 /**
1355 ** Set the list
1356 */
1357 void DropDownWidget::setList(lua_State *lua, lua_Object *lo)
1358 {
1359 listmodel.setList(lua, lo);
1360 setListModel(&listmodel);
1361 }
1362
1363 /**
1364 ** Set the drop down size
1365 */
1366 void DropDownWidget::setSize(int width, int height)
1367 {
1368 DropDown::setSize(width, height);
1369 this->getListBox()->setSize(width, height);
1370 }
1371
1372 /*----------------------------------------------------------------------------
1373 -- DropDownWidget
1374 ----------------------------------------------------------------------------*/
1375
1376 /**
1377 ** StatBoxWidget constructor
1378 **
1379 ** @param width Width of the StatBoxWidget.
1380 ** @param height Height of the StatBoxWidget.
1381 */
1382 StatBoxWidget::StatBoxWidget(int width, int height) : percent(100)
1383 {
1384 setWidth(width);
1385 setHeight(height);
1386
1387 setBackgroundColor(gcn::Color(0, 0, 0));
1388 setBaseColor(gcn::Color(255, 255, 255));
1389 setForegroundColor(gcn::Color(128, 128, 128));
1390 }
1391
1392 /**
1393 ** Draw StatBoxWidget.
1394 **
1395 ** @param graphics Graphic driver used to draw.
1396 **
1397 ** @todo caption seem to be placed upper than the middle.
1398 ** @todo set direction (hor./vert.) and growing direction(up/down, left/rigth).
1399 */
1400 void StatBoxWidget::draw(gcn::Graphics *graphics)
1401 {
1402 int width;
1403 int height;
1404
1405 width = getWidth();
1406 height = getHeight();
1407
1408 graphics->setColor(getBackgroundColor());
1409 graphics->fillRectangle(gcn::Rectangle(0, 0, width, height));
1410
1411 graphics->setColor(getBaseColor());
1412 graphics->drawRectangle(gcn::Rectangle(1, 1, width - 2, height - 2));
1413
1414 graphics->setColor(getForegroundColor());
1415 width = percent * width / 100;
1416 graphics->fillRectangle(gcn::Rectangle(2, 2, width - 4, height - 4));
1417 graphics->setFont(getFont());
1418 graphics->drawText(getCaption(),
1419 (getWidth() - getFont()->getWidth(getCaption())) / 2,
1420 (height - getFont()->getHeight()) / 2);
1421 }
1422
1423 /**
1424 ** Set caption of StatBoxWidget.
1425 **
1426 ** @param caption New value.
1427 */
1428 void StatBoxWidget::setCaption(const std::string &caption)
1429 {
1430 this->caption = caption;
1431 this->setDirty(true);
1432 }
1433
1434 /**
1435 ** Get caption of StatBoxWidget.
1436 */
1437
1438 const std::string &StatBoxWidget::getCaption() const
1439 {
1440 return caption;
1441 }
1442
1443 /**
1444 ** Set percent of StatBoxWidget.
1445 **
1446 ** @param percent New value.
1447 */
1448 void StatBoxWidget::setPercent(const int percent)
1449 {
1450 this->setDirty(true);
1451 this->percent = percent;
1452 }
1453
1454 /**
1455 ** Get percent of StatBoxWidget.
1456 */
1457 int StatBoxWidget::getPercent() const
1458 {
1459 return percent;
1460 }
1461
1462
1463 /*----------------------------------------------------------------------------
1464 -- MenuScreen
1465 ----------------------------------------------------------------------------*/
1466
1467
1468 /**
1469 ** MenuScreen constructor
1470 */
1471 MenuScreen::MenuScreen() :
1472 Container(), runLoop(true), logiclistener(0), drawUnder(false)
1473 {
1474 setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height));
1475 setOpaque(false);
1476
1477 // The gui must be set immediately as it is used by widgets
1478 // when they are added to the container
1479 oldtop = Gui->getTop();
1480 Gui->setTop(this);
1481 }
1482
1483 /**
1484 ** Run the menu. Loops until stop is called.
1485 */
1486 int MenuScreen::run(bool loop)
1487 {
1488 this->loopResult = 0;
1489 this->runLoop = loop;
1490
1491 CursorState = CursorStatePoint;
1492 GameCursor = UI.Point.Cursor;
1493 CursorOn = CursorOnUnknown;
1494
1495 if (loop) {
1496 const EventCallback *old_callbacks = GetCallbacks();
1497 SetCallbacks(&GuichanCallbacks);
1498 while (runLoop) {
1499 UpdateDisplay();
1500 RealizeVideoMemory();
1501 WaitEventsOneFrame();
1502 }
1503 SetCallbacks(old_callbacks);
1504 Gui->setTop(this->oldtop);
1505 } else {
1506 SetCallbacks(&GuichanCallbacks);
1507 MenuStack.push(this);
1508 }
1509
1510 return this->loopResult;
1511 }
1512
1513 /**
1514 ** Stop the menu from running
1515 */
1516 void MenuScreen::stop(int result, bool stopAll)
1517 {
1518 if (!this->runLoop) {
1519 Gui->setTop(this->oldtop);
1520 Assert(MenuStack.top() == this);
1521 MenuStack.pop();
1522 if (stopAll) {
1523 while (!MenuStack.empty()) {
1524 MenuStack.pop();
1525 }
1526 }
1527 if (MenuStack.empty()) {
1528 //InterfaceState = IfaceStateNormal;
1529 if (!Editor.Running) {
1530 SetCallbacks(&GameCallbacks);
1531 } else {
1532 SetCallbacks(&EditorCallbacks);
1533 }
1534 GamePaused = false;
1535 UI.StatusLine.Clear();
1536 if (GameRunning) {
1537 UIHandleMouseMove(CursorX, CursorY);
1538 }
1539 }
1540 }
1541
1542 this->runLoop = false;
1543 this->loopResult = result;
1544 }
1545
1546 void MenuScreen::addLogicCallback(LuaActionListener *listener)
1547 {
1548 logiclistener = listener;
1549 }
1550
1551 void MenuScreen::draw(gcn::Graphics *graphics)
1552 {
1553 if (this->drawUnder) {
1554 gcn::Widget *w = Gui->getTop();
1555 gcn::Widget *f = w->_getFocusHandler()->getFocused();
1556 Gui->setTop(oldtop);
1557 gcn::Rectangle r = Gui->getGraphics()->getCurrentClipArea();
1558 Gui->getGraphics()->popClipArea();
1559 Gui->draw();
1560 Gui->setTop(w);
1561 w->_getFocusHandler()->setFocus(f);
1562 Gui->getGraphics()->pushClipArea(r);
1563 }
1564 gcn::Container::draw(graphics);
1565 }
1566
1567 void MenuScreen::logic()
1568 {
1569 if (NetConnectRunning == 2) {
1570 NetworkProcessClientRequest();
1571 }
1572 if (NetConnectRunning == 1) {
1573 NetworkProcessServerRequest();
1574 }
1575 if (logiclistener) {
1576 logiclistener->action("");
1577 }
1578 Container::logic();
1579 }
1580
1581 //@}