2009-01-14 04:37:51 -08:00
|
|
|
// Copyright (C) 2006-2009 Michael Zeilfelder
|
2007-06-30 16:30:40 -07:00
|
|
|
// This file uses the licence of the Irrlicht Engine.
|
|
|
|
|
|
|
|
#include "CGUISpinBox.h"
|
2007-07-25 22:30:19 -07:00
|
|
|
#ifdef _IRR_COMPILE_WITH_GUI_
|
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
#include "CGUIEditBox.h"
|
|
|
|
#include "CGUIButton.h"
|
|
|
|
#include "IGUIEnvironment.h"
|
|
|
|
#include "IEventReceiver.h"
|
|
|
|
#include "fast_atof.h"
|
|
|
|
#include <float.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace gui
|
|
|
|
{
|
|
|
|
|
|
|
|
//! constructor
|
|
|
|
CGUISpinBox::CGUISpinBox(const wchar_t* text, IGUIEnvironment* environment,
|
|
|
|
IGUIElement* parent, s32 id, const core::rect<s32>& rectangle)
|
2007-07-01 03:16:39 -07:00
|
|
|
: IGUISpinBox(environment, parent, id, rectangle),
|
|
|
|
EditBox(0), ButtonSpinUp(0), ButtonSpinDown(0), StepSize(1.f),
|
|
|
|
RangeMin(-FLT_MAX), RangeMax(FLT_MAX), FormatString(L"%f"),
|
|
|
|
DecimalPlaces(-1)
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
|
|
|
s32 ButtonWidth = 16;
|
|
|
|
IGUISpriteBank *sb = 0;
|
|
|
|
if (environment && environment->getSkin())
|
|
|
|
{
|
|
|
|
ButtonWidth = environment->getSkin()->getSize(EGDS_SCROLLBAR_SIZE);
|
|
|
|
sb = environment->getSkin()->getSpriteBank();
|
|
|
|
}
|
|
|
|
|
2007-07-01 03:16:39 -07:00
|
|
|
ButtonSpinDown = Environment->addButton(
|
|
|
|
core::rect<s32>(rectangle.getWidth() - ButtonWidth, rectangle.getHeight()/2 +1,
|
2007-06-30 16:30:40 -07:00
|
|
|
rectangle.getWidth(), rectangle.getHeight()), this);
|
2007-07-01 03:16:39 -07:00
|
|
|
ButtonSpinDown->grab();
|
2007-06-30 16:30:40 -07:00
|
|
|
ButtonSpinDown->setSubElement(true);
|
2007-07-07 00:03:54 -07:00
|
|
|
ButtonSpinDown->setTabStop(false);
|
2007-06-30 16:30:40 -07:00
|
|
|
ButtonSpinDown->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_CENTER, EGUIA_LOWERRIGHT);
|
|
|
|
|
2007-07-01 03:16:39 -07:00
|
|
|
ButtonSpinUp = Environment->addButton(
|
|
|
|
core::rect<s32>(rectangle.getWidth() - ButtonWidth, 0,
|
2007-06-30 16:30:40 -07:00
|
|
|
rectangle.getWidth(), rectangle.getHeight()/2), this);
|
2007-07-01 03:16:39 -07:00
|
|
|
ButtonSpinUp->grab();
|
2007-06-30 16:30:40 -07:00
|
|
|
ButtonSpinUp->setSubElement(true);
|
2007-07-07 00:03:54 -07:00
|
|
|
ButtonSpinUp->setTabStop(false);
|
2007-06-30 16:30:40 -07:00
|
|
|
ButtonSpinUp->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_CENTER);
|
|
|
|
if (sb)
|
|
|
|
{
|
|
|
|
IGUISkin *skin = environment->getSkin();
|
|
|
|
ButtonSpinDown->setSpriteBank(sb);
|
|
|
|
ButtonSpinDown->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_SMALL_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL));
|
|
|
|
ButtonSpinDown->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_SMALL_CURSOR_DOWN), skin->getColor(EGDC_WINDOW_SYMBOL));
|
|
|
|
ButtonSpinUp->setSpriteBank(sb);
|
|
|
|
ButtonSpinUp->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_SMALL_CURSOR_UP), skin->getColor(EGDC_WINDOW_SYMBOL));
|
|
|
|
ButtonSpinUp->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_SMALL_CURSOR_UP), skin->getColor(EGDC_WINDOW_SYMBOL));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ButtonSpinDown->setText(L"-");
|
|
|
|
ButtonSpinUp->setText(L"+");
|
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
const core::rect<s32> rectEdit(0, 0, rectangle.getWidth() - ButtonWidth - 1, rectangle.getHeight());
|
2007-07-01 03:16:39 -07:00
|
|
|
EditBox = Environment->addEditBox(text, rectEdit, true, this, -1);
|
|
|
|
EditBox->grab();
|
2007-06-30 16:30:40 -07:00
|
|
|
EditBox->setSubElement(true);
|
|
|
|
EditBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
|
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
//! destructor
|
|
|
|
CGUISpinBox::~CGUISpinBox()
|
|
|
|
{
|
|
|
|
if (ButtonSpinUp)
|
|
|
|
ButtonSpinUp->drop();
|
2007-07-01 03:16:39 -07:00
|
|
|
if (ButtonSpinDown)
|
|
|
|
ButtonSpinDown->drop();
|
|
|
|
if (EditBox)
|
|
|
|
EditBox->drop();
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
|
|
|
IGUIEditBox* CGUISpinBox::getEditBox() const
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
return EditBox;
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
void CGUISpinBox::setValue(f32 val)
|
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
wchar_t str[100];
|
|
|
|
|
|
|
|
swprintf(str, 99, FormatString.c_str(), val);
|
|
|
|
EditBox->setText(str);
|
|
|
|
verifyValueRange();
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
|
|
|
f32 CGUISpinBox::getValue() const
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
const wchar_t* val = EditBox->getText();
|
|
|
|
if ( !val )
|
|
|
|
return 0.f;
|
2007-06-30 16:30:40 -07:00
|
|
|
core::stringc tmp(val);
|
2007-07-01 03:16:39 -07:00
|
|
|
return core::fast_atof(tmp.c_str());
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
void CGUISpinBox::setRange(f32 min, f32 max)
|
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
RangeMin = min;
|
|
|
|
RangeMax = max;
|
|
|
|
verifyValueRange();
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-09-14 15:25:59 -07:00
|
|
|
f32 CGUISpinBox::getMin() const
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
return RangeMin;
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-09-14 15:25:59 -07:00
|
|
|
f32 CGUISpinBox::getMax() const
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
return RangeMax;
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-09-14 15:25:59 -07:00
|
|
|
f32 CGUISpinBox::getStepSize() const
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
|
|
|
return StepSize;
|
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
void CGUISpinBox::setStepSize(f32 step)
|
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
StepSize = step;
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-07-01 03:16:39 -07:00
|
|
|
//! Sets the number of decimal places to display.
|
2007-06-30 16:30:40 -07:00
|
|
|
void CGUISpinBox::setDecimalPlaces(s32 places)
|
|
|
|
{
|
|
|
|
DecimalPlaces = places;
|
|
|
|
if (places == -1)
|
|
|
|
FormatString = "%f";
|
|
|
|
else
|
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
FormatString = "%.";
|
|
|
|
FormatString += places;
|
2007-06-30 16:30:40 -07:00
|
|
|
FormatString += "f";
|
|
|
|
}
|
|
|
|
setValue(getValue());
|
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-09-16 16:41:55 -07:00
|
|
|
bool CGUISpinBox::OnEvent(const SEvent& event)
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2008-07-08 12:47:51 -07:00
|
|
|
if (IsEnabled)
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2008-07-08 12:47:51 -07:00
|
|
|
bool changeEvent = false;
|
|
|
|
switch(event.EventType)
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2008-07-08 12:47:51 -07:00
|
|
|
case EET_GUI_EVENT:
|
|
|
|
if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
|
2007-07-01 03:16:39 -07:00
|
|
|
{
|
2008-07-08 12:47:51 -07:00
|
|
|
if (event.GUIEvent.Caller == ButtonSpinUp)
|
|
|
|
{
|
|
|
|
f32 val = getValue();
|
|
|
|
val += StepSize;
|
|
|
|
setValue(val);
|
|
|
|
changeEvent = true;
|
|
|
|
}
|
|
|
|
else if ( event.GUIEvent.Caller == ButtonSpinDown)
|
|
|
|
{
|
|
|
|
f32 val = getValue();
|
|
|
|
val -= StepSize;
|
|
|
|
setValue(val);
|
|
|
|
changeEvent = true;
|
|
|
|
}
|
2007-07-01 03:16:39 -07:00
|
|
|
}
|
2008-07-08 12:47:51 -07:00
|
|
|
if ( event.GUIEvent.EventType == EGET_EDITBOX_ENTER )
|
2007-07-01 03:16:39 -07:00
|
|
|
{
|
2008-07-08 12:47:51 -07:00
|
|
|
if (event.GUIEvent.Caller == EditBox)
|
|
|
|
{
|
|
|
|
verifyValueRange();
|
|
|
|
changeEvent = true;
|
|
|
|
}
|
2007-07-01 03:16:39 -07:00
|
|
|
}
|
2008-07-08 12:47:51 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
2008-07-08 12:47:51 -07:00
|
|
|
|
|
|
|
if ( changeEvent )
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2008-07-08 12:47:51 -07:00
|
|
|
SEvent e;
|
|
|
|
e.EventType = EET_GUI_EVENT;
|
|
|
|
e.GUIEvent.Caller = this;
|
|
|
|
e.GUIEvent.Element = 0;
|
|
|
|
|
|
|
|
e.GUIEvent.EventType = EGET_SPINBOX_CHANGED;
|
|
|
|
if ( Parent )
|
|
|
|
Parent->OnEvent(e);
|
|
|
|
return true;
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-07 00:03:54 -07:00
|
|
|
return IGUIElement::OnEvent(event);
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
void CGUISpinBox::verifyValueRange()
|
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
f32 val = getValue();
|
|
|
|
if ( val < RangeMin )
|
|
|
|
val = RangeMin;
|
|
|
|
else if ( val > RangeMax )
|
|
|
|
val = RangeMax;
|
|
|
|
else
|
|
|
|
return;
|
2007-06-30 16:30:40 -07:00
|
|
|
|
2007-07-01 03:16:39 -07:00
|
|
|
setValue(val);
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
//! Sets the new caption of the element
|
|
|
|
void CGUISpinBox::setText(const wchar_t* text)
|
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
EditBox->setText(text);
|
2007-06-30 16:30:40 -07:00
|
|
|
setValue(getValue());
|
|
|
|
verifyValueRange();
|
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
//! Returns caption of this element.
|
2007-09-14 15:25:59 -07:00
|
|
|
const wchar_t* CGUISpinBox::getText() const
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
2007-07-01 03:16:39 -07:00
|
|
|
return EditBox->getText();
|
2007-06-30 16:30:40 -07:00
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
//! Writes attributes of the element.
|
2007-09-14 15:25:59 -07:00
|
|
|
void CGUISpinBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
|
2007-06-30 16:30:40 -07:00
|
|
|
{
|
|
|
|
IGUIElement::serializeAttributes(out, options);
|
|
|
|
out->addFloat("Min", getMin());
|
|
|
|
out->addFloat("Max", getMax());
|
|
|
|
out->addFloat("Step", getStepSize());
|
|
|
|
out->addInt("DecimalPlaces", DecimalPlaces);
|
|
|
|
}
|
|
|
|
|
2009-01-22 04:40:06 -08:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
//! Reads attributes of the element
|
|
|
|
void CGUISpinBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
|
|
|
|
{
|
|
|
|
IGUIElement::deserializeAttributes(in, options);
|
|
|
|
setRange(in->getAttributeAsFloat("Min"), in->getAttributeAsFloat("Max"));
|
|
|
|
setStepSize(in->getAttributeAsFloat("Step"));
|
|
|
|
setDecimalPlaces(in->getAttributeAsInt("DecimalPlaces"));
|
|
|
|
}
|
|
|
|
|
2007-09-15 17:18:13 -07:00
|
|
|
|
2007-06-30 16:30:40 -07:00
|
|
|
} // end namespace gui
|
|
|
|
} // end namespace irr
|
2007-07-25 22:30:19 -07:00
|
|
|
|
|
|
|
#endif // _IRR_COMPILE_WITH_GUI_
|
2007-09-15 17:18:13 -07:00
|
|
|
|