Skip to content

Commit

Permalink
Floating Panel and save button
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaxerror247 committed Dec 13, 2024
1 parent b1fc02d commit d698481
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
9 changes: 9 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
#include "scene/gui/rich_text_label.h"
#include "scene/gui/split_container.h"
#include "scene/gui/tab_container.h"
#ifdef ANDROID_ENABLED
#include "editor/gui/touch_actions_panel.h"
#endif
#include "scene/main/window.h"
#include "scene/property_utils.h"
#include "scene/resources/image_texture.h"
Expand Down Expand Up @@ -7601,6 +7604,12 @@ EditorNode::EditorNode() {

_update_layouts_menu();

#ifdef ANDROID_ENABLED
// Add floating TouchActionsPanel.
TouchActionsPanel *touch_actions_panel = memnew(TouchActionsPanel);
add_child(touch_actions_panel);
#endif

// Bottom panels.

bottom_panel = memnew(EditorBottomPanel);
Expand Down
9 changes: 0 additions & 9 deletions editor/gui/editor_bottom_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
#include "editor/editor_string_names.h"
#include "editor/gui/editor_toaster.h"
#include "editor/gui/editor_version_button.h"
#ifdef ANDROID_ENABLED
#include "editor/gui/touch_actions_panel.h"
#endif
#include "editor/themes/editor_scale.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
Expand Down Expand Up @@ -266,12 +263,6 @@ EditorBottomPanel::EditorBottomPanel() {
button_hbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
bottom_hbox->add_child(button_hbox);

#ifdef ANDROID_ENABLED
TouchActionsPanel *touch_actions_panel = memnew(TouchActionsPanel);
touch_actions_panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);
bottom_hbox->add_child(touch_actions_panel);
#endif

editor_toaster = memnew(EditorToaster);
bottom_hbox->add_child(editor_toaster);

Expand Down
72 changes: 71 additions & 1 deletion editor/gui/touch_actions_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@
#include "touch_actions_panel.h"
#include "core/input/input.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "scene/resources/style_box_flat.h"

void TouchActionsPanel::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
drag_handle->set_texture(get_editor_theme_icon(SNAME("DragHandle")));
save_button->set_button_icon(get_editor_theme_icon(SNAME("Save")));
undo_button->set_button_icon(get_editor_theme_icon(SNAME("UndoRedo")));
redo_button->set_button_icon(get_editor_theme_icon(SNAME("UndoRedo")));
} break;
default:
break;
}
}

void TouchActionsPanel::_simulate_action(const String &action_name) {
Ref<Shortcut> shortcut = ED_GET_SHORTCUT(action_name);
Expand All @@ -44,12 +59,66 @@ void TouchActionsPanel::_simulate_action(const String &action_name) {
}
}

void TouchActionsPanel::_on_drag_handle_gui_input(const Ref<InputEvent> &event) {
Ref<InputEventMouseButton> mouse_button_event = event;
if (mouse_button_event.is_valid() && mouse_button_event->get_button_index() == MouseButton::LEFT) {
if (mouse_button_event->is_pressed()) {
dragging = true;
drag_offset = mouse_button_event->get_position();
} else {
dragging = false;
}
}

Ref<InputEventMouseMotion> mouse_motion_event = event;
if (mouse_motion_event.is_valid() && dragging) {
Vector2 new_position = get_position() + mouse_motion_event->get_relative();
// Clamp the position to parent bounds
Vector2 parent_size = get_parent_area_size();
Vector2 panel_size = get_size();
new_position.x = CLAMP(new_position.x, 0, parent_size.x - panel_size.x);
new_position.y = CLAMP(new_position.y, 0, parent_size.y - panel_size.y);
set_position(new_position);
}
}

TouchActionsPanel::TouchActionsPanel() {
dragging = false;

//set_custom_minimum_size(Size2(300, 50));

// Add a StyleBoxFlat for padding and rounded corners
Ref<StyleBoxFlat> panel_style = memnew(StyleBoxFlat);
panel_style->set_bg_color(Color(0.1, 0.1, 0.1, 0.95));
panel_style->set_border_color(Color(0.3, 0.3, 0.3, 1));
panel_style->set_border_width_all(3);
panel_style->set_corner_radius_all(10);
panel_style->set_content_margin_all(12);
add_theme_style_override("panel", panel_style);

//set_anchors_preset(Control::PRESET_CENTER_BOTTOM);

hbox = memnew(HBoxContainer);
hbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
hbox->add_theme_constant_override("separation", 30);
hbox->add_theme_constant_override("separation", 10);
add_child(hbox);

// Create drag handle
drag_handle = memnew(TextureRect);
drag_handle->set_custom_minimum_size(Size2(50, 30));
drag_handle->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
drag_handle->connect(SceneStringName(gui_input), callable_mp(this, &TouchActionsPanel::_on_drag_handle_gui_input));
hbox->add_child(drag_handle);

// Add a dummy control node for horizontal spacing.
Control *spacer = memnew(Control);
hbox->add_child(spacer);

save_button = memnew(Button);
save_button->set_focus_mode(Control::FOCUS_NONE);
save_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_action).bind("editor/save_scene"));
hbox->add_child(save_button);

undo_button = memnew(Button);
undo_button->set_text(TTR("Undo"));
undo_button->set_focus_mode(Control::FOCUS_NONE);
Expand All @@ -59,6 +128,7 @@ TouchActionsPanel::TouchActionsPanel() {
redo_button = memnew(Button);
redo_button->set_text(TTR("Redo"));
redo_button->set_focus_mode(Control::FOCUS_NONE);
redo_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
redo_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_action).bind("ui_redo"));
hbox->add_child(redo_button);
}
9 changes: 9 additions & 0 deletions editor/gui/touch_actions_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,25 @@
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/panel_container.h"
#include "scene/gui/texture_rect.h"

class TouchActionsPanel : public PanelContainer {
GDCLASS(TouchActionsPanel, PanelContainer);

private:
HBoxContainer *hbox;
Button *save_button;
Button *undo_button;
Button *redo_button;
TextureRect *drag_handle;

bool dragging;
Vector2 drag_offset;

void _notification(int p_what);

void _simulate_action(const String &action_name);
void _on_drag_handle_gui_input(const Ref<InputEvent> &event);

public:
TouchActionsPanel();
Expand Down
1 change: 1 addition & 0 deletions editor/icons/DragHandle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d698481

Please sign in to comment.