Skip to content

Commit

Permalink
Clean up resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Jowan-Spooner committed Jan 10, 2024
1 parent c1f85b9 commit d24ad77
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 99 deletions.
32 changes: 16 additions & 16 deletions addons/dialogic/Resources/character.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@ extends Resource
class_name DialogicCharacter


@export var display_name:String = ""
@export var nicknames:Array = []
## Resource that represents a character in dialog.
## Manages/contains portraits, custom info and translation of characters.

@export var color:Color = Color()
@export var description:String = ""
@export var display_name := ""
@export var nicknames := []

@export var scale:float = 1.0
@export var offset:Vector2 = Vector2()
@export var mirror:bool = false
@export var color := Color()
@export var description := ""

@export var default_portrait:String = ""
@export var portraits:Dictionary = {}
@export var scale := 1.0
@export var offset := Vector2()
@export var mirror := false

@export var custom_info:Dictionary = {}
@export var default_portrait := ""
@export var portraits := {}

@export var custom_info := {}

## All valid properties that can be accessed by their translation.
enum TranslatedProperties {
NAME,
NICKNAMES,
}

var _translation_id: String = ""

func __get_property_list() -> Array:
return []
var _translation_id := ""


func _to_string() -> String:
return "[{name}:{id}]".format({"name":get_character_name(), "id":get_instance_id()})

func _hide_script_from_inspector() -> bool:
return true

## This is automatically called, no need to use this.
func add_translation_id() -> String:
Expand All @@ -45,6 +43,7 @@ func add_translation_id() -> String:
func remove_translation_id() -> void:
_translation_id = ""


## Checks [param property] and matches it to a translation key.
##
## Undefined behaviour if an invalid integer is passed.
Expand Down Expand Up @@ -123,6 +122,7 @@ func get_character_name() -> String:
else:
return "UnnamedCharacter"


## Returns the info of the given portrait.
## Uses the default portrait if the given portrait doesn't exist.
func get_portrait_info(portrait_name:String) -> Dictionary:
Expand Down
120 changes: 37 additions & 83 deletions addons/dialogic/Resources/timeline.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,49 @@
extends Resource
class_name DialogicTimeline

## Resource that defines a list of events.
## It can store them as text and load them from text too.

var events:Array = []:
get:
return events
set(value):
emit_changed()
notify_property_list_changed()
events = value

var events_processed:bool = false


func get_event(index):
if index >= len(events):
return null
return events[index]


func _set(property, value):
if str(property).begins_with("event/"):
var event_idx:int = str(property).split("/", true, 2)[1].to_int()
if event_idx < events.size():
events[event_idx] = value
else:
events.insert(event_idx, value)

emit_changed()
notify_property_list_changed()

return false


func _get(property):
if str(property).begins_with("event/"):
var event_idx:int = str(property).split("/", true, 2)[1].to_int()
if event_idx < len(events):
return events[event_idx]
return true


func _init() -> void:
events = []
resource_name = get_class()
var events: Array = []
var events_processed: bool = false


## Method used for printing timeline resources identifiably
func _to_string() -> String:
return "[DialogicTimeline:{file}]".format({"file":resource_path})


func _get_property_list() -> Array:
var p : Array = []
var usage = PROPERTY_USAGE_SCRIPT_VARIABLE
usage |= PROPERTY_USAGE_NO_EDITOR
usage |= PROPERTY_USAGE_EDITOR # Comment this line to hide events from editor
if events != null:
for event_idx in events.size():
p.append(
{
"name":"event/{idx}".format({"idx":event_idx}),
"type":TYPE_OBJECT,
"usage":PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_SCRIPT_VARIABLE
}
)
return p
## Helper method
func get_event(index:int) -> Variant:
if index >= len(events):
return null
return events[index]


## Parses the lines as seperate events and insert them in an array,
## so they can be converted to DialogicEvent's when processed later
func from_text(text:String) -> void:
# Parse the lines as seperate events and insert them in an array, so they can be converted to DialogicEvent's when processed later

events = text.split('\n', true)
events_processed = false


## Stores all events in their text format and returns them as a string
func as_text() -> String:
var result:String = ""
var result: String = ""

if events_processed:
var indent := 0
for idx in range(0, len(events)):
var event = events[idx]

if event['event_name'] == 'End Branch':
if event.event_name == 'End Branch':
indent -= 1
continue

if event != null:
for i in event.empty_lines_above:
result += "\t".repeat(indent)+"\n"
result += "\t".repeat(indent)+event['event_node_as_text'].replace('\n', "\n"+"\t".repeat(indent)) + "\n"
result += "\t".repeat(indent)+event.event_node_as_text.replace('\n', "\n"+"\t".repeat(indent)) + "\n"
if event.can_contain_events:
indent += 1
if indent < 0:
Expand All @@ -104,6 +58,7 @@ func as_text() -> String:
return result.strip_edges()


## Method that loads all the event resources from the strings, if it wasn't done before
func process() -> void:
if typeof(events[0]) == TYPE_STRING:
events_processed = false
Expand All @@ -118,46 +73,46 @@ func process() -> void:
var end_event := DialogicEndBranchEvent.new()

var prev_indent := ""
var _events := []
var processed_events := []

# this is needed to add an end branch event even to empty conditions/choices
var prev_was_opener := false

var lines := events
var idx := -1
var empty_lines = 0
var empty_lines := 0
while idx < len(lines)-1:
idx += 1

# make sure we are using the string version, in case this was already converted
var line: String = ""
var line := ""
if typeof(lines[idx]) == TYPE_STRING:
line = lines[idx]
else:
line = lines[idx]['event_node_as_text']
line = lines[idx].event_node_as_text

# ignore empty lines, but record them in @empty_lines
## Ignore empty lines, but record them in @empty_lines
var line_stripped :String = line.strip_edges(true, false)
if line_stripped.is_empty():
empty_lines += 1
continue


## Add an end event if the indent is smaller then previously
var indent :String= line.substr(0,len(line)-len(line_stripped))
var indent: String = line.substr(0,len(line)-len(line_stripped))
if len(indent) < len(prev_indent):
for i in range(len(prev_indent)-len(indent)):
_events.append(end_event.duplicate())
# Add an end event if the indent is the same but the previous was an opener
# (so for example choice that is empty)
processed_events.append(end_event.duplicate())
## Add an end event if the indent is the same but the previous was an opener
## (so for example choice that is empty)
if prev_was_opener and len(indent) <= len(prev_indent):
_events.append(end_event.duplicate())
processed_events.append(end_event.duplicate())

prev_indent = indent

## Now we process the event into a resource
## by checking on each event if it recognizes this string
var event_content :String = line_stripped
var event :Variant
var event_content: String = line_stripped
var event: DialogicEvent
for i in event_cache:
if i._test_event_string(event_content):
event = i.duplicate()
Expand All @@ -169,9 +124,9 @@ func process() -> void:
idx += 1
if idx == len(lines):
break
var following_line :String = lines[idx]
var following_line_stripped :String = following_line.strip_edges(true, false)
var following_line_indent :String = following_line.substr(0,len(following_line)-len(following_line_stripped))
var following_line: String = lines[idx]
var following_line_stripped: String = following_line.strip_edges(true, false)
var following_line_indent: String = following_line.substr(0,len(following_line)-len(following_line_stripped))
if following_line_stripped.is_empty():
break
if following_line_indent != indent:
Expand All @@ -180,16 +135,15 @@ func process() -> void:
event_content += "\n"+following_line_stripped

event._load_from_string(event_content)
event['event_node_as_text'] = event_content
event.event_node_as_text = event_content

_events.append(event)
processed_events.append(event)
prev_was_opener = event.can_contain_events
empty_lines = 0


if !prev_indent.is_empty():
for i in range(len(prev_indent)):
_events.append(end_event.duplicate())
processed_events.append(end_event.duplicate())

events = _events
events = processed_events
events_processed = true

0 comments on commit d24ad77

Please sign in to comment.