| 1 | /**************************************************************************/ |
| 2 | /* theme_editor_preview.cpp */ |
| 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 | #include "theme_editor_preview.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "editor/editor_node.h" |
| 35 | #include "editor/editor_string_names.h" |
| 36 | #include "editor/themes/editor_scale.h" |
| 37 | #include "scene/gui/button.h" |
| 38 | #include "scene/gui/check_box.h" |
| 39 | #include "scene/gui/check_button.h" |
| 40 | #include "scene/gui/color_picker.h" |
| 41 | #include "scene/gui/color_rect.h" |
| 42 | #include "scene/gui/label.h" |
| 43 | #include "scene/gui/margin_container.h" |
| 44 | #include "scene/gui/menu_button.h" |
| 45 | #include "scene/gui/option_button.h" |
| 46 | #include "scene/gui/panel.h" |
| 47 | #include "scene/gui/progress_bar.h" |
| 48 | #include "scene/gui/scroll_container.h" |
| 49 | #include "scene/gui/separator.h" |
| 50 | #include "scene/gui/slider.h" |
| 51 | #include "scene/gui/spin_box.h" |
| 52 | #include "scene/gui/tab_container.h" |
| 53 | #include "scene/gui/text_edit.h" |
| 54 | #include "scene/gui/tree.h" |
| 55 | #include "scene/resources/packed_scene.h" |
| 56 | #include "scene/theme/theme_db.h" |
| 57 | |
| 58 | constexpr double REFRESH_TIMER = 1.5; |
| 59 | |
| 60 | void ThemeEditorPreview::set_preview_theme(const Ref<Theme> &p_theme) { |
| 61 | preview_content->set_theme(p_theme); |
| 62 | } |
| 63 | |
| 64 | void ThemeEditorPreview::add_preview_overlay(Control *p_overlay) { |
| 65 | preview_overlay->add_child(p_overlay); |
| 66 | p_overlay->hide(); |
| 67 | } |
| 68 | |
| 69 | void ThemeEditorPreview::_propagate_redraw(Control *p_at) { |
| 70 | p_at->notification(NOTIFICATION_THEME_CHANGED); |
| 71 | p_at->update_minimum_size(); |
| 72 | p_at->queue_redraw(); |
| 73 | for (int i = 0; i < p_at->get_child_count(); i++) { |
| 74 | Control *a = Object::cast_to<Control>(p_at->get_child(i)); |
| 75 | if (a) { |
| 76 | _propagate_redraw(a); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void ThemeEditorPreview::_refresh_interval() { |
| 82 | // In case the project settings have changed. |
| 83 | preview_bg->set_color(GLOBAL_GET("rendering/environment/defaults/default_clear_color")); |
| 84 | |
| 85 | _propagate_redraw(preview_bg); |
| 86 | _propagate_redraw(preview_content); |
| 87 | } |
| 88 | |
| 89 | void ThemeEditorPreview::_preview_visibility_changed() { |
| 90 | set_process(is_visible_in_tree()); |
| 91 | } |
| 92 | |
| 93 | void ThemeEditorPreview::_picker_button_cbk() { |
| 94 | picker_overlay->set_visible(picker_button->is_pressed()); |
| 95 | if (picker_button->is_pressed()) { |
| 96 | _reset_picker_overlay(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | Control *ThemeEditorPreview::_find_hovered_control(Control *p_parent, Vector2 p_mouse_position) { |
| 101 | Control *found = nullptr; |
| 102 | |
| 103 | for (int i = p_parent->get_child_count() - 1; i >= 0; i--) { |
| 104 | Control *cc = Object::cast_to<Control>(p_parent->get_child(i)); |
| 105 | if (!cc || !cc->is_visible()) { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | Rect2 crect = cc->get_rect(); |
| 110 | if (crect.has_point(p_mouse_position)) { |
| 111 | // Check if there is a child control under mouse. |
| 112 | if (cc->get_child_count() > 0) { |
| 113 | found = _find_hovered_control(cc, p_mouse_position - cc->get_position()); |
| 114 | } |
| 115 | |
| 116 | // If there are no applicable children, use the control itself. |
| 117 | if (!found) { |
| 118 | found = cc; |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return found; |
| 125 | } |
| 126 | |
| 127 | void ThemeEditorPreview::_draw_picker_overlay() { |
| 128 | if (!picker_button->is_pressed()) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | picker_overlay->draw_rect(Rect2(Vector2(0.0, 0.0), picker_overlay->get_size()), theme_cache.preview_picker_overlay_color); |
| 133 | if (hovered_control) { |
| 134 | Rect2 highlight_rect = hovered_control->get_global_rect(); |
| 135 | highlight_rect.position = picker_overlay->get_global_transform().affine_inverse().xform(highlight_rect.position); |
| 136 | picker_overlay->draw_style_box(theme_cache.preview_picker_overlay, highlight_rect); |
| 137 | |
| 138 | String highlight_name = hovered_control->get_theme_type_variation(); |
| 139 | if (highlight_name == StringName()) { |
| 140 | highlight_name = hovered_control->get_class_name(); |
| 141 | } |
| 142 | |
| 143 | Rect2 highlight_label_rect = highlight_rect; |
| 144 | highlight_label_rect.size = theme_cache.preview_picker_font->get_string_size(highlight_name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size); |
| 145 | |
| 146 | int margin_top = theme_cache.preview_picker_label->get_margin(SIDE_TOP); |
| 147 | int margin_left = theme_cache.preview_picker_label->get_margin(SIDE_LEFT); |
| 148 | int margin_bottom = theme_cache.preview_picker_label->get_margin(SIDE_BOTTOM); |
| 149 | int margin_right = theme_cache.preview_picker_label->get_margin(SIDE_RIGHT); |
| 150 | highlight_label_rect.size.x += margin_left + margin_right; |
| 151 | highlight_label_rect.size.y += margin_top + margin_bottom; |
| 152 | |
| 153 | highlight_label_rect.position = highlight_label_rect.position.clamp(Vector2(), picker_overlay->get_size()); |
| 154 | |
| 155 | picker_overlay->draw_style_box(theme_cache.preview_picker_label, highlight_label_rect); |
| 156 | |
| 157 | Point2 label_pos = highlight_label_rect.position; |
| 158 | label_pos.y += highlight_label_rect.size.y - margin_bottom; |
| 159 | label_pos.x += margin_left; |
| 160 | picker_overlay->draw_string(theme_cache.preview_picker_font, label_pos, highlight_name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void ThemeEditorPreview::_gui_input_picker_overlay(const Ref<InputEvent> &p_event) { |
| 165 | if (!picker_button->is_pressed()) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | Ref<InputEventMouseButton> mb = p_event; |
| 170 | |
| 171 | if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
| 172 | if (hovered_control) { |
| 173 | StringName theme_type = hovered_control->get_theme_type_variation(); |
| 174 | if (theme_type == StringName()) { |
| 175 | theme_type = hovered_control->get_class_name(); |
| 176 | } |
| 177 | |
| 178 | emit_signal(SNAME("control_picked"), theme_type); |
| 179 | picker_button->set_pressed(false); |
| 180 | picker_overlay->set_visible(false); |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | Ref<InputEventMouseMotion> mm = p_event; |
| 186 | |
| 187 | if (mm.is_valid()) { |
| 188 | Vector2 mp = preview_content->get_local_mouse_position(); |
| 189 | hovered_control = _find_hovered_control(preview_content, mp); |
| 190 | picker_overlay->queue_redraw(); |
| 191 | } |
| 192 | |
| 193 | // Forward input to the scroll container underneath to allow scrolling. |
| 194 | preview_container->gui_input(p_event); |
| 195 | } |
| 196 | |
| 197 | void ThemeEditorPreview::_reset_picker_overlay() { |
| 198 | hovered_control = nullptr; |
| 199 | picker_overlay->queue_redraw(); |
| 200 | } |
| 201 | |
| 202 | void ThemeEditorPreview::_notification(int p_what) { |
| 203 | switch (p_what) { |
| 204 | case NOTIFICATION_POSTINITIALIZE: { |
| 205 | connect(SceneStringName(visibility_changed), callable_mp(this, &ThemeEditorPreview::_preview_visibility_changed)); |
| 206 | } break; |
| 207 | |
| 208 | case NOTIFICATION_ENTER_TREE: { |
| 209 | if (is_visible_in_tree()) { |
| 210 | set_process(true); |
| 211 | } |
| 212 | } break; |
| 213 | |
| 214 | case NOTIFICATION_READY: { |
| 215 | Vector<Ref<Theme>> preview_themes; |
| 216 | preview_themes.push_back(ThemeDB::get_singleton()->get_default_theme()); |
| 217 | ThemeDB::get_singleton()->create_theme_context(preview_root, preview_themes); |
| 218 | } break; |
| 219 | |
| 220 | case NOTIFICATION_THEME_CHANGED: { |
| 221 | picker_button->set_button_icon(get_editor_theme_icon(SNAME("ColorPick"))); |
| 222 | |
| 223 | theme_cache.preview_picker_overlay = get_theme_stylebox(SNAME("preview_picker_overlay"), SNAME("ThemeEditor")); |
| 224 | theme_cache.preview_picker_overlay_color = get_theme_color(SNAME("preview_picker_overlay_color"), SNAME("ThemeEditor")); |
| 225 | theme_cache.preview_picker_label = get_theme_stylebox(SNAME("preview_picker_label"), SNAME("ThemeEditor")); |
| 226 | theme_cache.preview_picker_font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)); |
| 227 | theme_cache.font_size = get_theme_default_font_size(); |
| 228 | } break; |
| 229 | |
| 230 | case NOTIFICATION_PROCESS: { |
| 231 | time_left -= get_process_delta_time(); |
| 232 | if (time_left < 0) { |
| 233 | time_left = REFRESH_TIMER; |
| 234 | _refresh_interval(); |
| 235 | } |
| 236 | } break; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void ThemeEditorPreview::_bind_methods() { |
| 241 | ADD_SIGNAL(MethodInfo("control_picked", PropertyInfo(Variant::STRING, "class_name"))); |
| 242 | } |
| 243 | |
| 244 | ThemeEditorPreview::ThemeEditorPreview() { |
| 245 | preview_toolbar = memnew(HBoxContainer); |
| 246 | add_child(preview_toolbar); |
| 247 | |
| 248 | picker_button = memnew(Button); |
| 249 | preview_toolbar->add_child(picker_button); |
| 250 | picker_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 251 | picker_button->set_toggle_mode(true); |
| 252 | picker_button->set_tooltip_text(TTR("Toggle the control picker, allowing to visually select control types for edit.")); |
| 253 | picker_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditorPreview::_picker_button_cbk)); |
| 254 | |
| 255 | MarginContainer *preview_body = memnew(MarginContainer); |
| 256 | preview_body->set_v_size_flags(SIZE_EXPAND_FILL); |
| 257 | add_child(preview_body); |
| 258 | |
| 259 | preview_container = memnew(ScrollContainer); |
| 260 | preview_body->add_child(preview_container); |
| 261 | |
| 262 | preview_root = memnew(MarginContainer); |
| 263 | preview_container->add_child(preview_root); |
| 264 | preview_root->set_clip_contents(true); |
| 265 | preview_root->set_custom_minimum_size(Size2(450, 0) * EDSCALE); |
| 266 | preview_root->set_v_size_flags(SIZE_EXPAND_FILL); |
| 267 | preview_root->set_h_size_flags(SIZE_EXPAND_FILL); |
| 268 | |
| 269 | preview_bg = memnew(ColorRect); |
| 270 | preview_bg->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 271 | preview_bg->set_color(GLOBAL_GET("rendering/environment/defaults/default_clear_color")); |
| 272 | preview_root->add_child(preview_bg); |
| 273 | |
| 274 | preview_content = memnew(MarginContainer); |
| 275 | preview_content->add_theme_constant_override("margin_right", 4 * EDSCALE); |
| 276 | preview_content->add_theme_constant_override("margin_top", 4 * EDSCALE); |
| 277 | preview_content->add_theme_constant_override("margin_left", 4 * EDSCALE); |
| 278 | preview_content->add_theme_constant_override("margin_bottom", 4 * EDSCALE); |
| 279 | preview_root->add_child(preview_content); |
| 280 | |
| 281 | preview_overlay = memnew(MarginContainer); |
| 282 | preview_overlay->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 283 | preview_overlay->set_clip_contents(true); |
| 284 | preview_body->add_child(preview_overlay); |
| 285 | |
| 286 | picker_overlay = memnew(Control); |
| 287 | add_preview_overlay(picker_overlay); |
| 288 | picker_overlay->connect(SceneStringName(draw), callable_mp(this, &ThemeEditorPreview::_draw_picker_overlay)); |
| 289 | picker_overlay->connect(SceneStringName(gui_input), callable_mp(this, &ThemeEditorPreview::_gui_input_picker_overlay)); |
| 290 | picker_overlay->connect(SceneStringName(mouse_exited), callable_mp(this, &ThemeEditorPreview::_reset_picker_overlay)); |
| 291 | } |
| 292 | |
| 293 | void DefaultThemeEditorPreview::_notification(int p_what) { |
| 294 | switch (p_what) { |
| 295 | case NOTIFICATION_THEME_CHANGED: { |
| 296 | test_color_picker_button->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("inspector_property_height"), EditorStringName(Editor)))); |
| 297 | } break; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | DefaultThemeEditorPreview::DefaultThemeEditorPreview() { |
| 302 | Panel *main_panel = memnew(Panel); |
| 303 | preview_content->add_child(main_panel); |
| 304 | |
| 305 | MarginContainer *main_mc = memnew(MarginContainer); |
| 306 | main_mc->add_theme_constant_override("margin_right", 4 * EDSCALE); |
| 307 | main_mc->add_theme_constant_override("margin_top", 4 * EDSCALE); |
| 308 | main_mc->add_theme_constant_override("margin_left", 4 * EDSCALE); |
| 309 | main_mc->add_theme_constant_override("margin_bottom", 4 * EDSCALE); |
| 310 | preview_content->add_child(main_mc); |
| 311 | |
| 312 | HBoxContainer *main_hb = memnew(HBoxContainer); |
| 313 | main_mc->add_child(main_hb); |
| 314 | main_hb->add_theme_constant_override("separation", 20 * EDSCALE); |
| 315 | |
| 316 | VBoxContainer *first_vb = memnew(VBoxContainer); |
| 317 | main_hb->add_child(first_vb); |
| 318 | first_vb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 319 | first_vb->add_theme_constant_override("separation", 10 * EDSCALE); |
| 320 | |
| 321 | first_vb->add_child(memnew(Label("Label"))); |
| 322 | |
| 323 | first_vb->add_child(memnew(Button("Button"))); |
| 324 | Button *bt = memnew(Button); |
| 325 | bt->set_text(TTR("Toggle Button")); |
| 326 | bt->set_toggle_mode(true); |
| 327 | bt->set_pressed(true); |
| 328 | first_vb->add_child(bt); |
| 329 | bt = memnew(Button); |
| 330 | bt->set_text(TTR("Disabled Button")); |
| 331 | bt->set_disabled(true); |
| 332 | first_vb->add_child(bt); |
| 333 | Button *tb = memnew(Button); |
| 334 | tb->set_flat(true); |
| 335 | tb->set_text("Flat Button"); |
| 336 | first_vb->add_child(tb); |
| 337 | |
| 338 | CheckButton *cb = memnew(CheckButton); |
| 339 | cb->set_text("CheckButton"); |
| 340 | first_vb->add_child(cb); |
| 341 | CheckBox *cbx = memnew(CheckBox); |
| 342 | cbx->set_text("CheckBox"); |
| 343 | first_vb->add_child(cbx); |
| 344 | |
| 345 | MenuButton *test_menu_button = memnew(MenuButton); |
| 346 | test_menu_button->set_text("MenuButton"); |
| 347 | test_menu_button->get_popup()->add_item(TTR("Item")); |
| 348 | test_menu_button->get_popup()->add_item(TTR("Disabled Item")); |
| 349 | test_menu_button->get_popup()->set_item_disabled(1, true); |
| 350 | test_menu_button->get_popup()->add_separator(); |
| 351 | test_menu_button->get_popup()->add_check_item(TTR("Check Item")); |
| 352 | test_menu_button->get_popup()->add_check_item(TTR("Checked Item")); |
| 353 | test_menu_button->get_popup()->set_item_checked(4, true); |
| 354 | test_menu_button->get_popup()->add_separator(); |
| 355 | test_menu_button->get_popup()->add_radio_check_item(TTR("Radio Item")); |
| 356 | test_menu_button->get_popup()->add_radio_check_item(TTR("Checked Radio Item")); |
| 357 | test_menu_button->get_popup()->set_item_checked(7, true); |
| 358 | test_menu_button->get_popup()->add_separator(TTR("Named Separator")); |
| 359 | |
| 360 | PopupMenu *test_submenu = memnew(PopupMenu); |
| 361 | test_menu_button->get_popup()->add_submenu_node_item(TTR("Submenu"), test_submenu); |
| 362 | test_submenu->add_item(TTR("Subitem 1")); |
| 363 | test_submenu->add_item(TTR("Subitem 2")); |
| 364 | first_vb->add_child(test_menu_button); |
| 365 | |
| 366 | OptionButton *test_option_button = memnew(OptionButton); |
| 367 | test_option_button->add_item("OptionButton"); |
| 368 | test_option_button->add_separator(); |
| 369 | test_option_button->add_item(TTR("Has")); |
| 370 | test_option_button->add_item(TTR("Many")); |
| 371 | test_option_button->add_item(TTR("Options")); |
| 372 | first_vb->add_child(test_option_button); |
| 373 | test_color_picker_button = memnew(ColorPickerButton); |
| 374 | first_vb->add_child(test_color_picker_button); |
| 375 | |
| 376 | VBoxContainer *second_vb = memnew(VBoxContainer); |
| 377 | second_vb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 378 | main_hb->add_child(second_vb); |
| 379 | second_vb->add_theme_constant_override("separation", 10 * EDSCALE); |
| 380 | LineEdit *le = memnew(LineEdit); |
| 381 | le->set_text("LineEdit"); |
| 382 | second_vb->add_child(le); |
| 383 | le = memnew(LineEdit); |
| 384 | le->set_text(TTR("Disabled LineEdit")); |
| 385 | le->set_editable(false); |
| 386 | second_vb->add_child(le); |
| 387 | TextEdit *te = memnew(TextEdit); |
| 388 | te->set_text("TextEdit"); |
| 389 | te->set_custom_minimum_size(Size2(0, 100) * EDSCALE); |
| 390 | second_vb->add_child(te); |
| 391 | second_vb->add_child(memnew(SpinBox)); |
| 392 | |
| 393 | HBoxContainer *vhb = memnew(HBoxContainer); |
| 394 | second_vb->add_child(vhb); |
| 395 | vhb->set_custom_minimum_size(Size2(0, 100) * EDSCALE); |
| 396 | vhb->add_child(memnew(VSlider)); |
| 397 | VScrollBar *vsb = memnew(VScrollBar); |
| 398 | vsb->set_page(25); |
| 399 | vhb->add_child(vsb); |
| 400 | vhb->add_child(memnew(VSeparator)); |
| 401 | VBoxContainer *hvb = memnew(VBoxContainer); |
| 402 | vhb->add_child(hvb); |
| 403 | hvb->set_alignment(BoxContainer::ALIGNMENT_CENTER); |
| 404 | hvb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 405 | hvb->add_child(memnew(HSlider)); |
| 406 | HScrollBar *hsb = memnew(HScrollBar); |
| 407 | hsb->set_page(25); |
| 408 | hvb->add_child(hsb); |
| 409 | HSlider *hs = memnew(HSlider); |
| 410 | hs->set_editable(false); |
| 411 | hvb->add_child(hs); |
| 412 | hvb->add_child(memnew(HSeparator)); |
| 413 | ProgressBar *pb = memnew(ProgressBar); |
| 414 | pb->set_value(50); |
| 415 | hvb->add_child(pb); |
| 416 | |
| 417 | VBoxContainer *third_vb = memnew(VBoxContainer); |
| 418 | third_vb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 419 | third_vb->add_theme_constant_override("separation", 10 * EDSCALE); |
| 420 | main_hb->add_child(third_vb); |
| 421 | |
| 422 | TabContainer *tc = memnew(TabContainer); |
| 423 | third_vb->add_child(tc); |
| 424 | tc->set_custom_minimum_size(Size2(0, 135) * EDSCALE); |
| 425 | Control *tcc = memnew(Control); |
| 426 | tcc->set_name(TTR("Tab 1")); |
| 427 | tc->add_child(tcc); |
| 428 | tcc = memnew(Control); |
| 429 | tcc->set_name(TTR("Tab 2")); |
| 430 | tc->add_child(tcc); |
| 431 | tcc = memnew(Control); |
| 432 | tcc->set_name(TTR("Tab 3")); |
| 433 | tc->add_child(tcc); |
| 434 | tc->set_tab_disabled(2, true); |
| 435 | |
| 436 | Tree *test_tree = memnew(Tree); |
| 437 | third_vb->add_child(test_tree); |
| 438 | test_tree->set_custom_minimum_size(Size2(0, 175) * EDSCALE); |
| 439 | |
| 440 | TreeItem *item = test_tree->create_item(); |
| 441 | item->set_text(0, "Tree"); |
| 442 | item = test_tree->create_item(test_tree->get_root()); |
| 443 | item->set_text(0, "Item"); |
| 444 | item = test_tree->create_item(test_tree->get_root()); |
| 445 | item->set_editable(0, true); |
| 446 | item->set_text(0, TTR("Editable Item")); |
| 447 | TreeItem *sub_tree = test_tree->create_item(test_tree->get_root()); |
| 448 | sub_tree->set_text(0, TTR("Subtree")); |
| 449 | item = test_tree->create_item(sub_tree); |
| 450 | item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK); |
| 451 | item->set_editable(0, true); |
| 452 | item->set_text(0, "Check Item"); |
| 453 | item = test_tree->create_item(sub_tree); |
| 454 | item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE); |
| 455 | item->set_editable(0, true); |
| 456 | item->set_range_config(0, 0, 20, 0.1); |
| 457 | item->set_range(0, 2); |
| 458 | item = test_tree->create_item(sub_tree); |
| 459 | item->set_cell_mode(0, TreeItem::CELL_MODE_RANGE); |
| 460 | item->set_editable(0, true); |
| 461 | item->set_text(0, TTR("Has,Many,Options")); |
| 462 | item->set_range(0, 2); |
| 463 | } |
| 464 | |
| 465 | void SceneThemeEditorPreview::_reload_scene() { |
| 466 | if (loaded_scene.is_null()) { |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | if (loaded_scene->get_path().is_empty() || !ResourceLoader::exists(loaded_scene->get_path())) { |
| 471 | EditorNode::get_singleton()->show_warning(TTR("Invalid path, the PackedScene resource was probably moved or removed.")); |
| 472 | emit_signal(SNAME("scene_invalidated")); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | for (int i = preview_content->get_child_count() - 1; i >= 0; i--) { |
| 477 | Node *node = preview_content->get_child(i); |
| 478 | node->queue_free(); |
| 479 | preview_content->remove_child(node); |
| 480 | } |
| 481 | |
| 482 | Node *instance = loaded_scene->instantiate(); |
| 483 | if (!instance || !Object::cast_to<Control>(instance)) { |
| 484 | EditorNode::get_singleton()->show_warning(TTR("Invalid PackedScene resource, must have a Control node at its root.")); |
| 485 | emit_signal(SNAME("scene_invalidated")); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | preview_content->add_child(instance); |
| 490 | emit_signal(SNAME("scene_reloaded")); |
| 491 | } |
| 492 | |
| 493 | void SceneThemeEditorPreview::_notification(int p_what) { |
| 494 | switch (p_what) { |
| 495 | case NOTIFICATION_THEME_CHANGED: { |
| 496 | reload_scene_button->set_button_icon(get_editor_theme_icon(SNAME("Reload"))); |
| 497 | } break; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void SceneThemeEditorPreview::_bind_methods() { |
| 502 | ADD_SIGNAL(MethodInfo("scene_invalidated")); |
| 503 | ADD_SIGNAL(MethodInfo("scene_reloaded")); |
| 504 | } |
| 505 | |
| 506 | bool SceneThemeEditorPreview::set_preview_scene(const String &p_path) { |
| 507 | loaded_scene = ResourceLoader::load(p_path); |
| 508 | if (loaded_scene.is_null()) { |
| 509 | EditorNode::get_singleton()->show_warning(TTR("Invalid file, not a PackedScene resource.")); |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | Node *instance = loaded_scene->instantiate(); |
| 514 | |
| 515 | if (!instance) { |
| 516 | EditorNode::get_singleton()->show_warning(TTR("Invalid PackedScene resource, could not instantiate it.")); |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | if (!Object::cast_to<Control>(instance)) { |
| 521 | EditorNode::get_singleton()->show_warning(TTR("Invalid PackedScene resource, must have a Control node at its root.")); |
| 522 | memdelete(instance); |
| 523 | return false; |
| 524 | } |
| 525 | |
| 526 | preview_content->add_child(instance); |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | String SceneThemeEditorPreview::get_preview_scene_path() const { |
| 531 | if (loaded_scene.is_null()) { |
| 532 | return ""; |
| 533 | } |
| 534 | |
| 535 | return loaded_scene->get_path(); |
| 536 | } |
| 537 | |
| 538 | SceneThemeEditorPreview::SceneThemeEditorPreview() { |
| 539 | preview_toolbar->add_child(memnew(VSeparator)); |
| 540 | |
| 541 | reload_scene_button = memnew(Button); |
| 542 | reload_scene_button->set_flat(true); |
| 543 | reload_scene_button->set_tooltip_text(TTR("Reload the scene to reflect its most actual state.")); |
| 544 | preview_toolbar->add_child(reload_scene_button); |
| 545 | reload_scene_button->connect(SceneStringName(pressed), callable_mp(this, &SceneThemeEditorPreview::_reload_scene)); |
| 546 | } |