diff options
Diffstat (limited to 'src/gui/widgets')
-rw-r--r-- | src/gui/widgets/setupitem.cpp | 161 | ||||
-rw-r--r-- | src/gui/widgets/setupitem.h | 51 |
2 files changed, 207 insertions, 5 deletions
diff --git a/src/gui/widgets/setupitem.cpp b/src/gui/widgets/setupitem.cpp index 1d6bf5916..581d9f239 100644 --- a/src/gui/widgets/setupitem.cpp +++ b/src/gui/widgets/setupitem.cpp @@ -25,6 +25,7 @@ #include "logger.h" #include "gui/editdialog.h" +#include "gui/gui.h" #include "gui/widgets/button.h" #include "gui/widgets/checkbox.h" @@ -40,7 +41,9 @@ #include "utils/dtor.h" #include "utils/gettext.h" +#include "utils/mathutils.h" +#include <guichan/font.hpp> SetupItem::SetupItem(std::string text, std::string description, std::string keyName, SetupTabScroll *parent, @@ -591,10 +594,6 @@ void SetupItemDropDown::toWidget() } - - - - SetupItemSlider::SetupItemSlider(std::string text, std::string description, std::string keyName, SetupTabScroll *parent, std::string eventName, double min, double max, @@ -686,3 +685,157 @@ void SetupItemSlider::apply(std::string eventName) fromWidget(); save(); } + +SetupItemSlider2::SetupItemSlider2(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, int min, int max, + SetupItemNames *values, bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, mainConfig), + mHorizont(nullptr), + mLabel(nullptr), + mLabel2(nullptr), + mSlider(nullptr), + mValues(values), + mMin(min), + mMax(max), + mInvert(false), + mInvertValue(0) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemSlider2::SetupItemSlider2(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, int min, int max, + SetupItemNames *values, std::string def, + bool mainConfig) : + SetupItem(text, description, keyName, parent, eventName, def, mainConfig), + mHorizont(nullptr), + mLabel(nullptr), + mLabel2(nullptr), + mSlider(nullptr), + mValues(values), + mMin(min), + mMax(max), + mInvert(false), + mInvertValue(0) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemSlider2::~SetupItemSlider2() +{ + mHorizont = nullptr; + mWidget = nullptr; + mSlider = nullptr; + mLabel = nullptr; +} + +void SetupItemSlider2::createControls() +{ + load(); + mHorizont = new HorizontContainer(32, 2); + + int width = getMaxWidth(); + + mLabel = new Label(mText); + mLabel2 = new Label(""); + mLabel2->setWidth(width); + mSlider = new Slider(mMin, mMax); + mSlider->setActionEventId(mEventName); + mSlider->addActionListener(mParent); + mSlider->setValue(atof(mValue.c_str())); + mSlider->setHeight(30); + + mWidget = mSlider; + mSlider->setWidth(150); + mSlider->setHeight(40); + mHorizont->add(mLabel); + mHorizont->add(mSlider, -10); + mHorizont->add(mLabel2); + + mParent->getContainer()->add2(mHorizont, true, 4); + mParent->addControl(this); + mParent->addActionListener(this); + mWidget->addActionListener(this); + updateLabel(); +} + +int SetupItemSlider2::getMaxWidth() +{ + if (!mValues || !gui) + return 1; + + int maxWidth = 0; + SetupItemNamesConstIter it = mValues->begin(); + SetupItemNamesConstIter it_end = mValues->end(); + gcn::Font *font = gui->getFont(); + + while (it != it_end) + { + int w = font->getWidth(*it); + if (w > maxWidth) + maxWidth = w; + + ++ it; + } + return maxWidth; +} + +void SetupItemSlider2::fromWidget() +{ + if (!mSlider) + return; + + int val = roundDouble(mSlider->getValue()); + if (mInvert) + val = mInvertValue - val; + mValue = toString(val); +} + +void SetupItemSlider2::toWidget() +{ + if (!mSlider) + return; + + int val = roundDouble(atof(mValue.c_str())); + if (mInvert) + val = mInvertValue - val; + mSlider->setValue(val); + updateLabel(); +} + +void SetupItemSlider2::action(const gcn::ActionEvent &event A_UNUSED) +{ + fromWidget(); + updateLabel(); +} + +void SetupItemSlider2::updateLabel() +{ + int val = mSlider->getValue() - mMin; + if (val < 0) + val = 0; + else if (val >= (signed)mValues->size()) + val = (signed)mValues->size() - 1; + std::string str = mValues->at(val); + mLabel2->setCaption(str); +} + +void SetupItemSlider2::apply(std::string eventName) +{ + if (eventName != mEventName) + return; + + fromWidget(); + save(); +} + +void SetupItemSlider2::setInvertValue(int v) +{ + mInvert = true; + mInvertValue = v; + toWidget(); +} diff --git a/src/gui/widgets/setupitem.h b/src/gui/widgets/setupitem.h index 220a55494..cf847f55d 100644 --- a/src/gui/widgets/setupitem.h +++ b/src/gui/widgets/setupitem.h @@ -92,7 +92,6 @@ class SetupItem : public gcn::ActionListener virtual void cancel(std::string eventName); virtual void externalUpdated(std::string eventName); -// virtual int add(ContainerPlacer &place, int x, int y, int width); bool isMainConfig() const { return mMainConfig; } @@ -290,6 +289,8 @@ class SetupItemSlider : public SetupItem void apply(std::string eventName); + void updateLabel(); + protected: HorizontContainer *mHorizont; Label *mLabel; @@ -298,4 +299,52 @@ class SetupItemSlider : public SetupItem double mMax; }; +typedef std::vector<std::string> SetupItemNames; +typedef SetupItemNames::iterator SetupItemNamesIter; +typedef SetupItemNames::const_iterator SetupItemNamesConstIter; + +class SetupItemSlider2 : public SetupItem +{ + public: + SetupItemSlider2(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, int min, int max, + SetupItemNames *values, bool mainConfig = true); + + SetupItemSlider2(std::string text, std::string description, + std::string keyName, SetupTabScroll *parent, + std::string eventName, int min, int max, + SetupItemNames *values, std::string def, + bool mainConfig = true); + + ~SetupItemSlider2(); + + void createControls(); + + void fromWidget(); + + void toWidget(); + + void action(const gcn::ActionEvent &event); + + void apply(std::string eventName); + + void setInvertValue(int v); + + protected: + void updateLabel(); + + int getMaxWidth(); + + HorizontContainer *mHorizont; + Label *mLabel; + Label *mLabel2; + Slider *mSlider; + SetupItemNames *mValues; + int mMin; + int mMax; + bool mInvert; + int mInvertValue; +}; + #endif |