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
43 #include "BuiltInFont.h"
\r
51 const io::path CGUIEnvironment::DefaultFontName = "#DefaultFont";
\r
54 CGUIEnvironment::CGUIEnvironment(io::IFileSystem* fs, video::IVideoDriver* driver, IOSOperator* op)
\r
55 : IGUIElement(EGUIET_ROOT, 0, 0, 0, core::rect<s32>(driver ? core::dimension2d<s32>(driver->getScreenSize()) : core::dimension2d<s32>(0,0))),
\r
56 Driver(driver), Hovered(0), HoveredNoSubelement(0), Focus(0), LastHoveredMousePos(0,0), CurrentSkin(0),
\r
57 FileSystem(fs), UserReceiver(0), Operator(op), FocusFlags(EFF_SET_ON_LMOUSE_DOWN|EFF_SET_ON_TAB)
\r
69 IGUIEnvironment::setDebugName("CGUIEnvironment");
\r
73 IGUIElementFactory* factory = new CDefaultGUIElementFactory(this);
\r
74 registerGUIElementFactory(factory);
\r
79 IGUISkin* skin = createSkin( gui::EGST_WINDOWS_METALLIC );
\r
83 //set tooltip default
\r
84 ToolTip.LastTime = 0;
\r
85 ToolTip.EnterTime = 0;
\r
86 ToolTip.LaunchTime = 1000;
\r
87 ToolTip.RelaunchTime = 500;
\r
88 ToolTip.Element = 0;
\r
90 // environment is root tab group
\r
97 CGUIEnvironment::~CGUIEnvironment()
\r
99 clearDeletionQueue();
\r
101 if ( HoveredNoSubelement && HoveredNoSubelement != this )
\r
103 HoveredNoSubelement->drop();
\r
104 HoveredNoSubelement = 0;
\r
107 if (Hovered && Hovered != this)
\r
119 if (ToolTip.Element)
\r
121 ToolTip.Element->drop();
\r
122 ToolTip.Element = 0;
\r
128 CurrentSkin->drop();
\r
134 // delete all sprite banks
\r
135 for (i=0; i<Banks.size(); ++i)
\r
137 Banks[i].Bank->drop();
\r
139 // delete all fonts
\r
140 for (i=0; i<Fonts.size(); ++i)
\r
141 Fonts[i].Font->drop();
\r
143 // remove all factories
\r
144 for (i=0; i<GUIElementFactoryList.size(); ++i)
\r
145 GUIElementFactoryList[i]->drop();
\r
155 FileSystem->drop();
\r
167 void CGUIEnvironment::loadBuiltInFont()
\r
169 io::IReadFile* file = FileSystem->createMemoryReadFile(BuiltInFontData,
\r
170 BuiltInFontDataSize, DefaultFontName, false);
\r
172 CGUIFont* font = new CGUIFont(this, DefaultFontName );
\r
173 if (!font->load(file))
\r
175 os::Printer::log("Error: Could not load built-in Font. Did you compile without the BMP loader?", ELL_ERROR);
\r
182 f.NamedPath.setPath(DefaultFontName);
\r
184 Fonts.push_back(f);
\r
190 //! draws all gui elements
\r
191 void CGUIEnvironment::drawAll(bool useScreenSize)
\r
193 if (useScreenSize && Driver)
\r
195 core::dimension2d<s32> dim(Driver->getScreenSize());
\r
196 if (AbsoluteRect.LowerRightCorner.X != dim.Width ||
\r
197 AbsoluteRect.UpperLeftCorner.X != 0 ||
\r
198 AbsoluteRect.LowerRightCorner.Y != dim.Height ||
\r
199 AbsoluteRect.UpperLeftCorner.Y != 0
\r
202 setRelativePosition(core::recti(0,0,dim.Width, dim.Height));
\r
206 // make sure tooltip is always on top
\r
207 if (ToolTip.Element)
\r
208 bringToFront(ToolTip.Element);
\r
211 OnPostRender ( os::Timer::getTime () );
\r
213 clearDeletionQueue();
\r
217 //! sets the focus to an element
\r
218 bool CGUIEnvironment::setFocus(IGUIElement* element)
\r
220 if (Focus == element)
\r
225 // GUI Environment should just reset the focus to 0
\r
226 if (element == this)
\r
229 // stop element from being deleted
\r
233 // focus may change or be removed in this call
\r
234 IGUIElement *currentFocus = 0;
\r
237 currentFocus = Focus;
\r
238 currentFocus->grab();
\r
240 e.EventType = EET_GUI_EVENT;
\r
241 e.GUIEvent.Caller = Focus;
\r
242 e.GUIEvent.Element = element;
\r
243 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
244 if (Focus->OnEvent(e))
\r
248 currentFocus->drop();
\r
251 currentFocus->drop();
\r
257 currentFocus = Focus;
\r
259 currentFocus->grab();
\r
261 // send focused event
\r
263 e.EventType = EET_GUI_EVENT;
\r
264 e.GUIEvent.Caller = element;
\r
265 e.GUIEvent.Element = Focus;
\r
266 e.GUIEvent.EventType = EGET_ELEMENT_FOCUSED;
\r
267 if (element->OnEvent(e))
\r
272 currentFocus->drop();
\r
278 currentFocus->drop();
\r
283 // element is the new focus so it doesn't have to be dropped
\r
290 //! returns the element with the focus
\r
291 IGUIElement* CGUIEnvironment::getFocus() const
\r
296 //! returns the element last known to be under the mouse cursor
\r
297 IGUIElement* CGUIEnvironment::getHovered() const
\r
303 //! removes the focus from an element
\r
304 bool CGUIEnvironment::removeFocus(IGUIElement* element)
\r
306 if (Focus && Focus==element)
\r
309 e.EventType = EET_GUI_EVENT;
\r
310 e.GUIEvent.Caller = Focus;
\r
311 e.GUIEvent.Element = 0;
\r
312 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
313 if (Focus->OnEvent(e))
\r
328 //! Returns whether the element has focus
\r
329 bool CGUIEnvironment::hasFocus(const IGUIElement* element, bool checkSubElements) const
\r
331 if (element == Focus)
\r
334 if ( !checkSubElements || !element )
\r
337 IGUIElement* f = Focus;
\r
338 while ( f && f->isSubElement() )
\r
340 f = f->getParent();
\r
341 if ( f == element )
\r
348 //! returns the current video driver
\r
349 video::IVideoDriver* CGUIEnvironment::getVideoDriver() const
\r
355 //! returns the current file system
\r
356 io::IFileSystem* CGUIEnvironment::getFileSystem() const
\r
362 //! returns a pointer to the OS operator
\r
363 IOSOperator* CGUIEnvironment::getOSOperator() const
\r
369 //! clear all GUI elements
\r
370 void CGUIEnvironment::clear()
\r
372 // Remove the focus
\r
379 if (Hovered && Hovered != this)
\r
384 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
386 HoveredNoSubelement->drop();
\r
387 HoveredNoSubelement = 0;
\r
390 // get the root's children in case the root changes in future
\r
391 const core::list<IGUIElement*>& children = getRootGUIElement()->getChildren();
\r
393 while (!children.empty())
\r
394 (*children.getLast())->remove();
\r
398 //! called by ui if an event happened.
\r
399 bool CGUIEnvironment::OnEvent(const SEvent& event)
\r
404 && (event.EventType != EET_MOUSE_INPUT_EVENT)
\r
405 && (event.EventType != EET_KEY_INPUT_EVENT)
\r
406 && (event.EventType != EET_GUI_EVENT || event.GUIEvent.Caller != this))
\r
408 ret = UserReceiver->OnEvent(event);
\r
415 void CGUIEnvironment::OnPostRender( u32 time )
\r
418 if ( ToolTip.Element == 0 &&
\r
419 HoveredNoSubelement && HoveredNoSubelement != this &&
\r
420 (time - ToolTip.EnterTime >= ToolTip.LaunchTime
\r
421 || (time - ToolTip.LastTime >= ToolTip.RelaunchTime && time - ToolTip.LastTime < ToolTip.LaunchTime)) &&
\r
422 HoveredNoSubelement->getToolTipText().size() &&
\r
424 getSkin()->getFont(EGDF_TOOLTIP)
\r
427 core::rect<s32> pos;
\r
429 pos.UpperLeftCorner = LastHoveredMousePos;
\r
430 core::dimension2du dim = getSkin()->getFont(EGDF_TOOLTIP)->getDimension(HoveredNoSubelement->getToolTipText().c_str());
\r
431 dim.Width += getSkin()->getSize(EGDS_TEXT_DISTANCE_X)*2;
\r
432 dim.Height += getSkin()->getSize(EGDS_TEXT_DISTANCE_Y)*2;
\r
434 pos.UpperLeftCorner.Y -= dim.Height+1;
\r
435 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + dim.Height-1;
\r
436 pos.LowerRightCorner.X = pos.UpperLeftCorner.X + dim.Width;
\r
438 pos.constrainTo(getAbsolutePosition());
\r
440 ToolTip.Element = addStaticText(HoveredNoSubelement->getToolTipText().c_str(), pos, true, true, this, -1, true);
\r
441 ToolTip.Element->setOverrideColor(getSkin()->getColor(EGDC_TOOLTIP));
\r
442 ToolTip.Element->setBackgroundColor(getSkin()->getColor(EGDC_TOOLTIP_BACKGROUND));
\r
443 ToolTip.Element->setOverrideFont(getSkin()->getFont(EGDF_TOOLTIP));
\r
444 ToolTip.Element->setSubElement(true);
\r
445 ToolTip.Element->grab();
\r
447 s32 textHeight = ToolTip.Element->getTextHeight();
\r
448 pos = ToolTip.Element->getRelativePosition();
\r
449 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + textHeight;
\r
450 ToolTip.Element->setRelativePosition(pos);
\r
453 if (ToolTip.Element && ToolTip.Element->isVisible() ) // (isVisible() check only because we might use visibility for ToolTip one day)
\r
455 ToolTip.LastTime = time;
\r
457 // got invisible or removed in the meantime?
\r
458 if ( !HoveredNoSubelement ||
\r
459 !HoveredNoSubelement->isVisible() ||
\r
460 !HoveredNoSubelement->getParent()
\r
461 ) // got invisible or removed in the meantime?
\r
463 ToolTip.Element->remove();
\r
464 ToolTip.Element->drop();
\r
465 ToolTip.Element = 0;
\r
469 IGUIElement::OnPostRender ( time );
\r
472 void CGUIEnvironment::addToDeletionQueue(IGUIElement* element)
\r
478 DeletionQueue.push_back(element);
\r
481 void CGUIEnvironment::clearDeletionQueue()
\r
483 if (DeletionQueue.empty())
\r
486 for (u32 i=0; i<DeletionQueue.size(); ++i)
\r
488 DeletionQueue[i]->remove();
\r
489 DeletionQueue[i]->drop();
\r
492 DeletionQueue.clear();
\r
496 void CGUIEnvironment::updateHoveredElement(core::position2d<s32> mousePos)
\r
498 IGUIElement* lastHovered = Hovered;
\r
499 IGUIElement* lastHoveredNoSubelement = HoveredNoSubelement;
\r
500 LastHoveredMousePos = mousePos;
\r
502 Hovered = getElementFromPoint(mousePos);
\r
504 if ( ToolTip.Element && Hovered == ToolTip.Element )
\r
506 // When the mouse is over the ToolTip we remove that so it will be re-created at a new position.
\r
507 // Note that ToolTip.EnterTime does not get changed here, so it will be re-created at once.
\r
508 ToolTip.Element->remove();
\r
509 ToolTip.Element->drop();
\r
510 ToolTip.Element = 0;
\r
512 // Get the real Hovered
\r
513 Hovered = getElementFromPoint(mousePos);
\r
516 // for tooltips we want the element itself and not some of it's subelements
\r
517 HoveredNoSubelement = Hovered;
\r
518 while ( HoveredNoSubelement && HoveredNoSubelement->isSubElement() )
\r
520 HoveredNoSubelement = HoveredNoSubelement->getParent();
\r
523 if (Hovered && Hovered != this)
\r
525 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
526 HoveredNoSubelement->grab();
\r
528 if (Hovered != lastHovered)
\r
531 event.EventType = EET_GUI_EVENT;
\r
535 event.GUIEvent.Caller = lastHovered;
\r
536 event.GUIEvent.Element = 0;
\r
537 event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
\r
538 lastHovered->OnEvent(event);
\r
543 event.GUIEvent.Caller = Hovered;
\r
544 event.GUIEvent.Element = Hovered;
\r
545 event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
\r
546 Hovered->OnEvent(event);
\r
550 if ( lastHoveredNoSubelement != HoveredNoSubelement )
\r
552 if (ToolTip.Element)
\r
554 ToolTip.Element->remove();
\r
555 ToolTip.Element->drop();
\r
556 ToolTip.Element = 0;
\r
559 if ( HoveredNoSubelement )
\r
561 u32 now = os::Timer::getTime();
\r
562 ToolTip.EnterTime = now;
\r
566 if (lastHovered && lastHovered != this)
\r
567 lastHovered->drop();
\r
568 if (lastHoveredNoSubelement && lastHoveredNoSubelement != this)
\r
569 lastHoveredNoSubelement->drop();
\r
573 //! This sets a new event receiver for gui events. Usually you do not have to
\r
574 //! use this method, it is used by the internal engine.
\r
575 void CGUIEnvironment::setUserEventReceiver(IEventReceiver* evr)
\r
577 UserReceiver = evr;
\r
581 //! posts an input event to the environment
\r
582 bool CGUIEnvironment::postEventFromUser(const SEvent& event)
\r
584 switch(event.EventType)
\r
586 case EET_GUI_EVENT:
\r
588 // hey, why is the user sending gui events..?
\r
592 case EET_MOUSE_INPUT_EVENT:
\r
594 updateHoveredElement(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
\r
596 if ( Hovered != Focus )
\r
598 IGUIElement * focusCandidate = Hovered;
\r
600 // Only allow enabled elements to be focused (unless EFF_CAN_FOCUS_DISABLED is set)
\r
601 if ( Hovered && !Hovered->isEnabled() && !(FocusFlags & EFF_CAN_FOCUS_DISABLED))
\r
602 focusCandidate = NULL; // we still remove focus from the active element
\r
604 // Please don't merge this into a single if clause, it's easier to debug the way it is
\r
605 if (FocusFlags & EFF_SET_ON_LMOUSE_DOWN &&
\r
606 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
\r
608 setFocus(focusCandidate);
\r
610 else if ( FocusFlags & EFF_SET_ON_RMOUSE_DOWN &&
\r
611 event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN )
\r
613 setFocus(focusCandidate);
\r
615 else if ( FocusFlags & EFF_SET_ON_MOUSE_OVER &&
\r
616 event.MouseInput.Event == EMIE_MOUSE_MOVED )
\r
618 setFocus(focusCandidate);
\r
622 // sending input to focus
\r
623 if (Focus && Focus->OnEvent(event))
\r
626 // focus could have died in last call
\r
627 if (!Focus && Hovered)
\r
629 return Hovered->OnEvent(event);
\r
633 case EET_KEY_INPUT_EVENT:
\r
635 if (Focus && Focus->OnEvent(event))
\r
638 // For keys we handle the event before changing focus to give elements the chance for catching the TAB
\r
639 // Send focus changing event
\r
640 if (FocusFlags & EFF_SET_ON_TAB &&
\r
641 event.EventType == EET_KEY_INPUT_EVENT &&
\r
642 event.KeyInput.PressedDown &&
\r
643 event.KeyInput.Key == KEY_TAB)
\r
645 IGUIElement *next = getNextElement(event.KeyInput.Shift, event.KeyInput.Control);
\r
646 if (next && next != Focus)
\r
648 if (setFocus(next))
\r
654 case EET_STRING_INPUT_EVENT:
\r
655 if (Focus && Focus->OnEvent(event))
\r
666 //! returns the current gui skin
\r
667 IGUISkin* CGUIEnvironment::getSkin() const
\r
669 return CurrentSkin;
\r
673 //! Sets a new GUI Skin
\r
674 void CGUIEnvironment::setSkin(IGUISkin* skin)
\r
676 if (CurrentSkin==skin)
\r
680 CurrentSkin->drop();
\r
682 CurrentSkin = skin;
\r
685 CurrentSkin->grab();
\r
689 //! Creates a new GUI Skin based on a template.
\r
690 /** \return Returns a pointer to the created skin.
\r
691 If you no longer need the skin, you should call IGUISkin::drop().
\r
692 See IReferenceCounted::drop() for more information. */
\r
693 IGUISkin* CGUIEnvironment::createSkin(EGUI_SKIN_TYPE type)
\r
695 IGUISkin* skin = new CGUISkin(type, Driver);
\r
697 IGUIFont* builtinfont = getBuiltInFont();
\r
698 IGUIFontBitmap* bitfont = 0;
\r
699 if (builtinfont && builtinfont->getType() == EGFT_BITMAP)
\r
700 bitfont = (IGUIFontBitmap*)builtinfont;
\r
702 IGUISpriteBank* bank = 0;
\r
703 skin->setFont(builtinfont);
\r
706 bank = bitfont->getSpriteBank();
\r
708 skin->setSpriteBank(bank);
\r
714 //! Returns the default element factory which can create all built in elements
\r
715 IGUIElementFactory* CGUIEnvironment::getDefaultGUIElementFactory() const
\r
717 return getGUIElementFactory(0);
\r
721 //! Adds an element factory to the gui environment.
\r
722 /** Use this to extend the gui environment with new element types which it should be
\r
723 able to create automatically, for example when loading data from xml files. */
\r
724 void CGUIEnvironment::registerGUIElementFactory(IGUIElementFactory* factoryToAdd)
\r
728 factoryToAdd->grab();
\r
729 GUIElementFactoryList.push_back(factoryToAdd);
\r
734 //! Returns amount of registered scene node factories.
\r
735 u32 CGUIEnvironment::getRegisteredGUIElementFactoryCount() const
\r
737 return GUIElementFactoryList.size();
\r
741 //! Returns a scene node factory by index
\r
742 IGUIElementFactory* CGUIEnvironment::getGUIElementFactory(u32 index) const
\r
744 if (index < GUIElementFactoryList.size())
\r
745 return GUIElementFactoryList[index];
\r
751 //! adds a GUI Element using its name
\r
752 IGUIElement* CGUIEnvironment::addGUIElement(const c8* elementName, IGUIElement* parent)
\r
754 IGUIElement* node=0;
\r
759 for (s32 i=GUIElementFactoryList.size()-1; i>=0 && !node; --i)
\r
760 node = GUIElementFactoryList[i]->addGUIElement(elementName, parent);
\r
767 //! Saves the current gui into a file.
\r
768 //! \param filename: Name of the file .
\r
769 bool CGUIEnvironment::saveGUI(const io::path& filename, IGUIElement* start)
\r
771 io::IWriteFile* file = FileSystem->createAndWriteFile(filename);
\r
777 bool ret = saveGUI(file, start);
\r
783 //! Saves the current gui into a file.
\r
784 bool CGUIEnvironment::saveGUI(io::IWriteFile* file, IGUIElement* start)
\r
790 //! Loads the gui. Note that the current gui is not cleared before.
\r
791 //! \param filename: Name of the file.
\r
792 bool CGUIEnvironment::loadGUI(const io::path& filename, IGUIElement* parent)
\r
794 io::IReadFile* read = FileSystem->createAndOpenFile(filename);
\r
797 os::Printer::log("Unable to open gui file", filename, ELL_ERROR);
\r
801 bool ret = loadGUI(read, parent);
\r
808 //! Loads the gui. Note that the current gui is not cleared before.
\r
809 bool CGUIEnvironment::loadGUI(io::IReadFile* file, IGUIElement* parent)
\r
815 //! Writes attributes of the environment
\r
816 void CGUIEnvironment::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
\r
818 IGUISkin* skin = getSkin();
\r
822 out->addEnum("Skin", getSkin()->getType(), GUISkinTypeNames);
\r
823 skin->serializeAttributes(out, options);
\r
828 //! Reads attributes of the environment
\r
829 void CGUIEnvironment::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
\r
831 if (in->existsAttribute("Skin"))
\r
833 IGUISkin *skin = getSkin();
\r
835 EGUI_SKIN_TYPE t = (EGUI_SKIN_TYPE) in->getAttributeAsEnumeration("Skin",GUISkinTypeNames);
\r
836 if ( !skin || t != skin->getType())
\r
838 skin = createSkin(t);
\r
847 skin->deserializeAttributes(in, options);
\r
852 RelativeRect = AbsoluteRect =
\r
853 core::rect<s32>(Driver ? core::dimension2di(Driver->getScreenSize()) : core::dimension2d<s32>(0,0));
\r
857 //! adds a button. The returned pointer must not be dropped.
\r
858 IGUIButton* CGUIEnvironment::addButton(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, const wchar_t *tooltiptext)
\r
860 IGUIButton* button = new CGUIButton(this, parent ? parent : this, id, rectangle);
\r
862 button->setText(text);
\r
865 button->setToolTipText ( tooltiptext );
\r
872 //! adds a window. The returned pointer must not be dropped.
\r
873 IGUIWindow* CGUIEnvironment::addWindow(const core::rect<s32>& rectangle, bool modal,
\r
874 const wchar_t* text, IGUIElement* parent, s32 id)
\r
876 parent = parent ? parent : this;
\r
878 IGUIWindow* win = new CGUIWindow(this, parent, id, rectangle);
\r
880 win->setText(text);
\r
885 // 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
886 // careful not to get virtual function call, like OnEvent, in the window.
\r
887 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
888 modalScreen->drop();
\r
889 modalScreen->addChild(win);
\r
896 //! adds a modal screen. The returned pointer must not be dropped.
\r
897 IGUIElement* CGUIEnvironment::addModalScreen(IGUIElement* parent)
\r
899 parent = parent ? parent : this;
\r
901 IGUIElement *win = new CGUIModalScreen(this, parent, -1);
\r
908 //! Adds a message box.
\r
909 IGUIWindow* CGUIEnvironment::addMessageBox(const wchar_t* caption, const wchar_t* text,
\r
910 bool modal, s32 flag, IGUIElement* parent, s32 id, video::ITexture* image)
\r
915 parent = parent ? parent : this;
\r
917 core::rect<s32> rect;
\r
918 core::dimension2d<u32> screenDim, msgBoxDim;
\r
920 screenDim.Width = parent->getAbsolutePosition().getWidth();
\r
921 screenDim.Height = parent->getAbsolutePosition().getHeight();
\r
922 msgBoxDim.Width = 2;
\r
923 msgBoxDim.Height = 2;
\r
925 rect.UpperLeftCorner.X = (screenDim.Width - msgBoxDim.Width) / 2;
\r
926 rect.UpperLeftCorner.Y = (screenDim.Height - msgBoxDim.Height) / 2;
\r
927 rect.LowerRightCorner.X = rect.UpperLeftCorner.X + msgBoxDim.Width;
\r
928 rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + msgBoxDim.Height;
\r
930 IGUIWindow* win = new CGUIMessageBox(this, caption, text, flag,
\r
931 parent, id, rect, image);
\r
936 // 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
937 // careful not to get virtual function call, like OnEvent, in the CGUIMessageBox.
\r
938 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
939 modalScreen->drop();
\r
940 modalScreen->addChild( win );
\r
948 //! adds a scrollbar. The returned pointer must not be dropped.
\r
949 IGUIScrollBar* CGUIEnvironment::addScrollBar(bool horizontal, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id)
\r
951 IGUIScrollBar* bar = new CGUIScrollBar(horizontal, this, parent ? parent : this, id, rectangle);
\r
956 //! Adds a table to the environment
\r
957 IGUITable* CGUIEnvironment::addTable(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, bool drawBackground)
\r
959 CGUITable* b = new CGUITable(this, parent ? parent : this, id, rectangle, true, drawBackground, false);
\r
964 //! Adds an element to display the information from the Irrlicht profiler
\r
965 IGUIProfiler* CGUIEnvironment::addProfilerDisplay(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id)
\r
967 CGUIProfiler* p = new CGUIProfiler(this, parent ? parent : this, id, rectangle, NULL);
\r
972 //! Adds an image element.
\r
973 IGUIImage* CGUIEnvironment::addImage(video::ITexture* image, core::position2d<s32> pos,
\r
974 bool useAlphaChannel, IGUIElement* parent, s32 id, const wchar_t* text)
\r
976 core::dimension2d<s32> sz(0,0);
\r
978 sz = core::dimension2d<s32>(image->getOriginalSize());
\r
980 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
981 id, core::rect<s32>(pos, sz));
\r
984 img->setText(text);
\r
986 if (useAlphaChannel)
\r
987 img->setUseAlphaChannel(true);
\r
990 img->setImage(image);
\r
997 //! adds an image. The returned pointer must not be dropped.
\r
998 IGUIImage* CGUIEnvironment::addImage(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, bool useAlphaChannel)
\r
1000 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
1004 img->setText(text);
\r
1006 if ( useAlphaChannel )
\r
1007 img->setUseAlphaChannel(true);
\r
1014 //! adds an mesh viewer. The returned pointer must not be dropped.
\r
1015 IGUIMeshViewer* CGUIEnvironment::addMeshViewer(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text)
\r
1017 IGUIMeshViewer* v = new CGUIMeshViewer(this, parent ? parent : this,
\r
1028 //! adds a checkbox
\r
1029 IGUICheckBox* CGUIEnvironment::addCheckBox(bool checked, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text)
\r
1031 IGUICheckBox* b = new CGUICheckBox(checked, this,
\r
1032 parent ? parent : this , id , rectangle);
\r
1042 //! adds a list box
\r
1043 IGUIListBox* CGUIEnvironment::addListBox(const core::rect<s32>& rectangle,
\r
1044 IGUIElement* parent, s32 id, bool drawBackground)
\r
1046 IGUIListBox* b = new CGUIListBox(this, parent ? parent : this, id, rectangle,
\r
1047 true, drawBackground, false);
\r
1049 if (CurrentSkin && CurrentSkin->getSpriteBank())
\r
1051 b->setSpriteBank(CurrentSkin->getSpriteBank());
\r
1053 else if (getBuiltInFont() && getBuiltInFont()->getType() == EGFT_BITMAP)
\r
1055 b->setSpriteBank( ((IGUIFontBitmap*)getBuiltInFont())->getSpriteBank());
\r
1062 //! adds a tree view
\r
1063 IGUITreeView* CGUIEnvironment::addTreeView(const core::rect<s32>& rectangle,
\r
1064 IGUIElement* parent, s32 id,
\r
1065 bool drawBackground,
\r
1066 bool scrollBarVertical, bool scrollBarHorizontal)
\r
1068 IGUITreeView* b = new CGUITreeView(this, parent ? parent : this, id, rectangle,
\r
1069 true, drawBackground, scrollBarVertical, scrollBarHorizontal);
\r
1071 b->setIconFont ( getBuiltInFont () );
\r
1076 //! adds a file open dialog. The returned pointer must not be dropped.
\r
1077 IGUIFileOpenDialog* CGUIEnvironment::addFileOpenDialog(const wchar_t* title,
\r
1078 bool modal, IGUIElement* parent, s32 id,
\r
1079 bool restoreCWD, io::path::char_type* startDir)
\r
1081 parent = parent ? parent : this;
\r
1083 IGUIFileOpenDialog* d = new CGUIFileOpenDialog(title, this, parent, id,
\r
1084 restoreCWD, startDir);
\r
1089 // 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
1090 // careful not to get virtual function call, like OnEvent, in the window.
\r
1091 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
1092 modalScreen->drop();
\r
1093 modalScreen->addChild(d);
\r
1100 //! adds a color select dialog. The returned pointer must not be dropped.
\r
1101 IGUIColorSelectDialog* CGUIEnvironment::addColorSelectDialog(const wchar_t* title,
\r
1102 bool modal, IGUIElement* parent, s32 id)
\r
1104 parent = parent ? parent : this;
\r
1106 IGUIColorSelectDialog* d = new CGUIColorSelectDialog( title,
\r
1107 this, parent, id);
\r
1112 // 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
1113 // careful not to get virtual function call, like OnEvent, in the window.
\r
1114 CGUIModalScreen * modalScreen = new CGUIModalScreen(this, parent, -1);
\r
1115 modalScreen->drop();
\r
1116 modalScreen->addChild(d);
\r
1123 //! adds a static text. The returned pointer must not be dropped.
\r
1124 IGUIStaticText* CGUIEnvironment::addStaticText(const wchar_t* text,
\r
1125 const core::rect<s32>& rectangle,
\r
1126 bool border, bool wordWrap,
\r
1127 IGUIElement* parent, s32 id, bool background)
\r
1129 IGUIStaticText* d = new CGUIStaticText(text, border, this,
\r
1130 parent ? parent : this, id, rectangle, background);
\r
1132 d->setWordWrap(wordWrap);
\r
1139 //! Adds an edit box. The returned pointer must not be dropped.
\r
1140 IGUIEditBox* CGUIEnvironment::addEditBox(const wchar_t* text,
\r
1141 const core::rect<s32>& rectangle, bool border,
\r
1142 IGUIElement* parent, s32 id)
\r
1144 IGUIEditBox* d = new CGUIEditBox(text, border, this,
\r
1145 parent ? parent : this, id, rectangle);
\r
1152 //! Adds a spin box to the environment
\r
1153 IGUISpinBox* CGUIEnvironment::addSpinBox(const wchar_t* text,
\r
1154 const core::rect<s32> &rectangle,
\r
1155 bool border,IGUIElement* parent, s32 id)
\r
1157 IGUISpinBox* d = new CGUISpinBox(text, border,this,
\r
1158 parent ? parent : this, id, rectangle);
\r
1165 //! Adds a tab control to the environment.
\r
1166 IGUITabControl* CGUIEnvironment::addTabControl(const core::rect<s32>& rectangle,
\r
1167 IGUIElement* parent, bool fillbackground, bool border, s32 id)
\r
1169 IGUITabControl* t = new CGUITabControl(this, parent ? parent : this,
\r
1170 rectangle, fillbackground, border, id);
\r
1176 //! Adds tab to the environment.
\r
1177 IGUITab* CGUIEnvironment::addTab(const core::rect<s32>& rectangle,
\r
1178 IGUIElement* parent, s32 id)
\r
1180 IGUITab* t = new CGUITab(this, parent ? parent : this,
\r
1187 //! Adds a context menu to the environment.
\r
1188 IGUIContextMenu* CGUIEnvironment::addContextMenu(const core::rect<s32>& rectangle,
\r
1189 IGUIElement* parent, s32 id)
\r
1191 IGUIContextMenu* c = new CGUIContextMenu(this,
\r
1192 parent ? parent : this, id, rectangle, true);
\r
1198 //! Adds a menu to the environment.
\r
1199 IGUIContextMenu* CGUIEnvironment::addMenu(IGUIElement* parent, s32 id)
\r
1204 IGUIContextMenu* c = new CGUIMenu(this,
\r
1205 parent, id, core::rect<s32>(0,0,
\r
1206 parent->getAbsolutePosition().getWidth(),
\r
1207 parent->getAbsolutePosition().getHeight()));
\r
1214 //! Adds a toolbar to the environment. It is like a menu is always placed on top
\r
1215 //! in its parent, and contains buttons.
\r
1216 IGUIToolBar* CGUIEnvironment::addToolBar(IGUIElement* parent, s32 id)
\r
1221 IGUIToolBar* b = new CGUIToolBar(this, parent, id, core::rect<s32>(0,0,10,10));
\r
1227 //! Adds an element for fading in or out.
\r
1228 IGUIInOutFader* CGUIEnvironment::addInOutFader(const core::rect<s32>* rectangle, IGUIElement* parent, s32 id)
\r
1230 core::rect<s32> rect;
\r
1233 rect = *rectangle;
\r
1235 rect = core::rect<s32>(core::dimension2di(Driver->getScreenSize()));
\r
1240 IGUIInOutFader* fader = new CGUIInOutFader(this, parent, id, rect);
\r
1246 //! Adds a combo box to the environment.
\r
1247 IGUIComboBox* CGUIEnvironment::addComboBox(const core::rect<s32>& rectangle,
\r
1248 IGUIElement* parent, s32 id)
\r
1250 IGUIComboBox* t = new CGUIComboBox(this, parent ? parent : this,
\r
1257 //! returns the font
\r
1258 IGUIFont* CGUIEnvironment::getFont(const io::path& filename)
\r
1260 // search existing font
\r
1263 f.NamedPath.setPath(filename);
\r
1265 s32 index = Fonts.binary_search(f);
\r
1267 return Fonts[index].Font;
\r
1269 // font doesn't exist, attempt to load it
\r
1271 // does the file exist?
\r
1273 if (!FileSystem->existFile(filename))
\r
1275 os::Printer::log("Could not load font because the file does not exist", f.NamedPath.getPath(), ELL_ERROR);
\r
1279 IGUIFont* ifont=0;
\r
1282 CGUIFont* font = new CGUIFont(this, filename);
\r
1283 ifont = (IGUIFont*)font;
\r
1286 io::path directory;
\r
1287 core::splitFilename(filename, &directory);
\r
1288 if (!font->load(xml, directory))
\r
1301 CGUIFont* font = new CGUIFont(this, f.NamedPath.getPath() );
\r
1302 ifont = (IGUIFont*)font;
\r
1303 if (!font->load(f.NamedPath.getPath()))
\r
1313 Fonts.push_back(f);
\r
1319 //! add an externally loaded font
\r
1320 IGUIFont* CGUIEnvironment::addFont(const io::path& name, IGUIFont* font)
\r
1325 f.NamedPath.setPath(name);
\r
1326 s32 index = Fonts.binary_search(f);
\r
1328 return Fonts[index].Font;
\r
1330 Fonts.push_back(f);
\r
1336 //! remove loaded font
\r
1337 void CGUIEnvironment::removeFont(IGUIFont* font)
\r
1341 for ( u32 i=0; i<Fonts.size(); ++i )
\r
1343 if ( Fonts[i].Font == font )
\r
1345 Fonts[i].Font->drop();
\r
1352 //! returns default font
\r
1353 IGUIFont* CGUIEnvironment::getBuiltInFont() const
\r
1355 if (Fonts.empty())
\r
1358 return Fonts[0].Font;
\r
1362 IGUISpriteBank* CGUIEnvironment::getSpriteBank(const io::path& filename)
\r
1364 // search for the file name
\r
1367 b.NamedPath.setPath(filename);
\r
1369 s32 index = Banks.binary_search(b);
\r
1371 return Banks[index].Bank;
\r
1373 // we don't have this sprite bank, we should load it
\r
1374 if (!FileSystem->existFile(b.NamedPath.getPath()))
\r
1376 if ( filename != DefaultFontName )
\r
1378 os::Printer::log("Could not load sprite bank because the file does not exist", b.NamedPath.getPath(), ELL_DEBUG);
\r
1389 IGUISpriteBank* CGUIEnvironment::addEmptySpriteBank(const io::path& name)
\r
1391 // no duplicate names allowed
\r
1394 b.NamedPath.setPath(name);
\r
1396 const s32 index = Banks.binary_search(b);
\r
1400 // create a new sprite bank
\r
1402 b.Bank = new CGUISpriteBank(this);
\r
1403 Banks.push_back(b);
\r
1409 //! Creates the image list from the given texture.
\r
1410 IGUIImageList* CGUIEnvironment::createImageList( video::ITexture* texture,
\r
1411 core::dimension2d<s32> imageSize, bool useAlphaChannel )
\r
1413 CGUIImageList* imageList = new CGUIImageList( Driver );
\r
1414 if( !imageList->createImageList( texture, imageSize, useAlphaChannel ) )
\r
1416 imageList->drop();
\r
1423 //! Returns the root gui element.
\r
1424 IGUIElement* CGUIEnvironment::getRootGUIElement()
\r
1430 //! Returns the next element in the tab group starting at the focused element
\r
1431 IGUIElement* CGUIEnvironment::getNextElement(bool reverse, bool group)
\r
1433 // start the search at the root of the current tab group
\r
1434 IGUIElement *startPos = Focus ? Focus->getTabGroup() : 0;
\r
1435 s32 startOrder = -1;
\r
1437 // if we're searching for a group
\r
1438 if (group && startPos)
\r
1440 startOrder = startPos->getTabOrder();
\r
1443 if (!group && Focus && !Focus->isTabGroup())
\r
1445 startOrder = Focus->getTabOrder();
\r
1446 if (startOrder == -1)
\r
1448 // this element is not part of the tab cycle,
\r
1449 // but its parent might be...
\r
1450 IGUIElement *el = Focus;
\r
1451 while (el && el->getParent() && startOrder == -1)
\r
1453 el = el->getParent();
\r
1454 startOrder = el->getTabOrder();
\r
1460 if (group || !startPos)
\r
1461 startPos = this; // start at the root
\r
1463 // find the element
\r
1464 IGUIElement *closest = 0;
\r
1465 IGUIElement *first = 0;
\r
1466 startPos->getNextElement(startOrder, reverse, group, first, closest, false, (FocusFlags & EFF_CAN_FOCUS_DISABLED) != 0);
\r
1469 return closest; // we found an element
\r
1471 return first; // go to the end or the start
\r
1473 return this; // no group found? root group
\r
1478 void CGUIEnvironment::setFocusBehavior(u32 flags)
\r
1480 FocusFlags = flags;
\r
1483 u32 CGUIEnvironment::getFocusBehavior() const
\r
1485 return FocusFlags;
\r
1488 //! creates an GUI Environment
\r
1489 IGUIEnvironment* createGUIEnvironment(io::IFileSystem* fs,
\r
1490 video::IVideoDriver* Driver,
\r
1493 return new CGUIEnvironment(fs, Driver, op);
\r
1497 } // end namespace gui
\r
1498 } // end namespace irr
\r
1500 #endif // _IRR_COMPILE_WITH_GUI_
\r