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 #include "IVideoDriver.h"
\r
10 #include "CGUISkin.h"
\r
11 #include "CGUIButton.h"
\r
12 #include "CGUIScrollBar.h"
\r
13 #include "CGUIFont.h"
\r
14 #include "CGUISpriteBank.h"
\r
15 #include "CGUIImage.h"
\r
16 #include "CGUICheckBox.h"
\r
17 #include "CGUIListBox.h"
\r
18 #include "CGUIImageList.h"
\r
19 #include "CGUIFileOpenDialog.h"
\r
20 #include "CGUIStaticText.h"
\r
21 #include "CGUIEditBox.h"
\r
22 #include "CGUITabControl.h"
\r
23 #include "CGUIComboBox.h"
\r
25 #include "IWriteFile.h"
\r
26 #ifdef IRR_ENABLE_BUILTIN_FONT
\r
27 #include "BuiltInFont.h"
\r
36 const io::path CGUIEnvironment::DefaultFontName = "#DefaultFont";
\r
39 CGUIEnvironment::CGUIEnvironment(io::IFileSystem* fs, video::IVideoDriver* driver, IOSOperator* op)
\r
40 : IGUIElement(EGUIET_ROOT, 0, 0, 0, core::rect<s32>(driver ? core::dimension2d<s32>(driver->getScreenSize()) : core::dimension2d<s32>(0,0))),
\r
41 Driver(driver), Hovered(0), HoveredNoSubelement(0), Focus(0), LastHoveredMousePos(0,0), CurrentSkin(0),
\r
42 FileSystem(fs), UserReceiver(0), Operator(op), FocusFlags(EFF_SET_ON_LMOUSE_DOWN|EFF_SET_ON_TAB)
\r
54 IGUIEnvironment::setDebugName("CGUIEnvironment");
\r
59 IGUISkin* skin = createSkin( gui::EGST_WINDOWS_METALLIC );
\r
63 //set tooltip default
\r
64 ToolTip.LastTime = 0;
\r
65 ToolTip.EnterTime = 0;
\r
66 ToolTip.LaunchTime = 1000;
\r
67 ToolTip.RelaunchTime = 500;
\r
68 ToolTip.Element = 0;
\r
70 // environment is root tab group
\r
77 CGUIEnvironment::~CGUIEnvironment()
\r
79 clearDeletionQueue();
\r
81 if ( HoveredNoSubelement && HoveredNoSubelement != this )
\r
83 HoveredNoSubelement->drop();
\r
84 HoveredNoSubelement = 0;
\r
87 if (Hovered && Hovered != this)
\r
99 if (ToolTip.Element)
\r
101 ToolTip.Element->drop();
\r
102 ToolTip.Element = 0;
\r
108 CurrentSkin->drop();
\r
114 // delete all sprite banks
\r
115 for (i=0; i<Banks.size(); ++i)
\r
117 Banks[i].Bank->drop();
\r
119 // delete all fonts
\r
120 for (i=0; i<Fonts.size(); ++i)
\r
121 Fonts[i].Font->drop();
\r
131 FileSystem->drop();
\r
143 void CGUIEnvironment::loadBuiltInFont()
\r
145 #ifdef IRR_ENABLE_BUILTIN_FONT
\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
168 //! draws all gui elements
\r
169 void CGUIEnvironment::drawAll(bool useScreenSize)
\r
171 if (useScreenSize && Driver)
\r
173 core::dimension2d<s32> dim(Driver->getScreenSize());
\r
174 if (AbsoluteRect.LowerRightCorner.X != dim.Width ||
\r
175 AbsoluteRect.UpperLeftCorner.X != 0 ||
\r
176 AbsoluteRect.LowerRightCorner.Y != dim.Height ||
\r
177 AbsoluteRect.UpperLeftCorner.Y != 0
\r
180 setRelativePosition(core::recti(0,0,dim.Width, dim.Height));
\r
184 // make sure tooltip is always on top
\r
185 if (ToolTip.Element)
\r
186 bringToFront(ToolTip.Element);
\r
189 OnPostRender ( os::Timer::getTime () );
\r
191 clearDeletionQueue();
\r
195 //! sets the focus to an element
\r
196 bool CGUIEnvironment::setFocus(IGUIElement* element)
\r
198 if (Focus == element)
\r
203 // GUI Environment should just reset the focus to 0
\r
204 if (element == this)
\r
207 // stop element from being deleted
\r
211 // focus may change or be removed in this call
\r
212 IGUIElement *currentFocus = 0;
\r
215 currentFocus = Focus;
\r
216 currentFocus->grab();
\r
218 e.EventType = EET_GUI_EVENT;
\r
219 e.GUIEvent.Caller = Focus;
\r
220 e.GUIEvent.Element = element;
\r
221 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
222 if (Focus->OnEvent(e))
\r
226 currentFocus->drop();
\r
229 currentFocus->drop();
\r
235 currentFocus = Focus;
\r
237 currentFocus->grab();
\r
239 // send focused event
\r
241 e.EventType = EET_GUI_EVENT;
\r
242 e.GUIEvent.Caller = element;
\r
243 e.GUIEvent.Element = Focus;
\r
244 e.GUIEvent.EventType = EGET_ELEMENT_FOCUSED;
\r
245 if (element->OnEvent(e))
\r
250 currentFocus->drop();
\r
256 currentFocus->drop();
\r
261 // element is the new focus so it doesn't have to be dropped
\r
268 //! returns the element with the focus
\r
269 IGUIElement* CGUIEnvironment::getFocus() const
\r
274 //! returns the element last known to be under the mouse cursor
\r
275 IGUIElement* CGUIEnvironment::getHovered() const
\r
281 //! removes the focus from an element
\r
282 bool CGUIEnvironment::removeFocus(IGUIElement* element)
\r
284 if (Focus && Focus==element)
\r
287 e.EventType = EET_GUI_EVENT;
\r
288 e.GUIEvent.Caller = Focus;
\r
289 e.GUIEvent.Element = 0;
\r
290 e.GUIEvent.EventType = EGET_ELEMENT_FOCUS_LOST;
\r
291 if (Focus->OnEvent(e))
\r
306 //! Returns whether the element has focus
\r
307 bool CGUIEnvironment::hasFocus(const IGUIElement* element, bool checkSubElements) const
\r
309 if (element == Focus)
\r
312 if ( !checkSubElements || !element )
\r
315 IGUIElement* f = Focus;
\r
316 while ( f && f->isSubElement() )
\r
318 f = f->getParent();
\r
319 if ( f == element )
\r
326 //! returns the current video driver
\r
327 video::IVideoDriver* CGUIEnvironment::getVideoDriver() const
\r
333 //! returns the current file system
\r
334 io::IFileSystem* CGUIEnvironment::getFileSystem() const
\r
340 //! returns a pointer to the OS operator
\r
341 IOSOperator* CGUIEnvironment::getOSOperator() const
\r
347 //! clear all GUI elements
\r
348 void CGUIEnvironment::clear()
\r
350 // Remove the focus
\r
357 if (Hovered && Hovered != this)
\r
362 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
364 HoveredNoSubelement->drop();
\r
365 HoveredNoSubelement = 0;
\r
368 getRootGUIElement()->removeAllChildren();
\r
372 //! called by ui if an event happened.
\r
373 bool CGUIEnvironment::OnEvent(const SEvent& event)
\r
378 && (event.EventType != EET_MOUSE_INPUT_EVENT)
\r
379 && (event.EventType != EET_KEY_INPUT_EVENT)
\r
380 && (event.EventType != EET_GUI_EVENT || event.GUIEvent.Caller != this))
\r
382 ret = UserReceiver->OnEvent(event);
\r
389 void CGUIEnvironment::OnPostRender( u32 time )
\r
392 if ( ToolTip.Element == 0 &&
\r
393 HoveredNoSubelement && HoveredNoSubelement != this &&
\r
394 (time - ToolTip.EnterTime >= ToolTip.LaunchTime
\r
395 || (time - ToolTip.LastTime >= ToolTip.RelaunchTime && time - ToolTip.LastTime < ToolTip.LaunchTime)) &&
\r
396 HoveredNoSubelement->getToolTipText().size() &&
\r
398 getSkin()->getFont(EGDF_TOOLTIP)
\r
401 core::rect<s32> pos;
\r
403 pos.UpperLeftCorner = LastHoveredMousePos;
\r
404 core::dimension2du dim = getSkin()->getFont(EGDF_TOOLTIP)->getDimension(HoveredNoSubelement->getToolTipText().c_str());
\r
405 dim.Width += getSkin()->getSize(EGDS_TEXT_DISTANCE_X)*2;
\r
406 dim.Height += getSkin()->getSize(EGDS_TEXT_DISTANCE_Y)*2;
\r
408 pos.UpperLeftCorner.Y -= dim.Height+1;
\r
409 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + dim.Height-1;
\r
410 pos.LowerRightCorner.X = pos.UpperLeftCorner.X + dim.Width;
\r
412 pos.constrainTo(getAbsolutePosition());
\r
414 ToolTip.Element = addStaticText(HoveredNoSubelement->getToolTipText().c_str(), pos, true, true, this, -1, true);
\r
415 ToolTip.Element->setOverrideColor(getSkin()->getColor(EGDC_TOOLTIP));
\r
416 ToolTip.Element->setBackgroundColor(getSkin()->getColor(EGDC_TOOLTIP_BACKGROUND));
\r
417 ToolTip.Element->setOverrideFont(getSkin()->getFont(EGDF_TOOLTIP));
\r
418 ToolTip.Element->setSubElement(true);
\r
419 ToolTip.Element->grab();
\r
421 s32 textHeight = ToolTip.Element->getTextHeight();
\r
422 pos = ToolTip.Element->getRelativePosition();
\r
423 pos.LowerRightCorner.Y = pos.UpperLeftCorner.Y + textHeight;
\r
424 ToolTip.Element->setRelativePosition(pos);
\r
427 if (ToolTip.Element && ToolTip.Element->isVisible() ) // (isVisible() check only because we might use visibility for ToolTip one day)
\r
429 ToolTip.LastTime = time;
\r
431 // got invisible or removed in the meantime?
\r
432 if ( !HoveredNoSubelement ||
\r
433 !HoveredNoSubelement->isVisible() ||
\r
434 !HoveredNoSubelement->getParent()
\r
435 ) // got invisible or removed in the meantime?
\r
437 ToolTip.Element->remove();
\r
438 ToolTip.Element->drop();
\r
439 ToolTip.Element = 0;
\r
443 IGUIElement::OnPostRender ( time );
\r
446 void CGUIEnvironment::addToDeletionQueue(IGUIElement* element)
\r
452 DeletionQueue.push_back(element);
\r
455 void CGUIEnvironment::clearDeletionQueue()
\r
457 if (DeletionQueue.empty())
\r
460 for (u32 i=0; i<DeletionQueue.size(); ++i)
\r
462 DeletionQueue[i]->remove();
\r
463 DeletionQueue[i]->drop();
\r
466 DeletionQueue.clear();
\r
470 void CGUIEnvironment::updateHoveredElement(core::position2d<s32> mousePos)
\r
472 IGUIElement* lastHovered = Hovered;
\r
473 IGUIElement* lastHoveredNoSubelement = HoveredNoSubelement;
\r
474 LastHoveredMousePos = mousePos;
\r
476 Hovered = getElementFromPoint(mousePos);
\r
478 if ( ToolTip.Element && Hovered == ToolTip.Element )
\r
480 // When the mouse is over the ToolTip we remove that so it will be re-created at a new position.
\r
481 // Note that ToolTip.EnterTime does not get changed here, so it will be re-created at once.
\r
482 ToolTip.Element->remove();
\r
483 ToolTip.Element->drop();
\r
484 ToolTip.Element = 0;
\r
486 // Get the real Hovered
\r
487 Hovered = getElementFromPoint(mousePos);
\r
490 // for tooltips we want the element itself and not some of it's subelements
\r
491 HoveredNoSubelement = Hovered;
\r
492 while ( HoveredNoSubelement && HoveredNoSubelement->isSubElement() )
\r
494 HoveredNoSubelement = HoveredNoSubelement->getParent();
\r
497 if (Hovered && Hovered != this)
\r
499 if ( HoveredNoSubelement && HoveredNoSubelement != this)
\r
500 HoveredNoSubelement->grab();
\r
502 if (Hovered != lastHovered)
\r
505 event.EventType = EET_GUI_EVENT;
\r
509 event.GUIEvent.Caller = lastHovered;
\r
510 event.GUIEvent.Element = 0;
\r
511 event.GUIEvent.EventType = EGET_ELEMENT_LEFT;
\r
512 lastHovered->OnEvent(event);
\r
517 event.GUIEvent.Caller = Hovered;
\r
518 event.GUIEvent.Element = Hovered;
\r
519 event.GUIEvent.EventType = EGET_ELEMENT_HOVERED;
\r
520 Hovered->OnEvent(event);
\r
524 if ( lastHoveredNoSubelement != HoveredNoSubelement )
\r
526 if (ToolTip.Element)
\r
528 ToolTip.Element->remove();
\r
529 ToolTip.Element->drop();
\r
530 ToolTip.Element = 0;
\r
533 if ( HoveredNoSubelement )
\r
535 u32 now = os::Timer::getTime();
\r
536 ToolTip.EnterTime = now;
\r
540 if (lastHovered && lastHovered != this)
\r
541 lastHovered->drop();
\r
542 if (lastHoveredNoSubelement && lastHoveredNoSubelement != this)
\r
543 lastHoveredNoSubelement->drop();
\r
547 //! This sets a new event receiver for gui events. Usually you do not have to
\r
548 //! use this method, it is used by the internal engine.
\r
549 void CGUIEnvironment::setUserEventReceiver(IEventReceiver* evr)
\r
551 UserReceiver = evr;
\r
555 //! posts an input event to the environment
\r
556 bool CGUIEnvironment::postEventFromUser(const SEvent& event)
\r
558 switch(event.EventType)
\r
560 case EET_GUI_EVENT:
\r
562 // hey, why is the user sending gui events..?
\r
566 case EET_MOUSE_INPUT_EVENT:
\r
568 updateHoveredElement(core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
\r
570 if ( Hovered != Focus )
\r
572 IGUIElement * focusCandidate = Hovered;
\r
574 // Only allow enabled elements to be focused (unless EFF_CAN_FOCUS_DISABLED is set)
\r
575 if ( Hovered && !Hovered->isEnabled() && !(FocusFlags & EFF_CAN_FOCUS_DISABLED))
\r
576 focusCandidate = NULL; // we still remove focus from the active element
\r
578 // Please don't merge this into a single if clause, it's easier to debug the way it is
\r
579 if (FocusFlags & EFF_SET_ON_LMOUSE_DOWN &&
\r
580 event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
\r
582 setFocus(focusCandidate);
\r
584 else if ( FocusFlags & EFF_SET_ON_RMOUSE_DOWN &&
\r
585 event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN )
\r
587 setFocus(focusCandidate);
\r
589 else if ( FocusFlags & EFF_SET_ON_MOUSE_OVER &&
\r
590 event.MouseInput.Event == EMIE_MOUSE_MOVED )
\r
592 setFocus(focusCandidate);
\r
596 // sending input to focus
\r
597 if (Focus && Focus->OnEvent(event))
\r
600 // focus could have died in last call
\r
601 if (!Focus && Hovered)
\r
603 return Hovered->OnEvent(event);
\r
607 case EET_KEY_INPUT_EVENT:
\r
609 if (Focus && Focus->OnEvent(event))
\r
612 // For keys we handle the event before changing focus to give elements the chance for catching the TAB
\r
613 // Send focus changing event
\r
614 // CAREFUL when changing - there's an identical check in CGUIModalScreen::OnEvent
\r
615 if (FocusFlags & EFF_SET_ON_TAB &&
\r
616 event.KeyInput.PressedDown &&
\r
617 event.KeyInput.Key == KEY_TAB)
\r
619 IGUIElement *next = getNextElement(event.KeyInput.Shift, event.KeyInput.Control);
\r
620 if (next && next != Focus)
\r
622 if (setFocus(next))
\r
628 case EET_STRING_INPUT_EVENT:
\r
629 if (Focus && Focus->OnEvent(event))
\r
640 //! returns the current gui skin
\r
641 IGUISkin* CGUIEnvironment::getSkin() const
\r
643 return CurrentSkin;
\r
647 //! Sets a new GUI Skin
\r
648 void CGUIEnvironment::setSkin(IGUISkin* skin)
\r
650 if (CurrentSkin==skin)
\r
654 CurrentSkin->drop();
\r
656 CurrentSkin = skin;
\r
659 CurrentSkin->grab();
\r
663 //! Creates a new GUI Skin based on a template.
\r
664 /** \return Returns a pointer to the created skin.
\r
665 If you no longer need the skin, you should call IGUISkin::drop().
\r
666 See IReferenceCounted::drop() for more information. */
\r
667 IGUISkin* CGUIEnvironment::createSkin(EGUI_SKIN_TYPE type)
\r
669 IGUISkin* skin = new CGUISkin(type, Driver);
\r
671 IGUIFont* builtinfont = getBuiltInFont();
\r
672 IGUIFontBitmap* bitfont = 0;
\r
673 if (builtinfont && builtinfont->getType() == EGFT_BITMAP)
\r
674 bitfont = (IGUIFontBitmap*)builtinfont;
\r
676 IGUISpriteBank* bank = 0;
\r
677 skin->setFont(builtinfont);
\r
680 bank = bitfont->getSpriteBank();
\r
682 skin->setSpriteBank(bank);
\r
688 //! adds a button. The returned pointer must not be dropped.
\r
689 IGUIButton* CGUIEnvironment::addButton(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, const wchar_t *tooltiptext)
\r
691 IGUIButton* button = new CGUIButton(this, parent ? parent : this, id, rectangle);
\r
693 button->setText(text);
\r
696 button->setToolTipText ( tooltiptext );
\r
703 //! adds a scrollbar. The returned pointer must not be dropped.
\r
704 IGUIScrollBar* CGUIEnvironment::addScrollBar(bool horizontal, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id)
\r
706 IGUIScrollBar* bar = new CGUIScrollBar(horizontal, this, parent ? parent : this, id, rectangle);
\r
712 //! Adds an image element.
\r
713 IGUIImage* CGUIEnvironment::addImage(video::ITexture* image, core::position2d<s32> pos,
\r
714 bool useAlphaChannel, IGUIElement* parent, s32 id, const wchar_t* text)
\r
716 core::dimension2d<s32> sz(0,0);
\r
718 sz = core::dimension2d<s32>(image->getOriginalSize());
\r
720 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
721 id, core::rect<s32>(pos, sz));
\r
724 img->setText(text);
\r
726 if (useAlphaChannel)
\r
727 img->setUseAlphaChannel(true);
\r
730 img->setImage(image);
\r
737 //! adds an image. The returned pointer must not be dropped.
\r
738 IGUIImage* CGUIEnvironment::addImage(const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text, bool useAlphaChannel)
\r
740 IGUIImage* img = new CGUIImage(this, parent ? parent : this,
\r
744 img->setText(text);
\r
746 if ( useAlphaChannel )
\r
747 img->setUseAlphaChannel(true);
\r
754 //! adds a checkbox
\r
755 IGUICheckBox* CGUIEnvironment::addCheckBox(bool checked, const core::rect<s32>& rectangle, IGUIElement* parent, s32 id, const wchar_t* text)
\r
757 IGUICheckBox* b = new CGUICheckBox(checked, this,
\r
758 parent ? parent : this , id , rectangle);
\r
768 //! adds a list box
\r
769 IGUIListBox* CGUIEnvironment::addListBox(const core::rect<s32>& rectangle,
\r
770 IGUIElement* parent, s32 id, bool drawBackground)
\r
772 IGUIListBox* b = new CGUIListBox(this, parent ? parent : this, id, rectangle,
\r
773 true, drawBackground, false);
\r
775 if (CurrentSkin && CurrentSkin->getSpriteBank())
\r
777 b->setSpriteBank(CurrentSkin->getSpriteBank());
\r
779 else if (getBuiltInFont() && getBuiltInFont()->getType() == EGFT_BITMAP)
\r
781 b->setSpriteBank( ((IGUIFontBitmap*)getBuiltInFont())->getSpriteBank());
\r
789 //! adds a file open dialog. The returned pointer must not be dropped.
\r
790 IGUIFileOpenDialog* CGUIEnvironment::addFileOpenDialog(const wchar_t* title,
\r
791 bool modal, IGUIElement* parent, s32 id,
\r
792 bool restoreCWD, io::path::char_type* startDir)
\r
794 parent = parent ? parent : this;
\r
799 IGUIFileOpenDialog* d = new CGUIFileOpenDialog(title, this, parent, id,
\r
800 restoreCWD, startDir);
\r
807 //! adds a static text. The returned pointer must not be dropped.
\r
808 IGUIStaticText* CGUIEnvironment::addStaticText(const wchar_t* text,
\r
809 const core::rect<s32>& rectangle,
\r
810 bool border, bool wordWrap,
\r
811 IGUIElement* parent, s32 id, bool background)
\r
813 IGUIStaticText* d = new CGUIStaticText(text, border, this,
\r
814 parent ? parent : this, id, rectangle, background);
\r
816 d->setWordWrap(wordWrap);
\r
823 //! Adds an edit box. The returned pointer must not be dropped.
\r
824 IGUIEditBox* CGUIEnvironment::addEditBox(const wchar_t* text,
\r
825 const core::rect<s32>& rectangle, bool border,
\r
826 IGUIElement* parent, s32 id)
\r
828 IGUIEditBox* d = new CGUIEditBox(text, border, this,
\r
829 parent ? parent : this, id, rectangle);
\r
836 //! Adds a tab control to the environment.
\r
837 IGUITabControl* CGUIEnvironment::addTabControl(const core::rect<s32>& rectangle,
\r
838 IGUIElement* parent, bool fillbackground, bool border, s32 id)
\r
840 IGUITabControl* t = new CGUITabControl(this, parent ? parent : this,
\r
841 rectangle, fillbackground, border, id);
\r
847 //! Adds tab to the environment.
\r
848 IGUITab* CGUIEnvironment::addTab(const core::rect<s32>& rectangle,
\r
849 IGUIElement* parent, s32 id)
\r
851 IGUITab* t = new CGUITab(this, parent ? parent : this,
\r
858 //! Adds a combo box to the environment.
\r
859 IGUIComboBox* CGUIEnvironment::addComboBox(const core::rect<s32>& rectangle,
\r
860 IGUIElement* parent, s32 id)
\r
862 IGUIComboBox* t = new CGUIComboBox(this, parent ? parent : this,
\r
869 //! returns the font
\r
870 IGUIFont* CGUIEnvironment::getFont(const io::path& filename)
\r
872 // search existing font
\r
875 f.NamedPath.setPath(filename);
\r
877 s32 index = Fonts.binary_search(f);
\r
879 return Fonts[index].Font;
\r
881 // font doesn't exist, attempt to load it
\r
883 // does the file exist?
\r
885 if (!FileSystem->existFile(filename))
\r
887 os::Printer::log("Could not load font because the file does not exist", f.NamedPath.getPath(), ELL_ERROR);
\r
894 CGUIFont* font = new CGUIFont(this, filename);
\r
895 ifont = (IGUIFont*)font;
\r
898 io::path directory;
\r
899 core::splitFilename(filename, &directory);
\r
900 if (!font->load(xml, directory))
\r
913 CGUIFont* font = new CGUIFont(this, f.NamedPath.getPath() );
\r
914 ifont = (IGUIFont*)font;
\r
915 if (!font->load(f.NamedPath.getPath()))
\r
925 Fonts.push_back(f);
\r
931 //! add an externally loaded font
\r
932 IGUIFont* CGUIEnvironment::addFont(const io::path& name, IGUIFont* font)
\r
937 f.NamedPath.setPath(name);
\r
938 s32 index = Fonts.binary_search(f);
\r
940 return Fonts[index].Font;
\r
942 Fonts.push_back(f);
\r
948 //! remove loaded font
\r
949 void CGUIEnvironment::removeFont(IGUIFont* font)
\r
953 for ( u32 i=0; i<Fonts.size(); ++i )
\r
955 if ( Fonts[i].Font == font )
\r
957 Fonts[i].Font->drop();
\r
964 //! returns default font
\r
965 IGUIFont* CGUIEnvironment::getBuiltInFont() const
\r
970 return Fonts[0].Font;
\r
974 IGUISpriteBank* CGUIEnvironment::getSpriteBank(const io::path& filename)
\r
976 // search for the file name
\r
979 b.NamedPath.setPath(filename);
\r
981 s32 index = Banks.binary_search(b);
\r
983 return Banks[index].Bank;
\r
985 // we don't have this sprite bank, we should load it
\r
986 if (!FileSystem->existFile(b.NamedPath.getPath()))
\r
988 if ( filename != DefaultFontName )
\r
990 os::Printer::log("Could not load sprite bank because the file does not exist", b.NamedPath.getPath(), ELL_DEBUG);
\r
1001 IGUISpriteBank* CGUIEnvironment::addEmptySpriteBank(const io::path& name)
\r
1003 // no duplicate names allowed
\r
1006 b.NamedPath.setPath(name);
\r
1008 const s32 index = Banks.binary_search(b);
\r
1012 // create a new sprite bank
\r
1014 b.Bank = new CGUISpriteBank(this);
\r
1015 Banks.push_back(b);
\r
1021 //! Creates the image list from the given texture.
\r
1022 IGUIImageList* CGUIEnvironment::createImageList( video::ITexture* texture,
\r
1023 core::dimension2d<s32> imageSize, bool useAlphaChannel )
\r
1025 CGUIImageList* imageList = new CGUIImageList( Driver );
\r
1026 if( !imageList->createImageList( texture, imageSize, useAlphaChannel ) )
\r
1028 imageList->drop();
\r
1035 //! Returns the root gui element.
\r
1036 IGUIElement* CGUIEnvironment::getRootGUIElement()
\r
1042 //! Returns the next element in the tab group starting at the focused element
\r
1043 IGUIElement* CGUIEnvironment::getNextElement(bool reverse, bool group)
\r
1045 // start the search at the root of the current tab group
\r
1046 IGUIElement *startPos = Focus ? Focus->getTabGroup() : 0;
\r
1047 s32 startOrder = -1;
\r
1049 // if we're searching for a group
\r
1050 if (group && startPos)
\r
1052 startOrder = startPos->getTabOrder();
\r
1055 if (!group && Focus && !Focus->isTabGroup())
\r
1057 startOrder = Focus->getTabOrder();
\r
1058 if (startOrder == -1)
\r
1060 // this element is not part of the tab cycle,
\r
1061 // but its parent might be...
\r
1062 IGUIElement *el = Focus;
\r
1063 while (el && el->getParent() && startOrder == -1)
\r
1065 el = el->getParent();
\r
1066 startOrder = el->getTabOrder();
\r
1072 if (group || !startPos)
\r
1073 startPos = this; // start at the root
\r
1075 // find the element
\r
1076 IGUIElement *closest = 0;
\r
1077 IGUIElement *first = 0;
\r
1078 startPos->getNextElement(startOrder, reverse, group, first, closest, false, (FocusFlags & EFF_CAN_FOCUS_DISABLED) != 0);
\r
1081 return closest; // we found an element
\r
1083 return first; // go to the end or the start
\r
1085 return this; // no group found? root group
\r
1090 void CGUIEnvironment::setFocusBehavior(u32 flags)
\r
1092 FocusFlags = flags;
\r
1095 u32 CGUIEnvironment::getFocusBehavior() const
\r
1097 return FocusFlags;
\r
1100 //! creates an GUI Environment
\r
1101 IGUIEnvironment* createGUIEnvironment(io::IFileSystem* fs,
\r
1102 video::IVideoDriver* Driver,
\r
1105 return new CGUIEnvironment(fs, Driver, op);
\r
1109 } // end namespace gui
\r
1110 } // end namespace irr
\r