2 * Copyright (c) 2023 Codefoco (codefoco@codefoco.com)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 using System.Runtime.InteropServices;
25 using cpDataPointer = System.IntPtr;
26 using cpShape = System.IntPtr;
27 using cpSpaceDebugDrawCircleImpl = System.IntPtr;
28 using cpSpaceDebugDrawColorForShapeImpl = System.IntPtr;
29 using cpSpaceDebugDrawDotImpl = System.IntPtr;
30 using cpSpaceDebugDrawFatSegmentImpl = System.IntPtr;
31 using cpSpaceDebugDrawFlags = System.Int32;
32 using cpSpaceDebugDrawPolygonImpl = System.IntPtr;
33 using cpSpaceDebugDrawSegmentImpl = System.IntPtr;
34 using cpVertPointer = System.IntPtr;
35 using voidptr_t = System.IntPtr;
37 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
41 namespace Tizen.NUI.Physics2D.Chipmunk
44 /// Struct used with Space.DebugDraw() containing drawing callbacks and other drawing settings.
46 [StructLayout(LayoutKind.Sequential)]
47 internal struct cpSpaceDebugDrawOptions
50 /// Function that will be invoked to draw circles.
52 cpSpaceDebugDrawCircleImpl drawCircle;
55 /// Function that will be invoked to draw line segments.
57 cpSpaceDebugDrawSegmentImpl drawSegment;
60 /// Function that will be invoked to draw thick line segments.
62 cpSpaceDebugDrawFatSegmentImpl drawFatSegment;
65 /// Function that will be invoked to draw convex polygons.
67 cpSpaceDebugDrawPolygonImpl drawPolygon;
70 /// Function that will be invoked to draw dots.
72 cpSpaceDebugDrawDotImpl drawDot;
75 /// Flags that request which things to draw (collision shapes, constraints, contact points).
77 cpSpaceDebugDrawFlags flags;
80 /// Outline color passed to the drawing function.
82 DebugColor shapeOutlineColor;
85 /// Function that decides what fill color to draw shapes using.
87 cpSpaceDebugDrawColorForShapeImpl colorForShape;
90 /// Color passed to drawing functions for constraints.
92 DebugColor constraintColor;
95 /// Color passed to drawing functions for collision points.
97 DebugColor collisionPointColor;
100 /// User defined context pointer passed to all of the callback functions as the 'data' argument.
104 private IntPtr ToPointer()
106 IntPtr drawOptionsPtr = NativeInterop.AllocStructure<cpSpaceDebugDrawOptions>();
109 Marshal.StructureToPtr<cpSpaceDebugDrawOptions>(this, drawOptionsPtr, false);
111 catch (Exception exception)
113 Tizen.Log.Fatal("NUI", "[Error] got exception during Marshal.StructureToPtr, this should not occur, message : " + exception.Message);
116 return drawOptionsPtr;
120 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
121 #pragma warning disable CA1416 // Validate platform compatibility
122 [MonoPInvokeCallback(typeof(SpaceDebugDrawCircleImpl))]
123 #pragma warning restore CA1416 // Validate platform compatibility
125 private static void SpaceDebugDrawCircleCallback(Vect pos, double angle, double radius, DebugColor outlineColor, DebugColor fillColor, voidptr_t data)
127 IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
129 debugDraw.DrawCircle(pos, angle, radius, outlineColor, fillColor);
132 private static SpaceDebugDrawCircleImpl spaceDebugDrawCircleCallback = SpaceDebugDrawCircleCallback;
134 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
135 #pragma warning disable CA1416 // Validate platform compatibility
136 [MonoPInvokeCallback(typeof(SpaceDebugDrawSegmentImpl))]
137 #pragma warning restore CA1416 // Validate platform compatibility
139 private static void SpaceDebugDrawSegmentCallback(Vect a, Vect b, DebugColor color, voidptr_t data)
141 IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
143 debugDraw.DrawSegment(a, b, color);
146 private static SpaceDebugDrawSegmentImpl spaceDebugDrawSegmentCallback = SpaceDebugDrawSegmentCallback;
148 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
149 #pragma warning disable CA1416 // Validate platform compatibility
150 [MonoPInvokeCallback(typeof(SpaceDebugDrawFatSegmentImpl))]
151 #pragma warning restore CA1416 // Validate platform compatibility
153 private static void SpaceDebugDrawFatSegmentCallback(Vect a, Vect b, double radius, DebugColor outlineColor, DebugColor fillColor, voidptr_t data)
155 IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
157 debugDraw.DrawFatSegment(a, b, radius, outlineColor, fillColor);
160 private static SpaceDebugDrawFatSegmentImpl spaceDebugDrawFatSegmentCallback = SpaceDebugDrawFatSegmentCallback;
162 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
163 #pragma warning disable CA1416 // Validate platform compatibility
164 [MonoPInvokeCallback(typeof(SpaceDebugDrawPolygonImpl))]
165 #pragma warning restore CA1416 // Validate platform compatibility
167 private static void SpaceDebugDrawPolygonCallback(int count, cpVertPointer verts, double radius, DebugColor outlineColor, DebugColor fillColor, voidptr_t data)
169 IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
171 Vect[] vectors = NativeInterop.PtrToStructureArray<Vect>(verts, count);
173 debugDraw.DrawPolygon(vectors, radius, outlineColor, fillColor);
176 private static SpaceDebugDrawPolygonImpl spaceDebugDrawPolygonCallback = SpaceDebugDrawPolygonCallback;
178 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
179 #pragma warning disable CA1416 // Validate platform compatibility
180 [MonoPInvokeCallback(typeof(SpaceDebugDrawDotImpl))]
181 #pragma warning restore CA1416 // Validate platform compatibility
183 private static void SpaceDebugDrawDotCallback(double size, Vect pos, DebugColor color, voidptr_t data)
185 IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
187 debugDraw.DrawDot(size, pos, color);
190 private static SpaceDebugDrawDotImpl spaceDebugDrawDotCallback = SpaceDebugDrawDotCallback;
192 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
193 #pragma warning disable CA1416 // Validate platform compatibility
194 [MonoPInvokeCallback(typeof(SpaceDebugDrawColorForShapeImpl))]
195 #pragma warning restore CA1416 // Validate platform compatibility
197 private static DebugColor SpaceDebugDrawColorForShapeCallback(cpShape handleShape, voidptr_t data)
199 IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
200 var shape = Shape.FromHandle(handleShape);
202 return debugDraw.ColorForShape(shape);
205 private static SpaceDebugDrawColorForShapeImpl spaceDebugDrawColorForShapeCallback = SpaceDebugDrawColorForShapeCallback;
208 public IntPtr AcquireDebugDrawOptions(IDebugDraw debugDraw, DebugDrawFlags flags, DebugDrawColors colors)
210 this.flags = (int)flags;
211 collisionPointColor = colors.CollisionPoint;
212 constraintColor = colors.Constraint;
213 shapeOutlineColor = colors.ShapeOutline;
215 drawCircle = spaceDebugDrawCircleCallback.ToFunctionPointer();
216 drawSegment = spaceDebugDrawSegmentCallback.ToFunctionPointer();
217 drawFatSegment = spaceDebugDrawFatSegmentCallback.ToFunctionPointer();
218 drawPolygon = spaceDebugDrawPolygonCallback.ToFunctionPointer();
219 drawDot = spaceDebugDrawDotCallback.ToFunctionPointer();
220 colorForShape = spaceDebugDrawColorForShapeCallback.ToFunctionPointer();
222 data = NativeInterop.RegisterHandle(debugDraw);
227 public void ReleaseDebugDrawOptions(IntPtr debugDrawOptionsPointer)
229 NativeInterop.ReleaseHandle(data);
230 NativeInterop.FreeStructure(debugDrawOptionsPointer);