sandbox
Loading...
Searching...
No Matches
interop.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_SCRIPTING_INTEROP_HPP_
3#define LIBSBX_SCRIPTING_INTEROP_HPP_
4
5#include <functional>
6
7#include <libsbx/utility/enum.hpp>
8#include <libsbx/utility/type_name.hpp>
9
10#include <libsbx/devices/input.hpp>
11
12#include <libsbx/scenes/node.hpp>
13#include <libsbx/scenes/scenes_module.hpp>
14
15#include <libsbx/physics/character_controller.hpp>
16
17#include <libsbx/scripting/managed/string.hpp>
18#include <libsbx/scripting/managed/type.hpp>
19#include <libsbx/scripting/managed/assembly.hpp>
20
21namespace sbx::scripting {
22
23struct interop {
24
25 enum class log_level : std::int32_t {
26 trace = utility::bit_v<0>,
27 debug = utility::bit_v<1>,
28 info = utility::bit_v<2>,
29 warn = utility::bit_v<3>,
30 error = utility::bit_v<4>,
31 critical = utility::bit_v<5>
32 }; // enum class log_level
33
34 static auto log_log_message(log_level level, managed::string message) -> void;
35
36 static auto behavior_add_component(std::uint32_t node, managed::reflection_type component_type) -> void;
37
38 static auto behavior_has_component(std::uint32_t node, managed::reflection_type component_type) -> bool;
39
40 // static auto behavior_remove_component(std::uint32_t node, managed::reflection_type component_type) -> bool;
41
42 static auto tag_get_tag(std::uint32_t node) -> managed::string;
43
44 static auto tag_set_tag(std::uint32_t node, managed::string tag) -> void;
45
46 static auto transform_get_position(std::uint32_t node, math::vector3* position) -> void;
47
48 static auto transform_set_position(std::uint32_t node, math::vector3* position) -> void;
49
50 static auto transform_get_world_position(std::uint32_t node, math::vector3* position) -> void;
51
52 static auto transform_get_rotation(std::uint32_t node, math::quaternion* rotation) -> void;
53
54 static auto transform_set_rotation(std::uint32_t node, math::quaternion* rotation) -> void;
55
56 static auto transform_get_right(std::uint32_t node, math::vector3* right) -> void;
57
58 static auto transform_get_forward(std::uint32_t node, math::vector3* forward) -> void;
59
60 static auto transform_get_up(std::uint32_t node, math::vector3* up) -> void;
61
62 static auto transform_look_at(std::uint32_t node, math::vector3* target) -> void;
63
64 static auto character_controller_get_height(std::uint32_t node, std::float_t* height) -> void;
65
66 static auto character_controller_get_radius(std::uint32_t node, std::float_t* radius) -> void;
67
68 static auto character_controller_get_slope_limit(std::uint32_t node, std::float_t* slope_limit) -> void;
69
70 static auto character_controller_get_step_offset(std::uint32_t node, std::float_t* step_offset) -> void;
71
72 static auto character_controller_get_is_grounded(std::uint32_t node) -> managed::bool32;
73
74 static auto character_controller_get_flags(std::uint32_t node, std::uint8_t* flags) -> void;
75
76 static auto character_controller_move(std::uint32_t node, math::vector3* displacement) -> void;
77
78 static auto input_is_key_pressed(devices::key key) -> managed::bool32;
79
80 static auto input_is_key_down(devices::key key) -> managed::bool32;
81
82 static auto input_is_key_released(devices::key key) -> managed::bool32;
83
84 static auto input_is_mouse_button_pressed(devices::mouse_button mouse_button) -> managed::bool32;
85
86 static auto input_is_mouse_button_down(devices::mouse_button mouse_button) -> managed::bool32;
87
88 static auto input_is_mouse_button_released(devices::mouse_button mouse_button) -> managed::bool32;
89
90 static auto input_mouse_position(math::vector2* position) -> void;
91
92 static auto input_scroll_delta(math::vector2* scroll_delta) -> void;
93
94 static auto camera_screen_point_to_ray(math::ray* ray, math::vector2* position) -> void;
95
96 static auto camera_get_position(math::vector3* position) -> void;
97
98 static auto camera_set_position(math::vector3* position) -> void;
99
100 static auto camera_get_rotation(math::quaternion* rotation) -> void;
101
102 static auto camera_set_rotation(math::quaternion* rotation) -> void;
103
104 static auto camera_get_forward(math::vector3* forward) -> void;
105
106 static auto camera_get_right(math::vector3* right) -> void;
107
108 static auto camera_get_up(math::vector3* up) -> void;
109
110 static auto camera_get_viewport(math::vector2* viewport) -> void;
111
112 static auto time_delta_time(std::float_t* delta_time) -> void;
113
114 template<typename Type>
115 static auto register_managed_component(std::string_view name, managed::assembly& core_assembly) -> void {
116 auto& scenes_module = core::engine::get_module<scenes::scenes_module>();
117
118 const auto component_name = std::format("Sbx.Core.{}", name);
119
120 auto& type = core_assembly.get_type(component_name);
121
122 if (type) {
123 _add_component_functions[type.get_type_id()] = [&scenes_module](scenes::node node) -> void {
124 auto& scenes_module = core::engine::get_module<scenes::scenes_module>();
125 auto& scene = scenes_module.active_scene();
126 auto& graph = scene.graph();
127
128 graph.add_component<Type>(node);
129 };
130 _has_component_functions[type.get_type_id()] = [&scenes_module](scenes::node node) -> bool {
131 auto& scenes_module = core::engine::get_module<scenes::scenes_module>();
132 auto& scene = scenes_module.active_scene();
133 auto& graph = scene.graph();
134
135 return graph.has_component<Type>(node);
136 };
137 // _remove_component_functions[type.get_type_id()] = [&scenes_module](scenes::node node) {
138 // auto& scenes_module = core::engine::get_module<scenes::scenes_module>();
139 // auto& scene = scenes_module.active_scene();
140 // auto& environment = scene.environment();
141 // auto& graph = scene.graph();
142
143 // scene.remove_component<Type>(node);
144 // };
145 } else {
146 utility::logger<"scripting">::warn("No C# component class found for {}!", component_name);
147 }
148 }
149
150private:
151
152 inline static auto _add_component_functions = std::unordered_map<managed::type_id, std::function<void(scenes::node)>>{};
153 inline static auto _has_component_functions = std::unordered_map<managed::type_id, std::function<bool(scenes::node)>>{};
154 // inline static auto _remove_component_functions = std::unordered_map<managed::type_id, std::function<void(scenes::node)>>{};
155
156}; // class interop
157
158} // namespace sbx::scripting
159
160#endif // LIBSBX_SCRIPTING_INTEROP_HPP_
Definition: tests.cpp:6
Definition: quaternion.hpp:25
A vector in two-dimensional space.
Definition: vector2.hpp:28
Definition: vector3.hpp:23
3D ray with normalized direction.
Definition: ray.hpp:37
Definition: assembly.hpp:27
Definition: string.hpp:11
Definition: logger.hpp:124
Definition: interop.hpp:23