Skip to content

Commit

Permalink
Merge pull request #39364 from Calinou/editor-flags-solo-mode
Browse files Browse the repository at this point in the history
Replace all flags with one value when holding Cmd in the layers editor
  • Loading branch information
akien-mga committed Aug 17, 2023
2 parents 0aa3c2d + 81d603a commit 16b5cd9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
40 changes: 26 additions & 14 deletions editor/editor_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ EditorPropertyFlags::EditorPropertyFlags() {

void EditorPropertyLayersGrid::_rename_pressed(int p_menu) {
// Show rename popup for active layer.
if (renamed_layer_index == -1) {
if (renamed_layer_index == INT32_MAX) {
return;
}
String name = names[renamed_layer_index];
Expand Down Expand Up @@ -902,8 +902,8 @@ void EditorPropertyLayersGrid::_update_hovered(const Vector2 &p_position) {
}

// Remove highlight when no square is hovered.
if (hovered_index != -1) {
hovered_index = -1;
if (hovered_index != INT32_MAX) {
hovered_index = INT32_MAX;
queue_redraw();
}
}
Expand All @@ -913,20 +913,32 @@ void EditorPropertyLayersGrid::_on_hover_exit() {
expand_hovered = false;
queue_redraw();
}
if (hovered_index != -1) {
hovered_index = -1;
if (hovered_index != INT32_MAX) {
hovered_index = INT32_MAX;
queue_redraw();
}
}

void EditorPropertyLayersGrid::_update_flag() {
if (hovered_index >= 0) {
void EditorPropertyLayersGrid::_update_flag(bool p_replace) {
if (hovered_index != INT32_MAX) {
// Toggle the flag.
// We base our choice on the hovered flag, so that it always matches the hovered flag.
if (value & (1 << hovered_index)) {
value &= ~(1 << hovered_index);
if (p_replace) {
// Replace all flags with the hovered flag ("solo mode"),
// instead of toggling the hovered flags while preserving other flags' state.
if (value == uint32_t(1 << hovered_index)) {
// If the flag is already enabled, enable all other items and disable the current flag.
// This allows for quicker toggling.
value = INT32_MAX - (1 << hovered_index);
} else {
value = 1 << hovered_index;
}
} else {
value |= (1 << hovered_index);
if (value & (1 << hovered_index)) {
value &= ~(1 << hovered_index);
} else {
value |= (1 << hovered_index);
}
}

emit_signal(SNAME("flag_changed"), value);
Expand All @@ -951,10 +963,10 @@ void EditorPropertyLayersGrid::gui_input(const Ref<InputEvent> &p_ev) {
const Ref<InputEventMouseButton> mb = p_ev;
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
_update_hovered(mb->get_position());
_update_flag();
_update_flag(mb->is_command_or_control_pressed());
}
if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
if (hovered_index >= 0) {
if (hovered_index != INT32_MAX) {
renamed_layer_index = hovered_index;
layer_rename->set_position(get_screen_position() + mb->get_position());
layer_rename->reset_size();
Expand Down Expand Up @@ -987,7 +999,7 @@ void EditorPropertyLayersGrid::_notification(int p_what) {

const int vofs = (grid_size.height - h) / 2;

int layer_index = 0;
uint32_t layer_index = 0;

Point2 arrow_pos;

Expand Down Expand Up @@ -1242,7 +1254,7 @@ void EditorPropertyLayers::_button_pressed() {
}

void EditorPropertyLayers::_menu_pressed(int p_menu) {
if (p_menu == grid->layer_count) {
if (uint32_t(p_menu) == grid->layer_count) {
ProjectSettingsEditor::get_singleton()->popup_project_settings(true);
ProjectSettingsEditor::get_singleton()->set_general_page(basename);
} else {
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class EditorPropertyLayersGrid : public Control {
bool expand_hovered = false;
bool expanded = false;
int expansion_rows = 0;
int hovered_index = -1;
uint32_t hovered_index = INT32_MAX; // Nothing is hovered.
bool read_only = false;
int renamed_layer_index = -1;
PopupMenu *layer_rename = nullptr;
Expand All @@ -275,7 +275,7 @@ class EditorPropertyLayersGrid : public Control {
void _rename_operation_confirm();
void _update_hovered(const Vector2 &p_position);
void _on_hover_exit();
void _update_flag();
void _update_flag(bool p_replace);
Size2 get_grid_size() const;

protected:
Expand All @@ -285,7 +285,7 @@ class EditorPropertyLayersGrid : public Control {
public:
uint32_t value = 0;
int layer_group_size = 0;
int layer_count = 0;
uint32_t layer_count = 0;
Vector<String> names;
Vector<String> tooltips;

Expand Down

0 comments on commit 16b5cd9

Please sign in to comment.