1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
|
/* Copyright (C) 2013 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
/*
CGUI
--Overview--
This is the top class of the whole GUI, all objects
and settings are stored within this class.
--More info--
Check GUI.h
*/
#ifndef INCLUDED_CGUI
#define INCLUDED_CGUI
//--------------------------------------------------------
// Includes / Compiler directives
//--------------------------------------------------------
// NOTE: GUI.h included at the bottom of this file (has to be after CGUI class
// definition)
#include "GUITooltip.h"
#include "GUIbase.h"
#include "scriptinterface/ScriptInterface.h"
#include "ps/Overlay.h" // CPos and CRect
#include "lib/input.h"
#include "ps/XML/Xeromyces.h"
#include <boost/unordered_set.hpp>
//--------------------------------------------------------
// Macros
//--------------------------------------------------------
//--------------------------------------------------------
// Types
//--------------------------------------------------------
//--------------------------------------------------------
// Error declarations
//--------------------------------------------------------
ERROR_TYPE(GUI, JSOpenFailed);
//--------------------------------------------------------
// Declarations
//--------------------------------------------------------
/**
* Contains a list of values for new defaults to objects.
*/
struct SGUIStyle
{
// A list of defaults for
std::map<CStr, CStrW> m_SettingsDefaults;
};
class JSObject; // The GUI stores a JSObject*, so needs to know that JSObject exists
class IGUIObject;
class CGUISpriteInstance;
struct SGUIText;
struct CColor;
struct SGUIText;
struct SGUIIcon;
class CGUIString;
class CGUISprite;
struct SGUIImageEffects;
struct SGUIScrollBarStyle;
class GUITooltip;
/**
* The main object that represents a whole GUI page.
*
* No interfacial functions throws.
*/
class CGUI
{
NONCOPYABLE(CGUI);
friend class IGUIObject;
friend class IGUIScrollBarOwner;
friend class CInternalCGUIAccessorBase;
private:
// Private typedefs
typedef IGUIObject *(*ConstructObjectFunction)();
public:
CGUI(const shared_ptr<ScriptRuntime>& runtime);
~CGUI();
/**
* Initializes the GUI, needs to be called before the GUI is used
*/
void Initialize();
/**
* Performs processing that should happen every frame
* (including sending the "Tick" event to scripts)
*/
void TickObjects();
/**
* Sends a specified script event to every object
*
* @param EventName String representation of event name
*/
void SendEventToAll(const CStr& EventName);
/**
* Displays the whole GUI
*/
void Draw();
/**
* Draw GUI Sprite
*
* @param Sprite Object referring to the sprite (which also caches
* calculations for faster rendering)
* @param CellID Number of the icon cell to use. (Ignored if this sprite doesn't
* have any images with "cell-size")
* @param Z Drawing order, depth value
* @param Rect Position and Size
* @param Clipping The sprite shouldn't be drawn outside this rectangle
*/
void DrawSprite(const CGUISpriteInstance& Sprite, int CellID, const float &Z,
const CRect &Rect, const CRect &Clipping=CRect());
/**
* Draw a SGUIText object
*
* @param Text Text object.
* @param DefaultColor Color used if no tag applied.
* @param pos position
* @param z z value.
* @param clipping
*/
void DrawText(SGUIText &Text, const CColor &DefaultColor,
const CPos &pos, const float &z, const CRect &clipping);
/**
* Clean up, call this to clean up all memory allocated
* within the GUI.
*/
void Destroy();
/**
* The replacement of Process(), handles an SDL_Event_
*
* @param ev SDL Event, like mouse/keyboard input
*/
InReaction HandleEvent(const SDL_Event_* ev);
/**
* Load a GUI XML file into the GUI.
*
* <b>VERY IMPORTANT!</b> All \<styles\>-files must be read before
* everything else!
*
* @param Filename Name of file
* @param Paths Set of paths; all XML and JS files loaded will be added to this
*/
void LoadXmlFile(const VfsPath& Filename, boost::unordered_set<VfsPath>& Paths);
/**
* Checks if object exists and return true or false accordingly
*
* @param Name String name of object
* @return true if object exists
*/
bool ObjectExists(const CStr& Name) const;
/**
* Returns the GUI object with the desired name, or NULL
* if no match is found,
*
* @param Name String name of object
* @return Matching object, or NULL
*/
IGUIObject* FindObjectByName(const CStr& Name) const;
/**
* Returns the GUI object under the mouse, or NULL if none.
*/
IGUIObject* FindObjectUnderMouse() const;
/**
* The GUI needs to have all object types inputted and
* their constructors. Also it needs to associate a type
* by a string name of the type.
*
* To add a type:
* @code
* AddObjectType("button", &CButton::ConstructObject);
* @endcode
*
* @param str Reference name of object type
* @param pFunc Pointer of function ConstuctObject() in the object
*
* @see CGUI#ConstructObject()
*/
void AddObjectType(const CStr& str, ConstructObjectFunction pFunc) { m_ObjectTypes[str] = pFunc; }
/**
* Update Resolution, should be called every time the resolution
* of the OpenGL screen has been changed, this is because it needs
* to re-cache all its actual sizes
*
* Needs no input since screen resolution is global.
*
* @see IGUIObject#UpdateCachedSize()
*/
void UpdateResolution();
/**
* Generate a SGUIText object from the inputted string.
* The function will break down the string and its
* tags to calculate exactly which rendering queries
* will be sent to the Renderer. Also, horizontal alignment
* is taken into acount in this method but NOT vertical alignment.
*
* Done through the CGUI since it can communicate with
*
* @param Text Text to generate SGUIText object from
* @param Font Default font, notice both Default color and default font
* can be changed by tags.
* @param Width Width, 0 if no word-wrapping.
* @param BufferZone space between text and edge, and space between text and images.
* @param pObject Optional parameter for error output. Used *only* if error parsing fails,
* and we need to be able to output which object the error occured in to aid the user.
*/
SGUIText GenerateText(const CGUIString &Text, const CStrW& Font,
const float &Width, const float &BufferZone,
const IGUIObject *pObject=NULL);
/**
* Check if an icon exists
*/
bool IconExists(const CStr& str) const { return (m_Icons.count(str) != 0); }
/**
* Get Icon (a copy, can never be changed)
*/
SGUIIcon GetIcon(const CStr& str) const { return m_Icons.find(str)->second; }
/**
* Get pre-defined color (if it exists)
* Returns false if it fails.
*/
bool GetPreDefinedColor(const CStr& name, CColor &Output);
shared_ptr<ScriptInterface> GetScriptInterface() { return m_ScriptInterface; };
jsval GetGlobalObject() { return m_ScriptInterface->GetGlobalObject(); };
private:
/**
* Updates the object pointers, needs to be called each
* time an object has been added or removed.
*
* This function is atomic, meaning if it throws anything, it will
* have seen it through that nothing was ultimately changed.
*
* @throws PSERROR_GUI that is thrown from IGUIObject::AddToPointersMap().
*/
void UpdateObjects();
/**
* Adds an object to the GUI's object database
* Private, since you can only add objects through
* XML files. Why? Because it enables the GUI to
* be much more encapsulated and safe.
*
* @throws Rethrows PSERROR_GUI from IGUIObject::AddChild().
*/
void AddObject(IGUIObject* pObject);
/**
* You input the name of the object type, and let's
* say you input "button", then it will construct a
* CGUIObjet* as a CButton.
*
* @param str Name of object type
* @return Newly constructed IGUIObject (but constructed as a subclass)
*/
IGUIObject *ConstructObject(const CStr& str);
/**
* Get Focused Object.
*/
IGUIObject *GetFocusedObject() { return m_FocusedObject; }
public:
/**
* Change focus to new object.
* Will send LOST_FOCUS/GOT_FOCUS messages as appropriate.
* pObject can be NULL to remove all focus.
*/
void SetFocusedObject(IGUIObject* pObject);
private:
//--------------------------------------------------------
/** @name XML Reading Xeromyces specific subroutines
*
* These does not throw!
* Because when reading in XML files, it won't be fatal
* if an error occurs, perhaps one particular object
* fails, but it'll still continue reading in the next.
* All Error are reported with ReportParseError
*/
//--------------------------------------------------------
/*
Xeromyces_* functions tree
<objects> (ReadRootObjects)
|
+-<script> (ReadScript)
|
+-<object> (ReadObject)
|
+-<action>
|
+-Optional Type Extensions (IGUIObject::ReadExtendedElement) TODO
|
+-<<object>> *recursive*
<styles> (ReadRootStyles)
|
+-<style> (ReadStyle)
<sprites> (ReadRootSprites)
|
+-<sprite> (ReadSprite)
|
+-<image> (ReadImage)
<setup> (ReadRootSetup)
|
+-<tooltip> (ReadToolTip)
|
+-<scrollbar> (ReadScrollBar)
|
+-<icon> (ReadIcon)
|
+-<color> (ReadColor)
*/
//@{
// Read Roots
/**
* Reads in the root element \<objects\> (the DOMElement).
*
* @param Element The Xeromyces object that represents
* the objects-tag.
* @param pFile The Xeromyces object for the file being read
* @param Paths Collects the set of all XML/JS files that are loaded
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadRootObjects(XMBElement Element, CXeromyces* pFile, boost::unordered_set<VfsPath>& Paths);
/**
* Reads in the root element \<sprites\> (the DOMElement).
*
* @param Element The Xeromyces object that represents
* the sprites-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadRootSprites(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the root element \<styles\> (the DOMElement).
*
* @param Element The Xeromyces object that represents
* the styles-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadRootStyles(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the root element \<setup\> (the DOMElement).
*
* @param Element The Xeromyces object that represents
* the setup-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadRootSetup(XMBElement Element, CXeromyces* pFile);
// Read Subs
/**
* Notice! Recursive function!
*
* Read in an \<object\> (the XMBElement) and stores it
* as a child in the pParent.
*
* It will also check the object's children and call this function
* on them too. Also it will call all other functions that reads
* in other stuff that can be found within an object. Check the
* tree in the beginning of this class' Xeromyces_* section.
*
* @param Element The Xeromyces object that represents
* the object-tag.
* @param pFile The Xeromyces object for the file being read
* @param pParent Parent to add this object as child in.
* @param NameSubst A set of substitution strings that will be
* applied to all object names within this object.
* @param Paths Output set of file paths that this GUI object
* relies on.
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, const std::vector<std::pair<CStr, CStr> >& NameSubst, boost::unordered_set<VfsPath>& Paths, u32 nesting_depth);
/**
* Reads in the element \<repeat\>, which repeats its child \<object\>s
* 'count' times, replacing the string "[n]" in its descendants' names
* with "[0]", "[1]", etc.
*/
void Xeromyces_ReadRepeat(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, boost::unordered_set<VfsPath>& Paths, u32 nesting_depth);
/**
* Reads in the element \<script\> (the XMBElement) and executes
* the script's code.
*
* @param Element The Xeromyces object that represents
* the script-tag.
* @param pFile The Xeromyces object for the file being read
* @param Paths Output set of file paths that this script is loaded from.
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadScript(XMBElement Element, CXeromyces* pFile, boost::unordered_set<VfsPath>& Paths);
/**
* Reads in the element \<sprite\> (the XMBElement) and stores the
* result in a new CGUISprite.
*
* @param Element The Xeromyces object that represents
* the sprite-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadSprite(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the element \<image\> (the XMBElement) and stores the
* result within the CGUISprite.
*
* @param Element The Xeromyces object that represents
* the image-tag.
* @param pFile The Xeromyces object for the file being read
* @param parent Parent sprite.
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite &parent);
/**
* Reads in the element \<effect\> (the XMBElement) and stores the
* result within the SGUIImageEffects.
*
* @param Element The Xeromyces object that represents
* the image-tag.
* @param pFile The Xeromyces object for the file being read
* @param effects Effects object to add this effect to.
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadEffects(XMBElement Element, CXeromyces* pFile, SGUIImageEffects &effects);
/**
* Reads in the element \<style\> (the XMBElement) and stores the
* result in m_Styles.
*
* @param Element The Xeromyces object that represents
* the style-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadStyle(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the element \<scrollbar\> (the XMBElement) and stores the
* result in m_ScrollBarStyles.
*
* @param Element The Xeromyces object that represents
* the scrollbar-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadScrollBarStyle(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the element \<icon\> (the XMBElement) and stores the
* result in m_Icons.
*
* @param Element The Xeromyces object that represents
* the scrollbar-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadIcon(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the element \<tooltip\> (the XMBElement) and stores the
* result as an object with the name __tooltip_#.
*
* @param Element The Xeromyces object that represents
* the scrollbar-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadTooltip(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the element \<color\> (the XMBElement) and stores the
* result in m_PreDefinedColors
*
* @param Element The Xeromyces object that represents
* the scrollbar-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadColor(XMBElement Element, CXeromyces* pFile);
//@}
private:
// Variables
//--------------------------------------------------------
/** @name Miscellaneous */
//--------------------------------------------------------
//@{
shared_ptr<ScriptInterface> m_ScriptInterface;
/**
* don't want to pass this around with the
* ChooseMouseOverAndClosest broadcast -
* we'd need to pack this and pNearest in a struct
*/
CPos m_MousePos;
/**
* Indicates which buttons are pressed (bit 0 = LMB,
* bit 1 = RMB, bit 2 = MMB)
*/
unsigned int m_MouseButtons;
// Tooltip
GUITooltip m_Tooltip;
/**
* This is a bank of custom colors, it is simply a look up table that
* will return a color object when someone inputs the name of that
* color. Of course the colors have to be declared in XML, there are
* no hard-coded values.
*/
std::map<CStr, CColor> m_PreDefinedColors;
//@}
//--------------------------------------------------------
/** @name Objects */
//--------------------------------------------------------
//@{
/**
* Base Object, all its children are considered parentless
* because this is not a real object per se.
*/
IGUIObject* m_BaseObject;
/**
* Focused object!
* Say an input box that is selected. That one is focused.
* There can only be one focused object.
*/
IGUIObject* m_FocusedObject;
/**
* Just pointers for fast name access, each object
* is really constructed within its parent for easy
* recursive management.
* Notice m_BaseObject won't belong here since it's
* not considered a real object.
*/
map_pObjects m_pAllObjects;
/**
* Number of object that has been given name automatically.
* the name given will be '__internal(#)', the number (#)
* being this variable. When an object's name has been set
* as followed, the value will increment.
*/
int m_InternalNameNumber;
/**
* Function pointers to functions that constructs
* IGUIObjects by name... For instance m_ObjectTypes["button"]
* is filled with a function that will "return new CButton();"
*/
std::map<CStr, ConstructObjectFunction> m_ObjectTypes;
/**
* Map from hotkey names to objects that listen to the hotkey.
* (This is an optimisation to avoid recursing over the whole GUI
* tree every time a hotkey is pressed).
* Currently this is only set at load time - dynamic changes to an
* object's hotkey property will be ignored.
*/
std::map<CStr, std::vector<IGUIObject*> > m_HotkeyObjects;
//--------------------------------------------------------
// Databases
//--------------------------------------------------------
// Sprites
std::map<CStr, CGUISprite*> m_Sprites;
// Styles
std::map<CStr, SGUIStyle> m_Styles;
// Scroll-bar styles
std::map<CStr, SGUIScrollBarStyle> m_ScrollBarStyles;
// Icons
std::map<CStr, SGUIIcon> m_Icons;
};
#endif
|