CGUICheckBox.cpp 4.9 KB

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