Unravel Engine C++ Reference
Loading...
Searching...
No Matches
script_interop.h
Go to the documentation of this file.
1#pragma once
2#include <math/math.h>
3#include <monort/mono_pod_wrapper.h>
4#include <hpp/small_vector.hpp>
5#include <engine/ecs/ecs.h>
6#include <monopp/mono_field_invoker.h>
7#include <monopp/mono_domain.h>
8#include <monopp/mono_assembly.h>
11#include <engine/engine.h>
12
13namespace mono
14{
15
16template <typename T, size_t StaticCapacity>
17struct mono_converter<hpp::small_vector<T, StaticCapacity>>
18{
19 using native_type = hpp::small_vector<T, StaticCapacity>;
20 using managed_type = MonoObject*;
21
22 static auto to_mono(const native_type& obj) -> managed_type
23 {
24 return mono_array<T>(obj).get_internal_ptr();
25 }
26
27 static auto from_mono(const managed_type& obj) -> native_type
28 {
29 if(!obj)
30 {
31 return {};
32 }
33 return mono_array<T>(mono_object(obj)).template to_vector<native_type>();
34 }
35};
36
37// template <typename T, size_t StaticCapacity>
38// struct mono_converter<vector_like_wrapper<hpp::small_vector<T, StaticCapacity>>>
39// {
40// using native_type = vector_like_wrapper<hpp::small_vector<T, StaticCapacity>>;
41// using managed_type = MonoObject*;
42
43// static auto to_mono(const native_type& obj) -> managed_type
44// {
45// return mono_array<T>(obj.container, obj.type).get_internal_ptr();
46// }
47
48// static auto from_mono(const managed_type& obj) -> native_type
49// {
50// if(!obj)
51// {
52// return {};
53// }
54// auto array = mono_array<T>(mono_object(obj));
55// native_type result;
56// result.container = array.template to_vector<hpp::small_vector<T, StaticCapacity>>();
57// result.type = array.get_element_type();
58// return result;
59// }
60// };
61
62
63namespace managed_interface
64{
65
66struct vector2
67{
68 float x;
69 float y;
70};
71template<>
72auto converter::convert(const math::vec2& v) -> vector2;
73
74template<>
75auto converter::convert(const vector2& v) -> math::vec2;
76
77struct vector3
78{
79 float x;
80 float y;
81 float z;
82};
83template<>
84auto converter::convert(const math::vec3& v) -> vector3;
85
86template<>
87auto converter::convert(const vector3& v) -> math::vec3;
88
89struct vector4
90{
91 float x;
92 float y;
93 float z;
94 float w;
95};
96template<>
97auto converter::convert(const math::vec4& v) -> vector4;
98template<>
99auto converter::convert(const vector4& v) -> math::vec4;
100
102{
103 float x;
104 float y;
105 float z;
106 float w;
107};
108template<>
109auto converter::convert(const math::quat& q) -> quaternion;
110template<>
111auto converter::convert(const quaternion& q) -> math::quat;
112
113
114struct color
115{
116 float r;
117 float g;
118 float b;
119 float a;
120};
121template<>
122auto converter::convert(const math::color& v) -> color;
123template<>
124auto converter::convert(const color& v) -> math::color;
125
126struct bbox
127{
128 vector3 min{};
129 vector3 max{};
130};
131template<>
132auto converter::convert(const math::bbox& v) -> bbox;
133template<>
134auto converter::convert(const bbox& v) -> math::bbox;
135
137{
138 entt::entity entity{};
141 float distance{};
142};
143
149
151{
152 bool valid{};
153
154 color base_color{1.0f, 1.0f, 1.0f, 1.0f};
156 vector2 tiling{1.0f, 1.0f};
157 float roughness{0.5};
158 float metalness{};
159 float bumpiness{1.0f};
160};
161
163{
164 std::intptr_t native_ptr{};
165 std::string target_element_id;
166 std::intptr_t target_element_ptr{};
168 std::intptr_t current_element_ptr{};
169 int phase{};
170 std::string event_type;
171
172 // Note: Mouse and keyboard specific properties moved to derived structs
173 // for better type safety and cleaner architecture
174};
175
177{
178 // Generic pointer position (from RmlUi mouse_x, mouse_y parameters)
179 float x{};
180 float y{};
181
182 // Button index (from RmlUi button parameter, -1 if not applicable)
183 int button{-1};
184
185 // Modifier keys (from RmlUi key modifier parameters)
186 bool ctrl_key{};
187 bool shift_key{};
188 bool alt_key{};
189 bool meta_key{};
190
191 // Scroll deltas (from RmlUi wheel_delta_x, wheel_delta_y parameters)
192 float delta_x{};
193 float delta_y{};
194};
195
197{
198 // Converted key code
200
201 // Modifier keys (from RmlUi key modifier parameters)
202 bool ctrl_key{};
203 bool shift_key{};
204 bool alt_key{};
205 bool meta_key{};
206};
207
209{
210 // Text input (from RmlUi text parameter)
211 std::string text;
212
213 // Modifier keys (from RmlUi key modifier parameters)
214 bool ctrl_key{};
215 bool shift_key{};
216 bool alt_key{};
217 bool meta_key{};
218};
219
220
222{
223 float value{};
224 float min_value{};
225 float max_value{};
226 float step{};
227};
228
230{
231 std::string value;
232};
233
234
235
236} // namespace managed_interface
237
243
244// Custom converter for ui_event_base since it contains std::string fields
245template<>
246struct mono_converter<managed_interface::ui_event_base>
247{
249 using managed_type = MonoObject*;
250
251 static auto create_instance(const std::string& namespace_name, const std::string& type_name) -> mono::mono_object
252 {
253 auto& ctx = unravel::engine::context();
254 auto app_assembly = ctx.get_cached<unravel::script_system>().get_engine_assembly();
255 auto type = app_assembly.get_type(namespace_name, type_name);
256 return type.new_instance();
257 }
258 static void to_mono_base(const native_type& obj, mono::mono_object& instance)
259 {
260 mono::set_field_value(instance, "nativePtr", obj.native_ptr);
261 mono::set_field_value(instance, "targetElementId", obj.target_element_id);
262 mono::set_field_value(instance, "targetElementPtr", obj.target_element_ptr);
263 mono::set_field_value(instance, "currentElementId", obj.current_element_id);
264 mono::set_field_value(instance, "currentElementPtr", obj.current_element_ptr);
265 mono::set_field_value(instance, "phase", obj.phase);
266 mono::set_field_value(instance, "eventType", obj.event_type);
267 }
268 static void from_mono_base(const mono::mono_object& object, native_type& data)
269 {
270 mono::get_field_value(object, "nativePtr", data.native_ptr);
271 mono::get_field_value(object, "targetElementId", data.target_element_id);
272 mono::get_field_value(object, "targetElementPtr", data.target_element_ptr);
273 mono::get_field_value(object, "currentElementId", data.current_element_id);
274 mono::get_field_value(object, "currentElementPtr", data.current_element_ptr);
275 mono::get_field_value(object, "phase", data.phase);
276 mono::get_field_value(object, "eventType", data.event_type);
277 }
278
279 static auto to_mono(const native_type& obj) -> managed_type
280 {
281 auto instance = create_instance("Unravel.Core", "UIEventBase");
282 to_mono_base(obj, instance);
283 // Note: Mouse and keyboard properties removed from base event
284 return instance.get_internal_ptr();
285 }
286
287 static auto from_mono(const managed_type& obj) -> native_type
288 {
289 mono::mono_object object(obj);
290 native_type data;
291 from_mono_base(object, data);
292 // Note: Mouse and keyboard properties removed from base event
293 return data;
294 }
295};
296
297// Custom converter for ui_pointer_event
298template<>
299struct mono_converter<managed_interface::ui_pointer_event> : mono_converter<managed_interface::ui_event_base>
300{
302 using managed_type = MonoObject*;
303
304 static auto to_mono(const native_type& obj) -> managed_type
305 {
306 auto instance = create_instance("Unravel.Core", "UIPointerEvent");
307
308 // Set base class fields
309 to_mono_base(obj, instance);
310
311 // Set pointer-specific fields
312 mono::set_field_value(instance, "x", obj.x);
313 mono::set_field_value(instance, "y", obj.y);
314 mono::set_field_value(instance, "button", obj.button);
315 mono::set_field_value(instance, "ctrlKey", obj.ctrl_key);
316 mono::set_field_value(instance, "shiftKey", obj.shift_key);
317 mono::set_field_value(instance, "altKey", obj.alt_key);
318 mono::set_field_value(instance, "metaKey", obj.meta_key);
319 mono::set_field_value(instance, "deltaX", obj.delta_x);
320 mono::set_field_value(instance, "deltaY", obj.delta_y);
321
322 return instance.get_internal_ptr();
323 }
324
325 static auto from_mono(const managed_type& obj) -> native_type
326 {
327 // Implementation for C# to C++ conversion if needed
328 mono::mono_object object(obj);
329 native_type data;
330 // Set base fields
331 from_mono_base(object, data);
332 // Set pointer-specific fields
333 mono::get_field_value(object, "x", data.x);
334 mono::get_field_value(object, "y", data.y);
335 mono::get_field_value(object, "button", data.button);
336 mono::get_field_value(object, "ctrlKey", data.ctrl_key);
337 mono::get_field_value(object, "shiftKey", data.shift_key);
338 mono::get_field_value(object, "altKey", data.alt_key);
339 mono::get_field_value(object, "metaKey", data.meta_key);
340 mono::get_field_value(object, "deltaX", data.delta_x);
341 mono::get_field_value(object, "deltaY", data.delta_y);
342 return data;
343 }
344};
345
346// Custom converter for ui_key_event
347template<>
348struct mono_converter<managed_interface::ui_key_event> : mono_converter<managed_interface::ui_event_base>
349{
351 using managed_type = MonoObject*;
352
353 static auto to_mono(const native_type& obj) -> managed_type
354 {
355 auto instance = create_instance("Unravel.Core", "UIKeyEvent");
356
357 // Set base class fields
358 to_mono_base(obj, instance);
359
360 // Set key-specific fields
361 mono::set_field_value(instance, "keyCode", obj.key_code);
362 mono::set_field_value(instance, "ctrlKey", obj.ctrl_key);
363 mono::set_field_value(instance, "shiftKey", obj.shift_key);
364 mono::set_field_value(instance, "altKey", obj.alt_key);
365 mono::set_field_value(instance, "metaKey", obj.meta_key);
366
367 return instance.get_internal_ptr();
368 }
369
370 static auto from_mono(const managed_type& obj) -> native_type
371 {
372 // Implementation for C# to C++ conversion if needed
373 mono::mono_object object(obj);
374 native_type data;
375 // Set base fields
376 from_mono_base(object, data);
377 // Set key-specific fields
378 mono::get_field_value(object, "keyCode", data.key_code);
379 mono::get_field_value(object, "ctrlKey", data.ctrl_key);
380 mono::get_field_value(object, "shiftKey", data.shift_key);
381 mono::get_field_value(object, "altKey", data.alt_key);
382 mono::get_field_value(object, "metaKey", data.meta_key);
383 return data;
384 }
385};
386
387// Custom converter for ui_textinput_event
388template<>
389struct mono_converter<managed_interface::ui_textinput_event> : mono_converter<managed_interface::ui_event_base>
390{
392 using managed_type = MonoObject*;
393
394 static auto to_mono(const native_type& obj) -> managed_type
395 {
396 auto instance = create_instance("Unravel.Core", "UITextInputEvent");
397
398 // Set base class fields
399 to_mono_base(obj, instance);
400
401 // Set text input-specific fields
402 mono::set_field_value(instance, "text", obj.text);
403 mono::set_field_value(instance, "ctrlKey", obj.ctrl_key);
404 mono::set_field_value(instance, "shiftKey", obj.shift_key);
405 mono::set_field_value(instance, "altKey", obj.alt_key);
406 mono::set_field_value(instance, "metaKey", obj.meta_key);
407
408 return instance.get_internal_ptr();
409 }
410
411 static auto from_mono(const managed_type& obj) -> native_type
412 {
413 // Implementation for C# to C++ conversion if needed
414 mono::mono_object object(obj);
415 native_type data;
416 // Set base fields
417 from_mono_base(object, data);
418 // Set text input-specific fields
419 mono::get_field_value(object, "text", data.text);
420 mono::get_field_value(object, "ctrlKey", data.ctrl_key);
421 mono::get_field_value(object, "shiftKey", data.shift_key);
422 mono::get_field_value(object, "altKey", data.alt_key);
423 mono::get_field_value(object, "metaKey", data.meta_key);
424 return data;
425 }
426};
427
428// Custom converter for ui_slider_event
429template<>
430struct mono_converter<managed_interface::ui_slider_event> : mono_converter<managed_interface::ui_event_base>
431{
433 using managed_type = MonoObject*;
434
435 static auto to_mono(const native_type& obj) -> managed_type
436 {
437 auto instance = create_instance("Unravel.Core", "UISliderEvent");
438
439 // Set base class fields
440 to_mono_base(obj, instance);
441
442 // Set slider-specific fields
443 mono::set_field_value(instance, "value", obj.value);
444 mono::set_field_value(instance, "minValue", obj.min_value);
445 mono::set_field_value(instance, "maxValue", obj.max_value);
446 mono::set_field_value(instance, "step", obj.step);
447
448 return instance.get_internal_ptr();
449 }
450
451 static auto from_mono(const managed_type& obj) -> native_type
452 {
453 // Implementation for C# to C++ conversion if needed
454 mono::mono_object object(obj);
455 native_type data;
456 // Set base fields
457 from_mono_base(object, data);
458 // Set slider-specific fields
459 mono::get_field_value(object, "value", data.value);
460 mono::get_field_value(object, "minValue", data.min_value);
461 mono::get_field_value(object, "maxValue", data.max_value);
462 mono::get_field_value(object, "step", data.step);
463 return data;
464 }
465};
466
467// Custom converter for ui_change_event
468template<>
469struct mono_converter<managed_interface::ui_change_event> : mono_converter<managed_interface::ui_event_base>
470{
472 using managed_type = MonoObject*;
473
474 static auto to_mono(const native_type& obj) -> managed_type
475 {
476 auto instance = create_instance("Unravel.Core", "UIChangeEvent");
477
478 // Set base class fields
479 to_mono_base(obj, instance);
480
481 // Set change-specific fields
482 mono::set_field_value(instance, "value", obj.value);
483
484 return instance.get_internal_ptr();
485 }
486
487 static auto from_mono(const managed_type& obj) -> native_type
488 {
489 // Implementation for C# to C++ conversion if needed
490 mono::mono_object object(obj);
491 native_type data;
492 // Set base fields
493 from_mono_base(object, data);
494 // Set change-specific fields
495 mono::get_field_value(object, "value", data.value);
496 return data;
497 }
498};
499
500} // namespace mono
501
manifold_type type
const btCollisionObject * object
key_code
Definition key.hpp:6
register_basic_mono_converter_for_pod(math::vec2, managed_interface::vector2)
Storage for box vector values and wraps up common functionality.
Definition bbox.h:21
static auto to_mono(const native_type &obj) -> managed_type
static auto from_mono(const managed_type &obj) -> native_type
static auto to_mono(const native_type &obj) -> managed_type
static auto from_mono(const managed_type &obj) -> native_type
static auto to_mono(const native_type &obj) -> managed_type
static auto create_instance(const std::string &namespace_name, const std::string &type_name) -> mono::mono_object
static void to_mono_base(const native_type &obj, mono::mono_object &instance)
static auto from_mono(const managed_type &obj) -> native_type
static void from_mono_base(const mono::mono_object &object, native_type &data)
static auto to_mono(const native_type &obj) -> managed_type
static auto from_mono(const managed_type &obj) -> native_type
static auto from_mono(const managed_type &obj) -> native_type
static auto to_mono(const native_type &obj) -> managed_type
static auto to_mono(const native_type &obj) -> managed_type
static auto from_mono(const managed_type &obj) -> native_type
static auto to_mono(const native_type &obj) -> managed_type
static auto from_mono(const managed_type &obj) -> native_type
static auto context() -> rtti::context &
Definition engine.cpp:116