mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-02 01:30:34 -06:00
179 lines
7.7 KiB
C++
179 lines
7.7 KiB
C++
/*****************************************************************************************
|
|
* *
|
|
* OpenSpace *
|
|
* *
|
|
* Copyright (c) 2014-2025 *
|
|
* *
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
|
* software and associated documentation files (the "Software"), to deal in the Software *
|
|
* without restriction, including without limitation the rights to use, copy, modify, *
|
|
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
|
* permit persons to whom the Software is furnished to do so, subject to the following *
|
|
* conditions: *
|
|
* *
|
|
* The above copyright notice and this permission notice shall be included in all copies *
|
|
* or substantial portions of the Software. *
|
|
* *
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
|
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
|
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
|
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
|
****************************************************************************************/
|
|
|
|
#include <modules/base/dashboard/dashboarditemelapsedtime.h>
|
|
|
|
#include <openspace/documentation/documentation.h>
|
|
#include <openspace/documentation/verifier.h>
|
|
#include <openspace/engine/globals.h>
|
|
#include <openspace/util/spicemanager.h>
|
|
#include <openspace/util/timeconversion.h>
|
|
#include <openspace/util/timemanager.h>
|
|
#include <openspace/util/time.h>
|
|
#include <ghoul/font/font.h>
|
|
#include <ghoul/font/fontmanager.h>
|
|
#include <ghoul/font/fontrenderer.h>
|
|
|
|
namespace {
|
|
constexpr openspace::properties::Property::PropertyInfo FormatStringInfo = {
|
|
"FormatString",
|
|
"Format string",
|
|
"The format text describing how this dashboard item renders its text. This text "
|
|
"must contain exactly one {} which is a placeholder that will contain the value "
|
|
"of the elapsed time.",
|
|
openspace::properties::Property::Visibility::AdvancedUser
|
|
};
|
|
|
|
constexpr openspace::properties::Property::PropertyInfo ReferenceTimeInfo = {
|
|
"ReferenceTime",
|
|
"Reference time",
|
|
"The reference time relative to which the elapsed time is specified. The format "
|
|
"must be an ISO 8601-compliant date string.",
|
|
openspace::properties::Property::Visibility::User
|
|
};
|
|
|
|
constexpr openspace::properties::Property::PropertyInfo SimplifyTimeInfo = {
|
|
"SimplifyTime",
|
|
"Simplify time",
|
|
"If this value is enabled, the elapsed time will be simplified into seconds, "
|
|
"minutes, hours, etc. If the value is disabled, the elapsed time is always "
|
|
"presented in seconds. The default value for this is 'true'.",
|
|
openspace::properties::Property::Visibility::User
|
|
};
|
|
|
|
constexpr openspace::properties::Property::PropertyInfo LowestTimeUnitInfo = {
|
|
"LowestTimeUnit",
|
|
"Lowest time unit when simplifying",
|
|
"If 'SimplifyTime' is enabled, this is the lowest time unit that will be shown. "
|
|
"All finer grained timesteps will be ignored.",
|
|
openspace::properties::Property::Visibility::User
|
|
};
|
|
|
|
// This `DashboardItem` displays the remaining time until a provided `ReferenceTime`
|
|
// or the elapsed time since the `ReferenceTime`. The output can be configured through
|
|
// the `FormatString` and the unit that is used to display the configurable as well.
|
|
struct [[codegen::Dictionary(DashboardItemElapsedTime)]] Parameters {
|
|
// [[codegen::verbatim(FormatStringInfo.description)]]
|
|
std::optional<std::string> formatString;
|
|
|
|
// [[codegen::verbatim(ReferenceTimeInfo.description)]]
|
|
std::string referenceTime [[codegen::datetime()]];
|
|
|
|
// [[codegen::verbatim(SimplifyTimeInfo.description)]]
|
|
std::optional<bool> simplifyTime;
|
|
|
|
enum class [[codegen::map(openspace::TimeUnit)]] TimeUnit {
|
|
Nanosecond,
|
|
Microsecond,
|
|
Millisecond,
|
|
Second,
|
|
Minute,
|
|
Hour,
|
|
Day,
|
|
Month,
|
|
Year
|
|
};
|
|
|
|
// [[codegen::verbatim(LowestTimeUnitInfo.description)]]
|
|
std::optional<TimeUnit> lowestTimeUnit;
|
|
};
|
|
#include "dashboarditemelapsedtime_codegen.cpp"
|
|
} // namespace
|
|
|
|
namespace openspace {
|
|
|
|
documentation::Documentation DashboardItemElapsedTime::Documentation() {
|
|
return codegen::doc<Parameters>(
|
|
"base_dashboarditem_elapsedtime",
|
|
DashboardTextItem::Documentation()
|
|
);
|
|
}
|
|
|
|
DashboardItemElapsedTime::DashboardItemElapsedTime(const ghoul::Dictionary& dictionary)
|
|
: DashboardTextItem(dictionary)
|
|
, _formatString(FormatStringInfo, "Elapsed time: {}")
|
|
, _referenceTime(ReferenceTimeInfo)
|
|
, _simplifyTime(SimplifyTimeInfo, true)
|
|
, _lowestTimeUnit(LowestTimeUnitInfo)
|
|
{
|
|
const Parameters p = codegen::bake<Parameters>(dictionary);
|
|
|
|
_formatString = p.formatString.value_or(_formatString);
|
|
addProperty(_formatString);
|
|
|
|
_referenceTime.onChange([this]() {
|
|
_referenceJ2000 = Time::convertTime(_referenceTime);
|
|
});
|
|
_referenceTime = p.referenceTime;
|
|
addProperty(_referenceTime);
|
|
|
|
_simplifyTime = p.simplifyTime.value_or(_simplifyTime);
|
|
addProperty(_simplifyTime);
|
|
|
|
for (const TimeUnit u : TimeUnits) {
|
|
_lowestTimeUnit.addOption(static_cast<int>(u), std::string(nameForTimeUnit(u)));
|
|
}
|
|
_lowestTimeUnit = static_cast<int>(TimeUnit::Second);
|
|
const TimeUnit u = codegen::map<TimeUnit>(
|
|
p.lowestTimeUnit.value_or(Parameters::TimeUnit::Second)
|
|
);
|
|
_lowestTimeUnit = static_cast<int>(u);
|
|
addProperty(_lowestTimeUnit);
|
|
}
|
|
|
|
void DashboardItemElapsedTime::update() {
|
|
ZoneScoped;
|
|
|
|
const double delta = global::timeManager->time().j2000Seconds() - _referenceJ2000;
|
|
|
|
if (_simplifyTime) {
|
|
using namespace std::chrono;
|
|
|
|
const TimeUnit lowestTime = TimeUnit(_lowestTimeUnit.value());
|
|
|
|
const std::vector<std::pair<double, std::string_view>> ts = splitTime(delta);
|
|
std::string time;
|
|
for (const std::pair<double, std::string_view>& t : ts) {
|
|
if (timeUnitFromString(t.second) < lowestTime) {
|
|
// We have reached the lowest unit the user was interested in
|
|
break;
|
|
}
|
|
time += std::format("{} {} ", t.first, t.second);
|
|
}
|
|
|
|
// Remove the " " at the end
|
|
time = time.substr(0, time.size() - 1);
|
|
|
|
// @CPP26(abock): This can be replaced with std::runtime_format
|
|
_buffer = std::vformat(_formatString.value(), std::make_format_args(time));
|
|
}
|
|
else {
|
|
std::string time = std::format("{} s", delta);
|
|
// @CPP26(abock): This can be replaced with std::runtime_format
|
|
_buffer = std::vformat(_formatString.value(), std::make_format_args(time));
|
|
}
|
|
}
|
|
|
|
} // namespace openspace
|