1 #ifndef __C_GUI_COLOR_ATTRIBUTE_H_INCLUDED__
2 #define __C_GUI_COLOR_ATTRIBUTE_H_INCLUDED__
3 
4 #include "CGUIAttribute.h"
5 #include "IGUIStaticText.h"
6 #include "IGUIScrollBar.h"
7 #include "IGUITabControl.h"
8 #include "EGUIEditTypes.h"
9 
10 namespace irr
11 {
12 namespace gui
13 {
14 	class CGUIColorAttribute : public CGUIAttribute
15 	{
16 	public:
17 		//
CGUIColorAttribute(IGUIEnvironment * environment,IGUIElement * parent,s32 myParentID)18 		CGUIColorAttribute(IGUIEnvironment* environment, IGUIElement *parent, s32 myParentID) :
19 		  	CGUIAttribute(environment, parent, myParentID),
20 			AttribSliderA(0), AttribSliderR(0), AttribSliderG(0), AttribSliderB(0),
21 			AttribEditBox(0), AttribColor(0)
22 		{
23 			s32 fh = Environment->getSkin()->getFont()->getDimension(L"A").Height;
24 
25 			core::rect<s32> r0(getAbsolutePosition()),
26 					r2(0, fh + 5, r0.getWidth() - 5, fh*2 + 10 ),
27 					r3(r2),
28 					r4(r2.getWidth() - 20, 3, r2.getWidth() - 3, r2.getHeight()-3);
29 
30 			AttribColor = Environment->addTab(r4, this, 0);
31 			AttribColor->grab();
32 			AttribColor->setDrawBackground(true);
33 			AttribColor->setSubElement(true);
34 			AttribColor->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
35 
36 			s32 h=2;
37 			r2 += core::position2di(0, h*4 + Environment->getSkin()->getSize(EGDS_WINDOW_BUTTON_WIDTH)*2);
38 			r3.LowerRightCorner.Y = r3.UpperLeftCorner.Y + Environment->getSkin()->getSize(EGDS_WINDOW_BUTTON_WIDTH)/2;
39 
40 			AttribSliderA = environment->addScrollBar(true, r3, this, -1);
41 			AttribSliderA->setMax(255);
42 			AttribSliderA->grab();
43 			AttribSliderA->setSubElement(true);
44 			AttribSliderA->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
45 			r3 += core::position2di(0, r3.getHeight()+h);
46 			AttribSliderR = environment->addScrollBar(true, r3, this, -1);
47 			AttribSliderR->setMax(255);
48 			AttribSliderR->grab();
49 			AttribSliderR->setSubElement(true);
50 			AttribSliderR->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
51 			r3 += core::position2di(0, r3.getHeight()+h);
52 			AttribSliderG = environment->addScrollBar(true, r3, this, -1);
53 			AttribSliderG->setMax(255);
54 			AttribSliderG->grab();
55 			AttribSliderG->setSubElement(true);
56 			AttribSliderG->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
57 			r3 += core::position2di(0, r3.getHeight()+h);
58 			AttribSliderB = environment->addScrollBar(true, r3, this, -1);
59 			AttribSliderB->setMax(255);
60 			AttribSliderB->grab();
61 			AttribSliderB->setSubElement(true);
62 			AttribSliderB->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
63 
64 			// add editbox
65 			AttribEditBox = environment->addEditBox(
66 					L"",
67 					r2,
68 					true, this, -1);
69 			AttribEditBox->grab();
70 			AttribEditBox->setSubElement(true);
71 			AttribEditBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
72 		}
73 
~CGUIColorAttribute()74 		virtual ~CGUIColorAttribute()
75 		{
76 			if (AttribSliderA)
77 				AttribSliderA->drop();
78 			if (AttribSliderR)
79 				AttribSliderR->drop();
80 			if (AttribSliderG)
81 				AttribSliderG->drop();
82 			if (AttribSliderB)
83 				AttribSliderB->drop();
84 			if (AttribEditBox)
85 				AttribEditBox->drop();
86 			if (AttribColor)
87 				AttribColor->drop();
88 		}
89 
setAttrib(io::IAttributes * attribs,u32 attribIndex)90 		virtual void setAttrib(io::IAttributes *attribs, u32 attribIndex)
91 		{
92 			video::SColor col = attribs->getAttributeAsColor(attribIndex);
93 
94 			AttribSliderA->setPos(col.getAlpha());
95 			AttribSliderR->setPos(col.getRed());
96 			AttribSliderG->setPos(col.getGreen());
97 			AttribSliderB->setPos(col.getBlue());
98 			AttribEditBox->setText( attribs->getAttributeAsStringW(attribIndex).c_str() );
99 			AttribColor->setBackgroundColor(col);
100 
101 			CGUIAttribute::setAttrib(attribs, attribIndex);
102 		}
103 
OnEvent(const SEvent & e)104 		virtual bool OnEvent(const SEvent &e)
105 		{
106 			switch (e.EventType)
107 			{
108 
109 			case EET_GUI_EVENT:
110 				switch (e.GUIEvent.EventType)
111 				{
112 				case EGET_EDITBOX_ENTER:
113 				case EGET_ELEMENT_FOCUS_LOST:
114 					if (e.GUIEvent.Caller == AttribEditBox)
115 					{
116 						// update scrollbars from textbox
117 						Attribs->setAttribute(Index, AttribEditBox->getText());
118 						video::SColor col = Attribs->getAttributeAsColor(Index);
119 						AttribSliderA->setPos(col.getAlpha());
120 						AttribSliderR->setPos(col.getRed());
121 						AttribSliderG->setPos(col.getGreen());
122 						AttribSliderB->setPos(col.getBlue());
123 						// update colour
124 						AttribColor->setBackgroundColor(col);
125 					}
126 					break;
127 				case EGET_SCROLL_BAR_CHANGED:
128 					{
129 						// update editbox from scrollbars
130 						video::SColor col( AttribSliderA->getPos(), AttribSliderR->getPos(),
131 							AttribSliderG->getPos(), AttribSliderB->getPos());
132 
133 						Attribs->setAttribute(Index, col);
134 						AttribEditBox->setText( Attribs->getAttributeAsStringW(Index).c_str());
135 						// update colour
136 						AttribColor->setBackgroundColor(col);
137 					}
138 					return updateAttrib();
139 				default:
140 					break;
141 				}
142 				break;
143 			default:
144 				break;
145 			}
146 			return CGUIAttribute::OnEvent(e);
147 		}
148 
149 		// save the attribute and possibly post the event to its parent
150 		virtual bool updateAttrib(bool sendEvent=true)
151 		{
152 			if (!Attribs)
153 				return true;
154 
155 			Attribs->setAttribute(Index, AttribEditBox->getText());
156 			AttribEditBox->setText(Attribs->getAttributeAsStringW(Index).c_str());
157 			return CGUIAttribute::updateAttrib(sendEvent);
158 		}
159 
160 		//! Returns the type name of the gui element.
getTypeName()161 		virtual const c8* getTypeName() const
162 		{
163 			return GUIEditElementTypeNames[EGUIEDIT_COLORATTRIBUTE];
164 		}
165 
166 	private:
167 		IGUIScrollBar*		AttribSliderA;
168 		IGUIScrollBar*		AttribSliderR;
169 		IGUIScrollBar*		AttribSliderG;
170 		IGUIScrollBar*		AttribSliderB;
171 		IGUIEditBox*		AttribEditBox;
172 		IGUITab*		AttribColor;
173 	};
174 
175 } // namespace gui
176 } // namespace irr
177 
178 #endif
179 
180