Files
OpenSpace/modules/base/rendering/screenspacedate.cpp
Alexander Bock 32cff07bf8 New ScreenSpaceText objects (#3794)
* First version of new screenspacetext and screenspacetextdate
* Code restructuring and adding new types
 - ScreenSpaceFramebuffer -> ScreenSpaceRenderableFramebuffer
 - ScreenSpaceText -> ScreenSpaceRenderableText
 - Add new user-facing ScreenSpaceText
 - Add examples
* Add documentation for new ScreenSpace objects
* Add visual tests to new screenspace objects
* Address PR comments
2025-09-26 15:35:05 +02:00

109 lines
5.1 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/rendering/screenspacedate.h>
#include <openspace/util/spicemanager.h>
#include <openspace/util/time.h>
#include <openspace/util/timemanager.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/opengl/framebufferobject.h>
#include <ghoul/opengl/texture.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 date "
"in the format as specified by `TimeFormat`.",
openspace::properties::Property::Visibility::AdvancedUser
};
constexpr openspace::properties::Property::PropertyInfo TimeFormatInfo = {
"TimeFormat",
"Time format",
"The format string used for formatting the date/time before being passed to the "
"string in FormatString. See "
"https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/timout_c.html for full "
"information about how to structure this format.",
openspace::properties::Property::Visibility::User
};
// This `ScreenSpaceRenderable` shows the current in-game simulation time. The
// `FormatString` and the `TimeFormat` options provide the ability to customize the
// output that is printed. See these two parameters for more information on how to
// structure the inputs.
struct [[codegen::Dictionary(ScreenSpaceTextDate)]] Parameters {
// [[codegen::verbatim(FormatStringInfo.description)]]
std::optional<std::string> formatString;
// [[codegen::verbatim(TimeFormatInfo.description)]]
std::optional<std::string> timeFormat;
};
#include "screenspacedate_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation ScreenSpaceDate::Documentation() {
return codegen::doc<Parameters>(
"base_screenspace_date",
ScreenSpaceRenderableText::Documentation()
);
}
ScreenSpaceDate::ScreenSpaceDate(const ghoul::Dictionary& dictionary)
: ScreenSpaceRenderableText(dictionary)
, _formatString(FormatStringInfo, "Date: {}")
, _timeFormat(TimeFormatInfo, "YYYY MON DD HR:MN:SC.### UTC ::RND")
{
const Parameters p = codegen::bake<Parameters>(dictionary);
_formatString = p.formatString.value_or(_formatString);
addProperty(_formatString);
_timeFormat = p.timeFormat.value_or(_timeFormat);
addProperty(_timeFormat);
}
void ScreenSpaceDate::update() {
std::string time = SpiceManager::ref().dateFromEphemerisTime(
global::timeManager->time().j2000Seconds(),
_timeFormat.value().c_str()
);
try {
// @CPP26(abock): This can be replaced with std::runtime_format
_buffer = std::vformat(_formatString.value(), std::make_format_args(time));
}
catch (const std::format_error&) {
LERRORC("ScreenSpaceDate", "Illegal format string");
}
ScreenSpaceRenderableText::update();
}
} // namespace openspace