aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CGUIColorSelectDialog.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CGUIColorSelectDialog.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CGUIColorSelectDialog.cpp483
1 files changed, 483 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CGUIColorSelectDialog.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CGUIColorSelectDialog.cpp
new file mode 100644
index 0000000..0744ce9
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CGUIColorSelectDialog.cpp
@@ -0,0 +1,483 @@
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
5#include "CGUIColorSelectDialog.h"
6
7#ifdef _IRR_COMPILE_WITH_GUI_
8
9#include "IGUISkin.h"
10#include "IGUIEnvironment.h"
11#include "IVideoDriver.h"
12#include "IGUIButton.h"
13#include "IGUIStaticText.h"
14#include "IGUIFont.h"
15#include "IGUISpriteBank.h"
16#include "IFileList.h"
17#include "os.h"
18#include "fast_atof.h"
19
20namespace irr
21{
22namespace gui
23{
24
25const s32 CSD_WIDTH = 350;
26const s32 CSD_HEIGHT = 300;
27
28namespace
29{
30
31struct subElementPredefines
32{
33 const wchar_t *pre;
34 const wchar_t *init;
35 const wchar_t *post;
36 int x, y;
37 int range_down ,range_up;
38};
39
40static const subElementPredefines Template [] =
41{
42 { L"A:", L"0", 0,50,165, 0, 255 },
43 { L"R:", L"0", 0,20,205, 0, 255 },
44 { L"G:", L"0", 0,20,230, 0, 255 },
45 { L"B:", L"0", 0,20,255, 0, 255 },
46 { L"H:", L"0", L"°",80,205, 0, 360 },
47 { L"S:", L"0", L"%",80,230, 0, 100 },
48 { L"L:", L"0", L"%",80,255, 0, 100 },
49};
50
51}
52
53//! constructor
54CGUIColorSelectDialog::CGUIColorSelectDialog(const wchar_t* title, IGUIEnvironment* environment, IGUIElement* parent, s32 id)
55 : IGUIColorSelectDialog(environment, parent, id,
56 core::rect<s32>((parent->getAbsolutePosition().getWidth()-CSD_WIDTH)/2,
57 (parent->getAbsolutePosition().getHeight()-CSD_HEIGHT)/2,
58 (parent->getAbsolutePosition().getWidth()-CSD_WIDTH)/2+CSD_WIDTH,
59 (parent->getAbsolutePosition().getHeight()-CSD_HEIGHT)/2+CSD_HEIGHT)),
60 Dragging(false)
61{
62 #ifdef _DEBUG
63 IGUIElement::setDebugName("CGUIColorSelectDialog");
64 #endif
65
66 Text = title;
67
68 IGUISkin* skin = Environment->getSkin();
69
70 const s32 buttonw = environment->getSkin()->getSize(EGDS_WINDOW_BUTTON_WIDTH);
71 const s32 posx = RelativeRect.getWidth() - buttonw - 4;
72
73 CloseButton = Environment->addButton(core::rect<s32>(posx, 3, posx + buttonw, 3 + buttonw),
74 this, -1, L"", skin ? skin->getDefaultText(EGDT_WINDOW_CLOSE) : L"Close");
75 if (skin && skin->getSpriteBank())
76 {
77 CloseButton->setSpriteBank(skin->getSpriteBank());
78 CloseButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_WINDOW_CLOSE), skin->getColor(EGDC_WINDOW_SYMBOL));
79 CloseButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_WINDOW_CLOSE), skin->getColor(EGDC_WINDOW_SYMBOL));
80 }
81 CloseButton->setSubElement(true);
82 CloseButton->setTabStop(false);
83 CloseButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
84 CloseButton->grab();
85
86 OKButton = Environment->addButton(
87 core::rect<s32>(RelativeRect.getWidth()-80, 30, RelativeRect.getWidth()-10, 50),
88 this, -1, skin ? skin->getDefaultText(EGDT_MSG_BOX_OK) : L"OK");
89 OKButton->setSubElement(true);
90 OKButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
91 OKButton->grab();
92
93 CancelButton = Environment->addButton(
94 core::rect<s32>(RelativeRect.getWidth()-80, 55, RelativeRect.getWidth()-10, 75),
95 this, -1, skin ? skin->getDefaultText(EGDT_MSG_BOX_CANCEL) : L"Cancel");
96 CancelButton->setSubElement(true);
97 CancelButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
98 CancelButton->grab();
99
100 video::IVideoDriver* driver = Environment->getVideoDriver();
101 ColorRing.Texture = driver->getTexture ( "#colorring" );
102 if ( 0 == ColorRing.Texture )
103 {
104 buildColorRing(core::dimension2d<u32>(128, 128), 1,
105 Environment->getSkin()->getColor(EGDC_3D_SHADOW));
106 }
107
108 core::rect<s32> r(20,20, 0,0);
109
110 ColorRing.Control = Environment->addImage(ColorRing.Texture, r.UpperLeftCorner, true, this);
111 ColorRing.Control->setSubElement(true);
112 ColorRing.Control->grab();
113
114 for ( u32 i = 0; i != sizeof (Template) / sizeof ( subElementPredefines ); ++i )
115 {
116 if ( Template[i].pre )
117 {
118 r.UpperLeftCorner.X = Template[i].x;
119 r.UpperLeftCorner.Y = Template[i].y;
120 r.LowerRightCorner.X = r.UpperLeftCorner.X + 15;
121 r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 20;
122 IGUIElement *t = Environment->addStaticText(Template[i].pre, r, false, false, this);
123 t->setSubElement(true);
124 }
125
126 if ( Template[i].post )
127 {
128 r.UpperLeftCorner.X = Template[i].x + 56;
129 r.UpperLeftCorner.Y = Template[i].y;
130 r.LowerRightCorner.X = r.UpperLeftCorner.X + 15;
131 r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 20;
132 IGUIElement *t = Environment->addStaticText( Template[i].post, r, false, false, this);
133 t->setSubElement(true);
134 }
135
136 r.UpperLeftCorner.X = Template[i].x + 15;
137 r.UpperLeftCorner.Y = Template[i].y-2;
138 r.LowerRightCorner.X = r.UpperLeftCorner.X + 40;
139 r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 20;
140
141 gui::IGUISpinBox* spin = Environment->addSpinBox( Template[i].init, r, true, this);
142 spin->setSubElement(true);
143 spin->setDecimalPlaces(0);
144 spin->setRange((f32)Template[i].range_down, (f32)Template[i].range_up);
145 spin->grab();
146
147 Battery.push_back(spin);
148 }
149
150 bringToFront(CancelButton);
151 bringToFront(OKButton);
152}
153
154
155//! destructor
156CGUIColorSelectDialog::~CGUIColorSelectDialog()
157{
158 if (CloseButton)
159 CloseButton->drop();
160
161 if (OKButton)
162 OKButton->drop();
163
164 if (CancelButton)
165 CancelButton->drop();
166
167 for (u32 i = 0; i != Battery.size(); ++i)
168 Battery[i]->drop();
169
170 if (ColorRing.Control)
171 ColorRing.Control->drop();
172}
173
174
175//! renders a antialiased, colored ring
176void CGUIColorSelectDialog::buildColorRing( const core::dimension2d<u32> & dim, s32 supersample, const video::SColor& borderColor )
177{
178 const core::dimension2d<u32> d(dim.Width * supersample, dim.Height * supersample);
179 video::IVideoDriver* driver = Environment->getVideoDriver();
180
181 video::IImage *RawTexture = driver->createImage(video::ECF_A8R8G8B8, d);
182
183 RawTexture->fill ( 0x00808080 );
184
185 const s32 radiusOut = ( d.Width / 2 ) - 4;
186 const s32 fullR2 = radiusOut * radiusOut;
187
188 video::SColorf rgb(0,0,0);
189 video::SColorHSL hsl;
190 hsl.Luminance = 50;
191 hsl.Saturation = 100;
192
193 core::position2d<s32> p;
194 for ( p.Y = -radiusOut; p.Y <= radiusOut; p.Y += 1 )
195 {
196 s32 y2 = p.Y * p.Y;
197
198 for (p.X = -radiusOut; p.X <= radiusOut; p.X += 1)
199 {
200 s32 r2 = y2 + ( p.X * p.X );
201
202 // test point in circle
203 s32 testa = r2 - fullR2;
204
205 if ( testa < 0 )
206 {
207 // dotproduct u ( x,y ) * v ( 1, 0 ) = cosinus(a)
208
209 const f32 r = sqrtf((f32) r2);
210
211 // normalize, dotproduct = xnorm
212 const f32 xn = r == 0.f ? 0.f : -p.X * core::reciprocal(r);
213
214 hsl.Hue = acosf(xn)*core::RADTODEG;
215 if ( p.Y > 0 )
216 hsl.Hue = 360 - hsl.Hue;
217 hsl.Hue -= 90;
218
219 const f32 rTest = r / radiusOut;
220#if 0
221 if (rTest < 0.33f)
222 {
223 // luminance from 0 to 50
224 hsl.Luminance = 50*(rTest/0.33);
225 hsl.Saturation = 0.f;
226 hsl.toRGB(rgb);
227 }
228 else
229 if ( rTest < 0.66f )
230 {
231 // saturation from 0 to 100
232 hsl.Saturation = 100*(( rTest - 0.33f ) / 0.33f);
233 hsl.Luminance = 50;
234 hsl.toRGB(rgb);
235 }
236 else
237 {
238 // luminance from 50 to 100
239 hsl.Luminance = 100*(0.5f + ( ( rTest - 0.66f ) / .66f ));
240 hsl.Saturation = 100;
241 hsl.toRGB(rgb);
242 }
243 // borders should be slightly transparent
244 if ( rTest >= 0.95f )
245 rgb.a = (1.f-rTest)*20;
246 else
247 rgb.a=1.f;
248#else
249 if ( rTest > 0.5f )
250 {
251 hsl.Saturation = 100;
252 hsl.Luminance = 50;
253 hsl.toRGB(rgb);
254 }
255 // borders should be slightly transparent
256 if ( rTest < 0.5f )
257 rgb.a = 0;
258 else if ( rTest >= 0.95f )
259 rgb.a = (1.f-rTest)*20;
260 else if ( rTest <= 0.55f )
261 rgb.a = (rTest-0.5f)*20;
262 else
263 rgb.a=1.f;
264#endif
265 RawTexture->setPixel(4+p.X+radiusOut, 4+p.Y+radiusOut, rgb.toSColor());
266 }
267 }
268 }
269
270 RawTexture->unlock ();
271
272 if ( supersample > 1 )
273 {
274 video::IImage * filter = driver->createImage(video::ECF_A8R8G8B8, dim );
275 RawTexture->copyToScalingBoxFilter(filter);
276 RawTexture->drop();
277 RawTexture = filter;
278 }
279
280 bool generateMipLevels = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
281 driver->setTextureCreationFlag( video::ETCF_CREATE_MIP_MAPS, false);
282
283 ColorRing.Texture = driver->addTexture ( "#colorring", RawTexture);
284 RawTexture->drop();
285
286 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, generateMipLevels);
287}
288
289
290//! called if an event happened.
291bool CGUIColorSelectDialog::OnEvent(const SEvent& event)
292{
293 if (isEnabled())
294 {
295 switch(event.EventType)
296 {
297 case EET_GUI_EVENT:
298 switch(event.GUIEvent.EventType)
299 {
300 case EGET_SPINBOX_CHANGED:
301 {
302 for ( u32 i = 0; i!= Battery.size (); ++i )
303 {
304 if ( event.GUIEvent.Caller == Battery[i] )
305 {
306 if (i<4)
307 {
308 video::SColor rgb((u32)Battery[0]->getValue(), (u32)Battery[1]->getValue(),
309 (u32)Battery[2]->getValue(), (u32)Battery[3]->getValue());
310 video::SColorHSL hsl;
311 video::SColorf rgb2(rgb);
312 hsl.fromRGB(rgb2);
313 Battery[4]->setValue(hsl.Hue);
314 Battery[5]->setValue(hsl.Saturation);
315 Battery[6]->setValue(hsl.Luminance);
316 }
317 else
318 {
319 video::SColorHSL hsl(Battery[4]->getValue(), Battery[5]->getValue(),
320 Battery[6]->getValue());
321 video::SColorf rgb2;
322 hsl.toRGB(rgb2);
323 video::SColor rgb = rgb2.toSColor();
324 Battery[1]->setValue((f32)rgb.getRed());
325 Battery[2]->setValue((f32)rgb.getGreen());
326 Battery[3]->setValue((f32)rgb.getBlue());
327 }
328 }
329 }
330 return true;
331 }
332
333 case EGET_ELEMENT_FOCUS_LOST:
334 Dragging = false;
335 break;
336 case EGET_BUTTON_CLICKED:
337 if (event.GUIEvent.Caller == CloseButton ||
338 event.GUIEvent.Caller == CancelButton)
339 {
340 sendCancelEvent();
341 remove();
342 return true;
343 }
344 else
345 if (event.GUIEvent.Caller == OKButton)
346 {
347 sendSelectedEvent();
348 remove();
349 return true;
350 }
351 break;
352
353 case EGET_LISTBOX_CHANGED:
354 case EGET_LISTBOX_SELECTED_AGAIN:
355 default:
356 break;
357 }
358 break;
359 case EET_MOUSE_INPUT_EVENT:
360 switch(event.MouseInput.Event)
361 {
362 case EMIE_LMOUSE_PRESSED_DOWN:
363 DragStart.X = event.MouseInput.X;
364 DragStart.Y = event.MouseInput.Y;
365 Dragging = true;
366 Environment->setFocus(this);
367 return true;
368 case EMIE_LMOUSE_LEFT_UP:
369 Dragging = false;
370 Environment->removeFocus(this);
371 return true;
372 case EMIE_MOUSE_MOVED:
373 if (Dragging)
374 {
375 // gui window should not be dragged outside its parent
376 if (Parent)
377 if (event.MouseInput.X < Parent->getAbsolutePosition().UpperLeftCorner.X +1 ||
378 event.MouseInput.Y < Parent->getAbsolutePosition().UpperLeftCorner.Y +1 ||
379 event.MouseInput.X > Parent->getAbsolutePosition().LowerRightCorner.X -1 ||
380 event.MouseInput.Y > Parent->getAbsolutePosition().LowerRightCorner.Y -1)
381
382 return true;
383
384 move(core::position2d<s32>(event.MouseInput.X - DragStart.X, event.MouseInput.Y - DragStart.Y));
385 DragStart.X = event.MouseInput.X;
386 DragStart.Y = event.MouseInput.Y;
387 return true;
388 }
389 default:
390 break;
391 }
392 default:
393 break;
394 }
395 }
396
397 return IGUIElement::OnEvent(event);
398}
399
400
401//! draws the element and its children
402void CGUIColorSelectDialog::draw()
403{
404 if (!IsVisible)
405 return;
406
407 IGUISkin* skin = Environment->getSkin();
408 core::rect<s32> rect = skin->draw3DWindowBackground(this, true, skin->getColor(EGDC_ACTIVE_BORDER),
409 AbsoluteRect, &AbsoluteClippingRect);
410
411 if (Text.size())
412 {
413 rect.UpperLeftCorner.X += 2;
414 rect.LowerRightCorner.X -= skin->getSize(EGDS_WINDOW_BUTTON_WIDTH) + 5;
415
416 IGUIFont* font = skin->getFont(EGDF_WINDOW);
417 if (font)
418 font->draw(Text.c_str(), rect, skin->getColor(EGDC_ACTIVE_CAPTION), false, true,
419 &AbsoluteClippingRect);
420 }
421
422 IGUIElement::draw();
423
424 // draw color selector after the window elements
425 core::vector2di pos(ColorRing.Control->getAbsolutePosition().UpperLeftCorner);
426 pos.X += ColorRing.Texture->getOriginalSize().Width/2;
427 pos.Y += ColorRing.Texture->getOriginalSize().Height/2;
428#if 0
429 const f32 h = Battery[4]->getValue();
430 const f32 s = Battery[5]->getValue();
431 const f32 l = Battery[6]->getValue();
432 const f32 factor = 58.f*(((s==0)&&(l<50))?(l*0.33f/50):(
433 (s<100)?((.33f+(s*0.33f/100))):((0.66f+(l-50)*0.33f/50))));
434
435#else
436 const f32 factor = 44;
437#endif
438 pos.X += core::round32(sinf(Battery[4]->getValue()*core::DEGTORAD)*factor);
439 pos.Y -= core::round32(cosf(Battery[4]->getValue()*core::DEGTORAD)*factor);
440 Environment->getVideoDriver()->draw2DPolygon(pos, 4, 0xffffffff, 4);
441}
442
443
444video::SColor CGUIColorSelectDialog::getColor()
445{
446 return video::SColor((u32)Battery[0]->getValue(), (u32)Battery[1]->getValue(),
447 (u32)Battery[2]->getValue(), (u32)Battery[3]->getValue());
448}
449
450video::SColorHSL CGUIColorSelectDialog::getColorHSL()
451{
452 return video::SColorHSL(Battery[4]->getValue(), Battery[5]->getValue(),
453 Battery[6]->getValue());
454}
455
456//! sends the event that the file has been selected.
457void CGUIColorSelectDialog::sendSelectedEvent()
458{
459 SEvent event;
460 event.EventType = EET_GUI_EVENT;
461 event.GUIEvent.Caller = this;
462 event.GUIEvent.Element = 0;
463 event.GUIEvent.EventType = EGET_FILE_SELECTED;
464 Parent->OnEvent(event);
465}
466
467
468//! sends the event that the file choose process has been canceld
469void CGUIColorSelectDialog::sendCancelEvent()
470{
471 SEvent event;
472 event.EventType = EET_GUI_EVENT;
473 event.GUIEvent.Caller = this;
474 event.GUIEvent.Element = 0;
475 event.GUIEvent.EventType = EGET_FILE_CHOOSE_DIALOG_CANCELLED;
476 Parent->OnEvent(event);
477}
478
479
480} // end namespace gui
481} // end namespace irr
482
483#endif // _IRR_COMPILE_WITH_GUI_