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