Merge pull request #57848 from timothyqiu/2d-grid-options

This commit is contained in:
Rémi Verschelde 2022-03-15 08:57:41 +01:00 committed by GitHub
commit 32179d7732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 19 deletions

View File

@ -489,12 +489,12 @@ void CanvasItemEditor::unhandled_key_input(const Ref<InputEvent> &p_ev) {
viewport->update();
}
if (k->is_pressed() && !k->is_ctrl_pressed() && !k->is_echo()) {
if ((grid_snap_active || show_grid) && multiply_grid_step_shortcut.is_valid() && multiply_grid_step_shortcut->matches_event(p_ev)) {
if (k->is_pressed() && !k->is_ctrl_pressed() && !k->is_echo() && (grid_snap_active || _is_grid_visible())) {
if (multiply_grid_step_shortcut.is_valid() && multiply_grid_step_shortcut->matches_event(p_ev)) {
// Multiply the grid size
grid_step_multiplier = MIN(grid_step_multiplier + 1, 12);
viewport->update();
} else if ((grid_snap_active || show_grid) && divide_grid_step_shortcut.is_valid() && divide_grid_step_shortcut->matches_event(p_ev)) {
} else if (divide_grid_step_shortcut.is_valid() && divide_grid_step_shortcut->matches_event(p_ev)) {
// Divide the grid size
Point2 new_grid_step = grid_step * Math::pow(2.0, grid_step_multiplier - 1);
if (new_grid_step.x >= 1.0 && new_grid_step.y >= 1.0) {
@ -938,6 +938,60 @@ void CanvasItemEditor::_reset_create_position() {
node_create_position = Point2();
}
bool CanvasItemEditor::_is_grid_visible() const {
switch (grid_visibility) {
case GRID_VISIBILITY_SHOW:
return true;
case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
return grid_snap_active;
case GRID_VISIBILITY_HIDE:
return false;
}
ERR_FAIL_V_MSG(true, "Unexpected grid_visibility value");
}
void CanvasItemEditor::_prepare_grid_menu() {
for (int i = GRID_VISIBILITY_SHOW; i <= GRID_VISIBILITY_HIDE; i++) {
grid_menu->set_item_checked(i, i == grid_visibility);
}
}
void CanvasItemEditor::_on_grid_menu_id_pressed(int p_id) {
switch (p_id) {
case GRID_VISIBILITY_SHOW:
case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
case GRID_VISIBILITY_HIDE:
grid_visibility = (GridVisibility)p_id;
viewport->update();
view_menu->get_popup()->hide();
return;
}
// Toggle grid: go to the least restrictive option possible.
if (grid_snap_active) {
switch (grid_visibility) {
case GRID_VISIBILITY_SHOW:
case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
grid_visibility = GRID_VISIBILITY_HIDE;
break;
case GRID_VISIBILITY_HIDE:
grid_visibility = GRID_VISIBILITY_SHOW_WHEN_SNAPPING;
break;
}
} else {
switch (grid_visibility) {
case GRID_VISIBILITY_SHOW:
grid_visibility = GRID_VISIBILITY_SHOW_WHEN_SNAPPING;
break;
case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
case GRID_VISIBILITY_HIDE:
grid_visibility = GRID_VISIBILITY_SHOW;
break;
}
}
viewport->update();
}
bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> b = p_event;
Ref<InputEventMouseMotion> m = p_event;
@ -2728,7 +2782,7 @@ void CanvasItemEditor::_draw_rulers() {
// The rule transform
Transform2D ruler_transform = Transform2D();
if (show_grid || grid_snap_active) {
if (grid_snap_active || _is_grid_visible()) {
List<CanvasItem *> selection = _get_edited_canvas_items();
if (snap_relative && selection.size() > 0) {
ruler_transform.translate(_get_encompassing_rect_from_list(selection).position);
@ -2808,7 +2862,7 @@ void CanvasItemEditor::_draw_rulers() {
}
void CanvasItemEditor::_draw_grid() {
if (show_grid || grid_snap_active) {
if (_is_grid_visible()) {
// Draw the grid
Vector2 real_grid_offset;
const List<CanvasItem *> selection = _get_edited_canvas_items();
@ -4139,12 +4193,6 @@ void CanvasItemEditor::_update_override_camera_button(bool p_game_running) {
void CanvasItemEditor::_popup_callback(int p_op) {
last_option = MenuOption(p_op);
switch (p_op) {
case SHOW_GRID: {
show_grid = !show_grid;
int idx = view_menu->get_popup()->get_item_index(SHOW_GRID);
view_menu->get_popup()->set_item_checked(idx, show_grid);
viewport->update();
} break;
case SHOW_ORIGIN: {
show_origin = !show_origin;
int idx = view_menu->get_popup()->get_item_index(SHOW_ORIGIN);
@ -4617,7 +4665,7 @@ Dictionary CanvasItemEditor::get_state() const {
state["snap_node_center"] = snap_node_center;
state["snap_other_nodes"] = snap_other_nodes;
state["snap_guides"] = snap_guides;
state["show_grid"] = show_grid;
state["grid_visibility"] = grid_visibility;
state["show_origin"] = show_origin;
state["show_viewport"] = show_viewport;
state["show_rulers"] = show_rulers;
@ -4719,10 +4767,8 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
smartsnap_config_popup->set_item_checked(idx, snap_guides);
}
if (state.has("show_grid")) {
show_grid = state["show_grid"];
int idx = view_menu->get_popup()->get_item_index(SHOW_GRID);
view_menu->get_popup()->set_item_checked(idx, show_grid);
if (state.has("grid_visibility")) {
grid_visibility = (GridVisibility)(int)(state["grid_visibility"]);
}
if (state.has("show_origin")) {
@ -5132,7 +5178,19 @@ CanvasItemEditor::CanvasItemEditor() {
p = view_menu->get_popup();
p->set_hide_on_checkable_item_selection(false);
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_grid", TTR("Always Show Grid"), Key::NUMBERSIGN), SHOW_GRID);
grid_menu = memnew(PopupMenu);
grid_menu->connect("about_to_popup", callable_mp(this, &CanvasItemEditor::_prepare_grid_menu));
grid_menu->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_on_grid_menu_id_pressed));
grid_menu->set_name("GridMenu");
grid_menu->add_radio_check_item(TTR("Show"), GRID_VISIBILITY_SHOW);
grid_menu->add_radio_check_item(TTR("Show When Snapping"), GRID_VISIBILITY_SHOW_WHEN_SNAPPING);
grid_menu->add_radio_check_item(TTR("Hide"), GRID_VISIBILITY_HIDE);
grid_menu->add_separator();
grid_menu->add_shortcut(ED_SHORTCUT("canvas_item_editor/toggle_grid", TTR("Toggle Grid"), KeyModifierMask::CMD | Key::APOSTROPHE));
p->add_child(grid_menu);
p->add_submenu_item(TTR("Grid"), "GridMenu");
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_helpers", TTR("Show Helpers"), Key::H), SHOW_HELPERS);
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_rulers", TTR("Show Rulers")), SHOW_RULERS);
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_guides", TTR("Show Guides"), Key::Y), SHOW_GUIDES);

View File

@ -116,7 +116,6 @@ private:
SNAP_RELATIVE,
SNAP_CONFIGURE,
SNAP_USE_PIXEL,
SHOW_GRID,
SHOW_HELPERS,
SHOW_RULERS,
SHOW_GUIDES,
@ -175,6 +174,12 @@ private:
DRAG_KEY_MOVE
};
enum GridVisibility {
GRID_VISIBILITY_SHOW,
GRID_VISIBILITY_SHOW_WHEN_SNAPPING,
GRID_VISIBILITY_HIDE,
};
bool selection_menu_additive_selection;
Tool tool = TOOL_SELECT;
@ -190,7 +195,7 @@ private:
HBoxContainer *hbc_context_menu;
Transform2D transform;
bool show_grid = false;
GridVisibility grid_visibility = GRID_VISIBILITY_SHOW_WHEN_SNAPPING;
bool show_rulers = true;
bool show_guides = true;
bool show_origin = true;
@ -314,6 +319,7 @@ private:
MenuButton *skeleton_menu;
Button *override_camera_button;
MenuButton *view_menu;
PopupMenu *grid_menu;
HBoxContainer *animation_hb;
MenuButton *animation_menu;
@ -390,6 +396,9 @@ private:
void _node_created(Node *p_node);
void _reset_create_position();
void _update_editor_settings();
bool _is_grid_visible() const;
void _prepare_grid_menu();
void _on_grid_menu_id_pressed(int p_id);
UndoRedo *undo_redo;