2023-01-05 12:25:55 +00: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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "style_box_editor_plugin.h"
|
2019-12-24 07:17:23 +00:00
|
|
|
|
|
|
|
#include "editor/editor_scale.h"
|
2023-05-12 20:02:42 +00:00
|
|
|
#include "scene/gui/button.h"
|
2023-07-14 20:35:39 +00:00
|
|
|
#include "scene/resources/style_box_texture.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-05-23 21:17:12 +00:00
|
|
|
bool StyleBoxPreview::grid_preview_enabled = true;
|
|
|
|
|
|
|
|
void StyleBoxPreview::_grid_preview_toggled(bool p_active) {
|
|
|
|
grid_preview_enabled = p_active;
|
2023-05-12 20:02:42 +00:00
|
|
|
queue_redraw();
|
2018-05-17 21:02:16 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-05-17 21:02:16 +00:00
|
|
|
void StyleBoxPreview::edit(const Ref<StyleBox> &p_stylebox) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (stylebox.is_valid()) {
|
2023-07-03 19:29:37 +00:00
|
|
|
stylebox->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
stylebox = p_stylebox;
|
2023-05-12 20:02:42 +00:00
|
|
|
if (stylebox.is_valid()) {
|
2023-07-03 19:29:37 +00:00
|
|
|
stylebox->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2023-05-12 20:02:42 +00:00
|
|
|
Ref<StyleBoxTexture> sbt = stylebox;
|
2022-05-23 21:17:12 +00:00
|
|
|
grid_preview->set_visible(sbt.is_valid());
|
2023-05-12 20:02:42 +00:00
|
|
|
queue_redraw();
|
2019-10-24 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 21:17:12 +00:00
|
|
|
void StyleBoxPreview::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2023-05-12 20:02:42 +00:00
|
|
|
set_texture(get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons")));
|
|
|
|
grid_preview->set_icon(get_theme_icon(SNAME("StyleBoxGrid"), SNAME("EditorIcons")));
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
_redraw();
|
2022-05-23 21:17:12 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 21:20:52 +00:00
|
|
|
void StyleBoxPreview::_redraw() {
|
2018-05-17 21:02:16 +00:00
|
|
|
if (stylebox.is_valid()) {
|
2023-05-12 20:02:42 +00:00
|
|
|
float grid_button_width = get_theme_icon(SNAME("StyleBoxGrid"), SNAME("EditorIcons"))->get_size().x;
|
|
|
|
Rect2 preview_rect = get_rect();
|
|
|
|
preview_rect = preview_rect.grow(-grid_button_width);
|
2019-11-24 15:26:30 +00:00
|
|
|
|
2023-05-12 20:02:42 +00: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 15:26:30 +00:00
|
|
|
|
2023-05-12 20:02:42 +00:00
|
|
|
draw_style_box(stylebox, preview_rect);
|
2022-05-23 21:17:12 +00:00
|
|
|
|
|
|
|
Ref<StyleBoxTexture> sbt = stylebox;
|
2023-05-12 20:02:42 +00:00
|
|
|
// Draw the "grid". Use white lines, as well as subtle black lines to ensure contrast.
|
2022-05-23 21:17:12 +00:00
|
|
|
if (sbt.is_valid() && grid_preview->is_pressed()) {
|
2023-05-12 20:02:42 +00: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-23 21:17:12 +00:00
|
|
|
}
|
2018-05-17 21:02:16 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 21:02:16 +00:00
|
|
|
StyleBoxPreview::StyleBoxPreview() {
|
2023-05-12 20:02:42 +00: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 11:48:53 +00: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-23 21:17:12 +00:00
|
|
|
grid_preview->set_toggle_mode(true);
|
|
|
|
grid_preview->connect("toggled", callable_mp(this, &StyleBoxPreview::_grid_preview_toggled));
|
|
|
|
grid_preview->set_pressed(grid_preview_enabled);
|
2023-05-12 20:02:42 +00: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 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:36:51 +00:00
|
|
|
StyleBoxEditorPlugin::StyleBoxEditorPlugin() {
|
2018-05-17 21:02:16 +00:00
|
|
|
Ref<EditorInspectorPluginStyleBox> inspector_plugin;
|
2021-06-17 22:03:09 +00:00
|
|
|
inspector_plugin.instantiate();
|
2018-05-17 21:02:16 +00:00
|
|
|
add_inspector_plugin(inspector_plugin);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|