public static void ApplyThemeToElement(Transform element, UIThemeAsset theme) { try { if (element.GetComponent <NonThemableElement>()) { return; } //determine element class and color class DetermineElement(element, out var elementClass, out var elementColorClass, out var applyToChildren); //apply theme ApplyThemeToElement(element, elementClass, elementColorClass, theme); //done! } catch (Exception e) { Debug.LogError($"[ThemeEngine] Error in outer ApplyThemeToElement \"{(element.Ref()?.name ?? "null")}\" ({e.GetType().Name})"); if (ConfigState.Instance.UseVerboseLogging) { Debug.LogException(e); } } }
/// <summary> /// Applies a theme to an element and its children /// </summary> public void ApplyThemeRecurse(Transform root, UIThemeAsset theme) { if (CoreParams.UIThemeMode == UIThemePolicy.Disabled || theme == null) { return; } ThemeEngine.ApplyThemeToAll(root, theme); }
/// <summary> /// Applies a theme to an element /// </summary> public void ApplyTheme(Transform element, UIThemeAsset theme) { if (CoreParams.UIThemeMode == UIThemePolicy.Disabled || theme == null) { return; } ThemeEngine.ApplyThemeToElement(element, theme); }
//TODO add some try/catch public static void ApplyThemeToAll(Transform root, UIThemeAsset theme) { Queue <Transform> elements = new Queue <Transform>(); ApplyThemeToElement(root, theme, elements); while (elements.Count > 0) { ApplyThemeToElement(elements.Dequeue(), theme, elements); } }
/// <summary> /// Registers a theme /// </summary> public void RegisterTheme(UIThemeAsset theme) { if (CoreParams.UIThemeMode == UIThemePolicy.Disabled) { return; } string name = theme.name; if (Themes.ContainsKey(name)) { LogWarning($"A theme named \"{name}\" is already registered and will be overwritten!"); } Themes[name] = theme; }
/// <summary> /// Applies a theme to an element and its children /// </summary> public void ApplyThemeRecurse(Transform root, UIThemeAsset theme) { if (CoreParams.UIThemeMode == UIThemePolicy.Disabled) { LogWarning($"Can't apply theme to element because theme engine is disabled!"); return; } if (theme == null) { LogWarning($"Can't apply theme to element because theme is null!"); return; } ThemeEngine.ApplyThemeToAll(root, theme); }
public static void ApplyThemeToElement(Transform element, UIThemeAsset theme, Queue <Transform> elements) { try { //handle non-themable/ignore element var nonThemableComponent = element.GetComponent <NonThemableElement>(); if (nonThemableComponent != null) { if (!nonThemableComponent.IgnoreChildren) { foreach (Transform child in element) { elements.Enqueue(child); } } return; } //determine element class and color class DetermineElement(element, out var elementClass, out var elementColorClass, out var applyToChildren); //apply theme ApplyThemeToElement(element, elementClass, elementColorClass, theme); //append children if applicable if (applyToChildren) { foreach (Transform child in element) { elements.Enqueue(child); } } } catch (Exception e) { Debug.LogError($"[ThemeEngine] Error in outer ApplyThemeToElement \"{(element.Ref()?.name ?? "null")}\" ({e.GetType().Name})"); if (ConfigState.Instance.UseVerboseLogging) { Debug.LogException(e); } } }
private static void ApplyThemeToElement(Transform element, ElementClass elementClass, ElementColorClass elementColorClass, UIThemeAsset theme) { try { switch (elementClass) { case ElementClass.HeadingText: case ElementClass.BodyText: case ElementClass.MonospaceText: { var text = element.GetComponent <Text>(); switch (elementClass) { case ElementClass.HeadingText: text.font = theme.HeadingFont; break; case ElementClass.BodyText: text.font = theme.BodyFont; break; case ElementClass.MonospaceText: text.font = theme.MonospaceFont; break; } switch (elementColorClass) { case ElementColorClass.Normal: text.color = theme.TextColor; break; case ElementColorClass.Contrasting: text.color = theme.TextContrastingColor; break; } } break; case ElementClass.Panel: { var image = element.GetComponent <Image>(); switch (elementColorClass) { case ElementColorClass.Normal: image.sprite = theme.Panel; break; case ElementColorClass.Contrasting: image.sprite = theme.PanelContrasting; break; } } break; case ElementClass.Frame: { var image = element.GetComponent <Image>(); switch (elementColorClass) { case ElementColorClass.Normal: image.sprite = theme.Frame; break; case ElementColorClass.Contrasting: image.sprite = theme.FrameContrasting; break; } } break; case ElementClass.Button: { //button is a little more complicated because we have to style the children also var button = element.GetComponent <Button>(); Color textColor = default; switch (elementColorClass) { case ElementColorClass.Normal: if (button.image != null) { button.image.sprite = theme.Button; } textColor = theme.TextColor; break; case ElementColorClass.Contrasting: if (button.image != null) { button.image.sprite = theme.ButtonContrasting; } textColor = theme.TextContrastingColor; break; } var texts = element.GetComponentsInChildren <Text>(true); foreach (Text text in texts) { text.font = theme.BodyFont; text.color = textColor; } } break; case ElementClass.Slider: { var slider = element.GetComponent <Slider>(); var backgroundElement = element.Find("Background").Ref() ?? element.Find("background"); if (backgroundElement != null) { var backgroundImage = backgroundElement.GetComponent <Image>(); if (backgroundImage != null) { backgroundImage.sprite = theme.Slider; } } var fill = slider.fillRect.Ref()?.GetComponentInChildren <Image>(); if (fill != null) { fill.sprite = theme.SliderFill; } var handle = slider.handleRect.Ref()?.GetComponentInChildren <Image>(); if (handle != null) { handle.sprite = theme.SliderHandle; } } break; case ElementClass.Bar: { var slider = element.GetComponent <Slider>(); if (element.childCount > 0) { var backgroundElement = element.Find("Background").Ref() ?? element.Find("background").Ref() ?? element.GetChild(0); if (backgroundElement != null) { var backgroundImage = backgroundElement.GetComponent <Image>(); if (backgroundImage != null) { backgroundImage.sprite = theme.Bar; } } } var fill = slider.fillRect.Ref()?.GetComponentInChildren <Image>(); if (fill != null) { fill.sprite = theme.BarFill; } } break; case ElementClass.RadioButton: { var toggle = element.GetComponent <Toggle>(); if (toggle.targetGraphic.Ref() is Image targetGraphicImage) { targetGraphicImage.sprite = theme.RadioButton; } else { var backgroundElement = element.Find("Background").Ref() ?? element.Find("background"); if (backgroundElement != null) { var backgroundImage = backgroundElement.GetComponent <Image>(); if (backgroundImage != null) { backgroundImage.sprite = theme.RadioButton; } } } if (toggle.graphic != null) { if (toggle.graphic is Image graphicImage) { graphicImage.sprite = theme.RadioButtonCheck; } } } break; case ElementClass.ToggleButton: { var toggle = element.GetComponent <Toggle>(); if (toggle.targetGraphic.Ref() is Image targetGraphicImage) { targetGraphicImage.sprite = theme.ToggleButton; } else { var backgroundElement = element.Find("Background").Ref() ?? element.Find("background"); if (backgroundElement != null) { var backgroundImage = backgroundElement.GetComponent <Image>(); if (backgroundImage != null) { backgroundImage.sprite = theme.ToggleButton; } } } if (toggle.graphic != null) { if (toggle.graphic is Image graphicImage) { graphicImage.sprite = theme.ToggleButtonCheck; } } var labelElement = element.Find("Label").Ref() ?? element.Find("label"); if (labelElement != null) { var labelText = labelElement.GetComponentInChildren <Text>(); if (labelText != null) { labelText.font = theme.BodyFont; } } } break; case ElementClass.ComboBox: { var dropdown = element.GetComponent <Dropdown>(); if (dropdown.captionText) { dropdown.captionText.font = theme.BodyFont; } if (dropdown.itemText) { dropdown.itemText.font = theme.BodyFont; } var backgroundImage = element.GetComponent <Image>(); if (backgroundImage != null) { backgroundImage.sprite = theme.ComboBox; } var templateElement = dropdown.template; if (templateElement) { var templateImage = templateElement.GetComponent <Image>(); if (templateImage) { templateImage.sprite = theme.ComboBoxList; } } var arrowElement = element.Find("Arrow").Ref() ?? element.Find("arrow"); if (arrowElement != null) { var arrowImage = arrowElement.GetComponent <Image>(); if (arrowImage != null) { arrowImage.sprite = theme.ComboBoxArrow; } var arrowText = arrowElement.GetComponent <Text>(); if (arrowText != null) { arrowText.font = theme.MonospaceFont; } } } break; case ElementClass.InputField: { var inputfield = element.GetComponent <InputField>(); var backgroundImage = element.GetComponent <Image>(); if (backgroundImage != null) { backgroundImage.sprite = theme.InputField; } if (inputfield.placeholder.Ref() is Text inputfieldPlaceholderText) { inputfieldPlaceholderText.font = theme.BodyFont; } if (inputfield.textComponent) { inputfield.textComponent.font = theme.BodyFont; } } break; case ElementClass.Container: //nop break; default: if (ConfigState.Instance.UseVerboseLogging) { Debug.LogWarning($"[ThemeEngine] Failed to apply theme to element \"{element.name}\" (unknown class)"); } break; } } catch (Exception e) { Debug.LogError($"[ThemeEngine] Error applying theme to element \"{(element.Ref()?.name ?? "null")}\" ({e.GetType().Name})"); if (ConfigState.Instance.UseVerboseLogging) { Debug.LogException(e); } } }