Path: blob/master/editor/scene/texture/bit_map_editor_plugin.cpp
9903 views
/**************************************************************************/1/* bit_map_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 "bit_map_editor_plugin.h"3132#include "editor/editor_string_names.h"33#include "editor/themes/editor_scale.h"34#include "scene/gui/aspect_ratio_container.h"35#include "scene/gui/label.h"36#include "scene/gui/margin_container.h"37#include "scene/gui/texture_rect.h"38#include "scene/resources/image_texture.h"3940void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) {41Ref<ImageTexture> bitmap_texture = ImageTexture::create_from_image(p_bitmap->convert_to_image());42texture_rect->set_texture(bitmap_texture);43if (bitmap_texture.is_valid()) {44centering_container->set_custom_minimum_size(Size2(0, 250) * EDSCALE);45centering_container->set_ratio(bitmap_texture->get_size().aspect());46outline_overlay->connect(SceneStringName(draw), callable_mp(this, &BitMapEditor::_draw_outline));47}48size_label->set_text(vformat(U"%s×%s", p_bitmap->get_size().width, p_bitmap->get_size().height));49}5051void BitMapEditor::_notification(int p_what) {52switch (p_what) {53case NOTIFICATION_THEME_CHANGED: {54cached_outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor));55} break;56}57}5859void BitMapEditor::_draw_outline() {60const float outline_width = Math::round(EDSCALE);61const Rect2 outline_rect = Rect2(Vector2(), texture_rect->get_size()).grow(outline_width * 0.5);62outline_overlay->draw_rect(outline_rect, cached_outline_color, false, outline_width);63}6465BitMapEditor::BitMapEditor() {66MarginContainer *margin_container = memnew(MarginContainer);67const float outline_width = Math::round(EDSCALE);68margin_container->add_theme_constant_override("margin_right", outline_width);69margin_container->add_theme_constant_override("margin_top", outline_width);70margin_container->add_theme_constant_override("margin_left", outline_width);71margin_container->add_theme_constant_override("margin_bottom", outline_width);72add_child(margin_container);7374centering_container = memnew(AspectRatioContainer);75margin_container->add_child(centering_container);7677texture_rect = memnew(TextureRect);78texture_rect->set_texture_filter(TEXTURE_FILTER_NEAREST);79texture_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);80centering_container->add_child(texture_rect);8182outline_overlay = memnew(Control);83centering_container->add_child(outline_overlay);8485size_label = memnew(Label);86size_label->set_focus_mode(FOCUS_ACCESSIBILITY);87size_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);88add_child(size_label);8990// Reduce extra padding on top and bottom of size label.91Ref<StyleBoxEmpty> stylebox;92stylebox.instantiate();93stylebox->set_content_margin(SIDE_RIGHT, 4 * EDSCALE);94size_label->add_theme_style_override(CoreStringName(normal), stylebox);95}9697///////////////////////9899bool EditorInspectorPluginBitMap::can_handle(Object *p_object) {100return Object::cast_to<BitMap>(p_object) != nullptr;101}102103void EditorInspectorPluginBitMap::parse_begin(Object *p_object) {104BitMap *bitmap = Object::cast_to<BitMap>(p_object);105if (!bitmap) {106return;107}108Ref<BitMap> bm(bitmap);109110BitMapEditor *editor = memnew(BitMapEditor);111editor->setup(bm);112add_custom_control(editor);113}114115///////////////////////116117BitMapEditorPlugin::BitMapEditorPlugin() {118Ref<EditorInspectorPluginBitMap> plugin;119plugin.instantiate();120add_inspector_plugin(plugin);121}122123124