CGUIEditWindow.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "CGUIEditWindow.h"
  2. #include "IGUISkin.h"
  3. #include "IGUIEnvironment.h"
  4. #include "IGUIElementFactory.h"
  5. #include "IAttributes.h"
  6. #include "IGUIFont.h"
  7. #include "IGUITabControl.h"
  8. #include "IGUITreeView.h"
  9. #include "CGUIEditWorkspace.h"
  10. using namespace irr;
  11. using namespace gui;
  12. //! constructor
  13. CGUIEditWindow::CGUIEditWindow(IGUIEnvironment* environment, core::rect<s32> rectangle, IGUIElement *parent)
  14. : IGUIWindow(environment, parent, -1, rectangle),
  15. Dragging(false), IsDraggable(true), Resizing(false), SelectedElement(0),
  16. AttribEditor(0), OptionEditor(0), EnvEditor(0)
  17. {
  18. #ifdef _DEBUG
  19. setDebugName("CGUIEditWindow");
  20. #endif
  21. // we can't tab out of this window
  22. setTabGroup(true);
  23. // we can ctrl+tab to it
  24. setTabStop(true);
  25. // the tab order number is auto-assigned
  26. setTabOrder(-1);
  27. // set window text
  28. setText(L"GUI Editor");
  29. // return if we have no skin.
  30. IGUISkin *skin = environment->getSkin();
  31. if (!skin)
  32. return;
  33. core::rect<s32> dlgRect(50,50,250,500);
  34. setRelativePosition(dlgRect);
  35. setMinSize(core::dimension2du(200,200));
  36. s32 th = skin->getSize(EGDS_WINDOW_BUTTON_WIDTH);
  37. IGUITabControl *TabControl = environment->addTabControl(core::rect<s32>(1,th+5,dlgRect.getWidth()-1,dlgRect.getHeight()-1), this, false, true);
  38. TabControl->setSubElement(true);
  39. TabControl->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
  40. //TabControl->addTab(L"Tools");
  41. //L"Texture Cache Browser"
  42. //L"Font Browser"
  43. //L"Font Generator"
  44. //L"Sprite Editor"
  45. //Environment->addGUIElement("textureCacheBrowser", this);
  46. IGUITab* EditorTab = TabControl->addTab(L"Editor");
  47. OptionEditor = (CGUIAttributeEditor*) environment->addGUIElement("attributeEditor", EditorTab);
  48. OptionEditor->grab();
  49. OptionEditor->setID(EGUIEDCE_OPTION_EDITOR);
  50. OptionEditor->setRelativePositionProportional(core::rect<f32>(0.0f, 0.0f, 1.0f, 1.0f));
  51. OptionEditor->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
  52. if (Parent && Parent->getParent() == Environment->getRootGUIElement())
  53. {
  54. IGUITab* EnvTab = TabControl->addTab(L"Env");
  55. EnvEditor = (CGUIAttributeEditor*) environment->addGUIElement("attributeEditor", EnvTab);
  56. EnvEditor->grab();
  57. EnvEditor->setID(EGUIEDCE_ENV_EDITOR);
  58. EnvEditor->setRelativePositionProportional(core::rect<f32>(0.0f, 0.0f, 1.0f, 1.0f));
  59. EnvEditor->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
  60. }
  61. IGUITab* ElementTab = TabControl->addTab(L"Element");
  62. AttribEditor = (CGUIAttributeEditor*) environment->addGUIElement("attributeEditor", ElementTab);
  63. AttribEditor->grab();
  64. AttribEditor->setID(EGUIEDCE_ATTRIB_EDITOR);
  65. AttribEditor->setRelativePositionProportional(core::rect<f32>(0.0f, 0.0f, 1.0f, 1.0f));
  66. AttribEditor->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
  67. IGUITab* TreeTab = TabControl->addTab(L"Tree");
  68. TreeView = environment->addTreeView(core::rect<s32>(0,0,0,0), TreeTab);
  69. TreeView->setRelativePositionProportional(core::rect<f32>(0.0f, 0.0f, 1.0f, 1.0f));
  70. TreeView->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
  71. IGUITreeViewNode* treenode = TreeView->getRoot();
  72. //treenode->addChildFront(L"Elements");
  73. ResizeButton = environment->addButton(core::rect<s32>(dlgRect.getWidth()-(th+1),dlgRect.getHeight()-(th+1),dlgRect.getWidth()-1,dlgRect.getHeight()-1), this);
  74. ResizeButton->setDrawBorder(false);
  75. ResizeButton->setEnabled(false);
  76. ResizeButton->setSpriteBank(skin->getSpriteBank());
  77. ResizeButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_WINDOW_RESIZE), skin->getColor(EGDC_WINDOW_SYMBOL));
  78. ResizeButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_WINDOW_RESIZE), skin->getColor(EGDC_WINDOW_SYMBOL));
  79. ResizeButton->grab();
  80. ResizeButton->setSubElement(true);
  81. ResizeButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT);
  82. updateTree();
  83. }
  84. //! destructor
  85. CGUIEditWindow::~CGUIEditWindow()
  86. {
  87. // drop everything
  88. if (AttribEditor)
  89. AttribEditor->drop();
  90. if (EnvEditor)
  91. EnvEditor->drop();
  92. if (OptionEditor)
  93. OptionEditor->drop();
  94. if (ResizeButton)
  95. ResizeButton->drop();
  96. }
  97. IGUITreeView* CGUIEditWindow::getTreeView() const
  98. {
  99. return TreeView;
  100. }
  101. CGUIAttributeEditor* CGUIEditWindow::getEnvironmentEditor() const
  102. {
  103. return EnvEditor;
  104. }
  105. CGUIAttributeEditor* CGUIEditWindow::getAttributeEditor() const
  106. {
  107. return AttribEditor;
  108. }
  109. CGUIAttributeEditor* CGUIEditWindow::getOptionEditor() const
  110. {
  111. return OptionEditor;
  112. }
  113. IGUITreeViewNode* CGUIEditWindow::getTreeNode(IGUIElement* element, IGUITreeViewNode* searchnode)
  114. {
  115. IGUITreeViewNode* child = searchnode->getFirstChild();
  116. while (child)
  117. {
  118. if (((IGUIElement*) child->getData()) == element)
  119. return child;
  120. if (child->hasChildren())
  121. {
  122. IGUITreeViewNode* foundnode = getTreeNode(element, child);
  123. if (foundnode)
  124. return foundnode;
  125. }
  126. child = child->getNextSibling();
  127. }
  128. return 0;
  129. }
  130. void CGUIEditWindow::addChildrenToTree(IGUIElement* parentElement, IGUITreeViewNode* treenode)
  131. {
  132. core::stringw name = core::stringw(parentElement->getTypeName());
  133. if (parentElement->getID() != -1)
  134. name += core::stringw(L" [") + core::stringw(parentElement->getID()) + core::stringw(L"]");
  135. IGUITreeViewNode* newnode = treenode->addChildBack(name.c_str());
  136. newnode->setData((void*)parentElement);
  137. core::list<IGUIElement*> children = parentElement->getChildren();
  138. for (core::list<IGUIElement*>::Iterator i = children.begin(); i != children.end(); i++ )
  139. {
  140. if(core::stringc((*i)->getTypeName()) != "GUIEditor" && !(*i)->isSubElement())
  141. addChildrenToTree(*i, newnode);
  142. }
  143. }
  144. void CGUIEditWindow::updateTree()
  145. {
  146. TreeView->getRoot()->clearChildren();
  147. IGUIElement* root = Environment->getRootGUIElement();
  148. addChildrenToTree(root, TreeView->getRoot());
  149. TreeView->getRoot()->getFirstChild()->setExpanded(true);
  150. }
  151. void CGUIEditWindow::setSelectedElement(IGUIElement *sel)
  152. {
  153. // save changes
  154. AttribEditor->updateAttribs();
  155. IGUITreeViewNode* elementTreeNode = getTreeNode(sel, TreeView->getRoot());
  156. if (elementTreeNode)
  157. {
  158. elementTreeNode->setSelected(true);
  159. while (elementTreeNode)
  160. {
  161. elementTreeNode->setExpanded(true);
  162. elementTreeNode = elementTreeNode->getParent();
  163. }
  164. }
  165. io::IAttributes* Attribs = AttribEditor->getAttribs();
  166. if (SelectedElement && sel != SelectedElement)
  167. {
  168. // deserialize attributes
  169. SelectedElement->deserializeAttributes(Attribs);
  170. }
  171. // clear the attributes list
  172. Attribs->clear();
  173. SelectedElement = sel;
  174. // get the new attributes
  175. if (SelectedElement)
  176. SelectedElement->serializeAttributes(Attribs);
  177. AttribEditor->refreshAttribs();
  178. }
  179. //! draws the element and its children.
  180. //! same code as for a window
  181. void CGUIEditWindow::draw()
  182. {
  183. if (!IsVisible)
  184. return;
  185. IGUISkin* skin = Environment->getSkin();
  186. core::rect<s32> rect = AbsoluteRect;
  187. // draw body fast
  188. rect = skin->draw3DWindowBackground(this, true, skin->getColor(EGDC_ACTIVE_BORDER),
  189. AbsoluteRect, &AbsoluteClippingRect);
  190. if (Text.size())
  191. {
  192. rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
  193. rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y);
  194. rect.LowerRightCorner.X -= skin->getSize(EGDS_WINDOW_BUTTON_WIDTH) + 5;
  195. IGUIFont* font = skin->getFont();
  196. if (font)
  197. font->draw(Text.c_str(), rect, skin->getColor(EGDC_ACTIVE_CAPTION), false, true, &AbsoluteClippingRect);
  198. }
  199. IGUIElement::draw();
  200. }
  201. //! called if an event happened.
  202. bool CGUIEditWindow::OnEvent(const SEvent &event)
  203. {
  204. switch(event.EventType)
  205. {
  206. case EET_GUI_EVENT:
  207. switch(event.GUIEvent.EventType)
  208. {
  209. case EGET_ELEMENT_FOCUS_LOST:
  210. if (event.GUIEvent.Caller == this ||
  211. event.GUIEvent.Caller == ResizeButton)
  212. {
  213. Dragging = false;
  214. Resizing = false;
  215. }
  216. break;
  217. default:
  218. break;
  219. }
  220. break;
  221. case EET_MOUSE_INPUT_EVENT:
  222. switch(event.MouseInput.Event)
  223. {
  224. case EMIE_LMOUSE_PRESSED_DOWN:
  225. {
  226. DragStart.X = event.MouseInput.X;
  227. DragStart.Y = event.MouseInput.Y;
  228. IGUIElement* clickedElement = getElementFromPoint(DragStart);
  229. if (clickedElement == this)
  230. {
  231. Dragging = IsDraggable;
  232. //Environment->setFocus(this);
  233. if (Parent)
  234. Parent->bringToFront(this);
  235. return true;
  236. }
  237. else if (clickedElement == ResizeButton)
  238. {
  239. Resizing = true;
  240. //Environment->setFocus(this);
  241. if (Parent)
  242. Parent->bringToFront(this);
  243. return true;
  244. }
  245. break;
  246. }
  247. case EMIE_LMOUSE_LEFT_UP:
  248. if (Dragging || Resizing)
  249. {
  250. Dragging = false;
  251. Resizing = false;
  252. return true;
  253. }
  254. break;
  255. case EMIE_MOUSE_MOVED:
  256. if (Dragging || Resizing)
  257. {
  258. // gui window should not be dragged outside of its parent
  259. if (Parent)
  260. if (event.MouseInput.X < Parent->getAbsolutePosition().UpperLeftCorner.X +1 ||
  261. event.MouseInput.Y < Parent->getAbsolutePosition().UpperLeftCorner.Y +1 ||
  262. event.MouseInput.X > Parent->getAbsolutePosition().LowerRightCorner.X -1 ||
  263. event.MouseInput.Y > Parent->getAbsolutePosition().LowerRightCorner.Y -1)
  264. return true;
  265. core::position2di diff(event.MouseInput.X - DragStart.X, event.MouseInput.Y - DragStart.Y);
  266. if (Dragging)
  267. {
  268. move(diff);
  269. DragStart.X = event.MouseInput.X;
  270. DragStart.Y = event.MouseInput.Y;
  271. }
  272. else if (Resizing)
  273. {
  274. core::position2di dp = RelativeRect.LowerRightCorner + diff;
  275. setRelativePosition(core::rect<s32>(RelativeRect.UpperLeftCorner, dp));
  276. DragStart += dp - RelativeRect.LowerRightCorner + diff;
  277. }
  278. return true;
  279. }
  280. break;
  281. default:
  282. break;
  283. }
  284. default:
  285. break;
  286. }
  287. return Parent ? Parent->OnEvent(event) : false;
  288. }
  289. bool CGUIEditWindow::isDraggable() const
  290. {
  291. return IsDraggable;
  292. }
  293. void CGUIEditWindow::setDraggable(bool draggable)
  294. {
  295. IsDraggable = draggable;
  296. if (Dragging && !IsDraggable)
  297. Dragging = false;
  298. }
  299. // we're supposed to supply these if we're creating an IGUIWindow
  300. // but we don't need them so we'll just return null
  301. //! Returns the rectangle of the drawable area (without border, without titlebar and without scrollbars)
  302. core::rect<s32> CGUIEditWindow::getClientRect() const {return core::recti();}
  303. IGUIButton* CGUIEditWindow::getCloseButton() const {return 0;}
  304. IGUIButton* CGUIEditWindow::getMinimizeButton() const {return 0;}
  305. IGUIButton* CGUIEditWindow::getMaximizeButton() const {return 0;}