2014-02-10 09:10:30 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* style_box_editor_plugin.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "style_box_editor_plugin.h"
|
2019-12-24 15:17:23 +08:00
|
|
|
|
2024-01-15 20:14:55 +08:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2023-05-13 04:02:42 +08:00
|
|
|
#include "scene/gui/button.h"
|
2023-07-15 04:35:39 +08:00
|
|
|
#include "scene/resources/style_box_texture.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2022-05-24 05:17:12 +08:00
|
|
|
bool StyleBoxPreview::grid_preview_enabled = true;
|
|
|
|
|
|
|
|
void StyleBoxPreview::_grid_preview_toggled(bool p_active) {
|
|
|
|
grid_preview_enabled = p_active;
|
2023-05-13 04:02:42 +08:00
|
|
|
queue_redraw();
|
2018-05-18 05:02:16 +08:00
|
|
|
}
|
2020-05-14 20:29:06 +08:00
|
|
|
|
2018-05-18 05:02:16 +08:00
|
|
|
void StyleBoxPreview::edit(const Ref<StyleBox> &p_stylebox) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (stylebox.is_valid()) {
|
2023-07-04 03:29:37 +08:00
|
|
|
stylebox->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
stylebox = p_stylebox;
|
2023-05-13 04:02:42 +08:00
|
|
|
if (stylebox.is_valid()) {
|
2023-07-04 03:29:37 +08:00
|
|
|
stylebox->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2023-05-13 04:02:42 +08:00
|
|
|
Ref<StyleBoxTexture> sbt = stylebox;
|
2022-05-24 05:17:12 +08:00
|
|
|
grid_preview->set_visible(sbt.is_valid());
|
2023-05-13 04:02:42 +08:00
|
|
|
queue_redraw();
|
2019-10-25 05:20:52 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 05:17:12 +08:00
|
|
|
void StyleBoxPreview::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2023-08-13 08:33:39 +08:00
|
|
|
set_texture(get_editor_theme_icon(SNAME("Checkerboard")));
|
2024-10-10 06:21:47 +08:00
|
|
|
grid_preview->set_button_icon(get_editor_theme_icon(SNAME("StyleBoxGrid")));
|
2023-05-13 04:02:42 +08:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
_redraw();
|
2022-05-24 05:17:12 +08:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 05:20:52 +08:00
|
|
|
void StyleBoxPreview::_redraw() {
|
2018-05-18 05:02:16 +08:00
|
|
|
if (stylebox.is_valid()) {
|
2023-08-13 08:33:39 +08:00
|
|
|
float grid_button_width = get_editor_theme_icon(SNAME("StyleBoxGrid"))->get_size().x;
|
2023-05-13 04:02:42 +08:00
|
|
|
Rect2 preview_rect = get_rect();
|
|
|
|
preview_rect = preview_rect.grow(-grid_button_width);
|
2019-11-24 23:26:30 +08:00
|
|
|
|
2023-05-13 04:02:42 +08:00
|
|
|
// Re-adjust preview panel to fit all drawn content.
|
|
|
|
Rect2 drawing_rect = stylebox->get_draw_rect(preview_rect);
|
|
|
|
preview_rect.size -= drawing_rect.size - preview_rect.size;
|
|
|
|
preview_rect.position -= drawing_rect.position - preview_rect.position;
|
2019-11-24 23:26:30 +08:00
|
|
|
|
2023-05-13 04:02:42 +08:00
|
|
|
draw_style_box(stylebox, preview_rect);
|
2022-05-24 05:17:12 +08:00
|
|
|
|
|
|
|
Ref<StyleBoxTexture> sbt = stylebox;
|
2023-05-13 04:02:42 +08:00
|
|
|
// Draw the "grid". Use white lines, as well as subtle black lines to ensure contrast.
|
2022-05-24 05:17:12 +08:00
|
|
|
if (sbt.is_valid() && grid_preview->is_pressed()) {
|
2023-05-13 04:02:42 +08:00
|
|
|
const Color dark_color = Color(0, 0, 0, 0.4);
|
|
|
|
const Color bright_color = Color(1, 1, 1, 0.8);
|
|
|
|
int x_left = drawing_rect.position.x + sbt->get_margin(SIDE_LEFT);
|
|
|
|
int x_right = drawing_rect.position.x + drawing_rect.size.width - sbt->get_margin(SIDE_RIGHT);
|
|
|
|
int y_top = drawing_rect.position.y + sbt->get_margin(SIDE_TOP);
|
|
|
|
int y_bottom = drawing_rect.position.y + drawing_rect.size.height - sbt->get_margin(SIDE_BOTTOM);
|
|
|
|
|
|
|
|
draw_line(Point2(x_left + 2, 0), Point2(x_left + 2, get_size().height), dark_color);
|
|
|
|
draw_line(Point2(x_right + 1, 0), Point2(x_right + 1, get_size().height), dark_color);
|
|
|
|
draw_line(Point2(0, y_top + 2), Point2(get_size().width, y_top + 2), dark_color);
|
|
|
|
draw_line(Point2(0, y_bottom + 1), Point2(get_size().width, y_bottom + 1), dark_color);
|
|
|
|
|
|
|
|
draw_line(Point2(x_left + 1, 0), Point2(x_left + 1, get_size().height), bright_color);
|
|
|
|
draw_line(Point2(x_right, 0), Point2(x_right, get_size().height), bright_color);
|
|
|
|
draw_line(Point2(0, y_top + 1), Point2(get_size().width, y_top + 1), bright_color);
|
|
|
|
draw_line(Point2(0, y_bottom), Point2(get_size().width, y_bottom), bright_color);
|
2022-05-24 05:17:12 +08:00
|
|
|
}
|
2018-05-18 05:02:16 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2018-05-18 05:02:16 +08:00
|
|
|
StyleBoxPreview::StyleBoxPreview() {
|
2023-05-13 04:02:42 +08:00
|
|
|
set_clip_contents(true);
|
|
|
|
set_custom_minimum_size(Size2(0, 150) * EDSCALE);
|
|
|
|
set_stretch_mode(TextureRect::STRETCH_TILE);
|
|
|
|
set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
|
|
|
|
set_anchors_and_offsets_preset(PRESET_FULL_RECT);
|
|
|
|
|
|
|
|
grid_preview = memnew(Button);
|
2023-06-29 19:48:53 +08:00
|
|
|
// This theme variation works better than the normal theme because there's no focus highlight.
|
|
|
|
grid_preview->set_theme_type_variation("PreviewLightButton");
|
2022-05-24 05:17:12 +08:00
|
|
|
grid_preview->set_toggle_mode(true);
|
2024-06-01 18:15:13 +08:00
|
|
|
grid_preview->connect(SceneStringName(toggled), callable_mp(this, &StyleBoxPreview::_grid_preview_toggled));
|
2022-05-24 05:17:12 +08:00
|
|
|
grid_preview->set_pressed(grid_preview_enabled);
|
2023-05-13 04:02:42 +08:00
|
|
|
add_child(grid_preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorInspectorPluginStyleBox::can_handle(Object *p_object) {
|
|
|
|
return Object::cast_to<StyleBox>(p_object) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorInspectorPluginStyleBox::parse_begin(Object *p_object) {
|
|
|
|
Ref<StyleBox> sb = Ref<StyleBox>(Object::cast_to<StyleBox>(p_object));
|
|
|
|
|
|
|
|
StyleBoxPreview *preview = memnew(StyleBoxPreview);
|
|
|
|
preview->edit(sb);
|
|
|
|
add_custom_control(preview);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2022-01-27 17:36:51 +08:00
|
|
|
StyleBoxEditorPlugin::StyleBoxEditorPlugin() {
|
2018-05-18 05:02:16 +08:00
|
|
|
Ref<EditorInspectorPluginStyleBox> inspector_plugin;
|
2021-06-18 06:03:09 +08:00
|
|
|
inspector_plugin.instantiate();
|
2018-05-18 05:02:16 +08:00
|
|
|
add_inspector_plugin(inspector_plugin);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|