1 #ifndef __C_GUI_TEXTURE_ATTRIBUTE_H_INCLUDED__
2 #define __C_GUI_TEXTURE_ATTRIBUTE_H_INCLUDED__
3 
4 #include "CGUIAttribute.h"
5 #include "IGUIEditBox.h"
6 #include "IGUIImage.h"
7 #include "IGUIButton.h"
8 #include "EGUIEditTypes.h"
9 
10 namespace irr
11 {
12 namespace gui
13 {
14 
15 	class CGUITextureAttribute : public CGUIAttribute
16 	{
17 	public:
18 		//
CGUITextureAttribute(IGUIEnvironment * environment,IGUIElement * parent,s32 myParentID)19 		CGUITextureAttribute(IGUIEnvironment* environment, IGUIElement *parent, s32 myParentID) :
20 			CGUIAttribute(environment, parent, myParentID),
21 			AttribEditBox(0), AttribImage(0), AttribButton(0)
22 		{
23 			IGUISkin* skin = Environment->getSkin();
24 
25 			core::rect<s32> r = getAbsolutePosition();
26 			s32 topy = skin->getFont()->getDimension(L"A").Height + 10;
27 			s32 h = skin->getFont()->getDimension(L"A").Height + 5;
28 
29 			AttribImage = environment->addImage(0, core::position2di(0, topy), false, this);
30 			AttribImage->setRelativePosition( core::rect<s32>(0,topy, r.getWidth() - 5, 100+topy));
31 			AttribImage->grab();
32 			AttribImage->setSubElement(true);
33 			AttribImage->setScaleImage(true);
34 			AttribImage->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
35 
36 			topy += 105;
37 
38 			core::rect<s32> r2(0, topy, r.getWidth() - 15 - skin->getSize(EGDS_CHECK_BOX_WIDTH), topy + h);
39 			core::rect<s32> br(r.getWidth() - 10 - skin->getSize(EGDS_CHECK_BOX_WIDTH), topy, r.getWidth(), topy + h);
40 
41 			AttribEditBox = environment->addEditBox(0, r2, true, this, -1);
42 			AttribEditBox->grab();
43 			AttribEditBox->setSubElement(true);
44 			AttribEditBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
45 
46 			AttribButton = environment->addButton(br, this, -1, L"...");
47 			AttribButton->grab();
48 			AttribButton->setSubElement(true);
49 			AttribButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
50 			//AttribButton->setSpriteBank(skin->getSpriteBank());
51 			//AttribButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_FILE), skin->getColor(EGDC_WINDOW_SYMBOL));
52 			//AttribButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_FILE), skin->getColor(EGDC_WINDOW_SYMBOL), true);
53 		}
54 
~CGUITextureAttribute()55 		virtual ~CGUITextureAttribute()
56 		{
57 			if (AttribEditBox)
58 				AttribEditBox->drop();
59 			if (AttribImage)
60 				AttribImage->drop();
61 			if (AttribButton)
62 				AttribButton->drop();
63 		}
64 
OnEvent(const SEvent & e)65 		virtual bool OnEvent(const SEvent &e)
66 		{
67 
68 			if (IsEnabled)
69 			{
70 				switch (e.EventType)
71 				{
72 				case EET_GUI_EVENT:
73 					switch (e.GUIEvent.EventType)
74 					{
75 					case EGET_BUTTON_CLICKED:
76 						// button click: open file dialog
77 						if (e.GUIEvent.Caller == AttribButton)
78 						{
79 							//Environment->addGUIElement("textureBrowser", Environment->getRootGUIElement());
80 							return true;
81 						}
82 						break;
83 					case EGET_FILE_SELECTED:
84 						// file selected: change editbox value and set event
85 
86 						return true;
87 					case EGET_FILE_CHOOSE_DIALOG_CANCELLED:
88 
89 						return true;
90 					default:
91 						break;
92 					}
93 					break;
94 				case EET_KEY_INPUT_EVENT:
95 					return true;
96 				default:
97 					break;
98 				}
99 			}
100 			return CGUIAttribute::OnEvent(e);
101 		}
102 
setAttrib(io::IAttributes * attribs,u32 attribIndex)103 		virtual void setAttrib(io::IAttributes *attribs, u32 attribIndex)
104 		{
105 			AttribEditBox->setText(attribs->getAttributeAsStringW(attribIndex).c_str());
106 			AttribImage->setImage(attribs->getAttributeAsTexture(Index));
107 
108 			CGUIAttribute::setAttrib(attribs, attribIndex);
109 		}
110 
111 		//! save the attribute and possibly post the event to its parent
112 		virtual bool updateAttrib(bool sendEvent=true)
113 		{
114 			if (!Attribs)
115 				return true;
116 
117 			Attribs->setAttribute(Index, AttribEditBox->getText());
118 			core::stringw tmp = Attribs->getAttributeAsStringW(Index);
119 			AttribEditBox->setText(Attribs->getAttributeAsStringW(Index).c_str());
120 			AttribImage->setImage(Attribs->getAttributeAsTexture(Index));
121 
122 			return CGUIAttribute::updateAttrib(sendEvent);
123 		}
124 
125 		//! Returns the type name of the gui element.
getTypeName()126 		virtual const c8* getTypeName() const
127 		{
128 			return GUIEditElementTypeNames[EGUIEDIT_TEXTUREATTRIBUTE];
129 		}
130 
131 	private:
132 		IGUIEditBox*		AttribEditBox;
133 		IGUIImage*			AttribImage;
134 		IGUIButton*			AttribButton;
135 	};
136 
137 } // namespace gui
138 } // namespace irr
139 
140 #endif
141