// // Kerbal Engineer Redux // // Copyright (C) 2016 CYBUTEK // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . // namespace KerbalEngineer { using Unity; using Unity.UI; using UnityEngine; using UnityEngine.UI; public static class StyleManager { private static GameObject m_WindowPrefab; private static GameObject m_SettingPrefab; /// /// Creates a setting on the supplied window. /// public static Setting CreateSetting(string label, Window window) { Setting setting = null; GameObject settingPrefab = GetSettingPrefab(); if (settingPrefab != null && window != null) { GameObject settingObject = Object.Instantiate(settingPrefab); if (settingObject != null) { setting = settingObject.GetComponent(); if (setting != null) { setting.SetLabel(label); window.AddToContent(settingObject); } } } return setting; } /// /// Creates and returns a new window object. /// public static Window CreateWindow(string title, float width) { GameObject windowPrefab = GetWindowPrefab(); if (windowPrefab == null) { return null; } GameObject windowObject = Object.Instantiate(windowPrefab); if (windowObject == null) { return null; } // process style applicators Process(windowObject); // assign game object to be a child of the main canvas windowObject.transform.SetParent(MainCanvasUtil.MainCanvas.transform, false); // set window values Window window = windowObject.GetComponent(); if (window != null) { window.SetTitle(title); window.SetWidth(width); } return window; } /// /// Processes all of the style applicators on the supplied game object. /// public static void Process(GameObject gameObject) { if (gameObject == null) { return; } StyleApplicator[] applicators = gameObject.GetComponentsInChildren(); if (applicators != null) { for (int i = 0; i < applicators.Length; i++) { Process(applicators[i]); } } } /// /// Processes all the style applicators on the supplied component's game object. /// public static void Process(Component component) { if (component != null) { Process(component.gameObject); } } /// /// Gets a setting prefab object. /// private static GameObject GetSettingPrefab() { if (m_SettingPrefab == null) { m_SettingPrefab = AssetBundleLoader.Prefabs.LoadAsset("Setting"); } return m_SettingPrefab; } /// /// Gets a new ThemeTextStyle created from KSP UIStyle and UIStyleState objects. /// private static TextStyle GetTextStyle(UIStyle style, UIStyleState styleState) { TextStyle textStyle = new TextStyle(); if (style != null) { textStyle.Font = style.font; textStyle.Style = style.fontStyle; textStyle.Size = style.fontSize; } if (styleState != null) { textStyle.Colour = styleState.textColor; } return textStyle; } /// /// Gets a window prefab object. /// private static GameObject GetWindowPrefab() { if (m_WindowPrefab == null) { m_WindowPrefab = AssetBundleLoader.Prefabs.LoadAsset("Window"); } return m_WindowPrefab; } /// /// Processes a theme on the supplied applicator. /// private static void Process(StyleApplicator applicator) { if (applicator == null) { return; } // get the default skin UISkinDef skin = UISkinManager.defaultSkin; if (skin == null) { return; } // apply selected theme type switch (applicator.ElementType) { case StyleApplicator.ElementTypes.Window: applicator.SetImage(skin.window.normal.background, Image.Type.Sliced); break; case StyleApplicator.ElementTypes.Box: applicator.SetImage(skin.box.normal.background, Image.Type.Sliced); break; case StyleApplicator.ElementTypes.Button: applicator.SetSelectable(null, skin.button.normal.background, skin.button.highlight.background, skin.button.active.background, skin.button.disabled.background); break; case StyleApplicator.ElementTypes.ButtonToggle: applicator.SetToggle(null, skin.button.normal.background, skin.button.highlight.background, skin.button.active.background, skin.button.disabled.background); break; case StyleApplicator.ElementTypes.Label: applicator.SetText(GetTextStyle(skin.label, skin.label.normal)); break; } } } }