Skip to content

Commit

Permalink
Add ImguiWindow class to utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Oct 31, 2022
1 parent 1072300 commit 1eb85df
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
- [`Extensions`](KKAPI.Utilities.md#extensions)
- [`HSceneUtils`](KKAPI.Utilities.md#hsceneutils)
- [`IMGUIUtils`](KKAPI.Utilities.md#imguiutils)
- [`ImguiWindow<T>`](KKAPI.Utilities.md#imguiwindowt)
- [`MemoryInfo`](KKAPI.Utilities.md#memoryinfo)
- [`ObservableExtensions`](KKAPI.Utilities.md#observableextensions)
- [`OpenFileDialog`](KKAPI.Utilities.md#openfiledialog)
Expand Down
37 changes: 37 additions & 0 deletions doc/KKAPI.Utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,43 @@ Static Methods
| `void` | EatInputInRect(`Rect` eatRect) | Block input from going through to the game/canvases if the mouse cursor is within the specified Rect. Use after a GUI.Window call or the window will not be able to get the inputs either. <example> GUILayout.Window(362, screenRect, TreeWindow, "Select character folder"); Utils.EatInputInRect(screenRect); </example> |


## `ImguiWindow<T>`

Base class for IMGUI windows that are implemented as full MonoBehaviours. Instantiate to add the window, only one instance should ever exist. Turn drawing the window on and off by setting the enable property (off by default).
```csharp
public abstract class KKAPI.Utilities.ImguiWindow<T>
: MonoBehaviour

```

Properties

| Type | Name | Summary |
| --- | --- | --- |
| `Vector2` | MinimumSize | Minimum size of the window. |
| `String` | Title | Title of the window. |
| `Int32` | WindowId | ID of the window, set to a random number by default. |
| `Rect` | WindowRect | Position and size of the window. |


Methods

| Type | Name | Summary |
| --- | --- | --- |
| `void` | DrawContents() | Draw contents of the IMGUI window (this is inside of the GUILayout.Window func). Use GUILayout instead of GUI, and expect the window size to change during runtime. |
| `Rect` | GetDefaultWindowRect(`Rect` screenRect) | Should return the initial desired size of the window, adjusted to fit inside the screen space. |
| `void` | OnEnable() | Make sure to call base.OnEnable when overriding! |
| `void` | OnGUI() | Make sure to call base.OnGUI when overriding! |
| `void` | ResetWindowRect() | Reset the window rect (position and size) to its default value. |


Static Properties

| Type | Name | Summary |
| --- | --- | --- |
| `T` | Instance | Instance of the window. Null if none were created yet. |


## `MemoryInfo`

Provides information about system memory status
Expand Down
1 change: 1 addition & 0 deletions src/Shared.Core/Shared.Core.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Maker\UI\BaseGuiEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Maker\UI\Sidebar\ISidebarControl.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SceneApi.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\IMGUI\ImguiWindow.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\TimelineCompatibility.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ConfigurationManagerAttributes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\CoroutineUtils.cs" />
Expand Down
123 changes: 123 additions & 0 deletions src/Shared.Core/Utilities/IMGUI/ImguiWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using UnityEngine;

namespace KKAPI.Utilities
{
/// <summary>
/// Base class for IMGUI windows that are implemented as full MonoBehaviours.
/// Instantiate to add the window, only one instance should ever exist.
/// Turn drawing the window on and off by setting the enable property (off by default).
/// </summary>
public abstract class ImguiWindow<T> : MonoBehaviour where T : ImguiWindow<T>
{
/// <summary>
/// Instance of the window. Null if none were created yet.
/// </summary>
public static T Instance { get; private set; }

/// <summary>
/// Base constructor
/// </summary>
protected ImguiWindow()
{
WindowId = base.GetHashCode();
Instance = (T)this;
enabled = false;
}

/// <summary>
/// Make sure to call base.OnGUI when overriding!
/// </summary>
protected virtual void OnGUI()
{
WindowRect = GUILayout.Window(WindowId, WindowRect, DrawContentsInt, Title);
if (WindowRect.width < MinimumSize.x)
{
var rect = WindowRect;
rect.width = MinimumSize.x;
WindowRect = rect;
}

if (WindowRect.height < MinimumSize.y)
{
var rect = WindowRect;
rect.height = MinimumSize.y;
WindowRect = rect;
}
}

private void DrawContentsInt(int id)
{
int visibleAreaSize = GUI.skin.window.border.top - 4;// 10;
if (GUI.Button(new Rect(WindowRect.width - visibleAreaSize - 2, 2, visibleAreaSize, visibleAreaSize), "X"))
{
enabled = false;
return;
}

try
{
DrawContents();
IMGUIUtils.DrawTooltip(WindowRect);
}
catch (Exception ex)
{
// Ignore mismatch exceptions caused by virtual lists, there will be an unity error shown anyways
if (!ex.Message.Contains("GUILayout")) ;
//Logger.Log(LogLevel.Error, $"[{Title}] GUI crash: {ex}");
}

WindowRect = IMGUIUtils.DragResizeEatWindow(id, WindowRect);
}

/// <summary>
/// Should return the initial desired size of the window, adjusted to fit inside the screen space.
/// </summary>
protected abstract Rect GetDefaultWindowRect(Rect screenRect);

/// <summary>
/// Draw contents of the IMGUI window (this is inside of the GUILayout.Window func).
/// Use GUILayout instead of GUI, and expect the window size to change during runtime.
/// </summary>
protected abstract void DrawContents();

/// <summary>
/// Make sure to call base.OnEnable when overriding!
/// </summary>
protected virtual void OnEnable()
{
if (WindowRect == default) ResetWindowRect();
}

/// <summary>
/// Reset the window rect (position and size) to its default value.
/// </summary>
public void ResetWindowRect()
{
//var screenRect = new Rect(
// ScreenOffset,
// ScreenOffset,
// Screen.width - ScreenOffset * 2,
// Screen.height - ScreenOffset * 2);
var screenRect = new Rect(0, 0, Screen.width, Screen.height);
WindowRect = GetDefaultWindowRect(screenRect);
}

/// <summary>
/// Title of the window.
/// </summary>
public virtual string Title { get; set; }
/// <summary>
/// ID of the window, set to a random number by default.
/// </summary>
public int WindowId { get; set; }
/// <summary>
/// Position and size of the window.
/// </summary>
public virtual Rect WindowRect { get; set; }
/// <summary>
/// Minimum size of the window.
/// </summary>
public Vector2 MinimumSize { get; set; } = new Vector2(100, 100);
}
}

0 comments on commit 1eb85df

Please sign in to comment.