Add NumericEditBox widget

deque
scrawl 10 years ago
parent e4c097b4f7
commit 0bc840aadd

@ -2,6 +2,8 @@
#include <boost/lexical_cast.hpp>
#include <components/widgets/numericeditbox.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
@ -19,7 +21,7 @@ namespace MWGui
mCancelButton->eventMouseButtonClick += MyGUI::newDelegate(this, &CountDialog::onCancelButtonClicked);
mOkButton->eventMouseButtonClick += MyGUI::newDelegate(this, &CountDialog::onOkButtonClicked);
mItemEdit->eventEditTextChange += MyGUI::newDelegate(this, &CountDialog::onEditTextChange);
mItemEdit->eventValueChanged += MyGUI::newDelegate(this, &CountDialog::onEditValueChanged);
mSlider->eventScrollChangePosition += MyGUI::newDelegate(this, &CountDialog::onSliderMoved);
// make sure we read the enter key being pressed to accept multiple items
mItemEdit->eventEditSelectAccept += MyGUI::newDelegate(this, &CountDialog::onEnterKeyPressed);
@ -46,7 +48,10 @@ namespace MWGui
MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mItemEdit);
mSlider->setScrollPosition(maxCount-1);
mItemEdit->setCaption(boost::lexical_cast<std::string>(maxCount));
mItemEdit->setMinValue(1);
mItemEdit->setMaxValue(maxCount);
mItemEdit->setValue(maxCount);
}
void CountDialog::cancel() //Keeping this here as I don't know if anything else relies on it.
@ -80,30 +85,13 @@ namespace MWGui
setVisible(false);
}
void CountDialog::onEditTextChange(MyGUI::EditBox* _sender)
void CountDialog::onEditValueChanged(int value)
{
if (_sender->getCaption() == "")
return;
unsigned int count;
try
{
count = boost::lexical_cast<unsigned int>(_sender->getCaption());
}
catch (std::bad_cast&)
{
count = 1;
}
if (count > mSlider->getScrollRange())
{
count = mSlider->getScrollRange();
}
mSlider->setScrollPosition(count-1);
onSliderMoved(mSlider, count-1);
mSlider->setScrollPosition(value-1);
}
void CountDialog::onSliderMoved(MyGUI::ScrollBar* _sender, size_t _position)
{
mItemEdit->setCaption(boost::lexical_cast<std::string>(_position+1));
mItemEdit->setValue(_position+1);
}
}

@ -3,6 +3,11 @@
#include "windowbase.hpp"
namespace Gui
{
class NumericEditBox;
}
namespace MWGui
{
class CountDialog : public WindowModal
@ -22,7 +27,7 @@ namespace MWGui
private:
MyGUI::ScrollBar* mSlider;
MyGUI::EditBox* mItemEdit;
Gui::NumericEditBox* mItemEdit;
MyGUI::TextBox* mItemText;
MyGUI::TextBox* mLabelText;
MyGUI::Button* mOkButton;
@ -30,7 +35,7 @@ namespace MWGui
void onCancelButtonClicked(MyGUI::Widget* _sender);
void onOkButtonClicked(MyGUI::Widget* _sender);
void onEditTextChange(MyGUI::EditBox* _sender);
void onEditValueChanged(int value);
void onSliderMoved(MyGUI::ScrollBar* _sender, size_t _position);
void onEnterKeyPressed(MyGUI::EditBox* _sender);
};

@ -2,6 +2,8 @@
#include <boost/lexical_cast.hpp>
#include <components/widgets/numericeditbox.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/soundmanager.hpp"
@ -86,7 +88,7 @@ namespace MWGui
mDecreaseButton->eventMouseButtonPressed += MyGUI::newDelegate(this, &TradeWindow::onDecreaseButtonPressed);
mDecreaseButton->eventMouseButtonReleased += MyGUI::newDelegate(this, &TradeWindow::onBalanceButtonReleased);
mTotalBalance->eventEditTextChange += MyGUI::newDelegate(this, &TradeWindow::onBalanceEdited);
mTotalBalance->eventValueChanged += MyGUI::newDelegate(this, &TradeWindow::onBalanceValueChanged);
setCoord(400, 0, 400, 300);
}
@ -433,21 +435,14 @@ namespace MWGui
MyGUI::ControllerManager::getInstance().removeItem(_sender);
}
void TradeWindow::onBalanceEdited(MyGUI::EditBox *_sender)
void TradeWindow::onBalanceValueChanged(int value)
{
try
{
unsigned int count = boost::lexical_cast<unsigned int>(_sender->getCaption());
mCurrentBalance = count * (mCurrentBalance >= 0 ? 1 : -1);
updateLabels();
}
catch (std::bad_cast&)
{
if (_sender->getCaption().empty())
mTotalBalance->setCaption("0");
else
mTotalBalance->setCaption(boost::lexical_cast<std::string>(std::abs(mCurrentBalance)));
}
// Entering a "-" sign inverts the buying/selling state
mCurrentBalance = (mCurrentBalance >= 0 ? 1 : -1) * value;
updateLabels();
if (value != std::abs(value))
mTotalBalance->setValue(std::abs(value));
}
void TradeWindow::onIncreaseButtonTriggered()
@ -471,19 +466,16 @@ namespace MWGui
mPlayerGold->setCaptionWithReplacing("#{sYourGold} " + boost::lexical_cast<std::string>(playerGold));
std::string balanceCaption;
if (mCurrentBalance > 0)
{
mTotalBalanceLabel->setCaptionWithReplacing("#{sTotalSold}");
balanceCaption = boost::lexical_cast<std::string>(mCurrentBalance);
}
else
{
mTotalBalanceLabel->setCaptionWithReplacing("#{sTotalCost}");
balanceCaption = boost::lexical_cast<std::string>(-mCurrentBalance);
}
if (balanceCaption != mTotalBalance->getCaption().asUTF8()) // Don't reset text cursor if text doesn't need to be changed
mTotalBalance->setCaption(balanceCaption);
mTotalBalance->setValue(std::abs(mCurrentBalance));
mMerchantGold->setCaptionWithReplacing("#{sSellerGold} " + boost::lexical_cast<std::string>(getMerchantGold()));
}

@ -3,10 +3,9 @@
#include "container.hpp"
namespace MyGUI
namespace Gui
{
class Gui;
class Widget;
class NumericEditBox;
}
namespace MWGui
@ -54,7 +53,7 @@ namespace MWGui
MyGUI::Button* mIncreaseButton;
MyGUI::Button* mDecreaseButton;
MyGUI::TextBox* mTotalBalanceLabel;
MyGUI::EditBox* mTotalBalance;
Gui::NumericEditBox* mTotalBalance;
MyGUI::Widget* mBottomPane;
@ -84,7 +83,7 @@ namespace MWGui
void onIncreaseButtonPressed(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id);
void onDecreaseButtonPressed(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id);
void onBalanceButtonReleased(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id);
void onBalanceEdited(MyGUI::EditBox* _sender);
void onBalanceValueChanged(int value);
void onRepeatClick(MyGUI::Widget* widget, MyGUI::ControllerItem* controller);
void addRepeatController(MyGUI::Widget* widget);

@ -18,7 +18,7 @@
#include <components/fontloader/fontloader.hpp>
#include <components/widgets/box.hpp>
#include <components/widgets/widgets.hpp>
#include <components/widgets/tags.hpp>
#include "../mwbase/inputmanager.hpp"
@ -165,13 +165,6 @@ namespace MWGui
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWEffectList>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWSpellEffect>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWDynamicStat>("Widget");
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<MWGui::ExposedWindow>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWScrollBar>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<VideoWidget>("Widget");
@ -179,6 +172,7 @@ namespace MWGui
BookPage::registerMyGUIComponents ();
ItemView::registerComponents();
ItemWidget::registerComponents();
Gui::registerAllWidgets();
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Controllers::ControllerRepeatEvent>("Controller");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Controllers::ControllerFollowMouse>("Controller");

@ -96,7 +96,7 @@ add_component_dir (ogreinit
)
add_component_dir (widgets
box imagebutton tags list
box imagebutton tags list numericeditbox widgets
)
add_component_dir (fontloader

@ -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

@ -12,7 +12,7 @@
<Property key="TextAlign" value="Right"/>
</Widget>
<Widget type="EditBox" skin="MW_TextEdit" position="520 30 52 24" name="ItemEdit" align="Right Top">
<Widget type="NumericEditBox" skin="MW_TextEdit" position="520 30 52 24" name="ItemEdit" align="Right Top">
<Property key="TextAlign" value="Center"/>
</Widget>

@ -44,7 +44,7 @@
</Widget>
<Widget type="TextBox" skin="SandText" position="48 0 140 24" name="TotalBalanceLabel" align="Left Top "/>
<Widget type="EditBox" skin="MW_TextEdit" position="48 28 140 24" name="TotalBalance" align="Left Top">
<Widget type="NumericEditBox" skin="MW_TextEdit" position="48 28 140 24" name="TotalBalance" align="Left Top">
<Property key="TextAlign" value="Center"/>
</Widget>

@ -11,10 +11,8 @@
#include <components/files/configurationmanager.hpp>
#include <components/fontloader/fontloader.hpp>
#include <components/widgets/imagebutton.hpp>
#include <components/widgets/box.hpp>
#include <components/widgets/tags.hpp>
#include <components/widgets/list.hpp>
#include <components/widgets/widgets.hpp>
#include <OgreTextureManager.h>
#include <OgreHardwarePixelBuffer.h>
@ -137,13 +135,7 @@ namespace MyGUIPlugin
{
MyGUI::FactoryManager::getInstance().registerFactory<MWScrollBar>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<Gui::ImageButton>("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::MWList>("Widget");
Gui::registerAllWidgets();
}
void ResourcePlugin::createTransparentBGTexture()

Loading…
Cancel
Save