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 "CGUIScrollBar.h"
\r
15 #include "CGUIFont.h"
\r
16 #include "CGUISpriteBank.h"
\r
17 #include "CGUIImage.h"
\r
18 #include "CGUICheckBox.h"
\r
19 #include "CGUIListBox.h"
\r
20 #include "CGUIImageList.h"
\r
21 #include "CGUIFileOpenDialog.h"
\r
22 #include "CGUIStaticText.h"
\r
23 #include "CGUIEditBox.h"
\r
24 #include "CGUITabControl.h"
\r
25 #include "CGUIComboBox.h"
\r
27 #include "IWriteFile.h"
\r
29 #include "BuiltInFont.h"
\r
37 const io::path CGUIEnvironment::DefaultFontName = "#DefaultFont";
\r
40 CGUIEnvironment::CGUIEnvironment(io::IFileSystem* fs, video::IVideoDriver* driver, IOSOperator* op)
\r
41 : IGUIElement(EGUIET_ROOT, 0, 0, 0, core::rect<s32>(driver ? core::dimension2d<s32>(driver->getScreenSize()) : core::dimension2d<s32>(0,0))),
\r
42 Driver(driver), Hovered(0), HoveredNoSubelement(0), Focus(0), LastHoveredMousePos(0,0), CurrentSkin(0),
\r
43 FileSystem(fs), UserReceiver(0), Operator(op), FocusFlags(EFF_SET_ON_LMOUSE_DOWN|EFF_SET_ON_TAB)
\r
55 IGUIEnvironment::setDebugName("CGUIEnvironment");
\r
60 IGUISkin* skin = createSkin( gui::EGST_WINDOWS_METALLIC );
\r
64 //set tooltip default
\r
65 ToolTip.LastTime = 0;
\r
66 ToolTip.EnterTime = 0;
\r
67 ToolTip.LaunchTime = 1000;
\r
68 ToolTip.RelaunchTime = 500;
\r
69 ToolTip.Element = 0;
\r
71 // environment is root tab group
\r
78 CGUIEnvironment::~CGUIEnvironment()
\r
80 clearDeletionQueue();
\r
82 if ( HoveredNoSubelement && HoveredNoSubelement != this )
\r
84 HoveredNoSubelement->drop();
\r
85 HoveredNoSubelement = 0;
\r
88 if (Hovered && Hovered != this)
\r
100 if (ToolTip.Element)
\r
102 ToolTip.Element->drop();
\r
103 ToolTip.Element = 0;
\r
109 CurrentSkin->drop();
\r
115 // delete all sprite banks
\r
116 for (i=0; i<Banks.size(); ++i)
\r
118 Banks[i].Bank->drop();
\r
120 // delete all fonts
\r
121 for (i=0; i<Fonts.size(); ++i)
\r
122 Fonts[i].Font->drop();
\r
132 FileSystem->drop();
\r
144 void CGUIEnvironment::loadBuiltInFont()
\r
146 io::IReadFile* file = FileSystem->createMemoryReadFile(BuiltInFontData,
\r
147 BuiltInFontDataSize, DefaultFontName, false);
\r
149 CGUIFont* font = new CGUIFont(this, DefaultFontName );
\r
150 if (!font->load(file))
\r
152 os::Printer::log("Error: Could not load built-in Font. Did you compile without the BMP loader?", ELL_ERROR);
\r
159 f.NamedPath.setPath(DefaultFontName);
\r
161 Fonts.push_back(f);
\r
167 //! draws all gui elements
\r
168 void CGUIEnvironment::drawAll(bool useScreenSize)
\r
170 if (useScreenSize && Driver)
\r
172 core::dimension2d<s32> dim(Driver->getScreenSize());
\r
173 if (AbsoluteRect.LowerRightCorner.X != dim.Width ||
\r
174 AbsoluteRect.UpperLeftCorner.X != 0 ||
\r
175 AbsoluteRect.LowerRightCorner.Y != dim.Height ||
\r
176 AbsoluteRect.UpperLeftCorner.Y != 0
\r
179 setRelativePosition(core::recti(0,0,dim.Width, dim.Height));
\r
183 // make sure tooltip is always on top
\r
184 if (ToolTip.Element)
\r
185 bringToFront(ToolTip.Element);
\r
188 OnPostRender ( os::Timer::getTime () );
\r
190 clearDeletionQueue();
\r
194 //! sets the focus to an element
\r
195 bool CGUIEnvironment::setFocus(IGUIElement* element)
\r
197 if (Focus == element)
\r
202 // GUI Environment should just reset the focus to 0
\r
203 if (element == this)
\r
206 // stop element from being deleted
\r
210 // focus may change or be removed in this call
\r
211 IGUIElement *currentFocus = 0;
\r
214 currentFocus = Focus;
\r
215 currentFocus->grab();
\r
217 e.EventType = EET_GUI_EVENT;
\r
218 e.GUIEvent.Caller = Focus;
\r
219 e.GUIEvent.Element = element;
\r
220 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
221 if (Focus->OnEvent(e))
\r
225 currentFocus->drop();
\r
228 currentFocus->drop();
\r
234 currentFocus = Focus;
\r
236 currentFocus->grab();
\r
238 // send focused event
\r
240 e.EventType = EET_GUI_EVENT;
\r
241 e.GUIEvent.Caller = element;
\r
242 e.GUIEvent.Element = Focus;
\r
243 e.GUIEvent.EventType = EGET_ELEMENT_FOCUSED;
\r
244 if (element->OnEvent(e))
\r
249 currentFocus->drop();
\r
255 currentFocus->drop();
\r
260 // element is the new focus so it doesn't have to be dropped
\r
267 //! returns the element with the focus
\r
268 IGUIElement* CGUIEnvironment::getFocus() const
\r
273 //! returns the element last known to be under the mouse cursor
\r
274 IGUIElement* CGUIEnvironment::getHovered() const
\r
280 //! removes the focus from an element
\r
281 bool CGUIEnvironment::removeFocus(IGUIElement* element)
\r
283 if (Focus && Focus==element)
\r
286 e.EventType = EET_GUI_EVENT;
\r
287 e.GUIEvent.Caller = Focus;
\r
288 e.GUIEvent.Element = 0;
\r
289 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
290 if (Focus->OnEvent(e))
\r
305 //! Returns whether the element has focus
\r
306 bool CGUIEnvironment::hasFocus(const IGUIElement* element, bool checkSubElements) const
\r
308 if (element == Focus)
\r
311 if ( !checkSubElements || !element )
\r
314 IGUIElement* f = Focus;
\r
315 while ( f && f->isSubElement() )
\r
317 f = f->getParent();
\r
318 if ( f == element )
\r
325 //! returns the current video driver
\r
326 video::IVideoDriver* CGUIEnvironment::getVideoDriver() const
\r
332 //! returns the current file system
\r
333 io::IFileSystem* CGUIEnvironment::getFileSystem() const
\r
339 //! returns a pointer to the OS operator
\r
340 IOSOperator* CGUIEnvironment::getOSOperator() const
\r
346 //! clear all GUI elements
\r
347 void CGUIEnvironment::clear()
\r
349 // Remove the focus
\r
356 if (Hovered && Hovered != this)
\r
361 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
363 HoveredNoSubelement->drop();
\r
364 HoveredNoSubelement = 0;
\r
367 getRootGUIElement()->removeAllChildren();
\r
371 //! called by ui if an event happened.
\r
372 bool CGUIEnvironment::OnEvent(const SEvent& event)
\r
377 && (event.EventType != EET_MOUSE_INPUT_EVENT)
\r
378 && (event.EventType != EET_KEY_INPUT_EVENT)
\r
379 && (event.EventType != EET_GUI_EVENT || event.GUIEvent.Caller != this))
\r
381 ret = UserReceiver->OnEvent(event);
\r
388 void CGUIEnvironment::OnPostRender( u32 time )
\r
391 if ( ToolTip.Element == 0 &&
\r
392 HoveredNoSubelement && HoveredNoSubelement != this &&
\r
393 (time - ToolTip.EnterTime >= ToolTip.LaunchTime
\r
394 || (time - ToolTip.LastTime >= ToolTip.RelaunchTime && time - ToolTip.LastTime < ToolTip.LaunchTime)) &&
\r
395 HoveredNoSubelement->getToolTipText().size() &&
\r
397 getSkin()->getFont(EGDF_TOOLTIP)
\r
400 core::rect<s32> pos;
\r
402 pos.UpperLeftCorner = LastHoveredMousePos;
\r
403 core::dimension2du dim = getSkin()->getFont(EGDF_TOOLTIP)->getDimension(HoveredNoSubelement->getToolTipText().c_str());
\r
404 dim.Width += getSkin()->getSize(EGDS_TEXT_DISTANCE_X)*2;
\r
405 dim.Height += getSkin()->getSize(EGDS_TEXT_DISTANCE_Y)*2;
\r
407 pos.UpperLeftCorner.Y -= dim.Height+1;
\r
408 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + dim.Height-1;
\r
409 pos.LowerRightCorner.X = pos.UpperLeftCorner.X + dim.Width;
\r
411 pos.constrainTo(getAbsolutePosition());
\r
413 ToolTip.Element = addStaticText(HoveredNoSubelement->getToolTipText().c_str(), pos, true, true, this, -1, true);
\r
414 ToolTip.Element->setOverrideColor(getSkin()->getColor(EGDC_TOOLTIP));
\r
415 ToolTip.Element->setBackgroundColor(getSkin()->getColor(EGDC_TOOLTIP_BACKGROUND));
\r
416 ToolTip.Element->setOverrideFont(getSkin()->getFont(EGDF_TOOLTIP));
\r
417 ToolTip.Element->setSubElement(true);
\r
418 ToolTip.Element->grab();
\r
420 s32 textHeight = ToolTip.Element->getTextHeight();
\r
421 pos = ToolTip.Element->getRelativePosition();
\r
422 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + textHeight;
\r
423 ToolTip.Element->setRelativePosition(pos);
\r
426 if (ToolTip.Element && ToolTip.Element->isVisible() ) // (isVisible() check only because we might use visibility for ToolTip one day)
\r
428 ToolTip.LastTime = time;
\r
430 // got invisible or removed in the meantime?
\r
431 if ( !HoveredNoSubelement ||
\r
432 !HoveredNoSubelement->isVisible() ||
\r
433 !HoveredNoSubelement->getParent()
\r
434 ) // got invisible or removed in the meantime?
\r
436 ToolTip.Element->remove();
\r
437 ToolTip.Element->drop();
\r
438 ToolTip.Element = 0;
\r
442 IGUIElement::OnPostRender ( time );
\r
445 void CGUIEnvironment::addToDeletionQueue(IGUIElement* element)
\r
451 DeletionQueue.push_back(element);
\r
454 void CGUIEnvironment::clearDeletionQueue()
\r
456 if (DeletionQueue.empty())
\r
459 for (u32 i=0; i<DeletionQueue.size(); ++i)
\r
461 DeletionQueue[i]->remove();
\r
462 DeletionQueue[i]->drop();
\r
465 DeletionQueue.clear();
\r
469 void CGUIEnvironment::updateHoveredElement(core::position2d<s32> mousePos)
\r
471 IGUIElement* lastHovered = Hovered;
\r
472 IGUIElement* lastHoveredNoSubelement = HoveredNoSubelement;
\r
473 LastHoveredMousePos = mousePos;
\r
475 Hovered = getElementFromPoint(mousePos);
\r
477 if ( ToolTip.Element && Hovered == ToolTip.Element )
\r
479 // When the mouse is over the ToolTip we remove that so it will be re-created at a new position.
\r
480 // Note that ToolTip.EnterTime does not get changed here, so it will be re-created at once.
\r
481 ToolTip.Element->remove();
\r
482 ToolTip.Element->drop();
\r
483 ToolTip.Element = 0;
\r
485 // Get the real Hovered
\r
486 Hovered = getElementFromPoint(mousePos);
\r
489 // for tooltips we want the element itself and not some of it's subelements
\r
490 HoveredNoSubelement = Hovered;
\r
491 while ( HoveredNoSubelement && HoveredNoSubelement->isSubElement() )
\r
493 HoveredNoSubelement = HoveredNoSubelement->getParent();
\r
496 if (Hovered && Hovered != this)
\r
498 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
499 HoveredNoSubelement->grab();
\r
501 if (Hovered != lastHovered)
\r
504 event.EventType = EET_GUI_EVENT;
\r
508 event.GUIEvent.Caller = lastHovered;
\r
509 event.GUIEvent.Element = 0;
\r
510 event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
\r
511 lastHovered->OnEvent(event);
\r
516 event.GUIEvent.Caller = Hovered;
\r
517 event.GUIEvent.Element = Hovered;
\r
518 event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
\r
519 Hovered->OnEvent(event);
\r
523 if ( lastHoveredNoSubelement != HoveredNoSubelement )
\r
525 if (ToolTip.Element)
\r
527 ToolTip.Element->remove();
\r
528 ToolTip.Element->drop();
\r
529 ToolTip.Element = 0;
\r
532 if ( HoveredNoSubelement )
\r
534 u32 now = os::Timer::getTime();
\r
535 ToolTip.EnterTime = now;
\r
539 if (lastHovered && lastHovered != this)
\r
540 lastHovered->drop();
\r
541 if (lastHoveredNoSubelement && lastHoveredNoSubelement != this)
\r
542 lastHoveredNoSubelement->drop();
\r
546 //! This sets a new event receiver for gui events. Usually you do not have to
\r
547 //! use this method, it is used by the internal engine.
\r
548 void CGUIEnvironment::setUserEventReceiver(IEventReceiver* evr)
\r
550 UserReceiver = evr;
\r
554 //! posts an input event to the environment
\r
555 bool CGUIEnvironment::postEventFromUser(const SEvent& event)
\r
557 switch(event.EventType)
\r
559 case EET_GUI_EVENT:
\r
561 // hey, why is the user sending gui events..?
\r
565 case EET_MOUSE_INPUT_EVENT:
\r
567 updateHoveredElement(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
\r
569 if ( Hovered != Focus )
\r
571 IGUIElement * focusCandidate = Hovered;
\r
573 // Only allow enabled elements to be focused (unless EFF_CAN_FOCUS_DISABLED is set)
\r
574 if ( Hovered && !Hovered->isEnabled() && !(FocusFlags & EFF_CAN_FOCUS_DISABLED))
\r
575 focusCandidate = NULL; // we still remove focus from the active element
\r
577 // Please don't merge this into a single if clause, it's easier to debug the way it is
\r
578 if (FocusFlags & EFF_SET_ON_LMOUSE_DOWN &&
\r
579 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
\r
581 setFocus(focusCandidate);
\r
583 else if ( FocusFlags & EFF_SET_ON_RMOUSE_DOWN &&
\r
584 event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN )
\r
586 setFocus(focusCandidate);
\r
588 else if ( FocusFlags & EFF_SET_ON_MOUSE_OVER &&
\r
589 event.MouseInput.Event == EMIE_MOUSE_MOVED )
\r
591 setFocus(focusCandidate);
\r
595 // sending input to focus
\r
596 if (Focus && Focus->OnEvent(event))
\r
599 // focus could have died in last call
\r
600 if (!Focus && Hovered)
\r
602 return Hovered->OnEvent(event);
\r
606 case EET_KEY_INPUT_EVENT:
\r
608 if (Focus && Focus->OnEvent(event))
\r
611 // For keys we handle the event before changing focus to give elements the chance for catching the TAB
\r
612 // Send focus changing event
\r
613 // CAREFUL when changing - there's an identical check in CGUIModalScreen::OnEvent
\r
614 if (FocusFlags & EFF_SET_ON_TAB &&
\r
615 event.KeyInput.PressedDown &&
\r
616 event.KeyInput.Key == KEY_TAB)
\r
618 IGUIElement *next = getNextElement(event.KeyInput.Shift, event.KeyInput.Control);
\r
619 if (next && next != Focus)
\r
621 if (setFocus(next))
\r
627 case EET_STRING_INPUT_EVENT:
\r
628 if (Focus && Focus->OnEvent(event))
\r
639 //! returns the current gui skin
\r
640 IGUISkin* CGUIEnvironment::getSkin() const
\r
642 return CurrentSkin;
\r
646 //! Sets a new GUI Skin
\r
647 void CGUIEnvironment::setSkin(IGUISkin* skin)
\r
649 if (CurrentSkin==skin)
\r
653 CurrentSkin->drop();
\r
655 CurrentSkin = skin;
\r
658 CurrentSkin->grab();
\r
662 //! Creates a new GUI Skin based on a template.
\r
663 /** \return Returns a pointer to the created skin.
\r
664 If you no longer need the skin, you should call IGUISkin::drop().
\r
665 See IReferenceCounted::drop() for more information. */
\r
666 IGUISkin* CGUIEnvironment::createSkin(EGUI_SKIN_TYPE type)
\r
668 IGUISkin* skin = new CGUISkin(type, Driver);
\r
670 IGUIFont* builtinfont = getBuiltInFont();
\r
671 IGUIFontBitmap* bitfont = 0;
\r
672 if (builtinfont && builtinfont->getType() == EGFT_BITMAP)
\r
673 bitfont = (IGUIFontBitmap*)builtinfont;
\r
675 IGUISpriteBank* bank = 0;
\r
676 skin->setFont(builtinfont);
\r
679 bank = bitfont->getSpriteBank();
\r
681 skin->setSpriteBank(bank);
\r
687 //! adds a button. The returned pointer must not be dropped.
\r
688 IGUIButton* CGUIEnvironment::addButton(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, const wchar_t *tooltiptext)
\r
690 IGUIButton* button = new CGUIButton(this, parent ? parent : this, id, rectangle);
\r
692 button->setText(text);
\r
695 button->setToolTipText ( tooltiptext );
\r
702 //! adds a scrollbar. The returned pointer must not be dropped.
\r
703 IGUIScrollBar* CGUIEnvironment::addScrollBar(bool horizontal, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id)
\r
705 IGUIScrollBar* bar = new CGUIScrollBar(horizontal, this, parent ? parent : this, id, rectangle);
\r
711 //! Adds an image element.
\r
712 IGUIImage* CGUIEnvironment::addImage(video::ITexture* image, core::position2d<s32> pos,
\r
713 bool useAlphaChannel, IGUIElement* parent, s32 id, const wchar_t* text)
\r
715 core::dimension2d<s32> sz(0,0);
\r
717 sz = core::dimension2d<s32>(image->getOriginalSize());
\r
719 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
720 id, core::rect<s32>(pos, sz));
\r
723 img->setText(text);
\r
725 if (useAlphaChannel)
\r
726 img->setUseAlphaChannel(true);
\r
729 img->setImage(image);
\r
736 //! adds an image. The returned pointer must not be dropped.
\r
737 IGUIImage* CGUIEnvironment::addImage(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, bool useAlphaChannel)
\r
739 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
743 img->setText(text);
\r
745 if ( useAlphaChannel )
\r
746 img->setUseAlphaChannel(true);
\r
753 //! adds a checkbox
\r
754 IGUICheckBox* CGUIEnvironment::addCheckBox(bool checked, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text)
\r
756 IGUICheckBox* b = new CGUICheckBox(checked, this,
\r
757 parent ? parent : this , id , rectangle);
\r
767 //! adds a list box
\r
768 IGUIListBox* CGUIEnvironment::addListBox(const core::rect<s32>& rectangle,
\r
769 IGUIElement* parent, s32 id, bool drawBackground)
\r
771 IGUIListBox* b = new CGUIListBox(this, parent ? parent : this, id, rectangle,
\r
772 true, drawBackground, false);
\r
774 if (CurrentSkin && CurrentSkin->getSpriteBank())
\r
776 b->setSpriteBank(CurrentSkin->getSpriteBank());
\r
778 else if (getBuiltInFont() && getBuiltInFont()->getType() == EGFT_BITMAP)
\r
780 b->setSpriteBank( ((IGUIFontBitmap*)getBuiltInFont())->getSpriteBank());
\r
788 //! adds a file open dialog. The returned pointer must not be dropped.
\r
789 IGUIFileOpenDialog* CGUIEnvironment::addFileOpenDialog(const wchar_t* title,
\r
790 bool modal, IGUIElement* parent, s32 id,
\r
791 bool restoreCWD, io::path::char_type* startDir)
\r
793 parent = parent ? parent : this;
\r
798 IGUIFileOpenDialog* d = new CGUIFileOpenDialog(title, this, parent, id,
\r
799 restoreCWD, startDir);
\r
806 //! adds a static text. The returned pointer must not be dropped.
\r
807 IGUIStaticText* CGUIEnvironment::addStaticText(const wchar_t* text,
\r
808 const core::rect<s32>& rectangle,
\r
809 bool border, bool wordWrap,
\r
810 IGUIElement* parent, s32 id, bool background)
\r
812 IGUIStaticText* d = new CGUIStaticText(text, border, this,
\r
813 parent ? parent : this, id, rectangle, background);
\r
815 d->setWordWrap(wordWrap);
\r
822 //! Adds an edit box. The returned pointer must not be dropped.
\r
823 IGUIEditBox* CGUIEnvironment::addEditBox(const wchar_t* text,
\r
824 const core::rect<s32>& rectangle, bool border,
\r
825 IGUIElement* parent, s32 id)
\r
827 IGUIEditBox* d = new CGUIEditBox(text, border, this,
\r
828 parent ? parent : this, id, rectangle);
\r
835 //! Adds a tab control to the environment.
\r
836 IGUITabControl* CGUIEnvironment::addTabControl(const core::rect<s32>& rectangle,
\r
837 IGUIElement* parent, bool fillbackground, bool border, s32 id)
\r
839 IGUITabControl* t = new CGUITabControl(this, parent ? parent : this,
\r
840 rectangle, fillbackground, border, id);
\r
846 //! Adds tab to the environment.
\r
847 IGUITab* CGUIEnvironment::addTab(const core::rect<s32>& rectangle,
\r
848 IGUIElement* parent, s32 id)
\r
850 IGUITab* t = new CGUITab(this, parent ? parent : this,
\r
857 //! Adds a combo box to the environment.
\r
858 IGUIComboBox* CGUIEnvironment::addComboBox(const core::rect<s32>& rectangle,
\r
859 IGUIElement* parent, s32 id)
\r
861 IGUIComboBox* t = new CGUIComboBox(this, parent ? parent : this,
\r
868 //! returns the font
\r
869 IGUIFont* CGUIEnvironment::getFont(const io::path& filename)
\r
871 // search existing font
\r
874 f.NamedPath.setPath(filename);
\r
876 s32 index = Fonts.binary_search(f);
\r
878 return Fonts[index].Font;
\r
880 // font doesn't exist, attempt to load it
\r
882 // does the file exist?
\r
884 if (!FileSystem->existFile(filename))
\r
886 os::Printer::log("Could not load font because the file does not exist", f.NamedPath.getPath(), ELL_ERROR);
\r
893 CGUIFont* font = new CGUIFont(this, filename);
\r
894 ifont = (IGUIFont*)font;
\r
897 io::path directory;
\r
898 core::splitFilename(filename, &directory);
\r
899 if (!font->load(xml, directory))
\r
912 CGUIFont* font = new CGUIFont(this, f.NamedPath.getPath() );
\r
913 ifont = (IGUIFont*)font;
\r
914 if (!font->load(f.NamedPath.getPath()))
\r
924 Fonts.push_back(f);
\r
930 //! add an externally loaded font
\r
931 IGUIFont* CGUIEnvironment::addFont(const io::path& name, IGUIFont* font)
\r
936 f.NamedPath.setPath(name);
\r
937 s32 index = Fonts.binary_search(f);
\r
939 return Fonts[index].Font;
\r
941 Fonts.push_back(f);
\r
947 //! remove loaded font
\r
948 void CGUIEnvironment::removeFont(IGUIFont* font)
\r
952 for ( u32 i=0; i<Fonts.size(); ++i )
\r
954 if ( Fonts[i].Font == font )
\r
956 Fonts[i].Font->drop();
\r
963 //! returns default font
\r
964 IGUIFont* CGUIEnvironment::getBuiltInFont() const
\r
969 return Fonts[0].Font;
\r
973 IGUISpriteBank* CGUIEnvironment::getSpriteBank(const io::path& filename)
\r
975 // search for the file name
\r
978 b.NamedPath.setPath(filename);
\r
980 s32 index = Banks.binary_search(b);
\r
982 return Banks[index].Bank;
\r
984 // we don't have this sprite bank, we should load it
\r
985 if (!FileSystem->existFile(b.NamedPath.getPath()))
\r
987 if ( filename != DefaultFontName )
\r
989 os::Printer::log("Could not load sprite bank because the file does not exist", b.NamedPath.getPath(), ELL_DEBUG);
\r
1000 IGUISpriteBank* CGUIEnvironment::addEmptySpriteBank(const io::path& name)
\r
1002 // no duplicate names allowed
\r
1005 b.NamedPath.setPath(name);
\r
1007 const s32 index = Banks.binary_search(b);
\r
1011 // create a new sprite bank
\r
1013 b.Bank = new CGUISpriteBank(this);
\r
1014 Banks.push_back(b);
\r
1020 //! Creates the image list from the given texture.
\r
1021 IGUIImageList* CGUIEnvironment::createImageList( video::ITexture* texture,
\r
1022 core::dimension2d<s32> imageSize, bool useAlphaChannel )
\r
1024 CGUIImageList* imageList = new CGUIImageList( Driver );
\r
1025 if( !imageList->createImageList( texture, imageSize, useAlphaChannel ) )
\r
1027 imageList->drop();
\r
1034 //! Returns the root gui element.
\r
1035 IGUIElement* CGUIEnvironment::getRootGUIElement()
\r
1041 //! Returns the next element in the tab group starting at the focused element
\r
1042 IGUIElement* CGUIEnvironment::getNextElement(bool reverse, bool group)
\r
1044 // start the search at the root of the current tab group
\r
1045 IGUIElement *startPos = Focus ? Focus->getTabGroup() : 0;
\r
1046 s32 startOrder = -1;
\r
1048 // if we're searching for a group
\r
1049 if (group && startPos)
\r
1051 startOrder = startPos->getTabOrder();
\r
1054 if (!group && Focus && !Focus->isTabGroup())
\r
1056 startOrder = Focus->getTabOrder();
\r
1057 if (startOrder == -1)
\r
1059 // this element is not part of the tab cycle,
\r
1060 // but its parent might be...
\r
1061 IGUIElement *el = Focus;
\r
1062 while (el && el->getParent() && startOrder == -1)
\r
1064 el = el->getParent();
\r
1065 startOrder = el->getTabOrder();
\r
1071 if (group || !startPos)
\r
1072 startPos = this; // start at the root
\r
1074 // find the element
\r
1075 IGUIElement *closest = 0;
\r
1076 IGUIElement *first = 0;
\r
1077 startPos->getNextElement(startOrder, reverse, group, first, closest, false, (FocusFlags & EFF_CAN_FOCUS_DISABLED) != 0);
\r
1080 return closest; // we found an element
\r
1082 return first; // go to the end or the start
\r
1084 return this; // no group found? root group
\r
1089 void CGUIEnvironment::setFocusBehavior(u32 flags)
\r
1091 FocusFlags = flags;
\r
1094 u32 CGUIEnvironment::getFocusBehavior() const
\r
1096 return FocusFlags;
\r
1099 //! creates an GUI Environment
\r
1100 IGUIEnvironment* createGUIEnvironment(io::IFileSystem* fs,
\r
1101 video::IVideoDriver* Driver,
\r
1104 return new CGUIEnvironment(fs, Driver, op);
\r
1108 } // end namespace gui
\r
1109 } // end namespace irr
\r
1111 #endif // _IRR_COMPILE_WITH_GUI_
\r