Path: blob/master/editor/gui/touch_actions_panel.cpp
10277 views
/**************************************************************************/1/* touch_actions_panel.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "touch_actions_panel.h"3132#include "core/input/input.h"33#include "editor/editor_string_names.h"34#include "editor/settings/editor_settings.h"35#include "scene/gui/box_container.h"36#include "scene/gui/button.h"37#include "scene/gui/color_rect.h"38#include "scene/gui/texture_rect.h"39#include "scene/resources/style_box_flat.h"4041void TouchActionsPanel::_notification(int p_what) {42switch (p_what) {43case NOTIFICATION_ENTER_TREE: {44DisplayServer::get_singleton()->set_hardware_keyboard_connection_change_callback(callable_mp(this, &TouchActionsPanel::_hardware_keyboard_connected));45_hardware_keyboard_connected(DisplayServer::get_singleton()->has_hardware_keyboard());46if (!is_floating) {47get_parent()->move_child(this, embedded_panel_index);48}49} break;50case NOTIFICATION_VISIBILITY_CHANGED: {51set_process_input(is_visible_in_tree());52} break;53case NOTIFICATION_THEME_CHANGED: {54if (is_floating) {55drag_handle->set_texture(get_editor_theme_icon(SNAME("DragHandle")));56layout_toggle_button->set_button_icon(get_editor_theme_icon(SNAME("Orientation")));57lock_panel_button->set_button_icon(get_editor_theme_icon(SNAME("Lock")));58} else {59if (embedded_panel_index == 1) {60panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));61} else {62panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));63}64}65save_button->set_button_icon(get_editor_theme_icon(SNAME("Save")));66delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));67undo_button->set_button_icon(get_editor_theme_icon(SNAME("UndoRedo")));68redo_button->set_button_icon(get_editor_theme_icon(SNAME("Redo")));69cut_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCut")));70copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));71paste_button->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));72} break;73}74}7576void TouchActionsPanel::input(const Ref<InputEvent> &event) {77if (ctrl_btn_pressed) {78event->call(SNAME("set_ctrl_pressed"), true);79}8081if (shift_btn_pressed) {82event->call(SNAME("set_shift_pressed"), true);83}8485if (alt_btn_pressed) {86event->call(SNAME("set_alt_pressed"), true);87}88}8990void TouchActionsPanel::_hardware_keyboard_connected(bool p_connected) {91set_visible(!p_connected);92}9394void TouchActionsPanel::_simulate_editor_shortcut(const String &p_shortcut_name) {95Ref<Shortcut> shortcut = ED_GET_SHORTCUT(p_shortcut_name);9697if (shortcut.is_valid() && !shortcut->get_events().is_empty()) {98Ref<InputEventKey> event = shortcut->get_events()[0];99if (event.is_valid()) {100event->set_pressed(true);101Input::get_singleton()->parse_input_event(event);102}103}104}105106void TouchActionsPanel::_simulate_key_press(Key p_keycode) {107Ref<InputEventKey> event;108event.instantiate();109event->set_keycode(p_keycode);110event->set_pressed(true);111Input::get_singleton()->parse_input_event(event);112}113114void TouchActionsPanel::_on_modifier_button_toggled(bool p_pressed, int p_modifier) {115switch ((Modifier)p_modifier) {116case MODIFIER_CTRL:117ctrl_btn_pressed = p_pressed;118break;119case MODIFIER_SHIFT:120shift_btn_pressed = p_pressed;121break;122case MODIFIER_ALT:123alt_btn_pressed = p_pressed;124break;125}126}127128Button *TouchActionsPanel::_add_new_action_button(const String &p_shortcut, const String &p_name, Key p_keycode) {129Button *action_button = memnew(Button);130action_button->set_theme_type_variation("FlatMenuButton");131action_button->set_accessibility_name(p_name);132action_button->set_focus_mode(FOCUS_ACCESSIBILITY);133action_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);134if (p_keycode == Key::NONE) {135action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_editor_shortcut).bind(p_shortcut));136} else {137action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_key_press).bind(p_keycode));138}139box->add_child(action_button);140return action_button;141}142143void TouchActionsPanel::_add_new_modifier_button(Modifier p_modifier) {144String text;145switch (p_modifier) {146case MODIFIER_CTRL:147text = "Ctrl";148break;149case MODIFIER_SHIFT:150text = "Shift";151break;152case MODIFIER_ALT:153text = "Alt";154break;155}156Button *toggle_button = memnew(Button);157toggle_button->set_text(text);158toggle_button->set_toggle_mode(true);159toggle_button->set_theme_type_variation("FlatMenuButton");160toggle_button->set_accessibility_name(text);161toggle_button->set_focus_mode(FOCUS_ACCESSIBILITY);162toggle_button->connect(SceneStringName(toggled), callable_mp(this, &TouchActionsPanel::_on_modifier_button_toggled).bind((int)p_modifier));163box->add_child(toggle_button);164}165166void TouchActionsPanel::_on_drag_handle_gui_input(const Ref<InputEvent> &p_event) {167if (locked_panel) {168return;169}170Ref<InputEventMouseButton> mouse_button_event = p_event;171if (mouse_button_event.is_valid() && mouse_button_event->get_button_index() == MouseButton::LEFT) {172if (mouse_button_event->is_pressed()) {173dragging = true;174drag_offset = mouse_button_event->get_position();175} else {176if (dragging) {177dragging = false;178EditorSettings::get_singleton()->set("_touch_actions_panel_position", get_position());179EditorSettings::get_singleton()->save();180}181}182}183184Ref<InputEventMouseMotion> mouse_motion_event = p_event;185if (dragging && mouse_motion_event.is_valid()) {186Vector2 new_position = get_position() + mouse_motion_event->get_relative();187const float margin = 25.0;188Vector2 parent_size = get_parent_area_size();189Vector2 panel_size = get_size();190new_position = new_position.clamp(Vector2(margin, margin), parent_size - panel_size - Vector2(margin, margin));191set_position(new_position);192}193}194195void TouchActionsPanel::_switch_layout() {196box->set_vertical(!box->is_vertical());197reset_size();198queue_redraw();199EditorSettings::get_singleton()->set("_touch_actions_panel_vertical_layout", box->is_vertical());200EditorSettings::get_singleton()->save();201}202203void TouchActionsPanel::_lock_panel_toggled(bool p_pressed) {204locked_panel = p_pressed;205layout_toggle_button->set_visible(!p_pressed);206drag_handle->set_visible(!p_pressed);207reset_size();208queue_redraw();209}210211void TouchActionsPanel::_switch_embedded_panel_side() {212if (embedded_panel_index == 0) {213embedded_panel_index = 1;214panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));215} else {216embedded_panel_index = 0;217panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));218}219get_parent()->move_child(this, embedded_panel_index); // Parent is a hbox with only two children -- TouchActionsPanel and main Editor UI.220EditorSettings::get_singleton()->set("_touch_actions_panel_embed_index", embedded_panel_index);221EditorSettings::get_singleton()->save();222}223224TouchActionsPanel::TouchActionsPanel() {225int panel_mode = EDITOR_GET("interface/touchscreen/touch_actions_panel");226is_floating = panel_mode == 2;227228if (is_floating) {229Ref<StyleBoxFlat> panel_style;230panel_style.instantiate();231panel_style->set_bg_color(Color(0.1, 0.1, 0.1, 1));232panel_style->set_border_color(Color(0.3, 0.3, 0.3, 1));233panel_style->set_border_width_all(3);234panel_style->set_corner_radius_all(10);235panel_style->set_content_margin_all(12);236add_theme_style_override(SceneStringName(panel), panel_style);237238set_position(EDITOR_DEF("_touch_actions_panel_position", Point2(480, 480))); // Dropped it here for no good reason — users can move it anyway.239}240241box = memnew(BoxContainer);242box->add_theme_constant_override("separation", 20);243if (is_floating) {244box->set_vertical(EDITOR_DEF("_touch_actions_panel_vertical_layout", false));245} else {246box->set_vertical(true);247}248add_child(box);249250if (is_floating) {251drag_handle = memnew(TextureRect);252drag_handle->set_custom_minimum_size(Size2(40, 40));253drag_handle->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);254drag_handle->connect(SceneStringName(gui_input), callable_mp(this, &TouchActionsPanel::_on_drag_handle_gui_input));255box->add_child(drag_handle);256257layout_toggle_button = memnew(Button);258layout_toggle_button->set_theme_type_variation("FlatMenuButton");259layout_toggle_button->set_accessibility_name(TTRC("Switch Layout"));260layout_toggle_button->set_focus_mode(FOCUS_ACCESSIBILITY);261layout_toggle_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);262layout_toggle_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_switch_layout));263box->add_child(layout_toggle_button);264265lock_panel_button = memnew(Button);266lock_panel_button->set_toggle_mode(true);267lock_panel_button->set_theme_type_variation("FlatMenuButton");268lock_panel_button->set_accessibility_name(TTRC("Lock Panel"));269lock_panel_button->set_focus_mode(FOCUS_ACCESSIBILITY);270lock_panel_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);271lock_panel_button->connect(SceneStringName(toggled), callable_mp(this, &TouchActionsPanel::_lock_panel_toggled));272box->add_child(lock_panel_button);273} else {274panel_pos_button = memnew(Button);275panel_pos_button->set_theme_type_variation("FlatMenuButton");276panel_pos_button->set_accessibility_name(TTRC("Switch Embedded Panel Position"));277panel_pos_button->set_focus_mode(FOCUS_ACCESSIBILITY);278panel_pos_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);279panel_pos_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_switch_embedded_panel_side));280box->add_child(panel_pos_button);281282embedded_panel_index = EDITOR_DEF("_touch_actions_panel_embed_index", 0);283}284285ColorRect *separator = memnew(ColorRect);286separator->set_color(Color(0.5, 0.5, 0.5));287separator->set_custom_minimum_size(Size2(2, 2));288box->add_child(separator);289290// Add action buttons.291save_button = _add_new_action_button("editor/save_scene", TTRC("Save"));292delete_button = _add_new_action_button("", TTRC("Delete"), Key::KEY_DELETE);293undo_button = _add_new_action_button("ui_undo", TTRC("Undo"));294redo_button = _add_new_action_button("ui_redo", TTRC("Redo"));295cut_button = _add_new_action_button("ui_cut", TTRC("Cut"));296copy_button = _add_new_action_button("ui_copy", TTRC("Copy"));297paste_button = _add_new_action_button("ui_paste", TTRC("Paste"));298299_add_new_modifier_button(MODIFIER_CTRL);300_add_new_modifier_button(MODIFIER_SHIFT);301_add_new_modifier_button(MODIFIER_ALT);302}303304305