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
|
/***********************************************************************
filename: Sample_Text.cpp
modified: 26/2/2008
author: Cegui Team
*************************************************************************/
/***************************************************************************
* Copyright (C) 2004 - 2008 Paul D Turner & The CEGUI Development Team
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
***************************************************************************/
#include "Sample_Text.h"
#include "CEGUI.h"
#include "CEGuiBaseApplication.h"
#include <cstdlib>
using namespace CEGUI;
/*************************************************************************
Sample specific initialisation goes here.
*************************************************************************/
bool TextDemo::initialiseSample()
{
// we will make extensive use of the WindowManager.
WindowManager& winMgr = WindowManager::getSingleton();
// load scheme and set up defaults
SchemeManager::getSingleton().create("TaharezLook.scheme");
System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
// We need a font
FontManager::getSingleton().create("DejaVuSans-10.font");
// Font defaulting
if(FontManager::getSingleton().isDefined("DejaVuSans-10"))
{
System::getSingleton().setDefaultFont("DejaVuSans-10");
}
// load an image to use as a background
ImagesetManager::getSingleton().createFromImageFile("BackgroundImage", "GPN-2000-001437.tga");
// here we will use a StaticImage as the root, then we can use it to place a background image
Window* background = winMgr.createWindow("TaharezLook/StaticImage", "background_wnd");
// set position and size
background->setPosition(UVector2(cegui_reldim(0), cegui_reldim( 0)));
background->setSize(UVector2(cegui_reldim(1), cegui_reldim( 1)));
// disable frame and standard background
background->setProperty("FrameEnabled", "false");
background->setProperty("BackgroundEnabled", "false");
// set the background image
background->setProperty("Image", "set:BackgroundImage image:full_image");
// install this as the root GUI sheet
System::getSingleton().setGUISheet(background);
// Load our layout as a basic
background->addChildWindow (winMgr.loadWindowLayout ("TextDemo.layout"));
// Init the seperate blocks which make up this sample
initStaticText();
initSingleLineEdit();
initMultiLineEdit();
// Quit button
subscribeEvent("TextDemo/Quit", PushButton::EventClicked, Event::Subscriber(&TextDemo::quit, this));
// Success (so far)
return true;
}
void TextDemo::initStaticText()
{
// Name, Group, Selected
initRadio("TextDemo/HorzLeft", 0, true);
initRadio("TextDemo/HorzRight", 0, false);
initRadio("TextDemo/HorzCentered", 0, false);
// New group!
initRadio("TextDemo/VertTop", 1, true);
initRadio("TextDemo/VertBottom", 1, false);
initRadio("TextDemo/VertCentered", 1, false);
//
// Events
//
// Word-wrap checkbox (we can't re-use a handler struct for the last argument!!)
subscribeEvent("TextDemo/Wrap", Checkbox::EventCheckStateChanged, Event::Subscriber(&TextDemo::formatChangedHandler, this));
subscribeEvent("TextDemo/HorzLeft", RadioButton::EventSelectStateChanged, Event::Subscriber(&TextDemo::formatChangedHandler, this));
subscribeEvent("TextDemo/HorzRight", RadioButton::EventSelectStateChanged, Event::Subscriber(&TextDemo::formatChangedHandler, this));
subscribeEvent("TextDemo/HorzCentered", RadioButton::EventSelectStateChanged, Event::Subscriber(&TextDemo::formatChangedHandler, this));
subscribeEvent("TextDemo/VertTop", RadioButton::EventSelectStateChanged, Event::Subscriber(&TextDemo::formatChangedHandler, this));
subscribeEvent("TextDemo/VertBottom", RadioButton::EventSelectStateChanged, Event::Subscriber(&TextDemo::formatChangedHandler, this));
subscribeEvent("TextDemo/VertCentered", RadioButton::EventSelectStateChanged, Event::Subscriber(&TextDemo::formatChangedHandler, this));
}
void TextDemo::initSingleLineEdit()
{
WindowManager& winMgr = WindowManager::getSingleton();
// Only accepts digits for the age field
if (winMgr.isWindowPresent("TextDemo/editAge"))
{
static_cast<Editbox*>(winMgr.getWindow("TextDemo/editAge"))->setValidationString("[0-9]*");
}
// Set password restrictions
if (winMgr.isWindowPresent("TextDemo/editAge"))
{
Editbox* passwd = static_cast<Editbox*>(winMgr.getWindow("TextDemo/editPasswd"));
passwd->setValidationString("[A-Za-z0-9]*");
// Render masked
passwd->setTextMasked(true);
}
}
void TextDemo::initMultiLineEdit()
{
// Scrollbar checkbox
subscribeEvent("TextDemo/forceScroll", Checkbox::EventCheckStateChanged, Event::Subscriber(&TextDemo::vertScrollChangedHandler, this));
}
void TextDemo::initRadio(const CEGUI::String& radio, int group, bool selected)
{
WindowManager& winMgr = WindowManager::getSingleton();
if (winMgr.isWindowPresent(radio))
{
RadioButton* button = static_cast<RadioButton*>(winMgr.getWindow(radio));
button->setGroupID(group);
button->setSelected(selected);
}
}
void TextDemo::subscribeEvent(const String& widget, const String& event, const Event::Subscriber& method)
{
WindowManager& winMgr = WindowManager::getSingleton();
if (winMgr.isWindowPresent(widget))
{
Window* window = winMgr.getWindow(widget);
window->subscribeEvent(event, method);
}
}
bool TextDemo::isRadioSelected(const CEGUI::String& radio)
{
WindowManager& winMgr = WindowManager::getSingleton();
// Check
if (winMgr.isWindowPresent(radio))
{
RadioButton* button = static_cast<RadioButton*>(winMgr.getWindow(radio));
return button->isSelected();
}
return false;
}
bool TextDemo::isCheckboxSelected(const CEGUI::String& checkbox)
{
WindowManager& winMgr = WindowManager::getSingleton();
// Check
if (winMgr.isWindowPresent(checkbox))
{
Checkbox* button = static_cast<Checkbox*>(winMgr.getWindow(checkbox));
return button->isSelected();
}
return false;
}
bool TextDemo::formatChangedHandler(const CEGUI::EventArgs&)
{
// we will use the WindowManager to get access to the widgets
WindowManager& winMgr = WindowManager::getSingleton();
if (winMgr.isWindowPresent("TextDemo/StaticText"))
{
// and also the static text for which we will set the formatting options
Window* st = winMgr.getWindow("TextDemo/StaticText");
// handle vertical formatting settings
if (isRadioSelected("TextDemo/VertTop"))
st->setProperty("VertFormatting", "TopAligned");
else if (isRadioSelected("TextDemo/VertBottom"))
st->setProperty("VertFormatting", "BottomAligned");
else if (isRadioSelected("TextDemo/VertCentered"))
st->setProperty("VertFormatting", "VertCentred");
// handle horizontal formatting settings
bool wrap = isCheckboxSelected("TextDemo/Wrap");
if (isRadioSelected("TextDemo/HorzLeft"))
st->setProperty("HorzFormatting", wrap ? "WordWrapLeftAligned" : "LeftAligned");
else if (isRadioSelected("TextDemo/HorzRight"))
st->setProperty("HorzFormatting", wrap ? "WordWrapRightAligned" : "RightAligned");
else if (isRadioSelected("TextDemo/HorzCentered"))
st->setProperty("HorzFormatting", wrap ? "WordWrapCentred" : "HorzCentred");
}
// event was handled
return true;
}
bool TextDemo::vertScrollChangedHandler(const CEGUI::EventArgs&)
{
WindowManager& winMgr = WindowManager::getSingleton();
if (winMgr.isWindowPresent("TextDemo/editMulti"))
{
MultiLineEditbox* multiEdit = static_cast<MultiLineEditbox*>(winMgr.getWindow("TextDemo/editMulti"));
// Use setter for a change
multiEdit->setShowVertScrollbar(isCheckboxSelected("TextDemo/forceScroll"));
}
// event was handled
return true;
}
bool TextDemo::quit(const CEGUI::EventArgs&)
{
// signal quit
d_sampleApp->setQuitting();
// event was handled
return true;
}
/*************************************************************************
Cleans up resources allocated in the initialiseSample call.
*************************************************************************/
void TextDemo::cleanupSample()
{
// nothing to do here!
}
// Main app
int main(int /*argc*/, char* /*argv*/[])
{
// This is a basic start-up for the sample application which is
// object orientated in nature, so we just need an instance of
// the CEGuiSample based object and then tell that sample application
// to run. All of the samples will use code similar to this in the
// main/WinMain function.
TextDemo app;
return app.run();
}
|