]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CGUICheckBox.cpp
Drop _IRR_COMPILE_WITH_GUI_
[irrlicht.git] / source / Irrlicht / CGUICheckBox.cpp
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
4 \r
5 #include "CGUICheckBox.h"\r
6 \r
7 #include "IGUISkin.h"\r
8 #include "IGUIEnvironment.h"\r
9 #include "IVideoDriver.h"\r
10 #include "IGUIFont.h"\r
11 #include "os.h"\r
12 \r
13 namespace irr\r
14 {\r
15 namespace gui\r
16 {\r
17 \r
18 //! constructor\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
22 {\r
23         #ifdef _DEBUG\r
24         setDebugName("CGUICheckBox");\r
25         #endif\r
26 \r
27         // this element can be tabbed into\r
28         setTabStop(true);\r
29         setTabOrder(-1);\r
30 }\r
31 \r
32 \r
33 //! called if an event happened.\r
34 bool CGUICheckBox::OnEvent(const SEvent& event)\r
35 {\r
36         if (isEnabled())\r
37         {\r
38                 switch(event.EventType)\r
39                 {\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
43                         {\r
44                                 Pressed = true;\r
45                                 return true;\r
46                         }\r
47                         else\r
48                         if (Pressed && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE)\r
49                         {\r
50                                 Pressed = false;\r
51                                 return true;\r
52                         }\r
53                         else\r
54                         if (!event.KeyInput.PressedDown && Pressed &&\r
55                                 (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))\r
56                         {\r
57                                 Pressed = false;\r
58                                 if (Parent)\r
59                                 {\r
60                                         SEvent newEvent;\r
61                                         newEvent.EventType = EET_GUI_EVENT;\r
62                                         newEvent.GUIEvent.Caller = this;\r
63                                         newEvent.GUIEvent.Element = 0;\r
64                                         Checked = !Checked;\r
65                                         newEvent.GUIEvent.EventType = EGET_CHECKBOX_CHANGED;\r
66                                         Parent->OnEvent(newEvent);\r
67                                 }\r
68                                 return true;\r
69                         }\r
70                         break;\r
71                 case EET_GUI_EVENT:\r
72                         if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)\r
73                         {\r
74                                 if (event.GUIEvent.Caller == this)\r
75                                         Pressed = false;\r
76                         }\r
77                         break;\r
78                 case EET_MOUSE_INPUT_EVENT:\r
79                         if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)\r
80                         {\r
81                                 Pressed = true;\r
82                                 CheckTime = os::Timer::getTime();\r
83                                 return true;\r
84                         }\r
85                         else\r
86                         if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)\r
87                         {\r
88                                 bool wasPressed = Pressed;\r
89                                 Pressed = false;\r
90 \r
91                                 if (wasPressed && Parent)\r
92                                 {\r
93                                         if ( !AbsoluteClippingRect.isPointInside( core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y) ) )\r
94                                         {\r
95                                                 Pressed = false;\r
96                                                 return true;\r
97                                         }\r
98 \r
99                                         SEvent newEvent;\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
106                                 }\r
107 \r
108                                 return true;\r
109                         }\r
110                         break;\r
111                 default:\r
112                         break;\r
113                 }\r
114         }\r
115 \r
116         return IGUIElement::OnEvent(event);\r
117 }\r
118 \r
119 \r
120 //! draws the element and its children\r
121 void CGUICheckBox::draw()\r
122 {\r
123         if (!IsVisible)\r
124                 return;\r
125 \r
126         IGUISkin* skin = Environment->getSkin();\r
127         if (skin)\r
128         {\r
129                 video::IVideoDriver* driver = Environment->getVideoDriver();\r
130                 core::rect<s32> frameRect(AbsoluteRect);\r
131 \r
132                 // draw background\r
133                 if (Background)\r
134                 {\r
135                         video::SColor bgColor = skin->getColor(gui::EGDC_3D_FACE);\r
136                         driver->draw2DRectangle(bgColor, frameRect, &AbsoluteClippingRect);\r
137                 }\r
138 \r
139                 // draw the border\r
140                 if (Border)\r
141                 {\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
145                 }\r
146 \r
147                 const s32 height = skin->getSize(EGDS_CHECK_BOX_WIDTH);\r
148 \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
152                                         0, 0);\r
153 \r
154                 checkRect.LowerRightCorner.X = checkRect.UpperLeftCorner.X + height;\r
155                 checkRect.LowerRightCorner.Y = checkRect.UpperLeftCorner.Y + height;\r
156 \r
157                 EGUI_DEFAULT_COLOR col = EGDC_GRAY_EDITABLE;\r
158                 if ( isEnabled() )\r
159                         col = Pressed ? EGDC_FOCUSED_EDITABLE : EGDC_EDITABLE;\r
160                 skin->draw3DSunkenPane(this, skin->getColor(col),\r
161                         false, true, checkRect, &AbsoluteClippingRect);\r
162 \r
163                 // the checked icon\r
164                 if (Checked)\r
165                 {\r
166                         skin->drawIcon(this, EGDI_CHECK_BOX_CHECKED, checkRect.getCenter(),\r
167                                 CheckTime, os::Timer::getTime(), false, &AbsoluteClippingRect);\r
168                 }\r
169 \r
170                 // associated text\r
171                 if (Text.size())\r
172                 {\r
173                         checkRect = frameRect;\r
174                         checkRect.UpperLeftCorner.X += height + 5;\r
175 \r
176                         IGUIFont* font = skin->getFont();\r
177                         if (font)\r
178                         {\r
179                                 font->draw(Text.c_str(), checkRect,\r
180                                                 skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT), false, true, &AbsoluteClippingRect);\r
181                         }\r
182                 }\r
183         }\r
184         IGUIElement::draw();\r
185 }\r
186 \r
187 \r
188 //! set if box is checked\r
189 void CGUICheckBox::setChecked(bool checked)\r
190 {\r
191         Checked = checked;\r
192 }\r
193 \r
194 \r
195 //! returns if box is checked\r
196 bool CGUICheckBox::isChecked() const\r
197 {\r
198         return Checked;\r
199 }\r
200 \r
201 //! Sets whether to draw the background\r
202 void CGUICheckBox::setDrawBackground(bool draw)\r
203 {\r
204         Background = draw;\r
205 }\r
206 \r
207 //! Checks if background drawing is enabled\r
208 bool CGUICheckBox::isDrawBackgroundEnabled() const\r
209 {\r
210         return Background;\r
211 }\r
212 \r
213 //! Sets whether to draw the border\r
214 void CGUICheckBox::setDrawBorder(bool draw)\r
215 {\r
216         Border = draw;\r
217 }\r
218 \r
219 //! Checks if border drawing is enabled\r
220 bool CGUICheckBox::isDrawBorderEnabled() const\r
221 {\r
222         return Border;\r
223 }\r
224 \r
225 \r
226 } // end namespace gui\r
227 } // end namespace irr\r