From 9cbe0d07fc1676aaebd78630a363d446a05776f8 Mon Sep 17 00:00:00 2001 From: Eric Mellino Date: Thu, 9 Feb 2017 01:08:48 -0800 Subject: [PATCH] Implement DrawList functionality, and a few other misc things --- src/ImGui.NET/DrawData.cs | 2 +- src/ImGui.NET/DrawList.cs | 55 +++++++++++++++++- src/ImGui.NET/ImGui.cs | 23 +++++++- src/ImGui.NET/ImGuiNative.cs | 107 ++++++++++++++++++++++++++++++++--- 4 files changed, 177 insertions(+), 10 deletions(-) diff --git a/src/ImGui.NET/DrawData.cs b/src/ImGui.NET/DrawData.cs index 98f2064..9d26d51 100644 --- a/src/ImGui.NET/DrawData.cs +++ b/src/ImGui.NET/DrawData.cs @@ -12,7 +12,7 @@ namespace ImGuiNET /// Only valid after Render() is called and before the next NewFrame() is called. /// public byte Valid; - public DrawList** CmdLists; + public NativeDrawList** CmdLists; public int CmdListsCount; /// /// For convenience, sum of all cmd_lists vtx_buffer.Size diff --git a/src/ImGui.NET/DrawList.cs b/src/ImGui.NET/DrawList.cs index a32add0..824d220 100644 --- a/src/ImGui.NET/DrawList.cs +++ b/src/ImGui.NET/DrawList.cs @@ -1,8 +1,61 @@ using System; +using System.Numerics; using System.Runtime.InteropServices; namespace ImGuiNET { + public unsafe struct DrawList + { + private readonly NativeDrawList* _nativeDrawList; + public DrawList(NativeDrawList* nativeDrawList) + { + _nativeDrawList = nativeDrawList; + } + + public static DrawList GetForCurrentWindow() + { + return new DrawList(ImGuiNative.igGetWindowDrawList()); + } + + public void AddLine(Vector2 a, Vector2 b, uint color, float thickness) + { + ImGuiNative.ImDrawList_AddLine(_nativeDrawList, a, b, color, thickness); + } + + public void AddRect(Vector2 a, Vector2 b, uint color, float rounding, int rounding_corners, float thickness) + { + ImGuiNative.ImDrawList_AddRect(_nativeDrawList, a, b, color, rounding, rounding_corners, thickness); + } + + public void AddRectFilled(Vector2 a, Vector2 b, uint color, float rounding, int rounding_corners = ~0) + { + ImGuiNative.ImDrawList_AddRectFilled(_nativeDrawList, a, b, color, rounding, rounding_corners); + } + + public void AddRectFilledMultiColor( + Vector2 a, + Vector2 b, + uint colorUpperLeft, + uint colorUpperRight, + uint colorBottomRight, + uint colorBottomLeft) + { + ImGuiNative.ImDrawList_AddRectFilledMultiColor( + _nativeDrawList, + a, + b, + colorUpperLeft, + colorUpperRight, + colorBottomRight, + colorBottomLeft); + } + + public void AddCircle(Vector2 center, float radius, uint color, int numSegments, float thickness) + { + ImGuiNative.ImDrawList_AddCircle(_nativeDrawList, center, radius, color, numSegments, thickness); + } + } + /// /// Draw command list /// This is the low-level list of polygons that ImGui functions are filling. At the end of the frame, all command lists are passed to your ImGuiIO::RenderDrawListFn function for rendering. @@ -12,7 +65,7 @@ namespace ImGuiNET /// All positions are in screen coordinates (0,0=top-left, 1 pixel per unit). Primitives are always added to the list and not culled (culling is done at render time and at a higher-level by ImGui:: functions). /// [StructLayout(LayoutKind.Sequential)] - public unsafe struct DrawList + public unsafe struct NativeDrawList { // This is what you have to render diff --git a/src/ImGui.NET/ImGui.cs b/src/ImGui.NET/ImGui.cs index a28d368..982170f 100644 --- a/src/ImGui.NET/ImGui.cs +++ b/src/ImGui.NET/ImGui.cs @@ -147,6 +147,7 @@ namespace ImGuiNET return ImGuiNative.igCollapsingHeader(label, (defaultOpen ? default_open_flags : 0)); } + public static bool CollapsingHeader(string label, TreeNodeFlags flags) { return ImGuiNative.igCollapsingHeader(label, flags); @@ -446,7 +447,7 @@ namespace ImGuiNET { for (int i = 0; i < drawData->CmdListsCount; i++) { - DrawList* cmd_list = drawData->CmdLists[i]; + NativeDrawList* cmd_list = drawData->CmdLists[i]; for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { DrawCmd* drawCmdList = (DrawCmd*)cmd_list->CmdBuffer.Data; @@ -474,6 +475,14 @@ namespace ImGuiNET return size; } + public static Vector2 GetWindowPosition() + { + Vector2 pos; + ImGuiNative.igGetWindowPos(out pos); + return pos; + } + + public static void SetWindowSize(Vector2 size, SetCondition cond = 0) { ImGuiNative.igSetWindowSize(size, cond); @@ -689,6 +698,11 @@ namespace ImGuiNET return ImGuiNative.igIsMouseHoveringAnyWindow(); } + public static bool IsWindowFocused() + { + return ImGuiNative.igIsWindowFocused(); + } + public static bool IsMouseHoveringRect(Vector2 minPosition, Vector2 maxPosition, bool clip) { return IsMouseHoveringRect(minPosition, maxPosition, clip); @@ -744,6 +758,13 @@ namespace ImGuiNET return retVal; } + public static unsafe Vector2 GetCursorScreenPos() + { + Vector2 retVal; + ImGuiNative.igGetCursorScreenPos(&retVal); + return retVal; + } + public static bool BeginChild(string id, bool border = false, WindowFlags flags = 0) { return BeginChild(id, new Vector2(0, 0), border, flags); diff --git a/src/ImGui.NET/ImGuiNative.cs b/src/ImGui.NET/ImGuiNative.cs index 0552238..11e2025 100644 --- a/src/ImGui.NET/ImGuiNative.cs +++ b/src/ImGui.NET/ImGuiNative.cs @@ -69,7 +69,7 @@ namespace ImGuiNET public static extern float igGetWindowContentRegionWidth(); [DllImport(cimguiLib)] - public static extern DrawList* igGetWindowDrawList(); + public static extern NativeDrawList* igGetWindowDrawList(); [DllImport(cimguiLib)] public static extern void igSetWindowFontScale(float scale); [DllImport(cimguiLib)] @@ -783,20 +783,113 @@ namespace ImGuiNET public static extern void ImGuiIO_AddInputCharactersUTF8(string utf8_chars); [DllImport(cimguiLib)] - public static extern int ImDrawList_GetVertexBufferSize(DrawList* list); + public static extern int ImDrawList_GetVertexBufferSize(NativeDrawList* list); [DllImport(cimguiLib)] - public static extern DrawVert* ImDrawList_GetVertexPtr(DrawList* list, int n); + public static extern DrawVert* ImDrawList_GetVertexPtr(NativeDrawList* list, int n); [DllImport(cimguiLib)] - public static extern int ImDrawList_GetIndexBufferSize(DrawList* list); + public static extern int ImDrawList_GetIndexBufferSize(NativeDrawList* list); [DllImport(cimguiLib)] - public static extern ushort* ImDrawList_GetIndexPtr(DrawList* list, int n); + public static extern ushort* ImDrawList_GetIndexPtr(NativeDrawList* list, int n); [DllImport(cimguiLib)] - public static extern int ImDrawList_GetCmdSize(DrawList* list); + public static extern int ImDrawList_GetCmdSize(NativeDrawList* list); [DllImport(cimguiLib)] - public static extern DrawCmd* ImDrawList_GetCmdPtr(DrawList* list, int n); + public static extern DrawCmd* ImDrawList_GetCmdPtr(NativeDrawList* list, int n); [DllImport(cimguiLib)] public static extern void ImDrawData_DeIndexAllBuffers(DrawData* drawData); + [DllImport(cimguiLib)] + public static extern void ImDrawList_Clear(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_ClearFreeMemory(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PushClipRect(NativeDrawList* list, Vector2 clip_rect_min, Vector2 clip_rect_max, byte intersect_with_current_clip_rect); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PushClipRectFullScreen(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PopClipRect(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PushTextureID(NativeDrawList* list, void* texture_id); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PopTextureID(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddLine(NativeDrawList* list, Vector2 a, Vector2 b, uint col, float thickness); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddRect(NativeDrawList* list, Vector2 a, Vector2 b, uint col, float rounding, int rounding_corners, float thickness); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddRectFilled(NativeDrawList* list, Vector2 a, Vector2 b, uint col, float rounding, int rounding_corners); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddRectFilledMultiColor(NativeDrawList* list, Vector2 a, Vector2 b, uint col_upr_left, uint col_upr_right, uint col_bot_right, uint col_bot_left); + [DllImport(cimguiLib)] + public static extern void ImDrawLust_AddQuad(NativeDrawList* list, Vector2 a, Vector2 b, Vector2 c, Vector2 d, uint col, float thickness); + [DllImport(cimguiLib)] + public static extern void ImDrawLust_AddQuadFilled(NativeDrawList* list, Vector2 a, Vector2 b, Vector2 c, Vector2 d, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddTriangle(NativeDrawList* list, Vector2 a, Vector2 b, Vector2 c, uint col, float thickness); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddTriangleFilled(NativeDrawList* list, Vector2 a, Vector2 b, Vector2 c, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddCircle(NativeDrawList* list, Vector2 centre, float radius, uint col, int num_segments, float thickness); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddCircleFilled(NativeDrawList* list, Vector2 centre, float radius, uint col, int num_segments); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddText(NativeDrawList* list, Vector2 pos, uint col, char* text_begin, char* text_end); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddTextExt(NativeDrawList* list, NativeFont* font, float font_size, Vector2 pos, uint col, char* text_begin, char* text_end, float wrap_width, Vector4* cpu_fine_clip_rect); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddImage(NativeDrawList* list, void* user_texture_id, Vector2 a, Vector2 b, Vector2 uv0, Vector2 uv1, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddPolyline(NativeDrawList* list, Vector2* points, int num_points, uint col, byte closed, float thickness, byte anti_aliased); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddConvexPolyFilled(NativeDrawList* list, Vector2* points, int num_points, uint col, byte anti_aliased); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddBezierCurve(NativeDrawList* list, Vector2 pos0, Vector2 cp0, Vector2 cp1, Vector2 pos1, uint col, float thickness, int num_segments); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathClear(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathLineTo(NativeDrawList* list, Vector2 pos); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathLineToMergeDuplicate(NativeDrawList* list, Vector2 pos); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathFill(NativeDrawList* list, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathStroke(NativeDrawList* list, uint col, byte closed, float thickness); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathArcTo(NativeDrawList* list, Vector2 centre, float radius, float a_min, float a_max, int num_segments); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathArcToFast(NativeDrawList* list, Vector2 centre, float radius, int a_min_of_12, int a_max_of_12); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathBezierCurveTo(NativeDrawList* list, Vector2 p1, Vector2 p2, Vector2 p3, int num_segments); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PathRect(NativeDrawList* list, Vector2 rect_min, Vector2 rect_max, float rounding, int rounding_corners); + [DllImport(cimguiLib)] + public static extern void ImDrawList_ChannelsSplit(NativeDrawList* list, int channels_count); + [DllImport(cimguiLib)] + public static extern void ImDrawList_ChannelsMerge(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_ChannelsSetCurrent(NativeDrawList* list, int channel_index); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddCallback(NativeDrawList* list, ImDrawCallback callback, void* callback_data); + [DllImport(cimguiLib)] + public static extern void ImDrawList_AddDrawCmd(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PrimReserve(NativeDrawList* list, int idx_count, int vtx_count); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PrimRect(NativeDrawList* list, Vector2 a, Vector2 b, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PrimRectUV(NativeDrawList* list, Vector2 a, Vector2 b, Vector2 uv_a, Vector2 uv_b, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PrimQuadUV(NativeDrawList* list, Vector2 a, Vector2 b, Vector2 c, Vector2 d, Vector2 uv_a, Vector2 uv_b, Vector2 uv_c, Vector2 uv_d, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PrimVtx(NativeDrawList* list, Vector2 pos, Vector2 uv, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PrimWriteVtx(NativeDrawList* list, Vector2 pos, Vector2 uv, uint col); + [DllImport(cimguiLib)] + public static extern void ImDrawList_PrimWriteIdx(NativeDrawList* list, ushort idx); + [DllImport(cimguiLib)] + public static extern void ImDrawList_UpdateClipRect(NativeDrawList* list); + [DllImport(cimguiLib)] + public static extern void ImDrawList_UpdateTextureID(NativeDrawList* list); } public delegate bool ItemSelectedCallback(IntPtr data, int index, string out_text); + public unsafe delegate void ImDrawCallback(DrawList* parent_list, DrawCmd* cmd); }