]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/Jump/event_jump.gd
Adding import files
[wolf-seeking-sheep.git] / addons / dialogic / Modules / Jump / event_jump.gd
1 @tool
2 class_name DialogicJumpEvent
3 extends DialogicEvent
4
5 ## Event that allows starting another timeline. Also can jump to a label in that or the current timeline.
6
7
8 ### Settings
9
10 ## The timeline to jump to, if null then it's the current one. This setting should be a dialogic timeline resource.
11 var timeline: DialogicTimeline
12 ## If not empty, the event will try to find a Label event with this set as name. Empty by default..
13 var label_name := ""
14
15
16 ### Helpers
17
18 ## Used to set the timeline resource from the unique name identifier and vice versa
19 var timeline_identifier := "":
20         get:
21                 if timeline:
22                         var identifier := DialogicResourceUtil.get_unique_identifier(timeline.resource_path)
23                         if not identifier.is_empty():
24                                 return identifier
25                 return timeline_identifier
26         set(value):
27                 timeline_identifier = value
28                 timeline = DialogicResourceUtil.get_timeline_resource(value)
29                 if (not timeline_identifier in DialogicResourceUtil.get_label_cache().keys()
30                                 or not label_name in DialogicResourceUtil.get_label_cache()[timeline_identifier]):
31                         label_name = ""
32                         ui_update_needed.emit()
33
34
35 ################################################################################
36 ##                                              EXECUTION
37 ################################################################################
38
39 func _execute() -> void:
40         dialogic.Jump.push_to_jump_stack()
41         if timeline and timeline != dialogic.current_timeline:
42                 dialogic.Jump.switched_timeline.emit({'previous_timeline':dialogic.current_timeline, 'timeline':timeline, 'label':label_name})
43                 dialogic.start_timeline(timeline, label_name)
44         else:
45                 if label_name:
46                         dialogic.Jump.jump_to_label(label_name)
47                         finish()
48                 else:
49                         dialogic.start_timeline(dialogic.current_timeline)
50
51
52 ################################################################################
53 ##                                              INITIALIZE
54 ################################################################################
55
56 func _init() -> void:
57         event_name = "Jump"
58         set_default_color('Color4')
59         event_category = "Flow"
60         event_sorting_index = 4
61
62
63 func _get_icon() -> Resource:
64         return load(self.get_script().get_path().get_base_dir().path_join('icon_jump.png'))
65
66
67 ################################################################################
68 ##                                              SAVING/LOADING
69 ################################################################################
70 func to_text() -> String:
71         var result := "jump "
72         if timeline_identifier:
73                 result += timeline_identifier+'/'
74                 if label_name:
75                         result += label_name
76         elif label_name:
77                 result += label_name
78         return result
79
80
81 func from_text(string:String) -> void:
82         var result := RegEx.create_from_string(r"jump (?<timeline>.*\/)?(?<label>.*)?").search(string.strip_edges())
83         if result:
84                 timeline_identifier = result.get_string('timeline').trim_suffix('/')
85                 label_name = result.get_string('label')
86
87
88 func is_valid_event(string:String) -> bool:
89         if string.strip_edges().begins_with("jump"):
90                 return true
91         return false
92
93
94 func get_shortcode_parameters() -> Dictionary:
95         return {
96                 #param_name     : property_info
97                 "timeline"              : {"property": "timeline_identifier",   "default": null,
98                                                         "suggestions": get_timeline_suggestions},
99                 "label"                 : {"property": "label_name",            "default": ""},
100         }
101
102
103 ################################################################################
104 ##                                              EDITOR REPRESENTATION
105 ################################################################################
106
107 func build_event_editor() -> void:
108         add_header_edit('timeline_identifier', ValueType.DYNAMIC_OPTIONS, {'left_text':'Jump to',
109                 'file_extension': '.dtl',
110                 'mode'                  : 2,
111                 'suggestions_func': get_timeline_suggestions,
112                 'editor_icon'   : ["TripleBar", "EditorIcons"],
113                 'empty_text'    : '(this timeline)',
114                 'autofocus'             : true,
115                 })
116         add_header_edit("label_name", ValueType.DYNAMIC_OPTIONS, {'left_text':"at",
117                 'empty_text':'the beginning',
118                 'suggestions_func':get_label_suggestions,
119                 'editor_icon':["ArrowRight", "EditorIcons"]})
120
121
122 func get_timeline_suggestions(_filter:String= "") -> Dictionary:
123         var suggestions := {}
124
125         suggestions['(this timeline)'] = {'value':'', 'editor_icon':['GuiRadioUnchecked', 'EditorIcons']}
126         for resource in  DialogicResourceUtil.get_timeline_directory().keys():
127                 suggestions[resource] = {'value': resource, 'tooltip':DialogicResourceUtil.get_timeline_directory()[resource], 'editor_icon': ["TripleBar", "EditorIcons"]}
128         return suggestions
129
130
131 func get_label_suggestions(_filter:String="") -> Dictionary:
132         var suggestions := {}
133         suggestions['at the beginning'] = {'value':'', 'editor_icon':['GuiRadioUnchecked', 'EditorIcons']}
134         if timeline_identifier in DialogicResourceUtil.get_label_cache().keys():
135                 for label in DialogicResourceUtil.get_label_cache()[timeline_identifier]:
136                         suggestions[label] = {'value': label, 'tooltip':label, 'editor_icon': ["ArrowRight", "EditorIcons"]}
137         return suggestions
138
139
140 ####################### CODE COMPLETION ########################################
141 ################################################################################
142
143 func _get_code_completion(CodeCompletionHelper:Node, TextNode:TextEdit, line:String, _word:String, symbol:String) -> void:
144         if symbol == ' ' and line.count(' ') == 1:
145                 CodeCompletionHelper.suggest_labels(TextNode, '', '\n', event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.6))
146                 CodeCompletionHelper.suggest_timelines(TextNode, CodeEdit.KIND_MEMBER, event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.6))
147         if symbol == '/':
148                 CodeCompletionHelper.suggest_labels(TextNode, line.strip_edges().trim_prefix('jump ').trim_suffix('/'+String.chr(0xFFFF)).strip_edges(), '\n', event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.6))
149
150
151 func _get_start_code_completion(_CodeCompletionHelper:Node, TextNode:TextEdit) -> void:
152         TextNode.add_code_completion_option(CodeEdit.KIND_PLAIN_TEXT, 'jump', 'jump ', event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.3))
153
154
155 #################### SYNTAX HIGHLIGHTING #######################################
156 ################################################################################
157
158 func _get_syntax_highlighting(Highlighter:SyntaxHighlighter, dict:Dictionary, line:String) -> Dictionary:
159         dict[line.find('jump')] = {"color":event_color.lerp(Highlighter.normal_color, 0.3)}
160         dict[line.find('jump')+4] = {"color":event_color.lerp(Highlighter.normal_color, 0.5)}
161         return dict