// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using FlaxEngine; using FlaxEngine.Assertions; using FlaxEngine.GUI; namespace FlaxEditor.CustomEditors.GUI { /// /// Custom property name label that contains a checkbox used to enable/disable a property. /// /// [HideInEditor] public class CheckablePropertyNameLabel : PropertyNameLabel { /// /// The check box. /// public readonly CheckBox CheckBox; /// /// Event fired when 'checked' state gets changed. /// public event Action CheckChanged; /// public CheckablePropertyNameLabel(string name) : base(name) { CheckBox = new CheckBox(2, 2) { Checked = true, Size = new Float2(14), Parent = this }; CheckBox.StateChanged += OnCheckChanged; Margin = new Margin(CheckBox.Right + 4, 0, 0, 0); } private void OnCheckChanged(CheckBox box) { CheckChanged?.Invoke(this); UpdateStyle(); } /// /// Updates the label style. /// public virtual void UpdateStyle() { var style = Style.Current; bool check = CheckBox.Checked; // Update label text color TextColor = check ? style.Foreground : style.ForegroundGrey; // Update child controls enabled state if (FirstChildControlIndex >= 0 && Parent is PropertiesList propertiesList) { var controls = propertiesList.Children; var labels = propertiesList.Element.Labels; var thisIndex = labels.IndexOf(this); Assert.AreNotEqual(-1, thisIndex, "Invalid label linkage."); int childControlsCount = 0; if (thisIndex + 1 < labels.Count) childControlsCount = labels[thisIndex + 1].FirstChildControlIndex - FirstChildControlIndex - 1; else childControlsCount = controls.Count; int lastControl = Mathf.Min(FirstChildControlIndex + childControlsCount, controls.Count); for (int i = FirstChildControlIndex; i < lastControl; i++) { controls[i].Enabled = check; } } } /// protected override void PerformLayoutBeforeChildren() { base.PerformLayoutBeforeChildren(); // Center checkbox CheckBox.Y = (Height - CheckBox.Height) / 2; } } }