Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/gui/gl/glgui_widget.cc @ 10225

Last change on this file since 10225 was 10225, checked in by patrick, 17 years ago

texture problem solved, now all backgrounds are shown

File size: 23.8 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI
17
18#include "glgui_widget.h"
19
20#include "glgui_cursor.h"
21
22#include "material.h"
23
24#include "debug.h"
25
26#include "loading/load_param.h"
27#include "loading/resource_manager.h"
28
29namespace OrxGui
30{
31  ObjectListDefinition(GLGuiWidget);
32  /**
33   * @brief standard constructor
34  */
35  GLGuiWidget::GLGuiWidget (GLGuiWidget* parent)
36  {
37    this->init();
38
39    this->setParentWidget(parent);
40  }
41
42
43  /**
44   * @brief loads Parameters for a Style from XML
45   * @param root the XML-Element to load from.
46   */
47  void GLGuiWidget::loadParams(const TiXmlElement* root)
48  {
49
50    /// STYLE
51    LoadParam(root, "border-left", this, GLGuiWidget, setBorderLeft);
52    LoadParam(root, "border-right", this, GLGuiWidget, setBorderRight);
53    LoadParam(root, "border-top", this, GLGuiWidget, setBorderTop);
54    LoadParam(root, "border-bottom", this, GLGuiWidget, setBorderBottom);
55
56    LoadParam(root, "text-size", this, GLGuiWidget, setTextSize);
57    LoadParam(root, "background-color", this, GLGuiWidget, setBackgroundColorS);
58    LoadParam(root, "foreground-color", this, GLGuiWidget, setForegroundColorS);
59
60    //    LoadParamXML(root, "backmat", this, GLGuiWidget, loadBackgroundMaterial);
61    //    LoadParamXML(root, "frontmat", this, GLGuiWidget, loadForegroundMaterial);
62
63    LoadParam(root, "feature-position", this, GLGuiWidget, setFeaturePositionS);
64    LoadParam(root, "Font", this, GLGuiWidget, setFont);
65
66    LoadParam(root, "animated-state-changes", this, GLGuiWidget, setAnimatedStateChanges);
67  }
68
69
70  /**
71   * @brief standard deconstructor
72   */
73  GLGuiWidget::~GLGuiWidget()
74  {
75    if (this == GLGuiWidget::_mouseFocused)
76      GLGuiWidget::_mouseFocused = NULL;
77    if (this == GLGuiWidget::selected())
78      this->unselect();
79
80    if (this->_parent != NULL)
81      this->_parent->removeChildWidget(this);
82  }
83
84  GLGuiWidget* GLGuiWidget::_selected = NULL;
85  GLGuiWidget* GLGuiWidget::_mouseFocused = NULL;
86  GLGuiWidget* GLGuiWidget::_inputGrabber = NULL;
87  Font* GLGuiWidget::_defaultFont = NULL;
88
89
90  /**
91   * initializes the GUI-element
92   */
93  void GLGuiWidget::init()
94  {
95    this->registerObject(this, GLGuiWidget::_objectList);
96
97    this->_focusable = false;
98    this->_clickable = false;
99    this->_selectable = false;
100    this->_pushed = false;
101    this->_state = OrxGui::Normal;
102
103    if(GLGuiWidget::_defaultFont == NULL)
104      GLGuiWidget::_defaultFont = new Font(Resources::ResourceManager::getInstance()->mainGlobalPath().name() + "/fonts/final_frontier.ttf", 20);
105
106    this->_font = *GLGuiWidget::_defaultFont;
107    this->resetStyle();
108
109    this->_animating = false;
110    this->_animationCycle = 0.0;
111    this->_animationDuration = 1.0;
112
113
114    this->setBackgroundColor(Color(0, 0, 0, 0.0));
115    this->setBackgroundColor(Color(0, 0, 0, 0), OrxGui::Selected);
116
117
118    this->_style[0]._background.setBlendFunc( GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
119    this->_style[1]._background.setBlendFunc( GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
120    this->_style[2]._background.setBlendFunc( GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
121    this->_style[3]._background.setBlendFunc( GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
122
123    this->setForegroundColor(Color(.8, .8, 1, 1), OrxGui::Normal);
124    this->setForegroundColor(Color(0, .4, 1.0, 1), OrxGui::Selected);
125    this->setForegroundColor(Color(0, .0, 1.0, 1), OrxGui::Focused);
126    this->setForegroundColor(Color(.1, .1, .1, 1), OrxGui::Insensitive);
127
128    this->setVisibility(GLGUI_WIDGET_DEFAULT_VISIBLE);
129
130    this->setBorderLeft(15);
131    //this->setBackgroundTexture("pogo_bunny.png");
132
133    this->switchState(_state);
134    this->_currentStyle = this->_style[_state];
135  }
136
137
138  void GLGuiWidget::setParentWidget(GLGuiWidget* parent)
139  {
140    this->_parent = parent;
141
142    if (parent != NULL)
143      parent->addChild2D(this);
144  }
145
146
147  void GLGuiWidget::setFrontColor(const Color& frontColor, bool instantaniously)
148  {
149    this->_currentStyle._foreground.setDiffuseColor(frontColor);
150    this->animateBack();
151  };
152
153
154  bool GLGuiWidget::focusOverWidget(const Vector2D& position) const
155  {
156    return (this->getAbsCoor2D().x < position.x && this->getAbsCoor2D().x + this->getSizeX2D() > position.x &&
157        this->getAbsCoor2D().y < position.y && this->getAbsCoor2D().y + this->getSizeY2D() > position.y);
158  }
159
160  bool GLGuiWidget::focusOverWidget(const GLGuiCursor* const cursor) const
161  {
162    return this->focusOverWidget(cursor->getAbsCoor2D());
163  }
164
165
166
167  /** @brief gives focus to this widget */
168  void GLGuiWidget::giveMouseFocus()
169  {
170    if (this->_state == OrxGui::Insensitive)
171      return ;
172
173    if (GLGuiWidget::mouseFocused() != NULL)
174      GLGuiWidget::mouseFocused()->breakMouseFocus();
175    GLGuiWidget::_mouseFocused = this;
176
177    this->switchState(OrxGui::Focused);
178
179    this->receivedFocus();
180  };
181
182  void GLGuiWidget::breakMouseFocus()
183  {
184    if (GLGuiWidget::_mouseFocused == this)
185    {
186      GLGuiWidget::_mouseFocused = NULL;
187
188      if (GLGuiWidget::_selected != this)
189        this->switchState(OrxGui::Normal);
190      else
191        this->switchState(OrxGui::Selected);
192
193      this->removedFocus();
194    }
195  };
196
197  /**
198   * @brief selects the Widget, unselecting the old one (if existing)
199   */
200  void GLGuiWidget::select()
201  {
202    if (GLGuiWidget::_selected != NULL)
203      GLGuiWidget::selected()->unselect();
204    GLGuiWidget::_selected = this;
205
206    this->switchState(OrxGui::Selected);
207  }
208
209  /**
210   * @brief unselects the current Widget.
211   *
212   * if the current Widget is not selected, nothing is done here.
213   */
214  void GLGuiWidget::unselect()
215  {
216    if (GLGuiWidget::_selected != this)
217      return;
218
219    if (GLGuiWidget::_mouseFocused == this)
220      this->switchState(OrxGui::Focused);
221    else
222      this->switchState(OrxGui::Normal);
223
224    GLGuiWidget::_selected = NULL;
225  }
226
227
228  void GLGuiWidget::resize()
229  {
230    this->backRect().setSize(this->getSize2D());
231    if (this->parent() != NULL)
232      this->parent()->resize();
233  }
234
235
236  void GLGuiWidget::click(const Vector2D& pos)
237  {
238    assert (!this->_pushed);
239    this->_pushed = true;
240
241    this->clicking(pos);
242  }
243
244  void GLGuiWidget::release(const Vector2D& pos)
245  {
246    if (this->_pushed)
247    {
248      this->releasing(pos, GLGuiWidget::_mouseFocused == this);
249      this->_pushed = false;
250    }
251  }
252
253
254  void GLGuiWidget::clicking(const Vector2D& pos)
255  {}
256
257  void GLGuiWidget::releasing(const Vector2D& pos, bool focused)
258  {}
259
260  void GLGuiWidget::receivedFocus()
261  {
262  }
263
264  void GLGuiWidget::removedFocus()
265  {
266
267  }
268
269  void GLGuiWidget::selecting()
270  {
271  }
272
273  void GLGuiWidget::unselecting()
274  {
275  }
276
277
278  void GLGuiWidget::destroying()
279  {
280  }
281
282
283  void GLGuiWidget::setWidgetSize(const Vector2D& size)
284  {
285    this->setSize2D(size);
286    this->resize();
287
288  }
289
290
291  void GLGuiWidget::setWidgetSize(float x, float y)
292  {
293    this->setWidgetSize(Vector2D(x, y));
294  }
295
296  void GLGuiWidget::show()
297  {
298    this->setVisibility(true);
299    this->showing();
300  }
301
302
303
304  void GLGuiWidget::hide()
305  {
306    this->setVisibility(false);
307    this->hiding();
308  }
309
310
311  /**
312   * @brief resets the Style to the default Settings.
313   */
314  void GLGuiWidget::resetStyle()
315  {
316    this->setBorderLeft(1.0);
317    this->setBorderRight(1.0);
318    this->setBorderTop(1.0);
319    this->setBorderBottom(1.0);
320
321    this->setTextSize(20.0);
322    this->setBackgroundColor(1.0);
323    this->setForegroundColor(1.0);
324
325    this->setFeaturePosition(FeatureLeft);
326
327    this->setAnimatedStateChanges(true);
328  }
329
330
331  /**
332   * @brief sets the Width of the left border for all States
333   * @param value the borderWidth
334   */
335  void GLGuiWidget::setBorderLeft(float value)
336  {
337    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
338      setBorderLeft(value, (OrxGui::State)i);
339  }
340
341  /**
342   * @brief sets the Width of the left border.
343   * @param value the borderWidth
344   * @param state the State to set the borderwidth to
345   */
346  void GLGuiWidget::setBorderLeft(float value, OrxGui::State state)
347  {
348    _style[state]._borderLeft = value;
349    if (state == _state)
350      _currentStyle._borderLeft = value;
351  }
352
353  /**
354   * @brief sets the Width of the left border.
355   * @param value the borderWidth
356   * @param stateName the State to set the borderwidth to
357   */
358  void GLGuiWidget::setBorderLeftS(float value, const std::string& stateName)
359  {
360    OrxGui::State state;
361    if (getState(stateName, &state))
362      this->setBorderLeft(value, state);
363    else
364      this->setBorderLeft(value);
365  }
366
367  /**
368   * @brief sets the Width of the right border for all states.
369   * @param value the borderWidth
370   */
371  void GLGuiWidget::setBorderRight(float value)
372  {
373    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
374      setBorderRight(value, (OrxGui::State)i);
375  }
376
377  /**
378   * @brief sets the Width of the right border.
379   * @param value the borderWidth
380   * @param state the State to setup.
381   */
382  void GLGuiWidget::setBorderRight(float value, OrxGui::State state)
383  {
384    _style[state]._borderRight = value;
385    if (state == _state)
386      _currentStyle._borderRight = value;
387  }
388
389  /**
390   * @brief sets the Width of the right border.
391   * @param value the borderWidth
392   * @param stateName the State to setup.
393   */
394  void GLGuiWidget::setBorderRightS(float value, const std::string& stateName)
395  {
396    OrxGui::State state;
397    if (getState(stateName, &state))
398      this->setBorderRight(value, state);
399    else
400      this->setBorderRight(value);
401  }
402
403
404  /**
405   * @brief sets the Width of the top border for all states.
406   * @param value the borderWidth
407   */
408  void GLGuiWidget::setBorderTop(float value)
409  {
410    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
411      setBorderTop(value, (OrxGui::State)i);
412  }
413
414  /**
415   * @brief sets the Width of the top border.
416   * @param value the borderWidth
417   * @param state the State to setup.
418   */
419  void GLGuiWidget::setBorderTop(float value, OrxGui::State state)
420  {
421    _style[state]._borderTop = value;
422    if (state == _state)
423      _currentStyle._borderTop = value;
424  }
425
426  /**
427   * @brief sets the Width of the top border.
428   * @param value the borderWidth
429   * @param stateName the State to setup.
430   */
431  void GLGuiWidget::setBorderTopS(float value, const std::string& stateName)
432  {
433    OrxGui::State state;
434    if (getState(stateName, &state))
435      this->setBorderTop(value, state);
436    else
437      this->setBorderTop(value);
438  }
439
440
441  /**
442   * @brief sets the Width of the bottom border for all states.
443   * @param value the borderWidth
444   */
445  void GLGuiWidget::setBorderBottom(float value)
446  {
447    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
448      setBorderBottom(value, (OrxGui::State)i);
449  }
450
451  /**
452   * @brief sets the Width of the bottom border.
453   * @param value the borderWidth
454   * @param state the State to setup.
455   */
456  void GLGuiWidget::setBorderBottom(float value, OrxGui::State state)
457  {
458    _style[state]._borderBottom = value;
459    if (state == _state)
460      _currentStyle._borderBottom = value;
461
462  }
463
464  /**
465   * @brief sets the Width of the bottom border for all states.
466   * @param value the borderWidth
467   * @param stateName the State to setup.
468   */
469  void GLGuiWidget::setBorderBottomS(float value, const std::string& stateName)
470  {
471    OrxGui::State state;
472    if (getState(stateName, &state))
473      this->setBorderBottom(value, state);
474    else
475      this->setBorderBottom(value);
476  }
477
478
479  /**
480   * @brief sets the TextSize for all states.
481   * @param value the TextSize
482   */
483  void GLGuiWidget::setTextSize(float value)
484  {
485    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
486      setTextSize(value, (OrxGui::State)i);
487  }
488
489  /**
490   * @brief sets the TextSize.
491   * @param value the TextSize.
492   * @param state: the State to setup
493   */
494  void GLGuiWidget::setTextSize(float value, OrxGui::State state)
495  {
496    _style[state]._textSize = value;
497    if (state == _state)
498      _currentStyle._textSize = value;
499  }
500
501  /**
502   * @brief sets the TextSize.
503   * @param value the TextSize.
504   * @param stateName: the State to setup
505   */
506  void GLGuiWidget::setTextSizeS(float value, const std::string& stateName)
507  {
508    OrxGui::State state;
509    if (getState(stateName, &state))
510      this->setTextSize(value, state);
511    else
512      this->setTextSize(value);
513  }
514
515
516  /**
517   * @brief sets the Background Color for all States.
518   * @param color the Color.
519   */
520  void GLGuiWidget::setBackgroundColor(const Color& color)
521  {
522    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
523      setBackgroundColor(color, (OrxGui::State)i);
524  }
525
526  /**
527   * @brief sets the Background Color.
528   * @param color the Color.
529   * @param state: the State to setup
530   */
531  void GLGuiWidget::setBackgroundColor(const Color& color, OrxGui::State state)
532  {
533    _style[state]._background.setDiffuseColor(color);
534    if (state == _state)
535      _currentStyle._background.setDiffuseColor(color);
536
537  }
538
539  /**
540   * @brief sets the Background Color.
541   * @param r the Color's red part.
542   * @param g the Color's green part.
543   * @param b the Color's blue part.
544   * @param a the Color's alpha part.
545   * @param stateName: the State to setup
546   */
547  void GLGuiWidget::setBackgroundColorS(float r, float g, float b, float a, const std::string& stateName)
548  {
549    OrxGui::State state;
550    if (getState(stateName, &state))
551      this->setBackgroundColor(Color(r,g,b,a), state);
552    else
553      this->setBackgroundColor(Color(r,g,b,a));
554  }
555
556
557  /**
558   * @brief sets the Background Texture for all States.
559   * @param texture the Texture.
560   */
561  void GLGuiWidget::setBackgroundTexture(const Texture& texture)
562  {
563    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
564      setBackgroundTexture(texture, (OrxGui::State)i);
565  }
566
567  /**
568  * @brief sets the Background Texture to all States.
569  * @param textureName the Texture's fileName.
570   */
571  void GLGuiWidget::setBackgroundTexture(const std::string& textureName)
572  {
573    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i) {
574      _style[i]._background.setDiffuseMap(textureName);
575      _style[i]._background.setDiffuseColor(Color(1.0, 1.0, 1.0, 1.0));
576    }
577    this->_currentStyle._background.setDiffuseMap(textureName);
578    this->_currentStyle._background.setDiffuseColor(Color(1.0, 1.0, 1.0, 1.0));
579  }
580
581  /**
582   * @brief sets the Background Texture.
583   * @param texture the Texture.
584   * @param state the State to setup.
585   */
586  void GLGuiWidget::setBackgroundTexture(const Texture& texture, OrxGui::State state)
587  {
588    _style[state]._background.setDiffuseMap(texture);
589    _style[state]._background.setDiffuseColor(Color(1.0, 1.0, 1.0, 1.0));
590
591    if (state == _state) {
592      _currentStyle._background.setDiffuseMap(texture);
593      _currentStyle._background.setDiffuseColor(Color(1.0, 1.0, 1.0, 1.0));
594    }
595  }
596
597
598
599  /**
600   * @brief sets the Background Texture.
601   * @param texture the Texture.
602   * @param stateName the State to setup.
603   */
604  void GLGuiWidget::setBackgroundTexture(const std::string& textureName, const std::string& stateName)
605  {
606    OrxGui::State state;
607    if (getState(stateName, &state))
608      ; /// FIXME this->setBackgroundTexture(textureName, state);
609    else
610      ; ///    this->setBackgroundTexture(textureName);
611  }
612
613
614  /**
615   * @brief sets the Foreground Color for all States.
616   * @param color the Color.
617   */
618  void GLGuiWidget::setForegroundColor(const Color& color)
619  {
620    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
621      setForegroundColor(color, (OrxGui::State)i);
622  }
623
624  /**
625   * @brief sets the Foreground Color.
626   * @param color the Color.
627   * @param state the State to setup
628   */
629  void GLGuiWidget::setForegroundColor(const Color& color, OrxGui::State state)
630  {
631    _style[state]._foreground.setDiffuseColor(color);
632    if (state == _state)
633      _currentStyle._foreground.setDiffuseColor(color);
634
635  }
636
637  /**
638   * @brief sets the Foreground Color.
639   * @param r the Color's red part.
640   * @param g the Color's green part.
641   * @param b the Color's blue part.
642   * @param a the Color's alpha part.
643   * @param stateName: the State to setup
644   */
645  void GLGuiWidget::setForegroundColorS(float r, float g, float b, float a, const std::string& stateName)
646  {
647    OrxGui::State state;
648    if (getState(stateName, &state))
649      this->setForegroundColor(Color(r,g,b,a), state);
650    else
651      this->setForegroundColor(Color(r,g,b,a));
652  }
653
654
655    /**
656   * @brief sets the Foreground Texture for all States.
657   * @param texture the Texture.
658     */
659  void GLGuiWidget::setForegroundTexture(const Texture& texture)
660  {
661    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
662      setForegroundTexture(texture, (OrxGui::State)i);
663  }
664
665  /**
666   * @brief sets the Foreground Texture to all States.
667   * @param textureName the Texture's fileName.
668   */
669  void GLGuiWidget::setForegroundTexture(const std::string& textureName)
670  {
671    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
672      _style[i]._foreground.setDiffuseMap(textureName);
673    this->_currentStyle._foreground.setDiffuseMap(textureName);
674  }
675
676  /**
677   * @brief sets the Foreground Texture.
678   * @param texture the Texture.
679   * @param state the State to setup.
680   */
681  void GLGuiWidget::setForegroundTexture(const Texture& texture, OrxGui::State state)
682  {
683    _style[state]._background.setDiffuseMap(texture);
684    if (state == _state)
685      _currentStyle._background.setDiffuseMap(texture);
686  }
687
688
689
690  /**
691   * @brief sets the Foreground Texture.
692   * @param texture the Texture.
693   * @param stateName the State to setup.
694   */
695  void GLGuiWidget::setForegroundTexture(const std::string& textureName, const std::string& stateName)
696  {
697    OrxGui::State state;
698    if (getState(stateName, &state))
699      ; /// FIXME this->setForegroundTexture(textureName, state);
700    else
701      ; ///    this->setForegroundTexture(textureName);
702  }
703
704
705  void GLGuiWidget::loadBackgroundMaterial(const Material& material)
706  {
707    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
708      this->loadForegroundMaterial(material, (OrxGui::State)i);
709  }
710
711  void GLGuiWidget::loadBackgroundMaterial(const Material& material, OrxGui::State state)
712  {
713    this->_style[state]._background = material;
714    if (state == _state)
715      _currentStyle._background = material;
716
717  }
718
719  void GLGuiWidget::loadBackgroundMaterial(const TiXmlElement* element)
720  {
721    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
722      this->loadBackgroundMaterial(element, (OrxGui::State)i);
723  }
724
725  void GLGuiWidget::loadBackgroundMaterial(const TiXmlElement* element, OrxGui::State state)
726  {
727    this->_style[state]._background.loadParams(element);
728    if (state == _state)
729      this->_currentStyle._background = _style[state]._background;
730  }
731
732  void GLGuiWidget::loadBackgroundMaterialS(const TiXmlElement* element, const std::string& stateName)
733  {
734    OrxGui::State state;
735    if (getState(stateName, &state))
736      this->loadBackgroundMaterial(element, state);
737    else
738      this->loadBackgroundMaterial(element);
739  }
740
741  void GLGuiWidget::loadForegroundMaterial(const Material& material)
742{}
743  void GLGuiWidget::loadForegroundMaterial(const Material& material, OrxGui::State state)
744  {}
745  void GLGuiWidget::loadForegroundMaterial(const TiXmlElement* element, OrxGui::State state)
746  {}
747  void GLGuiWidget::loadForegroundMaterialS(const TiXmlElement* element, const std::string& stateName)
748  {}
749
750
751  /**
752   * @brief sets the Feature-Position.
753   * @param featurePosition the Feature-Position.
754   */
755  void GLGuiWidget::setFeaturePosition(FeaturePosition featurePosition)
756  {
757    this->_featurePosition = featurePosition;
758  }
759
760  /**
761   * @brief sets the Feature-Position by converting from a String.
762   * @param featurePosition the Feature-Position.
763   */
764  void GLGuiWidget::setFeaturePositionS(const std::string& featurePosition)
765  {
766    for (unsigned int i = 0; i < 4; ++i)
767    {
768      if (featurePosition == FeaturePositionString[i])
769      {
770        this->setFeaturePosition((FeaturePosition)i);
771      }
772    }
773  }
774
775  /**
776   * @brief sets the Font.
777   * @param font the Font.
778   */
779  void GLGuiWidget::setFont(const Font& font)
780  {
781    this->_font = font;
782  }
783
784  /**
785   * @brief sets the font from a Font-Name.
786   * @param fontName the FileName of the Font.
787   */
788  void GLGuiWidget::setFont(const std::string& fontName, unsigned int renderSize)
789  {
790    this->setFont(Font(fontName, renderSize));
791  }
792
793  /**
794   * @brief sets the AnimatedState.
795   * @param animated: it states-changes should animate true, otherwise false.
796   */
797  void GLGuiWidget::setAnimatedStateChanges(bool animated)
798  {
799    this->_animatedStateChanges = animated;
800  }
801
802
803  void GLGuiWidget::switchState(OrxGui::State state)
804  {
805    //this->_currentStyle = this->_style[state];
806    this->_state = state;
807    PRINTF(4)("%s::%s Switches to state %s\n", this->getClassCName(), this->getCName(), OrxGui::StateString[state].c_str());
808
809    this->animateBack();
810  }
811
812
813  void GLGuiWidget::animateBack()
814  {
815    this->_animating = true;
816    this->_animationCycle = 0.0f;
817  }
818
819
820  void GLGuiWidget::tick(float dt)
821  {
822    if (this->_animating)
823    {
824      this->foregroundColor();
825
826      _animationCycle += dt / _animationDuration;
827      if (_animationCycle >= 1.0)
828      {
829        _currentStyle._foreground.diffuseColor() = this->foregroundColor(_state);
830        _currentStyle._background.diffuseColor() = this->backgroundColor(_state);
831        _animating = false;
832      }
833      else
834      {
835        _currentStyle._foreground.diffuseColor().slerp(this->foregroundColor(_state), _animationCycle);
836        _currentStyle._background.diffuseColor().slerp(this->backgroundColor(_state), _animationCycle);
837      }
838      this->updateFrontColor();
839    }
840  }
841
842
843  /**
844   * USE THIS FUNCTION ONLY FROM DERIVED CLASS
845   */
846  void GLGuiWidget::draw() const
847  {
848    this->background().select();
849    this->drawRect(this->backRect());
850    this->background().unselect();
851  }
852
853
854  /**
855   * @param stateName the Name of a State.
856   * @param state the found State is returned here if found.
857   * @returns true if String is found, false otherwise.
858   */
859  bool GLGuiWidget::getState(const std::string& stateName, OrxGui::State* state)
860  {
861    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
862      if (stateName == OrxGui::StateString[i])
863      {
864        *state = (OrxGui::State)i;
865        return true;
866      }
867    return false;
868  }
869
870  /**
871   * @brief print out some nice debug output about the Widget.
872   */
873  void GLGuiWidget::debug(unsigned int level) const
874  {
875    PRINT(0)("Debug of %s::%s - WidgetPart ", this->getClassCName(), this->getCName());
876    if (_parent != NULL)
877      PRINT(0)("- Parent %s::%s ", _parent->getClassCName(), _parent->getCName());
878    PRINT(0)("- State: %s", StateString[_state].c_str());
879
880    if (_focusable)
881      PRINT(0)("- focusable ");
882    if (_clickable)
883      PRINT(0)("- Clickable ");
884    if (_pushed)
885      PRINT(0)("- Pushed ");
886    PRINT(0)("\n");
887
888
889    PRINT(0)("Minimum Size %0.2f %0.2f ", _minSize.x, _minSize.y);
890    PRINT(0)("Back Rect: ");
891    _backRect.debug();
892    PRINT(0)("Style:\n");
893
894    for (unsigned int i = 0; i < GLGUI_STATE_COUNT; ++i)
895    {
896      PRINT(0)("In State %s: \n", StateString[i].c_str());
897
898      PRINT(0)("  Borders: Left: %0.2f, Right: %0.2f, Top: %0.2f, Bottom %0.2f\n",
899               _style[i]._borderLeft, _style[i]._borderRight, _style[i]._borderTop, _style[i]._borderBottom);
900      PRINT(0)("  TextSize %0.2f\n", _style[i]._textSize);
901      PRINT(0)("  BackgroundColor: "); _style[i]._background.diffuseColor().debug();
902      PRINT(0)("  ForegroundColor: "); _style[i]._foreground.diffuseColor().debug();
903      PRINT(0)("\n");
904    }
905
906
907    PRINT(0)(" Feature at %s ", FeaturePositionString[_featurePosition].c_str());
908    /// TODO    PRINT(0)("");    Font*               _font;                 //!< The Font used in the current Widget.
909
910    if (_animatedStateChanges)
911      PRINT(0)("- AnimatedStateChanges");
912    PRINT(0)("\n");
913
914    /*
915    if (_animating)
916      PRINT(0)("- Animated ");
917
918    //
919    float               _animationCycle;
920    float               _animationDuration;
921    StatedStyle         _currentStyle;
922    OrxGui::State       _currentState;
923    */
924  }
925
926
927}
Note: See TracBrowser for help on using the repository browser.