Debian Codegraph @

platform/macos/editor/embedded_game_view_plugin.mm

raw
1 /**************************************************************************/
2 /* embedded_game_view_plugin.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "embedded_game_view_plugin.h"
32
33 #include "embedded_process_macos.h"
34
35 #include "editor/editor_node.h"
36 #include "editor/gui/window_wrapper.h"
37
38 HashMap<String, GameViewDebuggerMacOS::ParseMessageFunc> GameViewDebuggerMacOS::parse_message_handlers;
39
40 bool GameViewDebuggerMacOS::_msg_set_context_id(const Array &p_args) {
41 ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "set_context_id: invalid number of arguments.");
42
43 embedded_process->set_context_id(p_args[0]);
44 return true;
45 }
46
47 bool GameViewDebuggerMacOS::_msg_cursor_set_shape(const Array &p_args) {
48 ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "cursor_set_shape: invalid number of arguments.");
49
50 Control::CursorShape shape = Control::CursorShape(p_args[0]);
51 embedded_process->get_layer_host()->set_default_cursor_shape(static_cast<Control::CursorShape>(shape));
52
53 return true;
54 }
55
56 bool GameViewDebuggerMacOS::_msg_cursor_set_custom_image(const Array &p_args) {
57 ERR_FAIL_COND_V_MSG(p_args.size() != 3, false, "cursor_set_custom_image: invalid number of arguments.");
58
59 Ref<Image> image;
60 image.instantiate();
61 PackedByteArray cursor_data = p_args[0];
62 if (!cursor_data.is_empty()) {
63 image->load_png_from_buffer(cursor_data);
64 }
65 DisplayServer::CursorShape shape = DisplayServer::CursorShape(p_args[1]);
66 Vector2 hotspot = p_args[2];
67
68 embedded_process->get_layer_host()->cursor_set_custom_image(image, shape, hotspot);
69
70 return true;
71 }
72
73 bool GameViewDebuggerMacOS::_msg_mouse_set_mode(const Array &p_args) {
74 ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "mouse_set_mode: invalid number of arguments.");
75
76 DisplayServer::MouseMode mode = DisplayServer::MouseMode(p_args[0]);
77 embedded_process->mouse_set_mode(mode);
78
79 return true;
80 }
81
82 bool GameViewDebuggerMacOS::_msg_window_set_ime_active(const Array &p_args) {
83 ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "window_set_ime_active: invalid number of arguments.");
84
85 bool active = p_args[0];
86 DisplayServer::WindowID wid = embedded_process->get_window()->get_window_id();
87 DisplayServer::get_singleton()->window_set_ime_active(active, wid);
88 return true;
89 }
90
91 bool GameViewDebuggerMacOS::_msg_window_set_ime_position(const Array &p_args) {
92 ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "window_set_ime_position: invalid number of arguments.");
93
94 Point2i pos = p_args[0];
95 Point2i xpos = embedded_process->get_layer_host()->get_global_transform_with_canvas().xform(pos);
96 DisplayServer::WindowID wid = embedded_process->get_window()->get_window_id();
97 DisplayServer::get_singleton()->window_set_ime_position(xpos, wid);
98 return true;
99 }
100
101 bool GameViewDebuggerMacOS::_msg_joy_start(const Array &p_args) {
102 ERR_FAIL_COND_V_MSG(p_args.size() != 3, false, "joy_start: invalid number of arguments.");
103
104 int joy_id = p_args[0];
105 float duration = p_args[1];
106 Vector2 strength = p_args[2];
107 Input::get_singleton()->start_joy_vibration(joy_id, strength.x, strength.y, duration);
108 return true;
109 }
110
111 bool GameViewDebuggerMacOS::_msg_joy_stop(const Array &p_args) {
112 ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "joy_stop: invalid number of arguments.");
113
114 int joy_id = p_args[0];
115 Input::get_singleton()->stop_joy_vibration(joy_id);
116 return true;
117 }
118
119 bool GameViewDebuggerMacOS::_msg_warp_mouse(const Array &p_args) {
120 ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "warp_mouse: invalid number of arguments.");
121
122 Vector2i pos = p_args[0];
123 embedded_process->get_layer_host()->warp_mouse(pos);
124 return true;
125 }
126
127 void GameViewDebuggerMacOS::_init_capture_message_handlers() {
128 parse_message_handlers["game_view:set_context_id"] = &GameViewDebuggerMacOS::_msg_set_context_id;
129 parse_message_handlers["game_view:cursor_set_shape"] = &GameViewDebuggerMacOS::_msg_cursor_set_shape;
130 parse_message_handlers["game_view:cursor_set_custom_image"] = &GameViewDebuggerMacOS::_msg_cursor_set_custom_image;
131 parse_message_handlers["game_view:mouse_set_mode"] = &GameViewDebuggerMacOS::_msg_mouse_set_mode;
132 parse_message_handlers["game_view:window_set_ime_active"] = &GameViewDebuggerMacOS::_msg_window_set_ime_active;
133 parse_message_handlers["game_view:window_set_ime_position"] = &GameViewDebuggerMacOS::_msg_window_set_ime_position;
134 parse_message_handlers["game_view:warp_mouse"] = &GameViewDebuggerMacOS::_msg_warp_mouse;
135 }
136
137 bool GameViewDebuggerMacOS::capture(const String &p_message, const Array &p_data, int p_session) {
138 Ref<EditorDebuggerSession> session = get_session(p_session);
139 ERR_FAIL_COND_V(session.is_null(), true);
140
141 ParseMessageFunc *fn_ptr = parse_message_handlers.getptr(p_message);
142 if (fn_ptr) {
143 return (this->**fn_ptr)(p_data);
144 } else {
145 return GameViewDebugger::capture(p_message, p_data, p_session);
146 }
147
148 return true;
149 }
150
151 GameViewDebuggerMacOS::GameViewDebuggerMacOS(EmbeddedProcessMacOS *p_embedded_process) :
152 embedded_process(p_embedded_process) {
153 if (parse_message_handlers.is_empty()) {
154 _init_capture_message_handlers();
155 }
156 }
157
158 GameViewPluginMacOS::GameViewPluginMacOS() {
159 if (Engine::get_singleton()->is_recovery_mode_hint()) {
160 return;
161 }
162
163 EmbeddedProcessMacOS *embedded_process = memnew(EmbeddedProcessMacOS);
164
165 Ref<GameViewDebuggerMacOS> debugger;
166 debugger.instantiate(embedded_process);
167
168 setup(debugger, embedded_process);
169 }
170
171 extern "C" GameViewPluginBase *get_game_view_plugin() {
172 return memnew(GameViewPluginMacOS);
173 }