CGUIAttribute.h 4.2 KB

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