LCOV - code coverage report
Current view: top level - engine/input - event_formatters.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 76 76
Test Date: 2026-03-11 04:45:14 Functions: 100.0 % 22 22

            Line data    Source code
       1              : #pragma once
       2              : 
       3              : #include <jage/engine/input/event.hpp>
       4              : #include <jage/engine/input/keyboard/action.hpp>
       5              : #include <jage/engine/input/keyboard/events/key_press.hpp>
       6              : #include <jage/engine/input/keyboard/key.hpp>
       7              : #include <jage/engine/input/keyboard/scancode.hpp>
       8              : #include <jage/engine/input/mouse/action.hpp>
       9              : #include <jage/engine/input/mouse/button.hpp>
      10              : #include <jage/engine/input/mouse/events/click.hpp>
      11              : #include <jage/engine/input/mouse/events/cursor/motion.hpp>
      12              : #include <jage/engine/input/mouse/events/cursor/position.hpp>
      13              : #include <jage/engine/input/mouse/events/horizontal_scroll.hpp>
      14              : #include <jage/engine/input/mouse/events/vertical_scroll.hpp>
      15              : #include <jage/engine/time/durations.hpp>
      16              : #include <jage/engine/time/events/snapshot.hpp>
      17              : 
      18              : #include <jage/engine/ext/internal/overloaded.hpp>
      19              : 
      20              : #include <chrono>
      21              : #include <fmt/chrono.h>
      22              : #include <fmt/format.h>
      23              : #include <string>
      24              : #include <string_view>
      25              : 
      26              : template <>
      27              : struct fmt::formatter<jage::engine::input::keyboard::key>
      28              :     : fmt::formatter<std::string_view> {
      29            7 :   auto format(const jage::engine::input::keyboard::key &input_key,
      30              :               fmt::format_context &ctx) const {
      31           14 :     return fmt::formatter<std::string_view>::format(
      32           14 :         jage::engine::input::keyboard::serialize(input_key), ctx);
      33              :   }
      34              : };
      35              : 
      36              : template <>
      37              : struct fmt::formatter<jage::engine::input::keyboard::action>
      38              :     : fmt::formatter<std::string_view> {
      39            6 :   auto format(const jage::engine::input::keyboard::action &input_action,
      40              :               fmt::format_context &ctx) const {
      41           12 :     return fmt::formatter<std::string_view>::format(
      42           12 :         jage::engine::input::keyboard::serialize(input_action), ctx);
      43              :   }
      44              : };
      45              : 
      46              : template <>
      47              : struct fmt::formatter<jage::engine::input::keyboard::scancode>
      48              :     : fmt::formatter<std::string_view> {
      49            6 :   auto format(const jage::engine::input::keyboard::scancode &input_scancode,
      50              :               fmt::format_context &ctx) const {
      51           12 :     return fmt::formatter<std::string_view>::format(
      52           12 :         jage::engine::input::keyboard::serialize(input_scancode), ctx);
      53              :   }
      54              : };
      55              : 
      56              : template <>
      57              : struct fmt::formatter<jage::engine::input::mouse::button>
      58              :     : fmt::formatter<std::string_view> {
      59            6 :   auto format(const jage::engine::input::mouse::button &input_button,
      60              :               fmt::format_context &ctx) const {
      61           12 :     return fmt::formatter<std::string_view>::format(
      62           12 :         jage::engine::input::mouse::serialize(input_button), ctx);
      63              :   }
      64              : };
      65              : 
      66              : template <>
      67              : struct fmt::formatter<jage::engine::input::mouse::action>
      68              :     : fmt::formatter<std::string_view> {
      69            5 :   auto format(const jage::engine::input::mouse::action &input_action,
      70              :               fmt::format_context &ctx) const {
      71           10 :     return fmt::formatter<std::string_view>::format(
      72           10 :         jage::engine::input::mouse::serialize(input_action), ctx);
      73              :   }
      74              : };
      75              : 
      76              : template <>
      77              : struct fmt::formatter<jage::engine::time::durations::nanoseconds>
      78              :     : fmt::formatter<std::string> {
      79            8 :   auto format(const jage::engine::time::durations::nanoseconds &input_timestamp,
      80              :               fmt::format_context &ctx) const {
      81              :     auto timepoint = std::chrono::system_clock::time_point{
      82            8 :         std::chrono::duration_cast<std::chrono::system_clock::duration>(
      83            8 :             input_timestamp)};
      84              :     const auto text =
      85              :         fmt::format("{:%Y-%m-%d %H:%M:%S} ({} ns since epoch)", timepoint,
      86            8 :                     static_cast<std::uint64_t>(input_timestamp.count()));
      87           24 :     return fmt::formatter<std::string>::format(text, ctx);
      88            8 :   }
      89              : };
      90              : 
      91              : template <>
      92              : struct fmt::formatter<std::chrono::nanoseconds> : fmt::formatter<std::string> {
      93            5 :   auto format(const std::chrono::nanoseconds &input_timestamp,
      94              :               fmt::format_context &ctx) const {
      95            5 :     constexpr auto cst_adj =
      96              :         std::chrono::duration_cast<std::chrono::system_clock::duration>(
      97              :             std::chrono::nanoseconds{21600000000000});
      98              :     auto timepoint = std::chrono::system_clock::time_point{
      99            5 :         std::chrono::duration_cast<std::chrono::system_clock::duration>(
     100            5 :             input_timestamp) -
     101            5 :         cst_adj};
     102              :     const auto text =
     103              :         fmt::format("{:%Y-%m-%d %H:%M:%S} ({} ns since epoch)", timepoint,
     104            5 :                     static_cast<std::uint64_t>(input_timestamp.count()));
     105           15 :     return fmt::formatter<std::string>::format(text, ctx);
     106            5 :   }
     107              : };
     108              : 
     109              : template <>
     110              : struct fmt::formatter<jage::engine::input::mouse::events::click>
     111              :     : fmt::formatter<std::string> {
     112            3 :   auto format(const jage::engine::input::mouse::events::click &input_event,
     113              :               fmt::format_context &ctx) const {
     114              :     static constexpr auto template_text = R"("mouse-click": {{
     115              :       "button": {},
     116              :       "action": {},
     117              :       "modifiers": {}
     118              :   }})";
     119            9 :     return fmt::formatter<std::string>::format(
     120            9 :         fmt::format(fmt::runtime(template_text), input_event.button,
     121            6 :                     input_event.action, input_event.modifiers.to_string()),
     122            6 :         ctx);
     123              :   }
     124              : };
     125              : 
     126              : template <>
     127              : struct fmt::formatter<jage::engine::input::keyboard::events::key_press>
     128              :     : fmt::formatter<std::string> {
     129              :   auto
     130            3 :   format(const jage::engine::input::keyboard::events::key_press &input_event,
     131              :          fmt::format_context &ctx) const {
     132              :     static constexpr auto template_text = R"("key-press": {{
     133              :       "key": {},
     134              :       "scancode": {},
     135              :       "action": {},
     136              :       "modifiers": {}
     137              :   }})";
     138            9 :     return fmt::formatter<std::string>::format(
     139            9 :         fmt::format(fmt::runtime(template_text), input_event.key,
     140            3 :                     input_event.scancode, input_event.action,
     141            6 :                     input_event.modifiers.to_string()),
     142            6 :         ctx);
     143              :   }
     144              : };
     145              : 
     146              : template <>
     147              : struct fmt::formatter<jage::engine::input::mouse::events::cursor::position>
     148              :     : fmt::formatter<std::string> {
     149            4 :   auto format(
     150              :       const jage::engine::input::mouse::events::cursor::position &input_event,
     151              :       fmt::format_context &ctx) const {
     152              :     static constexpr auto template_text = R"("cursor-position": {{
     153              :       "x": {},
     154              :       "y": {}
     155              :   }})";
     156           12 :     return fmt::formatter<std::string>::format(
     157           16 :         fmt::format(fmt::runtime(template_text), input_event.x, input_event.y),
     158            8 :         ctx);
     159              :   }
     160              : };
     161              : 
     162              : template <>
     163              : struct fmt::formatter<jage::engine::input::mouse::events::cursor::motion>
     164              :     : fmt::formatter<std::string> {
     165              :   auto
     166            2 :   format(const jage::engine::input::mouse::events::cursor::motion &input_event,
     167              :          fmt::format_context &ctx) const {
     168              :     static constexpr auto template_text = R"("cursor-motion": {{
     169              :       "delta_x": {},
     170              :       "delta_y": {}
     171              :   }})";
     172            6 :     return fmt::formatter<std::string>::format(
     173            6 :         fmt::format(fmt::runtime(template_text), input_event.delta_x,
     174            2 :                     input_event.delta_y),
     175            4 :         ctx);
     176              :   }
     177              : };
     178              : 
     179              : template <>
     180              : struct fmt::formatter<jage::engine::input::mouse::events::horizontal_scroll>
     181              :     : fmt::formatter<std::string> {
     182            4 :   auto format(
     183              :       const jage::engine::input::mouse::events::horizontal_scroll &input_event,
     184              :       fmt::format_context &ctx) const {
     185              :     static constexpr auto template_text = R"("horizontal-scroll": {{
     186              :       "offset": {}
     187              :   }})";
     188           12 :     return fmt::formatter<std::string>::format(
     189           16 :         fmt::format(fmt::runtime(template_text), input_event.offset), ctx);
     190              :   }
     191              : };
     192              : 
     193              : template <>
     194              : struct fmt::formatter<jage::engine::input::mouse::events::vertical_scroll>
     195              :     : fmt::formatter<std::string> {
     196              :   auto
     197            2 :   format(const jage::engine::input::mouse::events::vertical_scroll &input_event,
     198              :          fmt::format_context &ctx) const {
     199              :     static constexpr auto template_text = R"("vertical-scroll": {{
     200              :       "offset": {}
     201              :   }})";
     202            6 :     return fmt::formatter<std::string>::format(
     203            8 :         fmt::format(fmt::runtime(template_text), input_event.offset), ctx);
     204              :   }
     205              : };
     206              : 
     207              : template <>
     208              : struct fmt::formatter<jage::engine::time::events::snapshot<
     209              :     jage::engine::time::durations::nanoseconds>> : fmt::formatter<std::string> {
     210            1 :   auto format(const jage::engine::time::events::snapshot<
     211              :                   jage::engine::time::durations::nanoseconds> &input_event,
     212              :               fmt::format_context &ctx) const {
     213              :     static constexpr auto template_text = R"(
     214              : "frame": {{
     215              :   "wall-time": {},
     216              :   "snapshot": {{
     217              :     "real_time": {},
     218              :     "tick_duration": {},
     219              :     "time_scale": {},
     220              :     "elapsed_time": {},
     221              :     "elapsed_frames": {},
     222              :     "frame": {},
     223              :     "accumulated_time": {}
     224              : }})";
     225            3 :     return fmt::formatter<std::string>::format(
     226            3 :         fmt::format(
     227              :             fmt::runtime(template_text),
     228            2 :             std::chrono::high_resolution_clock::now().time_since_epoch(),
     229            1 :             input_event.real_time, input_event.tick_duration,
     230            1 :             input_event.time_scale, input_event.elapsed_time,
     231            1 :             input_event.elapsed_frames, input_event.frame,
     232            1 :             input_event.accumulated_time),
     233            2 :         ctx);
     234              :   }
     235              : };
     236              : 
     237              : template <>
     238              : struct fmt::formatter<jage::engine::input::event<
     239              :     jage::engine::time::durations::nanoseconds>::payload_type>
     240              :     : fmt::formatter<std::string> {
     241            8 :   auto format(const jage::engine::input::event<
     242              :                   jage::engine::time::durations::nanoseconds>::payload_type
     243              :                   &input_event,
     244              :               fmt::format_context &ctx) const {
     245            8 :     return fmt::formatter<std::string>::format(
     246           16 :         std::visit(jage::engine::ext::internal::overloaded{
     247            8 :                        [&]<class T>(const T &payload) -> std::string
     248              :                          requires fmt::is_formattable<T>::value
     249           16 :                        { return fmt::format("{}", payload); },
     250              :                        [](auto &&) -> std::string {
     251              :                          return "event type formatter is missing";
     252              :                        },
     253              :                        },
     254              :                        input_event),
     255           16 :                    ctx);
     256              :   }
     257              : };
     258              : 
     259              : template <>
     260              : struct fmt::formatter<
     261              :     jage::engine::input::event<jage::engine::time::durations::nanoseconds>>
     262              :     : fmt::formatter<std::string> {
     263            2 :   auto format(const jage::engine::input::event<
     264              :                   jage::engine::time::durations::nanoseconds> &input_event,
     265              :               fmt::format_context &ctx) const {
     266              :     static constexpr auto template_text = R"("input-event": {{
     267              :     "wall-time": {},
     268              :     "timestamp": {},
     269              :     {}
     270              : }})";
     271            6 :     return fmt::formatter<std::string>::format(
     272            6 :         fmt::format(
     273              :             fmt::runtime(template_text),
     274            4 :             std::chrono::high_resolution_clock::now().time_since_epoch(),
     275            2 :             input_event.timestamp, input_event.payload),
     276            4 :         ctx);
     277              :   }
     278              : };
        

Generated by: LCOV version 2.0-1