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 wrong cursor texture orientation on rotated canvas #21

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
22 changes: 20 additions & 2 deletions project/addons/net.yarvis.pixel_pen/editor/editor_canvas.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[gd_scene load_steps=14 format=3 uid="uid://dde4flu0tmqb3"]
[gd_scene load_steps=16 format=3 uid="uid://dde4flu0tmqb3"]

[ext_resource type="Shader" path="res://addons/net.yarvis.pixel_pen/resources/editor_canvas_cursor.gdshader" id="1_iq3vk"]
[ext_resource type="Script" path="res://addons/net.yarvis.pixel_pen/editor/editor_canvas/editor_canvas.gd" id="1_je1bm"]
[ext_resource type="Shader" path="res://addons/net.yarvis.pixel_pen/resources/editor_canvas.gdshader" id="2_ehwxr"]
[ext_resource type="Shader" path="res://addons/net.yarvis.pixel_pen/resources/grayscale.gdshader" id="3_g2v7b"]
[ext_resource type="Shader" path="res://addons/net.yarvis.pixel_pen/resources/marching_ant.gdshader" id="4_n22mx"]
[ext_resource type="Shader" path="res://addons/net.yarvis.pixel_pen/resources/marching_ant_filled.gdshader" id="5_6iuph"]
[ext_resource type="Script" path="res://addons/net.yarvis.pixel_pen/editor/editor_canvas/cursor.gd" id="7_qpw1y"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_c4sdf"]
shader = ExtResource("1_iq3vk")
Expand Down Expand Up @@ -41,7 +42,10 @@ shader_parameter/grayscale = false

[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_oy7v3"]

[node name="EditorCanvas" type="Node2D" node_paths=PackedStringArray("tile_node", "background_canvas", "onion_skinning", "layers", "camera", "overlay_hint", "selection_tool_hint", "filter")]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_06oqe"]
shader = ExtResource("1_iq3vk")

[node name="EditorCanvas" type="Node2D" node_paths=PackedStringArray("tile_node", "background_canvas", "onion_skinning", "layers", "camera", "overlay_hint", "selection_tool_hint", "filter", "cursor_surface")]
material = SubResource("ShaderMaterial_c4sdf")
script = ExtResource("1_je1bm")
tile_node = NodePath("Tiled")
Expand All @@ -52,6 +56,7 @@ camera = NodePath("Camera2D")
overlay_hint = NodePath("Overlay")
selection_tool_hint = NodePath("SelectionTool")
filter = NodePath("Filter")
cursor_surface = NodePath("CursorSurface/Cursor")

[node name="BackgroudCanvas" type="Sprite2D" parent="."]
show_behind_parent = true
Expand Down Expand Up @@ -94,3 +99,16 @@ texture = SubResource("PlaceholderTexture2D_oy7v3")
centered = false

[node name="Camera2D" type="Camera2D" parent="."]

[node name="CursorSurface" type="CanvasLayer" parent="."]

[node name="Cursor" type="Control" parent="CursorSurface"]
material = SubResource("ShaderMaterial_06oqe")
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
script = ExtResource("7_qpw1y")
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func _on_shift_pressed(pressed : bool):

func _on_draw_cursor(mouse_position : Vector2):
if shift_mode:
draw_color_picker_cursor(mouse_position)
draw_plus_cursor(mouse_position)
node.overlay_hint.visible = false
return
node.overlay_hint.visible = true
Expand All @@ -201,6 +201,11 @@ func _on_draw_cursor(mouse_position : Vector2):
node.overlay_hint.position = floor(mouse_position) - (brush_mask.get_size() - Vector2.ONE) * 0.5


func _on_get_tool_texture() -> Texture2D:
if shift_mode:
return color_picker_texture
return null

func update_brush():
if PixelPen.state.current_project == null:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func on_draw_cursor(mouse_position : Vector2):
tool.draw_invalid_cursor(mouse_position)


func on_get_tool_texture() -> Texture2D:
return tool._on_get_tool_texture()


func on_draw_hint(mouse_position : Vector2):
if tool.node.virtual_mouse:
tool._on_draw_hint(tool.node.virtual_mouse_origin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ func _on_shift_pressed(pressed : bool):


func _on_draw_cursor(mouse_position : Vector2):
if shift_mode:
draw_color_picker_cursor(mouse_position)
return
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)


func _on_get_tool_texture() -> Texture2D:
if shift_mode:
return color_picker_texture
return texture
16 changes: 16 additions & 0 deletions project/addons/net.yarvis.pixel_pen/editor/editor_canvas/cursor.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@tool
extends Control


@export var tool_texture : Texture2D


func _process(_delta):
if tool_texture != null:
queue_redraw()


func _draw():
if tool_texture != null:
var rect : Rect2 = Rect2(get_local_mouse_position() + Vector2(8, -24), Vector2(16, 16))
draw_texture_rect(tool_texture, rect, false)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var MoveTool := load("res://addons/net.yarvis.pixel_pen/editor/editor_canvas/mov
@export var overlay_hint : Sprite2D
@export var selection_tool_hint : Sprite2D
@export var filter : Sprite2D
@export var cursor_surface : Control
@export var silhouette : bool = false
@export var virtual_mouse : bool = false
@export var show_view_grayscale : bool = false:
Expand Down Expand Up @@ -322,10 +323,13 @@ func _draw():
if _on_pan_shorcut_mode:
if rmb_inject_mode:
canvas_paint.tool.draw_pan_cursor(virtual_mouse_origin)
cursor_surface.tool_texture = canvas_paint.tool.pan_texture
else:
canvas_paint.tool.draw_pan_cursor(get_local_mouse_position())
cursor_surface.tool_texture = canvas_paint.tool.pan_texture
else:
canvas_paint.on_draw_cursor(get_local_mouse_position())
cursor_surface.tool_texture = canvas_paint.on_get_tool_texture()
elif type_hovered == 0:
_symetric_guid_color_vertical.a = 0.75
canvas_paint.tool.draw_plus_cursor(get_local_mouse_position(), 15)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ func _on_shift_pressed(shift_pressed : bool):


func _on_draw_cursor(mouse_position : Vector2):
if shift_mode:
draw_color_picker_cursor(mouse_position)
return
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)


func _on_get_tool_texture() -> Texture2D:
if shift_mode:
return color_picker_texture
return texture


func show_hint(scale : float, force_outline : bool = false):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ func _on_shift_pressed(pressed : bool):


func _on_draw_cursor(mouse_position : Vector2):
if shift_mode:
draw_color_picker_cursor(mouse_position)
return
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)


func _on_get_tool_texture() -> Texture2D:
if shift_mode:
return color_picker_texture
return texture
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func _on_shift_pressed(pressed : bool):

func _on_draw_cursor(mouse_position : Vector2):
if shift_mode:
draw_color_picker_cursor(mouse_position)
draw_plus_cursor(mouse_position)
return
if is_pressed:
var rect : Rect2i = Rect2i(Vector2i.ZERO, node.canvas_size)
Expand Down Expand Up @@ -156,5 +156,9 @@ func _on_draw_cursor(mouse_position : Vector2):
_cache_line_mask = null

draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)


func _on_get_tool_texture() -> Texture2D:
if shift_mode:
return color_picker_texture
return texture
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,15 @@ func _on_force_cancel():

func _on_draw_cursor(mouse_position : Vector2):
draw_plus_cursor(mouse_position, 15.0 if is_pressed and mode == Mode.SELECT_PIXEL else 10.0)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
node.overlay_hint.material.set_shader_parameter("zoom_bias", node.get_viewport().get_camera_2d().zoom)


func _on_get_tool_texture() -> Texture2D:
if mode == Mode.SELECT_PIXEL:
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, magnet_texture)
return magnet_texture
else:
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, magnet_on_texture)
node.overlay_hint.material.set_shader_parameter("zoom_bias", node.get_viewport().get_camera_2d().zoom)
return magnet_on_texture
return null


func collecte_pixel(from : Vector2, to : Vector2) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,12 @@ func _on_force_cancel():

func _on_draw_cursor(mouse_position : Vector2):
draw_plus_cursor(mouse_position)
if not _is_rotate_anchor_hovered:
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)


func _on_get_tool_texture() -> Texture2D:
if _is_rotate_anchor_hovered:
return null
return texture


func _on_draw_hint(mouse_position : Vector2):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func _on_mouse_motion(mouse_position : Vector2, event_relative : Vector2, callba

func _on_draw_cursor(mouse_position : Vector2):
draw_pan_cursor(mouse_position)


func _on_get_tool_texture() -> Texture2D:
return pan_texture
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func _on_shift_pressed(pressed : bool):

func _on_draw_cursor(mouse_position : Vector2):
if shift_mode:
draw_color_picker_cursor(mouse_position)
draw_plus_cursor(mouse_position)
return

var has_zoom : bool = node.get_viewport().get_camera_2d() != null
Expand All @@ -175,3 +175,9 @@ func _on_draw_cursor(mouse_position : Vector2):

# Draw center cursor
draw_circle_cursor(mouse_position)


func _on_get_tool_texture() -> Texture2D:
if shift_mode:
return color_picker_texture
return null
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ func _on_shift_pressed(pressed : bool):


func _on_draw_cursor(mouse_position : Vector2):
if shift_mode:
draw_color_picker_cursor(mouse_position)
return
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)


func _on_get_tool_texture() -> Texture2D:
if shift_mode:
return color_picker_texture
return texture


func _on_draw_hint(mouse_position : Vector2):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func _on_mouse_pressed(mouse_position : Vector2, callback : Callable):

func _on_draw_cursor(mouse_position : Vector2):
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x


func _on_get_tool_texture() -> Texture2D:
if active_sub_tool_type == PixelPenEnum.ToolBoxSelect.TOOL_SELECT_COLOR:
var texture : Texture2D
match SelectionTool.sub_tool_selection_type:
Expand All @@ -80,6 +82,7 @@ func _on_draw_cursor(mouse_position : Vector2):
PixelPenEnum.ToolBoxSelection.TOOL_SELECTION_INTERSECTION:
texture = selection_intersection
if texture != null:
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)
return texture
elif active_sub_tool_type == PixelPenEnum.ToolBoxSelect.TOOL_SELECT_LAYER:
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, select_layer_texture)
return select_layer_texture
return null
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ func _on_force_cancel():

func _on_draw_cursor(mouse_position : Vector2):
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x


func _on_get_tool_texture() -> Texture2D:
var texture : Texture2D
match sub_tool_selection_type:
PixelPenEnum.ToolBoxSelection.TOOL_SELECTION_UNION:
Expand All @@ -131,7 +133,8 @@ func _on_draw_cursor(mouse_position : Vector2):
PixelPenEnum.ToolBoxSelection.TOOL_SELECTION_INTERSECTION:
texture = selection_intersection
if texture != null:
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, texture)
return texture
return null


func _on_draw_hint(mouse_position : Vector2):
Expand Down
15 changes: 4 additions & 11 deletions project/addons/net.yarvis.pixel_pen/editor/editor_canvas/tool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func _on_draw_hint(mouse_position : Vector2):
pass


func _on_get_tool_texture() -> Texture2D:
return null


func pick_color_from_canvas(mouse_position : Vector2, emit_color_picked : bool = true) -> int:
var index_image = (PixelPen.state.current_project as PixelPenProject).active_frame.layers
var coord : Vector2 = floor(mouse_position)
Expand Down Expand Up @@ -270,14 +274,10 @@ func draw_plus_cursor(mouse_position : Vector2, size : float = 10.0):

func draw_color_picker_cursor(mouse_position : Vector2):
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, color_picker_texture)


func draw_pan_cursor(mouse_position : Vector2):
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, pan_texture)


func draw_invalid_cursor(mouse_position : Vector2, size : float = 10.0):
Expand All @@ -287,13 +287,6 @@ func draw_invalid_cursor(mouse_position : Vector2, size : float = 10.0):
node.draw_line(mouse_position - line, mouse_position + line, Color.RED, 0.2 * cursor_length)


func draw_texture(position : Vector2, texture : Texture2D, scaled : bool = true):
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x
if not scaled:
cursor_length = 0.2
var rect = Rect2(position, Vector2.ONE * cursor_length)
node.draw_texture_rect(texture, rect, false, Color(1,1,1,0.75))

## Return center mass, if return Vector(-1, -1) -> ellipse outside canvas
static func get_midpoint_ellipse(start: Vector2, end: Vector2, color : Color, image : Image) -> Vector2:
var bound : Rect2i = Rect2i(Vector2i.ZERO, image.get_size())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ func _on_shift_pressed(pressed : bool):

func _on_draw_cursor(mouse_position : Vector2):
draw_plus_cursor(mouse_position)
var cursor_length : float = (node.get_viewport_transform().affine_inverse() * 20.0).x.x


func _on_get_tool_texture() -> Texture2D:
if active_sub_tool_type == PixelPenEnum.ToolBoxZoom.TOOL_ZOOM_IN:
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, zoom_out_texture if shift_mode else zoom_in_texture)
return zoom_out_texture if shift_mode else zoom_in_texture
elif active_sub_tool_type == PixelPenEnum.ToolBoxZoom.TOOL_ZOOM_OUT:
draw_texture(mouse_position + Vector2(0.5, -1.5) * cursor_length, zoom_in_texture if shift_mode else zoom_out_texture)
return zoom_in_texture if shift_mode else zoom_out_texture
return null