Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RTL visuals for sliders #99708

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions scene/gui/slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ void Slider::gui_input(const Ref<InputEvent> &p_event) {
if (orientation == VERTICAL) {
set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
} else {
set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max);
double v = ((double)grab.pos - (grab_width / 2.0)) / max;
set_as_ratio(is_layout_rtl() ? 1 - v : v);
}
set_block_signals(false);
grab.active = true;
Expand Down Expand Up @@ -113,6 +114,8 @@ void Slider::gui_input(const Ref<InputEvent> &p_event) {
double motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos;
if (orientation == VERTICAL) {
motion = -motion;
} else if (is_layout_rtl()) {
motion = -motion;
}
double areasize = orientation == VERTICAL ? size.height - grab_height : size.width - grab_width;
if (areasize <= 0) {
Expand All @@ -139,7 +142,11 @@ void Slider::gui_input(const Ref<InputEvent> &p_event) {
}
set_process_internal(true);
}
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
if (is_layout_rtl()) {
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
} else {
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
}
accept_event();
} else if (p_event->is_action_pressed("ui_right", true)) {
if (orientation != HORIZONTAL) {
Expand All @@ -151,7 +158,11 @@ void Slider::gui_input(const Ref<InputEvent> &p_event) {
}
set_process_internal(true);
}
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
if (is_layout_rtl()) {
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
} else {
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
}
accept_event();
} else if (p_event->is_action_pressed("ui_up", true)) {
if (orientation != VERTICAL) {
Expand Down Expand Up @@ -203,11 +214,19 @@ void Slider::_notification(int p_what) {
gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
if (orientation == HORIZONTAL) {
if (input->is_action_pressed("ui_left")) {
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
if (is_layout_rtl()) {
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
} else {
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
}
}

if (input->is_action_pressed("ui_right")) {
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
if (is_layout_rtl()) {
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
} else {
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
}
}
} else if (orientation == VERTICAL) {
if (input->is_action_pressed("ui_down")) {
Expand Down Expand Up @@ -292,9 +311,15 @@ void Slider::_notification(int p_what) {
int widget_height = style->get_minimum_size().height;
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;
bool rtl = is_layout_rtl();

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_width() / 2 + grabber_shift, widget_height)));
int p = areasize * (rtl ? 1 - ratio : ratio) + grabber->get_width() / 2 + grabber_shift;
if (rtl) {
grabber_area->draw(ci, Rect2i(Point2i(p, (size.height - widget_height) / 2), Size2i(size.width - p, widget_height)));
} else {
grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(p, widget_height)));
}

if (ticks > 1) {
int grabber_offset = (grabber->get_width() / 2 - tick->get_width() / 2);
Expand All @@ -306,7 +331,7 @@ void Slider::_notification(int p_what) {
tick->draw(ci, Point2i(ofs, (size.height - widget_height) / 2));
}
}
grabber->draw(ci, Point2i(ratio * areasize + grabber_shift, size.height / 2 - grabber->get_height() / 2 + theme_cache.grabber_offset));
grabber->draw(ci, Point2i((rtl ? 1 - ratio : ratio) * areasize + grabber_shift, size.height / 2 - grabber->get_height() / 2 + theme_cache.grabber_offset));
}
} break;
}
Expand Down