Skip to content

Commit

Permalink
Size DoubleSlider based on display scale
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Feb 1, 2025
1 parent e08d8e1 commit 9da1f3f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
59 changes: 48 additions & 11 deletions project/addons/terrain_3d/src/double_slider.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@ var min_value: float = 0.0
var max_value: float = 100.0
var step: float = 1.0
var range := Vector2(0, 100)
var display_scale: float = 1.
var position_x: float = 0.
var minimum_x: float = 60.


func _ready() -> void:
# Setup Display Scale
# 0 auto, 1 75%, 2 100%, 3 125%, 4 150%, 5 175%, 6 200%, 7 custom
var es: EditorSettings = EditorInterface.get_editor_settings()
var ds: int = es.get_setting("interface/editor/display_scale")
if ds == 0:
ds = 2
elif ds == 7:
display_scale = es.get_setting("interface/editor/custom_display_scale")
else:
display_scale = float(ds + 2) * .25

update_label()


Expand Down Expand Up @@ -71,19 +85,35 @@ func get_value() -> Vector2:
func update_label() -> void:
if label:
label.set_text(str(range.x) + suffix + "/" + str(range.y) + suffix)

if position_x == 0:
position_x = label.position.x
else:
label.position.x = position_x + 5 * display_scale
label.custom_minimum_size.x = minimum_x + 5 * display_scale


func _get_handle() -> int:
return 1


func _gui_input(p_event: InputEvent) -> void:
if p_event is InputEventMouseButton:
if p_event.get_button_index() == MOUSE_BUTTON_LEFT:
var button: int = p_event.get_button_index()
if button in [ MOUSE_BUTTON_LEFT, MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN ]:
if p_event.is_pressed():
var mid_point = (range.x + range.y) / 2.0
var xpos: float = p_event.get_position().x * 2.0
if xpos >= mid_point:
grabbed_handle = 1
else:
grabbed_handle = -1
set_slider(p_event.get_position().x)
match button:
MOUSE_BUTTON_LEFT:
set_slider(p_event.get_position().x)
MOUSE_BUTTON_WHEEL_DOWN:
set_slider(-1., true)
MOUSE_BUTTON_WHEEL_UP:
set_slider(1., true)
else:
grabbed_handle = 0

Expand All @@ -92,14 +122,20 @@ func _gui_input(p_event: InputEvent) -> void:
set_slider(p_event.get_position().x)


func set_slider(p_xpos: float) -> void:
func set_slider(p_xpos: float, p_relative: bool = false) -> void:
if grabbed_handle == 0:
return
var xpos_step: float = clamp(snappedf((p_xpos / size.x) * max_value, step), min_value, max_value)
if(grabbed_handle < 0):
range.x = xpos_step
if p_relative:
range.x += p_xpos
else:
range.x = xpos_step
else:
range.y = xpos_step
if p_relative:
range.y += p_xpos
else:
range.y = xpos_step
set_value(range)


Expand All @@ -114,14 +150,15 @@ func _notification(p_what: int) -> void:
# Draw foreground bar
var handle: Texture2D = get_theme_icon("grabber", "HSlider")
var area: StyleBox = get_theme_stylebox("grabber_area", "HSlider")
var h: float = size.y / 2 - handle.get_size().y / 2
var startx: float = (range.x / max_value) * size.x
var endx: float = (range.y / max_value) * size.x #- startx
var endx: float = (range.y / max_value) * size.x
draw_style_box(area, Rect2(Vector2(startx, mid_y), Vector2(endx - startx, bg_height)))

# Draw handles, slightly in so they don't get on the outside edges
var handle_pos: Vector2
handle_pos.x = clamp(startx - handle.get_size().x/2, -5, size.x)
handle_pos.x = clamp(startx - handle.get_size().x/2, -10, size.x)
handle_pos.y = clamp(endx - handle.get_size().x/2, 0, size.x - 10)
draw_texture(handle, Vector2(handle_pos.x, -mid_y))
draw_texture(handle, Vector2(handle_pos.y, -mid_y))
draw_texture(handle, Vector2(handle_pos.x, -mid_y - 10 * (display_scale - 1.)))
draw_texture(handle, Vector2(handle_pos.y, -mid_y - 10 * (display_scale - 1.)))

update_label()
4 changes: 2 additions & 2 deletions project/addons/terrain_3d/src/tool_settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func add_setting(p_args: Dictionary) -> void:
pending_children.push_back(option)
control = option

SettingType.SLIDER, SettingType.DOUBLE_SLIDER:
SettingType.SLIDER, SettingType.DOUBLE_SLIDER:
var slider: Control
if p_type == SettingType.SLIDER:
# Create an editable value box
Expand Down Expand Up @@ -477,7 +477,7 @@ func add_setting(p_args: Dictionary) -> void:
else: # DOUBLE_SLIDER
var label := Label.new()
label.set_custom_minimum_size(Vector2(60, 0))
label.set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT)
label.set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER)
slider = DoubleSlider.new()
slider.label = label
slider.suffix = p_suffix
Expand Down

0 comments on commit 9da1f3f

Please sign in to comment.