// MIT License - Copyright (c) 2025 wallstop // Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE namespace WallstopStudios.UnityHelpers.Editor.Utils.WButton { #if UNITY_EDITOR using System.Collections.Generic; using UnityEditor; using WallstopStudios.UnityHelpers.Editor.Settings; using WallstopStudios.UnityHelpers.Utils; /// /// Helper class for integrating WButton functionality into custom editors. /// Use this when creating custom editors (including with Odin Inspector) to ensure /// WButton methods are drawn correctly in the inspector. /// /// /// /// // Example with Odin Inspector's SerializedMonoBehaviour /// #if ODIN_INSPECTOR /// using Sirenix.OdinInspector.Editor; /// /// public class MyOdinEditor : OdinEditor /// { /// private WButtonEditorHelper _wButtonHelper; /// /// protected override void OnEnable() /// { /// base.OnEnable(); /// _wButtonHelper = new WButtonEditorHelper(); /// } /// /// public override void OnInspectorGUI() /// { /// // Draw buttons at top /// _wButtonHelper.DrawButtonsAtTop(this); /// /// // Draw your custom inspector content /// base.OnInspectorGUI(); /// /// // Draw buttons at bottom and process invocations /// _wButtonHelper.DrawButtonsAtBottomAndProcessInvocations(this); /// } /// } /// #endif /// /// public sealed class WButtonEditorHelper { private readonly Dictionary _paginationStates = new(); private readonly Dictionary _foldoutStates = new(); private readonly List _triggeredContexts = new(); /// /// Draws WButton methods configured for top placement. /// Call this at the beginning of your OnInspectorGUI before drawing other content. /// /// The editor instance (typically 'this' from your custom editor) /// True if any buttons were drawn public bool DrawButtonsAtTop(Editor editor) { UnityHelpersSettings.WButtonActionsPlacement placement = UnityHelpersSettings.GetWButtonActionsPlacement(); UnityHelpersSettings.WButtonFoldoutBehavior foldoutBehavior = UnityHelpersSettings.GetWButtonFoldoutBehavior(); bool globalPlacementIsTop = placement == UnityHelpersSettings.WButtonActionsPlacement.Top; return WButtonGUI.DrawButtons( editor, WButtonPlacement.Top, _paginationStates, _foldoutStates, foldoutBehavior, _triggeredContexts, globalPlacementIsTop ); } /// /// Draws WButton methods configured for bottom placement. /// Call this at the end of your OnInspectorGUI after drawing other content. /// /// The editor instance (typically 'this' from your custom editor) /// True if any buttons were drawn public bool DrawButtonsAtBottom(Editor editor) { UnityHelpersSettings.WButtonActionsPlacement placement = UnityHelpersSettings.GetWButtonActionsPlacement(); UnityHelpersSettings.WButtonFoldoutBehavior foldoutBehavior = UnityHelpersSettings.GetWButtonFoldoutBehavior(); bool globalPlacementIsTop = placement == UnityHelpersSettings.WButtonActionsPlacement.Top; return WButtonGUI.DrawButtons( editor, WButtonPlacement.Bottom, _paginationStates, _foldoutStates, foldoutBehavior, _triggeredContexts, globalPlacementIsTop ); } /// /// Processes any button invocations that were triggered during the current GUI frame. /// Call this after all DrawButtons calls, typically at the very end of OnInspectorGUI. /// public void ProcessInvocations() { if (_triggeredContexts.Count > 0) { WButtonInvocationController.ProcessTriggeredMethods(_triggeredContexts); _triggeredContexts.Clear(); } } /// /// Convenience method that draws buttons at bottom and processes invocations. /// This is the most common pattern - call this at the end of your OnInspectorGUI. /// /// The editor instance (typically 'this' from your custom editor) /// True if any buttons were drawn public bool DrawButtonsAtBottomAndProcessInvocations(Editor editor) { bool anyDrawn = DrawButtonsAtBottom(editor); ProcessInvocations(); return anyDrawn; } /// /// Draws all buttons (both top and bottom placement) and processes invocations. /// Use this when you want to draw all buttons in a single location regardless of placement settings. /// /// The editor instance (typically 'this' from your custom editor) /// True if any buttons were drawn public bool DrawAllButtonsAndProcessInvocations(Editor editor) { bool anyDrawnTop = DrawButtonsAtTop(editor); bool anyDrawnBottom = DrawButtonsAtBottom(editor); ProcessInvocations(); return anyDrawnTop || anyDrawnBottom; } } #endif }