2019-11-20 22:11:21 +00:00
|
|
|
extends Button
|
2019-11-19 01:52:04 +00:00
|
|
|
|
|
|
|
const RULER_WIDTH := 16
|
|
|
|
|
|
|
|
var font := preload("res://Assets/Fonts/Roboto-Small.tres")
|
|
|
|
var major_subdivision := 2
|
|
|
|
var minor_subdivision := 5
|
|
|
|
|
|
|
|
var first : Vector2
|
|
|
|
var last : Vector2
|
|
|
|
|
2019-11-19 21:23:43 +00:00
|
|
|
# warning-ignore:unused_argument
|
2019-11-19 01:52:04 +00:00
|
|
|
func _process(delta) -> void:
|
|
|
|
update()
|
|
|
|
|
2019-11-20 12:41:02 +00:00
|
|
|
#Code taken and modified from Godot's source code
|
2019-11-19 01:52:04 +00:00
|
|
|
func _draw() -> void:
|
|
|
|
var transform := Transform2D()
|
|
|
|
var ruler_transform := Transform2D()
|
|
|
|
var major_subdivide := Transform2D()
|
|
|
|
var minor_subdivide := Transform2D()
|
|
|
|
var zoom := 1 / Global.camera.zoom.x
|
|
|
|
transform.y = Vector2(zoom, zoom)
|
|
|
|
|
2019-11-20 22:11:21 +00:00
|
|
|
transform.origin = Global.main_viewport.rect_size / 2 + Global.camera.offset * -zoom
|
2019-11-19 01:52:04 +00:00
|
|
|
|
|
|
|
var basic_rule := 100.0
|
|
|
|
var i := 0
|
|
|
|
while(basic_rule * zoom > 100):
|
|
|
|
basic_rule /= 5.0 if i % 2 else 2.0
|
|
|
|
i += 1
|
|
|
|
i = 0
|
|
|
|
while(basic_rule * zoom < 100):
|
|
|
|
basic_rule *= 2.0 if i % 2 else 5.0
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
ruler_transform = ruler_transform.scaled(Vector2(basic_rule, basic_rule))
|
|
|
|
|
|
|
|
major_subdivide = major_subdivide.scaled(Vector2(1.0 / major_subdivision, 1.0 / major_subdivision))
|
|
|
|
minor_subdivide = minor_subdivide.scaled(Vector2(1.0 / minor_subdivision, 1.0 / minor_subdivision))
|
|
|
|
|
2019-11-20 12:41:02 +00:00
|
|
|
first = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Vector2.ZERO)
|
|
|
|
last = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Global.main_viewport.rect_size)
|
2019-11-19 01:52:04 +00:00
|
|
|
|
|
|
|
for i in range(ceil(first.y), last.y):
|
|
|
|
var position : Vector2 = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Vector2(0, i))
|
|
|
|
if i % (major_subdivision * minor_subdivision) == 0:
|
|
|
|
draw_line(Vector2(0, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
|
2019-11-20 12:41:02 +00:00
|
|
|
var val = (ruler_transform * major_subdivide * minor_subdivide).xform(Vector2(0, i)).y
|
|
|
|
draw_string(font, Vector2(0, position.y - 2), str(int(val)))
|
2019-11-19 01:52:04 +00:00
|
|
|
else:
|
|
|
|
if i % minor_subdivision == 0:
|
|
|
|
draw_line(Vector2(RULER_WIDTH * 0.33, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
|
|
|
|
else:
|
2019-11-20 12:42:52 +00:00
|
|
|
draw_line(Vector2(RULER_WIDTH * 0.66, position.y), Vector2(RULER_WIDTH, position.y), Color.white)
|
2019-11-20 22:11:21 +00:00
|
|
|
|
|
|
|
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
|