diff options
Diffstat (limited to 'scene/gui/popup_menu.cpp')
-rw-r--r-- | scene/gui/popup_menu.cpp | 148 |
1 files changed, 98 insertions, 50 deletions
diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index c5c6305315..b439d85983 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -40,10 +40,11 @@ String PopupMenu::_get_accel_text(int p_item) const { ERR_FAIL_INDEX_V(p_item, items.size(), String()); - if (items[p_item].shortcut.is_valid()) + if (items[p_item].shortcut.is_valid()) { return items[p_item].shortcut->get_as_text(); - else if (items[p_item].accel) + } else if (items[p_item].accel) { return keycode_get_string(items[p_item].accel); + } return String(); } @@ -73,13 +74,15 @@ Size2 PopupMenu::_get_contents_minimum_size() const { size.width += items[i].h_ofs; - if (items[i].checkable_type) + if (items[i].checkable_type) { has_check = true; + } String text = items[i].xl_text; size.width += font->get_string_size(text).width; - if (i > 0) + if (i > 0) { size.height += vseparation; + } if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) { int accel_w = hseparation * 2; @@ -87,8 +90,9 @@ Size2 PopupMenu::_get_contents_minimum_size() const { accel_max_w = MAX(accel_w, accel_max_w); } - if (items[i].submenu != "") + if (items[i].submenu != "") { size.width += get_theme_icon("submenu")->get_width(); + } max_w = MAX(max_w, size.width); @@ -96,22 +100,25 @@ Size2 PopupMenu::_get_contents_minimum_size() const { } minsize.width += max_w + icon_w + accel_max_w; - if (has_check) + if (has_check) { minsize.width += check_w; + } return minsize; } int PopupMenu::_get_mouse_over(const Point2 &p_over) const { - if (p_over.x < 0 || p_over.x >= get_size().width) + if (p_over.x < 0 || p_over.x >= get_size().width) { return -1; + } Ref<StyleBox> style = get_theme_stylebox("panel"); Point2 ofs = style->get_offset(); - if (ofs.y > p_over.y) + if (ofs.y > p_over.y) { return -1; + } Ref<Font> font = get_theme_font("font"); int vseparation = get_theme_constant("vseparation"); @@ -142,8 +149,9 @@ void PopupMenu::_activate_submenu(int over) { ERR_FAIL_COND_MSG(!n, "Item subnode does not exist: " + items[over].submenu + "."); Popup *pm = Object::cast_to<Popup>(n); ERR_FAIL_COND_MSG(!pm, "Item subnode is not a Popup: " + items[over].submenu + "."); - if (pm->is_visible()) + if (pm->is_visible()) { return; //already visible! + } Point2 p = get_position(); Rect2 pr(p, get_size()); @@ -152,8 +160,9 @@ void PopupMenu::_activate_submenu(int over) { Point2 pos = p + Point2(get_size().width, items[over]._ofs_cache - style->get_offset().y); Size2 size = pm->get_size(); // fix pos - if (pos.x + size.width > get_parent_rect().size.width) + if (pos.x + size.width > get_parent_rect().size.width) { pos.x = p.x - size.width; + } pm->set_position(pos); // pm->set_scale(get_global_transform().get_scale()); @@ -175,8 +184,9 @@ void PopupMenu::_submenu_timeout() { //if (!has_focus()) { // return; //do not activate if not has focus //} - if (mouse_over == submenu_over) + if (mouse_over == submenu_over) { _activate_submenu(mouse_over); + } submenu_over = -1; } @@ -214,12 +224,14 @@ void PopupMenu::_scroll(float p_factor, const Point2 &p_over) { void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { if (p_event->is_action("ui_down") && p_event->is_pressed()) { int search_from = mouse_over + 1; - if (search_from >= items.size()) + if (search_from >= items.size()) { search_from = 0; + } for (int i = search_from; i < items.size(); i++) { - if (i < 0 || i >= items.size()) + if (i < 0 || i >= items.size()) { continue; + } if (!items[i].separator && !items[i].disabled) { mouse_over = i; @@ -231,12 +243,14 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { } } else if (p_event->is_action("ui_up") && p_event->is_pressed()) { int search_from = mouse_over - 1; - if (search_from < 0) + if (search_from < 0) { search_from = items.size() - 1; + } for (int i = search_from; i >= 0; i--) { - if (i >= items.size()) + if (i >= items.size()) { continue; + } if (!items[i].separator && !items[i].disabled) { mouse_over = i; @@ -271,8 +285,9 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> b = p_event; if (b.is_valid()) { - if (b->is_pressed()) + if (b->is_pressed()) { return; + } int button_idx = b->get_button_index(); switch (button_idx) { @@ -302,8 +317,9 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { break; //non-activable } - if (items[over].separator || items[over].disabled) + if (items[over].separator || items[over].disabled) { break; + } if (items[over].submenu != "") { _activate_submenu(over); @@ -322,8 +338,9 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { if (m.is_valid()) { if (invalidated_click) { moved += m->get_relative(); - if (moved.length() > 4) + if (moved.length() > 4) { invalidated_click = false; + } } for (List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) { @@ -370,19 +387,22 @@ void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { search_string = ""; } - if (String::chr(k->get_unicode()) != search_string) + if (String::chr(k->get_unicode()) != search_string) { search_string += String::chr(k->get_unicode()); + } for (int i = mouse_over + 1; i <= items.size(); i++) { if (i == items.size()) { - if (mouse_over <= 0) + if (mouse_over <= 0) { break; - else + } else { i = 0; + } } - if (i == mouse_over) + if (i == mouse_over) { break; + } if (items[i].text.findn(search_string) == 0) { mouse_over = i; @@ -424,22 +444,27 @@ void PopupMenu::_draw() { float icon_ofs = 0.0; bool has_check = false; for (int i = 0; i < items.size(); i++) { - if (!items[i].icon.is_null()) + if (!items[i].icon.is_null()) { icon_ofs = MAX(items[i].icon->get_size().width, icon_ofs); + } - if (items[i].checkable_type) + if (items[i].checkable_type) { has_check = true; + } } - if (icon_ofs > 0.0) + if (icon_ofs > 0.0) { icon_ofs += hseparation; + } float check_ofs = 0.0; - if (has_check) + if (has_check) { check_ofs = MAX(get_theme_icon("checked")->get_width(), get_theme_icon("radio_checked")->get_width()) + hseparation; + } for (int i = 0; i < items.size(); i++) { - if (i > 0) + if (i > 0) { ofs.y += vseparation; + } Point2 item_ofs = ofs; Size2 icon_size; float h; @@ -570,16 +595,19 @@ void PopupMenu::_notification(int p_what) { } for (int i = 0; i < items.size(); i++) { - if (items[i].submenu == "") + if (items[i].submenu == "") { continue; + } Node *n = get_node(items[i].submenu); - if (!n) + if (!n) { continue; + } PopupMenu *pm = Object::cast_to<PopupMenu>(n); - if (!pm || !pm->is_visible()) + if (!pm || !pm->is_visible()) { continue; + } pm->hide(); } @@ -826,8 +854,9 @@ String PopupMenu::get_item_text(int p_idx) const { int PopupMenu::get_item_idx_from_text(const String &text) const { for (int idx = 0; idx < items.size(); idx++) { - if (items[idx].text == text) + if (items[idx].text == text) { return idx; + } } return -1; @@ -865,8 +894,9 @@ int PopupMenu::get_item_id(int p_idx) const { int PopupMenu::get_item_index(int p_id) const { for (int i = 0; i < items.size(); i++) { - if (items[i].id == p_id) + if (items[i].id == p_id) { return i; + } } return -1; @@ -962,8 +992,9 @@ void PopupMenu::toggle_item_multistate(int p_idx) { } ++items.write[p_idx].state; - if (items.write[p_idx].max_states <= items[p_idx].state) + if (items.write[p_idx].max_states <= items[p_idx].state) { items.write[p_idx].state = 0; + } control->update(); } @@ -997,21 +1028,27 @@ bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_fo if (k.is_valid()) { code = k->get_keycode(); - if (code == 0) + if (code == 0) { code = k->get_unicode(); - if (k->get_control()) + } + if (k->get_control()) { code |= KEY_MASK_CTRL; - if (k->get_alt()) + } + if (k->get_alt()) { code |= KEY_MASK_ALT; - if (k->get_metakey()) + } + if (k->get_metakey()) { code |= KEY_MASK_META; - if (k->get_shift()) + } + if (k->get_shift()) { code |= KEY_MASK_SHIFT; + } } for (int i = 0; i < items.size(); i++) { - if (is_item_disabled(i) || items[i].shortcut_is_disabled) + if (is_item_disabled(i) || items[i].shortcut_is_disabled) { continue; + } if (items[i].shortcut.is_valid() && items[i].shortcut->is_shortcut(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) { activate_item(i); @@ -1025,12 +1062,14 @@ bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_fo if (items[i].submenu != "") { Node *n = get_node(items[i].submenu); - if (!n) + if (!n) { continue; + } PopupMenu *pm = Object::cast_to<PopupMenu>(n); - if (!pm) + if (!pm) { continue; + } if (pm->activate_item_by_event(p_event, p_for_global_only)) { return true; @@ -1053,13 +1092,16 @@ void PopupMenu::activate_item(int p_item) { // with hide_on_item_selection enabled if (items[p_item].checkable_type) { - if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) + if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) { break; + } } else if (0 < items[p_item].max_states) { - if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) + if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) { break; - } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) + } + } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) { break; + } pop->hide(); next = next->get_parent(); @@ -1072,13 +1114,16 @@ void PopupMenu::activate_item(int p_item) { bool need_hide = true; if (items[p_item].checkable_type) { - if (!hide_on_checkable_item_selection) + if (!hide_on_checkable_item_selection) { need_hide = false; + } } else if (0 < items[p_item].max_states) { - if (!hide_on_multistate_item_selection) + if (!hide_on_multistate_item_selection) { need_hide = false; - } else if (!hide_on_item_selection) + } + } else if (!hide_on_item_selection) { need_hide = false; + } emit_signal("id_pressed", id); emit_signal("index_pressed", p_item); @@ -1228,8 +1273,9 @@ bool PopupMenu::is_hide_on_multistate_item_selection() const { } void PopupMenu::set_submenu_popup_delay(float p_time) { - if (p_time <= 0) + if (p_time <= 0) { p_time = 0.01; + } submenu_timer->set_wait_time(p_time); } @@ -1248,8 +1294,9 @@ bool PopupMenu::get_allow_search() const { String PopupMenu::get_tooltip(const Point2 &p_pos) const { int over = _get_mouse_over(p_pos); - if (over < 0 || over >= items.size()) + if (over < 0 || over >= items.size()) { return ""; + } return items[over].tooltip; } @@ -1259,8 +1306,9 @@ void PopupMenu::set_parent_rect(const Rect2 &p_rect) { void PopupMenu::get_translatable_strings(List<String> *p_strings) const { for (int i = 0; i < items.size(); i++) { - if (items[i].xl_text != "") + if (items[i].xl_text != "") { p_strings->push_back(items[i].xl_text); + } } } |