CGUIToolBar.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CGUIToolBar.h"
  5. #ifdef _IRR_COMPILE_WITH_GUI_
  6. #include "IGUISkin.h"
  7. #include "IGUIEnvironment.h"
  8. #include "IVideoDriver.h"
  9. #include "IGUIButton.h"
  10. #include "IGUIFont.h"
  11. #include "CGUIButton.h"
  12. namespace irr
  13. {
  14. namespace gui
  15. {
  16. //! constructor
  17. CGUIToolBar::CGUIToolBar(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
  18. :IGUIToolBar(environment, parent, id, rectangle), ButtonX(5)
  19. {
  20. #ifdef _DEBUG
  21. setDebugName("CGUIToolBar");
  22. #endif
  23. // calculate position and find other menubars
  24. s32 y = 0;
  25. s32 parentwidth = 100;
  26. if (parent)
  27. {
  28. parentwidth = Parent->getAbsolutePosition().getWidth();
  29. const core::list<IGUIElement*>& children = parent->getChildren();
  30. core::list<IGUIElement*>::ConstIterator it = children.begin();
  31. for (; it != children.end(); ++it)
  32. {
  33. core::rect<s32> r = (*it)->getAbsolutePosition();
  34. if (r.UpperLeftCorner.X == 0 && r.UpperLeftCorner.Y <= y &&
  35. r.LowerRightCorner.X == parentwidth)
  36. y = r.LowerRightCorner.Y;
  37. }
  38. }
  39. core::rect<s32> rr;
  40. rr.UpperLeftCorner.X = 0;
  41. rr.UpperLeftCorner.Y = y;
  42. s32 height = Environment->getSkin()->getSize ( EGDS_MENU_HEIGHT );
  43. /*IGUISkin* skin = Environment->getSkin();
  44. IGUIFont* font = skin->getFont();
  45. if (font)
  46. {
  47. s32 t = font->getDimension(L"A").Height + 5;
  48. if (t > height)
  49. height = t;
  50. }*/
  51. rr.LowerRightCorner.X = parentwidth;
  52. rr.LowerRightCorner.Y = rr.UpperLeftCorner.Y + height;
  53. setRelativePosition(rr);
  54. }
  55. //! called if an event happened.
  56. bool CGUIToolBar::OnEvent(const SEvent& event)
  57. {
  58. if (isEnabled())
  59. {
  60. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  61. event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
  62. {
  63. if (AbsoluteClippingRect.isPointInside(core::position2di(event.MouseInput.X, event.MouseInput.Y)))
  64. return true;
  65. }
  66. }
  67. return IGUIElement::OnEvent(event);
  68. }
  69. //! draws the element and its children
  70. void CGUIToolBar::draw()
  71. {
  72. if (!IsVisible)
  73. return;
  74. IGUISkin* skin = Environment->getSkin();
  75. if (!skin)
  76. return;
  77. core::rect<s32> rect = AbsoluteRect;
  78. core::rect<s32>* clip = &AbsoluteClippingRect;
  79. // draw frame
  80. skin->draw3DToolBar(this, rect, clip);
  81. IGUIElement::draw();
  82. }
  83. //! Updates the absolute position.
  84. void CGUIToolBar::updateAbsolutePosition()
  85. {
  86. if (Parent)
  87. {
  88. DesiredRect.UpperLeftCorner.X = 0;
  89. DesiredRect.LowerRightCorner.X = Parent->getAbsolutePosition().getWidth();
  90. }
  91. IGUIElement::updateAbsolutePosition();
  92. }
  93. //! Adds a button to the tool bar
  94. IGUIButton* CGUIToolBar::addButton(s32 id, const wchar_t* text,const wchar_t* tooltiptext,
  95. video::ITexture* img, video::ITexture* pressed, bool isPushButton,
  96. bool useAlphaChannel)
  97. {
  98. ButtonX += 3;
  99. core::rect<s32> rectangle(ButtonX,2,ButtonX+1,3);
  100. if ( img )
  101. {
  102. const core::dimension2du &size = img->getOriginalSize();
  103. rectangle.LowerRightCorner.X = rectangle.UpperLeftCorner.X + size.Width + 8;
  104. rectangle.LowerRightCorner.Y = rectangle.UpperLeftCorner.Y + size.Height + 6;
  105. }
  106. if ( text )
  107. {
  108. IGUISkin* skin = Environment->getSkin();
  109. IGUIFont * font = skin->getFont(EGDF_BUTTON);
  110. if ( font )
  111. {
  112. core::dimension2d<u32> dim = font->getDimension(text);
  113. if ( (s32)dim.Width > rectangle.getWidth() )
  114. rectangle.LowerRightCorner.X = rectangle.UpperLeftCorner.X + dim.Width + 8;
  115. if ( (s32)dim.Height > rectangle.getHeight() )
  116. rectangle.LowerRightCorner.Y = rectangle.UpperLeftCorner.Y + dim.Height + 6;
  117. }
  118. }
  119. ButtonX += rectangle.getWidth();
  120. IGUIButton* button = new CGUIButton(Environment, this, id, rectangle);
  121. button->drop();
  122. if (text)
  123. button->setText(text);
  124. if (tooltiptext)
  125. button->setToolTipText(tooltiptext);
  126. if (img)
  127. button->setImage(img);
  128. if (pressed)
  129. button->setPressedImage(pressed);
  130. if (isPushButton)
  131. button->setIsPushButton(isPushButton);
  132. if (useAlphaChannel)
  133. button->setUseAlphaChannel(useAlphaChannel);
  134. return button;
  135. }
  136. } // end namespace gui
  137. } // end namespace irr
  138. #endif // _IRR_COMPILE_WITH_GUI_