Added guides

- Added horizontal & vertical guides which can be dragged from their respective rulers
- Fixed bug when opening and importing files
This commit is contained in:
OverloadedOrama 2019-11-21 00:11:21 +02:00
parent 8b5ee21d01
commit 224316256c
10 changed files with 186 additions and 98 deletions

View file

@ -10,6 +10,7 @@ var frame := 0 setget frame_changed
var frame_button : VBoxContainer
var frame_texture_rect : TextureRect
var current_pixel := Vector2.ZERO #pretty much same as mouse_pos, but can be accessed externally
var previous_mouse_pos := Vector2.ZERO
var previous_action := "None"
var mouse_inside_canvas := false #used for undo
@ -75,7 +76,8 @@ func camera_zoom() -> void:
func _process(delta) -> void:
sprite_changed_this_frame = false
update()
var mouse_pos := get_local_mouse_position() - location
current_pixel = get_local_mouse_position() - location
var mouse_pos := current_pixel
var mouse_pos_floored := mouse_pos.floor()
var mouse_pos_ceiled := mouse_pos.ceil()
var mouse_in_canvas := point_in_rectangle(mouse_pos, location, location + size)

View file

@ -34,6 +34,7 @@ var right_square_indicator_visible := false
var camera : Camera2D
var camera2 : Camera2D
var selection_rectangle : Polygon2D
var vertical_ruler : BaseButton
# warning-ignore:unused_class_variable
var selected_pixels := []
var image_clipboard : Image
@ -119,8 +120,8 @@ func _ready() -> void:
split_screen_button = find_node_by_name(root, "SplitScreenButton")
camera = find_node_by_name(canvas_parent, "Camera2D")
camera2 = find_node_by_name(root, "Camera2D2")
selection_rectangle = find_node_by_name(root, "SelectionRectangle")
vertical_ruler = find_node_by_name(root, "VerticalRuler")
image_clipboard = Image.new()
file_menu = find_node_by_name(root, "FileMenu")

62
Scripts/Guides.gd Normal file
View file

@ -0,0 +1,62 @@
extends Line2D
class_name Guide
enum TYPE {HORIZONTAL, VERTICAL}
var font := preload("res://Assets/Fonts/Roboto-Regular.tres")
var has_focus := true
var mouse_pos := Vector2.ZERO
var type = TYPE.HORIZONTAL
func _ready() -> void:
width = 0.1
# warning-ignore:unused_argument
func _process(delta) -> void:
width = Global.camera.zoom.x
mouse_pos = get_local_mouse_position()
var point0 := points[0]
var point1 := points[1]
if type == TYPE.HORIZONTAL:
point0.y -= width * 3
point1.y += width * 3
else:
point0.x -= width * 3
point1.x += width * 3
if point_in_rectangle(mouse_pos, point0, point1) && Input.is_action_just_pressed("left_mouse"):
if !point_in_rectangle(Global.canvas.current_pixel, Global.canvas.location, Global.canvas.location + Global.canvas.size):
has_focus = true
Global.has_focus = false
update()
if has_focus && Input.is_action_pressed("left_mouse"):
if type == TYPE.HORIZONTAL:
points[0].y = round(mouse_pos.y)
points[1].y = round(mouse_pos.y)
else:
points[0].x = round(mouse_pos.x)
points[1].x = round(mouse_pos.x)
if Input.is_action_just_released("left_mouse"):
if has_focus:
Global.has_focus = true
has_focus = false
update()
if type == TYPE.HORIZONTAL:
if points[0].y < 0 || points[0].y > Global.canvas.size.y:
queue_free()
else:
if points[0].x < 0 || points[0].x > Global.canvas.size.x:
queue_free()
func _draw() -> void:
if has_focus:
var viewport_size := Global.main_viewport.rect_size
var zoom := Global.camera.zoom
if type == TYPE.HORIZONTAL:
draw_set_transform(Vector2(Global.camera.offset.x - (viewport_size.x / 2) * zoom.x, points[0].y + font.get_height() * zoom.x * 2), rotation, zoom * 2)
draw_string(font, Vector2.ZERO, "%spx" % str(round(mouse_pos.y)))
else:
draw_set_transform(Vector2(points[0].x + font.get_height() * zoom.y, Global.camera.offset.y - (viewport_size.y / 2.25) * zoom.y), rotation, zoom * 2)
draw_string(font, Vector2.ZERO, "%spx" % str(round(mouse_pos.x)))
func point_in_rectangle(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y

View file

@ -1,4 +1,4 @@
extends Panel
extends Button
const RULER_WIDTH := 16
@ -22,7 +22,7 @@ func _draw() -> void:
var zoom := 1 / Global.camera.zoom.x
transform.x = Vector2(zoom, zoom)
transform.origin = Global.main_viewport.rect_size / 2 + (Global.camera.offset) * -zoom
transform.origin = Global.main_viewport.rect_size / 2 + Global.camera.offset * -zoom
var basic_rule := 100.0
var i := 0
@ -53,3 +53,15 @@ func _draw() -> void:
draw_line(Vector2(position.x + RULER_WIDTH, RULER_WIDTH * 0.33), Vector2(position.x + RULER_WIDTH, RULER_WIDTH), Color.white)
else:
draw_line(Vector2(position.x + RULER_WIDTH, RULER_WIDTH * 0.66), Vector2(position.x + RULER_WIDTH, RULER_WIDTH), Color.white)
func _on_HorizontalRuler_pressed() -> void:
var mouse_pos := get_local_mouse_position()
if mouse_pos.x < RULER_WIDTH: #For double guides
Global.vertical_ruler._on_VerticalRuler_pressed()
var line_2d := Guide.new()
line_2d.type = line_2d.TYPE.HORIZONTAL
line_2d.default_color = Color.purple
line_2d.add_point(Vector2(-99999, Global.canvas.current_pixel.y))
line_2d.add_point(Vector2(99999, Global.canvas.current_pixel.y))
Global.canvas.add_child(line_2d)
Global.has_focus = false

View file

@ -279,8 +279,6 @@ func _on_CreateNewImage_confirmed() -> void:
Global.canvas.layers[0][0].fill(fill_color)
Global.canvas.layers[0][0].lock()
Global.canvas.update_texture(0)
Global.remove_frame_button.disabled = true
Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN
Global.undo_redo.clear_history(false)
func _on_OpenSprite_file_selected(path) -> void:
@ -354,15 +352,8 @@ func _on_OpenSprite_file_selected(path) -> void:
Global.create_brush_button(image)
brush_line = file.get_line()
Global.undo_redo.clear_history(false)
file.close()
if frame > 1:
Global.remove_frame_button.disabled = false
Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
else:
Global.remove_frame_button.disabled = true
Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN
Global.undo_redo.clear_history(false)
func _on_SaveSprite_file_selected(path) -> void:
current_save_path = path
@ -445,12 +436,6 @@ func _on_ImportSprites_files_selected(paths) -> void:
Global.canvas = Global.canvases[Global.canvases.size() - 1]
Global.canvas.visible = true
biggest_canvas.camera_zoom()
if i > 1:
Global.remove_frame_button.disabled = false
Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
else:
Global.remove_frame_button.disabled = true
Global.remove_frame_button.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN
Global.undo_redo.clear_history(false)

View file

@ -46,7 +46,7 @@ func _draw() -> void:
draw_line(Vector2(position.x + RULER_WIDTH, 0), Vector2(position.x + RULER_WIDTH, RULER_WIDTH), Color.white)
var val = (ruler_transform * major_subdivide * minor_subdivide).xform(Vector2(i, 0)).x / 100
val = stepify(val, 0.01)
draw_string(font, Vector2(position.x + RULER_WIDTH + 2, font.get_height() - 6), str(val))
draw_string(font, Vector2(position.x + RULER_WIDTH + 2, font.get_height() - 6), "%ss" % str(val))
else:
if i % minor_subdivision == 0:
draw_line(Vector2(position.x + RULER_WIDTH, RULER_WIDTH * 0.33), Vector2(position.x + RULER_WIDTH, RULER_WIDTH), Color.white)

View file

@ -1,4 +1,4 @@
extends Panel
extends Button
const RULER_WIDTH := 16
@ -22,7 +22,7 @@ func _draw() -> void:
var zoom := 1 / Global.camera.zoom.x
transform.y = Vector2(zoom, zoom)
transform.origin = Global.main_viewport.rect_size / 2 + (Global.camera.offset) * -zoom
transform.origin = Global.main_viewport.rect_size / 2 + Global.camera.offset * -zoom
var basic_rule := 100.0
var i := 0
@ -53,3 +53,12 @@ func _draw() -> void:
draw_line(Vector2(RULER_WIDTH * 0.33, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
else:
draw_line(Vector2(RULER_WIDTH * 0.66, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
func _on_VerticalRuler_pressed() -> void:
var line_2d := Guide.new()
line_2d.type = line_2d.TYPE.VERTICAL
line_2d.default_color = Color.purple
line_2d.add_point(Vector2(Global.canvas.current_pixel.x, -99999))
line_2d.add_point(Vector2(Global.canvas.current_pixel.x, 99999))
Global.canvas.add_child(line_2d)
Global.has_focus = false