2 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
3 // This file is part of the "Irrlicht Engine".
\r
4 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
6 #include "CGUIEnvironment.h"
\r
8 #ifdef _IRR_COMPILE_WITH_GUI_
\r
10 #include "IVideoDriver.h"
\r
12 #include "CGUISkin.h"
\r
13 #include "CGUIButton.h"
\r
14 #include "CGUIWindow.h"
\r
15 #include "CGUIScrollBar.h"
\r
16 #include "CGUIFont.h"
\r
17 #include "CGUISpriteBank.h"
\r
18 #include "CGUIImage.h"
\r
19 #include "CGUIMeshViewer.h"
\r
20 #include "CGUICheckBox.h"
\r
21 #include "CGUIListBox.h"
\r
22 #include "CGUITreeView.h"
\r
23 #include "CGUIImageList.h"
\r
24 #include "CGUIFileOpenDialog.h"
\r
25 #include "CGUIColorSelectDialog.h"
\r
26 #include "CGUIStaticText.h"
\r
27 #include "CGUIEditBox.h"
\r
28 #include "CGUISpinBox.h"
\r
29 #include "CGUIInOutFader.h"
\r
30 #include "CGUIMessageBox.h"
\r
31 #include "CGUIModalScreen.h"
\r
32 #include "CGUITabControl.h"
\r
33 #include "CGUIContextMenu.h"
\r
34 #include "CGUIComboBox.h"
\r
35 #include "CGUIMenu.h"
\r
36 #include "CGUIToolBar.h"
\r
37 #include "CGUITable.h"
\r
38 #include "CGUIProfiler.h"
\r
40 #include "CDefaultGUIElementFactory.h"
\r
41 #include "IWriteFile.h"
\r
42 #include "IXMLWriter.h"
\r
44 #include "BuiltInFont.h"
\r
52 const wchar_t IRR_XML_FORMAT_GUI_ENV[] = L"irr_gui";
\r
53 const wchar_t IRR_XML_FORMAT_GUI_ELEMENT[] = L"element";
\r
54 const wchar_t IRR_XML_FORMAT_GUI_ELEMENT_ATTR_TYPE[] = L"type";
\r
56 const io::path CGUIEnvironment::DefaultFontName = "#DefaultFont";
\r
59 CGUIEnvironment::CGUIEnvironment(io::IFileSystem* fs, video::IVideoDriver* driver, IOSOperator* op)
\r
60 : IGUIElement(EGUIET_ROOT, 0, 0, 0, core::rect<s32>(driver ? core::dimension2d<s32>(driver->getScreenSize()) : core::dimension2d<s32>(0,0))),
\r
61 Driver(driver), Hovered(0), HoveredNoSubelement(0), Focus(0), LastHoveredMousePos(0,0), CurrentSkin(0),
\r
62 FileSystem(fs), UserReceiver(0), Operator(op), FocusFlags(EFF_SET_ON_LMOUSE_DOWN|EFF_SET_ON_TAB)
\r
74 IGUIEnvironment::setDebugName("CGUIEnvironment");
\r
78 IGUIElementFactory* factory = new CDefaultGUIElementFactory(this);
\r
79 registerGUIElementFactory(factory);
\r
84 IGUISkin* skin = createSkin( gui::EGST_WINDOWS_METALLIC );
\r
88 //set tooltip default
\r
89 ToolTip.LastTime = 0;
\r
90 ToolTip.EnterTime = 0;
\r
91 ToolTip.LaunchTime = 1000;
\r
92 ToolTip.RelaunchTime = 500;
\r
93 ToolTip.Element = 0;
\r
95 // environment is root tab group
\r
102 CGUIEnvironment::~CGUIEnvironment()
\r
104 if ( HoveredNoSubelement && HoveredNoSubelement != this )
\r
106 HoveredNoSubelement->drop();
\r
107 HoveredNoSubelement = 0;
\r
110 if (Hovered && Hovered != this)
\r
122 if (ToolTip.Element)
\r
124 ToolTip.Element->drop();
\r
125 ToolTip.Element = 0;
\r
131 CurrentSkin->drop();
\r
137 // delete all sprite banks
\r
138 for (i=0; i<Banks.size(); ++i)
\r
140 Banks[i].Bank->drop();
\r
142 // delete all fonts
\r
143 for (i=0; i<Fonts.size(); ++i)
\r
144 Fonts[i].Font->drop();
\r
146 // remove all factories
\r
147 for (i=0; i<GUIElementFactoryList.size(); ++i)
\r
148 GUIElementFactoryList[i]->drop();
\r
158 FileSystem->drop();
\r
170 void CGUIEnvironment::loadBuiltInFont()
\r
172 io::IReadFile* file = FileSystem->createMemoryReadFile(BuiltInFontData,
\r
173 BuiltInFontDataSize, DefaultFontName, false);
\r
175 CGUIFont* font = new CGUIFont(this, DefaultFontName );
\r
176 if (!font->load(file))
\r
178 os::Printer::log("Error: Could not load built-in Font. Did you compile without the BMP loader?", ELL_ERROR);
\r
185 f.NamedPath.setPath(DefaultFontName);
\r
187 Fonts.push_back(f);
\r
193 //! draws all gui elements
\r
194 void CGUIEnvironment::drawAll()
\r
198 core::dimension2d<s32> dim(Driver->getScreenSize());
\r
199 if (AbsoluteRect.LowerRightCorner.X != dim.Width ||
\r
200 AbsoluteRect.LowerRightCorner.Y != dim.Height)
\r
202 // resize gui environment
\r
203 DesiredRect.LowerRightCorner = dim;
\r
204 AbsoluteClippingRect = DesiredRect;
\r
205 AbsoluteRect = DesiredRect;
\r
206 updateAbsolutePosition();
\r
210 // make sure tooltip is always on top
\r
211 if (ToolTip.Element)
\r
212 bringToFront(ToolTip.Element);
\r
215 OnPostRender ( os::Timer::getTime () );
\r
219 //! sets the focus to an element
\r
220 bool CGUIEnvironment::setFocus(IGUIElement* element)
\r
222 if (Focus == element)
\r
227 // GUI Environment should just reset the focus to 0
\r
228 if (element == this)
\r
231 // stop element from being deleted
\r
235 // focus may change or be removed in this call
\r
236 IGUIElement *currentFocus = 0;
\r
239 currentFocus = Focus;
\r
240 currentFocus->grab();
\r
242 e.EventType = EET_GUI_EVENT;
\r
243 e.GUIEvent.Caller = Focus;
\r
244 e.GUIEvent.Element = element;
\r
245 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
246 if (Focus->OnEvent(e))
\r
250 currentFocus->drop();
\r
253 currentFocus->drop();
\r
259 currentFocus = Focus;
\r
261 currentFocus->grab();
\r
263 // send focused event
\r
265 e.EventType = EET_GUI_EVENT;
\r
266 e.GUIEvent.Caller = element;
\r
267 e.GUIEvent.Element = Focus;
\r
268 e.GUIEvent.EventType = EGET_ELEMENT_FOCUSED;
\r
269 if (element->OnEvent(e))
\r
274 currentFocus->drop();
\r
280 currentFocus->drop();
\r
285 // element is the new focus so it doesn't have to be dropped
\r
292 //! returns the element with the focus
\r
293 IGUIElement* CGUIEnvironment::getFocus() const
\r
298 //! returns the element last known to be under the mouse cursor
\r
299 IGUIElement* CGUIEnvironment::getHovered() const
\r
305 //! removes the focus from an element
\r
306 bool CGUIEnvironment::removeFocus(IGUIElement* element)
\r
308 if (Focus && Focus==element)
\r
311 e.EventType = EET_GUI_EVENT;
\r
312 e.GUIEvent.Caller = Focus;
\r
313 e.GUIEvent.Element = 0;
\r
314 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
315 if (Focus->OnEvent(e))
\r
330 //! Returns whether the element has focus
\r
331 bool CGUIEnvironment::hasFocus(const IGUIElement* element, bool checkSubElements) const
\r
333 if (element == Focus)
\r
336 if ( !checkSubElements || !element )
\r
339 IGUIElement* f = Focus;
\r
340 while ( f && f->isSubElement() )
\r
342 f = f->getParent();
\r
343 if ( f == element )
\r
350 //! returns the current video driver
\r
351 video::IVideoDriver* CGUIEnvironment::getVideoDriver() const
\r
357 //! returns the current file system
\r
358 io::IFileSystem* CGUIEnvironment::getFileSystem() const
\r
364 //! returns a pointer to the OS operator
\r
365 IOSOperator* CGUIEnvironment::getOSOperator() const
\r
371 //! clear all GUI elements
\r
372 void CGUIEnvironment::clear()
\r
374 // Remove the focus
\r
381 if (Hovered && Hovered != this)
\r
386 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
388 HoveredNoSubelement->drop();
\r
389 HoveredNoSubelement = 0;
\r
392 // get the root's children in case the root changes in future
\r
393 const core::list<IGUIElement*>& children = getRootGUIElement()->getChildren();
\r
395 while (!children.empty())
\r
396 (*children.getLast())->remove();
\r
400 //! called by ui if an event happened.
\r
401 bool CGUIEnvironment::OnEvent(const SEvent& event)
\r
406 && (event.EventType != EET_MOUSE_INPUT_EVENT)
\r
407 && (event.EventType != EET_KEY_INPUT_EVENT)
\r
408 && (event.EventType != EET_GUI_EVENT || event.GUIEvent.Caller != this))
\r
410 ret = UserReceiver->OnEvent(event);
\r
417 void CGUIEnvironment::OnPostRender( u32 time )
\r
420 if ( ToolTip.Element == 0 &&
\r
421 HoveredNoSubelement && HoveredNoSubelement != this &&
\r
422 (time - ToolTip.EnterTime >= ToolTip.LaunchTime
\r
423 || (time - ToolTip.LastTime >= ToolTip.RelaunchTime && time - ToolTip.LastTime < ToolTip.LaunchTime)) &&
\r
424 HoveredNoSubelement->getToolTipText().size() &&
\r
426 getSkin()->getFont(EGDF_TOOLTIP)
\r
429 core::rect<s32> pos;
\r
431 pos.UpperLeftCorner = LastHoveredMousePos;
\r
432 core::dimension2du dim = getSkin()->getFont(EGDF_TOOLTIP)->getDimension(HoveredNoSubelement->getToolTipText().c_str());
\r
433 dim.Width += getSkin()->getSize(EGDS_TEXT_DISTANCE_X)*2;
\r
434 dim.Height += getSkin()->getSize(EGDS_TEXT_DISTANCE_Y)*2;
\r
436 pos.UpperLeftCorner.Y -= dim.Height+1;
\r
437 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + dim.Height-1;
\r
438 pos.LowerRightCorner.X = pos.UpperLeftCorner.X + dim.Width;
\r
440 pos.constrainTo(getAbsolutePosition());
\r
442 ToolTip.Element = addStaticText(HoveredNoSubelement->getToolTipText().c_str(), pos, true, true, this, -1, true);
\r
443 ToolTip.Element->setOverrideColor(getSkin()->getColor(EGDC_TOOLTIP));
\r
444 ToolTip.Element->setBackgroundColor(getSkin()->getColor(EGDC_TOOLTIP_BACKGROUND));
\r
445 ToolTip.Element->setOverrideFont(getSkin()->getFont(EGDF_TOOLTIP));
\r
446 ToolTip.Element->setSubElement(true);
\r
447 ToolTip.Element->grab();
\r
449 s32 textHeight = ToolTip.Element->getTextHeight();
\r
450 pos = ToolTip.Element->getRelativePosition();
\r
451 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + textHeight;
\r
452 ToolTip.Element->setRelativePosition(pos);
\r
455 if (ToolTip.Element && ToolTip.Element->isVisible() ) // (isVisible() check only because we might use visibility for ToolTip one day)
\r
457 ToolTip.LastTime = time;
\r
459 // got invisible or removed in the meantime?
\r
460 if ( !HoveredNoSubelement ||
\r
461 !HoveredNoSubelement->isVisible() ||
\r
462 !HoveredNoSubelement->getParent()
\r
463 ) // got invisible or removed in the meantime?
\r
465 ToolTip.Element->remove();
\r
466 ToolTip.Element->drop();
\r
467 ToolTip.Element = 0;
\r
471 IGUIElement::OnPostRender ( time );
\r
476 void CGUIEnvironment::updateHoveredElement(core::position2d<s32> mousePos)
\r
478 IGUIElement* lastHovered = Hovered;
\r
479 IGUIElement* lastHoveredNoSubelement = HoveredNoSubelement;
\r
480 LastHoveredMousePos = mousePos;
\r
482 Hovered = getElementFromPoint(mousePos);
\r
484 if ( ToolTip.Element && Hovered == ToolTip.Element )
\r
486 // When the mouse is over the ToolTip we remove that so it will be re-created at a new position.
\r
487 // Note that ToolTip.EnterTime does not get changed here, so it will be re-created at once.
\r
488 ToolTip.Element->remove();
\r
489 ToolTip.Element->drop();
\r
490 ToolTip.Element = 0;
\r
492 // Get the real Hovered
\r
493 Hovered = getElementFromPoint(mousePos);
\r
496 // for tooltips we want the element itself and not some of it's subelements
\r
497 HoveredNoSubelement = Hovered;
\r
498 while ( HoveredNoSubelement && HoveredNoSubelement->isSubElement() )
\r
500 HoveredNoSubelement = HoveredNoSubelement->getParent();
\r
503 if (Hovered && Hovered != this)
\r
505 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
506 HoveredNoSubelement->grab();
\r
508 if (Hovered != lastHovered)
\r
511 event.EventType = EET_GUI_EVENT;
\r
515 event.GUIEvent.Caller = lastHovered;
\r
516 event.GUIEvent.Element = 0;
\r
517 event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
\r
518 lastHovered->OnEvent(event);
\r
523 event.GUIEvent.Caller = Hovered;
\r
524 event.GUIEvent.Element = Hovered;
\r
525 event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
\r
526 Hovered->OnEvent(event);
\r
530 if ( lastHoveredNoSubelement != HoveredNoSubelement )
\r
532 if (ToolTip.Element)
\r
534 ToolTip.Element->remove();
\r
535 ToolTip.Element->drop();
\r
536 ToolTip.Element = 0;
\r
539 if ( HoveredNoSubelement )
\r
541 u32 now = os::Timer::getTime();
\r
542 ToolTip.EnterTime = now;
\r
546 if (lastHovered && lastHovered != this)
\r
547 lastHovered->drop();
\r
548 if (lastHoveredNoSubelement && lastHoveredNoSubelement != this)
\r
549 lastHoveredNoSubelement->drop();
\r
553 //! This sets a new event receiver for gui events. Usually you do not have to
\r
554 //! use this method, it is used by the internal engine.
\r
555 void CGUIEnvironment::setUserEventReceiver(IEventReceiver* evr)
\r
557 UserReceiver = evr;
\r
561 //! posts an input event to the environment
\r
562 bool CGUIEnvironment::postEventFromUser(const SEvent& event)
\r
564 switch(event.EventType)
\r
566 case EET_GUI_EVENT:
\r
568 // hey, why is the user sending gui events..?
\r
572 case EET_MOUSE_INPUT_EVENT:
\r
574 updateHoveredElement(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
\r
576 if ( Hovered != Focus )
\r
578 IGUIElement * focusCandidate = Hovered;
\r
580 // Only allow enabled elements to be focused (unless EFF_CAN_FOCUS_DISABLED is set)
\r
581 if ( Hovered && !Hovered->isEnabled() && !(FocusFlags & EFF_CAN_FOCUS_DISABLED))
\r
582 focusCandidate = NULL; // we still remove focus from the active element
\r
584 // Please don't merge this into a single if clause, it's easier to debug the way it is
\r
585 if (FocusFlags & EFF_SET_ON_LMOUSE_DOWN &&
\r
586 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
\r
588 setFocus(focusCandidate);
\r
590 else if ( FocusFlags & EFF_SET_ON_RMOUSE_DOWN &&
\r
591 event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN )
\r
593 setFocus(focusCandidate);
\r
595 else if ( FocusFlags & EFF_SET_ON_MOUSE_OVER &&
\r
596 event.MouseInput.Event == EMIE_MOUSE_MOVED )
\r
598 setFocus(focusCandidate);
\r
602 // sending input to focus
\r
603 if (Focus && Focus->OnEvent(event))
\r
606 // focus could have died in last call
\r
607 if (!Focus && Hovered)
\r
609 return Hovered->OnEvent(event);
\r
613 case EET_KEY_INPUT_EVENT:
\r
615 if (Focus && Focus->OnEvent(event))
\r
618 // For keys we handle the event before changing focus to give elements the chance for catching the TAB
\r
619 // Send focus changing event
\r
620 if (FocusFlags & EFF_SET_ON_TAB &&
\r
621 event.EventType == EET_KEY_INPUT_EVENT &&
\r
622 event.KeyInput.PressedDown &&
\r
623 event.KeyInput.Key == KEY_TAB)
\r
625 IGUIElement *next = getNextElement(event.KeyInput.Shift, event.KeyInput.Control);
\r
626 if (next && next != Focus)
\r
628 if (setFocus(next))
\r
642 //! returns the current gui skin
\r
643 IGUISkin* CGUIEnvironment::getSkin() const
\r
645 return CurrentSkin;
\r
649 //! Sets a new GUI Skin
\r
650 void CGUIEnvironment::setSkin(IGUISkin* skin)
\r
652 if (CurrentSkin==skin)
\r
656 CurrentSkin->drop();
\r
658 CurrentSkin = skin;
\r
661 CurrentSkin->grab();
\r
665 //! Creates a new GUI Skin based on a template.
\r
666 /** \return Returns a pointer to the created skin.
\r
667 If you no longer need the skin, you should call IGUISkin::drop().
\r
668 See IReferenceCounted::drop() for more information. */
\r
669 IGUISkin* CGUIEnvironment::createSkin(EGUI_SKIN_TYPE type)
\r
671 IGUISkin* skin = new CGUISkin(type, Driver);
\r
673 IGUIFont* builtinfont = getBuiltInFont();
\r
674 IGUIFontBitmap* bitfont = 0;
\r
675 if (builtinfont && builtinfont->getType() == EGFT_BITMAP)
\r
676 bitfont = (IGUIFontBitmap*)builtinfont;
\r
678 IGUISpriteBank* bank = 0;
\r
679 skin->setFont(builtinfont);
\r
682 bank = bitfont->getSpriteBank();
\r
684 skin->setSpriteBank(bank);
\r
690 //! Returns the default element factory which can create all built in elements
\r
691 IGUIElementFactory* CGUIEnvironment::getDefaultGUIElementFactory() const
\r
693 return getGUIElementFactory(0);
\r
697 //! Adds an element factory to the gui environment.
\r
698 /** Use this to extend the gui environment with new element types which it should be
\r
699 able to create automatically, for example when loading data from xml files. */
\r
700 void CGUIEnvironment::registerGUIElementFactory(IGUIElementFactory* factoryToAdd)
\r
704 factoryToAdd->grab();
\r
705 GUIElementFactoryList.push_back(factoryToAdd);
\r
710 //! Returns amount of registered scene node factories.
\r
711 u32 CGUIEnvironment::getRegisteredGUIElementFactoryCount() const
\r
713 return GUIElementFactoryList.size();
\r
717 //! Returns a scene node factory by index
\r
718 IGUIElementFactory* CGUIEnvironment::getGUIElementFactory(u32 index) const
\r
720 if (index < GUIElementFactoryList.size())
\r
721 return GUIElementFactoryList[index];
\r
727 //! adds a GUI Element using its name
\r
728 IGUIElement* CGUIEnvironment::addGUIElement(const c8* elementName, IGUIElement* parent)
\r
730 IGUIElement* node=0;
\r
735 for (s32 i=GUIElementFactoryList.size()-1; i>=0 && !node; --i)
\r
736 node = GUIElementFactoryList[i]->addGUIElement(elementName, parent);
\r
743 //! Saves the current gui into a file.
\r
744 //! \param filename: Name of the file .
\r
745 bool CGUIEnvironment::saveGUI(const io::path& filename, IGUIElement* start)
\r
747 io::IWriteFile* file = FileSystem->createAndWriteFile(filename);
\r
753 bool ret = saveGUI(file, start);
\r
759 //! Saves the current gui into a file.
\r
760 bool CGUIEnvironment::saveGUI(io::IWriteFile* file, IGUIElement* start)
\r
767 io::IXMLWriter* writer = FileSystem->createXMLWriter(file);
\r
773 writer->writeXMLHeader();
\r
774 writeGUIElement(writer, start ? start : this);
\r
781 //! Loads the gui. Note that the current gui is not cleared before.
\r
782 //! \param filename: Name of the file.
\r
783 bool CGUIEnvironment::loadGUI(const io::path& filename, IGUIElement* parent)
\r
785 io::IReadFile* read = FileSystem->createAndOpenFile(filename);
\r
788 os::Printer::log("Unable to open gui file", filename, ELL_ERROR);
\r
792 bool ret = loadGUI(read, parent);
\r
799 //! Loads the gui. Note that the current gui is not cleared before.
\r
800 bool CGUIEnvironment::loadGUI(io::IReadFile* file, IGUIElement* parent)
\r
804 os::Printer::log("Unable to open GUI file", ELL_ERROR);
\r
808 io::IXMLReader* reader = FileSystem->createXMLReader(file);
\r
811 os::Printer::log("GUI is not a valid XML file", file->getFileName(), ELL_ERROR);
\r
816 while(reader->read())
\r
818 readGUIElement(reader, parent);
\r
828 //! reads an element
\r
829 void CGUIEnvironment::readGUIElement(io::IXMLReader* reader, IGUIElement* node)
\r
834 io::EXML_NODE nodeType = reader->getNodeType();
\r
836 if (nodeType == io::EXN_NONE || nodeType == io::EXN_UNKNOWN || nodeType == io::EXN_ELEMENT_END)
\r
839 IGUIElement* deferedNode = 0;
\r
840 if (!wcscmp(IRR_XML_FORMAT_GUI_ENV, reader->getNodeName()))
\r
842 // GuiEnvironment always must be this as it would serialize into a wrong element otherwise.
\r
843 // So we use the given node next time
\r
844 if ( node && node != this )
\r
845 deferedNode = node;
\r
846 node = this; // root
\r
848 else if (!wcscmp(IRR_XML_FORMAT_GUI_ELEMENT, reader->getNodeName()))
\r
850 // find node type and create it
\r
851 const core::stringc attrName = reader->getAttributeValue(IRR_XML_FORMAT_GUI_ELEMENT_ATTR_TYPE);
\r
853 node = addGUIElement(attrName.c_str(), node);
\r
856 os::Printer::log("Could not create GUI element of unknown type", attrName.c_str());
\r
861 while(reader->read())
\r
863 bool endreached = false;
\r
865 switch (reader->getNodeType())
\r
867 case io::EXN_ELEMENT_END:
\r
868 if (!wcscmp(IRR_XML_FORMAT_GUI_ELEMENT, reader->getNodeName()) ||
\r
869 !wcscmp(IRR_XML_FORMAT_GUI_ENV, reader->getNodeName()))
\r
874 case io::EXN_ELEMENT:
\r
875 if (!wcscmp(L"attributes", reader->getNodeName()))
\r
878 io::IAttributes* attr = FileSystem->createEmptyAttributes(Driver);
\r
879 attr->read(reader, true);
\r
882 node->deserializeAttributes(attr);
\r
887 if (!wcscmp(IRR_XML_FORMAT_GUI_ELEMENT, reader->getNodeName()) ||
\r
888 !wcscmp(IRR_XML_FORMAT_GUI_ENV, reader->getNodeName()))
\r
891 readGUIElement(reader, deferedNode);
\r
893 readGUIElement(reader, node);
\r
897 os::Printer::log("Found unknown element in irrlicht GUI file",
\r
898 core::stringc(reader->getNodeName()).c_str());
\r
912 //! writes an element
\r
913 void CGUIEnvironment::writeGUIElement(io::IXMLWriter* writer, IGUIElement* node)
\r
915 if (!writer || !node )
\r
918 const wchar_t* name = 0;
\r
920 // write properties
\r
922 io::IAttributes* attr = FileSystem->createEmptyAttributes();
\r
923 node->serializeAttributes(attr);
\r
925 // all gui elements must have at least one attribute
\r
926 // if they have nothing then we ignore them.
\r
927 if (attr->getAttributeCount() != 0)
\r
931 name = IRR_XML_FORMAT_GUI_ENV;
\r
932 writer->writeElement(name, false);
\r
936 name = IRR_XML_FORMAT_GUI_ELEMENT;
\r
937 writer->writeElement(name, false, IRR_XML_FORMAT_GUI_ELEMENT_ATTR_TYPE,
\r
938 core::stringw(node->getTypeName()).c_str());
\r
941 writer->writeLineBreak();
\r
943 attr->write(writer);
\r
948 core::list<IGUIElement*>::ConstIterator it = node->getChildren().begin();
\r
949 for (; it != node->getChildren().end(); ++it)
\r
951 if (!(*it)->isSubElement())
\r
953 writer->writeLineBreak();
\r
954 writeGUIElement(writer, (*it));
\r
958 // write closing brace if required
\r
959 if (attr->getAttributeCount() != 0)
\r
961 writer->writeClosingTag(name);
\r
962 writer->writeLineBreak();
\r
969 //! Writes attributes of the environment
\r
970 void CGUIEnvironment::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
\r
972 IGUISkin* skin = getSkin();
\r
976 out->addEnum("Skin", getSkin()->getType(), GUISkinTypeNames);
\r
977 skin->serializeAttributes(out, options);
\r
982 //! Reads attributes of the environment
\r
983 void CGUIEnvironment::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
\r
985 if (in->existsAttribute("Skin"))
\r
987 IGUISkin *skin = getSkin();
\r
989 EGUI_SKIN_TYPE t = (EGUI_SKIN_TYPE) in->getAttributeAsEnumeration("Skin",GUISkinTypeNames);
\r
990 if ( !skin || t != skin->getType())
\r
992 skin = createSkin(t);
\r
1001 skin->deserializeAttributes(in, options);
\r
1006 RelativeRect = AbsoluteRect =
\r
1007 core::rect<s32>(Driver ? core::dimension2di(Driver->getScreenSize()) : core::dimension2d<s32>(0,0));
\r
1011 //! adds a button. The returned pointer must not be dropped.
\r
1012 IGUIButton* CGUIEnvironment::addButton(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, const wchar_t *tooltiptext)
\r
1014 IGUIButton* button = new CGUIButton(this, parent ? parent : this, id, rectangle);
\r
1016 button->setText(text);
\r
1018 if ( tooltiptext )
\r
1019 button->setToolTipText ( tooltiptext );
\r
1026 //! adds a window. The returned pointer must not be dropped.
\r
1027 IGUIWindow* CGUIEnvironment::addWindow(const core::rect<s32>& rectangle, bool modal,
\r
1028 const wchar_t* text, IGUIElement* parent, s32 id)
\r
1030 parent = parent ? parent : this;
\r
1032 IGUIWindow* win = new CGUIWindow(this, parent, id, rectangle);
\r
1034 win->setText(text);
\r
1039 // Careful, don't just set the modal as parent above. That will mess up the focus (and is hard to change because we have to be very
\r
1040 // careful not to get virtual function call, like OnEvent, in the window.
\r
1041 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
1042 modalScreen->drop();
\r
1043 modalScreen->addChild(win);
\r
1050 //! adds a modal screen. The returned pointer must not be dropped.
\r
1051 IGUIElement* CGUIEnvironment::addModalScreen(IGUIElement* parent)
\r
1053 parent = parent ? parent : this;
\r
1055 IGUIElement *win = new CGUIModalScreen(this, parent, -1);
\r
1062 //! Adds a message box.
\r
1063 IGUIWindow* CGUIEnvironment::addMessageBox(const wchar_t* caption, const wchar_t* text,
\r
1064 bool modal, s32 flag, IGUIElement* parent, s32 id, video::ITexture* image)
\r
1069 parent = parent ? parent : this;
\r
1071 core::rect<s32> rect;
\r
1072 core::dimension2d<u32> screenDim, msgBoxDim;
\r
1074 screenDim.Width = parent->getAbsolutePosition().getWidth();
\r
1075 screenDim.Height = parent->getAbsolutePosition().getHeight();
\r
1076 msgBoxDim.Width = 2;
\r
1077 msgBoxDim.Height = 2;
\r
1079 rect.UpperLeftCorner.X = (screenDim.Width - msgBoxDim.Width) / 2;
\r
1080 rect.UpperLeftCorner.Y = (screenDim.Height - msgBoxDim.Height) / 2;
\r
1081 rect.LowerRightCorner.X = rect.UpperLeftCorner.X + msgBoxDim.Width;
\r
1082 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + msgBoxDim.Height;
\r
1084 IGUIWindow* win = new CGUIMessageBox(this, caption, text, flag,
\r
1085 parent, id, rect, image);
\r
1090 // Careful, don't just set the modal as parent above. That will mess up the focus (and is hard to change because we have to be very
\r
1091 // careful not to get virtual function call, like OnEvent, in the CGUIMessageBox.
\r
1092 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
1093 modalScreen->drop();
\r
1094 modalScreen->addChild( win );
\r
1102 //! adds a scrollbar. The returned pointer must not be dropped.
\r
1103 IGUIScrollBar* CGUIEnvironment::addScrollBar(bool horizontal, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id)
\r
1105 IGUIScrollBar* bar = new CGUIScrollBar(horizontal, this, parent ? parent : this, id, rectangle);
\r
1110 //! Adds a table to the environment
\r
1111 IGUITable* CGUIEnvironment::addTable(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, bool drawBackground)
\r
1113 CGUITable* b = new CGUITable(this, parent ? parent : this, id, rectangle, true, drawBackground, false);
\r
1118 //! Adds an element to display the information from the Irrlicht profiler
\r
1119 IGUIProfiler* CGUIEnvironment::addProfilerDisplay(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id)
\r
1121 CGUIProfiler* p = new CGUIProfiler(this, parent ? parent : this, id, rectangle, NULL);
\r
1126 //! Adds an image element.
\r
1127 IGUIImage* CGUIEnvironment::addImage(video::ITexture* image, core::position2d<s32> pos,
\r
1128 bool useAlphaChannel, IGUIElement* parent, s32 id, const wchar_t* text)
\r
1130 core::dimension2d<s32> sz(0,0);
\r
1132 sz = core::dimension2d<s32>(image->getOriginalSize());
\r
1134 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
1135 id, core::rect<s32>(pos, sz));
\r
1138 img->setText(text);
\r
1140 if (useAlphaChannel)
\r
1141 img->setUseAlphaChannel(true);
\r
1144 img->setImage(image);
\r
1151 //! adds an image. The returned pointer must not be dropped.
\r
1152 IGUIImage* CGUIEnvironment::addImage(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, bool useAlphaChannel)
\r
1154 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
1158 img->setText(text);
\r
1160 if ( useAlphaChannel )
\r
1161 img->setUseAlphaChannel(true);
\r
1168 //! adds an mesh viewer. The returned pointer must not be dropped.
\r
1169 IGUIMeshViewer* CGUIEnvironment::addMeshViewer(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text)
\r
1171 IGUIMeshViewer* v = new CGUIMeshViewer(this, parent ? parent : this,
\r
1182 //! adds a checkbox
\r
1183 IGUICheckBox* CGUIEnvironment::addCheckBox(bool checked, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text)
\r
1185 IGUICheckBox* b = new CGUICheckBox(checked, this,
\r
1186 parent ? parent : this , id , rectangle);
\r
1196 //! adds a list box
\r
1197 IGUIListBox* CGUIEnvironment::addListBox(const core::rect<s32>& rectangle,
\r
1198 IGUIElement* parent, s32 id, bool drawBackground)
\r
1200 IGUIListBox* b = new CGUIListBox(this, parent ? parent : this, id, rectangle,
\r
1201 true, drawBackground, false);
\r
1203 if (CurrentSkin && CurrentSkin->getSpriteBank())
\r
1205 b->setSpriteBank(CurrentSkin->getSpriteBank());
\r
1207 else if (getBuiltInFont() && getBuiltInFont()->getType() == EGFT_BITMAP)
\r
1209 b->setSpriteBank( ((IGUIFontBitmap*)getBuiltInFont())->getSpriteBank());
\r
1216 //! adds a tree view
\r
1217 IGUITreeView* CGUIEnvironment::addTreeView(const core::rect<s32>& rectangle,
\r
1218 IGUIElement* parent, s32 id,
\r
1219 bool drawBackground,
\r
1220 bool scrollBarVertical, bool scrollBarHorizontal)
\r
1222 IGUITreeView* b = new CGUITreeView(this, parent ? parent : this, id, rectangle,
\r
1223 true, drawBackground, scrollBarVertical, scrollBarHorizontal);
\r
1225 b->setIconFont ( getBuiltInFont () );
\r
1230 //! adds a file open dialog. The returned pointer must not be dropped.
\r
1231 IGUIFileOpenDialog* CGUIEnvironment::addFileOpenDialog(const wchar_t* title,
\r
1232 bool modal, IGUIElement* parent, s32 id,
\r
1233 bool restoreCWD, io::path::char_type* startDir)
\r
1235 parent = parent ? parent : this;
\r
1237 IGUIFileOpenDialog* d = new CGUIFileOpenDialog(title, this, parent, id,
\r
1238 restoreCWD, startDir);
\r
1243 // Careful, don't just set the modal as parent above. That will mess up the focus (and is hard to change because we have to be very
\r
1244 // careful not to get virtual function call, like OnEvent, in the window.
\r
1245 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
1246 modalScreen->drop();
\r
1247 modalScreen->addChild(d);
\r
1254 //! adds a color select dialog. The returned pointer must not be dropped.
\r
1255 IGUIColorSelectDialog* CGUIEnvironment::addColorSelectDialog(const wchar_t* title,
\r
1256 bool modal, IGUIElement* parent, s32 id)
\r
1258 parent = parent ? parent : this;
\r
1260 IGUIColorSelectDialog* d = new CGUIColorSelectDialog( title,
\r
1261 this, parent, id);
\r
1266 // Careful, don't just set the modal as parent above. That will mess up the focus (and is hard to change because we have to be very
\r
1267 // careful not to get virtual function call, like OnEvent, in the window.
\r
1268 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
1269 modalScreen->drop();
\r
1270 modalScreen->addChild(d);
\r
1277 //! adds a static text. The returned pointer must not be dropped.
\r
1278 IGUIStaticText* CGUIEnvironment::addStaticText(const wchar_t* text,
\r
1279 const core::rect<s32>& rectangle,
\r
1280 bool border, bool wordWrap,
\r
1281 IGUIElement* parent, s32 id, bool background)
\r
1283 IGUIStaticText* d = new CGUIStaticText(text, border, this,
\r
1284 parent ? parent : this, id, rectangle, background);
\r
1286 d->setWordWrap(wordWrap);
\r
1293 //! Adds an edit box. The returned pointer must not be dropped.
\r
1294 IGUIEditBox* CGUIEnvironment::addEditBox(const wchar_t* text,
\r
1295 const core::rect<s32>& rectangle, bool border,
\r
1296 IGUIElement* parent, s32 id)
\r
1298 IGUIEditBox* d = new CGUIEditBox(text, border, this,
\r
1299 parent ? parent : this, id, rectangle);
\r
1306 //! Adds a spin box to the environment
\r
1307 IGUISpinBox* CGUIEnvironment::addSpinBox(const wchar_t* text,
\r
1308 const core::rect<s32> &rectangle,
\r
1309 bool border,IGUIElement* parent, s32 id)
\r
1311 IGUISpinBox* d = new CGUISpinBox(text, border,this,
\r
1312 parent ? parent : this, id, rectangle);
\r
1319 //! Adds a tab control to the environment.
\r
1320 IGUITabControl* CGUIEnvironment::addTabControl(const core::rect<s32>& rectangle,
\r
1321 IGUIElement* parent, bool fillbackground, bool border, s32 id)
\r
1323 IGUITabControl* t = new CGUITabControl(this, parent ? parent : this,
\r
1324 rectangle, fillbackground, border, id);
\r
1330 //! Adds tab to the environment.
\r
1331 IGUITab* CGUIEnvironment::addTab(const core::rect<s32>& rectangle,
\r
1332 IGUIElement* parent, s32 id)
\r
1334 IGUITab* t = new CGUITab(this, parent ? parent : this,
\r
1341 //! Adds a context menu to the environment.
\r
1342 IGUIContextMenu* CGUIEnvironment::addContextMenu(const core::rect<s32>& rectangle,
\r
1343 IGUIElement* parent, s32 id)
\r
1345 IGUIContextMenu* c = new CGUIContextMenu(this,
\r
1346 parent ? parent : this, id, rectangle, true);
\r
1352 //! Adds a menu to the environment.
\r
1353 IGUIContextMenu* CGUIEnvironment::addMenu(IGUIElement* parent, s32 id)
\r
1358 IGUIContextMenu* c = new CGUIMenu(this,
\r
1359 parent, id, core::rect<s32>(0,0,
\r
1360 parent->getAbsolutePosition().getWidth(),
\r
1361 parent->getAbsolutePosition().getHeight()));
\r
1368 //! Adds a toolbar to the environment. It is like a menu is always placed on top
\r
1369 //! in its parent, and contains buttons.
\r
1370 IGUIToolBar* CGUIEnvironment::addToolBar(IGUIElement* parent, s32 id)
\r
1375 IGUIToolBar* b = new CGUIToolBar(this, parent, id, core::rect<s32>(0,0,10,10));
\r
1381 //! Adds an element for fading in or out.
\r
1382 IGUIInOutFader* CGUIEnvironment::addInOutFader(const core::rect<s32>* rectangle, IGUIElement* parent, s32 id)
\r
1384 core::rect<s32> rect;
\r
1387 rect = *rectangle;
\r
1389 rect = core::rect<s32>(core::dimension2di(Driver->getScreenSize()));
\r
1394 IGUIInOutFader* fader = new CGUIInOutFader(this, parent, id, rect);
\r
1400 //! Adds a combo box to the environment.
\r
1401 IGUIComboBox* CGUIEnvironment::addComboBox(const core::rect<s32>& rectangle,
\r
1402 IGUIElement* parent, s32 id)
\r
1404 IGUIComboBox* t = new CGUIComboBox(this, parent ? parent : this,
\r
1411 //! returns the font
\r
1412 IGUIFont* CGUIEnvironment::getFont(const io::path& filename)
\r
1414 // search existing font
\r
1417 f.NamedPath.setPath(filename);
\r
1419 s32 index = Fonts.binary_search(f);
\r
1421 return Fonts[index].Font;
\r
1423 // font doesn't exist, attempt to load it
\r
1425 // does the file exist?
\r
1427 if (!FileSystem->existFile(filename))
\r
1429 os::Printer::log("Could not load font because the file does not exist", f.NamedPath.getPath(), ELL_ERROR);
\r
1433 IGUIFont* ifont=0;
\r
1434 io::IXMLReader *xml = FileSystem->createXMLReader(filename );
\r
1437 // this is an XML font, but we need to know what type
\r
1438 EGUI_FONT_TYPE t = EGFT_CUSTOM;
\r
1441 while(!found && xml->read())
\r
1443 if (xml->getNodeType() == io::EXN_ELEMENT)
\r
1445 if (core::stringw(L"font") == xml->getNodeName())
\r
1447 if (core::stringw(L"vector") == xml->getAttributeValue(L"type"))
\r
1452 else if (core::stringw(L"bitmap") == xml->getAttributeValue(L"type"))
\r
1462 if (t==EGFT_BITMAP)
\r
1464 CGUIFont* font = new CGUIFont(this, filename);
\r
1465 ifont = (IGUIFont*)font;
\r
1468 io::path directory;
\r
1469 core::splitFilename(filename, &directory);
\r
1470 if (!font->load(xml, directory))
\r
1477 else if (t==EGFT_VECTOR)
\r
1479 // todo: vector fonts
\r
1480 os::Printer::log("Unable to load font, XML vector fonts are not supported yet", f.NamedPath, ELL_ERROR);
\r
1482 //CGUIFontVector* font = new CGUIFontVector(Driver);
\r
1483 //ifont = (IGUIFont*)font;
\r
1484 //if (!font->load(xml))
\r
1493 CGUIFont* font = new CGUIFont(this, f.NamedPath.getPath() );
\r
1494 ifont = (IGUIFont*)font;
\r
1495 if (!font->load(f.NamedPath.getPath()))
\r
1505 Fonts.push_back(f);
\r
1511 //! add an externally loaded font
\r
1512 IGUIFont* CGUIEnvironment::addFont(const io::path& name, IGUIFont* font)
\r
1517 f.NamedPath.setPath(name);
\r
1518 s32 index = Fonts.binary_search(f);
\r
1520 return Fonts[index].Font;
\r
1522 Fonts.push_back(f);
\r
1528 //! remove loaded font
\r
1529 void CGUIEnvironment::removeFont(IGUIFont* font)
\r
1533 for ( u32 i=0; i<Fonts.size(); ++i )
\r
1535 if ( Fonts[i].Font == font )
\r
1537 Fonts[i].Font->drop();
\r
1544 //! returns default font
\r
1545 IGUIFont* CGUIEnvironment::getBuiltInFont() const
\r
1547 if (Fonts.empty())
\r
1550 return Fonts[0].Font;
\r
1554 IGUISpriteBank* CGUIEnvironment::getSpriteBank(const io::path& filename)
\r
1556 // search for the file name
\r
1559 b.NamedPath.setPath(filename);
\r
1561 s32 index = Banks.binary_search(b);
\r
1563 return Banks[index].Bank;
\r
1565 // we don't have this sprite bank, we should load it
\r
1566 if (!FileSystem->existFile(b.NamedPath.getPath()))
\r
1568 if ( filename != DefaultFontName )
\r
1570 os::Printer::log("Could not load sprite bank because the file does not exist", b.NamedPath.getPath(), ELL_DEBUG);
\r
1581 IGUISpriteBank* CGUIEnvironment::addEmptySpriteBank(const io::path& name)
\r
1583 // no duplicate names allowed
\r
1586 b.NamedPath.setPath(name);
\r
1588 const s32 index = Banks.binary_search(b);
\r
1592 // create a new sprite bank
\r
1594 b.Bank = new CGUISpriteBank(this);
\r
1595 Banks.push_back(b);
\r
1601 //! Creates the image list from the given texture.
\r
1602 IGUIImageList* CGUIEnvironment::createImageList( video::ITexture* texture,
\r
1603 core::dimension2d<s32> imageSize, bool useAlphaChannel )
\r
1605 CGUIImageList* imageList = new CGUIImageList( Driver );
\r
1606 if( !imageList->createImageList( texture, imageSize, useAlphaChannel ) )
\r
1608 imageList->drop();
\r
1615 //! Returns the root gui element.
\r
1616 IGUIElement* CGUIEnvironment::getRootGUIElement()
\r
1622 //! Returns the next element in the tab group starting at the focused element
\r
1623 IGUIElement* CGUIEnvironment::getNextElement(bool reverse, bool group)
\r
1625 // start the search at the root of the current tab group
\r
1626 IGUIElement *startPos = Focus ? Focus->getTabGroup() : 0;
\r
1627 s32 startOrder = -1;
\r
1629 // if we're searching for a group
\r
1630 if (group && startPos)
\r
1632 startOrder = startPos->getTabOrder();
\r
1635 if (!group && Focus && !Focus->isTabGroup())
\r
1637 startOrder = Focus->getTabOrder();
\r
1638 if (startOrder == -1)
\r
1640 // this element is not part of the tab cycle,
\r
1641 // but its parent might be...
\r
1642 IGUIElement *el = Focus;
\r
1643 while (el && el->getParent() && startOrder == -1)
\r
1645 el = el->getParent();
\r
1646 startOrder = el->getTabOrder();
\r
1652 if (group || !startPos)
\r
1653 startPos = this; // start at the root
\r
1655 // find the element
\r
1656 IGUIElement *closest = 0;
\r
1657 IGUIElement *first = 0;
\r
1658 startPos->getNextElement(startOrder, reverse, group, first, closest, false, (FocusFlags & EFF_CAN_FOCUS_DISABLED) != 0);
\r
1661 return closest; // we found an element
\r
1663 return first; // go to the end or the start
\r
1665 return this; // no group found? root group
\r
1670 void CGUIEnvironment::setFocusBehavior(u32 flags)
\r
1672 FocusFlags = flags;
\r
1675 u32 CGUIEnvironment::getFocusBehavior() const
\r
1677 return FocusFlags;
\r
1680 //! creates an GUI Environment
\r
1681 IGUIEnvironment* createGUIEnvironment(io::IFileSystem* fs,
\r
1682 video::IVideoDriver* Driver,
\r
1685 return new CGUIEnvironment(fs, Driver, op);
\r
1689 } // end namespace gui
\r
1690 } // end namespace irr
\r
1692 #endif // _IRR_COMPILE_WITH_GUI_
\r