Skip to content

Commit 93147ee

Browse files
committed
Add grabber_max_size theme item to Slider
1 parent 8a33751 commit 93147ee

3 files changed

Lines changed: 59 additions & 18 deletions

File tree

doc/classes/Slider.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@
6161
<theme_item name="grabber_offset" data_type="constant" type="int" default="0">
6262
Vertical or horizontal offset of the grabber.
6363
</theme_item>
64+
<theme_item name="grabber_max_size" data_type="constant" type="int" default="0">
65+
The maximum allowed size of the [Slider]'s grabber icon. The grabber texture is scaled down so its widest axis is [theme_item grabber_max_size] and the other axis is scaled keeping the original ratio.
66+
</theme_item>
6467
<theme_item name="tick_offset" data_type="constant" type="int" default="0">
6568
Vertical or horizontal offset of the ticks. The offset is reversed for top or left ticks.
6669
</theme_item>
70+
<theme_item name="tick_max_size" data_type="constant" type="int" default="0">
71+
The maximum allowed size of the [Slider]'s tick icons. The tick texture is scaled down so its widest axis is [theme_item tick_max_size] and the other axis is scaled keeping the original ratio.
72+
</theme_item>
6773
<theme_item name="grabber" data_type="icon" type="Texture2D">
6874
The texture for the grabber (the draggable element).
6975
</theme_item>

scene/gui/slider.cpp

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -298,73 +298,87 @@ void Slider::_notification(int p_what) {
298298
grabber_area = theme_cache.grabber_area_style;
299299
}
300300

301+
Size2 grabber_size = grabber->get_size();
302+
grabber_size = _fit_icon_size(grabber_size, theme_cache.grabber_max_size);
303+
grabber_size = grabber_size.round();
304+
305+
Size2 tick_size = tick->get_size();
306+
tick_size = _fit_icon_size(tick_size, theme_cache.tick_max_size);
307+
tick_size = tick_size.round();
308+
301309
if (orientation == VERTICAL) {
302310
int widget_width = style->get_minimum_size().width;
303-
double areasize = size.height - (theme_cache.center_grabber ? 0 : grabber->get_height());
304-
int grabber_shift = theme_cache.center_grabber ? grabber->get_height() / 2 : 0;
311+
double areasize = size.height - (theme_cache.center_grabber ? 0 : grabber_size.height);
312+
int grabber_shift = theme_cache.center_grabber ? grabber_size.height / 2 : 0;
305313
style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height)));
306-
grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, Math::round(size.height - areasize * ratio - grabber->get_height() / 2 + grabber_shift)), Size2i(widget_width, Math::round(areasize * ratio + grabber->get_height() / 2 - grabber_shift))));
314+
grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, Math::round(size.height - areasize * ratio - grabber_size.height / 2 + grabber_shift)), Size2i(widget_width, Math::round(areasize * ratio + grabber_size.height / 2 - grabber_shift))));
307315

308316
if (ticks > 1) {
309-
int grabber_offset = (grabber->get_height() / 2 - tick->get_height() / 2);
317+
int grabber_offset = (grabber_size.height / 2 - tick_size.height / 2);
310318
for (int i = 0; i < ticks; i++) {
311319
if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) {
312320
continue;
313321
}
314322
int ofs = (i * areasize / (ticks - 1)) + grabber_offset - grabber_shift;
315323

316324
if (ticks_position == TICK_POSITION_BOTTOM_RIGHT || ticks_position == TICK_POSITION_BOTH) {
317-
tick->draw(ci, Point2i(widget_width + (size.width - widget_width) / 2 + theme_cache.tick_offset, ofs));
325+
Point2i pos = Point2i(widget_width + (size.width - widget_width) / 2 + theme_cache.tick_offset, ofs);
326+
tick->draw_rect(ci, Rect2i(pos, tick_size));
318327
}
319328

320329
if (ticks_position == TICK_POSITION_TOP_LEFT || ticks_position == TICK_POSITION_BOTH) {
321-
Point2i pos = Point2i((size.width - widget_width) / 2 - tick->get_width() - theme_cache.tick_offset, ofs);
322-
tick->draw_rect(ci, Rect2i(pos, Size2i(-tick->get_width(), tick->get_height())));
330+
Point2i pos = Point2i((size.width - widget_width) / 2 - tick_size.width - theme_cache.tick_offset, ofs);
331+
tick->draw_rect(ci, Rect2i(pos, Size2i(-tick_size.width, tick_size.height)));
323332
}
324333

325334
if (ticks_position == TICK_POSITION_CENTER) {
326-
tick->draw(ci, Point2i((size.width - tick->get_width()) / 2 + theme_cache.tick_offset, ofs));
335+
Point2i pos = Point2i((size.width - tick_size.width) / 2 + theme_cache.tick_offset, ofs);
336+
tick->draw_rect(ci, Rect2i(pos, tick_size));
327337
}
328338
}
329339
}
330-
grabber->draw(ci, Point2i(size.width / 2 - grabber->get_width() / 2 + theme_cache.grabber_offset, size.height - ratio * areasize - grabber->get_height() + grabber_shift));
340+
Point2i pos = Point2i(size.width / 2 - grabber_size.width / 2 + theme_cache.grabber_offset, size.height - ratio * areasize - grabber_size.height + grabber_shift);
341+
grabber->draw_rect(ci, Rect2(pos, grabber_size));
331342
} else {
332343
int widget_height = style->get_minimum_size().height;
333-
double areasize = size.width - (theme_cache.center_grabber ? 0 : grabber->get_size().width);
334-
int grabber_shift = theme_cache.center_grabber ? -grabber->get_width() / 2 : 0;
344+
double areasize = size.width - (theme_cache.center_grabber ? 0 : grabber_size.width);
345+
int grabber_shift = theme_cache.center_grabber ? -grabber_size.width / 2 : 0;
335346
bool rtl = is_layout_rtl();
336347

337348
style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height)));
338-
int p = areasize * (rtl ? 1 - ratio : ratio) + grabber->get_width() / 2 + grabber_shift;
349+
int p = areasize * (rtl ? 1 - ratio : ratio) + grabber_size.width / 2 + grabber_shift;
339350
if (rtl) {
340351
grabber_area->draw(ci, Rect2i(Point2i(p, (size.height - widget_height) / 2), Size2i(size.width - p, widget_height)));
341352
} else {
342353
grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(p, widget_height)));
343354
}
344355

345356
if (ticks > 1) {
346-
int grabber_offset = (grabber->get_width() / 2 - tick->get_width() / 2);
357+
int grabber_offset = (grabber_size.width / 2 - tick_size.width / 2);
347358
for (int i = 0; i < ticks; i++) {
348359
if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) {
349360
continue;
350361
}
351362
int ofs = (i * areasize / (ticks - 1)) + grabber_offset + grabber_shift;
352363

353364
if (ticks_position == TICK_POSITION_BOTTOM_RIGHT || ticks_position == TICK_POSITION_BOTH) {
354-
tick->draw(ci, Point2i(ofs, widget_height + (size.height - widget_height) / 2 + theme_cache.tick_offset));
365+
Point2i pos = Point2i(ofs, widget_height + (size.height - widget_height) / 2 + theme_cache.tick_offset);
366+
tick->draw_rect(ci, Rect2i(pos, tick_size));
355367
}
356368

357369
if (ticks_position == TICK_POSITION_TOP_LEFT || ticks_position == TICK_POSITION_BOTH) {
358-
Point2i pos = Point2i(ofs, (size.height - widget_height) / 2 - tick->get_height() - theme_cache.tick_offset);
359-
tick->draw_rect(ci, Rect2i(pos, Size2i(tick->get_width(), -tick->get_height())));
370+
Point2i pos = Point2i(ofs, (size.height - widget_height) / 2 - tick_size.height - theme_cache.tick_offset);
371+
tick->draw_rect(ci, Rect2i(pos, Size2i(tick_size.width, -tick_size.height)));
360372
}
361373

362374
if (ticks_position == TICK_POSITION_CENTER) {
363-
tick->draw(ci, Point2i(ofs, (size.height - tick->get_height()) / 2 + theme_cache.tick_offset));
375+
Point2i pos = Point2i(ofs, (size.height - tick_size.height) / 2 + theme_cache.tick_offset);
376+
tick->draw_rect(ci, Rect2i(Point2i(ofs, (size.height - tick_size.height) / 2 + theme_cache.tick_offset), tick_size));
364377
}
365378
}
366379
}
367-
grabber->draw(ci, Point2i((rtl ? 1 - ratio : ratio) * areasize + grabber_shift, size.height / 2 - grabber->get_height() / 2 + theme_cache.grabber_offset));
380+
Point2 pos = Point2i((rtl ? 1 - ratio : ratio) * areasize + grabber_shift, size.height / 2 - grabber_size.height / 2 + theme_cache.grabber_offset);
381+
grabber->draw_rect(ci, Rect2(pos, grabber_size));
368382
}
369383
} break;
370384
}
@@ -379,6 +393,22 @@ void Slider::_validate_property(PropertyInfo &p_property) const {
379393
}
380394
}
381395

396+
Size2 Slider::_fit_icon_size(const Size2 &p_size, int p_max_size) const {
397+
Size2 icon_size = p_size;
398+
399+
if (p_max_size > 0 && (icon_size.width > p_max_size || icon_size.height > p_max_size)) {
400+
if (icon_size.width > icon_size.height) {
401+
icon_size.height = icon_size.height * p_max_size / icon_size.width;
402+
icon_size.width = p_max_size;
403+
} else {
404+
icon_size.width = icon_size.width * p_max_size / icon_size.height;
405+
icon_size.height = p_max_size;
406+
}
407+
}
408+
409+
return icon_size;
410+
}
411+
382412
void Slider::set_custom_step(double p_custom_step) {
383413
custom_step = p_custom_step;
384414
}
@@ -488,7 +518,9 @@ void Slider::_bind_methods() {
488518

489519
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, center_grabber);
490520
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, grabber_offset);
521+
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, grabber_max_size);
491522
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, tick_offset);
523+
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, tick_max_size);
492524
}
493525

494526
Slider::Slider(Orientation p_orientation) {

scene/gui/slider.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ class Slider : public Range {
7676

7777
bool center_grabber = false;
7878
int grabber_offset = 0;
79+
int grabber_max_size = 0;
7980
int tick_offset = 0;
81+
int tick_max_size = 0;
8082
} theme_cache;
8183

8284
protected:
@@ -85,6 +87,7 @@ class Slider : public Range {
8587
virtual void gui_input(const Ref<InputEvent> &p_event) override;
8688
void _notification(int p_what);
8789
void _validate_property(PropertyInfo &p_property) const;
90+
Size2 _fit_icon_size(const Size2 &p_size, int p_max_size) const;
8891
static void _bind_methods();
8992

9093
public:

0 commit comments

Comments
 (0)