Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve random selection regex and highlighting #2478

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,13 @@ func color_shortcode_content(dict:Dictionary, line:String, from:int = 0, to:int
dict[x.get_start('value')+from-1] = {"color":base_color.lerp(normal_color, 0.7)}
dict[x.get_end()+from] = {"color":normal_color}
return dict


func dict_get_color_at_column(dict:Dictionary, column:int) -> Color:
var prev_idx := -1
for i in dict:
if i > prev_idx and i <= column:
prev_idx = i
if prev_idx != -1:
return dict[prev_idx].color
return normal_color
23 changes: 14 additions & 9 deletions addons/dialogic/Modules/Text/event_text.gd
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,10 @@ var text_effect_color := Color('#898276')
func _get_syntax_highlighting(Highlighter:SyntaxHighlighter, dict:Dictionary, line:String) -> Dictionary:
load_text_effects()
if text_random_word_regex.get_pattern().is_empty():
text_random_word_regex.compile("(?<!\\\\)\\<[^\\[\\>]+(\\/[^\\>]*)\\>")
text_random_word_regex.compile(r"(?<!\\)\<[^\>]+(\/[^\>]*)\>")

var result := regex.search(line)
if !result:
if not result:
return dict
if Highlighter.mode == Highlighter.Modes.FULL_HIGHLIGHTING:
if result.get_string('name'):
Expand All @@ -475,24 +475,29 @@ func _get_syntax_highlighting(Highlighter:SyntaxHighlighter, dict:Dictionary, li
dict[result.get_start('portrait')] = {"color":Highlighter.character_portrait_color}
dict[result.get_end('portrait')] = {"color":Highlighter.normal_color}
if result.get_string('text'):
var effects_result := text_effects_regex.search_all(line)
for eff in effects_result:
dict[eff.get_start()] = {"color":text_effect_color}
dict[eff.get_end()] = {"color":Highlighter.normal_color}
dict = Highlighter.color_region(dict, Highlighter.variable_color, line, '{', '}', result.get_start('text'))

## Color the random selection modifier
for replace_mod_match in text_random_word_regex.search_all(result.get_string('text')):
var color: Color = Highlighter.string_color
color = color.lerp(Highlighter.normal_color, 0.4)
dict[replace_mod_match.get_start()+result.get_start('text')] = {'color':Highlighter.string_color}
var offset := 1
for b in replace_mod_match.get_string().trim_suffix('>').trim_prefix('<').split('/'):
for b:RegExMatch in RegEx.create_from_string(r"(\[[^\]]*\]|[^\/]|\/\/)+").search_all(replace_mod_match.get_string().trim_prefix("<").trim_suffix(">")):
color.h = wrap(color.h+0.2, 0, 1)
dict[replace_mod_match.get_start()+result.get_start('text')+offset] = {'color':color}
offset += len(b)
offset += len(b.get_string())
dict[replace_mod_match.get_start()+result.get_start('text')+offset] = {'color':Highlighter.string_color}
offset += 1
dict[replace_mod_match.get_end()+result.get_start('text')] = {'color':Highlighter.normal_color}

## Color bbcode and text effects
var effects_result := text_effects_regex.search_all(line)
for eff in effects_result:
var prev_color: Color = Highlighter.dict_get_color_at_column(dict, eff.get_start())
dict[eff.get_start()] = {"color":text_effect_color.lerp(prev_color, 0.4)}
dict[eff.get_end()] = {"color":prev_color}
dict = Highlighter.color_region(dict, Highlighter.variable_color, line, '{', '}', result.get_start('text'))

return dict

#endregion
13 changes: 7 additions & 6 deletions addons/dialogic/Modules/Text/subsystem_text.gd
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,15 @@ func effect_mood(_text_node:Control, _skipped:bool, argument:String) -> void:
load(dialogic.current_state_info.speaker).custom_info.get('sound_moods', {}).get(argument, {}))


var modifier_words_select_regex := RegEx.create_from_string(r"(?<!\\)\<[^\[\>]+(\/[^\>]*)\>")
var modifier_select_regex := RegEx.create_from_string(r"(?<!\\)\<[^\>]+(\/[^\>]*)\>")
var modifier_select_split_regex := RegEx.create_from_string(r"(\[[^\]]*\]|[^\/]|\/\/)+")
func modifier_random_selection(text:String) -> String:
for replace_mod_match in modifier_words_select_regex.search_all(text):
for replace_mod_match: RegExMatch in modifier_select_regex.search_all(text):
var string: String = replace_mod_match.get_string().trim_prefix("<").trim_suffix(">")
string = string.replace('//', '<slash>')
var list: PackedStringArray = string.split('/')
var item: String = list[randi()%len(list)]
item = item.replace('<slash>', '/')
var options := []
for split: RegExMatch in modifier_select_split_regex.search_all(string):
options.append(split.get_string())
var item: String = options.pick_random()
text = text.replace(replace_mod_match.get_string(), item.strip_edges())
return text

Expand Down