Skip to content

Commit 3623c51

Browse files
committed
Add shortcut widget to change size and opacity, fix bottom panel not updating size/opacity
1 parent 055281d commit 3623c51

File tree

8 files changed

+392
-5
lines changed

8 files changed

+392
-5
lines changed

addons/zylann.hterrain/tools/brush/brush.gd

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ const HT_Painter = preload("./painter.gd")
1010

1111
const SHAPES_DIR = "addons/zylann.hterrain/tools/brush/shapes"
1212
const DEFAULT_BRUSH_TEXTURE_PATH = SHAPES_DIR + "/round2.exr"
13+
1314
# Reasonable size for sliders to be usable
15+
const MIN_SIZE_FOR_SLIDERS = 2
1416
const MAX_SIZE_FOR_SLIDERS = 500
17+
const MIN_OPACITY_FOR_SLIDERS = 0
18+
const MAX_OPACITY_FOR_SLIDERS = 100
1519
# Absolute size limit. Terrains can't be larger than that, and it will be very slow to paint
1620
const MAX_SIZE = 4000
1721

1822
signal size_changed(new_size)
23+
signal opacity_changed(new_opacity)
1924
signal shapes_changed
2025
signal shape_index_changed
2126

@@ -50,7 +55,11 @@ func get_size() -> int:
5055

5156

5257
func set_opacity(opacity: float):
53-
_opacity = clampf(opacity, 0.0, 1.0)
58+
var new_opacity := clampf(opacity, 0.0, 1.0)
59+
60+
if new_opacity != _opacity:
61+
_opacity = new_opacity
62+
opacity_changed.emit(_opacity)
5463

5564

5665
func get_opacity() -> float:
@@ -213,5 +222,3 @@ func configure_paint_input(painters: Array[HT_Painter], position: Vector2, press
213222
func on_paint_end():
214223
_prev_position = Vector2(-999, -999)
215224
_prev_time_ms = 0
216-
217-

addons/zylann.hterrain/tools/brush/brush_editor.gd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func _ready():
6868
_slope_limit_control.changed.connect(_on_slope_limit_changed)
6969

7070
_size_slider.max_value = HT_Brush.MAX_SIZE_FOR_SLIDERS
71+
_size_slider.min_value = HT_Brush.MIN_SIZE_FOR_SLIDERS
72+
_opacity_slider.max_value = HT_Brush.MAX_OPACITY_FOR_SLIDERS
73+
_opacity_slider.min_value = HT_Brush.MIN_OPACITY_FOR_SLIDERS
7174
#if NativeFactory.is_native_available():
7275
# _size_slider.max_value = 200
7376
#else:
@@ -104,6 +107,8 @@ func set_terrain_painter(terrain_painter: HT_TerrainPainter):
104107
_terrain_painter.flatten_height_changed.disconnect(_on_flatten_height_changed)
105108
_terrain_painter.get_brush().shapes_changed.disconnect(_on_brush_shapes_changed)
106109
_terrain_painter.get_brush().shape_index_changed.disconnect(_on_brush_shape_index_changed)
110+
_terrain_painter.get_brush().size_changed.disconnect(_on_brush_size_changed)
111+
_terrain_painter.get_brush().opacity_changed.disconnect(_on_brush_opacity_changed)
107112

108113
_terrain_painter = terrain_painter
109114

@@ -138,6 +143,8 @@ func set_terrain_painter(terrain_painter: HT_TerrainPainter):
138143
_terrain_painter.flatten_height_changed.connect(_on_flatten_height_changed)
139144
brush.shapes_changed.connect(_on_brush_shapes_changed)
140145
brush.shape_index_changed.connect(_on_brush_shape_index_changed)
146+
brush.size_changed.connect(_on_brush_size_changed)
147+
brush.opacity_changed.connect(_on_brush_opacity_changed)
141148

142149

143150
func _on_flatten_height_changed():
@@ -149,6 +156,14 @@ func _on_brush_shapes_changed():
149156
_update_shape_preview()
150157

151158

159+
func _on_brush_size_changed(new_size):
160+
_update_brush_size(new_size)
161+
162+
163+
func _on_brush_opacity_changed(new_opacity):
164+
_update_brush_opacity(new_opacity)
165+
166+
152167
func _on_brush_shape_index_changed():
153168
_update_shape_preview()
154169

@@ -159,6 +174,16 @@ func _update_shape_preview():
159174
_shape_texture_rect.texture = brush.get_shape(i)
160175

161176

177+
func _update_brush_size(new_size):
178+
if _terrain_painter != null:
179+
_size_slider.set_value_no_signal(new_size)
180+
181+
182+
func _update_brush_opacity(new_opacity):
183+
if _terrain_painter != null:
184+
_opacity_slider.set_value_no_signal(new_opacity * _opacity_slider.max_value)
185+
186+
162187
func set_display_mode(mode: int):
163188
var show_flatten := mode == HT_TerrainPainter.MODE_FLATTEN
164189
var show_color := mode == HT_TerrainPainter.MODE_COLOR
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
@tool
2+
extends Control
3+
4+
const MIN_UI_CIRCLE_SIZE = 40
5+
const MAX_UI_CIRCLE_SIZE = 500
6+
7+
@onready var _brush_size_background: TextureRect = %BrushSizeBackground
8+
@onready var _brush_size_preview: TextureRect = %BrushSizePreview
9+
@onready var _value_label: Label = %ValueLabel
10+
@onready var _overlay_name_label: Label = %OverlayNameLabel
11+
@onready var _exponential_slider: HSlider = %ExponentialSlider
12+
13+
@export var brush_size_factor: float = 2.5
14+
@export var min_value: float = -1
15+
@export var max_value: float = -1
16+
var _brush_preview_color: Color = Color.LIGHT_GREEN
17+
var _dpi_scale: float = 1.0
18+
var _value: float = 0.0
19+
20+
signal on_value_selected(new_value: int)
21+
signal on_cancel
22+
23+
var background_margin: int = 10
24+
25+
26+
func _physics_process(delta: float) -> void:
27+
_update_size(_get_mouse_distance())
28+
29+
30+
func _input(event: InputEvent) -> void:
31+
if event is InputEventMouseButton:
32+
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
33+
on_value_selected.emit(_value)
34+
else:
35+
on_cancel.emit()
36+
if event is InputEventKey:
37+
if event.keycode == KEY_ESCAPE and event.pressed:
38+
on_cancel.emit()
39+
40+
41+
func set_brush_preview_color(brush_color: Color) -> void:
42+
_brush_preview_color = brush_color
43+
_update_brush_preview_color()
44+
45+
46+
func _update_brush_preview_color() -> void:
47+
_brush_size_preview.modulate = _brush_preview_color
48+
49+
50+
func set_overlay_name(overlay_label_name: String) -> void:
51+
_overlay_name_label.text = overlay_label_name
52+
53+
54+
func _update_size(value: float) -> void:
55+
var dist := clampi(value * brush_size_factor, MIN_UI_CIRCLE_SIZE*_dpi_scale, MAX_UI_CIRCLE_SIZE*_dpi_scale )
56+
var ui_size := clampi(dist, MIN_UI_CIRCLE_SIZE*_dpi_scale, MAX_UI_CIRCLE_SIZE*_dpi_scale)
57+
_brush_size_background.size = Vector2(ui_size + background_margin, ui_size + background_margin)
58+
_brush_size_background.position = Vector2(-( (ui_size/2) + (background_margin/2)) , -( (ui_size/2) + (background_margin/2)))
59+
_brush_size_preview.size = Vector2(ui_size, ui_size)
60+
_brush_size_preview.position = Vector2(-(ui_size/2) , -(ui_size/2))
61+
62+
_exponential_slider.min_value = MIN_UI_CIRCLE_SIZE*_dpi_scale
63+
_exponential_slider.max_value = MAX_UI_CIRCLE_SIZE*_dpi_scale
64+
_exponential_slider.value = (_exponential_slider.min_value+_exponential_slider.max_value)-ui_size
65+
66+
var re_value: float = absf(1.0-_exponential_slider.get_as_ratio()) * (max_value-min_value)
67+
re_value += min_value
68+
69+
_value = roundi(re_value)
70+
_value_label.text = str(_value)
71+
72+
73+
func apply_dpi_scale(dpi_scale: float) -> void:
74+
_dpi_scale = dpi_scale
75+
76+
77+
func setup_start_position(start_pos: Vector2, initial_value: float) -> void:
78+
position = start_pos
79+
80+
_exponential_slider.min_value = MIN_UI_CIRCLE_SIZE*_dpi_scale
81+
_exponential_slider.max_value = MAX_UI_CIRCLE_SIZE*_dpi_scale
82+
83+
var reverse: float = (initial_value - min_value) / (max_value-min_value)
84+
reverse = absf(1-reverse)
85+
_exponential_slider.set_as_ratio(reverse)
86+
87+
var ui_size: float = (_exponential_slider.min_value+_exponential_slider.max_value) - _exponential_slider.value
88+
89+
position.x -= (ui_size/brush_size_factor)
90+
91+
92+
func _get_mouse_distance() -> float:
93+
var global_mouse_pos: Vector2 = get_global_mouse_position()
94+
95+
var distance: float = position.distance_to(global_mouse_pos)
96+
if position.x > global_mouse_pos.x:
97+
distance = 0
98+
99+
return distance;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[gd_scene load_steps=5 format=3 uid="uid://d324kwgdf7e6l"]
2+
3+
[ext_resource type="Script" path="res://addons/zylann.hterrain/tools/brush/brush_editor_overlay.gd" id="1_grgos"]
4+
[ext_resource type="Texture2D" uid="uid://fk2rcx7lsbam" path="res://addons/zylann.hterrain/tools/icons/brush_circle_background.svg" id="2_jeqe2"]
5+
[ext_resource type="Texture2D" uid="uid://c4ma6f4217y48" path="res://addons/zylann.hterrain/tools/icons/brush_circle.svg" id="3_sw64o"]
6+
7+
[sub_resource type="LabelSettings" id="LabelSettings_0d2ij"]
8+
font_size = 32
9+
10+
[node name="BrushEditorOverlay" type="Control"]
11+
layout_mode = 3
12+
anchors_preset = 15
13+
anchor_right = 1.0
14+
anchor_bottom = 1.0
15+
grow_horizontal = 2
16+
grow_vertical = 2
17+
script = ExtResource("1_grgos")
18+
min_value = 2.0
19+
max_value = 500.0
20+
21+
[node name="BrushSizeBackground" type="TextureRect" parent="."]
22+
unique_name_in_owner = true
23+
modulate = Color(0.478431, 0.478431, 0.478431, 0.423529)
24+
layout_mode = 0
25+
offset_left = -209.0
26+
offset_top = -209.0
27+
offset_right = 210.0
28+
offset_bottom = 210.0
29+
texture = ExtResource("2_jeqe2")
30+
expand_mode = 1
31+
metadata/_edit_use_anchors_ = true
32+
33+
[node name="BrushSizePreview" type="TextureRect" parent="."]
34+
unique_name_in_owner = true
35+
modulate = Color(1, 0, 0, 1)
36+
layout_mode = 1
37+
offset_left = -204.0
38+
offset_top = -204.0
39+
offset_right = 205.0
40+
offset_bottom = 205.0
41+
texture = ExtResource("3_sw64o")
42+
expand_mode = 1
43+
metadata/_edit_use_anchors_ = true
44+
45+
[node name="ValueLabel" type="Label" parent="."]
46+
unique_name_in_owner = true
47+
layout_mode = 1
48+
anchors_preset = -1
49+
anchor_top = 0.001
50+
anchor_bottom = 0.001
51+
offset_left = -35.0
52+
offset_top = -22.648
53+
offset_right = 38.0
54+
offset_bottom = 22.352
55+
grow_horizontal = 2
56+
grow_vertical = 2
57+
scale = Vector2(0.7, 0.7)
58+
pivot_offset = Vector2(33, 22)
59+
text = "266"
60+
label_settings = SubResource("LabelSettings_0d2ij")
61+
horizontal_alignment = 1
62+
vertical_alignment = 1
63+
64+
[node name="OverlayNameLabel" type="Label" parent="."]
65+
unique_name_in_owner = true
66+
layout_mode = 1
67+
anchors_preset = -1
68+
offset_left = -211.0
69+
offset_top = -62.0
70+
offset_right = 400.0
71+
offset_bottom = 19.0
72+
scale = Vector2(0.7, 0.7)
73+
text = "Brush size"
74+
label_settings = SubResource("LabelSettings_0d2ij")
75+
horizontal_alignment = 1
76+
vertical_alignment = 1
77+
78+
[node name="ExponentialSlider" type="HSlider" parent="."]
79+
unique_name_in_owner = true
80+
visible = false
81+
layout_mode = 0
82+
offset_left = -132.0
83+
offset_top = 50.0
84+
offset_right = 140.0
85+
offset_bottom = 66.0
86+
focus_mode = 0
87+
mouse_filter = 2
88+
min_value = 40.0
89+
max_value = 500.0
90+
value = 131.0
91+
exp_edit = true

addons/zylann.hterrain/tools/brush/settings_dialog/brush_settings_dialog.gd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func _ready():
3939
return
4040

4141
_size_slider.set_max_value(HT_Brush.MAX_SIZE_FOR_SLIDERS)
42+
_size_slider.set_min_value(HT_Brush.MIN_SIZE_FOR_SLIDERS)
43+
_opacity_slider.set_max_value(HT_Brush.MAX_OPACITY_FOR_SLIDERS)
44+
_opacity_slider.set_min_value(HT_Brush.MIN_OPACITY_FOR_SLIDERS)
4245
_size_slider.set_greater_max_value(HT_Brush.MAX_SIZE)
4346

4447
# TESTING
@@ -277,4 +280,3 @@ func _update_shape_list_buttons():
277280

278281
func _on_shape_list_empty_clicked(at_position, mouse_button_index):
279282
_update_shape_list_buttons()
280-
Lines changed: 42 additions & 0 deletions
Loading
Lines changed: 42 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)