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 const auto& domain = mono_domain::get_current_domain();
25 return mono_array<T>(domain, obj).get_internal_ptr();
26 }
27
28 static auto from_mono(const managed_type& obj) -> native_type
29 {
30 if(!obj)
31 {
32 return {};
33 }
34 return mono_array<T>(mono_object(obj)).template to_vector<native_type>();
35 }
36};
37
38
39namespace managed_interface
40{
41
42struct vector2
43{
44 float x;
45 float y;
46};
47template<>
48auto converter::convert(const math::vec2& v) -> vector2;
49
50template<>
51auto converter::convert(const vector2& v) -> math::vec2;
52
53struct vector3
54{
55 float x;
56 float y;
57 float z;
58};
59template<>
60auto converter::convert(const math::vec3& v) -> vector3;
61
62template<>
63auto converter::convert(const vector3& v) -> math::vec3;
64
65struct vector4
66{
67 float x;
68 float y;
69 float z;
70 float w;
71};
72template<>
73auto converter::convert(const math::vec4& v) -> vector4;
74template<>
75auto converter::convert(const vector4& v) -> math::vec4;
76
78{
79 float x;
80 float y;
81 float z;
82 float w;
83};
84template<>
85auto converter::convert(const math::quat& q) -> quaternion;
86template<>
87auto converter::convert(const quaternion& q) -> math::quat;
88
89
90struct color
91{
92 float r;
93 float g;
94 float b;
95 float a;
96};
97template<>
98auto converter::convert(const math::color& v) -> color;
99template<>
100auto converter::convert(const color& v) -> math::color;
101
102struct bbox
103{
104 vector3 min{};
105 vector3 max{};
106};
107template<>
108auto converter::convert(const math::bbox& v) -> bbox;
109template<>
110auto converter::convert(const bbox& v) -> math::bbox;
111
113{
114 entt::entity entity{};
117 float distance{};
118};
119
125
127{
128 bool valid{};
129
130 color base_color{1.0f, 1.0f, 1.0f, 1.0f};
132 vector2 tiling{1.0f, 1.0f};
133 float roughness{0.5};
134 float metalness{};
135 float bumpiness{1.0f};
136};
137
139{
140 std::intptr_t native_ptr{};
141 std::string target_element_id;
142 std::intptr_t target_element_ptr{};
144 std::intptr_t current_element_ptr{};
145 int phase{};
146 std::string event_type;
147
148 // Note: Mouse and keyboard specific properties moved to derived structs
149 // for better type safety and cleaner architecture
150};
151
153{
154 // Generic pointer position (from RmlUi mouse_x, mouse_y parameters)
155 float x{};
156 float y{};
157
158 // Button index (from RmlUi button parameter, -1 if not applicable)
159 int button{-1};
160
161 // Modifier keys (from RmlUi key modifier parameters)
162 bool ctrl_key{};
163 bool shift_key{};
164 bool alt_key{};
165 bool meta_key{};
166
167 // Scroll deltas (from RmlUi wheel_delta_x, wheel_delta_y parameters)
168 float delta_x{};
169 float delta_y{};
170};
171
173{
174 // Converted key code
176
177 // Modifier keys (from RmlUi key modifier parameters)
178 bool ctrl_key{};
179 bool shift_key{};
180 bool alt_key{};
181 bool meta_key{};
182};
183
185{
186 // Text input (from RmlUi text parameter)
187 std::string text;
188
189 // Modifier keys (from RmlUi key modifier parameters)
190 bool ctrl_key{};
191 bool shift_key{};
192 bool alt_key{};
193 bool meta_key{};
194};
195
196
198{
199 float value{};
200 float min_value{};
201 float max_value{};
202 float step{};
203};
204
206{
207 std::string value;
208};
209
210
211
212} // namespace managed_interface
213
219
220// Custom converter for ui_event_base since it contains std::string fields
221template<>
222struct mono_converter<managed_interface::ui_event_base>
223{
225 using managed_type = MonoObject*;
226
227 static auto create_instance(const std::string& namespace_name, const std::string& type_name) -> mono::mono_object
228 {
229 auto& ctx = unravel::engine::context();
230 auto app_assembly = ctx.get_cached<unravel::script_system>().get_engine_assembly();
231 auto type = app_assembly.get_type(namespace_name, type_name);
232 return type.new_instance();
233 }
234 static void to_mono_base(const native_type& obj, mono::mono_object& instance)
235 {
236 mono::set_field_value(instance, "nativePtr", obj.native_ptr);
237 mono::set_field_value(instance, "targetElementId", obj.target_element_id);
238 mono::set_field_value(instance, "targetElementPtr", obj.target_element_ptr);
239 mono::set_field_value(instance, "currentElementId", obj.current_element_id);
240 mono::set_field_value(instance, "currentElementPtr", obj.current_element_ptr);
241 mono::set_field_value(instance, "phase", obj.phase);
242 mono::set_field_value(instance, "eventType", obj.event_type);
243 }
244 static void from_mono_base(const mono::mono_object& object, native_type& data)
245 {
246 mono::get_field_value(object, "nativePtr", data.native_ptr);
247 mono::get_field_value(object, "targetElementId", data.target_element_id);
248 mono::get_field_value(object, "targetElementPtr", data.target_element_ptr);
249 mono::get_field_value(object, "currentElementId", data.current_element_id);
250 mono::get_field_value(object, "currentElementPtr", data.current_element_ptr);
251 mono::get_field_value(object, "phase", data.phase);
252 mono::get_field_value(object, "eventType", data.event_type);
253 }
254
255 static auto to_mono(const native_type& obj) -> managed_type
256 {
257 auto instance = create_instance("Unravel.Core", "UIEventBase");
258 to_mono_base(obj, instance);
259 // Note: Mouse and keyboard properties removed from base event
260 return instance.get_internal_ptr();
261 }
262
263 static auto from_mono(const managed_type& obj) -> native_type
264 {
265 mono::mono_object object(obj);
266 native_type data;
267 from_mono_base(object, data);
268 // Note: Mouse and keyboard properties removed from base event
269 return data;
270 }
271};
272
273// Custom converter for ui_pointer_event
274template<>
275struct mono_converter<managed_interface::ui_pointer_event> : mono_converter<managed_interface::ui_event_base>
276{
278 using managed_type = MonoObject*;
279
280 static auto to_mono(const native_type& obj) -> managed_type
281 {
282 auto instance = create_instance("Unravel.Core", "UIPointerEvent");
283
284 // Set base class fields
285 to_mono_base(obj, instance);
286
287 // Set pointer-specific fields
288 mono::set_field_value(instance, "x", obj.x);
289 mono::set_field_value(instance, "y", obj.y);
290 mono::set_field_value(instance, "button", obj.button);
291 mono::set_field_value(instance, "ctrlKey", obj.ctrl_key);
292 mono::set_field_value(instance, "shiftKey", obj.shift_key);
293 mono::set_field_value(instance, "altKey", obj.alt_key);
294 mono::set_field_value(instance, "metaKey", obj.meta_key);
295 mono::set_field_value(instance, "deltaX", obj.delta_x);
296 mono::set_field_value(instance, "deltaY", obj.delta_y);
297
298 return instance.get_internal_ptr();
299 }
300
301 static auto from_mono(const managed_type& obj) -> native_type
302 {
303 // Implementation for C# to C++ conversion if needed
304 mono::mono_object object(obj);
305 native_type data;
306 // Set base fields
307 from_mono_base(object, data);
308 // Set pointer-specific fields
309 mono::get_field_value(object, "x", data.x);
310 mono::get_field_value(object, "y", data.y);
311 mono::get_field_value(object, "button", data.button);
312 mono::get_field_value(object, "ctrlKey", data.ctrl_key);
313 mono::get_field_value(object, "shiftKey", data.shift_key);
314 mono::get_field_value(object, "altKey", data.alt_key);
315 mono::get_field_value(object, "metaKey", data.meta_key);
316 mono::get_field_value(object, "deltaX", data.delta_x);
317 mono::get_field_value(object, "deltaY", data.delta_y);
318 return data;
319 }
320};
321
322// Custom converter for ui_key_event
323template<>
324struct mono_converter<managed_interface::ui_key_event> : mono_converter<managed_interface::ui_event_base>
325{
327 using managed_type = MonoObject*;
328
329 static auto to_mono(const native_type& obj) -> managed_type
330 {
331 auto instance = create_instance("Unravel.Core", "UIKeyEvent");
332
333 // Set base class fields
334 to_mono_base(obj, instance);
335
336 // Set key-specific fields
337 mono::set_field_value(instance, "keyCode", obj.key_code);
338 mono::set_field_value(instance, "ctrlKey", obj.ctrl_key);
339 mono::set_field_value(instance, "shiftKey", obj.shift_key);
340 mono::set_field_value(instance, "altKey", obj.alt_key);
341 mono::set_field_value(instance, "metaKey", obj.meta_key);
342
343 return instance.get_internal_ptr();
344 }
345
346 static auto from_mono(const managed_type& obj) -> native_type
347 {
348 // Implementation for C# to C++ conversion if needed
349 mono::mono_object object(obj);
350 native_type data;
351 // Set base fields
352 from_mono_base(object, data);
353 // Set key-specific fields
354 mono::get_field_value(object, "keyCode", data.key_code);
355 mono::get_field_value(object, "ctrlKey", data.ctrl_key);
356 mono::get_field_value(object, "shiftKey", data.shift_key);
357 mono::get_field_value(object, "altKey", data.alt_key);
358 mono::get_field_value(object, "metaKey", data.meta_key);
359 return data;
360 }
361};
362
363// Custom converter for ui_textinput_event
364template<>
365struct mono_converter<managed_interface::ui_textinput_event> : mono_converter<managed_interface::ui_event_base>
366{
368 using managed_type = MonoObject*;
369
370 static auto to_mono(const native_type& obj) -> managed_type
371 {
372 auto instance = create_instance("Unravel.Core", "UITextInputEvent");
373
374 // Set base class fields
375 to_mono_base(obj, instance);
376
377 // Set text input-specific fields
378 mono::set_field_value(instance, "text", obj.text);
379 mono::set_field_value(instance, "ctrlKey", obj.ctrl_key);
380 mono::set_field_value(instance, "shiftKey", obj.shift_key);
381 mono::set_field_value(instance, "altKey", obj.alt_key);
382 mono::set_field_value(instance, "metaKey", obj.meta_key);
383
384 return instance.get_internal_ptr();
385 }
386
387 static auto from_mono(const managed_type& obj) -> native_type
388 {
389 // Implementation for C# to C++ conversion if needed
390 mono::mono_object object(obj);
391 native_type data;
392 // Set base fields
393 from_mono_base(object, data);
394 // Set text input-specific fields
395 mono::get_field_value(object, "text", data.text);
396 mono::get_field_value(object, "ctrlKey", data.ctrl_key);
397 mono::get_field_value(object, "shiftKey", data.shift_key);
398 mono::get_field_value(object, "altKey", data.alt_key);
399 mono::get_field_value(object, "metaKey", data.meta_key);
400 return data;
401 }
402};
403
404// Custom converter for ui_slider_event
405template<>
406struct mono_converter<managed_interface::ui_slider_event> : mono_converter<managed_interface::ui_event_base>
407{
409 using managed_type = MonoObject*;
410
411 static auto to_mono(const native_type& obj) -> managed_type
412 {
413 auto instance = create_instance("Unravel.Core", "UISliderEvent");
414
415 // Set base class fields
416 to_mono_base(obj, instance);
417
418 // Set slider-specific fields
419 mono::set_field_value(instance, "value", obj.value);
420 mono::set_field_value(instance, "minValue", obj.min_value);
421 mono::set_field_value(instance, "maxValue", obj.max_value);
422 mono::set_field_value(instance, "step", obj.step);
423
424 return instance.get_internal_ptr();
425 }
426
427 static auto from_mono(const managed_type& obj) -> native_type
428 {
429 // Implementation for C# to C++ conversion if needed
430 mono::mono_object object(obj);
431 native_type data;
432 // Set base fields
433 from_mono_base(object, data);
434 // Set slider-specific fields
435 mono::get_field_value(object, "value", data.value);
436 mono::get_field_value(object, "minValue", data.min_value);
437 mono::get_field_value(object, "maxValue", data.max_value);
438 mono::get_field_value(object, "step", data.step);
439 return data;
440 }
441};
442
443// Custom converter for ui_change_event
444template<>
445struct mono_converter<managed_interface::ui_change_event> : mono_converter<managed_interface::ui_event_base>
446{
448 using managed_type = MonoObject*;
449
450 static auto to_mono(const native_type& obj) -> managed_type
451 {
452 auto instance = create_instance("Unravel.Core", "UIChangeEvent");
453
454 // Set base class fields
455 to_mono_base(obj, instance);
456
457 // Set change-specific fields
458 mono::set_field_value(instance, "value", obj.value);
459
460 return instance.get_internal_ptr();
461 }
462
463 static auto from_mono(const managed_type& obj) -> native_type
464 {
465 // Implementation for C# to C++ conversion if needed
466 mono::mono_object object(obj);
467 native_type data;
468 // Set base fields
469 from_mono_base(object, data);
470 // Set change-specific fields
471 mono::get_field_value(object, "value", data.value);
472 return data;
473 }
474};
475
476} // namespace mono
477
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:115