예제 #1
0
    public override void OnInspectorGUI()
    {
        HeathenEngineering.UIX.KeyboardTemplateManager keyboard = target as HeathenEngineering.UIX.KeyboardTemplateManager;

        if (keyboard.workingTemplate == null)
        {
            keyboard.workingTemplate = new HeathenEngineering.UIX.Serialization.KeyboardTemplate()
            {
                TemplateName = "New Template"
            }
        }
        ;

        keyboard.RefreshTemplate();

        EditorGUILayout.LabelField("Template", EditorStyles.boldLabel);
        EditorGUILayout.BeginHorizontal();
        if (drawButton("Save As", 45))
        {
            keyboard.RefreshTemplate();
            string pathTarget = EditorUtility.SaveFilePanel("Save Keyboard Layout", Application.dataPath, keyboard.workingTemplate.TemplateName, "asset");
            if (!string.IsNullOrEmpty(pathTarget))
            {
                try
                {
                    //XmlSerializer serialize = new XmlSerializer(typeof(HeathenEngineering.UIX.Serialization.KeyboardTemplate));
                    //StreamWriter fileStream = new StreamWriter(pathTarget);
                    //serialize.Serialize(fileStream, keyboard.workingTemplate);
                    //fileStream.Close();
                    //fileStream.Dispose();

                    if (!pathTarget.StartsWith(Application.dataPath))
                    {
                        Debug.LogError("Key Layouts must be saved within the project's Assets folder.");
                    }
                    else
                    {
                        pathTarget = pathTarget.Replace(Application.dataPath, "Assets");
                        var fileName = pathTarget.Substring(pathTarget.LastIndexOf("/") + 1);
                        pathTarget = pathTarget.Substring(0, pathTarget.Length - fileName.Length);
                        KeyLayout.SaveLayout(keyboard.keyboard, fileName, pathTarget);
                    }
                }
                catch
                {
                    Debug.LogError("An error occured while attempting to save the keyboard's template data");
                }
            }
        }
        if (keyboard.selectedTemplate != null)
        {
            if (drawButton("Refresh From", 45))
            {
                ReloadFromTemplate(keyboard);
            }
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("References", EditorStyles.boldLabel);
        keyboard.Prototype = EditorGUILayout.ObjectField("Prototype", keyboard.Prototype, typeof(HeathenEngineering.UIX.KeyboardKey), false) as HeathenEngineering.UIX.KeyboardKey;
        keyboard.Container = EditorGUILayout.ObjectField("Container", keyboard.Container, typeof(RectTransform), true) as RectTransform;
        EditorGUILayout.Space();
        if (keyboard.headerRowTransform != null && keyboard.Container != null && keyboard.Prototype != null)
        {
            EditorGUILayout.HelpBox("If you do not intend to load templates at run time you can safely remove the Template Manager without effecting the instantiated keyboard.", MessageType.Info);
            EditorGUILayout.LabelField("Transforms", EditorStyles.boldLabel);
            ShowRows(keyboard);
        }
        EditorGUILayout.Space();
        LoadLayout(keyboard);
        LoadTemplate(keyboard);



        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
    }

    void ShowRows(HeathenEngineering.UIX.KeyboardTemplateManager keyboard)
    {
        keyboard.headerRowTransform = EditorGUILayout.ObjectField("Header Row", keyboard.headerRowTransform, typeof(RectTransform), true) as RectTransform;
        if (keyboard.rowTransforms == null)
        {
            keyboard.rowTransforms = new System.Collections.Generic.List <RectTransform>();
        }

        int rowCount = EditorGUILayout.IntField("Rows", keyboard.rowTransforms.Count);

        if (rowCount > keyboard.rowTransforms.Count)
        {
            int i = 1;
            foreach (RectTransform row in keyboard.rowTransforms)
            {
                keyboard.rowTransforms[i - 1] = EditorGUILayout.ObjectField("Row " + i.ToString(), row, typeof(RectTransform), true) as RectTransform;
                i++;
            }
            for (int x = 0; x < rowCount - (i - 1); x++)
            {
                keyboard.rowTransforms.Add(null);
            }
        }
        else if (rowCount < keyboard.rowTransforms.Count)
        {
            List <RectTransform> nTrans = new List <RectTransform>();
            for (int i = 0; i < rowCount; i++)
            {
                nTrans.Add(keyboard.rowTransforms[i]);
            }
            keyboard.rowTransforms.Clear();
            keyboard.rowTransforms.AddRange(nTrans);
        }
        else
        {
            for (int i = 0; i < keyboard.rowTransforms.Count; i++)
            {
                RectTransform row = keyboard.rowTransforms[i];
                keyboard.rowTransforms[i] = EditorGUILayout.ObjectField("Row " + i.ToString(), row, typeof(RectTransform), true) as RectTransform;
            }
        }
    }

    //void SaveToTemplate(HeathenEngineering.UIX.KeyboardTemplateManager keyboard)
    //{
    //    //TODO: save the current template to disk
    //}

    void LoadTemplate(HeathenEngineering.UIX.KeyboardTemplateManager keyboard)
    {
        legacyFoldOut = EditorGUILayout.BeginToggleGroup("Load Legacy Templates", legacyFoldOut);

        if (legacyFoldOut)
        {
            EditorGUILayout.LabelField("Current: " + (keyboard.selectedTemplate == null || string.IsNullOrEmpty(keyboard.selectedTemplate.TemplateName) ? "Unnamed" : keyboard.selectedTemplate.TemplateName));
            if (keyboard.Prototype != null && keyboard.Container != null)
            {
                TextAsset newAsset = EditorGUILayout.ObjectField("Legacy Template", null, typeof(TextAsset), false) as TextAsset;
                if (newAsset != null)
                {
                    XmlSerializer deserializer = new XmlSerializer(typeof(HeathenEngineering.UIX.Serialization.KeyboardTemplate));
                    StringReader  reader       = new StringReader(newAsset.text);

                    try
                    {
                        HeathenEngineering.UIX.Serialization.KeyboardTemplate result = deserializer.Deserialize(reader) as HeathenEngineering.UIX.Serialization.KeyboardTemplate;
                        reader.Close();

                        if (result != null)
                        {
                            keyboard.selectedTemplate = result;
                            ReloadFromTemplate(keyboard);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogError("Failed to read the selected template. Message: " + ex.Message);
                    }
                }
            }
            else
            {
                EditorGUILayout.HelpBox("You must provide a key prototype in the Prototype field and a target Rect Transform in the Container field in order to load a template.", MessageType.Info);
            }

            EditorGUILayout.Space();
        }
        EditorGUILayout.EndToggleGroup();
    }

    bool legacyFoldOut = false;
    bool keyTemplate   = true;

    void LoadLayout(HeathenEngineering.UIX.KeyboardTemplateManager keyboard)
    {
        keyTemplate = EditorGUILayout.BeginToggleGroup("Load Key Layout", keyTemplate);

        if (keyTemplate)
        {
            if (keyboard.Prototype != null)
            {
                KeyLayout newAsset = EditorGUILayout.ObjectField("Layout", null, typeof(KeyLayout), false) as KeyLayout;
                if (newAsset != null)
                {
                    newAsset.ApplyTo(keyboard.keyboard, keyboard.Prototype);
                }
            }
            else
            {
                EditorGUILayout.HelpBox("You must provide a key prototype in the Prototype field load a key layout.", MessageType.Info);
            }

            EditorGUILayout.Space();
        }
        EditorGUILayout.EndToggleGroup();
    }

    void ReloadFromTemplate(HeathenEngineering.UIX.KeyboardTemplateManager keyboard)
    {
        try
        {
            if (keyboard.selectedTemplate != null)
            {
                keyboard.workingTemplate = keyboard.selectedTemplate;
                keyboard.RefreshKeyboard();
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("Failed to load the selected template. Message: " + ex.Message);
        }
    }

    bool drawButton(string label, float width)
    {
        Rect r = EditorGUILayout.BeginHorizontal("Button", GUILayout.Width(width));

        if (GUI.Button(r, GUIContent.none))
        {
            return(true);
        }
        GUILayout.Label(label);
        EditorGUILayout.EndHorizontal();
        return(false);
    }

    bool drawButton(string label)
    {
        Rect r = EditorGUILayout.BeginHorizontal("Button");

        if (GUI.Button(r, GUIContent.none))
        {
            return(true);
        }
        GUILayout.Label(label);
        EditorGUILayout.EndHorizontal();
        return(false);
    }
}
        /// <summary>
        /// Refresh the Template with the current keyboard structure
        /// </summary>
        public void RefreshTemplate()
        {
            keyboard = GetComponent <Keyboard>();

            if (keyboard == null)
            {
                return;
            }

            if (workingTemplate == null)
            {
                workingTemplate = new Serialization.KeyboardTemplate()
                {
                    TemplateName = "New Template"
                }
            }
            ;

            if (headerRowTransform == null)
            {
                workingTemplate.HeaderRow = null;
            }
            else
            {
                if (workingTemplate.HeaderRow == null)
                {
                    workingTemplate.HeaderRow = new Serialization.KeyboardTemplateRow();
                }

                workingTemplate.HeaderRow.RowOffset   = headerRowTransform.anchoredPosition3D;
                workingTemplate.HeaderRow.RowRotation = headerRowTransform.localEulerAngles;

                List <KeyboardKeyTemplate> keyTemplates = new List <KeyboardKeyTemplate>();

                foreach (Transform trans in headerRowTransform)
                {
                    KeyboardKey key = trans.gameObject.GetComponent <KeyboardKey>();
                    if (key != null)
                    {
                        key.UpdateTemplate(ref key.template);
                        keyTemplates.Add(key.template);
                    }
                }

                workingTemplate.HeaderRow.Keys = keyTemplates.ToArray();
            }

            List <KeyboardTemplateRow> rowTemplates = new List <KeyboardTemplateRow>();

            if (rowTransforms == null)
            {
                rowTransforms = new List <RectTransform>();
            }

            foreach (RectTransform row in rowTransforms)
            {
                if (row == null)
                {
                    continue;
                }

                KeyboardTemplateRow nRow = new KeyboardTemplateRow();
                nRow.RowOffset   = row.anchoredPosition3D;
                nRow.RowRotation = row.localEulerAngles;

                List <KeyboardKeyTemplate> keyTemplates = new List <KeyboardKeyTemplate>();

                foreach (Transform trans in row)
                {
                    KeyboardKey key = trans.gameObject.GetComponent <KeyboardKey>();
                    if (key != null)
                    {
                        key.UpdateTemplate(ref key.template);
                        keyTemplates.Add(key.template);
                    }
                }

                nRow.Keys = keyTemplates.ToArray();
                rowTemplates.Add(nRow);
            }

            workingTemplate.PrimaryRows  = rowTemplates.ToArray();
            workingTemplate.TemplateName = name;
        }