CGUICheckBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "CGUICheckBox.h"
  5. #include "IGUISkin.h"
  6. #include "IGUIEnvironment.h"
  7. #include "IVideoDriver.h"
  8. #include "IGUIFont.h"
  9. #include "os.h"
  10. namespace irr
  11. {
  12. namespace gui
  13. {
  14. //! constructor
  15. CGUICheckBox::CGUICheckBox(bool checked, IGUIEnvironment *environment, IGUIElement *parent, s32 id, core::rect<s32> rectangle) :
  16. IGUICheckBox(environment, parent, id, rectangle), CheckTime(0), Pressed(false), Checked(checked), Border(false), Background(false)
  17. {
  18. // this element can be tabbed into
  19. setTabStop(true);
  20. setTabOrder(-1);
  21. }
  22. //! called if an event happened.
  23. bool CGUICheckBox::OnEvent(const SEvent &event)
  24. {
  25. if (isEnabled()) {
  26. switch (event.EventType) {
  27. case EET_KEY_INPUT_EVENT:
  28. if (event.KeyInput.PressedDown &&
  29. (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE)) {
  30. Pressed = true;
  31. return true;
  32. } else if (Pressed && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE) {
  33. Pressed = false;
  34. return true;
  35. } else if (!event.KeyInput.PressedDown && Pressed &&
  36. (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE)) {
  37. Pressed = false;
  38. if (Parent) {
  39. SEvent newEvent;
  40. newEvent.EventType = EET_GUI_EVENT;
  41. newEvent.GUIEvent.Caller = this;
  42. newEvent.GUIEvent.Element = 0;
  43. Checked = !Checked;
  44. newEvent.GUIEvent.EventType = EGET_CHECKBOX_CHANGED;
  45. Parent->OnEvent(newEvent);
  46. }
  47. return true;
  48. }
  49. break;
  50. case EET_GUI_EVENT:
  51. if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) {
  52. if (event.GUIEvent.Caller == this)
  53. Pressed = false;
  54. }
  55. break;
  56. case EET_MOUSE_INPUT_EVENT:
  57. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  58. Pressed = true;
  59. CheckTime = os::Timer::getTime();
  60. return true;
  61. } else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
  62. bool wasPressed = Pressed;
  63. Pressed = false;
  64. if (wasPressed && Parent) {
  65. if (!AbsoluteClippingRect.isPointInside(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
  66. Pressed = false;
  67. return true;
  68. }
  69. SEvent newEvent;
  70. newEvent.EventType = EET_GUI_EVENT;
  71. newEvent.GUIEvent.Caller = this;
  72. newEvent.GUIEvent.Element = 0;
  73. Checked = !Checked;
  74. newEvent.GUIEvent.EventType = EGET_CHECKBOX_CHANGED;
  75. Parent->OnEvent(newEvent);
  76. }
  77. return true;
  78. }
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. return IGUIElement::OnEvent(event);
  85. }
  86. //! draws the element and its children
  87. void CGUICheckBox::draw()
  88. {
  89. if (!IsVisible)
  90. return;
  91. IGUISkin *skin = Environment->getSkin();
  92. if (skin) {
  93. video::IVideoDriver *driver = Environment->getVideoDriver();
  94. core::rect<s32> frameRect(AbsoluteRect);
  95. // draw background
  96. if (Background) {
  97. video::SColor bgColor = skin->getColor(gui::EGDC_3D_FACE);
  98. driver->draw2DRectangle(bgColor, frameRect, &AbsoluteClippingRect);
  99. }
  100. // draw the border
  101. if (Border) {
  102. skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
  103. frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
  104. frameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X);
  105. }
  106. const s32 height = skin->getSize(EGDS_CHECK_BOX_WIDTH);
  107. // the rectangle around the "checked" area.
  108. core::rect<s32> checkRect(frameRect.UpperLeftCorner.X,
  109. ((frameRect.getHeight() - height) / 2) + frameRect.UpperLeftCorner.Y,
  110. 0, 0);
  111. checkRect.LowerRightCorner.X = checkRect.UpperLeftCorner.X + height;
  112. checkRect.LowerRightCorner.Y = checkRect.UpperLeftCorner.Y + height;
  113. EGUI_DEFAULT_COLOR col = EGDC_GRAY_EDITABLE;
  114. if (isEnabled())
  115. col = Pressed ? EGDC_FOCUSED_EDITABLE : EGDC_EDITABLE;
  116. skin->draw3DSunkenPane(this, skin->getColor(col),
  117. false, true, checkRect, &AbsoluteClippingRect);
  118. // the checked icon
  119. if (Checked) {
  120. skin->drawIcon(this, EGDI_CHECK_BOX_CHECKED, checkRect.getCenter(),
  121. CheckTime, os::Timer::getTime(), false, &AbsoluteClippingRect);
  122. }
  123. // associated text
  124. if (Text.size()) {
  125. checkRect = frameRect;
  126. checkRect.UpperLeftCorner.X += height + 5;
  127. IGUIFont *font = skin->getFont();
  128. if (font) {
  129. font->draw(Text.c_str(), checkRect,
  130. skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT), false, true, &AbsoluteClippingRect);
  131. }
  132. }
  133. }
  134. IGUIElement::draw();
  135. }
  136. //! set if box is checked
  137. void CGUICheckBox::setChecked(bool checked)
  138. {
  139. Checked = checked;
  140. }
  141. //! returns if box is checked
  142. bool CGUICheckBox::isChecked() const
  143. {
  144. return Checked;
  145. }
  146. //! Sets whether to draw the background
  147. void CGUICheckBox::setDrawBackground(bool draw)
  148. {
  149. Background = draw;
  150. }
  151. //! Checks if background drawing is enabled
  152. bool CGUICheckBox::isDrawBackgroundEnabled() const
  153. {
  154. return Background;
  155. }
  156. //! Sets whether to draw the border
  157. void CGUICheckBox::setDrawBorder(bool draw)
  158. {
  159. Border = draw;
  160. }
  161. //! Checks if border drawing is enabled
  162. bool CGUICheckBox::isDrawBorderEnabled() const
  163. {
  164. return Border;
  165. }
  166. } // end namespace gui
  167. } // end namespace irr