Skip to content

Commit

Permalink
feat: add platform base to reuse code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rofli Sanches committed May 21, 2021
1 parent 3d68712 commit b8dd7f8
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
109 changes: 109 additions & 0 deletions Source/Platform/PlatformBase.cs
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);
}
}
}
}
11 changes: 11 additions & 0 deletions Source/Platform/PlatformBase.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b8dd7f8

Please sign in to comment.