Add NumericEditBox widget
parent
e4c097b4f7
commit
0bc840aadd
@ -0,0 +1,74 @@
|
||||
#include "numericeditbox.hpp"
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
namespace Gui
|
||||
{
|
||||
|
||||
void NumericEditBox::initialiseOverride()
|
||||
{
|
||||
Base::initialiseOverride();
|
||||
eventEditTextChange += MyGUI::newDelegate(this, &NumericEditBox::onEditTextChange);
|
||||
|
||||
mValue = 0;
|
||||
setCaption("0");
|
||||
}
|
||||
|
||||
void NumericEditBox::shutdownOverride()
|
||||
{
|
||||
Base::shutdownOverride();
|
||||
eventEditTextChange -= MyGUI::newDelegate(this, &NumericEditBox::onEditTextChange);
|
||||
}
|
||||
|
||||
void NumericEditBox::onEditTextChange(MyGUI::EditBox *sender)
|
||||
{
|
||||
std::string newCaption = sender->getCaption();
|
||||
if (newCaption.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
mValue = boost::lexical_cast<int>(newCaption);
|
||||
int capped = std::min(mMaxValue, std::max(mValue, mMinValue));
|
||||
if (capped != mValue)
|
||||
{
|
||||
mValue = capped;
|
||||
setCaption(MyGUI::utility::toString(mValue));
|
||||
}
|
||||
}
|
||||
catch (boost::bad_lexical_cast&)
|
||||
{
|
||||
setCaption(MyGUI::utility::toString(mValue));
|
||||
}
|
||||
|
||||
eventValueChanged(mValue);
|
||||
}
|
||||
|
||||
void NumericEditBox::setValue(int value)
|
||||
{
|
||||
if (value != mValue)
|
||||
{
|
||||
setCaption(MyGUI::utility::toString(value));
|
||||
mValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
void NumericEditBox::setMinValue(int minValue)
|
||||
{
|
||||
mMinValue = minValue;
|
||||
}
|
||||
|
||||
void NumericEditBox::setMaxValue(int maxValue)
|
||||
{
|
||||
mMaxValue = maxValue;
|
||||
}
|
||||
|
||||
void NumericEditBox::onKeyLostFocus(MyGUI::Widget* _new)
|
||||
{
|
||||
Base::onKeyLostFocus(_new);
|
||||
setCaption(MyGUI::utility::toString(mValue));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#ifndef OPENMW_NUMERIC_EDIT_BOX_H
|
||||
#define OPENMW_NUMERIC_EDIT_BOX_H
|
||||
|
||||
#include <MyGUI_EditBox.h>
|
||||
|
||||
namespace Gui
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief A variant of the EditBox that only allows integer inputs
|
||||
*/
|
||||
class NumericEditBox : public MyGUI::EditBox
|
||||
{
|
||||
MYGUI_RTTI_DERIVED(NumericEditBox)
|
||||
|
||||
public:
|
||||
NumericEditBox()
|
||||
: mValue(0), mMinValue(std::numeric_limits<int>().min()),
|
||||
mMaxValue(std::numeric_limits<int>().max())
|
||||
{}
|
||||
|
||||
void initialiseOverride();
|
||||
void shutdownOverride();
|
||||
|
||||
typedef MyGUI::delegates::CMultiDelegate1<int> EventHandle_ValueChanged;
|
||||
EventHandle_ValueChanged eventValueChanged;
|
||||
|
||||
/// @note Does not trigger eventValueChanged
|
||||
void setValue (int value);
|
||||
|
||||
void setMinValue(int minValue);
|
||||
void setMaxValue(int maxValue);
|
||||
private:
|
||||
void onEditTextChange(MyGUI::EditBox* sender);
|
||||
void onKeyLostFocus(MyGUI::Widget* _new);
|
||||
|
||||
int mValue;
|
||||
|
||||
int mMinValue;
|
||||
int mMaxValue;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,25 @@
|
||||
#include "widgets.hpp"
|
||||
|
||||
#include <MyGUI_FactoryManager.h>
|
||||
|
||||
#include "list.hpp"
|
||||
#include "numericeditbox.hpp"
|
||||
#include "box.hpp"
|
||||
#include "imagebutton.hpp"
|
||||
|
||||
namespace Gui
|
||||
{
|
||||
|
||||
void registerAllWidgets()
|
||||
{
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::MWList>("Widget");
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::HBox>("Widget");
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::VBox>("Widget");
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::AutoSizedTextBox>("Widget");
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::AutoSizedEditBox>("Widget");
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::AutoSizedButton>("Widget");
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::ImageButton>("Widget");
|
||||
MyGUI::FactoryManager::getInstance().registerFactory<Gui::NumericEditBox>("Widget");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
#ifndef OPENMW_COMPONENTS_WIDGETS_H
|
||||
#define OPENMW_COMPONENTS_WIDGETS_H
|
||||
|
||||
namespace Gui
|
||||
{
|
||||
|
||||
/// Register all widgets from this component with MyGUI's factory manager.
|
||||
void registerAllWidgets();
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue