Skip to content

Commit

Permalink
speech connector
Browse files Browse the repository at this point in the history
  • Loading branch information
IntangibleMatter committed Jun 9, 2023
1 parent a9005b0 commit 5aac1d7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
8 changes: 5 additions & 3 deletions addons/IntCut/IntCutUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ class_name IntCutUtils
## Translate a world position to a screen position
func world_to_screen(world_coordinates: Vector2) -> Vector2:
var cam := get_viewport().get_camera_2d()
return world_coordinates - cam.global_position
return world_coordinates - cam.global_position - cam.offset

## get the top center point of an actor
func get_actor_top_center(actor: Node2D) -> Vector2:
var spr := get_actor_rect(actor)
return spr.position + Vector2(spr.size.x/2, 0)
return spr.position + Vector2(0, -spr.size.y/2)

## get an actor's rect
func get_actor_rect(actor: Node2D) -> Rect2:
var spr := actor.get_node("$Sprite2D") as Sprite2D
var spr := actor.get_node("Sprite2D") as Sprite2D
if !is_instance_valid(spr):
return Rect2()
var r := spr.get_rect()
return Rect2(spr.to_global(r.position), r.size)

Expand Down
43 changes: 29 additions & 14 deletions addons/IntCut/display/dialoguebubble/dialogue_bubble.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var bubble_points_base : PackedVector2Array
var bubble_points : PackedVector2Array # Displayed points. Updates.
## Colour of the dialogue bubble.
var bubble_colour := Color.BLACK
## Points used to draw the line between the speech bubble and the actor
var speech_line_points : PackedVector2Array


## The template used to determine how the corners look.
Expand Down Expand Up @@ -66,7 +68,7 @@ var corner_points_template : Array[PackedVector2Array] = [
@export var pos_flag : POS_FLAGS

## Speed at which bubble oscilates. Higher number is slower.
@export_range(1, 1000) var bubble_oscilate_speed : float = 500
@export_range(1, 1000) var bubble_oscilate_speed : float = 700
## Factor the corners will be scaled by.
@export var bubble_corner_size := 32
## Factor the corners of the bubble will oscilate by. No set maximum, as
Expand All @@ -75,8 +77,11 @@ var corner_points_template : Array[PackedVector2Array] = [
@export var bubble_point_move_scale := 2
## Additional padding between edges of RichTextLabel and the edges of the bubble.
@export var bubble_padding := 1

## Sets the maximum horizontal scale of the dialogue bubble.
@export_range(0.2, 1) var maximum_box_size : float = 0.9
## Sets the maximum width of the speech bubble connector
@export var maximum_speech_connector_width : float = 30


@onready var rich_text_label = $RichTextLabel
@onready var icutils := IntCutUtils.new()
Expand All @@ -85,7 +90,7 @@ func _ready() -> void:
bubble_rect = Rect2(calculate_bubble_location(), Vector2.ZERO)
calculate_bubble_points(bubble_rect)
rich_text_label.text = "[center]" + text[0].replace("\\n", "\n")
waittt()
scale_dialogue_box()

func _process(delta: float) -> void:
# prints("template", corner_points_template)
Expand All @@ -97,12 +102,6 @@ func _process(delta: float) -> void:
queue_redraw()


## Debug function. Please ignore.
func waittt() -> void:
await get_tree().create_timer(4)
print("GOOO")
scale_dialogue_box()

## Scales the dialogue box to the new text size.
func scale_dialogue_box() -> void:
scaling = true
Expand Down Expand Up @@ -172,20 +171,23 @@ func calculate_bubble_points(rect: Rect2) -> void:

func draw_bubble() -> void:
draw_colored_polygon(bubble_points, bubble_colour)
# draw_polyline(bubble_points, Color.WHITE, 8)
# prints("bubble points", bubble_points)


func draw_speech_line() -> void:
pass
draw_colored_polygon(speech_line_points, bubble_colour)
# draw_polyline(speech_line_points, Color.WHITE, 4)


func _draw() -> void:
if bubble_points.is_empty():
return
# prints("bubble points", bubble_points)
# print("drawing!")
draw_bubble()
draw_speech_line()
draw_bubble()


func offset_bubble_points(rect: Rect2) -> PackedVector2Array:
var temp_points : Array[PackedVector2Array] = []
Expand Down Expand Up @@ -232,7 +234,6 @@ func update_bubble_points() -> void:
# prints("pointttt", bubble_points_base[point], pointsign)
if pointsign.y <= 0:
newpoint.y = bubble_points_base[point].y - pointoffset.y

newpoint.x = bubble_points_base[point].x + pointoffset.x
else:
newpoint.y = bubble_points_base[point].y + pointoffset.y
Expand All @@ -244,6 +245,20 @@ func update_bubble_points() -> void:
# prints("bubble points", bubble_points)


## Changes to the next line of dialogue
## Updates the line between the actor and the dialogue bubble
func update_speech_line() -> void:
pass
var temp_points : PackedVector2Array
var rect_center := bubble_rect.get_center()

temp_points.append(icutils.get_actor_top_center(speaker))
# temp_points.append(get_viewport().get_mouse_position() - get_viewport().get_final_transform().get_origin())

if maximum_speech_connector_width * 2 > bubble_rect.size.x:
temp_points.append(rect_center - Vector2(bubble_rect.size.x/2, 0))
temp_points.append(rect_center + Vector2(bubble_rect.size.x/2, 0))
else:
temp_points.append(rect_center - Vector2(maximum_speech_connector_width, 0))
temp_points.append(rect_center + Vector2(maximum_speech_connector_width, 0))
# prints(get_viewport().get_mouse_position(), temp_points)
speech_line_points = temp_points

4 changes: 0 additions & 4 deletions addons/IntCut/display/dialoguebubble/dialogue_bubble.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(1, 0.239216, 1, 0.427451)

[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(158, 143)
enabled = false
19 changes: 19 additions & 0 deletions testscenes/dialoguebubbletest.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends Node2D

@export_node_path("Node2D") var actor

@onready var dialogue_bubble: Control = $DialogueBubble
@onready var camera_2d: Camera2D = $Camera2D
@onready var test_actor: Node2D = $TestActor

var speed = 400

func _process(delta: float) -> void:
if Input.is_action_pressed("ui_up"):
test_actor.position.y -= speed * delta
elif Input.is_action_pressed("ui_down"):
test_actor.position.y += speed * delta
if Input.is_action_pressed("ui_left"):
test_actor.position.x -= speed * delta
elif Input.is_action_pressed("ui_right"):
test_actor.position.x += speed * delta
26 changes: 20 additions & 6 deletions testscenes/dialoguebubbletest.tscn
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
[gd_scene load_steps=2 format=3 uid="uid://boki72uvxtcvs"]
[gd_scene load_steps=4 format=3 uid="uid://boki72uvxtcvs"]

[ext_resource type="PackedScene" uid="uid://boukjuupuqgnb" path="res://addons/IntCut/display/dialoguebubble/dialogue_bubble.tscn" id="1_4ggul"]
[ext_resource type="Script" path="res://testscenes/dialoguebubbletest.gd" id="1_m1g5f"]
[ext_resource type="Texture2D" uid="uid://tdxc17vshma2" path="res://icon.svg" id="2_xf533"]

[node name="dialoguebubbletest" type="Node2D"]
script = ExtResource("1_m1g5f")
actor = NodePath("TestActor")

[node name="DialogueBubble" parent="." instance=ExtResource("1_4ggul")]
[node name="DialogueBubble" parent="." node_paths=PackedStringArray("speaker") instance=ExtResource("1_4ggul")]
offset_left = 94.0
offset_top = 67.0
offset_right = 94.0
offset_bottom = 67.0
text = PackedStringArray("[wave amp=60] a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a aa a a")
bubble_oscilate_speed = 854.541
bubble_corner_size = 48
bubble_point_move_scale = 5
text = PackedStringArray("How\'s it going")
speaker = NodePath("../TestActor")
bubble_oscilate_speed = 500.0
bubble_point_move_scale = 4

[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(520, 264)

[node name="TestActor" type="Node2D" parent="."]
position = Vector2(112, 288)

[node name="Sprite2D" type="Sprite2D" parent="TestActor"]
texture = ExtResource("2_xf533")
metadata/_edit_lock_ = true

0 comments on commit 5aac1d7

Please sign in to comment.