1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CGUICheckBox.h"
\r
7 #include "IGUISkin.h"
\r
8 #include "IGUIEnvironment.h"
\r
9 #include "IVideoDriver.h"
\r
10 #include "IGUIFont.h"
\r
19 CGUICheckBox::CGUICheckBox(bool checked, IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
\r
20 : IGUICheckBox(environment, parent, id, rectangle), CheckTime(0), Pressed(false), Checked(checked)
\r
21 , Border(false), Background(false)
\r
24 setDebugName("CGUICheckBox");
\r
27 // this element can be tabbed into
\r
33 //! called if an event happened.
\r
34 bool CGUICheckBox::OnEvent(const SEvent& event)
\r
38 switch(event.EventType)
\r
40 case EET_KEY_INPUT_EVENT:
\r
41 if (event.KeyInput.PressedDown &&
\r
42 (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))
\r
48 if (Pressed && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE)
\r
54 if (!event.KeyInput.PressedDown && Pressed &&
\r
55 (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))
\r
61 newEvent.EventType = EET_GUI_EVENT;
\r
62 newEvent.GUIEvent.Caller = this;
\r
63 newEvent.GUIEvent.Element = 0;
\r
65 newEvent.GUIEvent.EventType = EGET_CHECKBOX_CHANGED;
\r
66 Parent->OnEvent(newEvent);
\r
72 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
\r
74 if (event.GUIEvent.Caller == this)
\r
78 case EET_MOUSE_INPUT_EVENT:
\r
79 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
\r
82 CheckTime = os::Timer::getTime();
\r
86 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
\r
88 bool wasPressed = Pressed;
\r
91 if (wasPressed && Parent)
\r
93 if ( !AbsoluteClippingRect.isPointInside( core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y) ) )
\r
100 newEvent.EventType = EET_GUI_EVENT;
\r
101 newEvent.GUIEvent.Caller = this;
\r
102 newEvent.GUIEvent.Element = 0;
\r
103 Checked = !Checked;
\r
104 newEvent.GUIEvent.EventType = EGET_CHECKBOX_CHANGED;
\r
105 Parent->OnEvent(newEvent);
\r
116 return IGUIElement::OnEvent(event);
\r
120 //! draws the element and its children
\r
121 void CGUICheckBox::draw()
\r
126 IGUISkin* skin = Environment->getSkin();
\r
129 video::IVideoDriver* driver = Environment->getVideoDriver();
\r
130 core::rect<s32> frameRect(AbsoluteRect);
\r
135 video::SColor bgColor = skin->getColor(gui::EGDC_3D_FACE);
\r
136 driver->draw2DRectangle(bgColor, frameRect, &AbsoluteClippingRect);
\r
142 skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
\r
143 frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
\r
144 frameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X);
\r
147 const s32 height = skin->getSize(EGDS_CHECK_BOX_WIDTH);
\r
149 // the rectangle around the "checked" area.
\r
150 core::rect<s32> checkRect(frameRect.UpperLeftCorner.X,
\r
151 ((frameRect.getHeight() - height) / 2) + frameRect.UpperLeftCorner.Y,
\r
154 checkRect.LowerRightCorner.X = checkRect.UpperLeftCorner.X + height;
\r
155 checkRect.LowerRightCorner.Y = checkRect.UpperLeftCorner.Y + height;
\r
157 EGUI_DEFAULT_COLOR col = EGDC_GRAY_EDITABLE;
\r
159 col = Pressed ? EGDC_FOCUSED_EDITABLE : EGDC_EDITABLE;
\r
160 skin->draw3DSunkenPane(this, skin->getColor(col),
\r
161 false, true, checkRect, &AbsoluteClippingRect);
\r
163 // the checked icon
\r
166 skin->drawIcon(this, EGDI_CHECK_BOX_CHECKED, checkRect.getCenter(),
\r
167 CheckTime, os::Timer::getTime(), false, &AbsoluteClippingRect);
\r
173 checkRect = frameRect;
\r
174 checkRect.UpperLeftCorner.X += height + 5;
\r
176 IGUIFont* font = skin->getFont();
\r
179 font->draw(Text.c_str(), checkRect,
\r
180 skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT), false, true, &AbsoluteClippingRect);
\r
184 IGUIElement::draw();
\r
188 //! set if box is checked
\r
189 void CGUICheckBox::setChecked(bool checked)
\r
195 //! returns if box is checked
\r
196 bool CGUICheckBox::isChecked() const
\r
201 //! Sets whether to draw the background
\r
202 void CGUICheckBox::setDrawBackground(bool draw)
\r
207 //! Checks if background drawing is enabled
\r
208 bool CGUICheckBox::isDrawBackgroundEnabled() const
\r
213 //! Sets whether to draw the border
\r
214 void CGUICheckBox::setDrawBorder(bool draw)
\r
219 //! Checks if border drawing is enabled
\r
220 bool CGUICheckBox::isDrawBorderEnabled() const
\r
226 } // end namespace gui
\r
227 } // end namespace irr
\r