Path: blob/master/editor/scene/gui/style_box_editor_plugin.cpp
9903 views
/**************************************************************************/1/* style_box_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "style_box_editor_plugin.h"3132#include "editor/themes/editor_scale.h"33#include "scene/gui/button.h"34#include "scene/resources/style_box_texture.h"3536bool StyleBoxPreview::grid_preview_enabled = true;3738void StyleBoxPreview::_grid_preview_toggled(bool p_active) {39grid_preview_enabled = p_active;40queue_redraw();41}4243void StyleBoxPreview::edit(const Ref<StyleBox> &p_stylebox) {44if (stylebox.is_valid()) {45stylebox->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));46}47stylebox = p_stylebox;48if (stylebox.is_valid()) {49stylebox->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));50}51Ref<StyleBoxTexture> sbt = stylebox;52grid_preview->set_visible(sbt.is_valid());53queue_redraw();54}5556void StyleBoxPreview::_notification(int p_what) {57switch (p_what) {58case NOTIFICATION_THEME_CHANGED: {59set_texture(get_editor_theme_icon(SNAME("Checkerboard")));60grid_preview->set_button_icon(get_editor_theme_icon(SNAME("StyleBoxGrid")));61} break;62case NOTIFICATION_DRAW: {63_redraw();64} break;65}66}6768void StyleBoxPreview::_redraw() {69if (stylebox.is_valid()) {70float grid_button_width = get_editor_theme_icon(SNAME("StyleBoxGrid"))->get_size().x;71Rect2 preview_rect = get_rect();72preview_rect = preview_rect.grow(-grid_button_width);7374// Re-adjust preview panel to fit all drawn content.75Rect2 drawing_rect = stylebox->get_draw_rect(preview_rect);76preview_rect.size -= drawing_rect.size - preview_rect.size;77preview_rect.position -= drawing_rect.position - preview_rect.position;7879draw_style_box(stylebox, preview_rect);8081Ref<StyleBoxTexture> sbt = stylebox;82// Draw the "grid". Use white lines, as well as subtle black lines to ensure contrast.83if (sbt.is_valid() && grid_preview->is_pressed()) {84const Color dark_color = Color(0, 0, 0, 0.4);85const Color bright_color = Color(1, 1, 1, 0.8);86int x_left = drawing_rect.position.x + sbt->get_margin(SIDE_LEFT);87int x_right = drawing_rect.position.x + drawing_rect.size.width - sbt->get_margin(SIDE_RIGHT);88int y_top = drawing_rect.position.y + sbt->get_margin(SIDE_TOP);89int y_bottom = drawing_rect.position.y + drawing_rect.size.height - sbt->get_margin(SIDE_BOTTOM);9091draw_line(Point2(x_left + 2, 0), Point2(x_left + 2, get_size().height), dark_color);92draw_line(Point2(x_right + 1, 0), Point2(x_right + 1, get_size().height), dark_color);93draw_line(Point2(0, y_top + 2), Point2(get_size().width, y_top + 2), dark_color);94draw_line(Point2(0, y_bottom + 1), Point2(get_size().width, y_bottom + 1), dark_color);9596draw_line(Point2(x_left + 1, 0), Point2(x_left + 1, get_size().height), bright_color);97draw_line(Point2(x_right, 0), Point2(x_right, get_size().height), bright_color);98draw_line(Point2(0, y_top + 1), Point2(get_size().width, y_top + 1), bright_color);99draw_line(Point2(0, y_bottom), Point2(get_size().width, y_bottom), bright_color);100}101}102}103104StyleBoxPreview::StyleBoxPreview() {105set_clip_contents(true);106set_custom_minimum_size(Size2(0, 150) * EDSCALE);107set_stretch_mode(TextureRect::STRETCH_TILE);108set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);109set_anchors_and_offsets_preset(PRESET_FULL_RECT);110111grid_preview = memnew(Button);112// This theme variation works better than the normal theme because there's no focus highlight.113grid_preview->set_theme_type_variation("PreviewLightButton");114grid_preview->set_tooltip_text(TTRC("Toggle margins preview grid."));115grid_preview->set_toggle_mode(true);116grid_preview->connect(SceneStringName(toggled), callable_mp(this, &StyleBoxPreview::_grid_preview_toggled));117grid_preview->set_pressed(grid_preview_enabled);118add_child(grid_preview);119}120121bool EditorInspectorPluginStyleBox::can_handle(Object *p_object) {122return Object::cast_to<StyleBox>(p_object) != nullptr;123}124125void EditorInspectorPluginStyleBox::parse_begin(Object *p_object) {126Ref<StyleBox> sb = Ref<StyleBox>(Object::cast_to<StyleBox>(p_object));127128StyleBoxPreview *preview = memnew(StyleBoxPreview);129preview->edit(sb);130add_custom_control(preview);131}132133StyleBoxEditorPlugin::StyleBoxEditorPlugin() {134Ref<EditorInspectorPluginStyleBox> inspector_plugin;135inspector_plugin.instantiate();136add_inspector_plugin(inspector_plugin);137}138139140