~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tools/GUIEditor/CGUIAttribute.h

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

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