// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using System.Collections.Generic; namespace PixelCrushers.DialogueSystem { /// /// Assets are composed primarily of data elements called fields. This class represents a /// field, which is a \ tuple such as \. This class /// also contains several static utility functions to work with fields. /// [System.Serializable] public class Field { /// /// The title of the field, such as Name or Age. /// public string title = null; /// /// The value of the field, such as Fred or 42. /// public string value = null; /// /// The data type of the field, such as Text or Number. /// public FieldType type = FieldType.Text; /// /// The name of a field drawer class. /// public string typeString = string.Empty; /// /// Initializes a new Field. /// public Field() { } /// /// Initializes a new Field copied from a Chat Mapper field. /// /// /// The Chat Mapper field to copy. /// public Field(ChatMapper.Field chatMapperField) { Assign(chatMapperField); } /// /// The list of fields that use filenames. The values of these fields should replace /// backslashes with forward slashes. /// private static readonly List filenameFields = new List { DialogueSystemFields.Pictures, DialogueSystemFields.TextureFiles, DialogueSystemFields.ModelFiles, DialogueSystemFields.AudioFiles, DialogueSystemFields.LipsyncFiles, DialogueSystemFields.AnimationFiles }; /// /// Initializes a new Field. /// /// /// Title of the field. /// /// /// Value of the field. /// /// /// Field type. /// public Field(string title, string value, FieldType type) { this.title = title; this.value = (filenameFields.Contains(title) && (value != null)) ? value.Replace('\\', '/') : value; this.type = type; this.typeString = GetTypeString(type); } /// /// Initializes a new Field. /// /// Title of the field. /// Value of the field. /// Field type. /// Custom type string. public Field(string title, string value, FieldType type, string typeString) { this.title = title; this.value = (filenameFields.Contains(title) && (value != null)) ? value.Replace('\\', '/') : value; this.type = type; this.typeString = typeString; } /// /// Copy constructor. /// /// Source field. public Field(Field sourceField) { this.title = sourceField.title; this.value = sourceField.value; this.type = sourceField.type; this.typeString = sourceField.typeString; } /// /// Copies the contents of a Chat Mapper field. /// /// /// The Chat Mapper field to copy. /// public void Assign(ChatMapper.Field chatMapperField) { if (chatMapperField != null) { title = chatMapperField.Title; value = (filenameFields.Contains(title) && (chatMapperField.Value != null)) ? chatMapperField.Value.Replace('\\', '/') : chatMapperField.Value; type = StringToFieldType(chatMapperField.Type); typeString = GetTypeString(type); } } /// /// A static utility method that converts a Chat Mapper type string into a FieldType. /// /// /// The Chat Mapper type string. /// /// /// The field type associated with the Chat Mapper type string. /// public static FieldType StringToFieldType(string chatMapperType) { if (string.Equals(chatMapperType, "Text")) return FieldType.Text; if (string.Equals(chatMapperType, "Number")) return FieldType.Number; if (string.Equals(chatMapperType, "Boolean")) return FieldType.Boolean; if (string.Equals(chatMapperType, "Files")) return FieldType.Files; if (string.Equals(chatMapperType, "Localization")) return FieldType.Localization; if (string.Equals(chatMapperType, "Actor")) return FieldType.Actor; if (string.Equals(chatMapperType, "Item")) return FieldType.Item; if (string.Equals(chatMapperType, "Location")) return FieldType.Location; if (string.Equals(chatMapperType, "Multiline")) return FieldType.Text; if (DialogueDebug.logWarnings) Debug.LogError(string.Format("{0}: Unrecognized Chat Mapper type: {1}", new System.Object[] { DialogueDebug.Prefix, chatMapperType })); return FieldType.Text; } /// /// A static utility method that creates a list of fields from a list of Chat Mapper fields. /// /// /// The Chat Mapper fields. /// /// /// A list of fields copied from the Chat Mapper fields. /// public static List CreateListFromChatMapperFields(List chatMapperFields) { List list = new List(); if (chatMapperFields != null) { foreach (var chatMapperField in chatMapperFields) { list.Add(new Field(chatMapperField)); } } return list; } /// /// Copies a field list. /// /// A new copy of the field list. /// Source fields. public static List CopyFields(List sourceFields) { List fields = new List(); foreach (var sourceField in sourceFields) { fields.Add(new Field(sourceField)); } return fields; } /// /// A static utility method that checks whether a field exists in a list of fields. /// /// /// true if the field exists; otherwise false. /// /// /// A list of fields. /// /// /// Title of the field. /// public static bool FieldExists(List fields, string title) { return Lookup(fields, title) != null; } /// /// A static utility method that looks up a field by title in a list of fields. /// /// /// A list of fields. /// /// /// Title of the field. /// /// /// The first field that matches the title, or null if no field matches. /// public static Field Lookup(List fields, string title) { return (fields == null) ? null : fields.Find(f => string.Equals(f.title, title)); } /// /// A static utility method that looks up a field in a list and returns its string value. /// /// /// A list of fields. /// /// /// Title of the field. /// /// /// The value of the field, or null if the field doesn't exist in the list. /// public static string LookupValue(List fields, string title) { Field field = Lookup(fields, title); return (field == null) ? null : field.value; } /// /// A static utility method that looks up a localized version of a field and /// returns its string value. Given a title, this method looks for a field appended /// with a blank space character or underscore and then the current language code. /// /// The localized value. /// A list of fields. /// The title of the field. public static string LookupLocalizedValue(List fields, string title) { if (Localization.isDefaultLanguage) { return LookupValue(fields, title); } else { string value = LookupValue(fields, title + " " + Localization.language); if (!string.IsNullOrEmpty(value)) { return value; } else { value = LookupValue(fields, title + "_" + Localization.language); if (!string.IsNullOrEmpty(value)) { return value; } else { return LookupValue(fields, title); } } } } /// /// A static utility method that looks up a field in a list and returns its int value. /// /// /// A list of fields. /// /// /// Title of the field. /// /// /// The value of the field, or 0 if the field doesn't exist or isn't an int. /// public static int LookupInt(List fields, string title) { return Tools.StringToInt(LookupValue(fields, title)); } /// /// A static utility method that looks up a field in a list and returns its float value. /// /// /// A list of fields. /// /// /// Title of the field. /// /// /// The value of the field, or 0 if the field doesn't exist or isn't a float. /// public static float LookupFloat(List fields, string title) { return Tools.StringToFloat(LookupValue(fields, title)); } /// /// A static utility method that looks up a field in a list and returns its bool value. /// /// /// A list of fields. /// /// /// Title of the field. /// /// /// The value of the field, or false if the field doesn't exist or isn't a bool. /// public static bool LookupBool(List fields, string title) { return Tools.StringToBool(LookupValue(fields, title)); } /// /// A static utility method that sets the string value of a field. /// /// /// A list of fields. /// /// /// The title of the field to set. /// /// /// The new value of the field. /// /// /// The type of the field. /// public static void SetValue(List fields, string title, string value, FieldType type) { Field field = Lookup(fields, title); if (field != null) { field.value = value; field.type = type; } else { fields.Add(new Field(title, value, type)); } } /// /// A static utility method that sets the string value of a field. /// /// /// A list of fields. /// /// /// The title of the field to set. /// /// /// The new value of the field. /// public static void SetValue(List fields, string title, string value) { SetValue(fields, title, value, FieldType.Text); } /// /// A static utility method that sets the float value of a field. /// /// /// A list of fields. /// /// /// The title of the field to set. /// /// /// The new value of the field. /// public static void SetValue(List fields, string title, float value) { SetValue(fields, title, value.ToString(System.Globalization.CultureInfo.InvariantCulture), FieldType.Number); } /// /// A static utility method that sets the int value of a field. /// /// /// A list of fields. /// /// /// The title of the field to set. /// /// /// The new value of the field. /// public static void SetValue(List fields, string title, int value) { SetValue(fields, title, value.ToString(), FieldType.Number); } /// /// A static utility method that sets the string bool of a field. /// /// /// A list of fields. /// /// /// The title of the field to set. /// /// /// The new value of the field. /// public static void SetValue(List fields, string title, bool value) { SetValue(fields, title, value.ToString(), FieldType.Boolean); } /// /// A static utility method that checks whether a field exists and has non-empty text. /// /// /// true if the field is assigned; otherwise, false. /// /// /// A list of fields. /// /// /// Title of the field. /// public static bool IsFieldAssigned(List fields, string title) { return (AssignedField(fields, title) != null); } /// /// A static utility method that returns a field if it exists and has non-empty text. /// /// /// The field, or null if it doesn't exist or has empty text. /// /// /// A list of fields. /// /// /// Title of the field. /// public static Field AssignedField(List fields, string title) { Field field = Lookup(fields, title); return ((field != null) && !string.IsNullOrEmpty(field.value)) ? field : null; } /// /// Removes the field with the specified title if it exists in the list. /// public static void RemoveField(List fields, string title) { Field field = Lookup(fields, title); if (field != null) fields.Remove(field); } /// /// Returns the value of a field. /// /// /// The value of the field, or null if field is null. /// /// /// The field. /// public static string FieldValue(Field field) { return (field != null) ? field.value : null; } /// /// Returns the localized field title. /// /// /// The localized title. If localization is currently using the default language, then the /// default title is returned. /// /// /// The default title. /// public static string LocalizedTitle(string title) { return Localization.isDefaultLanguage ? title : string.Format("{0} {1}", new System.Object[] { title, Localization.language }); } public static string GetTypeString(FieldType type) { switch (type) { case FieldType.Actor: return "CustomFieldType_Actor"; case FieldType.Boolean: return "CustomFieldType_Boolean"; case FieldType.Files: return "CustomFieldType_Files"; case FieldType.Item: return "CustomFieldType_Item"; case FieldType.Localization: return "CustomFieldType_Localization"; case FieldType.Location: return "CustomFieldType_Location"; case FieldType.Number: return "CustomFieldType_Number"; case FieldType.Text: default: return string.Empty; } } } }