Introduction

This tutorial explains how QuickGUI works on a high level, and includes what is needed by QuickGUI in order to be used in your application. For those who would like to see a full solution, a QuickGUIOgreDemo application is packaged with QuickGUI, which shows how Ogre and QuickGUI are used together.

History

Previously QuickGUI was built using Ogre as its primary method of rendering UI, however in this new version rendering has been abstracted out of the library. QuickGUI introduces a few abstract classes that must be implemented and passed in for QuickGUI to operate. Not only does this remove QuickGUI's dependency on Ogre, but it gives the developer control over how rendering occurs. This is especially useful if you work in an environment with certain limitations, ie power of 2 texture sizes, or use a specific or custom graphics library. In any case, you are in complete control of the rendering, so you can always adjust it without modifying QuickGUI source!

What if I don't know how to write a solution using Ogre for manual rendering?

In this tutorial I will provide an implementation that uses Ogre 1.7. It's important to realize that you can write your own rendering solution, and that the provided solution may not be the most efficient for all scenarios.

Creating QuickGUI Core

The Core class is similar to Ogre's Root class, providing access to all main subsystems within QuickGUI, and allowing creation and destruction of Interface objects. An Interface is a class that represents a collection of Window and RenderTarget objects, and will be covered later in this tutorial.

Core constructor:

/**
* Constructor.  The passed in ResourceManager and SkinEffectCreator classes will NOT be owned by QuickGUICore, as
* they might be part of a class with scope larger than QuickGUI. Passing in a NULL value for the SkinEffectCreator
* is valid, as long as your UI Skins do not have any Skin Effects.
*/
Core(ResourceManager* m, SkinEffectManager* sem);


NOTE: Both the constructor and destructor are public, and should be created/destroyed by your application.

As seen in the comments, a ResourceManager and SkinEffectManager are required. The SkinEffectManager is part of the Skinning system, and will be covered in another tutorial. Passing in NULL for the SkinEffectManager is perfectly acceptable, provided you don't use any SkinEffect's. (which should be covered in the same tutorial as the SkinEffectManagersmile)

ResourceManager

The ResourceManager class is an abstract class in QuickGUI, and meant to be derived and implemented by your application code. It performs the following functionality:

  • Access to Images by name. An Image is a class representing an image file on disk, and gives read-only access to pixel data. Depending on your implementation, this could also work for Images in memory.
  • Creation and destruction of RenderTexture objects. This class represents a texture in memory that supports drawing of Images and rectangles to it.
  • Creation and destruction of Texture objects. A Texture represents a image in memory, and provides both read and write access to pixel data.


Here is the provided implementation used by the QuickGUIOgreDemo application:
GUIResourceManager.h

NOTE: This class won't compile successfully until the Image, RenderTexture, and Texture classes have been defined and included!
NOTE: When you implement a ResourceManager for QuickGUI, you do not have to create a stand alone class specifically for QuickGUI. For example, your application could have a generic ResourceManager that inherits from the QuickGUI ResourceManager. Pass a pointer to this generic resource manager to the Core constructor, and QuickGUI will work just the same.

Image

A recognized QuickGUI Image must have the following interface:

public:

	/**
	* Returns the ColorValue of the pixel at the position specified.
	*/
	virtual ColorValue getColorAtPosition(const Position& p) = 0;
	/**
	* Gets the name of this Image.
	*/
	virtual std::string getName() = 0;
	/**
	* Gets the size of this Image, in pixels.
	*/
	virtual Size getSize() = 0;

Here is the provided implementation used by the QuickGUIOgreDemo application:
Image.h

RenderTexture

The RenderTexture interface is quite large. Here are a few of the APIs required by the class:

  • clear
  • drawLine
  • drawImage
  • drawRect
  • drawTiledImage
  • getClippingBounds
  • setClippingBounds
  • writeContentsToFile

Here is the provided implementation used by the QuickGUIOgreDemo application:
RenderTexture.h
RenderTexture.cpp

It is strongly recommended to look through the source of these files to gain a better understanding of one approach to providing an implementation for this class.

Texture

A recognized QuickGUI Texture must have the following interface:

public:

	/**
	* Copies a portion of an Image's contents to this Texture.
	* NOTE: width and height of 0 will be expanded to take the maximum width and height 
	*  of the source of destination area.
	*/
	virtual void copyImageToTexture(Image* i, Rect dest = Rect::ZERO, Rect source = Rect::ZERO) = 0;

	/**
	* Locks the pixel buffer for reading and writing purposes.
	*/
	virtual unsigned char* lockBuffer() = 0;

	/**
	* Unlocks the pixel buffer.
	*/
	virtual void unlockBuffer() = 0;

	/**
	* Writes the RenderTarget contents out to an image file.
	*/
	virtual void writeContentsToFile(const std::string& fileName) = 0;

NOTE: The Texture class inherits from the Image class, so the derived Texture class will need to implement all APIs defined by the Texture interface as well as the Image interface.

Here is the provided implementation used by the QuickGUIOgreDemo application:
Texture.h
Texture.cpp

So now we've provided QuickGUI with methods to retrieve image data, and draw them to textures in memory. Whats next?

QuickGUI Scene

As you might have guessed, QuickGUI works by generating Textures and displaying them. We've provided methods to generate textures, but what about displaying them? The Scene class provides QuickGUI with this functionality via RenderTargets, the Overlay and UIPanel classes.

The Scene class should be thought of as the object representing your 3d scene. Overlays are 2d Panels that are drawn on top of your view, and do not change with Camera orientation. UIPanels are 3d Panels inside your scene, that can change orientation, and get smaller, larger, or move out of view, depending on your Camera's position and orientation within the 3d scene. Its worth noting that the UIPanel doesn't have to be implemented if you don't want to support 3d UI. Likewise, the Overlay class doesn't have to be implemented if you don't want to support 2d UI. Simply return NULL for the creation and accessor methods, allowing the Scene class to compile correctly.

Here is the provided implementation used by the QuickGUIOgreDemo application:
Scene.h
Scene.cpp

NOTE: This class won't compile successfully until the Overlay and UIPanel classes have been defined and included!

Overlay

The Overlay implementation is pretty easy to implement, since Ogre already has an Overlay implementation.twisted

Some of the APIs required by the QuickGUI Overlay interface:

  • getPosition
  • getRenderTexture
  • getSize
  • getWindow
  • getZOrder
  • offsetPosition
  • setPosition
  • setRenderTexture
  • setSize
  • setWindow
  • setZOrder


NOTE: The Overlay interface inherits from the QuickGUI RenderTarget interface, so the derived class will need to implement APIs required by both interfaces.

Here is the provided implementation used by the QuickGUIOgreDemo application:
Overlay.h
Overlay.cpp

UIPanel

In terms of Ogre, the UIPanel is a very simple ManualObject consisting of 6 vertices, or 2 triangles, that make up a quad. The required interface is very similar to the QuickGUI Overlay interface, except for getPosition and setPosition.

NOTE: The UIPanel interface inherits from the QuickGUI RenderTarget interface, so the derived class will need to implement APIs required by both interfaces.
NOTE: QuickGUI does not require any UIPanel APIs related to orientation, but that doesn't mean you shouldn't add these in. This will allow you to perform special effects such as UI facing certain targets, or changing orientation.

Here is the provided implementation used by the QuickGUIOgreDemo application:
UIPanel.h
UIPanel.cpp

So now that we have a working Scene implementation, how do we let QuickGUI use it?

Interface


In QuickGUI, an Interface represents a grouping of UI Windows, the RenderTargets to display them, and allows for input injection to interact with the UI. Note that its perfectly acceptable to have multiple RenderTargets sharing the same Window (texture). This means you could have multiple Overlays and UIPanels displaying the same Window. Any change to the Window's texture will be reflected by all RenderTargets.

Interfaces are created via Core APIs:

/**
* Creates an empty Interface.
*/
Interface* createInterface(const std::string& name, Scene* s);
/**
* Creates an interface as defined in XML Data, using the name given.
*/
Interface* createInterface(const std::string& name, XMLData* d, Scene* s);


The second API makes use of the XMLData class, which will be outlined in another tutorial.

Input Injection

Input injection is the primary way users will interact with the GUI, and is done through Interface APIs:

/**
* The KeyCode represents the keyboard key that went down; the unicode_char represents the Text that is injected.
* If there is no text associated with the key, Text::UNIODE_NEL should be passed in.
* Example: KC_H, "h"
* Example: KC_BACKSPACE, UNICODE_NEL
*/
bool injectKeyDown(const KeyCode& kc, unicode_char c = Text::UNICODE_NEL);
bool injectKeyUp(const KeyCode& kc);
/**
* The following functions interact with the GUI using the Mouse.
*/
bool injectMouseButtonDown(const MouseButtonID& button);
bool injectMouseButtonUp(const MouseButtonID& button);
bool injectMouseClick(const MouseButtonID& button);
bool injectMouseDoubleClick(const MouseButtonID& button);
bool injectMouseTripleClick(const MouseButtonID& button);
/**
* This function changes the position of the Mouse Cursor, potentially entering or leaving widgets,
* causing UI state changes.
*/
bool injectMousePosition(const int& xPixelPosition, const int& yPixelPosition);
bool injectMouseWheelChange(float delta);


Some important things to note:

  • QuickGUI does not filter code point injection, as in previous versions. Its the responsibility of the application to decide what input to pass into the Interface.


From the QuickGUIOgreDemo application:

bool MainWindow::keyPressed(const OIS::KeyEvent &arg)
{
	unsigned int codePoint = arg.text;

	// by default, we support codepoints 9, and 32-166.
	if((codePoint == 9) || ((codePoint >= 32) && (codePoint <= 166)))
	{
		;
	}
	else
	{
		codePoint = QuickGUI::Text::UNICODE_NEL;
	}

	mInterface->injectKeyDown(static_cast<QuickGUI::KeyCode>(arg.key),static_cast<QuickGUI::UTFString::unicode_char>(codePoint));

	return true;
}

  • If using OIS, the MouseButtonID's cannot be directly casted to QuickGUI MouseButtonID's, as in previous versions.


From the QuickGUIOgreDemo application:

bool MainWindow::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
	// QuickGUI's MouseButton IDs are in binary values: 1/2/4/8/16/32/64/128
	mInterface->injectMouseButtonDown(static_cast<QuickGUI::MouseButtonID>(1 << id));

	return true;
}

  • By default, the Interface class will automatically determine click, double click, and triple click events and inject them for you. If you want to inject these input events manually, call Interface::setDetermineClickEvents(false);.

What's next?

Now that you're all setup and ready ready to write GUIs, where do you start? The next tutorial will cover creating a basic UI. (however if you want to dive in, the Interface class is the place to start!)

<HR>
Creative Commons Copyright -- Some rights reserved.


THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.

BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.

1. Definitions

  • "Collective Work" means a work, such as a periodical issue, anthology or encyclopedia, in which the Work in its entirety in unmodified form, along with a number of other contributions, constituting separate and independent works in themselves, are assembled into a collective whole. A work that constitutes a Collective Work will not be considered a Derivative Work (as defined below) for the purposes of this License.
  • "Derivative Work" means a work based upon the Work or upon the Work and other pre-existing works, such as a translation, musical arrangement, dramatization, fictionalization, motion picture version, sound recording, art reproduction, abridgment, condensation, or any other form in which the Work may be recast, transformed, or adapted, except that a work that constitutes a Collective Work will not be considered a Derivative Work for the purpose of this License. For the avoidance of doubt, where the Work is a musical composition or sound recording, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered a Derivative Work for the purpose of this License.
  • "Licensor" means the individual or entity that offers the Work under the terms of this License.
  • "Original Author" means the individual or entity who created the Work.
  • "Work" means the copyrightable work of authorship offered under the terms of this License.
  • "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation.
  • "License Elements" means the following high-level license attributes as selected by Licensor and indicated in the title of this License: Attribution, ShareAlike.

2. Fair Use Rights

Nothing in this license is intended to reduce, limit, or restrict any rights arising from fair use, first sale or other limitations on the exclusive rights of the copyright owner under copyright law or other applicable laws.

3. License Grant

Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below:

  • to reproduce the Work, to incorporate the Work into one or more Collective Works, and to reproduce the Work as incorporated in the Collective Works;
  • to create and reproduce Derivative Works;
  • to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission the Work including as incorporated in Collective Works;
  • to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission Derivative Works.
  • For the avoidance of doubt, where the work is a musical composition:
    • Performance Royalties Under Blanket Licenses. Licensor waives the exclusive right to collect, whether individually or via a performance rights society (e.g. ASCAP, BMI, SESAC), royalties for the public performance or public digital performance (e.g. webcast) of the Work.
    • Mechanical Rights and Statutory Royalties. Licensor waives the exclusive right to collect, whether individually or via a music rights society or designated agent (e.g. Harry Fox Agency), royalties for any phonorecord You create from the Work ("cover version") and distribute, subject to the compulsory license created by 17 USC Section 115 of the US Copyright Act (or the equivalent in other jurisdictions).
    • Webcasting Rights and Statutory Royalties. For the avoidance of doubt, where the Work is a sound recording, Licensor waives the exclusive right to collect, whether individually or via a performance-rights society (e.g. SoundExchange), royalties for the public digital performance (e.g. webcast) of the Work, subject to the compulsory license created by 17 USC Section 114 of the US Copyright Act (or the equivalent in other jurisdictions).


The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. All rights not expressly granted by Licensor are hereby reserved.

4. Restrictions

The license granted in Section 3 above is expressly made subject to and limited by the following restrictions:

  • You may distribute, publicly display, publicly perform, or publicly digitally perform the Work only under the terms of this License, and You must include a copy of, or the Uniform Resource Identifier for, this License with every copy or phonorecord of the Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Work that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Work itself to be made subject to the terms of this License. If You create a Collective Work, upon notice from any Licensor You must, to the extent practicable, remove from the Collective Work any credit as required by clause 4(c), as requested. If You create a Derivative Work, upon notice from any Licensor You must, to the extent practicable, remove from the Derivative Work any credit as required by clause 4(c), as requested.
  • You may distribute, publicly display, publicly perform, or publicly digitally perform a Derivative Work only under the terms of this License, a later version of this License with the same License Elements as this License, or a Creative Commons iCommons license that contains the same License Elements as this License (e.g. Attribution-ShareAlike 2.5 Japan). You must include a copy of, or the Uniform Resource Identifier for, this License or other license specified in the previous sentence with every copy or phonorecord of each Derivative Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Derivative Works that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder, and You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Derivative Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Derivative Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Derivative Work itself to be made subject to the terms of this License.
  • If you distribute, publicly display, publicly perform, or publicly digitally perform the Work or any Derivative Works or Collective Works, You must keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or (ii) if the Original Author and/or Licensor designate another party or parties (e.g. a sponsor institute, publishing entity, journal) for attribution in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; the title of the Work if supplied; to the extent reasonably practicable, the Uniform Resource Identifier, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and in the case of a Derivative Work, a credit identifying the use of the Work in the Derivative Work (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). Such credit may be implemented in any reasonable manner; provided, however, that in the case of a Derivative Work or Collective Work, at a minimum such credit will appear where any other comparable authorship credit appears and in a manner at least as prominent as such other comparable authorship credit.

5. Representations, Warranties and Disclaimer

UNLESS OTHERWISE AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE MATERIALS, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU.

6. Limitation on Liability.

EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

7. Termination

  • This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Derivative Works or Collective Works from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License.
  • Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above.

8. Miscellaneous

  • Each time You distribute or publicly digitally perform the Work or a Collective Work, the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License.
  • Each time You distribute or publicly digitally perform a Derivative Work, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License.
  • If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
  • No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent.
  • This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You.