aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/tools/GUIEditor/CGUIAttribute.h
blob: 05854980ead7737c67148d19700cbda156fd3f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
	This base class is used by the Attribute editor for making your own attribute types.

	The attribute editor will try and create an attribute called "AttribType_attribute",
	and if it fails, it will create a "string_attribute".

*/

#ifndef __C_GUI_ATTRIBUTE_H_INCLUDED__
#define __C_GUI_ATTRIBUTE_H_INCLUDED__

#include "IGUIElement.h"
#include "IGUIEnvironment.h"
#include "IGUIFont.h"
#include "IGUIStaticText.h"
#include "IAttributes.h"
#include "CGUIEditWorkspace.h"

namespace irr
{

namespace gui
{

	const u32 ATTRIBEDIT_ATTRIB_CHANGED=MAKE_IRR_ID('A','T','T','R');

	class CGUIAttribute : public IGUIElement
	{
	public:
		//! constructor
		CGUIAttribute(IGUIEnvironment* environment, IGUIElement *parent, s32 myParentID) :
			IGUIElement(EGUIET_ELEMENT, environment, parent, -1, core::rect<s32>(0, 0, 100, 100) ),
			AttribName(0), Attribs(0), Index(0), MyParentID(myParentID)
		{

			#ifdef _DEBUG
			setDebugName("CGUIAttribute");
			#endif

			AttribName = environment->addStaticText(0,
				core::rect<s32>(0, 0,
					100, Environment->getSkin()->getFont()->getDimension(L"A").Height),
					false, false, this, -1, false);
			AttribName->grab();
			AttribName->setSubElement(true);
			AttribName->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
		}

		virtual ~CGUIAttribute()
		{
			if (Attribs)
				Attribs->drop();
			if (AttribName)
				AttribName->drop();
		}

		virtual bool OnEvent(const SEvent &e)
		{
			if (IsEnabled)
			{
				switch (e.EventType)
				{
				case EET_GUI_EVENT:
					switch (e.GUIEvent.EventType)
					{
					case EGET_ELEMENT_FOCUSED:
						if (Parent && isMyChild(e.GUIEvent.Caller))
							Parent->bringToFront(this);
						break;
					case EGET_ELEMENT_HOVERED:
					case EGET_ELEMENT_LEFT:
						return IGUIElement::OnEvent(e);
					case EGET_ELEMENT_FOCUS_LOST:
						updateAttrib();
						return IGUIElement::OnEvent(e);
					default:
						return updateAttrib();
					}
					break;
				case EET_KEY_INPUT_EVENT:
					return true;
				default:
					break;
				}
			}

			return IGUIElement::OnEvent(e);
		}

		//! sets the attribute to use
		virtual void setAttrib(io::IAttributes *attribs, u32 attribIndex)
		{
			if (Attribs)
				Attribs->drop();
			Attribs = attribs;
			if (Attribs)
				Attribs->grab();
			Index = attribIndex;

			core::stringw name(attribs->getAttributeName(attribIndex));
			name += L" (";
			name += attribs->getAttributeTypeString(attribIndex);
			name += L")";
			AttribName->setText(name.c_str());

			core::rect<s32> r = Parent->getAbsolutePosition();
			core::rect<s32> r2(0, 5,
				r.getWidth(), Environment->getSkin()->getFont()->getDimension(L"A").Height + 10 );

			AttribName->setRelativePosition(r2);

			// get minimum height
			s32 y=0;
			core::list<IGUIElement*>::Iterator it = Children.begin();
			for (; it != Children.end(); ++it)
			{
				if (y < (*it)->getRelativePosition().LowerRightCorner.Y)
					y = (*it)->getRelativePosition().LowerRightCorner.Y;
			}
			setMinSize( core::dimension2du(0, y+5));

			updateAttrib(false);
		}

		//! sets the parent ID, for identifying where events came from
		void setParentID(s32 parentID)
		{
			MyParentID = parentID;
		}

		//! save the attribute and possibly post the event to its parent
		virtual bool updateAttrib(bool sendEvent=true)
		{
			if (Attribs && IsEnabled && sendEvent)
			{
				// build event and pass to parent
				SEvent event;
				event.EventType = (EEVENT_TYPE)ATTRIBEDIT_ATTRIB_CHANGED;
				event.UserEvent.UserData1 = MyParentID;
				event.UserEvent.UserData2 = Index;
				return Parent->OnEvent(event);
			}

			return true;
		}

		virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0)
		{
			IGUIElement::serializeAttributes(out, options);
		}

		virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
		{
			IGUIElement::deserializeAttributes(in, options);
			if (AttribName)
				AttribName->setText(Text.c_str());
		}

	protected:
		IGUIStaticText*		AttribName;
		io::IAttributes*	Attribs;
		u32			Index;
		s32			MyParentID;
	};

} // namespace gui
} // namespace irr

#endif