Skip to content

Commit

Permalink
Add shortcut widget to change size and opacity, fix bottom panel not …
Browse files Browse the repository at this point in the history
…updating size/opacity
  • Loading branch information
GustJc committed Nov 25, 2024
1 parent 055281d commit 1a5483f
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 3 deletions.
7 changes: 4 additions & 3 deletions addons/zylann.hterrain/tools/brush/brush.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const MAX_SIZE_FOR_SLIDERS = 500
const MAX_SIZE = 4000

signal size_changed(new_size)
signal opacity_changed(new_opacity)
signal shapes_changed
signal shape_index_changed

Expand Down Expand Up @@ -50,7 +51,9 @@ func get_size() -> int:


func set_opacity(opacity: float):
_opacity = clampf(opacity, 0.0, 1.0)
if opacity != _opacity:
_opacity = clampf(opacity, 0.0, 1.0)
opacity_changed.emit(_opacity)


func get_opacity() -> float:
Expand Down Expand Up @@ -213,5 +216,3 @@ func configure_paint_input(painters: Array[HT_Painter], position: Vector2, press
func on_paint_end():
_prev_position = Vector2(-999, -999)
_prev_time_ms = 0


22 changes: 22 additions & 0 deletions addons/zylann.hterrain/tools/brush/brush_editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func set_terrain_painter(terrain_painter: HT_TerrainPainter):
_terrain_painter.flatten_height_changed.disconnect(_on_flatten_height_changed)
_terrain_painter.get_brush().shapes_changed.disconnect(_on_brush_shapes_changed)
_terrain_painter.get_brush().shape_index_changed.disconnect(_on_brush_shape_index_changed)
_terrain_painter.get_brush().size_changed.disconnect(_on_brush_size_changed)
_terrain_painter.get_brush().opacity_changed.disconnect(_on_brush_opacity_changed)

_terrain_painter = terrain_painter

Expand Down Expand Up @@ -138,6 +140,8 @@ func set_terrain_painter(terrain_painter: HT_TerrainPainter):
_terrain_painter.flatten_height_changed.connect(_on_flatten_height_changed)
brush.shapes_changed.connect(_on_brush_shapes_changed)
brush.shape_index_changed.connect(_on_brush_shape_index_changed)
brush.size_changed.connect(_on_brush_size_changed)
brush.opacity_changed.connect(_on_brush_opacity_changed)


func _on_flatten_height_changed():
Expand All @@ -149,6 +153,14 @@ func _on_brush_shapes_changed():
_update_shape_preview()


func _on_brush_size_changed(new_size):
_update_brush_size(new_size)


func _on_brush_opacity_changed(new_opacity):
_update_brush_opacity(new_opacity)


func _on_brush_shape_index_changed():
_update_shape_preview()

Expand All @@ -159,6 +171,16 @@ func _update_shape_preview():
_shape_texture_rect.texture = brush.get_shape(i)


func _update_brush_size(new_size):
if _terrain_painter != null:
_size_slider.value = new_size


func _update_brush_opacity(new_opacity):
if _terrain_painter != null:
_opacity_slider.value = new_opacity * 100.0


func set_display_mode(mode: int):
var show_flatten := mode == HT_TerrainPainter.MODE_FLATTEN
var show_color := mode == HT_TerrainPainter.MODE_COLOR
Expand Down
65 changes: 65 additions & 0 deletions addons/zylann.hterrain/tools/brush/brush_size_selector.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
@tool
extends Control

@export var brush_size_factor: int = 3
@export var min_value: int = -1
@export var max_value: int = -1
@export var brush_preview_color: Color = Color.LIGHT_GREEN :
set(value):
brush_preview_color = value
if is_inside_tree():
%BrushSizePreview.modulate = brush_preview_color

signal on_value_selected(new_value: int)
signal on_cancel

var background_margin: int = 10


func _ready() -> void:
%BrushSizePreview.modulate = brush_preview_color


func _process(delta: float) -> void:
_update_size(_get_mouse_distance())


func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT:
var distance := _get_mouse_distance()
on_value_selected.emit(distance)
else:
on_cancel.emit()


func _update_size(value: int) -> void:
var size := value * brush_size_factor
var ui_size := clamp(size, 40, 1000)
%BrushSizeBackground.size = Vector2(ui_size + background_margin, ui_size + background_margin)
%BrushSizeBackground.position = Vector2(-( (ui_size/2) + (background_margin/2)) , -( (ui_size/2) + (background_margin/2)))
%BrushSizePreview.size = Vector2(ui_size, ui_size)
%BrushSizePreview.position = Vector2(-(ui_size/2) , -(ui_size/2))

%ValueLabel.text = str(value)


func _get_mouse_distance() -> int:
var global_mouse_pos = get_global_mouse_position()

var distance: int = position.distance_to(global_mouse_pos)
if position.x > global_mouse_pos.x:
distance = 0

if (min_value >= 0):
distance = maxi(distance, min_value)

if (max_value >= 0):
distance = mini(distance, max_value)

return distance;


func set_initial_value(value: float) -> void:
position -= Vector2(value, 0)
_update_size(value)
61 changes: 61 additions & 0 deletions addons/zylann.hterrain/tools/brush/brush_size_selector.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[gd_scene load_steps=5 format=3 uid="uid://d324kwgdf7e6l"]

[ext_resource type="Script" path="res://addons/zylann.hterrain/tools/brush/brush_size_selector.gd" id="1_r2t4n"]
[ext_resource type="Texture2D" uid="uid://dthlbsmf51jk3" path="res://addons/zylann.hterrain/tools/brush/ui_images/brush_circle.png" id="2_hu4m7"]
[ext_resource type="Texture2D" uid="uid://b8nf7x6l56anw" path="res://addons/zylann.hterrain/tools/brush/ui_images/brush_circle_background.png" id="2_mj44p"]

[sub_resource type="LabelSettings" id="LabelSettings_0d2ij"]
font_size = 32

[node name="BrushSizeSelector" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_r2t4n")
brush_preview_color = Color(1, 0, 0, 1)

[node name="BrushSizeBackground" type="TextureRect" parent="."]
unique_name_in_owner = true
modulate = Color(0.478431, 0.478431, 0.478431, 0.423529)
layout_mode = 0
offset_left = -162.0
offset_top = -162.0
offset_right = 163.0
offset_bottom = 163.0
texture = ExtResource("2_mj44p")
expand_mode = 1
metadata/_edit_use_anchors_ = true

[node name="BrushSizePreview" type="TextureRect" parent="."]
unique_name_in_owner = true
modulate = Color(1, 0, 0, 1)
layout_mode = 1
offset_left = -157.0
offset_top = -157.0
offset_right = 158.0
offset_bottom = 158.0
texture = ExtResource("2_hu4m7")
expand_mode = 1
metadata/_edit_use_anchors_ = true

[node name="ValueLabel" type="Label" parent="."]
unique_name_in_owner = true
layout_mode = 1
anchors_preset = -1
anchor_top = 0.001
anchor_bottom = 0.001
offset_left = -36.0
offset_top = -22.648
offset_right = 37.0
offset_bottom = 22.352
grow_horizontal = 2
grow_vertical = 2
scale = Vector2(0.7, 0.7)
pivot_offset = Vector2(36, 22)
text = "105"
label_settings = SubResource("LabelSettings_0d2ij")
horizontal_alignment = 1
vertical_alignment = 1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions addons/zylann.hterrain/tools/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ var _mouse_pressed := false
#var _pending_paint_action = null
var _pending_paint_commit := false

var _editor_viewports = null
var _overlay_selector = null
var _widget_action_name = ""

var _logger := HT_Logger.get_for(self)


Expand Down Expand Up @@ -282,6 +286,9 @@ func _enter_tree():

_texture_set_editor.import_selected.connect(_on_TextureSetEditor_import_selected)

var editor_viewports_container = _get_editor_viewports_container()
_editor_viewports = editor_viewports_container.find_children("*","Control", false,false)


func _exit_tree():
_logger.debug("HTerrain plugin Exit tree")
Expand Down Expand Up @@ -455,6 +462,20 @@ func _forward_3d_gui_input(p_camera: Camera3D, p_event: InputEvent) -> int:

var captured_event = false

if p_event is InputEventKey:
if p_event.keycode == KEY_G and p_event.is_echo() == false and p_event.pressed:
captured_event = true
_show_brush_size_selector(2, 300, Color.LIGHT_GREEN, _terrain_painter.get_brush_size(), "brush_size_action",
func on_value_changed(value):
_terrain_painter.set_brush_size(value)\
)
elif p_event.keycode == KEY_H and p_event.is_echo() == false and p_event.pressed:
captured_event = true
_show_brush_size_selector(0, 100, Color.LIGHT_GREEN, _terrain_painter.get_opacity()*100, "brush_opacity_action",
func on_value_changed(value):
_terrain_painter.set_opacity(value/100.0)\
)

if p_event is InputEventMouseButton:
var mb = p_event

Expand Down Expand Up @@ -852,6 +873,60 @@ func _debug_spawn_collider_indicators():
mi.position = hit.position



func _show_brush_size_selector(min_size, max_size, widget_color, initial_value, action_name, on_value_selected):
if _widget_action_name != action_name:
var active_viewport = _get_active_viewport()

if active_viewport == null:
# when mouse outside 3d viewport area, dont try to show widget
return

_widget_action_name = action_name
if active_viewport != null:
var selector_scene = ResourceLoader.load("res://addons/zylann.hterrain/tools/brush/brush_size_selector.tscn")
var selector = selector_scene.instantiate()

selector.min_value = min_size
selector.max_value = max_size
selector.brush_preview_color = widget_color

_overlay_selector = selector
_overlay_selector.position = active_viewport.get_global_mouse_position()
_overlay_selector.set_meta("action", action_name)

EditorInterface.get_base_control().add_child(_overlay_selector)
selector.set_initial_value(initial_value)

selector.on_value_selected.connect(func f(value):
on_value_selected.call(value)
_remove_overlay_selector()
)
selector.on_cancel.connect(func f():
_remove_overlay_selector()
)


func _remove_overlay_selector():
if _overlay_selector != null:
_overlay_selector.queue_free()
_overlay_selector = null
_widget_action_name = ""


func _get_active_viewport():
for viewport in _editor_viewports:
if viewport.get_rect().has_point(viewport.get_local_mouse_position()):
return viewport

return null


func _get_editor_viewports_container():
# Returns the Node3DEditorViewportContainer
return EditorInterface.get_editor_viewport_3d().get_parent().get_parent()


func _spawn_vertical_bound_boxes():
var data := _node.get_data()
# var sy = data._chunked_vertical_bounds_size_y
Expand Down

0 comments on commit 1a5483f

Please sign in to comment.