/*
* Copyright 2022 Peter Han
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using PeterHan.PLib.Core;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace PeterHan.PLib.UI {
///
/// Sets up common parameters for the UI in PLib based mods. Note that this class is still
/// specific to individual mods so the values in the latest PLib will not supersede them.
///
public static class PUITuning {
///
/// UI images.
///
public static class Images {
///
/// The right arrow image. Rotate it in the Image to get more directions.
///
public static Sprite Arrow { get; }
///
/// The image used to make a 1px solid black border.
///
public static Sprite BoxBorder { get; }
///
/// The image used to make a 1px solid white border.
///
public static Sprite BoxBorderWhite { get; }
///
/// The default image used for button appearance.
///
public static Sprite ButtonBorder { get; }
///
/// The border image around a checkbox.
///
public static Sprite CheckBorder { get; }
///
/// The image for a check box which is checked.
///
public static Sprite Checked { get; }
///
/// The image used for dialog close buttons.
///
public static Sprite Close { get; }
///
/// The image for contracting a category.
///
public static Sprite Contract { get; }
///
/// The image for expanding a category.
///
public static Sprite Expand { get; }
///
/// The image for a check box which is neither checked nor unchecked.
///
public static Sprite Partial { get; }
///
/// The border of a horizontal scroll bar.
///
public static Sprite ScrollBorderHorizontal { get; }
///
/// The handle of a horizontal scroll bar.
///
public static Sprite ScrollHandleHorizontal { get; }
///
/// The border of a vertical scroll bar.
///
public static Sprite ScrollBorderVertical { get; }
///
/// The handle of a vertical scroll bar.
///
public static Sprite ScrollHandleVertical { get; }
///
/// The handle of a horizontal slider.
///
public static Sprite SliderHandle { get; }
///
/// The sprite dictionary.
///
private static readonly IDictionary SPRITES;
static Images() {
SPRITES = new Dictionary(512);
// List out all sprites shipped with the game
foreach (var img in Resources.FindObjectsOfTypeAll()) {
string name = img?.name;
if (!string.IsNullOrEmpty(name) && !SPRITES.ContainsKey(name))
SPRITES.Add(name, img);
}
Arrow = GetSpriteByName("game_speed_play");
BoxBorder = GetSpriteByName("web_box");
BoxBorderWhite = GetSpriteByName("web_border");
ButtonBorder = GetSpriteByName("web_button");
CheckBorder = GetSpriteByName("overview_jobs_skill_box");
Checked = GetSpriteByName("overview_jobs_icon_checkmark");
Close = GetSpriteByName("cancel");
Contract = GetSpriteByName("iconDown");
Expand = GetSpriteByName("iconRight");
Partial = GetSpriteByName("overview_jobs_icon_mixed");
ScrollBorderHorizontal = GetSpriteByName("build_menu_scrollbar_frame_horizontal");
ScrollHandleHorizontal = GetSpriteByName("build_menu_scrollbar_inner_horizontal");
ScrollBorderVertical = GetSpriteByName("build_menu_scrollbar_frame");
ScrollHandleVertical = GetSpriteByName("build_menu_scrollbar_inner");
SliderHandle = GetSpriteByName("game_speed_selected_med");
}
///
/// Retrieves a sprite by its name.
///
/// The sprite name.
/// The matching sprite, or null if no sprite found in the resources has that name.
public static Sprite GetSpriteByName(string name) {
if (!SPRITES.TryGetValue(name, out Sprite sprite))
sprite = null;
return sprite;
}
}
///
/// UI colors.
///
public static class Colors {
///
/// A white color used for default backgrounds.
///
public static Color BackgroundLight { get; }
///
/// The color styles used on pink buttons.
///
public static ColorStyleSetting ButtonPinkStyle { get; }
///
/// The color styles used on blue buttons.
///
public static ColorStyleSetting ButtonBlueStyle { get; }
///
/// The default colors used on check boxes / toggles with dark backgrounds.
///
public static ColorStyleSetting ComponentDarkStyle { get; }
///
/// The default colors used on check boxes / toggles with white backgrounds.
///
public static ColorStyleSetting ComponentLightStyle { get; }
///
/// The color displayed on dialog backgrounds.
///
public static Color DialogBackground { get; }
///
/// The color displayed in the large border around the outsides of options dialogs.
///
public static Color DialogDarkBackground { get; }
///
/// The color displayed on options dialog backgrounds.
///
public static Color OptionsBackground { get; }
///
/// The color displayed on scrollbar handles.
///
public static ColorBlock ScrollbarColors { get; }
///
/// The background color for selections.
///
public static Color SelectionBackground { get; }
///
/// The foreground color for selections.
///
public static Color SelectionForeground { get; }
///
/// A completely transparent color.
///
public static Color Transparent { get; }
///
/// Used for dark-colored UI text.
///
public static Color UITextDark { get; }
///
/// Used for light-colored UI text.
///
public static Color UITextLight { get; }
static Colors() {
BackgroundLight = new Color32(255, 255, 255, 255);
DialogBackground = new Color32(0, 0, 0, 255);
DialogDarkBackground = new Color32(48, 52, 67, 255);
OptionsBackground = new Color32(31, 34, 43, 255);
SelectionBackground = new Color32(189, 218, 255, 255);
SelectionForeground = new Color32(0, 0, 0, 255);
Transparent = new Color32(255, 255, 255, 0);
UITextLight = new Color32(255, 255, 255, 255);
UITextDark = new Color32(0, 0, 0, 255);
// Check boxes
Color active = new Color(0.0f, 0.0f, 0.0f), disabled = new Color(0.784f,
0.784f, 0.784f, 1.0f);
ComponentLightStyle = ScriptableObject.CreateInstance();
ComponentLightStyle.activeColor = active;
ComponentLightStyle.inactiveColor = active;
ComponentLightStyle.hoverColor = active;
ComponentLightStyle.disabledActiveColor = disabled;
ComponentLightStyle.disabledColor = disabled;
ComponentLightStyle.disabledhoverColor = disabled;
active = new Color(1.0f, 1.0f, 1.0f);
ComponentDarkStyle = ScriptableObject.CreateInstance();
ComponentDarkStyle.activeColor = active;
ComponentDarkStyle.inactiveColor = active;
ComponentDarkStyle.hoverColor = active;
ComponentDarkStyle.disabledActiveColor = disabled;
ComponentDarkStyle.disabledColor = disabled;
ComponentDarkStyle.disabledhoverColor = disabled;
// Buttons: pink
ButtonPinkStyle = ScriptableObject.CreateInstance();
ButtonPinkStyle.activeColor = new Color(0.7941176f, 0.4496107f, 0.6242238f);
ButtonPinkStyle.inactiveColor = new Color(0.5294118f, 0.2724914f, 0.4009516f);
ButtonPinkStyle.disabledColor = new Color(0.4156863f, 0.4117647f, 0.4f);
ButtonPinkStyle.disabledActiveColor = Transparent;
ButtonPinkStyle.hoverColor = new Color(0.6176471f, 0.3315311f, 0.4745891f);
ButtonPinkStyle.disabledhoverColor = new Color(0.5f, 0.5f, 0.5f);
// Buttons: blue
ButtonBlueStyle = ScriptableObject.CreateInstance();
ButtonBlueStyle.activeColor = new Color(0.5033521f, 0.5444419f, 0.6985294f);
ButtonBlueStyle.inactiveColor = new Color(0.2431373f, 0.2627451f, 0.3411765f);
ButtonBlueStyle.disabledColor = new Color(0.4156863f, 0.4117647f, 0.4f);
ButtonBlueStyle.disabledActiveColor = new Color(0.625f, 0.6158088f, 0.5882353f);
ButtonBlueStyle.hoverColor = new Color(0.3461289f, 0.3739619f, 0.4852941f);
ButtonBlueStyle.disabledhoverColor = new Color(0.5f, 0.4898898f, 0.4595588f);
// Scrollbars
ScrollbarColors = new ColorBlock {
colorMultiplier = 1.0f,
fadeDuration = 0.1f,
disabledColor = new Color(0.392f, 0.392f, 0.392f),
highlightedColor = new Color32(161, 163, 174, 255),
normalColor = new Color32(161, 163, 174, 255),
pressedColor = BackgroundLight
};
}
}
///
/// Collects references to fonts in the game.
///
public static class Fonts {
///
/// The text font name.
///
private const string DEFAULT_FONT_TEXT = "NotoSans-Regular";
///
/// The UI font name.
///
private const string DEFAULT_FONT_UI = "GRAYSTROKE REGULAR SDF";
///
/// The default font size.
///
public static int DefaultSize { get; }
///
/// The default font asset for text strings.
///
private static readonly TMP_FontAsset DefaultTextFont;
///
/// The default font asset for UI titles and buttons.
///
private static readonly TMP_FontAsset DefaultUIFont;
///
/// The font used on text.
///
internal static TMP_FontAsset Text {
get {
TMP_FontAsset font = null;
if (Localization.GetSelectedLanguageType() != Localization.
SelectedLanguageType.None)
font = Localization.FontAsset;
return font ?? DefaultTextFont;
}
}
///
/// The text styles used on all items with a light background.
///
public static TextStyleSetting TextDarkStyle { get; }
///
/// The text styles used on all items with a dark background.
///
public static TextStyleSetting TextLightStyle { get; }
///
/// The font used on UI elements.
///
internal static TMP_FontAsset UI {
get {
TMP_FontAsset font = null;
if (Localization.GetSelectedLanguageType() != Localization.
SelectedLanguageType.None)
font = Localization.FontAsset;
return font ?? DefaultUIFont;
}
}
///
/// The text styles used on all UI items with a light background.
///
public static TextStyleSetting UIDarkStyle { get; }
///
/// The text styles used on all UI items with a dark background.
///
public static TextStyleSetting UILightStyle { get; }
///
/// The font dictionary.
///
private static readonly IDictionary FONTS;
static Fonts() {
FONTS = new Dictionary(16);
// List out all fonts shipped with the game
foreach (var newFont in Resources.FindObjectsOfTypeAll()) {
string name = newFont?.name;
if (!string.IsNullOrEmpty(name) && !FONTS.ContainsKey(name))
FONTS.Add(name, newFont);
}
// Initialization: UI fonts
if ((DefaultTextFont = GetFontByName(DEFAULT_FONT_TEXT)) == null)
PUIUtils.LogUIWarning("Unable to find font " + DEFAULT_FONT_TEXT);
if ((DefaultUIFont = GetFontByName(DEFAULT_FONT_UI)) == null)
PUIUtils.LogUIWarning("Unable to find font " + DEFAULT_FONT_UI);
// Initialization: Text style
DefaultSize = 14;
TextDarkStyle = ScriptableObject.CreateInstance();
TextDarkStyle.enableWordWrapping = false;
TextDarkStyle.fontSize = DefaultSize;
TextDarkStyle.sdfFont = Text;
TextDarkStyle.style = FontStyles.Normal;
TextDarkStyle.textColor = Colors.UITextDark;
TextLightStyle = TextDarkStyle.DeriveStyle(newColor: Colors.UITextLight);
UIDarkStyle = ScriptableObject.CreateInstance();
UIDarkStyle.enableWordWrapping = false;
UIDarkStyle.fontSize = DefaultSize;
UIDarkStyle.sdfFont = UI;
UIDarkStyle.style = FontStyles.Normal;
UIDarkStyle.textColor = Colors.UITextDark;
UILightStyle = UIDarkStyle.DeriveStyle(newColor: Colors.UITextLight);
}
///
/// Retrieves a font by its name.
///
/// The font name.
/// The matching font, or null if no font found in the resources has that name.
internal static TMP_FontAsset GetFontByName(string name) {
if (!FONTS.TryGetValue(name, out TMP_FontAsset font))
font = null;
return font;
}
}
///
/// The sounds played by the button.
///
internal static ButtonSoundPlayer ButtonSounds { get; }
///
/// The sounds played by the toggle.
///
internal static ToggleSoundPlayer ToggleSounds { get; }
static PUITuning() {
// Initialization: Button sounds
ButtonSounds = new ButtonSoundPlayer() {
Enabled = true
};
ToggleSounds = new ToggleSoundPlayer();
}
}
}