diff options
26 files changed, 1334 insertions, 1109 deletions
diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp index 367373dc17..d95b1de365 100644 --- a/editor/editor_inspector.cpp +++ b/editor/editor_inspector.cpp @@ -45,6 +45,7 @@ #include "scene/gui/texture_rect.h" #include "scene/property_utils.h" #include "scene/resources/packed_scene.h" +#include "scene/resources/style_box_flat.h" bool EditorInspector::_property_path_matches(const String &p_property_path, const String &p_filter, EditorPropertyNameProcessor::Style p_style) { if (p_property_path.findn(p_filter) != -1) { diff --git a/editor/editor_inspector.h b/editor/editor_inspector.h index a5737b7dc6..9a4c4f7f99 100644 --- a/editor/editor_inspector.h +++ b/editor/editor_inspector.h @@ -44,6 +44,7 @@ class OptionButton; class PanelContainer; class PopupMenu; class SpinBox; +class StyleBoxFlat; class TextureRect; class EditorPropertyRevert { diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index 92c111a418..99feee42b8 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -37,6 +37,9 @@ #include "editor/editor_scale.h" #include "editor/editor_settings.h" #include "scene/resources/image_texture.h" +#include "scene/resources/style_box_flat.h" +#include "scene/resources/style_box_line.h" +#include "scene/resources/style_box_texture.h" #include "modules/modules_enabled.gen.h" // For svg. #ifdef MODULE_SVG_ENABLED diff --git a/editor/gui/editor_toaster.cpp b/editor/gui/editor_toaster.cpp index 866a6db2a6..f928a0fd30 100644 --- a/editor/gui/editor_toaster.cpp +++ b/editor/gui/editor_toaster.cpp @@ -35,6 +35,7 @@ #include "scene/gui/button.h" #include "scene/gui/label.h" #include "scene/gui/panel_container.h" +#include "scene/resources/style_box_flat.h" EditorToaster *EditorToaster::singleton = nullptr; diff --git a/editor/gui/editor_toaster.h b/editor/gui/editor_toaster.h index 4837756b4e..3e39d9d519 100644 --- a/editor/gui/editor_toaster.h +++ b/editor/gui/editor_toaster.h @@ -37,6 +37,7 @@ class Button; class PanelContainer; +class StyleBoxFlat; class EditorToaster : public HBoxContainer { GDCLASS(EditorToaster, HBoxContainer); diff --git a/editor/plugins/animation_blend_tree_editor_plugin.cpp b/editor/plugins/animation_blend_tree_editor_plugin.cpp index ea210872f7..1abf9d57ce 100644 --- a/editor/plugins/animation_blend_tree_editor_plugin.cpp +++ b/editor/plugins/animation_blend_tree_editor_plugin.cpp @@ -48,6 +48,7 @@ #include "scene/gui/separator.h" #include "scene/gui/view_panner.h" #include "scene/main/window.h" +#include "scene/resources/style_box_flat.h" void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) { for (int i = 0; i < add_options.size(); i++) { diff --git a/editor/plugins/animation_state_machine_editor.cpp b/editor/plugins/animation_state_machine_editor.cpp index 49f073f245..54296411f7 100644 --- a/editor/plugins/animation_state_machine_editor.cpp +++ b/editor/plugins/animation_state_machine_editor.cpp @@ -50,6 +50,7 @@ #include "scene/gui/tree.h" #include "scene/main/viewport.h" #include "scene/main/window.h" +#include "scene/resources/style_box_flat.h" #include "scene/scene_string_names.h" bool AnimationNodeStateMachineEditor::can_edit(const Ref<AnimationNode> &p_node) { diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index d6a11f856a..0386a05cc2 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -62,6 +62,7 @@ #include "scene/main/canvas_layer.h" #include "scene/main/window.h" #include "scene/resources/packed_scene.h" +#include "scene/resources/style_box_texture.h" // Min and Max are power of two in order to play nicely with successive increment. // That way, we can naturally reach a 100% zoom from boundaries. diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index 4e2a629c7e..2f97cda343 100644 --- a/editor/plugins/canvas_item_editor_plugin.h +++ b/editor/plugins/canvas_item_editor_plugin.h @@ -39,12 +39,13 @@ class AcceptDialog; class CanvasItemEditorViewport; class ConfirmationDialog; class EditorData; -class EditorZoomWidget; class EditorSelection; +class EditorZoomWidget; class HScrollBar; class HSplitContainer; class MenuButton; class PanelContainer; +class StyleBoxTexture; class ViewPanner; class VScrollBar; class VSplitContainer; diff --git a/editor/plugins/style_box_editor_plugin.cpp b/editor/plugins/style_box_editor_plugin.cpp index c126aec008..16716e589e 100644 --- a/editor/plugins/style_box_editor_plugin.cpp +++ b/editor/plugins/style_box_editor_plugin.cpp @@ -32,6 +32,7 @@ #include "editor/editor_scale.h" #include "scene/gui/button.h" +#include "scene/resources/style_box_texture.h" bool StyleBoxPreview::grid_preview_enabled = true; diff --git a/editor/plugins/texture_region_editor_plugin.h b/editor/plugins/texture_region_editor_plugin.h index 6b7a94452d..6b7a198246 100644 --- a/editor/plugins/texture_region_editor_plugin.h +++ b/editor/plugins/texture_region_editor_plugin.h @@ -38,7 +38,7 @@ #include "scene/3d/sprite_3d.h" #include "scene/gui/dialogs.h" #include "scene/gui/nine_patch_rect.h" -#include "scene/resources/style_box.h" +#include "scene/resources/style_box_texture.h" class AtlasTexture; class OptionButton; diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index ed33115fbf..2f37c2ccb2 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -59,6 +59,7 @@ #include "scene/main/window.h" #include "scene/resources/curve_texture.h" #include "scene/resources/image_texture.h" +#include "scene/resources/style_box_flat.h" #include "scene/resources/visual_shader_nodes.h" #include "scene/resources/visual_shader_particle_nodes.h" #include "servers/display_server.h" diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index d23bc8ddaa..e50a1c0e88 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -37,6 +37,8 @@ #include "core/os/os.h" #include "scene/gui/color_mode.h" #include "scene/resources/image_texture.h" +#include "scene/resources/style_box_flat.h" +#include "scene/resources/style_box_texture.h" #include "servers/display_server.h" #include "thirdparty/misc/ok_color.h" #include "thirdparty/misc/ok_color_shader.h" diff --git a/scene/gui/color_picker.h b/scene/gui/color_picker.h index 03e47cb5c3..1a4b1b6ee1 100644 --- a/scene/gui/color_picker.h +++ b/scene/gui/color_picker.h @@ -46,6 +46,7 @@ #include "scene/gui/slider.h" #include "scene/gui/spin_box.h" #include "scene/gui/texture_rect.h" +#include "scene/resources/style_box_flat.h" class ColorMode; class ColorModeRGB; diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index 2c37017fa1..0c9ed37046 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -36,6 +36,7 @@ #include "scene/gui/box_container.h" #include "scene/gui/button.h" #include "scene/gui/view_panner.h" +#include "scene/resources/style_box_flat.h" constexpr int MINIMAP_OFFSET = 12; constexpr int MINIMAP_PADDING = 5; diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 8a925f8a2e..1c2ed16e63 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -202,6 +202,9 @@ #include "scene/resources/sky_material.h" #include "scene/resources/sphere_shape_3d.h" #include "scene/resources/style_box.h" +#include "scene/resources/style_box_flat.h" +#include "scene/resources/style_box_line.h" +#include "scene/resources/style_box_texture.h" #include "scene/resources/surface_tool.h" #include "scene/resources/syntax_highlighter.h" #include "scene/resources/text_file.h" diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 20424b06f4..24ffaca68d 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -36,6 +36,8 @@ #include "scene/resources/font.h" #include "scene/resources/gradient_texture.h" #include "scene/resources/image_texture.h" +#include "scene/resources/style_box_flat.h" +#include "scene/resources/style_box_line.h" #include "scene/resources/theme.h" #include "scene/theme/theme_db.h" #include "servers/text_server.h" diff --git a/scene/resources/style_box.cpp b/scene/resources/style_box.cpp index a9ebb21345..f87bf1ee05 100644 --- a/scene/resources/style_box.cpp +++ b/scene/resources/style_box.cpp @@ -32,8 +32,6 @@ #include "scene/main/canvas_item.h" -#include <limits.h> - Size2 StyleBox::get_minimum_size() const { Size2 min_size = Size2(get_margin(SIDE_LEFT) + get_margin(SIDE_RIGHT), get_margin(SIDE_TOP) + get_margin(SIDE_BOTTOM)); Size2 custom_size; @@ -145,925 +143,3 @@ StyleBox::StyleBox() { content_margin[i] = -1; } } - -void StyleBoxTexture::set_texture(Ref<Texture2D> p_texture) { - if (texture == p_texture) { - return; - } - texture = p_texture; - emit_changed(); -} - -Ref<Texture2D> StyleBoxTexture::get_texture() const { - return texture; -} - -void StyleBoxTexture::set_texture_margin(Side p_side, float p_size) { - ERR_FAIL_INDEX((int)p_side, 4); - - texture_margin[p_side] = p_size; - emit_changed(); -} - -void StyleBoxTexture::set_texture_margin_all(float p_size) { - for (int i = 0; i < 4; i++) { - texture_margin[i] = p_size; - } - emit_changed(); -} - -void StyleBoxTexture::set_texture_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { - texture_margin[SIDE_LEFT] = p_left; - texture_margin[SIDE_TOP] = p_top; - texture_margin[SIDE_RIGHT] = p_right; - texture_margin[SIDE_BOTTOM] = p_bottom; - emit_changed(); -} - -float StyleBoxTexture::get_texture_margin(Side p_side) const { - ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - - return texture_margin[p_side]; -} - -float StyleBoxTexture::get_style_margin(Side p_side) const { - ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - - return texture_margin[p_side]; -} - -Rect2 StyleBoxTexture::get_draw_rect(const Rect2 &p_rect) const { - return p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]); -} - -void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const { - if (texture.is_null()) { - return; - } - - Rect2 rect = p_rect; - Rect2 src_rect = region_rect; - - texture->get_rect_region(rect, src_rect, rect, src_rect); - - rect.position.x -= expand_margin[SIDE_LEFT]; - rect.position.y -= expand_margin[SIDE_TOP]; - rect.size.x += expand_margin[SIDE_LEFT] + expand_margin[SIDE_RIGHT]; - rect.size.y += expand_margin[SIDE_TOP] + expand_margin[SIDE_BOTTOM]; - - Vector2 start_offset = Vector2(texture_margin[SIDE_LEFT], texture_margin[SIDE_TOP]); - Vector2 end_offset = Vector2(texture_margin[SIDE_RIGHT], texture_margin[SIDE_BOTTOM]); - - RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), start_offset, end_offset, RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate); -} - -void StyleBoxTexture::set_draw_center(bool p_enabled) { - draw_center = p_enabled; - emit_changed(); -} - -bool StyleBoxTexture::is_draw_center_enabled() const { - return draw_center; -} - -void StyleBoxTexture::set_expand_margin(Side p_side, float p_size) { - ERR_FAIL_INDEX((int)p_side, 4); - expand_margin[p_side] = p_size; - emit_changed(); -} - -void StyleBoxTexture::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { - expand_margin[SIDE_LEFT] = p_left; - expand_margin[SIDE_TOP] = p_top; - expand_margin[SIDE_RIGHT] = p_right; - expand_margin[SIDE_BOTTOM] = p_bottom; - emit_changed(); -} - -void StyleBoxTexture::set_expand_margin_all(float p_expand_margin_size) { - for (int i = 0; i < 4; i++) { - expand_margin[i] = p_expand_margin_size; - } - emit_changed(); -} - -float StyleBoxTexture::get_expand_margin(Side p_side) const { - ERR_FAIL_INDEX_V((int)p_side, 4, 0); - return expand_margin[p_side]; -} - -void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) { - if (region_rect == p_region_rect) { - return; - } - - region_rect = p_region_rect; - emit_changed(); -} - -Rect2 StyleBoxTexture::get_region_rect() const { - return region_rect; -} - -void StyleBoxTexture::set_h_axis_stretch_mode(AxisStretchMode p_mode) { - ERR_FAIL_INDEX((int)p_mode, 3); - axis_h = p_mode; - emit_changed(); -} - -StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_h_axis_stretch_mode() const { - return axis_h; -} - -void StyleBoxTexture::set_v_axis_stretch_mode(AxisStretchMode p_mode) { - ERR_FAIL_INDEX((int)p_mode, 3); - axis_v = p_mode; - emit_changed(); -} - -StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_v_axis_stretch_mode() const { - return axis_v; -} - -void StyleBoxTexture::set_modulate(const Color &p_modulate) { - if (modulate == p_modulate) { - return; - } - modulate = p_modulate; - emit_changed(); -} - -Color StyleBoxTexture::get_modulate() const { - return modulate; -} - -void StyleBoxTexture::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_texture", "texture"), &StyleBoxTexture::set_texture); - ClassDB::bind_method(D_METHOD("get_texture"), &StyleBoxTexture::get_texture); - - ClassDB::bind_method(D_METHOD("set_texture_margin", "margin", "size"), &StyleBoxTexture::set_texture_margin); - ClassDB::bind_method(D_METHOD("set_texture_margin_all", "size"), &StyleBoxTexture::set_texture_margin_all); - ClassDB::bind_method(D_METHOD("get_texture_margin", "margin"), &StyleBoxTexture::get_texture_margin); - - ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxTexture::set_expand_margin); - ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_all); - ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxTexture::get_expand_margin); - - ClassDB::bind_method(D_METHOD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect); - ClassDB::bind_method(D_METHOD("get_region_rect"), &StyleBoxTexture::get_region_rect); - - ClassDB::bind_method(D_METHOD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center); - ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxTexture::is_draw_center_enabled); - - ClassDB::bind_method(D_METHOD("set_modulate", "color"), &StyleBoxTexture::set_modulate); - ClassDB::bind_method(D_METHOD("get_modulate"), &StyleBoxTexture::get_modulate); - - ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &StyleBoxTexture::set_h_axis_stretch_mode); - ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &StyleBoxTexture::get_h_axis_stretch_mode); - - ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &StyleBoxTexture::set_v_axis_stretch_mode); - ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &StyleBoxTexture::get_v_axis_stretch_mode); - - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); - - ADD_GROUP("Texture Margins", "texture_margin_"); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_TOP); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_BOTTOM); - - ADD_GROUP("Expand Margins", "expand_margin_"); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM); - - ADD_GROUP("Axis Stretch", "axis_stretch_"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode"); - - ADD_GROUP("Sub-Region", "region_"); - ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect"); - - ADD_GROUP("Modulate", "modulate_"); - ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate_color"), "set_modulate", "get_modulate"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled"); - - BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH); - BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE); - BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT); -} - -StyleBoxTexture::StyleBoxTexture() {} - -StyleBoxTexture::~StyleBoxTexture() {} - -//////////////// - -void StyleBoxFlat::set_bg_color(const Color &p_color) { - bg_color = p_color; - emit_changed(); -} - -Color StyleBoxFlat::get_bg_color() const { - return bg_color; -} - -void StyleBoxFlat::set_border_color(const Color &p_color) { - border_color = p_color; - emit_changed(); -} - -Color StyleBoxFlat::get_border_color() const { - return border_color; -} - -void StyleBoxFlat::set_border_width_all(int p_size) { - border_width[0] = p_size; - border_width[1] = p_size; - border_width[2] = p_size; - border_width[3] = p_size; - emit_changed(); -} - -int StyleBoxFlat::get_border_width_min() const { - return MIN(MIN(border_width[0], border_width[1]), MIN(border_width[2], border_width[3])); -} - -void StyleBoxFlat::set_border_width(Side p_side, int p_width) { - ERR_FAIL_INDEX((int)p_side, 4); - border_width[p_side] = p_width; - emit_changed(); -} - -int StyleBoxFlat::get_border_width(Side p_side) const { - ERR_FAIL_INDEX_V((int)p_side, 4, 0); - return border_width[p_side]; -} - -void StyleBoxFlat::set_border_blend(bool p_blend) { - blend_border = p_blend; - emit_changed(); -} - -bool StyleBoxFlat::get_border_blend() const { - return blend_border; -} - -void StyleBoxFlat::set_corner_radius_all(int radius) { - for (int i = 0; i < 4; i++) { - corner_radius[i] = radius; - } - - emit_changed(); -} - -void StyleBoxFlat::set_corner_radius_individual(const int radius_top_left, const int radius_top_right, const int radius_bottom_right, const int radius_bottom_left) { - corner_radius[0] = radius_top_left; - corner_radius[1] = radius_top_right; - corner_radius[2] = radius_bottom_right; - corner_radius[3] = radius_bottom_left; - - emit_changed(); -} - -void StyleBoxFlat::set_corner_radius(const Corner p_corner, const int radius) { - ERR_FAIL_INDEX((int)p_corner, 4); - corner_radius[p_corner] = radius; - emit_changed(); -} - -int StyleBoxFlat::get_corner_radius(const Corner p_corner) const { - ERR_FAIL_INDEX_V((int)p_corner, 4, 0); - return corner_radius[p_corner]; -} - -void StyleBoxFlat::set_expand_margin(Side p_side, float p_size) { - ERR_FAIL_INDEX((int)p_side, 4); - expand_margin[p_side] = p_size; - emit_changed(); -} - -void StyleBoxFlat::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { - expand_margin[SIDE_LEFT] = p_left; - expand_margin[SIDE_TOP] = p_top; - expand_margin[SIDE_RIGHT] = p_right; - expand_margin[SIDE_BOTTOM] = p_bottom; - emit_changed(); -} - -void StyleBoxFlat::set_expand_margin_all(float p_expand_margin_size) { - for (int i = 0; i < 4; i++) { - expand_margin[i] = p_expand_margin_size; - } - emit_changed(); -} - -float StyleBoxFlat::get_expand_margin(Side p_side) const { - ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - return expand_margin[p_side]; -} - -void StyleBoxFlat::set_draw_center(bool p_enabled) { - draw_center = p_enabled; - emit_changed(); -} - -bool StyleBoxFlat::is_draw_center_enabled() const { - return draw_center; -} - -void StyleBoxFlat::set_skew(Vector2 p_skew) { - skew = p_skew; - emit_changed(); -} - -Vector2 StyleBoxFlat::get_skew() const { - return skew; -} - -void StyleBoxFlat::set_shadow_color(const Color &p_color) { - shadow_color = p_color; - emit_changed(); -} - -Color StyleBoxFlat::get_shadow_color() const { - return shadow_color; -} - -void StyleBoxFlat::set_shadow_size(const int &p_size) { - shadow_size = p_size; - emit_changed(); -} - -int StyleBoxFlat::get_shadow_size() const { - return shadow_size; -} - -void StyleBoxFlat::set_shadow_offset(const Point2 &p_offset) { - shadow_offset = p_offset; - emit_changed(); -} - -Point2 StyleBoxFlat::get_shadow_offset() const { - return shadow_offset; -} - -void StyleBoxFlat::set_anti_aliased(const bool &p_anti_aliased) { - anti_aliased = p_anti_aliased; - emit_changed(); - notify_property_list_changed(); -} - -bool StyleBoxFlat::is_anti_aliased() const { - return anti_aliased; -} - -void StyleBoxFlat::set_aa_size(const real_t p_aa_size) { - aa_size = CLAMP(p_aa_size, 0.01, 10); - emit_changed(); -} - -real_t StyleBoxFlat::get_aa_size() const { - return aa_size; -} - -void StyleBoxFlat::set_corner_detail(const int &p_corner_detail) { - corner_detail = CLAMP(p_corner_detail, 1, 20); - emit_changed(); -} - -int StyleBoxFlat::get_corner_detail() const { - return corner_detail; -} - -inline void set_inner_corner_radius(const Rect2 style_rect, const Rect2 inner_rect, const real_t corner_radius[4], real_t *inner_corner_radius) { - real_t border_left = inner_rect.position.x - style_rect.position.x; - real_t border_top = inner_rect.position.y - style_rect.position.y; - real_t border_right = style_rect.size.width - inner_rect.size.width - border_left; - real_t border_bottom = style_rect.size.height - inner_rect.size.height - border_top; - - real_t rad; - - // Top left. - rad = MIN(border_top, border_left); - inner_corner_radius[0] = MAX(corner_radius[0] - rad, 0); - - // Top right; - rad = MIN(border_top, border_right); - inner_corner_radius[1] = MAX(corner_radius[1] - rad, 0); - - // Bottom right. - rad = MIN(border_bottom, border_right); - inner_corner_radius[2] = MAX(corner_radius[2] - rad, 0); - - // Bottom left. - rad = MIN(border_bottom, border_left); - inner_corner_radius[3] = MAX(corner_radius[3] - rad, 0); -} - -inline void draw_rounded_rectangle(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color> &colors, const Rect2 &style_rect, const real_t corner_radius[4], - const Rect2 &ring_rect, const Rect2 &inner_rect, const Color &inner_color, const Color &outer_color, const int corner_detail, const Vector2 &skew, bool is_filled = false) { - int vert_offset = verts.size(); - if (!vert_offset) { - vert_offset = 0; - } - - int adapted_corner_detail = (corner_radius[0] == 0 && corner_radius[1] == 0 && corner_radius[2] == 0 && corner_radius[3] == 0) ? 1 : corner_detail; - - bool draw_border = !is_filled; - - real_t ring_corner_radius[4]; - set_inner_corner_radius(style_rect, ring_rect, corner_radius, ring_corner_radius); - - // Corner radius center points. - Vector<Point2> outer_points = { - ring_rect.position + Vector2(ring_corner_radius[0], ring_corner_radius[0]), //tl - Point2(ring_rect.position.x + ring_rect.size.x - ring_corner_radius[1], ring_rect.position.y + ring_corner_radius[1]), //tr - ring_rect.position + ring_rect.size - Vector2(ring_corner_radius[2], ring_corner_radius[2]), //br - Point2(ring_rect.position.x + ring_corner_radius[3], ring_rect.position.y + ring_rect.size.y - ring_corner_radius[3]) //bl - }; - - real_t inner_corner_radius[4]; - set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius); - - Vector<Point2> inner_points = { - inner_rect.position + Vector2(inner_corner_radius[0], inner_corner_radius[0]), //tl - Point2(inner_rect.position.x + inner_rect.size.x - inner_corner_radius[1], inner_rect.position.y + inner_corner_radius[1]), //tr - inner_rect.position + inner_rect.size - Vector2(inner_corner_radius[2], inner_corner_radius[2]), //br - Point2(inner_rect.position.x + inner_corner_radius[3], inner_rect.position.y + inner_rect.size.y - inner_corner_radius[3]) //bl - }; - // Calculate the vertices. - - // If the center is filled, we do not draw the border and directly use the inner ring as reference. Because all calls to this - // method either draw a ring or a filled rounded rectangle, but not both. - int max_inner_outer = draw_border ? 2 : 1; - - for (int corner_index = 0; corner_index < 4; corner_index++) { - for (int detail = 0; detail <= adapted_corner_detail; detail++) { - for (int inner_outer = 0; inner_outer < max_inner_outer; inner_outer++) { - real_t radius; - Color color; - Point2 corner_point; - if (inner_outer == 0) { - radius = inner_corner_radius[corner_index]; - color = inner_color; - corner_point = inner_points[corner_index]; - } else { - radius = ring_corner_radius[corner_index]; - color = outer_color; - corner_point = outer_points[corner_index]; - } - - const real_t x = radius * (real_t)cos((corner_index + detail / (double)adapted_corner_detail) * (Math_TAU / 4.0) + Math_PI) + corner_point.x; - const real_t y = radius * (real_t)sin((corner_index + detail / (double)adapted_corner_detail) * (Math_TAU / 4.0) + Math_PI) + corner_point.y; - const float x_skew = -skew.x * (y - ring_rect.get_center().y); - const float y_skew = -skew.y * (x - ring_rect.get_center().x); - verts.push_back(Vector2(x + x_skew, y + y_skew)); - colors.push_back(color); - } - } - } - - int ring_vert_count = verts.size() - vert_offset; - - // Fill the indices and the colors for the border. - - if (draw_border) { - for (int i = 0; i < ring_vert_count; i++) { - indices.push_back(vert_offset + ((i + 0) % ring_vert_count)); - indices.push_back(vert_offset + ((i + 2) % ring_vert_count)); - indices.push_back(vert_offset + ((i + 1) % ring_vert_count)); - } - } - - if (is_filled) { - // Compute the triangles pattern to draw the rounded rectangle. - // Consists of vertical stripes of two triangles each. - - int stripes_count = ring_vert_count / 2 - 1; - int last_vert_id = ring_vert_count - 1; - - for (int i = 0; i < stripes_count; i++) { - // Polygon 1. - indices.push_back(vert_offset + i); - indices.push_back(vert_offset + last_vert_id - i - 1); - indices.push_back(vert_offset + i + 1); - // Polygon 2. - indices.push_back(vert_offset + i); - indices.push_back(vert_offset + last_vert_id - 0 - i); - indices.push_back(vert_offset + last_vert_id - 1 - i); - } - } -} - -inline void adapt_values(int p_index_a, int p_index_b, real_t *adapted_values, const real_t *p_values, const real_t p_width, const real_t p_max_a, const real_t p_max_b) { - if (p_values[p_index_a] + p_values[p_index_b] > p_width) { - real_t factor; - real_t new_value; - - factor = (real_t)p_width / (real_t)(p_values[p_index_a] + p_values[p_index_b]); - - new_value = (p_values[p_index_a] * factor); - if (new_value < adapted_values[p_index_a]) { - adapted_values[p_index_a] = new_value; - } - new_value = (p_values[p_index_b] * factor); - if (new_value < adapted_values[p_index_b]) { - adapted_values[p_index_b] = new_value; - } - } else { - adapted_values[p_index_a] = MIN(p_values[p_index_a], adapted_values[p_index_a]); - adapted_values[p_index_b] = MIN(p_values[p_index_b], adapted_values[p_index_b]); - } - adapted_values[p_index_a] = MIN(p_max_a, adapted_values[p_index_a]); - adapted_values[p_index_b] = MIN(p_max_b, adapted_values[p_index_b]); -} - -Rect2 StyleBoxFlat::get_draw_rect(const Rect2 &p_rect) const { - Rect2 draw_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]); - - if (shadow_size > 0) { - Rect2 shadow_rect = draw_rect.grow(shadow_size); - shadow_rect.position += shadow_offset; - draw_rect = draw_rect.merge(shadow_rect); - } - - return draw_rect; -} - -void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const { - bool draw_border = (border_width[0] > 0) || (border_width[1] > 0) || (border_width[2] > 0) || (border_width[3] > 0); - bool draw_shadow = (shadow_size > 0); - if (!draw_border && !draw_center && !draw_shadow) { - return; - } - - Rect2 style_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]); - if (Math::is_zero_approx(style_rect.size.width) || Math::is_zero_approx(style_rect.size.height)) { - return; - } - - const bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0); - // Only enable antialiasing if it is actually needed. This improve performances - // and maximizes sharpness for non-skewed StyleBoxes with sharp corners. - const bool aa_on = (rounded_corners || !skew.is_zero_approx()) && anti_aliased; - - const bool blend_on = blend_border && draw_border; - - Color border_color_alpha = Color(border_color.r, border_color.g, border_color.b, 0); - Color border_color_blend = (draw_center ? bg_color : border_color_alpha); - Color border_color_inner = blend_on ? border_color_blend : border_color; - - // Adapt borders (prevent weird overlapping/glitchy drawings). - real_t width = MAX(style_rect.size.width, 0); - real_t height = MAX(style_rect.size.height, 0); - real_t adapted_border[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 }; - adapt_values(SIDE_TOP, SIDE_BOTTOM, adapted_border, border_width, height, height, height); - adapt_values(SIDE_LEFT, SIDE_RIGHT, adapted_border, border_width, width, width, width); - - // Adapt corners (prevent weird overlapping/glitchy drawings). - real_t adapted_corner[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 }; - adapt_values(CORNER_TOP_RIGHT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]); - adapt_values(CORNER_TOP_LEFT, CORNER_BOTTOM_LEFT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]); - adapt_values(CORNER_TOP_LEFT, CORNER_TOP_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]); - adapt_values(CORNER_BOTTOM_LEFT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]); - - Rect2 infill_rect = style_rect.grow_individual(-adapted_border[SIDE_LEFT], -adapted_border[SIDE_TOP], -adapted_border[SIDE_RIGHT], -adapted_border[SIDE_BOTTOM]); - - Rect2 border_style_rect = style_rect; - if (aa_on) { - for (int i = 0; i < 4; i++) { - if (border_width[i] > 0) { - border_style_rect = border_style_rect.grow_side((Side)i, -aa_size); - } - } - } - - Vector<Point2> verts; - Vector<int> indices; - Vector<Color> colors; - Vector<Point2> uvs; - - // Create shadow - if (draw_shadow) { - Rect2 shadow_inner_rect = style_rect; - shadow_inner_rect.position += shadow_offset; - - Rect2 shadow_rect = style_rect.grow(shadow_size); - shadow_rect.position += shadow_offset; - - Color shadow_color_transparent = Color(shadow_color.r, shadow_color.g, shadow_color.b, 0); - - draw_rounded_rectangle(verts, indices, colors, shadow_inner_rect, adapted_corner, - shadow_rect, shadow_inner_rect, shadow_color, shadow_color_transparent, corner_detail, skew); - - if (draw_center) { - draw_rounded_rectangle(verts, indices, colors, shadow_inner_rect, adapted_corner, - shadow_inner_rect, shadow_inner_rect, shadow_color, shadow_color, corner_detail, skew, true); - } - } - - // Create border (no AA). - if (draw_border && !aa_on) { - draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, - border_style_rect, infill_rect, border_color_inner, border_color, corner_detail, skew); - } - - // Create infill (no AA). - if (draw_center && (!aa_on || blend_on)) { - draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, - infill_rect, infill_rect, bg_color, bg_color, corner_detail, skew, true); - } - - if (aa_on) { - real_t aa_border_width[4]; - real_t aa_border_width_half[4]; - real_t aa_fill_width[4]; - real_t aa_fill_width_half[4]; - if (draw_border) { - for (int i = 0; i < 4; i++) { - if (border_width[i] > 0) { - aa_border_width[i] = aa_size; - aa_border_width_half[i] = aa_size / 2; - aa_fill_width[i] = 0; - aa_fill_width_half[i] = 0; - } else { - aa_border_width[i] = 0; - aa_border_width_half[i] = 0; - aa_fill_width[i] = aa_size; - aa_fill_width_half[i] = aa_size / 2; - } - } - } else { - for (int i = 0; i < 4; i++) { - aa_border_width[i] = 0; - aa_border_width_half[i] = 0; - aa_fill_width[i] = aa_size; - aa_fill_width_half[i] = aa_size / 2; - } - } - - if (draw_center) { - // Infill rect, transparent side of antialiasing gradient (base infill rect enlarged by AA size) - Rect2 infill_rect_aa_transparent = infill_rect.grow_individual(aa_fill_width_half[SIDE_LEFT], aa_fill_width_half[SIDE_TOP], - aa_fill_width_half[SIDE_RIGHT], aa_fill_width_half[SIDE_BOTTOM]); - // Infill rect, colored side of antialiasing gradient (base infill rect shrunk by AA size) - Rect2 infill_rect_aa_colored = infill_rect_aa_transparent.grow_individual(-aa_fill_width[SIDE_LEFT], -aa_fill_width[SIDE_TOP], - -aa_fill_width[SIDE_RIGHT], -aa_fill_width[SIDE_BOTTOM]); - if (!blend_on) { - // Create center fill, not antialiased yet - draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, - infill_rect_aa_colored, infill_rect_aa_colored, bg_color, bg_color, corner_detail, skew, true); - } - if (!blend_on || !draw_border) { - Color alpha_bg = Color(bg_color.r, bg_color.g, bg_color.b, 0); - // Add antialiasing on the center fill - draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, - infill_rect_aa_transparent, infill_rect_aa_colored, bg_color, alpha_bg, corner_detail, skew); - } - } - - if (draw_border) { - // Inner border recct, fully colored side of antialiasing gradient (base inner rect enlarged by AA size) - Rect2 inner_rect_aa_colored = infill_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP], - aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]); - // Inner border rect, transparent side of antialiasing gradient (base inner rect shrunk by AA size) - Rect2 inner_rect_aa_transparent = inner_rect_aa_colored.grow_individual(-aa_border_width[SIDE_LEFT], -aa_border_width[SIDE_TOP], - -aa_border_width[SIDE_RIGHT], -aa_border_width[SIDE_BOTTOM]); - // Outer border rect, transparent side of antialiasing gradient (base outer rect enlarged by AA size) - Rect2 outer_rect_aa_transparent = style_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP], - aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]); - // Outer border rect, colored side of antialiasing gradient (base outer rect shrunk by AA size) - Rect2 outer_rect_aa_colored = border_style_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP], - aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]); - - // Create border ring, not antialiased yet - draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, - outer_rect_aa_colored, ((blend_on) ? infill_rect : inner_rect_aa_colored), border_color_inner, border_color, corner_detail, skew); - if (!blend_on) { - // Add antialiasing on the ring inner border - draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, - inner_rect_aa_colored, inner_rect_aa_transparent, border_color_blend, border_color, corner_detail, skew); - } - // Add antialiasing on the ring outer border - draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, - outer_rect_aa_transparent, outer_rect_aa_colored, border_color, border_color_alpha, corner_detail, skew); - } - } - - // Compute UV coordinates. - Rect2 uv_rect = style_rect.grow(aa_on ? aa_size : 0); - uvs.resize(verts.size()); - for (int i = 0; i < verts.size(); i++) { - uvs.write[i].x = (verts[i].x - uv_rect.position.x) / uv_rect.size.width; - uvs.write[i].y = (verts[i].y - uv_rect.position.y) / uv_rect.size.height; - } - - // Draw stylebox. - RenderingServer *vs = RenderingServer::get_singleton(); - vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors, uvs); -} - -float StyleBoxFlat::get_style_margin(Side p_side) const { - ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - return border_width[p_side]; -} - -void StyleBoxFlat::_validate_property(PropertyInfo &p_property) const { - if (!anti_aliased && p_property.name == "anti_aliasing_size") { - p_property.usage = PROPERTY_USAGE_NO_EDITOR; - } -} - -void StyleBoxFlat::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color); - ClassDB::bind_method(D_METHOD("get_bg_color"), &StyleBoxFlat::get_bg_color); - - ClassDB::bind_method(D_METHOD("set_border_color", "color"), &StyleBoxFlat::set_border_color); - ClassDB::bind_method(D_METHOD("get_border_color"), &StyleBoxFlat::get_border_color); - - ClassDB::bind_method(D_METHOD("set_border_width_all", "width"), &StyleBoxFlat::set_border_width_all); - ClassDB::bind_method(D_METHOD("get_border_width_min"), &StyleBoxFlat::get_border_width_min); - - ClassDB::bind_method(D_METHOD("set_border_width", "margin", "width"), &StyleBoxFlat::set_border_width); - ClassDB::bind_method(D_METHOD("get_border_width", "margin"), &StyleBoxFlat::get_border_width); - - ClassDB::bind_method(D_METHOD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend); - ClassDB::bind_method(D_METHOD("get_border_blend"), &StyleBoxFlat::get_border_blend); - - ClassDB::bind_method(D_METHOD("set_corner_radius_all", "radius"), &StyleBoxFlat::set_corner_radius_all); - - ClassDB::bind_method(D_METHOD("set_corner_radius", "corner", "radius"), &StyleBoxFlat::set_corner_radius); - ClassDB::bind_method(D_METHOD("get_corner_radius", "corner"), &StyleBoxFlat::get_corner_radius); - - ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin); - ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_all); - ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin); - - ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &StyleBoxFlat::set_draw_center); - ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxFlat::is_draw_center_enabled); - - ClassDB::bind_method(D_METHOD("set_skew", "skew"), &StyleBoxFlat::set_skew); - ClassDB::bind_method(D_METHOD("get_skew"), &StyleBoxFlat::get_skew); - - ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &StyleBoxFlat::set_shadow_color); - ClassDB::bind_method(D_METHOD("get_shadow_color"), &StyleBoxFlat::get_shadow_color); - - ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &StyleBoxFlat::set_shadow_size); - ClassDB::bind_method(D_METHOD("get_shadow_size"), &StyleBoxFlat::get_shadow_size); - - ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &StyleBoxFlat::set_shadow_offset); - ClassDB::bind_method(D_METHOD("get_shadow_offset"), &StyleBoxFlat::get_shadow_offset); - - ClassDB::bind_method(D_METHOD("set_anti_aliased", "anti_aliased"), &StyleBoxFlat::set_anti_aliased); - ClassDB::bind_method(D_METHOD("is_anti_aliased"), &StyleBoxFlat::is_anti_aliased); - - ClassDB::bind_method(D_METHOD("set_aa_size", "size"), &StyleBoxFlat::set_aa_size); - ClassDB::bind_method(D_METHOD("get_aa_size"), &StyleBoxFlat::get_aa_size); - - ClassDB::bind_method(D_METHOD("set_corner_detail", "detail"), &StyleBoxFlat::set_corner_detail); - ClassDB::bind_method(D_METHOD("get_corner_detail"), &StyleBoxFlat::get_corner_detail); - - ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), "set_bg_color", "get_bg_color"); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "skew"), "set_skew", "get_skew"); - - ADD_GROUP("Border Width", "border_width_"); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_top", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_TOP); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_bottom", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_BOTTOM); - - ADD_GROUP("Border", "border_"); - ADD_PROPERTY(PropertyInfo(Variant::COLOR, "border_color"), "set_border_color", "get_border_color"); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), "set_border_blend", "get_border_blend"); - - ADD_GROUP("Corner Radius", "corner_radius_"); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_LEFT); - - ADD_PROPERTY(PropertyInfo(Variant::INT, "corner_detail", PROPERTY_HINT_RANGE, "1,20,1"), "set_corner_detail", "get_corner_detail"); - - ADD_GROUP("Expand Margins", "expand_margin_"); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM); - - ADD_GROUP("Shadow", "shadow_"); - ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_shadow_size", "get_shadow_size"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_shadow_offset", "get_shadow_offset"); - - ADD_GROUP("Anti Aliasing", "anti_aliasing_"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "anti_aliasing"), "set_anti_aliased", "is_anti_aliased"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "anti_aliasing_size", PROPERTY_HINT_RANGE, "0.01,10,0.001,suffix:px"), "set_aa_size", "get_aa_size"); -} - -StyleBoxFlat::StyleBoxFlat() {} - -StyleBoxFlat::~StyleBoxFlat() {} - -void StyleBoxLine::set_color(const Color &p_color) { - color = p_color; - emit_changed(); -} - -Color StyleBoxLine::get_color() const { - return color; -} - -void StyleBoxLine::set_thickness(int p_thickness) { - thickness = p_thickness; - emit_changed(); -} - -int StyleBoxLine::get_thickness() const { - return thickness; -} - -void StyleBoxLine::set_vertical(bool p_vertical) { - vertical = p_vertical; - emit_changed(); -} - -bool StyleBoxLine::is_vertical() const { - return vertical; -} - -void StyleBoxLine::set_grow_end(float p_grow_end) { - grow_end = p_grow_end; - emit_changed(); -} - -float StyleBoxLine::get_grow_end() const { - return grow_end; -} - -void StyleBoxLine::set_grow_begin(float p_grow_begin) { - grow_begin = p_grow_begin; - emit_changed(); -} - -float StyleBoxLine::get_grow_begin() const { - return grow_begin; -} - -void StyleBoxLine::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_color", "color"), &StyleBoxLine::set_color); - ClassDB::bind_method(D_METHOD("get_color"), &StyleBoxLine::get_color); - ClassDB::bind_method(D_METHOD("set_thickness", "thickness"), &StyleBoxLine::set_thickness); - ClassDB::bind_method(D_METHOD("get_thickness"), &StyleBoxLine::get_thickness); - ClassDB::bind_method(D_METHOD("set_grow_begin", "offset"), &StyleBoxLine::set_grow_begin); - ClassDB::bind_method(D_METHOD("get_grow_begin"), &StyleBoxLine::get_grow_begin); - ClassDB::bind_method(D_METHOD("set_grow_end", "offset"), &StyleBoxLine::set_grow_end); - ClassDB::bind_method(D_METHOD("get_grow_end"), &StyleBoxLine::get_grow_end); - ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &StyleBoxLine::set_vertical); - ClassDB::bind_method(D_METHOD("is_vertical"), &StyleBoxLine::is_vertical); - - ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_begin", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_begin", "get_grow_begin"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_end", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_end", "get_grow_end"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "thickness", PROPERTY_HINT_RANGE, "0,100,suffix:px"), "set_thickness", "get_thickness"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical"); -} - -float StyleBoxLine::get_style_margin(Side p_side) const { - ERR_FAIL_INDEX_V((int)p_side, 4, 0); - - if (vertical) { - if (p_side == SIDE_LEFT || p_side == SIDE_RIGHT) { - return thickness / 2.0; - } - } else if (p_side == SIDE_TOP || p_side == SIDE_BOTTOM) { - return thickness / 2.0; - } - - return 0; -} - -void StyleBoxLine::draw(RID p_canvas_item, const Rect2 &p_rect) const { - RenderingServer *vs = RenderingServer::get_singleton(); - Rect2i r = p_rect; - - if (vertical) { - r.position.y -= grow_begin; - r.size.y += (grow_begin + grow_end); - r.size.x = thickness; - } else { - r.position.x -= grow_begin; - r.size.x += (grow_begin + grow_end); - r.size.y = thickness; - } - - vs->canvas_item_add_rect(p_canvas_item, r, color); -} - -StyleBoxLine::StyleBoxLine() {} - -StyleBoxLine::~StyleBoxLine() {} diff --git a/scene/resources/style_box.h b/scene/resources/style_box.h index 9d96e9a3b7..3f9a96be2f 100644 --- a/scene/resources/style_box.h +++ b/scene/resources/style_box.h @@ -32,8 +32,9 @@ #define STYLE_BOX_H #include "core/io/resource.h" -#include "scene/resources/texture.h" -#include "servers/rendering_server.h" +#include "core/object/class_db.h" +#include "core/object/gdvirtual.gen.inc" +#include "core/object/script_language.h" class CanvasItem; @@ -41,11 +42,12 @@ class StyleBox : public Resource { GDCLASS(StyleBox, Resource); RES_BASE_EXTENSION("stylebox"); OBJ_SAVE_TYPE(StyleBox); + float content_margin[4]; protected: - virtual float get_style_margin(Side p_side) const { return 0; } static void _bind_methods(); + virtual float get_style_margin(Side p_side) const { return 0; } GDVIRTUAL2C(_draw, RID, Rect2) GDVIRTUAL1RC(Rect2, _get_draw_rect, Rect2) @@ -82,184 +84,4 @@ public: StyleBoxEmpty() {} }; -class StyleBoxTexture : public StyleBox { - GDCLASS(StyleBoxTexture, StyleBox); - -public: - enum AxisStretchMode { - AXIS_STRETCH_MODE_STRETCH, - AXIS_STRETCH_MODE_TILE, - AXIS_STRETCH_MODE_TILE_FIT, - }; - -private: - float expand_margin[4] = {}; - float texture_margin[4] = {}; - Rect2 region_rect; - Ref<Texture2D> texture; - bool draw_center = true; - Color modulate = Color(1, 1, 1, 1); - AxisStretchMode axis_h = AXIS_STRETCH_MODE_STRETCH; - AxisStretchMode axis_v = AXIS_STRETCH_MODE_STRETCH; - -protected: - virtual float get_style_margin(Side p_side) const override; - static void _bind_methods(); - -public: - void set_expand_margin(Side p_expand_side, float p_size); - void set_expand_margin_all(float p_expand_margin_size); - void set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom); - float get_expand_margin(Side p_expand_side) const; - - void set_texture_margin(Side p_side, float p_size); - void set_texture_margin_all(float p_size); - void set_texture_margin_individual(float p_left, float p_top, float p_right, float p_bottom); - float get_texture_margin(Side p_side) const; - - void set_region_rect(const Rect2 &p_region_rect); - Rect2 get_region_rect() const; - - void set_texture(Ref<Texture2D> p_texture); - Ref<Texture2D> get_texture() const; - - void set_draw_center(bool p_enabled); - bool is_draw_center_enabled() const; - - void set_h_axis_stretch_mode(AxisStretchMode p_mode); - AxisStretchMode get_h_axis_stretch_mode() const; - - void set_v_axis_stretch_mode(AxisStretchMode p_mode); - AxisStretchMode get_v_axis_stretch_mode() const; - - void set_modulate(const Color &p_modulate); - Color get_modulate() const; - - virtual Rect2 get_draw_rect(const Rect2 &p_rect) const override; - virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; - - StyleBoxTexture(); - ~StyleBoxTexture(); -}; - -VARIANT_ENUM_CAST(StyleBoxTexture::AxisStretchMode) - -class StyleBoxFlat : public StyleBox { - GDCLASS(StyleBoxFlat, StyleBox); - - Color bg_color = Color(0.6, 0.6, 0.6); - Color shadow_color = Color(0, 0, 0, 0.6); - Color border_color = Color(0.8, 0.8, 0.8); - - real_t border_width[4] = {}; - real_t expand_margin[4] = {}; - real_t corner_radius[4] = {}; - - bool draw_center = true; - bool blend_border = false; - Vector2 skew; - bool anti_aliased = true; - - int corner_detail = 8; - int shadow_size = 0; - Point2 shadow_offset; - real_t aa_size = 1; - -protected: - virtual float get_style_margin(Side p_side) const override; - static void _bind_methods(); - void _validate_property(PropertyInfo &p_property) const; - -public: - void set_bg_color(const Color &p_color); - Color get_bg_color() const; - - void set_border_color(const Color &p_color); - Color get_border_color() const; - - void set_border_width_all(int p_size); - int get_border_width_min() const; - - void set_border_width(Side p_side, int p_width); - int get_border_width(Side p_side) const; - - void set_border_blend(bool p_blend); - bool get_border_blend() const; - - void set_corner_radius_all(int radius); - void set_corner_radius_individual(const int radius_top_left, const int radius_top_right, const int radius_bottom_right, const int radius_bottom_left); - - void set_corner_radius(Corner p_corner, const int radius); - int get_corner_radius(Corner p_corner) const; - - void set_corner_detail(const int &p_corner_detail); - int get_corner_detail() const; - - void set_expand_margin(Side p_expand_side, float p_size); - void set_expand_margin_all(float p_expand_margin_size); - void set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom); - float get_expand_margin(Side p_expand_side) const; - - void set_draw_center(bool p_enabled); - bool is_draw_center_enabled() const; - - void set_skew(Vector2 p_skew); - Vector2 get_skew() const; - - void set_shadow_color(const Color &p_color); - Color get_shadow_color() const; - - void set_shadow_size(const int &p_size); - int get_shadow_size() const; - - void set_shadow_offset(const Point2 &p_offset); - Point2 get_shadow_offset() const; - - void set_anti_aliased(const bool &p_anti_aliased); - bool is_anti_aliased() const; - void set_aa_size(const real_t p_aa_size); - real_t get_aa_size() const; - - virtual Rect2 get_draw_rect(const Rect2 &p_rect) const override; - virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; - - StyleBoxFlat(); - ~StyleBoxFlat(); -}; - -// Just used to draw lines. -class StyleBoxLine : public StyleBox { - GDCLASS(StyleBoxLine, StyleBox); - Color color; - int thickness = 1; - bool vertical = false; - float grow_begin = 1.0; - float grow_end = 1.0; - -protected: - virtual float get_style_margin(Side p_side) const override; - static void _bind_methods(); - -public: - void set_color(const Color &p_color); - Color get_color() const; - - void set_thickness(int p_thickness); - int get_thickness() const; - - void set_vertical(bool p_vertical); - bool is_vertical() const; - - void set_grow_begin(float p_grow); - float get_grow_begin() const; - - void set_grow_end(float p_grow); - float get_grow_end() const; - - virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; - - StyleBoxLine(); - ~StyleBoxLine(); -}; - #endif // STYLE_BOX_H diff --git a/scene/resources/style_box_flat.cpp b/scene/resources/style_box_flat.cpp new file mode 100644 index 0000000000..52d02e92cb --- /dev/null +++ b/scene/resources/style_box_flat.cpp @@ -0,0 +1,642 @@ +/**************************************************************************/ +/* style_box_flat.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. */ +/**************************************************************************/ + +#include "style_box_flat.h" + +#include "servers/rendering_server.h" + +float StyleBoxFlat::get_style_margin(Side p_side) const { + ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); + return border_width[p_side]; +} + +void StyleBoxFlat::_validate_property(PropertyInfo &p_property) const { + if (!anti_aliased && p_property.name == "anti_aliasing_size") { + p_property.usage = PROPERTY_USAGE_NO_EDITOR; + } +} + +void StyleBoxFlat::set_bg_color(const Color &p_color) { + bg_color = p_color; + emit_changed(); +} + +Color StyleBoxFlat::get_bg_color() const { + return bg_color; +} + +void StyleBoxFlat::set_border_color(const Color &p_color) { + border_color = p_color; + emit_changed(); +} + +Color StyleBoxFlat::get_border_color() const { + return border_color; +} + +void StyleBoxFlat::set_border_width_all(int p_size) { + border_width[0] = p_size; + border_width[1] = p_size; + border_width[2] = p_size; + border_width[3] = p_size; + emit_changed(); +} + +int StyleBoxFlat::get_border_width_min() const { + return MIN(MIN(border_width[0], border_width[1]), MIN(border_width[2], border_width[3])); +} + +void StyleBoxFlat::set_border_width(Side p_side, int p_width) { + ERR_FAIL_INDEX((int)p_side, 4); + border_width[p_side] = p_width; + emit_changed(); +} + +int StyleBoxFlat::get_border_width(Side p_side) const { + ERR_FAIL_INDEX_V((int)p_side, 4, 0); + return border_width[p_side]; +} + +void StyleBoxFlat::set_border_blend(bool p_blend) { + blend_border = p_blend; + emit_changed(); +} + +bool StyleBoxFlat::get_border_blend() const { + return blend_border; +} + +void StyleBoxFlat::set_corner_radius(const Corner p_corner, const int radius) { + ERR_FAIL_INDEX((int)p_corner, 4); + corner_radius[p_corner] = radius; + emit_changed(); +} + +void StyleBoxFlat::set_corner_radius_all(int radius) { + for (int i = 0; i < 4; i++) { + corner_radius[i] = radius; + } + + emit_changed(); +} + +void StyleBoxFlat::set_corner_radius_individual(const int radius_top_left, const int radius_top_right, const int radius_bottom_right, const int radius_bottom_left) { + corner_radius[0] = radius_top_left; + corner_radius[1] = radius_top_right; + corner_radius[2] = radius_bottom_right; + corner_radius[3] = radius_bottom_left; + + emit_changed(); +} + +int StyleBoxFlat::get_corner_radius(const Corner p_corner) const { + ERR_FAIL_INDEX_V((int)p_corner, 4, 0); + return corner_radius[p_corner]; +} + +void StyleBoxFlat::set_corner_detail(const int &p_corner_detail) { + corner_detail = CLAMP(p_corner_detail, 1, 20); + emit_changed(); +} + +int StyleBoxFlat::get_corner_detail() const { + return corner_detail; +} + +void StyleBoxFlat::set_expand_margin(Side p_side, float p_size) { + ERR_FAIL_INDEX((int)p_side, 4); + expand_margin[p_side] = p_size; + emit_changed(); +} + +void StyleBoxFlat::set_expand_margin_all(float p_expand_margin_size) { + for (int i = 0; i < 4; i++) { + expand_margin[i] = p_expand_margin_size; + } + emit_changed(); +} + +void StyleBoxFlat::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { + expand_margin[SIDE_LEFT] = p_left; + expand_margin[SIDE_TOP] = p_top; + expand_margin[SIDE_RIGHT] = p_right; + expand_margin[SIDE_BOTTOM] = p_bottom; + emit_changed(); +} + +float StyleBoxFlat::get_expand_margin(Side p_side) const { + ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); + return expand_margin[p_side]; +} + +void StyleBoxFlat::set_draw_center(bool p_enabled) { + draw_center = p_enabled; + emit_changed(); +} + +bool StyleBoxFlat::is_draw_center_enabled() const { + return draw_center; +} + +void StyleBoxFlat::set_skew(Vector2 p_skew) { + skew = p_skew; + emit_changed(); +} + +Vector2 StyleBoxFlat::get_skew() const { + return skew; +} + +void StyleBoxFlat::set_shadow_color(const Color &p_color) { + shadow_color = p_color; + emit_changed(); +} + +Color StyleBoxFlat::get_shadow_color() const { + return shadow_color; +} + +void StyleBoxFlat::set_shadow_size(const int &p_size) { + shadow_size = p_size; + emit_changed(); +} + +int StyleBoxFlat::get_shadow_size() const { + return shadow_size; +} + +void StyleBoxFlat::set_shadow_offset(const Point2 &p_offset) { + shadow_offset = p_offset; + emit_changed(); +} + +Point2 StyleBoxFlat::get_shadow_offset() const { + return shadow_offset; +} + +void StyleBoxFlat::set_anti_aliased(const bool &p_anti_aliased) { + anti_aliased = p_anti_aliased; + emit_changed(); + notify_property_list_changed(); +} + +bool StyleBoxFlat::is_anti_aliased() const { + return anti_aliased; +} + +void StyleBoxFlat::set_aa_size(const real_t p_aa_size) { + aa_size = CLAMP(p_aa_size, 0.01, 10); + emit_changed(); +} + +real_t StyleBoxFlat::get_aa_size() const { + return aa_size; +} + +inline void set_inner_corner_radius(const Rect2 style_rect, const Rect2 inner_rect, const real_t corner_radius[4], real_t *inner_corner_radius) { + real_t border_left = inner_rect.position.x - style_rect.position.x; + real_t border_top = inner_rect.position.y - style_rect.position.y; + real_t border_right = style_rect.size.width - inner_rect.size.width - border_left; + real_t border_bottom = style_rect.size.height - inner_rect.size.height - border_top; + + real_t rad; + + // Top left. + rad = MIN(border_top, border_left); + inner_corner_radius[0] = MAX(corner_radius[0] - rad, 0); + + // Top right; + rad = MIN(border_top, border_right); + inner_corner_radius[1] = MAX(corner_radius[1] - rad, 0); + + // Bottom right. + rad = MIN(border_bottom, border_right); + inner_corner_radius[2] = MAX(corner_radius[2] - rad, 0); + + // Bottom left. + rad = MIN(border_bottom, border_left); + inner_corner_radius[3] = MAX(corner_radius[3] - rad, 0); +} + +inline void draw_rounded_rectangle(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color> &colors, const Rect2 &style_rect, const real_t corner_radius[4], + const Rect2 &ring_rect, const Rect2 &inner_rect, const Color &inner_color, const Color &outer_color, const int corner_detail, const Vector2 &skew, bool is_filled = false) { + int vert_offset = verts.size(); + if (!vert_offset) { + vert_offset = 0; + } + + int adapted_corner_detail = (corner_radius[0] == 0 && corner_radius[1] == 0 && corner_radius[2] == 0 && corner_radius[3] == 0) ? 1 : corner_detail; + + bool draw_border = !is_filled; + + real_t ring_corner_radius[4]; + set_inner_corner_radius(style_rect, ring_rect, corner_radius, ring_corner_radius); + + // Corner radius center points. + Vector<Point2> outer_points = { + ring_rect.position + Vector2(ring_corner_radius[0], ring_corner_radius[0]), //tl + Point2(ring_rect.position.x + ring_rect.size.x - ring_corner_radius[1], ring_rect.position.y + ring_corner_radius[1]), //tr + ring_rect.position + ring_rect.size - Vector2(ring_corner_radius[2], ring_corner_radius[2]), //br + Point2(ring_rect.position.x + ring_corner_radius[3], ring_rect.position.y + ring_rect.size.y - ring_corner_radius[3]) //bl + }; + + real_t inner_corner_radius[4]; + set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius); + + Vector<Point2> inner_points = { + inner_rect.position + Vector2(inner_corner_radius[0], inner_corner_radius[0]), //tl + Point2(inner_rect.position.x + inner_rect.size.x - inner_corner_radius[1], inner_rect.position.y + inner_corner_radius[1]), //tr + inner_rect.position + inner_rect.size - Vector2(inner_corner_radius[2], inner_corner_radius[2]), //br + Point2(inner_rect.position.x + inner_corner_radius[3], inner_rect.position.y + inner_rect.size.y - inner_corner_radius[3]) //bl + }; + // Calculate the vertices. + + // If the center is filled, we do not draw the border and directly use the inner ring as reference. Because all calls to this + // method either draw a ring or a filled rounded rectangle, but not both. + int max_inner_outer = draw_border ? 2 : 1; + + for (int corner_index = 0; corner_index < 4; corner_index++) { + for (int detail = 0; detail <= adapted_corner_detail; detail++) { + for (int inner_outer = 0; inner_outer < max_inner_outer; inner_outer++) { + real_t radius; + Color color; + Point2 corner_point; + if (inner_outer == 0) { + radius = inner_corner_radius[corner_index]; + color = inner_color; + corner_point = inner_points[corner_index]; + } else { + radius = ring_corner_radius[corner_index]; + color = outer_color; + corner_point = outer_points[corner_index]; + } + + const real_t x = radius * (real_t)cos((corner_index + detail / (double)adapted_corner_detail) * (Math_TAU / 4.0) + Math_PI) + corner_point.x; + const real_t y = radius * (real_t)sin((corner_index + detail / (double)adapted_corner_detail) * (Math_TAU / 4.0) + Math_PI) + corner_point.y; + const float x_skew = -skew.x * (y - ring_rect.get_center().y); + const float y_skew = -skew.y * (x - ring_rect.get_center().x); + verts.push_back(Vector2(x + x_skew, y + y_skew)); + colors.push_back(color); + } + } + } + + int ring_vert_count = verts.size() - vert_offset; + + // Fill the indices and the colors for the border. + + if (draw_border) { + for (int i = 0; i < ring_vert_count; i++) { + indices.push_back(vert_offset + ((i + 0) % ring_vert_count)); + indices.push_back(vert_offset + ((i + 2) % ring_vert_count)); + indices.push_back(vert_offset + ((i + 1) % ring_vert_count)); + } + } + + if (is_filled) { + // Compute the triangles pattern to draw the rounded rectangle. + // Consists of vertical stripes of two triangles each. + + int stripes_count = ring_vert_count / 2 - 1; + int last_vert_id = ring_vert_count - 1; + + for (int i = 0; i < stripes_count; i++) { + // Polygon 1. + indices.push_back(vert_offset + i); + indices.push_back(vert_offset + last_vert_id - i - 1); + indices.push_back(vert_offset + i + 1); + // Polygon 2. + indices.push_back(vert_offset + i); + indices.push_back(vert_offset + last_vert_id - 0 - i); + indices.push_back(vert_offset + last_vert_id - 1 - i); + } + } +} + +inline void adapt_values(int p_index_a, int p_index_b, real_t *adapted_values, const real_t *p_values, const real_t p_width, const real_t p_max_a, const real_t p_max_b) { + if (p_values[p_index_a] + p_values[p_index_b] > p_width) { + real_t factor; + real_t new_value; + + factor = (real_t)p_width / (real_t)(p_values[p_index_a] + p_values[p_index_b]); + + new_value = (p_values[p_index_a] * factor); + if (new_value < adapted_values[p_index_a]) { + adapted_values[p_index_a] = new_value; + } + new_value = (p_values[p_index_b] * factor); + if (new_value < adapted_values[p_index_b]) { + adapted_values[p_index_b] = new_value; + } + } else { + adapted_values[p_index_a] = MIN(p_values[p_index_a], adapted_values[p_index_a]); + adapted_values[p_index_b] = MIN(p_values[p_index_b], adapted_values[p_index_b]); + } + adapted_values[p_index_a] = MIN(p_max_a, adapted_values[p_index_a]); + adapted_values[p_index_b] = MIN(p_max_b, adapted_values[p_index_b]); +} + +Rect2 StyleBoxFlat::get_draw_rect(const Rect2 &p_rect) const { + Rect2 draw_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]); + + if (shadow_size > 0) { + Rect2 shadow_rect = draw_rect.grow(shadow_size); + shadow_rect.position += shadow_offset; + draw_rect = draw_rect.merge(shadow_rect); + } + + return draw_rect; +} + +void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const { + bool draw_border = (border_width[0] > 0) || (border_width[1] > 0) || (border_width[2] > 0) || (border_width[3] > 0); + bool draw_shadow = (shadow_size > 0); + if (!draw_border && !draw_center && !draw_shadow) { + return; + } + + Rect2 style_rect = p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]); + if (Math::is_zero_approx(style_rect.size.width) || Math::is_zero_approx(style_rect.size.height)) { + return; + } + + const bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0); + // Only enable antialiasing if it is actually needed. This improve performances + // and maximizes sharpness for non-skewed StyleBoxes with sharp corners. + const bool aa_on = (rounded_corners || !skew.is_zero_approx()) && anti_aliased; + + const bool blend_on = blend_border && draw_border; + + Color border_color_alpha = Color(border_color.r, border_color.g, border_color.b, 0); + Color border_color_blend = (draw_center ? bg_color : border_color_alpha); + Color border_color_inner = blend_on ? border_color_blend : border_color; + + // Adapt borders (prevent weird overlapping/glitchy drawings). + real_t width = MAX(style_rect.size.width, 0); + real_t height = MAX(style_rect.size.height, 0); + real_t adapted_border[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 }; + adapt_values(SIDE_TOP, SIDE_BOTTOM, adapted_border, border_width, height, height, height); + adapt_values(SIDE_LEFT, SIDE_RIGHT, adapted_border, border_width, width, width, width); + + // Adapt corners (prevent weird overlapping/glitchy drawings). + real_t adapted_corner[4] = { 1000000.0, 1000000.0, 1000000.0, 1000000.0 }; + adapt_values(CORNER_TOP_RIGHT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]); + adapt_values(CORNER_TOP_LEFT, CORNER_BOTTOM_LEFT, adapted_corner, corner_radius, height, height - adapted_border[SIDE_BOTTOM], height - adapted_border[SIDE_TOP]); + adapt_values(CORNER_TOP_LEFT, CORNER_TOP_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]); + adapt_values(CORNER_BOTTOM_LEFT, CORNER_BOTTOM_RIGHT, adapted_corner, corner_radius, width, width - adapted_border[SIDE_RIGHT], width - adapted_border[SIDE_LEFT]); + + Rect2 infill_rect = style_rect.grow_individual(-adapted_border[SIDE_LEFT], -adapted_border[SIDE_TOP], -adapted_border[SIDE_RIGHT], -adapted_border[SIDE_BOTTOM]); + + Rect2 border_style_rect = style_rect; + if (aa_on) { + for (int i = 0; i < 4; i++) { + if (border_width[i] > 0) { + border_style_rect = border_style_rect.grow_side((Side)i, -aa_size); + } + } + } + + Vector<Point2> verts; + Vector<int> indices; + Vector<Color> colors; + Vector<Point2> uvs; + + // Create shadow + if (draw_shadow) { + Rect2 shadow_inner_rect = style_rect; + shadow_inner_rect.position += shadow_offset; + + Rect2 shadow_rect = style_rect.grow(shadow_size); + shadow_rect.position += shadow_offset; + + Color shadow_color_transparent = Color(shadow_color.r, shadow_color.g, shadow_color.b, 0); + + draw_rounded_rectangle(verts, indices, colors, shadow_inner_rect, adapted_corner, + shadow_rect, shadow_inner_rect, shadow_color, shadow_color_transparent, corner_detail, skew); + + if (draw_center) { + draw_rounded_rectangle(verts, indices, colors, shadow_inner_rect, adapted_corner, + shadow_inner_rect, shadow_inner_rect, shadow_color, shadow_color, corner_detail, skew, true); + } + } + + // Create border (no AA). + if (draw_border && !aa_on) { + draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, + border_style_rect, infill_rect, border_color_inner, border_color, corner_detail, skew); + } + + // Create infill (no AA). + if (draw_center && (!aa_on || blend_on)) { + draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, + infill_rect, infill_rect, bg_color, bg_color, corner_detail, skew, true); + } + + if (aa_on) { + real_t aa_border_width[4]; + real_t aa_border_width_half[4]; + real_t aa_fill_width[4]; + real_t aa_fill_width_half[4]; + if (draw_border) { + for (int i = 0; i < 4; i++) { + if (border_width[i] > 0) { + aa_border_width[i] = aa_size; + aa_border_width_half[i] = aa_size / 2; + aa_fill_width[i] = 0; + aa_fill_width_half[i] = 0; + } else { + aa_border_width[i] = 0; + aa_border_width_half[i] = 0; + aa_fill_width[i] = aa_size; + aa_fill_width_half[i] = aa_size / 2; + } + } + } else { + for (int i = 0; i < 4; i++) { + aa_border_width[i] = 0; + aa_border_width_half[i] = 0; + aa_fill_width[i] = aa_size; + aa_fill_width_half[i] = aa_size / 2; + } + } + + if (draw_center) { + // Infill rect, transparent side of antialiasing gradient (base infill rect enlarged by AA size) + Rect2 infill_rect_aa_transparent = infill_rect.grow_individual(aa_fill_width_half[SIDE_LEFT], aa_fill_width_half[SIDE_TOP], + aa_fill_width_half[SIDE_RIGHT], aa_fill_width_half[SIDE_BOTTOM]); + // Infill rect, colored side of antialiasing gradient (base infill rect shrunk by AA size) + Rect2 infill_rect_aa_colored = infill_rect_aa_transparent.grow_individual(-aa_fill_width[SIDE_LEFT], -aa_fill_width[SIDE_TOP], + -aa_fill_width[SIDE_RIGHT], -aa_fill_width[SIDE_BOTTOM]); + if (!blend_on) { + // Create center fill, not antialiased yet + draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, + infill_rect_aa_colored, infill_rect_aa_colored, bg_color, bg_color, corner_detail, skew, true); + } + if (!blend_on || !draw_border) { + Color alpha_bg = Color(bg_color.r, bg_color.g, bg_color.b, 0); + // Add antialiasing on the center fill + draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, + infill_rect_aa_transparent, infill_rect_aa_colored, bg_color, alpha_bg, corner_detail, skew); + } + } + + if (draw_border) { + // Inner border recct, fully colored side of antialiasing gradient (base inner rect enlarged by AA size) + Rect2 inner_rect_aa_colored = infill_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP], + aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]); + // Inner border rect, transparent side of antialiasing gradient (base inner rect shrunk by AA size) + Rect2 inner_rect_aa_transparent = inner_rect_aa_colored.grow_individual(-aa_border_width[SIDE_LEFT], -aa_border_width[SIDE_TOP], + -aa_border_width[SIDE_RIGHT], -aa_border_width[SIDE_BOTTOM]); + // Outer border rect, transparent side of antialiasing gradient (base outer rect enlarged by AA size) + Rect2 outer_rect_aa_transparent = style_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP], + aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]); + // Outer border rect, colored side of antialiasing gradient (base outer rect shrunk by AA size) + Rect2 outer_rect_aa_colored = border_style_rect.grow_individual(aa_border_width_half[SIDE_LEFT], aa_border_width_half[SIDE_TOP], + aa_border_width_half[SIDE_RIGHT], aa_border_width_half[SIDE_BOTTOM]); + + // Create border ring, not antialiased yet + draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, + outer_rect_aa_colored, ((blend_on) ? infill_rect : inner_rect_aa_colored), border_color_inner, border_color, corner_detail, skew); + if (!blend_on) { + // Add antialiasing on the ring inner border + draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, + inner_rect_aa_colored, inner_rect_aa_transparent, border_color_blend, border_color, corner_detail, skew); + } + // Add antialiasing on the ring outer border + draw_rounded_rectangle(verts, indices, colors, border_style_rect, adapted_corner, + outer_rect_aa_transparent, outer_rect_aa_colored, border_color, border_color_alpha, corner_detail, skew); + } + } + + // Compute UV coordinates. + Rect2 uv_rect = style_rect.grow(aa_on ? aa_size : 0); + uvs.resize(verts.size()); + for (int i = 0; i < verts.size(); i++) { + uvs.write[i].x = (verts[i].x - uv_rect.position.x) / uv_rect.size.width; + uvs.write[i].y = (verts[i].y - uv_rect.position.y) / uv_rect.size.height; + } + + // Draw stylebox. + RenderingServer *vs = RenderingServer::get_singleton(); + vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors, uvs); +} + +void StyleBoxFlat::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_bg_color", "color"), &StyleBoxFlat::set_bg_color); + ClassDB::bind_method(D_METHOD("get_bg_color"), &StyleBoxFlat::get_bg_color); + + ClassDB::bind_method(D_METHOD("set_border_color", "color"), &StyleBoxFlat::set_border_color); + ClassDB::bind_method(D_METHOD("get_border_color"), &StyleBoxFlat::get_border_color); + + ClassDB::bind_method(D_METHOD("set_border_width_all", "width"), &StyleBoxFlat::set_border_width_all); + ClassDB::bind_method(D_METHOD("get_border_width_min"), &StyleBoxFlat::get_border_width_min); + + ClassDB::bind_method(D_METHOD("set_border_width", "margin", "width"), &StyleBoxFlat::set_border_width); + ClassDB::bind_method(D_METHOD("get_border_width", "margin"), &StyleBoxFlat::get_border_width); + + ClassDB::bind_method(D_METHOD("set_border_blend", "blend"), &StyleBoxFlat::set_border_blend); + ClassDB::bind_method(D_METHOD("get_border_blend"), &StyleBoxFlat::get_border_blend); + + ClassDB::bind_method(D_METHOD("set_corner_radius_all", "radius"), &StyleBoxFlat::set_corner_radius_all); + + ClassDB::bind_method(D_METHOD("set_corner_radius", "corner", "radius"), &StyleBoxFlat::set_corner_radius); + ClassDB::bind_method(D_METHOD("get_corner_radius", "corner"), &StyleBoxFlat::get_corner_radius); + + ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin); + ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_all); + ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin); + + ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &StyleBoxFlat::set_draw_center); + ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxFlat::is_draw_center_enabled); + + ClassDB::bind_method(D_METHOD("set_skew", "skew"), &StyleBoxFlat::set_skew); + ClassDB::bind_method(D_METHOD("get_skew"), &StyleBoxFlat::get_skew); + + ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &StyleBoxFlat::set_shadow_color); + ClassDB::bind_method(D_METHOD("get_shadow_color"), &StyleBoxFlat::get_shadow_color); + + ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &StyleBoxFlat::set_shadow_size); + ClassDB::bind_method(D_METHOD("get_shadow_size"), &StyleBoxFlat::get_shadow_size); + + ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &StyleBoxFlat::set_shadow_offset); + ClassDB::bind_method(D_METHOD("get_shadow_offset"), &StyleBoxFlat::get_shadow_offset); + + ClassDB::bind_method(D_METHOD("set_anti_aliased", "anti_aliased"), &StyleBoxFlat::set_anti_aliased); + ClassDB::bind_method(D_METHOD("is_anti_aliased"), &StyleBoxFlat::is_anti_aliased); + + ClassDB::bind_method(D_METHOD("set_aa_size", "size"), &StyleBoxFlat::set_aa_size); + ClassDB::bind_method(D_METHOD("get_aa_size"), &StyleBoxFlat::get_aa_size); + + ClassDB::bind_method(D_METHOD("set_corner_detail", "detail"), &StyleBoxFlat::set_corner_detail); + ClassDB::bind_method(D_METHOD("get_corner_detail"), &StyleBoxFlat::get_corner_detail); + + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), "set_bg_color", "get_bg_color"); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "skew"), "set_skew", "get_skew"); + + ADD_GROUP("Border Width", "border_width_"); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_top", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "border_width_bottom", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_border_width", "get_border_width", SIDE_BOTTOM); + + ADD_GROUP("Border", "border_"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "border_color"), "set_border_color", "get_border_color"); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "border_blend"), "set_border_blend", "get_border_blend"); + + ADD_GROUP("Corner Radius", "corner_radius_"); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_top_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_TOP_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_right", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "corner_radius_bottom_left", PROPERTY_HINT_RANGE, "0,1024,1,suffix:px"), "set_corner_radius", "get_corner_radius", CORNER_BOTTOM_LEFT); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "corner_detail", PROPERTY_HINT_RANGE, "1,20,1"), "set_corner_detail", "get_corner_detail"); + + ADD_GROUP("Expand Margins", "expand_margin_"); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM); + + ADD_GROUP("Shadow", "shadow_"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_shadow_size", "get_shadow_size"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_shadow_offset", "get_shadow_offset"); + + ADD_GROUP("Anti Aliasing", "anti_aliasing_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "anti_aliasing"), "set_anti_aliased", "is_anti_aliased"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "anti_aliasing_size", PROPERTY_HINT_RANGE, "0.01,10,0.001,suffix:px"), "set_aa_size", "get_aa_size"); +} + +StyleBoxFlat::StyleBoxFlat() {} + +StyleBoxFlat::~StyleBoxFlat() {} diff --git a/scene/resources/style_box_flat.h b/scene/resources/style_box_flat.h new file mode 100644 index 0000000000..b6bb145f05 --- /dev/null +++ b/scene/resources/style_box_flat.h @@ -0,0 +1,118 @@ +/**************************************************************************/ +/* style_box_flat.h */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#ifndef STYLE_BOX_FLAT_H +#define STYLE_BOX_FLAT_H + +#include "scene/resources/style_box.h" + +class StyleBoxFlat : public StyleBox { + GDCLASS(StyleBoxFlat, StyleBox); + + Color bg_color = Color(0.6, 0.6, 0.6); + Color shadow_color = Color(0, 0, 0, 0.6); + Color border_color = Color(0.8, 0.8, 0.8); + + real_t border_width[4] = {}; + real_t expand_margin[4] = {}; + real_t corner_radius[4] = {}; + + bool draw_center = true; + bool blend_border = false; + Vector2 skew; + bool anti_aliased = true; + + int corner_detail = 8; + int shadow_size = 0; + Point2 shadow_offset; + real_t aa_size = 1; + +protected: + virtual float get_style_margin(Side p_side) const override; + static void _bind_methods(); + void _validate_property(PropertyInfo &p_property) const; + +public: + void set_bg_color(const Color &p_color); + Color get_bg_color() const; + + void set_border_color(const Color &p_color); + Color get_border_color() const; + + void set_border_width_all(int p_size); + int get_border_width_min() const; + + void set_border_width(Side p_side, int p_width); + int get_border_width(Side p_side) const; + + void set_border_blend(bool p_blend); + bool get_border_blend() const; + + void set_corner_radius_all(int radius); + void set_corner_radius_individual(const int radius_top_left, const int radius_top_right, const int radius_bottom_right, const int radius_bottom_left); + void set_corner_radius(Corner p_corner, const int radius); + int get_corner_radius(Corner p_corner) const; + + void set_corner_detail(const int &p_corner_detail); + int get_corner_detail() const; + + void set_expand_margin(Side p_expand_side, float p_size); + void set_expand_margin_all(float p_expand_margin_size); + void set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom); + float get_expand_margin(Side p_expand_side) const; + + void set_draw_center(bool p_enabled); + bool is_draw_center_enabled() const; + + void set_skew(Vector2 p_skew); + Vector2 get_skew() const; + + void set_shadow_color(const Color &p_color); + Color get_shadow_color() const; + + void set_shadow_size(const int &p_size); + int get_shadow_size() const; + + void set_shadow_offset(const Point2 &p_offset); + Point2 get_shadow_offset() const; + + void set_anti_aliased(const bool &p_anti_aliased); + bool is_anti_aliased() const; + void set_aa_size(const real_t p_aa_size); + real_t get_aa_size() const; + + virtual Rect2 get_draw_rect(const Rect2 &p_rect) const override; + virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; + + StyleBoxFlat(); + ~StyleBoxFlat(); +}; + +#endif // STYLE_BOX_FLAT_H diff --git a/scene/resources/style_box_line.cpp b/scene/resources/style_box_line.cpp new file mode 100644 index 0000000000..9aeba88531 --- /dev/null +++ b/scene/resources/style_box_line.cpp @@ -0,0 +1,132 @@ +/**************************************************************************/ +/* style_box_line.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. */ +/**************************************************************************/ + +#include "style_box_line.h" + +#include "servers/rendering_server.h" + +float StyleBoxLine::get_style_margin(Side p_side) const { + ERR_FAIL_INDEX_V((int)p_side, 4, 0); + + if (vertical) { + if (p_side == SIDE_LEFT || p_side == SIDE_RIGHT) { + return thickness / 2.0; + } + } else if (p_side == SIDE_TOP || p_side == SIDE_BOTTOM) { + return thickness / 2.0; + } + + return 0; +} + +void StyleBoxLine::set_color(const Color &p_color) { + color = p_color; + emit_changed(); +} + +Color StyleBoxLine::get_color() const { + return color; +} + +void StyleBoxLine::set_thickness(int p_thickness) { + thickness = p_thickness; + emit_changed(); +} + +int StyleBoxLine::get_thickness() const { + return thickness; +} + +void StyleBoxLine::set_vertical(bool p_vertical) { + vertical = p_vertical; + emit_changed(); +} + +bool StyleBoxLine::is_vertical() const { + return vertical; +} + +void StyleBoxLine::set_grow_end(float p_grow_end) { + grow_end = p_grow_end; + emit_changed(); +} + +float StyleBoxLine::get_grow_end() const { + return grow_end; +} + +void StyleBoxLine::set_grow_begin(float p_grow_begin) { + grow_begin = p_grow_begin; + emit_changed(); +} + +float StyleBoxLine::get_grow_begin() const { + return grow_begin; +} + +void StyleBoxLine::draw(RID p_canvas_item, const Rect2 &p_rect) const { + RenderingServer *vs = RenderingServer::get_singleton(); + Rect2i r = p_rect; + + if (vertical) { + r.position.y -= grow_begin; + r.size.y += (grow_begin + grow_end); + r.size.x = thickness; + } else { + r.position.x -= grow_begin; + r.size.x += (grow_begin + grow_end); + r.size.y = thickness; + } + + vs->canvas_item_add_rect(p_canvas_item, r, color); +} + +void StyleBoxLine::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_color", "color"), &StyleBoxLine::set_color); + ClassDB::bind_method(D_METHOD("get_color"), &StyleBoxLine::get_color); + ClassDB::bind_method(D_METHOD("set_thickness", "thickness"), &StyleBoxLine::set_thickness); + ClassDB::bind_method(D_METHOD("get_thickness"), &StyleBoxLine::get_thickness); + ClassDB::bind_method(D_METHOD("set_grow_begin", "offset"), &StyleBoxLine::set_grow_begin); + ClassDB::bind_method(D_METHOD("get_grow_begin"), &StyleBoxLine::get_grow_begin); + ClassDB::bind_method(D_METHOD("set_grow_end", "offset"), &StyleBoxLine::set_grow_end); + ClassDB::bind_method(D_METHOD("get_grow_end"), &StyleBoxLine::get_grow_end); + ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &StyleBoxLine::set_vertical); + ClassDB::bind_method(D_METHOD("is_vertical"), &StyleBoxLine::is_vertical); + + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_begin", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_begin", "get_grow_begin"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_end", PROPERTY_HINT_RANGE, "-300,300,1,suffix:px"), "set_grow_end", "get_grow_end"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "thickness", PROPERTY_HINT_RANGE, "0,100,suffix:px"), "set_thickness", "get_thickness"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical"); +} + +StyleBoxLine::StyleBoxLine() {} + +StyleBoxLine::~StyleBoxLine() {} diff --git a/scene/resources/style_box_line.h b/scene/resources/style_box_line.h new file mode 100644 index 0000000000..18f765a1e4 --- /dev/null +++ b/scene/resources/style_box_line.h @@ -0,0 +1,70 @@ +/**************************************************************************/ +/* style_box_line.h */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#ifndef STYLE_BOX_LINE_H +#define STYLE_BOX_LINE_H + +#include "scene/resources/style_box.h" + +class StyleBoxLine : public StyleBox { + GDCLASS(StyleBoxLine, StyleBox); + Color color; + int thickness = 1; + bool vertical = false; + float grow_begin = 1.0; + float grow_end = 1.0; + +protected: + virtual float get_style_margin(Side p_side) const override; + static void _bind_methods(); + +public: + void set_color(const Color &p_color); + Color get_color() const; + + void set_thickness(int p_thickness); + int get_thickness() const; + + void set_vertical(bool p_vertical); + bool is_vertical() const; + + void set_grow_begin(float p_grow); + float get_grow_begin() const; + + void set_grow_end(float p_grow); + float get_grow_end() const; + + virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; + + StyleBoxLine(); + ~StyleBoxLine(); +}; + +#endif // STYLE_BOX_LINE_H diff --git a/scene/resources/style_box_texture.cpp b/scene/resources/style_box_texture.cpp new file mode 100644 index 0000000000..156525d21c --- /dev/null +++ b/scene/resources/style_box_texture.cpp @@ -0,0 +1,243 @@ +/**************************************************************************/ +/* style_box_texture.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. */ +/**************************************************************************/ + +#include "style_box_texture.h" + +float StyleBoxTexture::get_style_margin(Side p_side) const { + ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); + + return texture_margin[p_side]; +} + +void StyleBoxTexture::set_texture(Ref<Texture2D> p_texture) { + if (texture == p_texture) { + return; + } + texture = p_texture; + emit_changed(); +} + +Ref<Texture2D> StyleBoxTexture::get_texture() const { + return texture; +} + +void StyleBoxTexture::set_texture_margin(Side p_side, float p_size) { + ERR_FAIL_INDEX((int)p_side, 4); + + texture_margin[p_side] = p_size; + emit_changed(); +} + +void StyleBoxTexture::set_texture_margin_all(float p_size) { + for (int i = 0; i < 4; i++) { + texture_margin[i] = p_size; + } + emit_changed(); +} + +void StyleBoxTexture::set_texture_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { + texture_margin[SIDE_LEFT] = p_left; + texture_margin[SIDE_TOP] = p_top; + texture_margin[SIDE_RIGHT] = p_right; + texture_margin[SIDE_BOTTOM] = p_bottom; + emit_changed(); +} + +float StyleBoxTexture::get_texture_margin(Side p_side) const { + ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); + + return texture_margin[p_side]; +} + +void StyleBoxTexture::set_expand_margin(Side p_side, float p_size) { + ERR_FAIL_INDEX((int)p_side, 4); + expand_margin[p_side] = p_size; + emit_changed(); +} + +void StyleBoxTexture::set_expand_margin_all(float p_expand_margin_size) { + for (int i = 0; i < 4; i++) { + expand_margin[i] = p_expand_margin_size; + } + emit_changed(); +} + +void StyleBoxTexture::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { + expand_margin[SIDE_LEFT] = p_left; + expand_margin[SIDE_TOP] = p_top; + expand_margin[SIDE_RIGHT] = p_right; + expand_margin[SIDE_BOTTOM] = p_bottom; + emit_changed(); +} + +float StyleBoxTexture::get_expand_margin(Side p_side) const { + ERR_FAIL_INDEX_V((int)p_side, 4, 0); + return expand_margin[p_side]; +} + +void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) { + if (region_rect == p_region_rect) { + return; + } + + region_rect = p_region_rect; + emit_changed(); +} + +Rect2 StyleBoxTexture::get_region_rect() const { + return region_rect; +} + +void StyleBoxTexture::set_draw_center(bool p_enabled) { + draw_center = p_enabled; + emit_changed(); +} + +bool StyleBoxTexture::is_draw_center_enabled() const { + return draw_center; +} + +void StyleBoxTexture::set_h_axis_stretch_mode(AxisStretchMode p_mode) { + ERR_FAIL_INDEX((int)p_mode, 3); + axis_h = p_mode; + emit_changed(); +} + +StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_h_axis_stretch_mode() const { + return axis_h; +} + +void StyleBoxTexture::set_v_axis_stretch_mode(AxisStretchMode p_mode) { + ERR_FAIL_INDEX((int)p_mode, 3); + axis_v = p_mode; + emit_changed(); +} + +StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_v_axis_stretch_mode() const { + return axis_v; +} + +void StyleBoxTexture::set_modulate(const Color &p_modulate) { + if (modulate == p_modulate) { + return; + } + modulate = p_modulate; + emit_changed(); +} + +Color StyleBoxTexture::get_modulate() const { + return modulate; +} + +Rect2 StyleBoxTexture::get_draw_rect(const Rect2 &p_rect) const { + return p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]); +} + +void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const { + if (texture.is_null()) { + return; + } + + Rect2 rect = p_rect; + Rect2 src_rect = region_rect; + + texture->get_rect_region(rect, src_rect, rect, src_rect); + + rect.position.x -= expand_margin[SIDE_LEFT]; + rect.position.y -= expand_margin[SIDE_TOP]; + rect.size.x += expand_margin[SIDE_LEFT] + expand_margin[SIDE_RIGHT]; + rect.size.y += expand_margin[SIDE_TOP] + expand_margin[SIDE_BOTTOM]; + + Vector2 start_offset = Vector2(texture_margin[SIDE_LEFT], texture_margin[SIDE_TOP]); + Vector2 end_offset = Vector2(texture_margin[SIDE_RIGHT], texture_margin[SIDE_BOTTOM]); + + RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), start_offset, end_offset, RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate); +} + +void StyleBoxTexture::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_texture", "texture"), &StyleBoxTexture::set_texture); + ClassDB::bind_method(D_METHOD("get_texture"), &StyleBoxTexture::get_texture); + + ClassDB::bind_method(D_METHOD("set_texture_margin", "margin", "size"), &StyleBoxTexture::set_texture_margin); + ClassDB::bind_method(D_METHOD("set_texture_margin_all", "size"), &StyleBoxTexture::set_texture_margin_all); + ClassDB::bind_method(D_METHOD("get_texture_margin", "margin"), &StyleBoxTexture::get_texture_margin); + + ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxTexture::set_expand_margin); + ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_all); + ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxTexture::get_expand_margin); + + ClassDB::bind_method(D_METHOD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect); + ClassDB::bind_method(D_METHOD("get_region_rect"), &StyleBoxTexture::get_region_rect); + + ClassDB::bind_method(D_METHOD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center); + ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxTexture::is_draw_center_enabled); + + ClassDB::bind_method(D_METHOD("set_modulate", "color"), &StyleBoxTexture::set_modulate); + ClassDB::bind_method(D_METHOD("get_modulate"), &StyleBoxTexture::get_modulate); + + ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &StyleBoxTexture::set_h_axis_stretch_mode); + ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &StyleBoxTexture::get_h_axis_stretch_mode); + + ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &StyleBoxTexture::set_v_axis_stretch_mode); + ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &StyleBoxTexture::get_v_axis_stretch_mode); + + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); + + ADD_GROUP("Texture Margins", "texture_margin_"); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_BOTTOM); + + ADD_GROUP("Expand Margins", "expand_margin_"); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM); + + ADD_GROUP("Axis Stretch", "axis_stretch_"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode"); + + ADD_GROUP("Sub-Region", "region_"); + ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect"); + + ADD_GROUP("Modulate", "modulate_"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate_color"), "set_modulate", "get_modulate"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled"); + + BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH); + BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE); + BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT); +} + +StyleBoxTexture::StyleBoxTexture() {} + +StyleBoxTexture::~StyleBoxTexture() {} diff --git a/scene/resources/style_box_texture.h b/scene/resources/style_box_texture.h new file mode 100644 index 0000000000..b1b833f470 --- /dev/null +++ b/scene/resources/style_box_texture.h @@ -0,0 +1,99 @@ +/**************************************************************************/ +/* style_box_texture.h */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#ifndef STYLE_BOX_TEXTURE_H +#define STYLE_BOX_TEXTURE_H + +#include "scene/resources/style_box.h" +#include "scene/resources/texture.h" + +class StyleBoxTexture : public StyleBox { + GDCLASS(StyleBoxTexture, StyleBox); + +public: + enum AxisStretchMode { + AXIS_STRETCH_MODE_STRETCH, + AXIS_STRETCH_MODE_TILE, + AXIS_STRETCH_MODE_TILE_FIT, + }; + +private: + float expand_margin[4] = {}; + float texture_margin[4] = {}; + Rect2 region_rect; + Ref<Texture2D> texture; + bool draw_center = true; + Color modulate = Color(1, 1, 1, 1); + AxisStretchMode axis_h = AXIS_STRETCH_MODE_STRETCH; + AxisStretchMode axis_v = AXIS_STRETCH_MODE_STRETCH; + +protected: + virtual float get_style_margin(Side p_side) const override; + static void _bind_methods(); + +public: + void set_texture(Ref<Texture2D> p_texture); + Ref<Texture2D> get_texture() const; + + void set_texture_margin(Side p_side, float p_size); + void set_texture_margin_all(float p_size); + void set_texture_margin_individual(float p_left, float p_top, float p_right, float p_bottom); + float get_texture_margin(Side p_side) const; + + void set_expand_margin(Side p_expand_side, float p_size); + void set_expand_margin_all(float p_expand_margin_size); + void set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom); + float get_expand_margin(Side p_expand_side) const; + + void set_region_rect(const Rect2 &p_region_rect); + Rect2 get_region_rect() const; + + void set_draw_center(bool p_enabled); + bool is_draw_center_enabled() const; + + void set_h_axis_stretch_mode(AxisStretchMode p_mode); + AxisStretchMode get_h_axis_stretch_mode() const; + + void set_v_axis_stretch_mode(AxisStretchMode p_mode); + AxisStretchMode get_v_axis_stretch_mode() const; + + void set_modulate(const Color &p_modulate); + Color get_modulate() const; + + virtual Rect2 get_draw_rect(const Rect2 &p_rect) const override; + virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; + + StyleBoxTexture(); + ~StyleBoxTexture(); +}; + +VARIANT_ENUM_CAST(StyleBoxTexture::AxisStretchMode) + +#endif // STYLE_BOX_TEXTURE_H diff --git a/tests/scene/test_theme.h b/tests/scene/test_theme.h index 59a5ad2cff..ad1ce1fd50 100644 --- a/tests/scene/test_theme.h +++ b/tests/scene/test_theme.h @@ -32,6 +32,7 @@ #define TEST_THEME_H #include "scene/resources/image_texture.h" +#include "scene/resources/style_box_flat.h" #include "scene/resources/theme.h" #include "tests/test_tools.h" |