-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add platform base to reuse code
- Loading branch information
Rofli Sanches
committed
May 21, 2021
1 parent
3d68712
commit b8dd7f8
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using ImGuiNET; | ||
using NumericsConverter; | ||
using System; | ||
using UImGui.Assets; | ||
using UnityEngine; | ||
using UnityEngine.Assertions; | ||
|
||
namespace UImGui.Platform | ||
{ | ||
/// <summary> | ||
/// TODO: Write all methods a this base class usage. | ||
/// </summary> | ||
internal class PlatformBase : IPlatform | ||
{ | ||
protected readonly IniSettingsAsset _iniSettings; | ||
protected readonly CursorShapesAsset _cursorShapes; | ||
|
||
protected readonly PlatformCallbacks _callbacks = new PlatformCallbacks(); | ||
|
||
protected ImGuiMouseCursor _lastCursor = ImGuiMouseCursor.COUNT; | ||
|
||
internal PlatformBase(CursorShapesAsset cursorShapes, IniSettingsAsset iniSettings) | ||
{ | ||
_cursorShapes = cursorShapes; | ||
_iniSettings = iniSettings; | ||
} | ||
|
||
public virtual bool Initialize(ImGuiIOPtr io, UIOConfig config, string platformName) | ||
{ | ||
io.SetBackendPlatformName("Unity Input System"); | ||
io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors; | ||
|
||
// TODO: Check if this works | ||
if ((config.ImGuiConfig & ImGuiConfigFlags.NavEnableSetMousePos) != 0) | ||
{ | ||
io.BackendFlags |= ImGuiBackendFlags.HasSetMousePos; | ||
io.WantSetMousePos = true; | ||
} | ||
else | ||
{ | ||
io.BackendFlags &= ~ImGuiBackendFlags.HasSetMousePos; | ||
io.WantSetMousePos = false; | ||
} | ||
|
||
unsafe | ||
{ | ||
// TODO: IMGUI_FEATURE_CUSTOM_ASSERT. | ||
#if IMGUI_FEATURE_CUSTOM_ASSERT | ||
//PlatformCallbacks.SetClipboardFunctions( | ||
// GetClipboardTextCallback, SetClipboardTextCallback, | ||
// ImeSetInputScreenPosCallback, LogAssertCallback, DebugBreakCallback); | ||
#else | ||
PlatformCallbacks.SetClipboardFunctions(PlatformCallbacks.GetClipboardTextCallback, PlatformCallbacks.SetClipboardTextCallback); | ||
#endif | ||
} | ||
|
||
_callbacks.Assign(io); | ||
io.ClipboardUserData = IntPtr.Zero; | ||
|
||
if (_iniSettings != null) | ||
{ | ||
io.SetIniFilename(null); | ||
ImGui.LoadIniSettingsFromMemory(_iniSettings.Load()); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public virtual void PrepareFrame(ImGuiIOPtr io, Rect displayRect) | ||
{ | ||
Assert.IsTrue(io.Fonts.IsBuilt(), "Font atlas not built! Generally built by the renderer. Missing call to renderer NewFrame() function?"); | ||
|
||
io.DisplaySize = displayRect.size.ToSystem(); // TODO: dpi aware, scale, etc. | ||
|
||
io.DeltaTime = Time.unscaledDeltaTime; | ||
|
||
if (_iniSettings != null && io.WantSaveIniSettings) | ||
{ | ||
_iniSettings.Save(ImGui.SaveIniSettingsToMemory()); | ||
io.WantSaveIniSettings = false; | ||
} | ||
} | ||
|
||
public virtual void Shutdown(ImGuiIOPtr io) | ||
{ | ||
io.SetBackendPlatformName(null); | ||
|
||
_callbacks.Unset(io); | ||
} | ||
|
||
protected void UpdateCursor(ImGuiIOPtr io, ImGuiMouseCursor cursor) | ||
{ | ||
if (io.MouseDrawCursor) | ||
{ | ||
cursor = ImGuiMouseCursor.None; | ||
} | ||
|
||
if (_lastCursor == cursor) return; | ||
if ((io.ConfigFlags & ImGuiConfigFlags.NoMouseCursorChange) != 0) return; | ||
|
||
_lastCursor = cursor; | ||
Cursor.visible = cursor != ImGuiMouseCursor.None; // Hide cursor if ImGui is drawing it or if it wants no cursor. | ||
if (_cursorShapes != null) | ||
{ | ||
Cursor.SetCursor(_cursorShapes[cursor].Texture, _cursorShapes[cursor].Hotspot, CursorMode.Auto); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.