]> git.lizzy.rs Git - irrlicht.git/blob - tools/GUIEditor/CGUIAttribute.h
05854980ead7737c67148d19700cbda156fd3f1e
[irrlicht.git] / tools / GUIEditor / CGUIAttribute.h
1 /*\r
2         This base class is used by the Attribute editor for making your own attribute types.\r
3 \r
4         The attribute editor will try and create an attribute called "AttribType_attribute",\r
5         and if it fails, it will create a "string_attribute".\r
6 \r
7 */\r
8 \r
9 #ifndef __C_GUI_ATTRIBUTE_H_INCLUDED__\r
10 #define __C_GUI_ATTRIBUTE_H_INCLUDED__\r
11 \r
12 #include "IGUIElement.h"\r
13 #include "IGUIEnvironment.h"\r
14 #include "IGUIFont.h"\r
15 #include "IGUIStaticText.h"\r
16 #include "IAttributes.h"\r
17 #include "CGUIEditWorkspace.h"\r
18 \r
19 namespace irr\r
20 {\r
21 \r
22 namespace gui\r
23 {\r
24 \r
25         const u32 ATTRIBEDIT_ATTRIB_CHANGED=MAKE_IRR_ID('A','T','T','R');\r
26 \r
27         class CGUIAttribute : public IGUIElement\r
28         {\r
29         public:\r
30                 //! constructor\r
31                 CGUIAttribute(IGUIEnvironment* environment, IGUIElement *parent, s32 myParentID) :\r
32                         IGUIElement(EGUIET_ELEMENT, environment, parent, -1, core::rect<s32>(0, 0, 100, 100) ),\r
33                         AttribName(0), Attribs(0), Index(0), MyParentID(myParentID)\r
34                 {\r
35 \r
36                         #ifdef _DEBUG\r
37                         setDebugName("CGUIAttribute");\r
38                         #endif\r
39 \r
40                         AttribName = environment->addStaticText(0,\r
41                                 core::rect<s32>(0, 0,\r
42                                         100, Environment->getSkin()->getFont()->getDimension(L"A").Height),\r
43                                         false, false, this, -1, false);\r
44                         AttribName->grab();\r
45                         AttribName->setSubElement(true);\r
46                         AttribName->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);\r
47                 }\r
48 \r
49                 virtual ~CGUIAttribute()\r
50                 {\r
51                         if (Attribs)\r
52                                 Attribs->drop();\r
53                         if (AttribName)\r
54                                 AttribName->drop();\r
55                 }\r
56 \r
57                 virtual bool OnEvent(const SEvent &e)\r
58                 {\r
59                         if (IsEnabled)\r
60                         {\r
61                                 switch (e.EventType)\r
62                                 {\r
63                                 case EET_GUI_EVENT:\r
64                                         switch (e.GUIEvent.EventType)\r
65                                         {\r
66                                         case EGET_ELEMENT_FOCUSED:\r
67                                                 if (Parent && isMyChild(e.GUIEvent.Caller))\r
68                                                         Parent->bringToFront(this);\r
69                                                 break;\r
70                                         case EGET_ELEMENT_HOVERED:\r
71                                         case EGET_ELEMENT_LEFT:\r
72                                                 return IGUIElement::OnEvent(e);\r
73                                         case EGET_ELEMENT_FOCUS_LOST:\r
74                                                 updateAttrib();\r
75                                                 return IGUIElement::OnEvent(e);\r
76                                         default:\r
77                                                 return updateAttrib();\r
78                                         }\r
79                                         break;\r
80                                 case EET_KEY_INPUT_EVENT:\r
81                                         return true;\r
82                                 default:\r
83                                         break;\r
84                                 }\r
85                         }\r
86 \r
87                         return IGUIElement::OnEvent(e);\r
88                 }\r
89 \r
90                 //! sets the attribute to use\r
91                 virtual void setAttrib(io::IAttributes *attribs, u32 attribIndex)\r
92                 {\r
93                         if (Attribs)\r
94                                 Attribs->drop();\r
95                         Attribs = attribs;\r
96                         if (Attribs)\r
97                                 Attribs->grab();\r
98                         Index = attribIndex;\r
99 \r
100                         core::stringw name(attribs->getAttributeName(attribIndex));\r
101                         name += L" (";\r
102                         name += attribs->getAttributeTypeString(attribIndex);\r
103                         name += L")";\r
104                         AttribName->setText(name.c_str());\r
105 \r
106                         core::rect<s32> r = Parent->getAbsolutePosition();\r
107                         core::rect<s32> r2(0, 5,\r
108                                 r.getWidth(), Environment->getSkin()->getFont()->getDimension(L"A").Height + 10 );\r
109 \r
110                         AttribName->setRelativePosition(r2);\r
111 \r
112                         // get minimum height\r
113                         s32 y=0;\r
114                         core::list<IGUIElement*>::Iterator it = Children.begin();\r
115                         for (; it != Children.end(); ++it)\r
116                         {\r
117                                 if (y < (*it)->getRelativePosition().LowerRightCorner.Y)\r
118                                         y = (*it)->getRelativePosition().LowerRightCorner.Y;\r
119                         }\r
120                         setMinSize( core::dimension2du(0, y+5));\r
121 \r
122                         updateAttrib(false);\r
123                 }\r
124 \r
125                 //! sets the parent ID, for identifying where events came from\r
126                 void setParentID(s32 parentID)\r
127                 {\r
128                         MyParentID = parentID;\r
129                 }\r
130 \r
131                 //! save the attribute and possibly post the event to its parent\r
132                 virtual bool updateAttrib(bool sendEvent=true)\r
133                 {\r
134                         if (Attribs && IsEnabled && sendEvent)\r
135                         {\r
136                                 // build event and pass to parent\r
137                                 SEvent event;\r
138                                 event.EventType = (EEVENT_TYPE)ATTRIBEDIT_ATTRIB_CHANGED;\r
139                                 event.UserEvent.UserData1 = MyParentID;\r
140                                 event.UserEvent.UserData2 = Index;\r
141                                 return Parent->OnEvent(event);\r
142                         }\r
143 \r
144                         return true;\r
145                 }\r
146 \r
147                 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0)\r
148                 {\r
149                         IGUIElement::serializeAttributes(out, options);\r
150                 }\r
151 \r
152                 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)\r
153                 {\r
154                         IGUIElement::deserializeAttributes(in, options);\r
155                         if (AttribName)\r
156                                 AttribName->setText(Text.c_str());\r
157                 }\r
158 \r
159         protected:\r
160                 IGUIStaticText*         AttribName;\r
161                 io::IAttributes*        Attribs;\r
162                 u32                     Index;\r
163                 s32                     MyParentID;\r
164         };\r
165 \r
166 } // namespace gui\r
167 } // namespace irr\r
168 \r
169 #endif\r