1 /***********************************************************************
2     created:    11/8/2012
3     author:     Lukas E Meindl
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2012 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #include "HelloWorld.h"
28 #include "CEGUI/CEGUI.h"
29 
30 #include <iostream>
31 
32 
33 /*************************************************************************
34     Sample specific initialisation goes here.
35 *************************************************************************/
initialise(CEGUI::GUIContext * guiContext)36 bool HelloWorldDemo::initialise(CEGUI::GUIContext* guiContext)
37 {
38     using namespace CEGUI;
39 
40      d_usedFiles = CEGUI::String(__FILE__);
41 
42     // CEGUI relies on various systems being set-up, so this is what we do
43     // here first.
44     //
45     // The first thing to do is load a CEGUI 'scheme' this is basically a file
46     // that groups all the required resources and definitions for a particular
47     // skin so they can be loaded / initialised easily
48     //
49     // So, we use the SchemeManager singleton to load in a scheme that loads the
50     // imagery and registers widgets for the TaharezLook skin.  This scheme also
51     // loads in a font that gets used as the system default.
52     SchemeManager::getSingleton().createFromFile("TaharezLook.scheme");
53 
54     // The next thing we do is to set a default mouse cursor image.  This is
55     // not strictly essential, although it is nice to always have a visible
56     // cursor if a window or widget does not explicitly set one of its own.
57     //
58     // The TaharezLook Imageset contains an Image named "MouseArrow" which is
59     // the ideal thing to have as a defult mouse cursor image.
60     guiContext->getMouseCursor().setDefaultImage("TaharezLook/MouseArrow");
61 
62     // Now the system is initialised, we can actually create some UI elements, for
63     // this first example, a full-screen 'root' window is set as the active GUI
64     // sheet, and then a simple frame window will be created and attached to it.
65 
66     // All windows and widgets are created via the WindowManager singleton.
67     WindowManager& winMgr = WindowManager::getSingleton();
68 
69     // Here we create a "DefaultWindow".  This is a native type, that is, it does
70     // not have to be loaded via a scheme, it is always available.  One common use
71     // for the DefaultWindow is as a generic container for other windows.  Its
72     // size defaults to 1.0f x 1.0f using the Relative metrics mode, which means
73     // when it is set as the root GUI sheet window, it will cover the entire display.
74     // The DefaultWindow does not perform any rendering of its own, so is invisible.
75     //
76     // Create a DefaultWindow called 'Root'.
77     d_root = (DefaultWindow*)winMgr.createWindow("DefaultWindow", "Root");
78 
79     // load font and setup default if not loaded via scheme
80     Font& defaultFont = FontManager::getSingleton().createFromFile("DejaVuSans-12.font");
81     // Set default font for the gui context
82     guiContext->setDefaultFont(&defaultFont);
83 
84     // Set the root window as root of our GUI Context
85     guiContext->setRootWindow(d_root);
86 
87     // A FrameWindow is a window with a frame and a titlebar which may be moved around
88     // and resized.
89     //
90     // Create a FrameWindow in the TaharezLook style, and name it 'Demo Window'
91     FrameWindow* wnd = (FrameWindow*)winMgr.createWindow("TaharezLook/FrameWindow", "Demo Window");
92 
93     // Here we attach the newly created FrameWindow to the previously created
94     // DefaultWindow which we will be using as the root of the displayed gui.
95     d_root->addChild(wnd);
96 
97     // Windows are in Relative metrics mode by default.  This means that we can
98     // specify sizes and positions without having to know the exact pixel size
99     // of the elements in advance.  The relative metrics mode co-ordinates are
100     // relative to the parent of the window where the co-ordinates are being set.
101     // This means that if 0.5f is specified as the width for a window, that window
102     // will be half as its parent window.
103     //
104     // Here we set the FrameWindow so that it is half the size of the display,
105     // and centered within the display.
106     wnd->setPosition(UVector2(cegui_reldim(0.25f), cegui_reldim( 0.25f)));
107     wnd->setSize(USize(cegui_reldim(0.5f), cegui_reldim( 0.5f)));
108 
109     // now we set the maximum and minum sizes for the new window.  These are
110     // specified using relative co-ordinates, but the important thing to note
111     // is that these settings are aways relative to the display rather than the
112     // parent window.
113     //
114     // here we set a maximum size for the FrameWindow which is equal to the size
115     // of the display, and a minimum size of one tenth of the display.
116     wnd->setMaxSize(USize(cegui_reldim(1.0f), cegui_reldim( 1.0f)));
117     wnd->setMinSize(USize(cegui_reldim(0.1f), cegui_reldim( 0.1f)));
118 
119     // As a final step in the initialisation of our sample window, we set the window's
120     // text to "Hello World!", so that this text will appear as the caption in the
121     // FrameWindow's titlebar.
122     wnd->setText("Hello World!");
123 
124     wnd->subscribeEvent(CEGUI::Window::EventMouseClick,  Event::Subscriber(&HelloWorldDemo::handleHelloWorldClicked, this));
125 
126     // return true so that the samples framework knows that initialisation was a
127     // success, and that it should now run the sample.
128     return true;
129 }
130 
131 
132 /*************************************************************************
133     Cleans up resources allocated in the initialiseSample call.
134 *************************************************************************/
deinitialise()135 void HelloWorldDemo::deinitialise()
136 {
137 }
138 
handleHelloWorldClicked(const CEGUI::EventArgs &)139 bool HelloWorldDemo::handleHelloWorldClicked(const CEGUI::EventArgs&)
140 {
141     std::cout << "Hello World!" << std::endl;
142 
143     return false;
144 }
145 
146 /*************************************************************************
147     Define the module function that returns an instance of the sample
148 *************************************************************************/
getSampleInstance()149 extern "C" SAMPLE_EXPORT Sample& getSampleInstance()
150 {
151     static HelloWorldDemo sample;
152     return sample;
153 }
154