From 2affb8ed6386b2f714cb340f0edac69f91fb4d09 Mon Sep 17 00:00:00 2001 From: capostrophic Date: Wed, 31 Jul 2019 15:41:32 +0300 Subject: [PATCH 1/2] Human-readable light source duration (feature #5091) --- CHANGELOG.md | 1 + apps/openmw/mwclass/light.cpp | 10 ++---- apps/openmw/mwgui/spellicons.cpp | 23 ++----------- apps/openmw/mwgui/tooltips.cpp | 57 ++++++++++++++++++++++++++++++++ apps/openmw/mwgui/tooltips.hpp | 3 ++ 5 files changed, 66 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c481994ea..25a518c87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -149,6 +149,7 @@ Feature #5036: Allow scripted faction leaving Feature #5046: Gamepad thumbstick cursor speed Feature #5051: Provide a separate textures for scrollbars + Feature #5091: Human-readable light source duration Feature #5094: Unix like console hotkeys Task #4686: Upgrade media decoder to a more current FFmpeg API Task #4695: Optimize Distant Terrain memory consumption diff --git a/apps/openmw/mwclass/light.cpp b/apps/openmw/mwclass/light.cpp index 38b7137f3..fb4cae63b 100644 --- a/apps/openmw/mwclass/light.cpp +++ b/apps/openmw/mwclass/light.cpp @@ -156,13 +156,9 @@ namespace MWClass std::string text; - if (Settings::Manager::getBool("show effect duration","Game")) - { - // -1 is infinite light source, so duration makes no sense here. Other negative values are treated as 0. - float remainingTime = ptr.getClass().getRemainingUsageTime(ptr); - if (remainingTime != -1.0f) - text += "\n#{sDuration}: " + MWGui::ToolTips::toString(std::max(0.f, remainingTime)); - } + // Don't show duration for infinite light sources. + if (Settings::Manager::getBool("show effect duration","Game") && ptr.getClass().getRemainingUsageTime(ptr) != -1) + text += MWGui::ToolTips::getDurationString(ptr.getClass().getRemainingUsageTime(ptr), "\n#{sDuration}"); text += MWGui::ToolTips::getWeightString(ref->mBase->mData.mWeight, "#{sWeight}"); text += MWGui::ToolTips::getValueString(ref->mBase->mData.mValue, "#{sValue}"); diff --git a/apps/openmw/mwgui/spellicons.cpp b/apps/openmw/mwgui/spellicons.cpp index 51508cd06..8a501e598 100644 --- a/apps/openmw/mwgui/spellicons.cpp +++ b/apps/openmw/mwgui/spellicons.cpp @@ -137,27 +137,8 @@ namespace MWGui MWBase::Environment::get().getWindowManager()->getGameSettingString("spoint", "") ); } } - if (effectInfo.mRemainingTime > -1 && - Settings::Manager::getBool("show effect duration","Game")) { - sourcesDescription += " #{sDuration}: "; - float duration = effectInfo.mRemainingTime; - if (duration > 3600) - { - int hour = duration / 3600; - duration -= hour*3600; - sourcesDescription += MWGui::ToolTips::toString(hour) + "h"; - } - if (duration > 60) - { - int minute = duration / 60; - duration -= minute*60; - sourcesDescription += MWGui::ToolTips::toString(minute) + "m"; - } - if (duration > 0.1) - { - sourcesDescription += MWGui::ToolTips::toString(duration) + "s"; - } - } + if (effectInfo.mRemainingTime > -1 && Settings::Manager::getBool("show effect duration","Game")) + sourcesDescription += MWGui::ToolTips::getDurationString(effectInfo.mRemainingTime, " #{sDuration}"); addNewLine = true; } diff --git a/apps/openmw/mwgui/tooltips.cpp b/apps/openmw/mwgui/tooltips.cpp index dc7a9668d..6b3f8cac6 100644 --- a/apps/openmw/mwgui/tooltips.cpp +++ b/apps/openmw/mwgui/tooltips.cpp @@ -667,6 +667,63 @@ namespace MWGui return ret; } + std::string ToolTips::getDurationString(float duration, const std::string& prefix) + { + std::string ret; + ret = prefix + ": "; + + if (duration < 1.f) + { + ret += "0 s"; + return ret; + } + + constexpr float secondsPerMinute = 60.f; // 60 seconds + constexpr float secondsPerHour = secondsPerMinute * 60.f; // 60 minutes + constexpr float secondsPerDay = secondsPerHour * 24.f; // 24 hours + constexpr float secondsPerMonth = secondsPerDay * 30.f; // 30 days + constexpr float secondsPerYear = secondsPerDay * 365.f; + int units = 0; + if (duration >= secondsPerYear) + { + units++; + int years = duration / secondsPerYear; + duration -= years * secondsPerYear; + ret += toString(years) + " y "; + } + if (duration >= secondsPerMonth) + { + units++; + int months = duration / secondsPerMonth; + duration -= months * secondsPerMonth; + ret += toString(months) + " mo "; + } + if (units < 2 && duration >= secondsPerDay) + { + units++; + int days = duration / secondsPerDay; + duration -= days * secondsPerDay; + ret += toString(days) + " d "; + } + if (units < 2 && duration >= secondsPerHour) + { + units++; + int hours = duration / secondsPerHour; + duration -= hours * secondsPerHour; + ret += toString(hours) + " h "; + } + if (units < 2 && duration >= secondsPerMinute) + { + int minutes = duration / secondsPerMinute; + duration -= minutes * secondsPerMinute; + ret += toString(minutes) + " min "; + } + if (units < 2 && duration >= 1.f) + ret += toString(int(duration)) + " s "; + + return ret; + } + bool ToolTips::toggleFullHelp() { mFullHelp = !mFullHelp; diff --git a/apps/openmw/mwgui/tooltips.hpp b/apps/openmw/mwgui/tooltips.hpp index 43187dc5c..afdc7dec0 100644 --- a/apps/openmw/mwgui/tooltips.hpp +++ b/apps/openmw/mwgui/tooltips.hpp @@ -84,6 +84,9 @@ namespace MWGui static std::string getCellRefString(const MWWorld::CellRef& cellref); ///< Returns a string containing debug tooltip information about the given cellref. + static std::string getDurationString (float duration, const std::string& prefix); + ///< Returns duration as two largest time units, rounded down. Note: not localized; no line break. + // these do not create an actual tooltip, but they fill in the data that is required so the tooltip // system knows what to show in case this widget is hovered static void createSkillToolTip(MyGUI::Widget* widget, int skillId); From 26f59e47db5702077b3bddeb34f9b83d05c2d08e Mon Sep 17 00:00:00 2001 From: capostrophic Date: Fri, 9 Aug 2019 19:56:21 +0300 Subject: [PATCH 2/2] Improve readability --- apps/openmw/mwgui/tooltips.cpp | 45 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/apps/openmw/mwgui/tooltips.cpp b/apps/openmw/mwgui/tooltips.cpp index 6b3f8cac6..7d8a07c21 100644 --- a/apps/openmw/mwgui/tooltips.cpp +++ b/apps/openmw/mwgui/tooltips.cpp @@ -678,48 +678,45 @@ namespace MWGui return ret; } - constexpr float secondsPerMinute = 60.f; // 60 seconds - constexpr float secondsPerHour = secondsPerMinute * 60.f; // 60 minutes - constexpr float secondsPerDay = secondsPerHour * 24.f; // 24 hours - constexpr float secondsPerMonth = secondsPerDay * 30.f; // 30 days - constexpr float secondsPerYear = secondsPerDay * 365.f; + constexpr int secondsPerMinute = 60; // 60 seconds + constexpr int secondsPerHour = secondsPerMinute * 60; // 60 minutes + constexpr int secondsPerDay = secondsPerHour * 24; // 24 hours + constexpr int secondsPerMonth = secondsPerDay * 30; // 30 days + constexpr int secondsPerYear = secondsPerDay * 365; + int fullDuration = static_cast(duration); int units = 0; - if (duration >= secondsPerYear) + int years = fullDuration / secondsPerYear; + int months = fullDuration % secondsPerYear / secondsPerMonth; + int days = fullDuration % secondsPerYear % secondsPerMonth / secondsPerDay; // Because a year is not exactly 12 "months" + int hours = fullDuration % secondsPerDay / secondsPerHour; + int minutes = fullDuration % secondsPerHour / secondsPerMinute; + int seconds = fullDuration % secondsPerMinute; + if (years) { units++; - int years = duration / secondsPerYear; - duration -= years * secondsPerYear; ret += toString(years) + " y "; } - if (duration >= secondsPerMonth) + if (months) { units++; - int months = duration / secondsPerMonth; - duration -= months * secondsPerMonth; ret += toString(months) + " mo "; } - if (units < 2 && duration >= secondsPerDay) + if (units < 2 && days) { units++; - int days = duration / secondsPerDay; - duration -= days * secondsPerDay; ret += toString(days) + " d "; } - if (units < 2 && duration >= secondsPerHour) + if (units < 2 && hours) { units++; - int hours = duration / secondsPerHour; - duration -= hours * secondsPerHour; ret += toString(hours) + " h "; } - if (units < 2 && duration >= secondsPerMinute) - { - int minutes = duration / secondsPerMinute; - duration -= minutes * secondsPerMinute; + if (units >= 2) + return ret; + if (minutes) ret += toString(minutes) + " min "; - } - if (units < 2 && duration >= 1.f) - ret += toString(int(duration)) + " s "; + if (seconds) + ret += toString(seconds) + " s "; return ret; }