oh-my-git/scenes/node.gd

136 lines
3.4 KiB
GDScript3
Raw Permalink Normal View History

2020-03-18 16:20:55 +01:00
extends Node2D
2023-09-06 16:04:23 +02:00
var id : set = id_set
var content : set = content_set
var type : set = type_set
var repository: Control
2020-03-18 16:20:55 +01:00
2023-09-06 16:04:23 +02:00
@onready var content_label = $Content/ContentLabel
2023-09-06 16:04:23 +02:00
var children = {}: set = children_set
2020-09-01 21:25:24 +02:00
var id_always_visible = false
var held = false
var hovered = false
2020-03-18 16:20:55 +01:00
var arrow = preload("res://scenes/arrow.tscn")
2020-03-18 16:20:55 +01:00
func _ready():
content_set(content)
2020-10-23 16:52:03 +02:00
type_set(type)
id_set(id)
if not repository.simplified_view or (type != "tree" and type != "blob"):
2023-09-06 16:04:23 +02:00
$Pop.pitch_scale = randf_range(0.8, 1.2)
2020-10-27 11:38:04 +01:00
$Pop.play()
2020-03-18 16:20:55 +01:00
func _process(delta):
if held:
if not Input.is_action_pressed("click"):
held = false
else:
global_position = get_global_mouse_position()
2020-09-14 16:03:01 +02:00
if visible:
if type == "head":
for c in children:
if repository.objects.has(c):
var other = repository.objects[c]
var offset = Vector2(0, -45)
var target_position = other.position + offset
position = lerp(position, target_position, 10*delta)
else:
apply_forces()
2020-09-14 16:03:01 +02:00
func apply_forces():
var offset = Vector2(-80, 0)
2020-09-14 16:03:01 +02:00
for c in children.keys():
# if type == "ref" or type == "head":
# offset = Vector2(0, 80)
2020-09-15 18:41:06 +02:00
if repository.objects.has(c):
var other = repository.objects[c]
2020-09-14 16:03:01 +02:00
if other.visible:
var d = other.position.distance_to(position+offset)
var dir = (other.position - (position+offset)).normalized()
var f = (d*0.06)
2020-09-14 16:03:01 +02:00
position += dir*f
other.position -= dir*f
2020-03-18 16:20:55 +01:00
func id_set(new_id):
id = new_id
$ID.text = id
if type == "ref":
$ID.text = $ID.text.replace("refs/heads/", "")
$ID.text = $ID.text.replace("refs/remotes/", "")
$ID.text = $ID.text.replace("refs/tags/", "")
2020-03-18 16:28:48 +01:00
func content_set(new_content):
content = new_content
if content_label:
content_label.text = content
2020-03-18 16:20:55 +01:00
func type_set(new_type):
type = new_type
if type != "ref":
$ID.text = $ID.text.substr(0,8)
2020-10-29 16:17:04 +01:00
z_index = -1
match new_type:
"blob":
2023-09-06 16:04:23 +02:00
$Sprite2D.texture = preload("res://nodes/blob.svg")
"tree":
2023-09-06 16:04:23 +02:00
$Sprite2D.texture = preload("res://nodes/tree.svg")
"commit":
2023-09-06 16:04:23 +02:00
$Sprite2D.texture = preload("res://nodes/commit.svg")
game.notify("You can drag these around with your mouse!", self, "drag-nodes")
"tag":
2023-09-06 16:04:23 +02:00
$Sprite2D.texture = preload("res://nodes/blob.svg")
"ref":
2023-09-06 16:04:23 +02:00
$Sprite2D.texture = preload("res://nodes/ref.svg")
2020-09-01 21:25:24 +02:00
id_always_visible = true
"head":
2023-09-06 16:04:23 +02:00
$Sprite2D.texture = preload("res://nodes/head3.svg")
2020-10-27 13:11:15 +01:00
id_always_visible = false
2020-10-29 16:17:04 +01:00
z_index = 0
2020-09-01 21:25:24 +02:00
if id_always_visible:
$ID.show()
2020-03-18 16:20:55 +01:00
func children_set(new_children):
for c in $Arrows.get_children():
if not new_children.has(c.target):
c.queue_free()
for c in new_children:
if not children.has(c):
2023-09-06 16:04:23 +02:00
var a = arrow.instantiate()
if type == "commit":
a.source = c
a.target = id
a.color = Color("c2bf26")
else:
a.source = id
a.target = c
2020-09-01 17:24:21 +02:00
a.repository = repository
$Arrows.add_child(a)
children = new_children
2020-03-18 16:28:48 +01:00
func _on_hover():
hovered = true
if not id_always_visible and type != "head":
content_label.visible = true
#$ID.visible = true
2020-03-18 16:28:48 +01:00
func _on_unhover():
hovered = false
if not id_always_visible and type != "head":
content_label.visible = false
#$ID.visible = false
func _input(event):
if hovered:
2020-10-29 16:07:22 +01:00
if event.is_action_pressed("click") and type != "head":
held = true
elif event.is_action_pressed("right_click"):
2023-09-06 16:04:23 +02:00
var input = get_tree().get_current_scene().find_child("Input")
input.text += id
2023-09-06 16:04:23 +02:00
input.caret_column = input.text.length()
if event.is_action_released("click"):
held = false