From 14cb9b5b263d276ce7219cc9edb93b5a7da97676 Mon Sep 17 00:00:00 2001 From: kobewi Date: Wed, 23 Nov 2022 14:56:10 +0100 Subject: [PATCH] Add center_grabber property to Slider --- doc/classes/ColorPicker.xml | 3 ++ doc/classes/HSlider.xml | 3 ++ doc/classes/VSlider.xml | 3 ++ editor/editor_themes.cpp | 3 ++ scene/gui/color_picker.cpp | 4 +++ scene/gui/color_picker.h | 2 ++ scene/gui/slider.cpp | 31 +++++++++++-------- scene/gui/slider.h | 3 ++ .../resources/default_theme/default_theme.cpp | 3 ++ 9 files changed, 42 insertions(+), 13 deletions(-) diff --git a/doc/classes/ColorPicker.xml b/doc/classes/ColorPicker.xml index 14656c649e4..61a0606f794 100644 --- a/doc/classes/ColorPicker.xml +++ b/doc/classes/ColorPicker.xml @@ -141,6 +141,9 @@ + + Overrides the [theme_item HSlider.center_grabber] theme property of the sliders. + The width of the hue selection slider. diff --git a/doc/classes/HSlider.xml b/doc/classes/HSlider.xml index 5e11c1c3ab1..2ab66a2e759 100644 --- a/doc/classes/HSlider.xml +++ b/doc/classes/HSlider.xml @@ -10,6 +10,9 @@ + + Boolean constant. If [code]1[/code], the grabber texture size will be ignored and it will fit within slider's bounds based only on its center position. + Vertical offset of the grabber. diff --git a/doc/classes/VSlider.xml b/doc/classes/VSlider.xml index 4a203d24437..b1906ccef26 100644 --- a/doc/classes/VSlider.xml +++ b/doc/classes/VSlider.xml @@ -14,6 +14,9 @@ + + Boolean constant. If [code]1[/code], the grabber texture size will be ignored and it will fit within slider's bounds based only on its center position. + Horizontal offset of the grabber. diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index aa639823cc7..bc85063b38d 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -1594,6 +1594,7 @@ Ref create_editor_theme(const Ref p_theme) { theme->set_stylebox("slider", "HSlider", make_flat_stylebox(dark_color_3, 0, default_margin_size / 2, 0, default_margin_size / 2, corner_width)); theme->set_stylebox("grabber_area", "HSlider", make_flat_stylebox(contrast_color_1, 0, default_margin_size / 2, 0, default_margin_size / 2, corner_width)); theme->set_stylebox("grabber_area_highlight", "HSlider", make_flat_stylebox(contrast_color_1, 0, default_margin_size / 2, 0, default_margin_size / 2)); + theme->set_constant("center_grabber", "HSlider", 0); theme->set_constant("grabber_offset", "HSlider", 0); // VSlider @@ -1602,6 +1603,7 @@ Ref create_editor_theme(const Ref p_theme) { theme->set_stylebox("slider", "VSlider", make_flat_stylebox(dark_color_3, default_margin_size / 2, 0, default_margin_size / 2, 0, corner_width)); theme->set_stylebox("grabber_area", "VSlider", make_flat_stylebox(contrast_color_1, default_margin_size / 2, 0, default_margin_size / 2, 0, corner_width)); theme->set_stylebox("grabber_area_highlight", "VSlider", make_flat_stylebox(contrast_color_1, default_margin_size / 2, 0, default_margin_size / 2, 0)); + theme->set_constant("center_grabber", "VSlider", 0); theme->set_constant("grabber_offset", "VSlider", 0); // RichTextLabel @@ -1858,6 +1860,7 @@ Ref create_editor_theme(const Ref p_theme) { theme->set_constant("sv_height", "ColorPicker", 256 * EDSCALE); theme->set_constant("h_width", "ColorPicker", 30 * EDSCALE); theme->set_constant("label_width", "ColorPicker", 10 * EDSCALE); + theme->set_constant("center_slider_grabbers", "ColorPicker", 1); theme->set_icon("screen_picker", "ColorPicker", theme->get_icon(SNAME("ColorPick"), SNAME("EditorIcons"))); theme->set_icon("shape_circle", "ColorPicker", theme->get_icon(SNAME("PickerShapeCircle"), SNAME("EditorIcons"))); theme->set_icon("shape_rect", "ColorPicker", theme->get_icon(SNAME("PickerShapeRectangle"), SNAME("EditorIcons"))); diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index cad09a1d9f1..b7dc1c4fbee 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -67,9 +67,11 @@ void ColorPicker::_notification(int p_what) { for (int i = 0; i < SLIDER_COUNT; i++) { labels[i]->set_custom_minimum_size(Size2(theme_cache.label_width, 0)); + sliders[i]->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers); set_offset((Side)i, get_offset((Side)i) + theme_cache.content_margin); } alpha_label->set_custom_minimum_size(Size2(theme_cache.label_width, 0)); + alpha_label->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers); set_offset((Side)0, get_offset((Side)0) + theme_cache.content_margin); for (int i = 0; i < MODE_BUTTON_COUNT; i++) { @@ -122,6 +124,8 @@ void ColorPicker::_update_theme_item_cache() { theme_cache.sv_height = get_theme_constant(SNAME("sv_height")); theme_cache.h_width = get_theme_constant(SNAME("h_width")); + theme_cache.center_slider_grabbers = get_theme_constant(SNAME("center_slider_grabbers")); + theme_cache.screen_picker = get_theme_icon(SNAME("screen_picker")); theme_cache.expanded_arrow = get_theme_icon(SNAME("expanded_arrow")); theme_cache.folded_arrow = get_theme_icon(SNAME("folded_arrow")); diff --git a/scene/gui/color_picker.h b/scene/gui/color_picker.h index 6e894efb00e..018ae109552 100644 --- a/scene/gui/color_picker.h +++ b/scene/gui/color_picker.h @@ -213,6 +213,8 @@ private: int sv_width = 0; int h_width = 0; + bool center_slider_grabbers = true; + Ref screen_picker; Ref expanded_arrow; Ref folded_arrow; diff --git a/scene/gui/slider.cpp b/scene/gui/slider.cpp index 292a4cfea2c..b9efa449cbd 100644 --- a/scene/gui/slider.cpp +++ b/scene/gui/slider.cpp @@ -64,8 +64,8 @@ void Slider::gui_input(const Ref &p_event) { grab.pos = orientation == VERTICAL ? mb->get_position().y : mb->get_position().x; - double grab_width = (double)grabber->get_size().width; - double grab_height = (double)grabber->get_size().height; + double grab_width = (double)grabber->get_width(); + double grab_height = (double)grabber->get_height(); double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width; if (orientation == VERTICAL) { set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max)); @@ -103,7 +103,7 @@ void Slider::gui_input(const Ref &p_event) { if (orientation == VERTICAL) { motion = -motion; } - double areasize = orientation == VERTICAL ? size.height - grabber->get_size().height : size.width - grabber->get_size().width; + double areasize = orientation == VERTICAL ? size.height - grabber->get_height() : size.width - grabber->get_width(); if (areasize <= 0) { return; } @@ -159,6 +159,9 @@ void Slider::_update_theme_item_cache() { theme_cache.grabber_hl_icon = get_theme_icon(SNAME("grabber_highlight")); theme_cache.grabber_disabled_icon = get_theme_icon(SNAME("grabber_disabled")); theme_cache.tick_icon = get_theme_icon(SNAME("tick")); + + theme_cache.center_grabber = get_theme_constant(SNAME("center_grabber")); + theme_cache.grabber_offset = get_theme_constant(SNAME("grabber_offset")); } void Slider::_notification(int p_what) { @@ -213,39 +216,41 @@ void Slider::_notification(int p_what) { if (orientation == VERTICAL) { int widget_width = style->get_minimum_size().width; - double areasize = size.height - grabber->get_size().height; + double areasize = size.height - (theme_cache.center_grabber ? 0 : grabber->get_height()); + int grabber_shift = theme_cache.center_grabber ? grabber->get_height() / 2 : 0; style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height))); - grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, size.height - areasize * ratio - grabber->get_size().height / 2), Size2i(widget_width, areasize * ratio + grabber->get_size().height / 2))); + grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, size.height - areasize * ratio - grabber->get_height() / 2 + grabber_shift), Size2i(widget_width, areasize * ratio + grabber->get_height() / 2 - grabber_shift))); if (ticks > 1) { - int grabber_offset = (grabber->get_size().height / 2 - tick->get_height() / 2); + int grabber_offset = (grabber->get_height() / 2 - tick->get_height() / 2); for (int i = 0; i < ticks; i++) { if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) { continue; } - int ofs = (i * areasize / (ticks - 1)) + grabber_offset; + int ofs = (i * areasize / (ticks - 1)) + grabber_offset - grabber_shift; tick->draw(ci, Point2i((size.width - widget_width) / 2, ofs)); } } - grabber->draw(ci, Point2i(size.width / 2 - grabber->get_size().width / 2 + get_theme_constant(SNAME("grabber_offset")), size.height - ratio * areasize - grabber->get_size().height)); + grabber->draw(ci, Point2i(size.width / 2 - grabber->get_width() / 2 + theme_cache.grabber_offset, size.height - ratio * areasize - grabber->get_height() + grabber_shift)); } else { int widget_height = style->get_minimum_size().height; - double areasize = size.width - grabber->get_size().width; + double areasize = size.width - (theme_cache.center_grabber ? 0 : grabber->get_size().width); + int grabber_shift = theme_cache.center_grabber ? -grabber->get_width() / 2 : 0; style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height))); - grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(areasize * ratio + grabber->get_size().width / 2, widget_height))); + grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(areasize * ratio + grabber->get_width() / 2 + grabber_shift, widget_height))); if (ticks > 1) { - int grabber_offset = (grabber->get_size().width / 2 - tick->get_width() / 2); + int grabber_offset = (grabber->get_width() / 2 - tick->get_width() / 2); for (int i = 0; i < ticks; i++) { if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) { continue; } - int ofs = (i * areasize / (ticks - 1)) + grabber_offset; + int ofs = (i * areasize / (ticks - 1)) + grabber_offset + grabber_shift; tick->draw(ci, Point2i(ofs, (size.height - widget_height) / 2)); } } - grabber->draw(ci, Point2i(ratio * areasize, size.height / 2 - grabber->get_size().height / 2 + get_theme_constant(SNAME("grabber_offset")))); + grabber->draw(ci, Point2i(ratio * areasize + grabber_shift, size.height / 2 - grabber->get_height() / 2 + theme_cache.grabber_offset)); } } break; } diff --git a/scene/gui/slider.h b/scene/gui/slider.h index 42778684af1..684445f2b34 100644 --- a/scene/gui/slider.h +++ b/scene/gui/slider.h @@ -58,6 +58,9 @@ class Slider : public Range { Ref grabber_hl_icon; Ref grabber_disabled_icon; Ref tick_icon; + + bool center_grabber = false; + int grabber_offset = 0; } theme_cache; protected: diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 0ee51a46e83..2032a5ee969 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -565,6 +565,7 @@ void fill_default_theme(Ref &theme, const Ref &default_font, const theme->set_icon("grabber_disabled", "HSlider", icons["slider_grabber_disabled"]); theme->set_icon("tick", "HSlider", icons["hslider_tick"]); + theme->set_constant("center_grabber", "HSlider", 0); theme->set_constant("grabber_offset", "HSlider", 0); // VSlider @@ -578,6 +579,7 @@ void fill_default_theme(Ref &theme, const Ref &default_font, const theme->set_icon("grabber_disabled", "VSlider", icons["slider_grabber_disabled"]); theme->set_icon("tick", "VSlider", icons["vslider_tick"]); + theme->set_constant("center_grabber", "VSlider", 0); theme->set_constant("grabber_offset", "VSlider", 0); // SpinBox @@ -903,6 +905,7 @@ void fill_default_theme(Ref &theme, const Ref &default_font, const theme->set_constant("sv_height", "ColorPicker", 256 * scale); theme->set_constant("h_width", "ColorPicker", 30 * scale); theme->set_constant("label_width", "ColorPicker", 10 * scale); + theme->set_constant("center_slider_grabbers", "ColorPicker", 1); theme->set_icon("folded_arrow", "ColorPicker", icons["arrow_right"]); theme->set_icon("expanded_arrow", "ColorPicker", icons["arrow_down"]);