Program Listing for File TimeDisplay.cc

Program Listing for File TimeDisplay.cc#

Return to documentation for file (doxygen_docs/GeneralKModels/TimeDisplay.cc)

#include <format>

#include "Karana/GeneralKModels/TimeDisplay.h"
#include "Karana/KCore/Allocator.h"

/**
 * @file
 * @brief TimeDisplay implementation.
 */

namespace Karana::Models {

    namespace kc = Karana::Core;
    namespace kd = Karana::Dynamics;
    namespace ks = Karana::Scene;
    namespace km = Karana::Math;

    TimeDisplayParams::TimeDisplayParams(std::string_view name)
        : KModelParams(name) {}

    bool TimeDisplayParams::isReady() const { return true; }

    TimeDisplay::TimeDisplay(std::string_view name,
                             const kc::ks_ptr<kd::ModelManager> &mm,
                             const kc::ks_ptr<ks::GraphicalScene> &scene)
        : KModel<TimeDisplay, TimeDisplayParams>(name, mm)
        , _scene(scene) {
        params = std::allocate_shared<TimeDisplayParams>(kc::Allocator<TimeDisplayParams>{},
                                                         std::format("{}_params", name));
    };

    kc::ks_ptr<TimeDisplay> TimeDisplay::create(std::string_view name,
                                                const kc::ks_ptr<kd::ModelManager> &mm,
                                                const kc::ks_ptr<ks::GraphicalScene> &scene) {
        kc::ks_ptr<TimeDisplay> model =
            std::allocate_shared<TimeDisplay>(kc::Allocator<TimeDisplay>{}, name, mm, scene);
        mm->registerModel(model);
        return model;
    }

    void TimeDisplay::postModelStep(const km::Ktime &t, const km::Vec &) {
        if (!_scene) {
            return;
        }
        if (!_overlay_id) {
            _overlay_id = _scene->addOverlayText("", 0.f, 0.05f, {.color = params->color});
        }
        const auto &message = std::format("Time: {}s", km::ktimeToSeconds(t));
        _scene->setOverlayText(_overlay_id, message);
    }

    void TimeDisplay::_unregisterModel() {
        // Call the parent. This will do the normal unregistration.
        KModel<TimeDisplay, TimeDisplayParams>::_unregisterModel();

        if (!_scene || !_overlay_id) {
            return;
        }

        _scene->removeOverlayText(_overlay_id);
        _overlay_id = 0;
    }

    // Destructor included for MacOS builds. Must have a key-function out-of-line to avoid dulpicate
    // symbols.
    TimeDisplay::~TimeDisplay(){};

} // namespace Karana::Models