CGUITextureAttribute.h 4.1 KB

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