/* * 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 UnityEngine; using UnityEngine.UI; namespace PeterHan.PLib.UI { /// /// A custom UI toggled button factory class. /// public sealed class PToggle : IDynamicSizable { /// /// The default margins around a toggle. /// internal static readonly RectOffset TOGGLE_MARGIN = new RectOffset(1, 1, 1, 1); /// /// Gets a realized toggle button's state. /// /// The realized toggle button. /// The toggle button state. public static bool GetToggleState(GameObject realized) { return realized != null && realized.TryGetComponent(out KToggle toggle) && UIDetours.IS_ON.Get(toggle); } /// /// Sets a realized toggle button's state. /// /// The realized toggle button. /// Whether the button should be on or off. public static void SetToggleState(GameObject realized, bool on) { if (realized != null && realized.TryGetComponent(out KToggle toggle)) UIDetours.IS_ON.Set(toggle, on); } /// /// The sprite to display when active. /// public Sprite ActiveSprite { get; set; } /// /// The toggle's color. /// public ColorStyleSetting Color { get; set; } public bool DynamicSize { get; set; } /// /// The flexible size bounds of this component. /// public Vector2 FlexSize { get; set; } /// /// The sprite to display when inactive. /// public Sprite InactiveSprite { get; set; } /// /// The initial state of the toggle button. /// public bool InitialState { get; set; } /// /// The margin around the component. /// public RectOffset Margin { get; set; } public string Name { get; } /// /// The action to trigger when the state changes. It is passed the realized source /// object. /// public PUIDelegates.OnToggleButton OnStateChanged { get; set; } /// /// The size to scale the toggle images. If 0x0, it will not be scaled. /// public Vector2 Size { get; set; } /// /// The tool tip text. /// public string ToolTip { get; set; } public event PUIDelegates.OnRealize OnRealize; public PToggle() : this(null) { } public PToggle(string name) { ActiveSprite = PUITuning.Images.Contract; Color = PUITuning.Colors.ComponentDarkStyle; InitialState = false; Margin = TOGGLE_MARGIN; Name = name ?? "Toggle"; InactiveSprite = PUITuning.Images.Expand; ToolTip = ""; } /// /// Adds a handler when this toggle button is realized. /// /// The handler to invoke on realization. /// This toggle button for call chaining. public PToggle AddOnRealize(PUIDelegates.OnRealize onRealize) { OnRealize += onRealize; return this; } public GameObject Build() { var toggle = PUIElements.CreateUI(null, Name); // Set on click event var kToggle = toggle.AddComponent(); var evt = OnStateChanged; if (evt != null) kToggle.onValueChanged += (on) => { evt?.Invoke(toggle, on); }; UIDetours.ART_EXTENSION.Set(kToggle, new KToggleArtExtensions()); UIDetours.SOUND_PLAYER_TOGGLE.Set(kToggle, PUITuning.ToggleSounds); // Background image var fgImage = toggle.AddComponent(); fgImage.color = Color.activeColor; fgImage.sprite = InactiveSprite; toggle.SetActive(false); // Toggled images var toggleImage = toggle.AddComponent(); toggleImage.TargetImage = fgImage; toggleImage.useSprites = true; toggleImage.InactiveSprite = InactiveSprite; toggleImage.ActiveSprite = ActiveSprite; toggleImage.startingState = InitialState ? ImageToggleState.State.Active : ImageToggleState.State.Inactive; toggleImage.useStartingState = true; toggleImage.ActiveColour = Color.activeColor; toggleImage.DisabledActiveColour = Color.disabledActiveColor; toggleImage.InactiveColour = Color.inactiveColor; toggleImage.DisabledColour = Color.disabledColor; toggleImage.HoverColour = Color.hoverColor; toggleImage.DisabledHoverColor = Color.disabledhoverColor; UIDetours.IS_ON.Set(kToggle, InitialState); toggle.SetActive(true); // Set size if (Size.x > 0.0f && Size.y > 0.0f) toggle.SetUISize(Size, true); else PUIElements.AddSizeFitter(toggle, DynamicSize); // Add tooltip PUIElements.SetToolTip(toggle, ToolTip).SetFlexUISize(FlexSize).SetActive(true); OnRealize?.Invoke(toggle); return toggle; } public override string ToString() { return string.Format("PToggle[Name={0}]", Name); } } }