From 92a66787367bd4816d8e2eb37138f3c3ba5e72f1 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Fri, 10 Jan 2014 16:01:28 -0600 Subject: [PATCH] Added DIMTIME slider to settings. Sliders now show their exact value + a unit suffix to the right of the slider. --- src/components/GuiSettingsMenu.cpp | 21 ++++++-- src/components/GuiSettingsMenu.h | 1 + src/components/SliderComponent.cpp | 57 ++++++++++++++++++--- src/components/SliderComponent.h | 15 +++++- src/views/gamelist/DetailedGameListView.cpp | 4 +- 5 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/components/GuiSettingsMenu.cpp b/src/components/GuiSettingsMenu.cpp index 0b2ea8feb..f2287b647 100644 --- a/src/components/GuiSettingsMenu.cpp +++ b/src/components/GuiSettingsMenu.cpp @@ -7,13 +7,14 @@ #include "../scrapers/GamesDBScraper.h" GuiSettingsMenu::GuiSettingsMenu(Window* window) : GuiComponent(window), - mList(window, Eigen::Vector2i(2, 6)), + mList(window, Eigen::Vector2i(2, 7)), mBox(mWindow, ":/frame.png", 0x444444FF), mDrawFramerateSwitch(window), - mVolumeSlider(window, 0, 100, 1), + mVolumeSlider(window, 0, 100, 1, "%"), mDisableSoundsSwitch(window, false), mScraperOptList(window), mScrapeRatingsSwitch(window), + mDimSlider(window, 0, 60, 1, "s"), mSaveButton(window) { loadStates(); @@ -80,10 +81,20 @@ GuiSettingsMenu::GuiSettingsMenu(Window* window) : GuiComponent(window), mList.setEntry(Vector2i(1, 4), Vector2i(1, 1), &mScrapeRatingsSwitch, true, ComponentListComponent::AlignCenter); + // dim time label + label = new TextComponent(mWindow); + label->setText("Dim after: "); + label->setColor(0x0000FFFF); + mLabels.push_back(label); + mList.setEntry(Vector2i(0, 5), Vector2i(1, 1), label, false, ComponentListComponent::AlignRight); + + // dim slider + mList.setEntry(Vector2i(1, 5), Vector2i(1, 1), &mDimSlider, true, ComponentListComponent::AlignCenter); + //save button mSaveButton.setText("SAVE", 0x00FF00FF); mSaveButton.setPressedFunc([this] () { applyStates(); delete this; }); - mList.setEntry(Vector2i(0, 5), Vector2i(2, 1), &mSaveButton, true, ComponentListComponent::AlignCenter, Matrix(false, true)); + mList.setEntry(Vector2i(0, 6), Vector2i(2, 1), &mSaveButton, true, ComponentListComponent::AlignCenter, Matrix(false, true)); //center list mList.setPosition(Renderer::getScreenWidth() / 2 - mList.getSize().x() / 2, Renderer::getScreenHeight() / 2 - mList.getSize().y() / 2); @@ -126,6 +137,8 @@ void GuiSettingsMenu::loadStates() mDisableSoundsSwitch.setState(s->getBool("DISABLESOUNDS")); mScrapeRatingsSwitch.setState(s->getBool("ScrapeRatings")); + + mDimSlider.setValue((float)(s->getInt("DIMTIME") / 1000)); } void GuiSettingsMenu::applyStates() @@ -142,5 +155,7 @@ void GuiSettingsMenu::applyStates() s->setBool("ScrapeRatings", mScrapeRatingsSwitch.getState()); + s->setInt("DIMTIME", (int)(mDimSlider.getValue() * 1000)); + s->saveFile(); } diff --git a/src/components/GuiSettingsMenu.h b/src/components/GuiSettingsMenu.h index a55258f90..bd0d726fe 100644 --- a/src/components/GuiSettingsMenu.h +++ b/src/components/GuiSettingsMenu.h @@ -33,6 +33,7 @@ private: SwitchComponent mDisableSoundsSwitch; OptionListComponent< std::shared_ptr > mScraperOptList; SwitchComponent mScrapeRatingsSwitch; + SliderComponent mDimSlider; ButtonComponent mSaveButton; std::vector mLabels; diff --git a/src/components/SliderComponent.cpp b/src/components/SliderComponent.cpp index 68a1e17b1..902ab17c2 100644 --- a/src/components/SliderComponent.cpp +++ b/src/components/SliderComponent.cpp @@ -1,9 +1,11 @@ #include "SliderComponent.h" #include #include "../Renderer.h" +#include "../resources/Font.h" +#include "../Log.h" -SliderComponent::SliderComponent(Window* window, float min, float max, float increment) : GuiComponent(window), - mMin(min), mMax(max), mIncrement(increment), mMoveRate(0), mRepeatWaitTimer(0) +SliderComponent::SliderComponent(Window* window, float min, float max, float increment, const std::string& suffix) : GuiComponent(window), + mMin(min), mMax(max), mIncrement(increment), mMoveRate(0), mRepeatWaitTimer(0), mSuffix(suffix) { assert((min - max) != 0); @@ -12,7 +14,7 @@ SliderComponent::SliderComponent(Window* window, float min, float max, float inc //calculate move scale mMoveScale = ((max - min) * 0.0007f) / increment; - setSize(128, 32); + setSize(196, 32); } bool SliderComponent::input(InputConfig* config, Input input) @@ -58,6 +60,8 @@ void SliderComponent::update(int deltaTime) if(mValue > mMax) mValue = mMax; + onValueChanged(); + if(mRepeatWaitTimer < 450) mRepeatWaitTimer += deltaTime; } @@ -70,30 +74,71 @@ void SliderComponent::render(const Eigen::Affine3f& parentTrans) Eigen::Affine3f trans = parentTrans * getTransform(); Renderer::setMatrix(trans); + float width = mSize.x() - (mValueCache ? mValueCache->metrics.size.x() + 4 : 0); + //render line const int lineWidth = 2; - Renderer::drawRect(0, (int)mSize.y() / 2 - lineWidth / 2, (int)mSize.x(), lineWidth, 0x000000CC); + Renderer::drawRect(0, (int)mSize.y() / 2 - lineWidth / 2, (int)width, lineWidth, 0x000000CC); //render left end const int capWidth = (int)(mSize.x() * 0.03f); Renderer::drawRect(0, 0, capWidth, (int)mSize.y(), 0x000000CC); //render right end - Renderer::drawRect((int)mSize.x() - capWidth, 0, capWidth, (int)mSize.y(), 0x000000CC); + Renderer::drawRect((int)width - capWidth, 0, capWidth, (int)mSize.y(), 0x000000CC); //render our value - const int lineLength = (int)mSize.x() - capWidth; + const int lineLength = (int)width - capWidth; Renderer::drawRect((int)(((mValue + mMin) / mMax) * lineLength), 0, capWidth, (int)mSize.y(), 0x0000FFFF); + // suffix + if(mValueCache) + mFont->renderTextCache(mValueCache.get()); + GuiComponent::renderChildren(trans); } void SliderComponent::setValue(float value) { mValue = value; + onValueChanged(); } float SliderComponent::getValue() { return mValue; } + +void SliderComponent::onSizeChanged() +{ + if(!mSuffix.empty()) + { + mFont = Font::get((int)(mSize.y() * 0.7f)); + onValueChanged(); + } +} + +void SliderComponent::onValueChanged() +{ + if(mFont) + { + std::stringstream ss; + ss << std::fixed; + ss.precision(0); + ss << mValue; + ss << mSuffix; + const std::string val = ss.str(); + + ss.str(""); + ss.clear(); + ss << std::fixed; + ss.precision(0); + ss << mMax; + ss << mSuffix; + const std::string max = ss.str(); + + float w = mFont->sizeText(max).x(); + mValueCache = std::shared_ptr(mFont->buildTextCache(val, mSize.x() - w, 0, 0x000000FF)); + mValueCache->metrics.size[0] = w; // fudge the width + } +} diff --git a/src/components/SliderComponent.h b/src/components/SliderComponent.h index 2cfb55e42..d39ef6cbf 100644 --- a/src/components/SliderComponent.h +++ b/src/components/SliderComponent.h @@ -2,11 +2,14 @@ #include "../GuiComponent.h" +class TextCache; +class Font; + class SliderComponent : public GuiComponent { public: - //Minimum value (far left of the slider), maximum value (far right of the slider), increment size (how much just pressing L/R moves by). - SliderComponent(Window* window, float min, float max, float increment); + //Minimum value (far left of the slider), maximum value (far right of the slider), increment size (how much just pressing L/R moves by), unit to display (optional). + SliderComponent(Window* window, float min, float max, float increment, const std::string& suffix = ""); void setValue(float val); float getValue(); @@ -15,12 +18,20 @@ public: void update(int deltaTime) override; void render(const Eigen::Affine3f& parentTrans) override; + void onSizeChanged() override; + private: + void onValueChanged(); + float mMin, mMax; float mValue; float mIncrement; float mMoveScale; int mRepeatWaitTimer; + std::string mSuffix; + std::shared_ptr mFont; + std::shared_ptr mValueCache; + float mMoveRate; }; diff --git a/src/views/gamelist/DetailedGameListView.cpp b/src/views/gamelist/DetailedGameListView.cpp index f905fcc00..7276c35dd 100644 --- a/src/views/gamelist/DetailedGameListView.cpp +++ b/src/views/gamelist/DetailedGameListView.cpp @@ -38,8 +38,8 @@ void DetailedGameListView::onThemeChanged(const std::shared_ptr& them mHeaderImage.setResize(mSize.x() * 0.5f, 0, true); BasicGameListView::onThemeChanged(theme); - if(mHeaderImage.getPosition().y() + mHeaderImage.getSize().y() > mImage.getPosition().y()) - mHeaderImage.setResize(0, mSize.y() * 0.185f, true); + //if(mHeaderImage.getPosition().y() + mHeaderImage.getSize().y() > mImage.getPosition().y()) + // mHeaderImage.setResize(0, mSize.y() * 0.185f, true); using namespace ThemeFlags; mImage.applyTheme(theme, getName(), "gameimage", POSITION | ThemeFlags::SIZE);