Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

12.2 KB
Raw Blame
/**************************************************************************/
/*  game_view_plugin.h                                                    */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#pragma once

#include "editor/debugger/editor_debugger_node.h"
#include "editor/debugger/editor_debugger_plugin.h"
#include "editor/editor_main_screen.h"
#include "editor/plugins/editor_plugin.h"
#include "scene/debugger/canvas_item_manipulator.h"
#include "scene/debugger/runtime_node_select.h"
#include "scene/gui/box_container.h"

class ButtonGroup;
class EditorToolbarGroup;
class EmbeddedProcessBase;
class VSeparator;
class WindowWrapper;
class ScriptEditorDebugger;
class EditorDock;

class GameViewDebugger : public EditorDebuggerPlugin {
	GDCLASS(GameViewDebugger, EditorDebuggerPlugin);

private:
	Vector<Ref<EditorDebuggerSession>> sessions;

	bool is_feature_enabled = true;
	bool was_feature_disabled_mid_play = false;
	int node_type = RuntimeNodeSelect::NODE_TYPE_NONE;
	bool selection_visible = true;
	int ci_tool = CanvasItemManipulator::TOOL_SELECT;
	int ci_local_space = true;
	int n3d_tool = RuntimeNodeSelect::SELECT_MODE_SINGLE;
	bool mute_audio = false;
	EditorDebuggerNode::CameraOverride camera_override_mode = EditorDebuggerNode::OVERRIDE_INGAME;

	bool selection_avoid_locked = false;
	bool selection_prefer_group = false;

	void _session_started(Ref<EditorDebuggerSession> p_session);
	void _session_stopped();

	void _feature_profile_changed();

	struct ScreenshotCB {
		Callable cb;
		Rect2i rect;
	};

	int64_t scr_rq_id = 0;
	HashMap<uint64_t, ScreenshotCB> screenshot_callbacks;

	bool _msg_get_screenshot(const Array &p_args);
	bool _msg_show_toaster(const Array &p_args);
	bool _msg_open_scene(const Array &p_args);

protected:
	static void _bind_methods();

public:
	virtual bool capture(const String &p_message, const Array &p_data, int p_session) override;
	virtual bool has_capture(const String &p_capture) const override;

	bool add_screenshot_callback(const Callable &p_callaback, const Rect2i &p_rect);

	void window_request_size();
	void hdr_output_request_state();

	void set_suspend(bool p_enabled);
	void next_frame();

	void set_time_scale(double p_scale);
	void reset_time_scale();

	void set_node_type(int p_type);
	void set_ci_tool(int p_tool);
	void set_n3d_tool(int p_tool);
	// FIXME: This is present only for Android, and the code that uses it needs to be updated.
	void set_select_mode(int p_mode) {}

	void set_selection_visible(bool p_visible);

	void set_selection_avoid_locked(bool p_enabled);
	void set_selection_prefer_group(bool p_enabled);

	void set_debug_mute_audio(bool p_enabled);

	void toggle_hdr_output_requested();

	void set_camera_override(bool p_enabled);
	void set_camera_manipulate_mode(EditorDebuggerNode::CameraOverride p_mode);

	void set_ci_local_space(bool p_enabled);

	void report_window_focused(bool p_focused);

	void reset_camera_2d_position();
	void reset_camera_3d_position();

	virtual void setup_session(int p_session_id) override;

	GameViewDebugger();
};

class GameView : public VBoxContainer {
	GDCLASS(GameView, VBoxContainer);

#ifdef ANDROID_ENABLED
	friend class GameViewPluginBase;
#endif

	enum {
		CAMERA_RESET_2D,
		CAMERA_RESET_3D,
		CAMERA_MODE_INGAME,
		CAMERA_MODE_EDITORS,
		SELECTION_AVOID_LOCKED,
		SELECTION_PREFER_GROUP,
		WINDOW_SIZE_MODE_FIXED,
		WINDOW_SIZE_MODE_KEEP_ASPECT,
		WINDOW_SIZE_MODE_STRETCH,
		WINDOW_SEPARATOR_DYNAMIC_RANGE,
		WINDOW_REQUEST_HDR_OUTPUT,
		WINDOW_HDR_OUTPUT_ERROR,
	};

	enum EmbedSizeMode {
		SIZE_MODE_FIXED,
		SIZE_MODE_KEEP_ASPECT,
		SIZE_MODE_STRETCH,
	};

	enum EmbedAvailability {
		EMBED_AVAILABLE,
		EMBED_NOT_AVAILABLE_FEATURE_NOT_SUPPORTED,
		EMBED_NOT_AVAILABLE_MINIMIZED,
		EMBED_NOT_AVAILABLE_MAXIMIZED,
		EMBED_NOT_AVAILABLE_FULLSCREEN,
		EMBED_NOT_AVAILABLE_SINGLE_WINDOW_MODE,
		EMBED_NOT_AVAILABLE_PROJECT_DISPLAY_DRIVER,
		EMBED_NOT_AVAILABLE_HEADLESS,
	};

	enum EmbedMode {
		EMBED_TYPE_DISABLED,
		EMBED_TYPE_FLOATING,
		EMBED_TYPE_EDITOR,
		EMBED_TYPE_MAX,
	};

	inline static GameView *singleton = nullptr;
	inline static EditorDock *dock_singleton = nullptr;

	Ref<GameViewDebugger> debugger;
	WindowWrapper *window_wrapper = nullptr;
	EditorDock *game_dock = nullptr;

	bool is_feature_enabled = true;
	bool was_feature_disabled_mid_play = false;
	int active_sessions = 0;
	int screen_index_before_start = -1;
	ScriptEditorDebugger *embedded_script_debugger = nullptr;

	bool embed_on_play = true;
	bool make_floating_on_play = true;
	EmbedSizeMode embed_size_mode = SIZE_MODE_FIXED;
	bool paused = false;
	Size2 size_paused;

	Rect2i floating_window_rect;
	int floating_window_screen = -1;

	bool debug_hide_selection = false;
	bool debug_mute_audio = false;

	HBoxContainer *ci_bar = nullptr;
	EditorToolbarGroup *n3d_bar = nullptr;

	bool selection_avoid_locked = false;
	bool selection_prefer_group = false;

	Button *suspend_button = nullptr;
	Button *next_frame_button = nullptr;

	Ref<ButtonGroup> node_group;
	Ref<ButtonGroup> ci_group;
	Ref<ButtonGroup> n3d_group;

	Button *node_type_button[RuntimeNodeSelect::NODE_TYPE_MAX];
	// Not all CI tools have buttons, so make sure the gaps are at least initialized with null.
	Button *ci_button[CanvasItemManipulator::TOOL_MAX] = {};
	Button *ci_local_space_button = nullptr;
	Button *n3d_button[RuntimeNodeSelect::SELECT_MODE_MAX];

	MenuButton *n3d_options_menu = nullptr;

	Button *camera_override_button = nullptr;
	MenuButton *camera_override_menu = nullptr;

	Button *debug_hide_selection_button = nullptr;
	Button *debug_mute_audio_button = nullptr;

	HBoxContainer *embedding_hb = nullptr;
	MenuButton *game_window_options_menu = nullptr;
	Label *game_size_label = nullptr;
	HBoxContainer *game_hb = nullptr;
	Ref<ButtonGroup> game_embed_group;
	Button *game_embed_mode_button[EmbedMode::EMBED_TYPE_MAX];
	Panel *panel = nullptr;
	EmbeddedProcessBase *embedded_process = nullptr;
	Label *state_label = nullptr;

	const int DEFAULT_TIME_SCALE_INDEX = 5;
	Array time_scale_range = { 0.0625f, 0.125f, 0.25f, 0.5f, 0.75f, 1.0f, 1.25f, 1.5f, 1.75f, 2.0f, 4.0f, 8.0f, 16.0f };
	Array time_scale_label = { "1/16", "1/8", "1/4", "1/2", "3/4", "1.0", "1.25", "1.5", "1.75", "2.0", "4.0", "8.0", "16.0" };
	int time_scale_index = DEFAULT_TIME_SCALE_INDEX;

	Size2i game_window_size = Size2i(-1, -1);
	bool hdr_output_enabled = false;
	float current_max_luminance = 0.0f;
	float current_reference_luminance = 0.0f;
	float output_max_linear_value = 1.0f;
	bool display_server_supports_hdr_output = false;
	bool renderer_supports_hdr_output = false;

	MenuButton *speed_state_button = nullptr;

	void _sessions_changed();

	void _update_debugger_buttons();

	void _suspend_button_toggled(bool p_pressed);

	void _node_type_pressed(int p_option);
	void _game_embed_mode_pressed(int p_option);
	void _selection_options_menu_id_pressed(int p_id);
	void _game_window_options_menu_menu_id_pressed(int p_id);

	void _reset_time_scales();
	void _speed_state_menu_pressed(int p_id);
	void _update_speed_buttons();
	void _update_speed_state_icon(int p_id);
	void _update_speed_state_color();
	void _update_speed_state_size();

	void _play_pressed();
	static void _instance_starting_static(int p_idx, List<String> &r_arguments);
	void _instance_starting(int p_idx, List<String> &r_arguments);
	static bool _instance_rq_screenshot_static(const Callable &p_callback);
	bool _instance_rq_screenshot(const Callable &p_callback);
	void _stop_pressed();
	void _embedding_completed();
	void _embedding_failed();
	void _embedded_process_focused();
	void _editor_or_project_settings_changed();
	void _inspector_object_changed();

	EmbedAvailability _get_embed_available();
	void _update_ui();
	void _update_embed_menu_options();
	void _update_embed_buttons();
	void _update_game_window_size_label();
	void _update_embed_window_size();
	void _update_arguments_for_instance(int p_idx, List<String> &r_arguments);
	void _show_update_window_wrapper();

	void _debug_hide_selection_button_pressed();
	void _debug_mute_audio_button_pressed();

	void _setup_complete();
	void _game_window_size_received(const Array &p_state);
	void _hdr_state_received(const Array &p_state);

	void _camera_override_button_toggled(bool p_pressed);
	void _camera_override_menu_id_pressed(int p_id);

	void _window_close_request();
	void _update_floating_window_settings();
	void _attach_script_debugger();
	void _detach_script_debugger();
	void _remote_window_title_changed(const String &p_title);

	void _debugger_breaked(bool p_breaked, bool p_can_debug);

	void _feature_profile_changed();

	Button *_setup_button(const StringName &p_icon, const Ref<ButtonGroup> &p_group, const Ref<Shortcut> &p_shortcut = nullptr);

protected:
	void _notification(int p_what);

public:
	static EditorDock *get_dock() { return dock_singleton; }

	void set_window_layout(Ref<ConfigFile> p_layout);
	void get_window_layout(Ref<ConfigFile> p_layout);

	MarginContainer *toolbar_margin = nullptr;

	GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process, WindowWrapper *p_wrapper);
};

class GameViewPluginBase : public EditorPlugin {
	GDCLASS(GameViewPluginBase, EditorPlugin);

#ifndef ANDROID_ENABLED
	GameView *game_view = nullptr;
#endif // ANDROID_ENABLED
	WindowWrapper *window_wrapper = nullptr;

	Ref<GameViewDebugger> debugger;

	Label *empty_dock_info = nullptr;

	void _window_visibility_changed(bool p_visible);
	void _create_empty_info_label(const String &p_message);

	void _focus_another_editor();
	bool _is_window_wrapper_enabled() const;
	void _dock_visibility_changed(EditorDock *p_dock);

#ifdef ANDROID_ENABLED
	void _on_show_game_dock();
#endif

protected:
	void _notification(int p_what);

	static void _bind_methods();

#ifndef ANDROID_ENABLED
	void setup(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process);
#else
	void setup_android();
#endif

public:
	virtual String get_plugin_name() const override { return "Game"; }
	virtual void edit(Object *p_object) override {}
	virtual bool handles(Object *p_object) const override { return false; }

	Ref<GameViewDebugger> get_debugger() const { return debugger; }

#ifndef ANDROID_ENABLED
	virtual void set_window_layout(Ref<ConfigFile> p_layout) override;
	virtual void get_window_layout(Ref<ConfigFile> p_layout) override;
#endif // ANDROID_ENABLED
	GameViewPluginBase();
};

class GameViewPlugin : public GameViewPluginBase {
	GDCLASS(GameViewPlugin, GameViewPluginBase);

public:
	GameViewPlugin();
};