Something went wrong. Try again.
Massive multiplayer game aiming to create a collaborative musical environment.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435/*********************************************************************** filename: CEGUISpinner.cpp created: 3/2/2005 author: Paul D Turner*************************************************************************//*************************************************************************** * Copyright (C) 2004 - 2006 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 "elements/CEGUISpinner.h"#include "elements/CEGUIPushButton.h"#include "elements/CEGUIEditbox.h"#include "CEGUIExceptions.h"#include "CEGUIWindowManager.h"#include <stdio.h>#include <sstream>#include <iomanip>
// Start of CEGUI namespace sectionnamespace CEGUI{ const String Spinner::WidgetTypeName("CEGUI/Spinner");
////////////////////////////////////////////////////////////////////////// // event strings const String Spinner::EventNamespace("Spinner"); const String Spinner::EventValueChanged("ValueChanged"); const String Spinner::EventStepChanged("StepChanged"); const String Spinner::EventMaximumValueChanged("MaximumValueChanged"); const String Spinner::EventMinimumValueChanged("MinimumValueChanged"); const String Spinner::EventTextInputModeChanged("TextInputModeChanged"); // Validator strings const String Spinner::FloatValidator("-?\\d*\\.?\\d*"); const String Spinner::IntegerValidator("-?\\d*"); const String Spinner::HexValidator("[0-9a-fA-F]*"); const String Spinner::OctalValidator("[0-7]*"); // component widget name suffix strings const String Spinner::EditboxNameSuffix( "__auto_editbox__" ); const String Spinner::IncreaseButtonNameSuffix( "__auto_incbtn__" ); const String Spinner::DecreaseButtonNameSuffix( "__auto_decbtn__" ); // properties SpinnerProperties::CurrentValue Spinner::d_currentValueProperty; SpinnerProperties::StepSize Spinner::d_stepSizeProperty; SpinnerProperties::MaximumValue Spinner::d_maxValueProperty; SpinnerProperties::MinimumValue Spinner::d_minValueProperty; SpinnerProperties::TextInputMode Spinner::d_textInputModeProperty; ////////////////////////////////////////////////////////////////////////// Spinner::Spinner(const String& type, const String& name) : Window(type, name), d_stepSize(1.0f), d_currentValue(1.0f), d_maxValue(32767.0f), d_minValue(-32768.0f), d_inputMode((TextInputMode)-1) { addSpinnerProperties(); }
Spinner::~Spinner(void) { // Nothing to do here. }
void Spinner::initialiseComponents(void) { // get all the component widgets PushButton* increaseButton = getIncreaseButton(); PushButton* decreaseButton = getDecreaseButton(); Editbox* editbox = getEditbox();
// setup component controls increaseButton->setWantsMultiClickEvents(false); increaseButton->setMouseAutoRepeatEnabled(true); decreaseButton->setWantsMultiClickEvents(false); decreaseButton->setMouseAutoRepeatEnabled(true);
// perform event subscriptions. increaseButton->subscribeEvent(Window::EventMouseButtonDown, Event::Subscriber(&Spinner::handleIncreaseButton, this)); decreaseButton->subscribeEvent(Window::EventMouseButtonDown, Event::Subscriber(&Spinner::handleDecreaseButton, this)); editbox->subscribeEvent(Window::EventTextChanged, Event::Subscriber(&Spinner::handleEditTextChange, this));
// final initialisation setTextInputMode(Integer); setCurrentValue(0.0f); performChildWindowLayout(); }
float Spinner::getCurrentValue(void) const { return d_currentValue; }
float Spinner::getStepSize(void) const { return d_stepSize; }
float Spinner::getMaximumValue(void) const { return d_maxValue; }
float Spinner::getMinimumValue(void) const { return d_minValue; }
Spinner::TextInputMode Spinner::getTextInputMode(void) const { return d_inputMode; }
void Spinner::setCurrentValue(float value) { if (value != d_currentValue) { // limit input value to within valid range for spinner value = ceguimax(ceguimin(value, d_maxValue), d_minValue);
d_currentValue = value;
WindowEventArgs args(this); onValueChanged(args); } }
void Spinner::setStepSize(float step) { if (step != d_stepSize) { d_stepSize = step; WindowEventArgs args(this); onStepChanged(args); } }
void Spinner::setMaximumValue(float maxValue) { if (maxValue != d_maxValue) { d_maxValue = maxValue;
WindowEventArgs args(this); onMaximumValueChanged(args); } }
void Spinner::setMinimumValue(float minVaue) { if (minVaue != d_minValue) { d_minValue = minVaue;
WindowEventArgs args(this); onMinimumValueChanged(args); } }
void Spinner::setTextInputMode(TextInputMode mode) { if (mode != d_inputMode) { switch (mode) { case FloatingPoint: getEditbox()->setValidationString(FloatValidator); break; case Integer: getEditbox()->setValidationString(IntegerValidator); break; case Hexadecimal: getEditbox()->setValidationString(HexValidator); break; case Octal: getEditbox()->setValidationString(OctalValidator); break; default: throw InvalidRequestException("Spinner::setTextInputMode - An unknown TextInputMode was specified."); }
d_inputMode = mode;
WindowEventArgs args(this); onTextInputModeChanged(args); } }
void Spinner::addSpinnerProperties(void) { addProperty(&d_currentValueProperty); addProperty(&d_stepSizeProperty); addProperty(&d_maxValueProperty); addProperty(&d_minValueProperty); addProperty(&d_textInputModeProperty); }
float Spinner::getValueFromText(void) const { String tmpTxt(getEditbox()->getText());
// handle empty and lone '-' or '.' cases if (tmpTxt.empty() || (tmpTxt == "-") || (tmpTxt == ".")) { return 0.0f; }
int res, tmp; uint utmp; float val;
switch (d_inputMode) { case FloatingPoint: res = sscanf(tmpTxt.c_str(), "%f", &val); break; case Integer: res = sscanf(tmpTxt.c_str(), "%d", &tmp); val = static_cast<float>(tmp); break; case Hexadecimal: res = sscanf(tmpTxt.c_str(), "%x", &utmp); val = static_cast<float>(utmp); break; case Octal: res = sscanf(tmpTxt.c_str(), "%o", &utmp); val = static_cast<float>(utmp); break; default: throw InvalidRequestException("Spinner::getValueFromText - An unknown TextInputMode was encountered."); }
if (res) { return val; }
throw InvalidRequestException("Spinner::getValueFromText - The string '" + getEditbox()->getText() + "' can not be converted to numerical representation."); }
String Spinner::getTextFromValue(void) const { std::stringstream tmp;
switch (d_inputMode) { case FloatingPoint: tmp << d_currentValue; break; case Integer: tmp << static_cast<int>(d_currentValue); break; case Hexadecimal: tmp << std::hex << std::uppercase << static_cast<int>(d_currentValue); break; case Octal: tmp << std::oct << static_cast<int>(d_currentValue); break; default: throw InvalidRequestException("Spinner::getValueFromText - An unknown TextInputMode was encountered."); }
return String(tmp.str()); }
void Spinner::onFontChanged(WindowEventArgs& e) { // Propagate to children getEditbox()->setFont(getFont()); // Call base class handler Window::onFontChanged(e); }
void Spinner::onTextChanged(WindowEventArgs& e) { Editbox* editbox = getEditbox();
// update only if needed if (editbox->getText() != d_text) { // done before doing base class processing so event subscribers see // 'updated' version. editbox->setText(d_text); e.handled = true;
Window::onTextChanged(e); } }
void Spinner::onActivated(ActivationEventArgs& e) { if (!isActive()) { Window::onActivated(e);
Editbox* editbox = getEditbox();
if (!editbox->isActive()) { editbox->activate(); } } }
void Spinner::onValueChanged(WindowEventArgs& e) { Editbox* editbox = getEditbox();
// mute to save doing unnecessary events work. bool wasMuted = editbox->isMuted(); editbox->setMutedState(true);
// Update text with new value. // (allow empty and '-' cases to equal 0 with no text change required) if (!(d_currentValue == 0 && (editbox->getText().empty() || editbox->getText() == "-"))) { editbox->setText(getTextFromValue()); } // restore previous mute state. editbox->setMutedState(wasMuted);
fireEvent(EventValueChanged, e, EventNamespace); }
void Spinner::onStepChanged(WindowEventArgs& e) { fireEvent(EventStepChanged, e, EventNamespace); }
void Spinner::onMaximumValueChanged(WindowEventArgs& e) { fireEvent(EventMaximumValueChanged, e, EventNamespace);
if (d_currentValue > d_maxValue) { setCurrentValue(d_maxValue); } }
void Spinner::onMinimumValueChanged(WindowEventArgs& e) { fireEvent(EventMinimumValueChanged, e, EventNamespace);
if (d_currentValue < d_minValue) { setCurrentValue(d_minValue); } }
void Spinner::onTextInputModeChanged(WindowEventArgs& e) { Editbox* editbox = getEditbox(); // update edit box text to reflect new mode. // mute to save doing unnecessary events work. bool wasMuted = editbox->isMuted(); editbox->setMutedState(true); // Update text with new value. editbox->setText(getTextFromValue()); // restore previous mute state. editbox->setMutedState(wasMuted);
fireEvent(EventTextInputModeChanged, e, EventNamespace); }
bool Spinner::handleIncreaseButton(const EventArgs& e) { if (((const MouseEventArgs&)e).button == LeftButton) { setCurrentValue(d_currentValue + d_stepSize); return true; } return false; }
bool Spinner::handleDecreaseButton(const EventArgs& e) { if (((const MouseEventArgs&)e).button == LeftButton) { setCurrentValue(d_currentValue - d_stepSize); return true; }
return false; }
bool Spinner::handleEditTextChange(const EventArgs& e) { // set this windows text to match setText(getEditbox()->getText()); // update value setCurrentValue(getValueFromText()); return true; }
PushButton* Spinner::getIncreaseButton() const { return static_cast<PushButton*>(WindowManager::getSingleton().getWindow( getName() + IncreaseButtonNameSuffix)); }
PushButton* Spinner::getDecreaseButton() const { return static_cast<PushButton*>(WindowManager::getSingleton().getWindow( getName() + DecreaseButtonNameSuffix)); }
Editbox* Spinner::getEditbox() const { return static_cast<Editbox*>(WindowManager::getSingleton().getWindow( getName() + EditboxNameSuffix)); }
} // End of CEGUI namespace section