aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/CGUIButton.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/CGUIButton.cpp1062
1 files changed, 531 insertions, 531 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CGUIButton.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CGUIButton.cpp
index 5a451cd..76fe444 100644
--- a/libraries/irrlicht-1.8/source/Irrlicht/CGUIButton.cpp
+++ b/libraries/irrlicht-1.8/source/Irrlicht/CGUIButton.cpp
@@ -1,531 +1,531 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt 1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine". 2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h 3// For conditions of distribution and use, see copyright notice in irrlicht.h
4 4
5#include "CGUIButton.h" 5#include "CGUIButton.h"
6#ifdef _IRR_COMPILE_WITH_GUI_ 6#ifdef _IRR_COMPILE_WITH_GUI_
7 7
8#include "IGUISkin.h" 8#include "IGUISkin.h"
9#include "IGUIEnvironment.h" 9#include "IGUIEnvironment.h"
10#include "IVideoDriver.h" 10#include "IVideoDriver.h"
11#include "IGUIFont.h" 11#include "IGUIFont.h"
12#include "os.h" 12#include "os.h"
13 13
14namespace irr 14namespace irr
15{ 15{
16namespace gui 16namespace gui
17{ 17{
18 18
19//! constructor 19//! constructor
20CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent, 20CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent,
21 s32 id, core::rect<s32> rectangle, bool noclip) 21 s32 id, core::rect<s32> rectangle, bool noclip)
22: IGUIButton(environment, parent, id, rectangle), 22: IGUIButton(environment, parent, id, rectangle),
23 SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0), 23 SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0),
24 ClickTime(0), HoverTime(0), FocusTime(0), 24 ClickTime(0), HoverTime(0), FocusTime(0),
25 IsPushButton(false), Pressed(false), 25 IsPushButton(false), Pressed(false),
26 UseAlphaChannel(false), DrawBorder(true), ScaleImage(false) 26 UseAlphaChannel(false), DrawBorder(true), ScaleImage(false)
27{ 27{
28 #ifdef _DEBUG 28 #ifdef _DEBUG
29 setDebugName("CGUIButton"); 29 setDebugName("CGUIButton");
30 #endif 30 #endif
31 setNotClipped(noclip); 31 setNotClipped(noclip);
32 32
33 // Initialize the sprites. 33 // Initialize the sprites.
34 for (u32 i=0; i<EGBS_COUNT; ++i) 34 for (u32 i=0; i<EGBS_COUNT; ++i)
35 ButtonSprites[i].Index = -1; 35 ButtonSprites[i].Index = -1;
36 36
37 // This element can be tabbed. 37 // This element can be tabbed.
38 setTabStop(true); 38 setTabStop(true);
39 setTabOrder(-1); 39 setTabOrder(-1);
40} 40}
41 41
42 42
43//! destructor 43//! destructor
44CGUIButton::~CGUIButton() 44CGUIButton::~CGUIButton()
45{ 45{
46 if (OverrideFont) 46 if (OverrideFont)
47 OverrideFont->drop(); 47 OverrideFont->drop();
48 48
49 if (Image) 49 if (Image)
50 Image->drop(); 50 Image->drop();
51 51
52 if (PressedImage) 52 if (PressedImage)
53 PressedImage->drop(); 53 PressedImage->drop();
54 54
55 if (SpriteBank) 55 if (SpriteBank)
56 SpriteBank->drop(); 56 SpriteBank->drop();
57} 57}
58 58
59 59
60//! Sets if the images should be scaled to fit the button 60//! Sets if the images should be scaled to fit the button
61void CGUIButton::setScaleImage(bool scaleImage) 61void CGUIButton::setScaleImage(bool scaleImage)
62{ 62{
63 ScaleImage = scaleImage; 63 ScaleImage = scaleImage;
64} 64}
65 65
66 66
67//! Returns whether the button scale the used images 67//! Returns whether the button scale the used images
68bool CGUIButton::isScalingImage() const 68bool CGUIButton::isScalingImage() const
69{ 69{
70 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 70 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
71 return ScaleImage; 71 return ScaleImage;
72} 72}
73 73
74 74
75//! Sets if the button should use the skin to draw its border 75//! Sets if the button should use the skin to draw its border
76void CGUIButton::setDrawBorder(bool border) 76void CGUIButton::setDrawBorder(bool border)
77{ 77{
78 DrawBorder = border; 78 DrawBorder = border;
79} 79}
80 80
81 81
82void CGUIButton::setSpriteBank(IGUISpriteBank* sprites) 82void CGUIButton::setSpriteBank(IGUISpriteBank* sprites)
83{ 83{
84 if (sprites) 84 if (sprites)
85 sprites->grab(); 85 sprites->grab();
86 86
87 if (SpriteBank) 87 if (SpriteBank)
88 SpriteBank->drop(); 88 SpriteBank->drop();
89 89
90 SpriteBank = sprites; 90 SpriteBank = sprites;
91} 91}
92 92
93 93
94void CGUIButton::setSprite(EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop) 94void CGUIButton::setSprite(EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop)
95{ 95{
96 if (SpriteBank) 96 if (SpriteBank)
97 { 97 {
98 ButtonSprites[(u32)state].Index = index; 98 ButtonSprites[(u32)state].Index = index;
99 ButtonSprites[(u32)state].Color = color; 99 ButtonSprites[(u32)state].Color = color;
100 ButtonSprites[(u32)state].Loop = loop; 100 ButtonSprites[(u32)state].Loop = loop;
101 } 101 }
102 else 102 else
103 { 103 {
104 ButtonSprites[(u32)state].Index = -1; 104 ButtonSprites[(u32)state].Index = -1;
105 } 105 }
106} 106}
107 107
108 108
109//! called if an event happened. 109//! called if an event happened.
110bool CGUIButton::OnEvent(const SEvent& event) 110bool CGUIButton::OnEvent(const SEvent& event)
111{ 111{
112 if (!isEnabled()) 112 if (!isEnabled())
113 return IGUIElement::OnEvent(event); 113 return IGUIElement::OnEvent(event);
114 114
115 switch(event.EventType) 115 switch(event.EventType)
116 { 116 {
117 case EET_KEY_INPUT_EVENT: 117 case EET_KEY_INPUT_EVENT:
118 if (event.KeyInput.PressedDown && 118 if (event.KeyInput.PressedDown &&
119 (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE)) 119 (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))
120 { 120 {
121 if (!IsPushButton) 121 if (!IsPushButton)
122 setPressed(true); 122 setPressed(true);
123 else 123 else
124 setPressed(!Pressed); 124 setPressed(!Pressed);
125 125
126 return true; 126 return true;
127 } 127 }
128 if (Pressed && !IsPushButton && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE) 128 if (Pressed && !IsPushButton && event.KeyInput.PressedDown && event.KeyInput.Key == KEY_ESCAPE)
129 { 129 {
130 setPressed(false); 130 setPressed(false);
131 return true; 131 return true;
132 } 132 }
133 else 133 else
134 if (!event.KeyInput.PressedDown && Pressed && 134 if (!event.KeyInput.PressedDown && Pressed &&
135 (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE)) 135 (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))
136 { 136 {
137 137
138 if (!IsPushButton) 138 if (!IsPushButton)
139 setPressed(false); 139 setPressed(false);
140 140
141 if (Parent) 141 if (Parent)
142 { 142 {
143 SEvent newEvent; 143 SEvent newEvent;
144 newEvent.EventType = EET_GUI_EVENT; 144 newEvent.EventType = EET_GUI_EVENT;
145 newEvent.GUIEvent.Caller = this; 145 newEvent.GUIEvent.Caller = this;
146 newEvent.GUIEvent.Element = 0; 146 newEvent.GUIEvent.Element = 0;
147 newEvent.GUIEvent.EventType = EGET_BUTTON_CLICKED; 147 newEvent.GUIEvent.EventType = EGET_BUTTON_CLICKED;
148 Parent->OnEvent(newEvent); 148 Parent->OnEvent(newEvent);
149 } 149 }
150 return true; 150 return true;
151 } 151 }
152 break; 152 break;
153 case EET_GUI_EVENT: 153 case EET_GUI_EVENT:
154 if (event.GUIEvent.Caller == this) 154 if (event.GUIEvent.Caller == this)
155 { 155 {
156 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) 156 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
157 { 157 {
158 if (!IsPushButton) 158 if (!IsPushButton)
159 setPressed(false); 159 setPressed(false);
160 FocusTime = os::Timer::getTime(); 160 FocusTime = os::Timer::getTime();
161 } 161 }
162 else if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUSED) 162 else if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUSED)
163 { 163 {
164 FocusTime = os::Timer::getTime(); 164 FocusTime = os::Timer::getTime();
165 } 165 }
166 else if (event.GUIEvent.EventType == EGET_ELEMENT_HOVERED || event.GUIEvent.EventType == EGET_ELEMENT_LEFT) 166 else if (event.GUIEvent.EventType == EGET_ELEMENT_HOVERED || event.GUIEvent.EventType == EGET_ELEMENT_LEFT)
167 { 167 {
168 HoverTime = os::Timer::getTime(); 168 HoverTime = os::Timer::getTime();
169 } 169 }
170 } 170 }
171 break; 171 break;
172 case EET_MOUSE_INPUT_EVENT: 172 case EET_MOUSE_INPUT_EVENT:
173 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) 173 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
174 { 174 {
175 if (Environment->hasFocus(this) && 175 if (Environment->hasFocus(this) &&
176 !AbsoluteClippingRect.isPointInside(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) 176 !AbsoluteClippingRect.isPointInside(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
177 { 177 {
178 Environment->removeFocus(this); 178 Environment->removeFocus(this);
179 return false; 179 return false;
180 } 180 }
181 181
182 if (!IsPushButton) 182 if (!IsPushButton)
183 setPressed(true); 183 setPressed(true);
184 184
185 Environment->setFocus(this); 185 Environment->setFocus(this);
186 return true; 186 return true;
187 } 187 }
188 else 188 else
189 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) 189 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
190 { 190 {
191 bool wasPressed = Pressed; 191 bool wasPressed = Pressed;
192 192
193 if ( !AbsoluteClippingRect.isPointInside( core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y ) ) ) 193 if ( !AbsoluteClippingRect.isPointInside( core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y ) ) )
194 { 194 {
195 if (!IsPushButton) 195 if (!IsPushButton)
196 setPressed(false); 196 setPressed(false);
197 return true; 197 return true;
198 } 198 }
199 199
200 if (!IsPushButton) 200 if (!IsPushButton)
201 setPressed(false); 201 setPressed(false);
202 else 202 else
203 { 203 {
204 setPressed(!Pressed); 204 setPressed(!Pressed);
205 } 205 }
206 206
207 if ((!IsPushButton && wasPressed && Parent) || 207 if ((!IsPushButton && wasPressed && Parent) ||
208 (IsPushButton && wasPressed != Pressed)) 208 (IsPushButton && wasPressed != Pressed))
209 { 209 {
210 SEvent newEvent; 210 SEvent newEvent;
211 newEvent.EventType = EET_GUI_EVENT; 211 newEvent.EventType = EET_GUI_EVENT;
212 newEvent.GUIEvent.Caller = this; 212 newEvent.GUIEvent.Caller = this;
213 newEvent.GUIEvent.Element = 0; 213 newEvent.GUIEvent.Element = 0;
214 newEvent.GUIEvent.EventType = EGET_BUTTON_CLICKED; 214 newEvent.GUIEvent.EventType = EGET_BUTTON_CLICKED;
215 Parent->OnEvent(newEvent); 215 Parent->OnEvent(newEvent);
216 } 216 }
217 217
218 return true; 218 return true;
219 } 219 }
220 break; 220 break;
221 default: 221 default:
222 break; 222 break;
223 } 223 }
224 224
225 return Parent ? Parent->OnEvent(event) : false; 225 return Parent ? Parent->OnEvent(event) : false;
226} 226}
227 227
228 228
229//! draws the element and its children 229//! draws the element and its children
230void CGUIButton::draw() 230void CGUIButton::draw()
231{ 231{
232 if (!IsVisible) 232 if (!IsVisible)
233 return; 233 return;
234 234
235 IGUISkin* skin = Environment->getSkin(); 235 IGUISkin* skin = Environment->getSkin();
236 video::IVideoDriver* driver = Environment->getVideoDriver(); 236 video::IVideoDriver* driver = Environment->getVideoDriver();
237 237
238 // todo: move sprite up and text down if the pressed state has a sprite 238 // todo: move sprite up and text down if the pressed state has a sprite
239 const core::position2di spritePos = AbsoluteRect.getCenter(); 239 const core::position2di spritePos = AbsoluteRect.getCenter();
240 240
241 if (!Pressed) 241 if (!Pressed)
242 { 242 {
243 if (DrawBorder) 243 if (DrawBorder)
244 skin->draw3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect); 244 skin->draw3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect);
245 245
246 if (Image) 246 if (Image)
247 { 247 {
248 core::position2d<s32> pos = spritePos; 248 core::position2d<s32> pos = spritePos;
249 pos.X -= ImageRect.getWidth() / 2; 249 pos.X -= ImageRect.getWidth() / 2;
250 pos.Y -= ImageRect.getHeight() / 2; 250 pos.Y -= ImageRect.getHeight() / 2;
251 251
252 driver->draw2DImage(Image, 252 driver->draw2DImage(Image,
253 ScaleImage? AbsoluteRect : 253 ScaleImage? AbsoluteRect :
254 core::recti(pos, ImageRect.getSize()), 254 core::recti(pos, ImageRect.getSize()),
255 ImageRect, &AbsoluteClippingRect, 255 ImageRect, &AbsoluteClippingRect,
256 0, UseAlphaChannel); 256 0, UseAlphaChannel);
257 } 257 }
258 } 258 }
259 else 259 else
260 { 260 {
261 if (DrawBorder) 261 if (DrawBorder)
262 skin->draw3DButtonPanePressed(this, AbsoluteRect, &AbsoluteClippingRect); 262 skin->draw3DButtonPanePressed(this, AbsoluteRect, &AbsoluteClippingRect);
263 263
264 if (PressedImage) 264 if (PressedImage)
265 { 265 {
266 core::position2d<s32> pos = spritePos; 266 core::position2d<s32> pos = spritePos;
267 pos.X -= PressedImageRect.getWidth() / 2; 267 pos.X -= PressedImageRect.getWidth() / 2;
268 pos.Y -= PressedImageRect.getHeight() / 2; 268 pos.Y -= PressedImageRect.getHeight() / 2;
269 269
270 if (Image == PressedImage && PressedImageRect == ImageRect) 270 if (Image == PressedImage && PressedImageRect == ImageRect)
271 { 271 {
272 pos.X += skin->getSize(EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X); 272 pos.X += skin->getSize(EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X);
273 pos.Y += skin->getSize(EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y); 273 pos.Y += skin->getSize(EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y);
274 } 274 }
275 driver->draw2DImage(PressedImage, 275 driver->draw2DImage(PressedImage,
276 ScaleImage? AbsoluteRect : 276 ScaleImage? AbsoluteRect :
277 core::recti(pos, PressedImageRect.getSize()), 277 core::recti(pos, PressedImageRect.getSize()),
278 PressedImageRect, &AbsoluteClippingRect, 278 PressedImageRect, &AbsoluteClippingRect,
279 0, UseAlphaChannel); 279 0, UseAlphaChannel);
280 } 280 }
281 } 281 }
282 282
283 if (SpriteBank) 283 if (SpriteBank)
284 { 284 {
285 // pressed / unpressed animation 285 // pressed / unpressed animation
286 u32 state = Pressed ? (u32)EGBS_BUTTON_DOWN : (u32)EGBS_BUTTON_UP; 286 u32 state = Pressed ? (u32)EGBS_BUTTON_DOWN : (u32)EGBS_BUTTON_UP;
287 if (ButtonSprites[state].Index != -1) 287 if (ButtonSprites[state].Index != -1)
288 { 288 {
289 SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos, 289 SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
290 &AbsoluteClippingRect, ButtonSprites[state].Color, ClickTime, os::Timer::getTime(), 290 &AbsoluteClippingRect, ButtonSprites[state].Color, ClickTime, os::Timer::getTime(),
291 ButtonSprites[state].Loop, true); 291 ButtonSprites[state].Loop, true);
292 } 292 }
293 293
294 // focused / unfocused animation 294 // focused / unfocused animation
295 state = Environment->hasFocus(this) ? (u32)EGBS_BUTTON_FOCUSED : (u32)EGBS_BUTTON_NOT_FOCUSED; 295 state = Environment->hasFocus(this) ? (u32)EGBS_BUTTON_FOCUSED : (u32)EGBS_BUTTON_NOT_FOCUSED;
296 if (ButtonSprites[state].Index != -1) 296 if (ButtonSprites[state].Index != -1)
297 { 297 {
298 SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos, 298 SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
299 &AbsoluteClippingRect, ButtonSprites[state].Color, FocusTime, os::Timer::getTime(), 299 &AbsoluteClippingRect, ButtonSprites[state].Color, FocusTime, os::Timer::getTime(),
300 ButtonSprites[state].Loop, true); 300 ButtonSprites[state].Loop, true);
301 } 301 }
302 302
303 // mouse over / off animation 303 // mouse over / off animation
304 if (isEnabled()) 304 if (isEnabled())
305 { 305 {
306 state = Environment->getHovered() == this ? (u32)EGBS_BUTTON_MOUSE_OVER : (u32)EGBS_BUTTON_MOUSE_OFF; 306 state = Environment->getHovered() == this ? (u32)EGBS_BUTTON_MOUSE_OVER : (u32)EGBS_BUTTON_MOUSE_OFF;
307 if (ButtonSprites[state].Index != -1) 307 if (ButtonSprites[state].Index != -1)
308 { 308 {
309 SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos, 309 SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
310 &AbsoluteClippingRect, ButtonSprites[state].Color, HoverTime, os::Timer::getTime(), 310 &AbsoluteClippingRect, ButtonSprites[state].Color, HoverTime, os::Timer::getTime(),
311 ButtonSprites[state].Loop, true); 311 ButtonSprites[state].Loop, true);
312 } 312 }
313 } 313 }
314 } 314 }
315 315
316 if (Text.size()) 316 if (Text.size())
317 { 317 {
318 IGUIFont* font = getActiveFont(); 318 IGUIFont* font = getActiveFont();
319 319
320 core::rect<s32> rect = AbsoluteRect; 320 core::rect<s32> rect = AbsoluteRect;
321 if (Pressed) 321 if (Pressed)
322 { 322 {
323 rect.UpperLeftCorner.X += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_X); 323 rect.UpperLeftCorner.X += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_X);
324 rect.UpperLeftCorner.Y += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_Y); 324 rect.UpperLeftCorner.Y += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_Y);
325 } 325 }
326 326
327 if (font) 327 if (font)
328 font->draw(Text.c_str(), rect, 328 font->draw(Text.c_str(), rect,
329 skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT), 329 skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT),
330 true, true, &AbsoluteClippingRect); 330 true, true, &AbsoluteClippingRect);
331 } 331 }
332 332
333 IGUIElement::draw(); 333 IGUIElement::draw();
334} 334}
335 335
336 336
337//! sets another skin independent font. if this is set to zero, the button uses the font of the skin. 337//! sets another skin independent font. if this is set to zero, the button uses the font of the skin.
338void CGUIButton::setOverrideFont(IGUIFont* font) 338void CGUIButton::setOverrideFont(IGUIFont* font)
339{ 339{
340 if (OverrideFont == font) 340 if (OverrideFont == font)
341 return; 341 return;
342 342
343 if (OverrideFont) 343 if (OverrideFont)
344 OverrideFont->drop(); 344 OverrideFont->drop();
345 345
346 OverrideFont = font; 346 OverrideFont = font;
347 347
348 if (OverrideFont) 348 if (OverrideFont)
349 OverrideFont->grab(); 349 OverrideFont->grab();
350} 350}
351 351
352//! Gets the override font (if any) 352//! Gets the override font (if any)
353IGUIFont * CGUIButton::getOverrideFont() const 353IGUIFont * CGUIButton::getOverrideFont() const
354{ 354{
355 return OverrideFont; 355 return OverrideFont;
356} 356}
357 357
358//! Get the font which is used right now for drawing 358//! Get the font which is used right now for drawing
359IGUIFont* CGUIButton::getActiveFont() const 359IGUIFont* CGUIButton::getActiveFont() const
360{ 360{
361 if ( OverrideFont ) 361 if ( OverrideFont )
362 return OverrideFont; 362 return OverrideFont;
363 IGUISkin* skin = Environment->getSkin(); 363 IGUISkin* skin = Environment->getSkin();
364 if (skin) 364 if (skin)
365 return skin->getFont(EGDF_BUTTON); 365 return skin->getFont(EGDF_BUTTON);
366 return 0; 366 return 0;
367} 367}
368 368
369//! Sets an image which should be displayed on the button when it is in normal state. 369//! Sets an image which should be displayed on the button when it is in normal state.
370void CGUIButton::setImage(video::ITexture* image) 370void CGUIButton::setImage(video::ITexture* image)
371{ 371{
372 if (image) 372 if (image)
373 image->grab(); 373 image->grab();
374 if (Image) 374 if (Image)
375 Image->drop(); 375 Image->drop();
376 376
377 Image = image; 377 Image = image;
378 if (image) 378 if (image)
379 ImageRect = core::rect<s32>(core::position2d<s32>(0,0), image->getOriginalSize()); 379 ImageRect = core::rect<s32>(core::position2d<s32>(0,0), image->getOriginalSize());
380 380
381 if (!PressedImage) 381 if (!PressedImage)
382 setPressedImage(Image); 382 setPressedImage(Image);
383} 383}
384 384
385 385
386//! Sets the image which should be displayed on the button when it is in its normal state. 386//! Sets the image which should be displayed on the button when it is in its normal state.
387void CGUIButton::setImage(video::ITexture* image, const core::rect<s32>& pos) 387void CGUIButton::setImage(video::ITexture* image, const core::rect<s32>& pos)
388{ 388{
389 setImage(image); 389 setImage(image);
390 ImageRect = pos; 390 ImageRect = pos;
391} 391}
392 392
393 393
394//! Sets an image which should be displayed on the button when it is in pressed state. 394//! Sets an image which should be displayed on the button when it is in pressed state.
395void CGUIButton::setPressedImage(video::ITexture* image) 395void CGUIButton::setPressedImage(video::ITexture* image)
396{ 396{
397 if (image) 397 if (image)
398 image->grab(); 398 image->grab();
399 399
400 if (PressedImage) 400 if (PressedImage)
401 PressedImage->drop(); 401 PressedImage->drop();
402 402
403 PressedImage = image; 403 PressedImage = image;
404 if (image) 404 if (image)
405 PressedImageRect = core::rect<s32>(core::position2d<s32>(0,0), image->getOriginalSize()); 405 PressedImageRect = core::rect<s32>(core::position2d<s32>(0,0), image->getOriginalSize());
406} 406}
407 407
408 408
409//! Sets the image which should be displayed on the button when it is in its pressed state. 409//! Sets the image which should be displayed on the button when it is in its pressed state.
410void CGUIButton::setPressedImage(video::ITexture* image, const core::rect<s32>& pos) 410void CGUIButton::setPressedImage(video::ITexture* image, const core::rect<s32>& pos)
411{ 411{
412 setPressedImage(image); 412 setPressedImage(image);
413 PressedImageRect = pos; 413 PressedImageRect = pos;
414} 414}
415 415
416 416
417//! Sets if the button should behave like a push button. Which means it 417//! Sets if the button should behave like a push button. Which means it
418//! can be in two states: Normal or Pressed. With a click on the button, 418//! can be in two states: Normal or Pressed. With a click on the button,
419//! the user can change the state of the button. 419//! the user can change the state of the button.
420void CGUIButton::setIsPushButton(bool isPushButton) 420void CGUIButton::setIsPushButton(bool isPushButton)
421{ 421{
422 IsPushButton = isPushButton; 422 IsPushButton = isPushButton;
423} 423}
424 424
425 425
426//! Returns if the button is currently pressed 426//! Returns if the button is currently pressed
427bool CGUIButton::isPressed() const 427bool CGUIButton::isPressed() const
428{ 428{
429 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 429 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
430 return Pressed; 430 return Pressed;
431} 431}
432 432
433 433
434//! Sets the pressed state of the button if this is a pushbutton 434//! Sets the pressed state of the button if this is a pushbutton
435void CGUIButton::setPressed(bool pressed) 435void CGUIButton::setPressed(bool pressed)
436{ 436{
437 if (Pressed != pressed) 437 if (Pressed != pressed)
438 { 438 {
439 ClickTime = os::Timer::getTime(); 439 ClickTime = os::Timer::getTime();
440 Pressed = pressed; 440 Pressed = pressed;
441 } 441 }
442} 442}
443 443
444 444
445//! Returns whether the button is a push button 445//! Returns whether the button is a push button
446bool CGUIButton::isPushButton() const 446bool CGUIButton::isPushButton() const
447{ 447{
448 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 448 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
449 return IsPushButton; 449 return IsPushButton;
450} 450}
451 451
452 452
453//! Sets if the alpha channel should be used for drawing images on the button (default is false) 453//! Sets if the alpha channel should be used for drawing images on the button (default is false)
454void CGUIButton::setUseAlphaChannel(bool useAlphaChannel) 454void CGUIButton::setUseAlphaChannel(bool useAlphaChannel)
455{ 455{
456 UseAlphaChannel = useAlphaChannel; 456 UseAlphaChannel = useAlphaChannel;
457} 457}
458 458
459 459
460//! Returns if the alpha channel should be used for drawing images on the button 460//! Returns if the alpha channel should be used for drawing images on the button
461bool CGUIButton::isAlphaChannelUsed() const 461bool CGUIButton::isAlphaChannelUsed() const
462{ 462{
463 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 463 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
464 return UseAlphaChannel; 464 return UseAlphaChannel;
465} 465}
466 466
467 467
468bool CGUIButton::isDrawingBorder() const 468bool CGUIButton::isDrawingBorder() const
469{ 469{
470 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; 470 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
471 return DrawBorder; 471 return DrawBorder;
472} 472}
473 473
474 474
475//! Writes attributes of the element. 475//! Writes attributes of the element.
476void CGUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const 476void CGUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
477{ 477{
478 IGUIButton::serializeAttributes(out,options); 478 IGUIButton::serializeAttributes(out,options);
479 479
480 out->addBool ("PushButton", IsPushButton ); 480 out->addBool ("PushButton", IsPushButton );
481 if (IsPushButton) 481 if (IsPushButton)
482 out->addBool("Pressed", Pressed); 482 out->addBool("Pressed", Pressed);
483 483
484 out->addTexture ("Image", Image); 484 out->addTexture ("Image", Image);
485 out->addRect ("ImageRect", ImageRect); 485 out->addRect ("ImageRect", ImageRect);
486 out->addTexture ("PressedImage", PressedImage); 486 out->addTexture ("PressedImage", PressedImage);
487 out->addRect ("PressedImageRect", PressedImageRect); 487 out->addRect ("PressedImageRect", PressedImageRect);
488 488
489 out->addBool ("UseAlphaChannel", isAlphaChannelUsed()); 489 out->addBool ("UseAlphaChannel", isAlphaChannelUsed());
490 out->addBool ("Border", isDrawingBorder()); 490 out->addBool ("Border", isDrawingBorder());
491 out->addBool ("ScaleImage", isScalingImage()); 491 out->addBool ("ScaleImage", isScalingImage());
492 492
493 // out->addString ("OverrideFont", OverrideFont); 493 // out->addString ("OverrideFont", OverrideFont);
494} 494}
495 495
496 496
497//! Reads attributes of the element 497//! Reads attributes of the element
498void CGUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) 498void CGUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
499{ 499{
500 IGUIButton::deserializeAttributes(in,options); 500 IGUIButton::deserializeAttributes(in,options);
501 501
502 IsPushButton = in->getAttributeAsBool("PushButton"); 502 IsPushButton = in->getAttributeAsBool("PushButton");
503 Pressed = IsPushButton ? in->getAttributeAsBool("Pressed") : false; 503 Pressed = IsPushButton ? in->getAttributeAsBool("Pressed") : false;
504 504
505 core::rect<s32> rec = in->getAttributeAsRect("ImageRect"); 505 core::rect<s32> rec = in->getAttributeAsRect("ImageRect");
506 if (rec.isValid()) 506 if (rec.isValid())
507 setImage( in->getAttributeAsTexture("Image"), rec); 507 setImage( in->getAttributeAsTexture("Image"), rec);
508 else 508 else
509 setImage( in->getAttributeAsTexture("Image") ); 509 setImage( in->getAttributeAsTexture("Image") );
510 510
511 rec = in->getAttributeAsRect("PressedImageRect"); 511 rec = in->getAttributeAsRect("PressedImageRect");
512 if (rec.isValid()) 512 if (rec.isValid())
513 setPressedImage( in->getAttributeAsTexture("PressedImage"), rec); 513 setPressedImage( in->getAttributeAsTexture("PressedImage"), rec);
514 else 514 else
515 setPressedImage( in->getAttributeAsTexture("PressedImage") ); 515 setPressedImage( in->getAttributeAsTexture("PressedImage") );
516 516
517 setDrawBorder(in->getAttributeAsBool("Border")); 517 setDrawBorder(in->getAttributeAsBool("Border"));
518 setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel")); 518 setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel"));
519 setScaleImage(in->getAttributeAsBool("ScaleImage")); 519 setScaleImage(in->getAttributeAsBool("ScaleImage"));
520 520
521 // setOverrideFont(in->getAttributeAsString("OverrideFont")); 521 // setOverrideFont(in->getAttributeAsString("OverrideFont"));
522 522
523 updateAbsolutePosition(); 523 updateAbsolutePosition();
524} 524}
525 525
526 526
527} // end namespace gui 527} // end namespace gui
528} // end namespace irr 528} // end namespace irr
529 529
530#endif // _IRR_COMPILE_WITH_GUI_ 530#endif // _IRR_COMPILE_WITH_GUI_
531 531