One_Eleven_Android/addons/dialogic/Nodes/History.gd
2024-11-10 03:34:28 +03:00

378 lines
11 KiB
GDScript

tool
extends Control
export (PackedScene) var HistoryRow = load("res://addons/dialogic/Example Assets/History/HistoryRow.tscn")
export (PackedScene) var HistoryDefaultBackground = load("res://addons/dialogic/Example Assets/History/HistoryBackground.tscn")
export (PackedScene) var HistoryOpenButton = load("res://addons/dialogic/Example Assets/History/HistoryButton.tscn")
export (PackedScene) var HistoryCloseButton = load("res://addons/dialogic/Example Assets/History/HistoryButton.tscn")
export (int) var Vertical_Separation = 16
onready var HistoryTimeline = $HistoryPopup / ScrollHistoryContainer / MarginContainer / HistoryTimeline
onready var scrollbar = $HistoryPopup / ScrollHistoryContainer.get_v_scrollbar()
onready var ScrollHistoryContainer = $HistoryPopup / ScrollHistoryContainer
onready var HistoryPopup = $HistoryPopup
onready var HistoryAudio = $HistoryPopup / HistoryAudio
var HistoryButton
var CloseButton
var HistoryBackground
var is_history_open = false
var is_mouse_on_button = false
var block_dialog_advance = false setget , history_advance_block
var lastQuestionNode = null
var curTheme = null
var prevState
var eventsToLog = ["dialogic_001", "dialogic_010"]
var logArrivals = false
var logExits = false
var scrollToBottom = true
var reverseTimeline = false
var characterNameColorOn = true
var lineBreakAfterName = true
var scrollToggle = false
func _ready():
var testHistoryRow = HistoryRow.instance()
assert (testHistoryRow.has_method("add_history"), "HistoryRow Scene must implement add_history(string, string) method.")
testHistoryRow.queue_free()
HistoryBackground = HistoryDefaultBackground.instance()
HistoryPopup.add_child(HistoryBackground)
HistoryPopup.move_child(HistoryBackground, 0)
if scrollbar.is_connected("changed", self, "handle_scrollbar_changed"):
scrollbar.connect("changed", self, "handle_scrollbar_changed")
func handle_scrollbar_changed():
if (scrollToggle):
scrollToggle = false
if (scrollToBottom):
ScrollHistoryContainer.scroll_vertical = scrollbar.max_value
else :
ScrollHistoryContainer.scroll_vertical = 0
func initalize_history():
if get_parent().settings.get_value("history", "enable_open_button", true):
HistoryButton = HistoryOpenButton.instance()
if get_parent().settings.get_value("history", "enable_close_button", true):
CloseButton = HistoryCloseButton.instance()
logArrivals = get_parent().settings.get_value("history", "log_arrivals", true)
logExits = get_parent().settings.get_value("history", "log_exits", true)
if logExits or logArrivals:
eventsToLog.push_back("dialogic_002")
scrollToBottom = get_parent().settings.get_value("history", "history_scroll_to_bottom", true)
reverseTimeline = get_parent().settings.get_value("history", "history_reverse_timeline", false)
characterNameColorOn = get_parent().settings.get_value("history", "history_name_color_on", true)
lineBreakAfterName = get_parent().settings.get_value("history", "history_break_after_name", false)
var button_anchor = int(get_parent().settings.get_value("history", "history_button_position", 2))
var screen_margin_x = get_parent().settings.get_value("history", "history_screen_margin_x", 0)
var screen_margin_y = get_parent().settings.get_value("history", "history_screen_margin_y", 0)
var container_margin_X = get_parent().settings.get_value("history", "history_container_margin_x", 0)
var container_margin_y = get_parent().settings.get_value("history", "history_container_margin_y", 0)
HistoryPopup.margin_left = screen_margin_x
HistoryPopup.margin_right = - screen_margin_x
HistoryPopup.margin_top = screen_margin_y
HistoryPopup.margin_bottom = - screen_margin_y - 50
ScrollHistoryContainer.margin_left = container_margin_X
ScrollHistoryContainer.margin_right = - container_margin_X
ScrollHistoryContainer.margin_top = container_margin_y
ScrollHistoryContainer.margin_bottom = - container_margin_y
for button in [HistoryButton, CloseButton]:
if button == null:
continue
continue;
var reference = button.get_parent().rect_size
button.connect("focus_entered", get_parent(), "_on_option_hovered", [button])
button.connect("mouse_entered", get_parent(), "_on_option_focused")
var anchor_values = [0, 0, 1, 1]
var position_offset = Vector2(0, 0)
if button_anchor == 0:
anchor_values = [0, 0, 0, 0]
position_offset.x = 0
position_offset.y = 0
elif button_anchor == 1:
anchor_values = [0.5, 0, 0.5, 0]
position_offset.x = reference.x / 2 - button.rect_size.x
position_offset.y = 0
elif button_anchor == 2:
anchor_values = [1, 0, 1, 0]
position_offset.x = reference.x - button.rect_size.x
position_offset.y = 0
elif button_anchor == 4:
anchor_values = [0, 0.5, 0, 0.5]
position_offset.x = 0
position_offset.y = reference.y / 2 - button.rect_size.y
elif button_anchor == 5:
anchor_values = [0.5, 0.5, 0.5, 0.5]
position_offset.x = reference.x / 2 - button.rect_size.x
position_offset.y = reference.y / 2 - button.rect_size.y
elif button_anchor == 6:
anchor_values = [1, 0.5, 1, 0.5]
position_offset.x = reference.x - button.rect_size.x
position_offset.y = reference.y / 2 - button.rect_size.y
elif button_anchor == 8:
anchor_values = [0, 1, 0, 1]
position_offset.x = 0
position_offset.y = reference.y - button.rect_size.y
elif button_anchor == 9:
anchor_values = [0.5, 1, 0.5, 1]
position_offset.x = reference.x / 2 - button.rect_size.x
position_offset.y = reference.y - button.rect_size.y
elif button_anchor == 10:
anchor_values = [1, 1, 1, 1]
position_offset.x = reference.x - button.rect_size.x
position_offset.y = reference.y - button.rect_size.y
button.anchor_left = anchor_values[0]
button.anchor_top = anchor_values[1]
button.anchor_right = anchor_values[2]
button.anchor_bottom = anchor_values[3]
button.rect_global_position = button.get_parent().rect_global_position + position_offset
func add_history_row_event(eventData, DialogNode, historyContainer):
if not eventsToLog.has(eventData.event_id) or (eventData.event_id == "dialogic_002" and eventData.get("type") == 2):
return
if eventData.event_id == "dialogic_002":
return
var newHistoryRow = HistoryRow.instance()
HistoryTimeline.add_child(newHistoryRow)
if (reverseTimeline):
HistoryTimeline.move_child(newHistoryRow, 0)
if newHistoryRow.has_method("load_theme") and get_parent().settings.get_value("history", "enable_dynamic_theme", false) == true:
newHistoryRow.load_theme(curTheme)
if not SettingsSingleton.GetDefaultTheme():
var textColor = Color(SettingsSingleton.GetTextColor());
newHistoryRow.get_node("HBoxContainer/RichTextLabel").set("custom_colors/default_color", textColor);
var characterPrefix = ""
if eventData.has("character") and eventData.character != "":
var characterData = DialogicUtil.get_character(eventData.character)
var characterName = characterData.get("name", "")
if eventData.has("character") and eventData.character == "[All]":
characterPrefix = str("Everyone")
elif characterData.data.get("display_name_bool", false) == true:
characterName = characterData.data.get("display_name", "")
if characterName != "":
var charDelimiter = get_parent().settings.get_value("history", "history_character_delimiter", "")
var parsed_name = DialogicParser.parse_definitions(get_parent(), characterName, true, false)
var charNameTr = LanguageLocalization.GetDialogicNames();
parsed_name = tr(charNameTr[parsed_name]);
var characterColor = characterData.data.get("color", Color.white)
if ( not characterNameColorOn):
characterColor = Color.white
if characterColor == "#ff0f0a0a":
characterColor = "#ff8f8f8f"
var lineBreak = ""
if (lineBreakAfterName):
lineBreak = "\n"
characterPrefix = str("[color=", characterColor, "]", parsed_name, "[/color]", charDelimiter, " ", lineBreak)
var audioData = ""
if eventData.event_id == "dialogic_001":
var parsedDefinitions = DialogicParser._insert_variable_definitions(DialogNode, eventData.text)
if parsedDefinitions != "":
if not AlreadyInHistory(parsedDefinitions, historyContainer):
newHistoryRow.add_history(str(characterPrefix, parsedDefinitions), audioData)
else :
HistoryTimeline.remove_child(newHistoryRow);
elif eventData.event_id == "dialogic_002":
return ;
elif eventData.event_id == "dialogic_010":
var choiceString = ""
if eventData.question != "":
eventData.question = tr(eventData.question);
newHistoryRow.add_history(str(characterPrefix, eventData.question), audioData)
choiceString += "\n";
if eventData.has("options") and get_parent().settings.get_value("history", "log_choices", true):
choiceString += " "
for choice in eventData["options"]:
if ShouldAddChoice(choice, DialogNode):
choiceString = str(choiceString, "[", choice.label, "] ")
newHistoryRow.add_history(choiceString, audioData)
lastQuestionNode = newHistoryRow
func ShouldAddChoice(option:Dictionary, DialogNode):
if not option["definition"].empty():
var def_value = null
for d in DialogNode.definitions["variables"]:
if d["id"] == option["definition"]:
def_value = d["value"]
return def_value != null and DialogicUtil.compare_definitions(def_value, option["value"], option["condition"]);
else :
return true
func AlreadyInHistory(text:String, historyContainer):
for i in historyContainer.get_children():
var lol = i.get_node("HBoxContainer/RichTextLabel").text;
if lol == text:
return true;
return false;
func add_answer_to_question(stringData):
if lastQuestionNode != null:
lastQuestionNode.add_history(str("\n ", stringData), lastQuestionNode.audioPath)
lastQuestionNode = null
func change_theme(newTheme:ConfigFile):
if get_parent().settings.get_value("history", "enable_dynamic_theme", false):
curTheme = newTheme
func load_theme(theme:ConfigFile):
curTheme = theme
func _on_audio_trigger(audioFilepath):
HistoryAudio.stream = load(audioFilepath)
HistoryAudio.play()
func _on_HistoryPopup_popup_hide():
HistoryAudio.stop()
func _on_HistoryPopup_about_to_show():
if HistoryButton != null:
scrollToggle = true
HistoryButton.hide()
func _on_HistoryButton_mouse_entered():
is_mouse_on_button = true
func _on_HistoryButton_mouse_exited():
is_mouse_on_button = false
func history_advance_block()->bool:
return is_mouse_on_button or is_history_open
func _on_toggle_history():
var textColor = Color.white
var backColor = Color.black
var backNode = get_node("HistoryPopup/ScrollHistoryContainer/MarginContainer/HistoryTimeline")
if SettingsSingleton.GetDefaultTheme():
for i in backNode.get_children():
i.get_child(1).texture = load("res://addons/dialogic/Example Assets/backgrounds/background-2.png")
i.get_child(1).modulate = Color.white
else :
textColor = SettingsSingleton.GetTextColor()
backColor = SettingsSingleton.GetBackgroundColor()
for i in backNode.get_children():
i.get_child(1).texture = load("res://addons/dialogic/Example Assets/backgrounds/background-white.png")
i.get_child(1).modulate = backColor
if HistoryPopup.visible == false:
_on_HistoryPopup_about_to_show()
HistoryPopup.show()
$HistoryButton.show();
is_history_open = true
is_mouse_on_button = false
else :
_on_HistoryPopup_popup_hide()
HistoryPopup.hide()
$HistoryButton.hide();
is_history_open = false
is_mouse_on_button = false