|
| 1 | +@tool |
| 2 | +class_name DialogueSyntaxHighlighter |
| 3 | +extends EditorSyntaxHighlighter |
| 4 | + |
| 5 | +var text_edit : CodeEdit |
| 6 | +var text_edit_initialized := false |
| 7 | + |
| 8 | +const COL : String = "color" |
| 9 | +const TRANSPARENT : Color = Color(0, 0, 0, 0) |
| 10 | + |
| 11 | +var string : String |
| 12 | +var dict : Dictionary = {} |
| 13 | + |
| 14 | +static var actor_name_line : Color |
| 15 | +static var actor_name_line_bg : Color |
| 16 | +static var actor_name_line_bg_2 : Color |
| 17 | +static var base_content : Color |
| 18 | +static var symbol : Color |
| 19 | +static var comment : Color |
| 20 | +static var tag_content : Color |
| 21 | +static var tag_braces : Color |
| 22 | +static var caller : Color |
| 23 | +static var func_name : Color |
| 24 | +static var func_args : Color |
| 25 | +static var section : Color |
| 26 | +static var section_bg : Color |
| 27 | +static var invalid : Color |
| 28 | + |
| 29 | +static var COL_actor_name_line : Dictionary |
| 30 | +static var COL_base_content : Dictionary |
| 31 | +static var COL_symbol : Dictionary |
| 32 | +static var COL_comment : Dictionary |
| 33 | +static var COL_tag_content : Dictionary |
| 34 | +static var COL_tag_braces : Dictionary |
| 35 | +static var COL_caller : Dictionary |
| 36 | +static var COL_func_name : Dictionary |
| 37 | +static var COL_func_args : Dictionary |
| 38 | +static var COL_section : Dictionary |
| 39 | +static var COL_invalid : Dictionary |
| 40 | + |
| 41 | +static var color_initialized := false |
| 42 | + |
| 43 | +static func initialize_colors() -> void: |
| 44 | + var editor_settings : EditorSettings = TheatrePlugin.editor_settings |
| 45 | + |
| 46 | + actor_name_line = editor_settings.get_setting("text_editor/theme/highlighting/base_type_color") |
| 47 | + base_content = editor_settings.get_setting("text_editor/theme/highlighting/text_color") |
| 48 | + symbol = editor_settings.get_setting("text_editor/theme/highlighting/symbol_color") |
| 49 | + comment = editor_settings.get_setting("text_editor/theme/highlighting/comment_color") |
| 50 | + tag_content = editor_settings.get_setting("text_editor/theme/highlighting/user_type_color") |
| 51 | + tag_braces = Color(editor_settings.get_setting("text_editor/theme/highlighting/user_type_color"), 0.65) |
| 52 | + caller = editor_settings.get_setting("text_editor/theme/highlighting/engine_type_color") |
| 53 | + func_name = editor_settings.get_setting("text_editor/theme/highlighting/function_color") |
| 54 | + func_args = editor_settings.get_setting("text_editor/theme/highlighting/string_color") |
| 55 | + section = editor_settings.get_setting("text_editor/theme/highlighting/keyword_color") |
| 56 | + invalid = editor_settings.get_setting("text_editor/theme/highlighting/comment_markers/critical_color") |
| 57 | + |
| 58 | + COL_actor_name_line = {COL: actor_name_line} |
| 59 | + COL_base_content = {COL: base_content} |
| 60 | + COL_symbol = {COL: symbol} |
| 61 | + COL_comment = {COL: comment} |
| 62 | + COL_tag_content = {COL: tag_content} |
| 63 | + COL_tag_braces = {COL: tag_braces} |
| 64 | + COL_caller = {COL: caller} |
| 65 | + COL_func_name = {COL: func_name} |
| 66 | + COL_func_args = {COL: func_args} |
| 67 | + COL_section = {COL: section} |
| 68 | + COL_invalid = {COL: invalid} |
| 69 | + |
| 70 | + actor_name_line_bg = Color(actor_name_line, 0.0825) |
| 71 | + actor_name_line_bg_2 = Color(actor_name_line, 0.04) |
| 72 | + section_bg = Color(section, 0.12) |
| 73 | + |
| 74 | +func initialize_text_edit() -> void: |
| 75 | + if get_text_edit() != null: |
| 76 | + text_edit = get_text_edit() |
| 77 | + text_edit.clear_string_delimiters() |
| 78 | + |
| 79 | +func _init() -> void: |
| 80 | + if !color_initialized: |
| 81 | + initialize_colors() |
| 82 | + color_initialized = true |
| 83 | + |
| 84 | +func _get_name() -> String: |
| 85 | + return "Theatre Dialogue" |
| 86 | + |
| 87 | +func _get_supported_languages() -> PackedStringArray: |
| 88 | + return ["dlg"] |
| 89 | + |
| 90 | +func is_indented(string : String) -> bool: |
| 91 | + return string != string.lstrip(" \t") |
| 92 | + |
| 93 | +func _get_line_syntax_highlighting(line: int) -> Dictionary: |
| 94 | + #initialize_colors() # NOTE: uncomment on development |
| 95 | + if !text_edit_initialized: |
| 96 | + initialize_text_edit() |
| 97 | + text_edit_initialized = true |
| 98 | + |
| 99 | + text_edit.set_line_background_color(line, TRANSPARENT) |
| 100 | + |
| 101 | + string = text_edit.get_line(line) |
| 102 | + |
| 103 | + dict.clear() |
| 104 | + dict[0] = COL_base_content |
| 105 | + |
| 106 | + if is_indented(string): |
| 107 | + var match_func := DialogueParser._regex_func_call.search(string) |
| 108 | + var match_newline_tag := null \ |
| 109 | + if match_func != null else DialogueParser._regex_dlg_tags_newline.search(string) |
| 110 | + |
| 111 | + if match_func != null: |
| 112 | + dict[match_func.get_start("caller")] = COL_caller |
| 113 | + dict[match_func.get_start("name")] = COL_func_name |
| 114 | + dict[match_func.get_start("name") - 1] = COL_symbol |
| 115 | + dict[match_func.get_end("name")] = COL_symbol |
| 116 | + dict[match_func.get_end("name") + 1] = COL_func_args |
| 117 | + dict[match_func.get_end() - 1] = COL_symbol |
| 118 | + dict[match_func.get_end()] = COL_base_content |
| 119 | + |
| 120 | + elif match_newline_tag != null: |
| 121 | + dict[match_newline_tag.get_start("tag")] = COL_tag_content |
| 122 | + dict[match_newline_tag.get_start("arg") - 1] = COL_symbol |
| 123 | + dict[match_newline_tag.get_start("arg")] = COL_tag_content |
| 124 | + |
| 125 | + else: |
| 126 | + for tag in DialogueParser._regex_dlg_tags.search_all(string): |
| 127 | + var START : int = tag.get_start() |
| 128 | + var END : int = tag.get_end() |
| 129 | + |
| 130 | + if !dict.has(START): |
| 131 | + dict[START] = COL_tag_braces |
| 132 | + |
| 133 | + dict[tag.get_start("tag")] = COL_tag_content |
| 134 | + |
| 135 | + if string.contains("="): |
| 136 | + dict[tag.get_start("tag") + 1] = COL_symbol |
| 137 | + dict[tag.get_start("arg")] = COL_tag_content |
| 138 | + |
| 139 | + dict[END - 1] = COL_tag_braces |
| 140 | + |
| 141 | + if !dict.has(END): |
| 142 | + dict[END] = COL_base_content |
| 143 | + |
| 144 | + for bb in DialogueParser._regex_bbcode_tags.search_all(string): |
| 145 | + var START : int = bb.get_start() |
| 146 | + var END : int = bb.get_end() |
| 147 | + |
| 148 | + if !dict.has(START): |
| 149 | + dict[START] = COL_tag_braces |
| 150 | + |
| 151 | + dict[bb.get_start("tag")] = COL_tag_content |
| 152 | + dict[END - 1] = COL_tag_braces |
| 153 | + |
| 154 | + if !dict.has(END): |
| 155 | + dict[END] = COL_base_content |
| 156 | + |
| 157 | + else: |
| 158 | + dict[0] = COL_invalid |
| 159 | + |
| 160 | + if string.begins_with(DialogueParser.HASH): |
| 161 | + dict[0] = COL_comment |
| 162 | + |
| 163 | + else: |
| 164 | + if string.ends_with(DialogueParser.COLON): |
| 165 | + dict[0] = COL_actor_name_line |
| 166 | + dict[string.rfind(DialogueParser.COLON)] = COL_symbol |
| 167 | + text_edit.toggle_foldable_line |
| 168 | + |
| 169 | + text_edit.set_line_background_color(line, actor_name_line_bg) |
| 170 | + if string.strip_edges() == DialogueParser.COLON: |
| 171 | + text_edit.set_line_background_color(line, actor_name_line_bg_2) |
| 172 | + |
| 173 | + elif string.begins_with(DialogueParser.COLON): |
| 174 | + dict[0] = COL_section |
| 175 | + text_edit.set_line_background_color(line, section_bg) |
| 176 | + |
| 177 | + # why does it need to be ordered???? |
| 178 | + # TODO: performance |
| 179 | + var dict_ordered : Dictionary = {} |
| 180 | + |
| 181 | + var dict_keys : PackedInt64Array = dict.keys() |
| 182 | + dict_keys.sort() |
| 183 | + |
| 184 | + for n in dict_keys: |
| 185 | + dict_ordered[n] = dict[n] |
| 186 | + |
| 187 | + return dict_ordered |
0 commit comments