Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion addons/rmsmartshape/assets/icon_editor_handle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion addons/rmsmartshape/assets/icon_editor_handle.svg.import
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.5
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.5
svg/scale=2.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
4 changes: 3 additions & 1 deletion addons/rmsmartshape/assets/icon_editor_handle_bezier.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.5
svg/scale=2.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
6 changes: 3 additions & 3 deletions addons/rmsmartshape/assets/icon_editor_handle_selected.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.25
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
82 changes: 45 additions & 37 deletions addons/rmsmartshape/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var ICON_SNAP: Texture2D = load("res://addons/rmsmartshape/assets/icon_editor_sn
var ICON_IMPORT_CLOSED: Texture2D = load("res://addons/rmsmartshape/assets/closed_shape.png")
var ICON_IMPORT_OPEN: Texture2D = load("res://addons/rmsmartshape/assets/open_shape.png")

const HANDLE_RADIUS: float = 13

const FUNC = preload("plugin_functionality.gd")
const ActionAddCollisionNodes := preload("res://addons/rmsmartshape/actions/action_add_collision_nodes.gd")
const ActionMoveVerticies := preload("res://addons/rmsmartshape/actions/action_move_verticies.gd")
Expand Down Expand Up @@ -457,21 +459,18 @@ func _forward_canvas_gui_input(event: InputEvent) -> bool:
cached_shape_global_transform = shape.get_global_transform()

var et: Transform2D = get_et()
var grab_threshold: float = EditorInterface.get_editor_settings().get(
"editors/polygon_editor/point_grab_radius"
)

var key_return_value := false
if event is InputEventKey:
key_return_value = _input_handle_keyboard_event(event)

var mb_return_value := false
if event is InputEventMouseButton:
mb_return_value = _input_handle_mouse_button_event(event, et, grab_threshold)
mb_return_value = _input_handle_mouse_button_event(event, et, HANDLE_RADIUS)

var mm_return_value := false
if event is InputEventMouseMotion:
mb_return_value = _input_handle_mouse_motion_event(event, et, grab_threshold)
mb_return_value = _input_handle_mouse_motion_event(event, et, HANDLE_RADIUS)

var return_value := key_return_value == true or mb_return_value == true or mm_return_value == true
_gui_update_info_panels()
Expand Down Expand Up @@ -733,12 +732,15 @@ func _forward_canvas_draw_over_viewport(overlay: Control) -> void:
if not is_shape_valid() or not is_inside_tree():
return

if Input.is_key_pressed(KEY_H):
return

match current_mode:
MODE.CREATE_VERT:
draw_mode_edit_vert(overlay)
if Input.is_key_pressed(KEY_ALT) and Input.is_key_pressed(KEY_SHIFT):
draw_new_shape_preview(overlay)
elif Input.is_key_pressed(KEY_ALT):
elif Input.is_key_pressed(KEY_ALT) or shape.is_shape_closed():
draw_new_point_close_preview(overlay)
else:
draw_new_point_preview(overlay)
Expand Down Expand Up @@ -777,8 +779,8 @@ func draw_mode_edit_edge(overlay: Control, color_normal: Color, color_highlight:
var t: Transform2D = get_et() * shape.get_global_transform()
var verts: PackedVector2Array = shape.get_vertices()

draw_shape_outline(overlay, t, verts, Color.BLACK, 3)
draw_shape_outline(overlay, t, verts, color_normal)
draw_vert_handles(overlay, t, verts, false)

if current_action.type == ACTION_VERT.MOVE_VERT:
var edge_point_keys := current_action.keys
Expand Down Expand Up @@ -824,38 +826,40 @@ func draw_mode_edit_vert(overlay: Control, show_vert_handles: bool = true) -> vo
var t: Transform2D = get_et() * shape.get_global_transform()
var verts: PackedVector2Array = shape.get_vertices()
var points: PackedVector2Array = shape.get_tessellated_points()
draw_shape_outline(overlay, t, points, shape.modulate)
var color := Color(1, 1, 1, 0.6)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this color a constant to make it reusable and to prevent needless allocations.

draw_shape_outline(overlay, t, points, color)
if show_vert_handles:
draw_vert_handles(overlay, t, verts, true)
if on_edge:
overlay.draw_texture(ICON_ADD_HANDLE, edge_point - ICON_ADD_HANDLE.get_size() * 0.5)

# Draw Highlighted Handle
if current_action.is_single_vert_selected():
var tex: Texture2D = ICON_HANDLE_SELECTED
overlay.draw_texture(
tex, t * verts[current_action.current_point_index(shape)] - tex.get_size() * 0.5
)


func draw_shape_outline(
overlay: Control, t: Transform2D, points: PackedVector2Array, color: Color, width: float = 2.0
overlay: Control, t: Transform2D, points: PackedVector2Array, color: Color, width: float = 1.0
) -> void:
if points.size() >= 2:
overlay.draw_polyline(t * points, Color.BLACK, width * 1.5, true)
overlay.draw_polyline(t * points, color, width, true)


func draw_vert_handles(
overlay: Control, t: Transform2D, verts: PackedVector2Array, control_points: bool
) -> void:
var transformed_verts := t * verts
for i in verts.size():
var draw_vert_count: int = max(verts.size() - 1, 0) if shape.is_shape_closed() else verts.size()

for i in draw_vert_count:
# Draw Vert handles
var hp: Vector2 = transformed_verts[i]
var icon: Texture2D = ICON_HANDLE_BEZIER if (Input.is_key_pressed(KEY_SHIFT) and not current_mode == MODE.FREEHAND) else ICON_HANDLE
var icon: Texture2D = ICON_HANDLE
overlay.draw_texture(icon, hp - icon.get_size() * 0.5)

# Draw Highlighted Handle
if current_action.is_single_vert_selected():
var tex: Texture2D = ICON_HANDLE_SELECTED
overlay.draw_texture(
tex, t * verts[current_action.current_point_index(shape)] - tex.get_size() * 0.5
)

# Draw Width handle
var offset: float = WIDTH_HANDLE_OFFSET
var width_handle_key: int = closest_key
Expand Down Expand Up @@ -885,26 +889,30 @@ func draw_vert_handles(
for i in verts.size():
var key: int = shape.get_point_key_at_index(i)
var hp: Vector2 = transformed_verts[i]
var is_bezier_vert: bool = false

# Drawing the point-out for the last point makes no sense, as there's no point ahead of it
if i < verts.size() - 1:
var pointout: Vector2 = t * (verts[i] + shape.get_point_out(key))
if hp != pointout:
_draw_control_point_line(overlay, hp, pointout, ICON_HANDLE_CONTROL)
is_bezier_vert = true
# Drawing the point-in for point 0 makes no sense, as there's no point behind it
if i > 0:
var pointin: Vector2 = t * (verts[i] + shape.get_point_in(key))
if hp != pointin:
_draw_control_point_line(overlay, hp, pointin, ICON_HANDLE_CONTROL)
is_bezier_vert = true

if is_bezier_vert:
overlay.draw_texture(ICON_HANDLE_BEZIER, hp - ICON_HANDLE_BEZIER.get_size() * 0.5)


func _draw_control_point_line(c: Control, vert: Vector2, cp: Vector2, tex: Texture2D) -> void:
# Draw the line with a dark and light color to be visible on all backgrounds
var color_dark := Color(0, 0, 0, 0.3)
var color_light := Color(1, 1, 1, .5)
var color_light := Color(1, 1, 1, .6)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the color constant from before.

var width := 2.0
var normal := (cp - vert).normalized()
c.draw_line(vert + normal * 4 + Vector2.DOWN, cp + Vector2.DOWN, color_dark, width)
c.draw_line(vert + normal * 4, cp, color_light, width)
c.draw_texture(tex, cp - tex.get_size() * 0.5)

Expand All @@ -918,13 +926,8 @@ func draw_new_point_preview(overlay: Control) -> void:
var mouse: Vector2 = overlay.get_local_mouse_position()

if verts.size() > 0:
var a: Vector2
if shape.is_shape_closed() and verts.size() > 1:
a = t * verts[verts.size() - 2]
overlay.draw_line(mouse, t * verts[0], color,width * .5)
else:
a = t * verts[verts.size() - 1]
overlay.draw_line(mouse, a, color, width)
var a: Vector2 = t * verts[verts.size() - 1]
overlay.draw_dashed_line(mouse, a, color, width, width * 3)

overlay.draw_texture(ICON_ADD_HANDLE, mouse - ICON_ADD_HANDLE.get_size() * 0.5)

Expand All @@ -938,9 +941,8 @@ func draw_new_point_close_preview(overlay: Control) -> void:
var mouse: Vector2 = overlay.get_local_mouse_position()
var a: Vector2 = t * shape.get_point_position(closest_edge_keys[0])
var b: Vector2 = t * shape.get_point_position(closest_edge_keys[1])
overlay.draw_line(mouse, a, color, width)
color.a = 0.1
overlay.draw_line(mouse, b, color, width)
overlay.draw_dashed_line(mouse, a, color, width, width * 3)
overlay.draw_dashed_line(mouse, b, color, width, width * 3)
overlay.draw_texture(ICON_ADD_HANDLE, mouse - ICON_ADD_HANDLE.get_size() * 0.5)


Expand Down Expand Up @@ -1053,12 +1055,13 @@ func _input_handle_left_click(

if current_mode == MODE.EDIT_VERT or current_mode == MODE.CREATE_VERT:
gui_edge_info_panel.visible = false
var can_add_point: bool = Input.is_key_pressed(KEY_ALT) or current_mode == MODE.CREATE_VERT
var is_first_selected: bool = current_action.is_single_vert_selected() and current_action.current_point_key() == shape.get_point_key_at_index(0)

if _defer_mesh_updates:
shape.begin_update()

var can_add_point: bool = Input.is_key_pressed(KEY_ALT) or current_mode == MODE.CREATE_VERT
var is_first_selected: bool = current_action.is_single_vert_selected() and current_action.current_point_key() == shape.get_point_key_at_index(0)

# Close the shape if the first point is clicked
if can_add_point and is_first_selected and shape.can_close():
var close_action := ActionCloseShape.new(shape)
Expand All @@ -1070,7 +1073,7 @@ func _input_handle_left_click(
return true

# Any nearby control points to move?
if not Input.is_key_pressed(KEY_ALT):
if not Input.is_key_pressed(KEY_ALT) and current_mode != MODE.CREATE_VERT:
if _input_move_control_points(mb, vp_m_pos, grab_threshold):
return true

Expand Down Expand Up @@ -1105,7 +1108,7 @@ func _input_handle_left_click(
selection.clear()
selection.add_node(copy)
shape = copy
elif Input.is_key_pressed(KEY_ALT):
elif Input.is_key_pressed(KEY_ALT) or shape.is_shape_closed():
# Add point between start and end points of the closest edge
idx = shape.get_point_index(closest_edge_keys[1])
var add_point := ActionAddPoint.new(shape, local_position, idx, not _defer_mesh_updates)
Expand Down Expand Up @@ -1212,6 +1215,9 @@ func _input_handle_keyboard_event(event: InputEventKey) -> bool:
if kb.keycode == KEY_ALT:
update_overlays()

if kb.keycode == KEY_H:
update_overlays()

return true
return false

Expand All @@ -1230,6 +1236,8 @@ func _is_valid_keyboard_scancode(kb: InputEventKey) -> bool:
return true
KEY_CTRL:
return true
KEY_H:
return true
return false


Expand Down Expand Up @@ -1530,7 +1538,7 @@ func _input_handle_mouse_motion_event(
on_edge = false
on_width_handle = true
current_action = select_verticies([mouse_over_width_handle], ACTION_VERT.NONE)
elif Input.is_key_pressed(KEY_ALT):
elif Input.is_key_pressed(KEY_ALT) or current_mode == MODE.CREATE_VERT:
_input_find_closest_edge_keys(mm)
else:
deselect_verts()
Expand Down