oh-my-git/node.gd

125 lines
2.9 KiB
GDScript3
Raw Normal View History

2020-03-18 16:20:55 +01:00
extends Node2D
var id setget id_set
2020-03-18 16:28:48 +01:00
var content setget content_set
2020-03-18 16:20:55 +01:00
var type setget type_set
var repository: Control
2020-03-18 16:20:55 +01:00
onready var content_label = $Content/ContentLabel
2020-03-18 20:03:17 +01:00
var children = {} setget 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://arrow.tscn")
func _ready():
content_set(content)
2020-09-24 10:52:58 +02:00
$Pop.pitch_scale = rand_range(0.8, 1.2)
$Pop.play()
2020-03-18 16:20:55 +01:00
2020-09-08 20:26:14 +02: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:
apply_forces()
func apply_forces():
2020-09-14 14:54:30 +02:00
var offset = Vector2(0, 80)
2020-09-14 16:03:01 +02:00
for c in children.keys():
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.03)
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
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 == "commit":
$FileBrowser.commit = self
$FileBrowser.title = "Commit"
if type != "ref":
$ID.text = $ID.text.substr(0,8)
2020-09-13 21:55:24 +02:00
#elif type == "ref":
#$ID.text = $ID.text.replace("refs/", "")
match new_type:
"blob":
$Sprite.texture = preload("res://nodes/blob.svg")
#$Rect.color = Color("#333333")
"tree":
$Sprite.texture = preload("res://nodes/tree.svg")
#$Rect.color = Color.darkgreen
"commit":
$Sprite.texture = preload("res://nodes/commit.svg")
#$Rect.color = Color.orange
"tag":
$Sprite.texture = preload("res://nodes/blob.svg")
#$Rect.color = Color.blue
"ref":
$Sprite.texture = preload("res://nodes/ref.svg")
#$Rect.color = Color("#6680ff")
2020-09-01 21:25:24 +02:00
id_always_visible = true
"head":
$Sprite.texture = preload("res://nodes/ref.svg")
#$Rect.color = Color.red
2020-09-01 21:25:24 +02:00
id_always_visible = true
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):
var a = arrow.instance()
a.label = new_children[c]
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:
content_label.visible = true
$ID.visible = true
2020-03-18 16:28:48 +01:00
func _on_unhover():
hovered = false
2020-09-01 21:25:24 +02:00
if not id_always_visible:
content_label.visible = false
2020-09-01 21:25:24 +02:00
$ID.visible = false
func _input(event):
if hovered:
if event.is_action_pressed("click"):
held = true
if type == "commit":
$FileBrowser.visible = not $FileBrowser.visible
elif event.is_action_pressed("right_click"):
var input = get_tree().get_current_scene().find_node("Input")
input.text += id
input.caret_position = input.text.length()
if event.is_action_released("click"):
held = false