Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ImGuiNET.Unity/Assets/StyleAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ sealed class StyleAsset : ScriptableObject
[Tooltip("Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.")]
public float CurveTessellationTol;

public float CircleTessellationMaxError { get; private set; }

[Tooltip("Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry.")]
public float CircleSegmentMaxError;

Expand Down Expand Up @@ -149,7 +151,7 @@ public unsafe void ApplyTo(ImGuiStylePtr s)
s.AntiAliasedLines = AntiAliasedLines;
s.AntiAliasedFill = AntiAliasedFill;
s.CurveTessellationTol = CurveTessellationTol;
s.CircleSegmentMaxError = CircleSegmentMaxError;
s.CircleTessellationMaxError = CircleSegmentMaxError;
for (var i = 0; i < Colors.Length; ++i)
s.Colors[i] = Colors[i];
}
Expand Down Expand Up @@ -190,7 +192,7 @@ public unsafe void SetFrom(ImGuiStylePtr s)
AntiAliasedLines = s.AntiAliasedLines;
AntiAliasedFill = s.AntiAliasedFill;
CurveTessellationTol = s.CurveTessellationTol;
CircleSegmentMaxError = s.CircleSegmentMaxError;
CircleTessellationMaxError = s.CircleTessellationMaxError;
for (var i = 0; i < Colors.Length; ++i)
Colors[i] = s.Colors[i];
}
Expand Down
6 changes: 3 additions & 3 deletions ImGuiNET.Unity/Data/FontConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct FontConfig
public bool MergeIntoPrevious;

[Tooltip("Settings for custom font rasterizer (e.g. FreeType). Leave as zero if you aren't using one.")]
public uint RasterizerFlags;
public uint FontBuilderFlags;

[Tooltip("Brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable.")]
public float RasterizerMultiply;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void ApplyTo(ImFontConfigPtr im)
im.GlyphMinAdvanceX = GlyphMinAdvanceX;
im.GlyphMaxAdvanceX = GlyphMaxAdvanceX;
im.MergeMode = MergeIntoPrevious;
im.RasterizerFlags = RasterizerFlags;
im.FontBuilderFlags = FontBuilderFlags;
im.RasterizerMultiply = RasterizerMultiply;
im.EllipsisChar = EllipsisChar;

Expand All @@ -90,7 +90,7 @@ public void SetFrom(ImFontConfigPtr im)
GlyphMinAdvanceX = im.GlyphMinAdvanceX;
GlyphMaxAdvanceX = im.GlyphMaxAdvanceX;
MergeIntoPrevious = im.MergeMode;
RasterizerFlags = im.RasterizerFlags;
FontBuilderFlags = im.FontBuilderFlags;
RasterizerMultiply = im.RasterizerMultiply;
EllipsisChar = (char)im.EllipsisChar;

Expand Down
4 changes: 2 additions & 2 deletions ImGuiNET.Unity/Data/IOConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void ApplyTo(ImGuiIOPtr io)
io.ConfigInputTextCursorBlink = TextCursorBlink;
io.ConfigWindowsResizeFromEdges = ResizeFromEdges;
io.ConfigWindowsMoveFromTitleBarOnly = MoveFromTitleOnly;
io.ConfigWindowsMemoryCompactTimer = MemoryCompactTimer;
io.ConfigMemoryCompactTimer = MemoryCompactTimer;
}

public void SetFrom(ImGuiIOPtr io)
Expand All @@ -93,7 +93,7 @@ public void SetFrom(ImGuiIOPtr io)
TextCursorBlink = io.ConfigInputTextCursorBlink;
ResizeFromEdges = io.ConfigWindowsResizeFromEdges;
MoveFromTitleOnly = io.ConfigWindowsMoveFromTitleBarOnly;
MemoryCompactTimer = io.ConfigWindowsMemoryCompactTimer;
MemoryCompactTimer = io.ConfigMemoryCompactTimer;
}
}
}
83 changes: 61 additions & 22 deletions ImGuiNET.Unity/Platform/ImGuiPlatformInputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using UnityEngine;
using UnityEngine.Assertions;

#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

namespace ImGuiNET.Unity
{
// Implemented features:
Expand Down Expand Up @@ -95,33 +99,52 @@ void SetupKeyboard(ImGuiIOPtr io)
{
_mainKeys = new int[] {
// map and store new keys by assigning io.KeyMap and setting value of array
io.KeyMap[(int)ImGuiKey.Tab ] = (int)KeyCode.Tab,
io.KeyMap[(int)ImGuiKey.LeftArrow ] = (int)KeyCode.LeftArrow,
io.KeyMap[(int)ImGuiKey.RightArrow ] = (int)KeyCode.RightArrow,
io.KeyMap[(int)ImGuiKey.UpArrow ] = (int)KeyCode.UpArrow,
io.KeyMap[(int)ImGuiKey.DownArrow ] = (int)KeyCode.DownArrow,
io.KeyMap[(int)ImGuiKey.PageUp ] = (int)KeyCode.PageUp,
io.KeyMap[(int)ImGuiKey.PageDown ] = (int)KeyCode.PageDown,
io.KeyMap[(int)ImGuiKey.Home ] = (int)KeyCode.Home,
io.KeyMap[(int)ImGuiKey.End ] = (int)KeyCode.End,
io.KeyMap[(int)ImGuiKey.Insert ] = (int)KeyCode.Insert,
io.KeyMap[(int)ImGuiKey.Delete ] = (int)KeyCode.Delete,
io.KeyMap[(int)ImGuiKey.Backspace ] = (int)KeyCode.Backspace,
io.KeyMap[(int)ImGuiKey.Space ] = (int)KeyCode.Space,
io.KeyMap[(int)ImGuiKey.Enter ] = (int)KeyCode.Return,
io.KeyMap[(int)ImGuiKey.Escape ] = (int)KeyCode.Escape,
io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)KeyCode.KeypadEnter,
io.KeyMap[(int)ImGuiKey.A ] = (int)KeyCode.A, // for text edit CTRL+A: select all
io.KeyMap[(int)ImGuiKey.C ] = (int)KeyCode.C, // for text edit CTRL+C: copy
io.KeyMap[(int)ImGuiKey.V ] = (int)KeyCode.V, // for text edit CTRL+V: paste
io.KeyMap[(int)ImGuiKey.X ] = (int)KeyCode.X, // for text edit CTRL+X: cut
io.KeyMap[(int)ImGuiKey.Y ] = (int)KeyCode.Y, // for text edit CTRL+Y: redo
io.KeyMap[(int)ImGuiKey.Z ] = (int)KeyCode.Z, // for text edit CTRL+Z: undo
io.KeyMap[(int)ImGuiKey.Tab ] = (int)Key.Tab,
io.KeyMap[(int)ImGuiKey.LeftArrow ] = (int)Key.LeftArrow,
io.KeyMap[(int)ImGuiKey.RightArrow ] = (int)Key.RightArrow,
io.KeyMap[(int)ImGuiKey.UpArrow ] = (int)Key.UpArrow,
io.KeyMap[(int)ImGuiKey.DownArrow ] = (int)Key.DownArrow,
io.KeyMap[(int)ImGuiKey.PageUp ] = (int)Key.PageUp,
io.KeyMap[(int)ImGuiKey.PageDown ] = (int)Key.PageDown,
io.KeyMap[(int)ImGuiKey.Home ] = (int)Key.Home,
io.KeyMap[(int)ImGuiKey.End ] = (int)Key.End,
io.KeyMap[(int)ImGuiKey.Insert ] = (int)Key.Insert,
io.KeyMap[(int)ImGuiKey.Delete ] = (int)Key.Delete,
io.KeyMap[(int)ImGuiKey.Backspace ] = (int)Key.Backspace,
io.KeyMap[(int)ImGuiKey.Space ] = (int)Key.Space,
io.KeyMap[(int)ImGuiKey.Enter ] = (int)Key.Enter,
io.KeyMap[(int)ImGuiKey.Escape ] = (int)Key.Escape,
io.KeyMap[(int)ImGuiKey.KeypadEnter] = (int)Key.NumpadEnter,
io.KeyMap[(int)ImGuiKey.A ] = (int)Key.A, // for text edit CTRL+A: select all
io.KeyMap[(int)ImGuiKey.C ] = (int)Key.C, // for text edit CTRL+C: copy
io.KeyMap[(int)ImGuiKey.V ] = (int)Key.V, // for text edit CTRL+V: paste
io.KeyMap[(int)ImGuiKey.X ] = (int)Key.X, // for text edit CTRL+X: cut
io.KeyMap[(int)ImGuiKey.Y ] = (int)Key.Y, // for text edit CTRL+Y: redo
io.KeyMap[(int)ImGuiKey.Z ] = (int)Key.Z, // for text edit CTRL+Z: undo
};
}

void UpdateKeyboard(ImGuiIOPtr io)
{
#if ENABLE_INPUT_SYSTEM
var keyboard = Keyboard.current;

// main keys
foreach (var key in _mainKeys)
io.KeysDown[key] = keyboard[(Key)key].isPressed;

// keyboard modifiers
io.KeyShift = keyboard.shiftKey.isPressed;
io.KeyCtrl = keyboard.ctrlKey.isPressed;
io.KeyAlt = keyboard.altKey.isPressed;
io.KeySuper = keyboard.leftCommandKey.isPressed || keyboard.rightCommandKey.isPressed
|| keyboard.leftWindowsKey.isPressed || keyboard.rightWindowsKey.isPressed;

// text input
while (Event.PopEvent(_e))
if (_e.rawType == EventType.KeyDown && _e.character != 0 && _e.character != '\n')
io.AddInputCharacter(_e.character);
#elif ENABLE_LEGACY_INPUT_MANAGER
// main keys
foreach (var key in _mainKeys)
io.KeysDown[key] = Input.GetKey((KeyCode)key);
Expand All @@ -137,10 +160,25 @@ void UpdateKeyboard(ImGuiIOPtr io)
while (Event.PopEvent(_e))
if (_e.rawType == EventType.KeyDown && _e.character != 0 && _e.character != '\n')
io.AddInputCharacter(_e.character);
#endif
}

static void UpdateMouse(ImGuiIOPtr io)
{
#if ENABLE_INPUT_SYSTEM
var mouse = Mouse.current;
var mousePosition = mouse.position.ReadValue();

io.MousePos = ImGuiUn.ScreenToImGui(new Vector2(mousePosition.x, mousePosition.y));

var scroll = mouse.scroll.ReadValue().normalized;
io.MouseWheel = scroll.y;
io.MouseWheelH = scroll.x;

io.MouseDown[0] = mouse.leftButton.isPressed;
io.MouseDown[1] = mouse.rightButton.isPressed;
io.MouseDown[2] = mouse.middleButton.isPressed;
#elif ENABLE_LEGACY_INPUT_MANAGER
io.MousePos = ImGuiUn.ScreenToImGui(new Vector2(Input.mousePosition.x, Input.mousePosition.y));

io.MouseWheel = Input.mouseScrollDelta.y;
Expand All @@ -149,6 +187,7 @@ static void UpdateMouse(ImGuiIOPtr io)
io.MouseDown[0] = Input.GetMouseButton(0);
io.MouseDown[1] = Input.GetMouseButton(1);
io.MouseDown[2] = Input.GetMouseButton(2);
#endif
}

void UpdateCursor(ImGuiIOPtr io, ImGuiMouseCursor cursor)
Expand Down
2 changes: 1 addition & 1 deletion ImGuiNET.Unity/Platform/ImGuiPlatformInputSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void SetupKeyboard(ImGuiIOPtr io, Keyboard kb)
io.KeyMap[(int)ImGuiKey.Space ] = (int)Key.Space,
io.KeyMap[(int)ImGuiKey.Enter ] = (int)Key.Enter,
io.KeyMap[(int)ImGuiKey.Escape ] = (int)Key.Escape,
io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)Key.NumpadEnter,
io.KeyMap[(int)ImGuiKey.KeypadEnter] = (int)Key.NumpadEnter,
// letter keys mapped by display name to avoid being layout agnostic (used as shortcuts)
io.KeyMap[(int)ImGuiKey.A ] = (int)((KeyControl)kb["#(a)"]).keyCode, // for text edit CTRL+A: select all
io.KeyMap[(int)ImGuiKey.C ] = (int)((KeyControl)kb["#(c)"]).keyCode, // for text edit CTRL+C: copy
Expand Down
8 changes: 4 additions & 4 deletions ImGuiNET.Unity/Platform/PlatformCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ unsafe class PlatformCallbacks
// after assigning its function pointers to unmanaged code
GetClipboardTextCallback _getClipboardText;
SetClipboardTextCallback _setClipboardText;
ImeSetInputScreenPosCallback _imeSetInputScreenPos;
ImeSetInputScreenPosCallback _setPlatformImeData;
#if IMGUI_FEATURE_CUSTOM_ASSERT
LogAssertCallback _logAssert;
DebugBreakCallback _debugBreak;
Expand All @@ -47,7 +47,7 @@ public void Assign(ImGuiIOPtr io)
{
io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_setClipboardText);
io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_getClipboardText);
io.ImeSetInputScreenPosFn = Marshal.GetFunctionPointerForDelegate(_imeSetInputScreenPos);
io.SetPlatformImeDataFn = Marshal.GetFunctionPointerForDelegate(_setPlatformImeData);
#if IMGUI_FEATURE_CUSTOM_ASSERT
io.SetBackendPlatformUserData<CustomAssertData>(new CustomAssertData
{
Expand All @@ -61,7 +61,7 @@ public void Unset(ImGuiIOPtr io)
{
io.SetClipboardTextFn = IntPtr.Zero;
io.GetClipboardTextFn = IntPtr.Zero;
io.ImeSetInputScreenPosFn = IntPtr.Zero;
io.SetPlatformImeDataFn = IntPtr.Zero;
#if IMGUI_FEATURE_CUSTOM_ASSERT
io.SetBackendPlatformUserData<CustomAssertData>(null);
#endif
Expand All @@ -88,7 +88,7 @@ public SetClipboardTextSafeCallback SetClipboardText

public ImeSetInputScreenPosCallback ImeSetInputScreenPos
{
set => _imeSetInputScreenPos = (x, y) =>
set => _setPlatformImeData = (x, y) =>
{
try { value(x, y); }
catch (Exception ex) { Debug.LogException(ex); }
Expand Down
7 changes: 5 additions & 2 deletions ImGuiNET.Unity/Platform/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ public SpriteInfo GetSpriteInfo(Sprite sprite)
if (!_spriteData.TryGetValue(sprite, out SpriteInfo sprInfo))
{
Vector2[] uvs = sprite.uv; // allocates

Debug.Assert(uvs.Length == 4, $"Sprite {sprite.name} must be set to Full Rect");

_spriteData[sprite] = sprInfo = new SpriteInfo
{
texture = sprite.texture,
size = sprite.rect.size,
uv0 = new Vector2(uvs[0].x, 1f - uvs[0].y),
uv1 = new Vector2(uvs[1].x, 1f - uvs[1].y),
uv0 = new Vector2(uvs[0].x, 1.0f - uvs[0].y),
uv1 = new Vector2(uvs[1].x, 1.0f - uvs[2].y),
};
}
return sprInfo;
Expand Down
16 changes: 8 additions & 8 deletions ImGuiNET.Unity/Wrapper.Unity/ImGui.Extra.Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@ public static void Image(Sprite sprite, Vector2 size)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ImageButton(Texture tex)
public static bool ImageButton(Texture tex)
{
ImGui.ImageButton((IntPtr)GetTextureId(tex), new Vector2(tex.width, tex.height));
return ImGui.ImageButton((IntPtr)GetTextureId(tex), new Vector2(tex.width, tex.height));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ImageButton(Texture tex, Vector2 size)
public static bool ImageButton(Texture tex, Vector2 size)
{
ImGui.ImageButton((IntPtr)GetTextureId(tex), size);
return ImGui.ImageButton((IntPtr)GetTextureId(tex), size);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ImageButton(Sprite sprite)
public static bool ImageButton(Sprite sprite)
{
SpriteInfo info = GetSpriteInfo(sprite);
ImGui.ImageButton((IntPtr)GetTextureId(info.texture), info.size, info.uv0, info.uv1);
return ImGui.ImageButton((IntPtr)GetTextureId(info.texture), info.size, info.uv0, info.uv1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ImageButton(Sprite sprite, Vector2 size)
public static bool ImageButton(Sprite sprite, Vector2 size)
{
SpriteInfo info = GetSpriteInfo(sprite);
ImGui.ImageButton((IntPtr)GetTextureId(info.texture), size, info.uv0, info.uv1);
return ImGui.ImageButton((IntPtr)GetTextureId(info.texture), size, info.uv0, info.uv1);
}
}
}
7 changes: 4 additions & 3 deletions ImGuiNET/ColorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using UnityEngine;
using Unity.Collections.LowLevel.Unsafe;

namespace ImGuiNET
{
Expand All @@ -8,19 +9,19 @@ public static class ColorExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Color32 ToColor32(this uint rgba)
{
return Unsafe.AsRef<Color32>(&rgba);
return UnsafeUtility.AsRef<Color32>(&rgba);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Color ToColor(this uint rgba)
{
return Unsafe.AsRef<Color32>(&rgba); // implicit conversion to Color
return UnsafeUtility.AsRef<Color32>(&rgba); // implicit conversion to Color
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe uint ToUint(this Color32 c32)
{
return Unsafe.AsRef<uint>(&c32);
return UnsafeUtility.AsRef<uint>(&c32);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
7 changes: 5 additions & 2 deletions ImGuiNET/Wrapper.Extra/ImFont.Extra.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using UnityEngine;
using Unity.Collections.LowLevel.Unsafe;

namespace ImGuiNET
{
Expand All @@ -26,7 +27,8 @@ public Vector2 CalcTextSizeA(float font_size, float max_width, float wrap_width,
}
else { native_text = null; }
byte* native_text_end = null;
Vector2 ret = ImGuiNative.ImFont_CalcTextSizeA(NativePtr, font_size, max_width, wrap_width, native_text, native_text_end, (byte**)0);
Vector2 ret;
ImGuiNative.ImFont_CalcTextSizeA(&ret, NativePtr, font_size, max_width, wrap_width, native_text, native_text_end, (byte**)0);
if (text_byteCount > Util.StackAllocationSizeLimit)
{
Util.Free(native_text);
Expand All @@ -44,7 +46,8 @@ public Vector2 CalcTextSizeA(float font_size, float max_width, float wrap_width,
int native_text_offset = Encoding.UTF8.GetBytes(&ch, 1, native_text, text_byteCount);
native_text[native_text_offset] = 0;
byte* native_text_end = null;
Vector2 ret = ImGuiNative.ImFont_CalcTextSizeA(NativePtr, font_size, max_width, wrap_width, native_text, native_text_end, (byte**)0);
Vector2 ret;
ImGuiNative.ImFont_CalcTextSizeA(&ret, NativePtr, font_size, max_width, wrap_width, native_text, native_text_end, (byte**)0);
return ret;
}

Expand Down
4 changes: 2 additions & 2 deletions ImGuiNET/Wrapper.Extra/ImFontConfigPtr.Extra.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;

namespace ImGuiNET
{
public unsafe partial struct ImFontConfigPtr
{
public ImFontConfigPtr(ref ImFontConfig fontConfig)
{
NativePtr = (ImFontConfig*)Unsafe.AsPointer(ref fontConfig);
NativePtr = (ImFontConfig*)UnsafeUtility.AddressOf(ref fontConfig);
}
}
}
10 changes: 5 additions & 5 deletions ImGuiNET/Wrapper.Extra/ImGui.Extra.DragDrop.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using System;
using System.Text;
using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;

namespace ImGuiNET
{
// ImGui extra functionality related with Drag and Drop
public static partial class ImGui
{
// TODO: review
// can now pass refs with Unsafe.AsRef
// can now pass refs with UnsafeUtility.AsRef

public static unsafe void SetDragDropPayload<T>(string type, T data, ImGuiCond cond = 0)
where T : unmanaged
{
void* ptr = Unsafe.AsPointer(ref data);
SetDragDropPayload(type, new IntPtr(ptr), (uint)Unsafe.SizeOf<T>(), cond);
void* ptr = UnsafeUtility.AddressOf(ref data);
SetDragDropPayload(type, new IntPtr(ptr), (uint)UnsafeUtility.SizeOf<T>(), cond);
}

public static unsafe bool AcceptDragDropPayload<T>(string type, out T payload, ImGuiDragDropFlags flags = ImGuiDragDropFlags.None)
where T : unmanaged
{
ImGuiPayload* pload = AcceptDragDropPayload(type, flags);
payload = (pload != null) ? Unsafe.Read<T>(pload->Data) : default;
payload = (pload != null) ? UnsafeUtility.ReadArrayElement<T>(pload->Data, 0) : default;
return pload != null;
}

Expand Down
Loading