| 1 | /**************************************************************************/ |
| 2 | /* item_list.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef ITEM_LIST_H |
| 32 | #define ITEM_LIST_H |
| 33 | |
| 34 | #include "scene/gui/control.h" |
| 35 | #include "scene/gui/scroll_bar.h" |
| 36 | #include "scene/resources/text_paragraph.h" |
| 37 | |
| 38 | class ItemList : public Control { |
| 39 | GDCLASS(ItemList, Control); |
| 40 | |
| 41 | public: |
| 42 | enum IconMode { |
| 43 | ICON_MODE_TOP, |
| 44 | ICON_MODE_LEFT |
| 45 | }; |
| 46 | |
| 47 | enum SelectMode { |
| 48 | SELECT_SINGLE, |
| 49 | SELECT_MULTI |
| 50 | }; |
| 51 | |
| 52 | private: |
| 53 | struct Item { |
| 54 | Ref<Texture2D> icon; |
| 55 | bool icon_transposed = false; |
| 56 | Rect2i icon_region; |
| 57 | Color icon_modulate = Color(1, 1, 1, 1); |
| 58 | Ref<Texture2D> tag_icon; |
| 59 | String text; |
| 60 | Ref<TextParagraph> text_buf; |
| 61 | String language; |
| 62 | TextDirection text_direction = TEXT_DIRECTION_AUTO; |
| 63 | |
| 64 | bool selectable = true; |
| 65 | bool selected = false; |
| 66 | bool disabled = false; |
| 67 | bool tooltip_enabled = true; |
| 68 | Variant metadata; |
| 69 | String tooltip; |
| 70 | Color custom_fg; |
| 71 | Color custom_bg = Color(0.0, 0.0, 0.0, 0.0); |
| 72 | |
| 73 | int column = 0; |
| 74 | Rect2 rect_cache; |
| 75 | Rect2 min_rect_cache; |
| 76 | |
| 77 | Size2 get_icon_size() const; |
| 78 | |
| 79 | bool operator<(const Item &p_another) const { return text < p_another.text; } |
| 80 | |
| 81 | Item() { |
| 82 | text_buf.instantiate(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | int current = -1; |
| 87 | int hovered = -1; |
| 88 | |
| 89 | bool shape_changed = true; |
| 90 | |
| 91 | bool ensure_selected_visible = false; |
| 92 | bool same_column_width = false; |
| 93 | bool allow_search = true; |
| 94 | |
| 95 | bool auto_height = false; |
| 96 | float auto_height_value = 0.0; |
| 97 | |
| 98 | Vector<Item> items; |
| 99 | Vector<int> separators; |
| 100 | |
| 101 | SelectMode select_mode = SELECT_SINGLE; |
| 102 | IconMode icon_mode = ICON_MODE_LEFT; |
| 103 | VScrollBar *scroll_bar = nullptr; |
| 104 | TextServer::OverrunBehavior text_overrun_behavior = TextServer::OVERRUN_TRIM_ELLIPSIS; |
| 105 | |
| 106 | uint64_t search_time_msec = 0; |
| 107 | String search_string; |
| 108 | |
| 109 | int current_columns = 1; |
| 110 | int fixed_column_width = 0; |
| 111 | int max_text_lines = 1; |
| 112 | int max_columns = 1; |
| 113 | |
| 114 | Size2 fixed_icon_size; |
| 115 | Size2 max_item_size_cache; |
| 116 | |
| 117 | int defer_select_single = -1; |
| 118 | bool allow_rmb_select = false; |
| 119 | bool allow_reselect = false; |
| 120 | |
| 121 | real_t icon_scale = 1.0; |
| 122 | |
| 123 | bool do_autoscroll_to_bottom = false; |
| 124 | |
| 125 | struct ThemeCache { |
| 126 | int h_separation = 0; |
| 127 | int v_separation = 0; |
| 128 | |
| 129 | Ref<StyleBox> panel_style; |
| 130 | Ref<StyleBox> focus_style; |
| 131 | |
| 132 | Ref<Font> font; |
| 133 | int font_size = 0; |
| 134 | Color font_color; |
| 135 | Color font_hovered_color; |
| 136 | Color font_selected_color; |
| 137 | int font_outline_size = 0; |
| 138 | Color font_outline_color; |
| 139 | |
| 140 | int line_separation = 0; |
| 141 | int icon_margin = 0; |
| 142 | Ref<StyleBox> hovered_style; |
| 143 | Ref<StyleBox> selected_style; |
| 144 | Ref<StyleBox> selected_focus_style; |
| 145 | Ref<StyleBox> cursor_style; |
| 146 | Ref<StyleBox> cursor_focus_style; |
| 147 | Color guide_color; |
| 148 | } theme_cache; |
| 149 | |
| 150 | void _scroll_changed(double); |
| 151 | void _check_shape_changed(); |
| 152 | void _shape_text(int p_idx); |
| 153 | void _mouse_exited(); |
| 154 | |
| 155 | protected: |
| 156 | void _notification(int p_what); |
| 157 | bool _set(const StringName &p_name, const Variant &p_value); |
| 158 | bool _get(const StringName &p_name, Variant &r_ret) const; |
| 159 | void _get_property_list(List<PropertyInfo> *p_list) const; |
| 160 | static void _bind_methods(); |
| 161 | |
| 162 | public: |
| 163 | virtual void gui_input(const Ref<InputEvent> &p_event) override; |
| 164 | |
| 165 | int add_item(const String &p_item, const Ref<Texture2D> &p_texture = Ref<Texture2D>(), bool p_selectable = true); |
| 166 | int add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable = true); |
| 167 | |
| 168 | void set_item_text(int p_idx, const String &p_text); |
| 169 | String get_item_text(int p_idx) const; |
| 170 | |
| 171 | void set_item_text_direction(int p_idx, TextDirection p_text_direction); |
| 172 | TextDirection get_item_text_direction(int p_idx) const; |
| 173 | |
| 174 | void set_item_language(int p_idx, const String &p_language); |
| 175 | String get_item_language(int p_idx) const; |
| 176 | |
| 177 | void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon); |
| 178 | Ref<Texture2D> get_item_icon(int p_idx) const; |
| 179 | |
| 180 | void set_item_icon_transposed(int p_idx, const bool transposed); |
| 181 | bool is_item_icon_transposed(int p_idx) const; |
| 182 | |
| 183 | void set_item_icon_region(int p_idx, const Rect2 &p_region); |
| 184 | Rect2 get_item_icon_region(int p_idx) const; |
| 185 | |
| 186 | void set_item_icon_modulate(int p_idx, const Color &p_modulate); |
| 187 | Color get_item_icon_modulate(int p_idx) const; |
| 188 | |
| 189 | void set_item_selectable(int p_idx, bool p_selectable); |
| 190 | bool is_item_selectable(int p_idx) const; |
| 191 | |
| 192 | void set_item_disabled(int p_idx, bool p_disabled); |
| 193 | bool is_item_disabled(int p_idx) const; |
| 194 | |
| 195 | void set_item_metadata(int p_idx, const Variant &p_metadata); |
| 196 | Variant get_item_metadata(int p_idx) const; |
| 197 | |
| 198 | void set_item_tag_icon(int p_idx, const Ref<Texture2D> &p_tag_icon); |
| 199 | |
| 200 | void set_item_tooltip_enabled(int p_idx, const bool p_enabled); |
| 201 | bool is_item_tooltip_enabled(int p_idx) const; |
| 202 | |
| 203 | void set_item_tooltip(int p_idx, const String &p_tooltip); |
| 204 | String get_item_tooltip(int p_idx) const; |
| 205 | |
| 206 | void set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color); |
| 207 | Color get_item_custom_bg_color(int p_idx) const; |
| 208 | |
| 209 | void set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color); |
| 210 | Color get_item_custom_fg_color(int p_idx) const; |
| 211 | |
| 212 | Rect2 get_item_rect(int p_idx, bool p_expand = true) const; |
| 213 | |
| 214 | void set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior); |
| 215 | TextServer::OverrunBehavior get_text_overrun_behavior() const; |
| 216 | |
| 217 | void select(int p_idx, bool p_single = true); |
| 218 | void deselect(int p_idx); |
| 219 | void deselect_all(); |
| 220 | bool is_selected(int p_idx) const; |
| 221 | Vector<int> get_selected_items(); |
| 222 | bool is_anything_selected(); |
| 223 | |
| 224 | void set_current(int p_current); |
| 225 | int get_current() const; |
| 226 | |
| 227 | void move_item(int p_from_idx, int p_to_idx); |
| 228 | |
| 229 | void set_item_count(int p_count); |
| 230 | int get_item_count() const; |
| 231 | void remove_item(int p_idx); |
| 232 | |
| 233 | void clear(); |
| 234 | |
| 235 | void set_fixed_column_width(int p_size); |
| 236 | int get_fixed_column_width() const; |
| 237 | |
| 238 | void set_same_column_width(bool p_enable); |
| 239 | bool is_same_column_width() const; |
| 240 | |
| 241 | void set_max_text_lines(int p_lines); |
| 242 | int get_max_text_lines() const; |
| 243 | |
| 244 | void set_max_columns(int p_amount); |
| 245 | int get_max_columns() const; |
| 246 | |
| 247 | void set_select_mode(SelectMode p_mode); |
| 248 | SelectMode get_select_mode() const; |
| 249 | |
| 250 | void set_icon_mode(IconMode p_mode); |
| 251 | IconMode get_icon_mode() const; |
| 252 | |
| 253 | void set_fixed_icon_size(const Size2i &p_size); |
| 254 | Size2i get_fixed_icon_size() const; |
| 255 | |
| 256 | void set_allow_rmb_select(bool p_allow); |
| 257 | bool get_allow_rmb_select() const; |
| 258 | |
| 259 | void set_allow_reselect(bool p_allow); |
| 260 | bool get_allow_reselect() const; |
| 261 | |
| 262 | void set_allow_search(bool p_allow); |
| 263 | bool get_allow_search() const; |
| 264 | |
| 265 | void ensure_current_is_visible(); |
| 266 | |
| 267 | void sort_items_by_text(); |
| 268 | int find_metadata(const Variant &p_metadata) const; |
| 269 | |
| 270 | virtual String get_tooltip(const Point2 &p_pos) const override; |
| 271 | int get_item_at_position(const Point2 &p_pos, bool p_exact = false) const; |
| 272 | bool is_pos_at_end_of_items(const Point2 &p_pos) const; |
| 273 | |
| 274 | void set_icon_scale(real_t p_scale); |
| 275 | real_t get_icon_scale() const; |
| 276 | |
| 277 | void set_auto_height(bool p_enable); |
| 278 | bool has_auto_height() const; |
| 279 | |
| 280 | Size2 get_minimum_size() const override; |
| 281 | |
| 282 | void set_autoscroll_to_bottom(const bool p_enable); |
| 283 | |
| 284 | VScrollBar *get_v_scroll_bar() { return scroll_bar; } |
| 285 | |
| 286 | ItemList(); |
| 287 | ~ItemList(); |
| 288 | }; |
| 289 | |
| 290 | VARIANT_ENUM_CAST(ItemList::SelectMode); |
| 291 | VARIANT_ENUM_CAST(ItemList::IconMode); |
| 292 | |
| 293 | #endif // ITEM_LIST_H |
| 294 | |