[NUI] Fix some SVACE issues related to methods of Marshal.
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Physics2D / src / internal / chipmunk / cpSpaceDebugDrawOptions.cs
1 /*
2  * Copyright (c) 2023 Codefoco (codefoco@codefoco.com)
3  *
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:
10  * 
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  * 
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
20  * SOFTWARE.
21  */
22
23 using System;
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;
36
37 #if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
38 using ObjCRuntime;
39 #endif
40
41 namespace Tizen.NUI.Physics2D.Chipmunk
42 {
43     /// <summary>
44     /// Struct used with Space.DebugDraw() containing drawing callbacks and other drawing settings.
45     /// </summary>
46     [StructLayout(LayoutKind.Sequential)]
47     internal struct cpSpaceDebugDrawOptions
48     {
49         /// <summary>
50         /// Function that will be invoked to draw circles.
51         /// </summary>
52         cpSpaceDebugDrawCircleImpl drawCircle;
53
54         /// <summary>
55         /// Function that will be invoked to draw line segments.
56         /// </summary>
57         cpSpaceDebugDrawSegmentImpl drawSegment;
58
59         /// <summary>
60         /// Function that will be invoked to draw thick line segments.
61         /// </summary>
62         cpSpaceDebugDrawFatSegmentImpl drawFatSegment;
63
64         /// <summary>
65         /// Function that will be invoked to draw convex polygons.
66         /// </summary>
67         cpSpaceDebugDrawPolygonImpl drawPolygon;
68
69         /// <summary>
70         /// Function that will be invoked to draw dots.
71         /// </summary>
72         cpSpaceDebugDrawDotImpl drawDot;
73
74         /// <summary>
75         /// Flags that request which things to draw (collision shapes, constraints, contact points).
76         /// </summary>
77         cpSpaceDebugDrawFlags flags;
78
79         /// <summary>
80         /// Outline color passed to the drawing function.
81         /// </summary>
82         DebugColor shapeOutlineColor;
83
84         /// <summary>
85         /// Function that decides what fill color to draw shapes using.
86         /// </summary>
87         cpSpaceDebugDrawColorForShapeImpl colorForShape;
88
89         /// <summary>
90         /// Color passed to drawing functions for constraints.
91         /// </summary>
92         DebugColor constraintColor;
93
94         /// <summary>
95         /// Color passed to drawing functions for collision points.
96         /// </summary>
97         DebugColor collisionPointColor;
98
99         /// <summary>
100         /// User defined context pointer passed to all of the callback functions as the 'data' argument.
101         /// </summary>
102         cpDataPointer data;
103
104         private IntPtr ToPointer()
105         {
106             IntPtr drawOptionsPtr = NativeInterop.AllocStructure<cpSpaceDebugDrawOptions>();
107             try
108             {
109                 Marshal.StructureToPtr<cpSpaceDebugDrawOptions>(this, drawOptionsPtr, false);
110             }
111             catch (Exception exception)
112             {
113                 Tizen.Log.Fatal("NUI", "[Error] got exception during Marshal.StructureToPtr, this should not occur, message : " + exception.Message);
114             }
115       
116             return drawOptionsPtr;
117         }
118
119
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
124 #endif
125         private static void SpaceDebugDrawCircleCallback(Vect pos, double angle, double radius, DebugColor outlineColor, DebugColor fillColor, voidptr_t data)
126         {
127             IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
128
129             debugDraw.DrawCircle(pos, angle, radius, outlineColor, fillColor);
130         }
131
132         private static SpaceDebugDrawCircleImpl spaceDebugDrawCircleCallback = SpaceDebugDrawCircleCallback;
133
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
138 #endif
139         private static void SpaceDebugDrawSegmentCallback(Vect a, Vect b, DebugColor color, voidptr_t data)
140         {
141             IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
142
143             debugDraw.DrawSegment(a, b, color);
144         }
145
146         private static SpaceDebugDrawSegmentImpl spaceDebugDrawSegmentCallback = SpaceDebugDrawSegmentCallback;
147
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
152 #endif
153         private static void SpaceDebugDrawFatSegmentCallback(Vect a, Vect b, double radius, DebugColor outlineColor, DebugColor fillColor, voidptr_t data)
154         {
155             IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
156
157             debugDraw.DrawFatSegment(a, b, radius, outlineColor, fillColor);
158         }
159
160         private static SpaceDebugDrawFatSegmentImpl spaceDebugDrawFatSegmentCallback = SpaceDebugDrawFatSegmentCallback;
161
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
166 #endif
167         private static void SpaceDebugDrawPolygonCallback(int count, cpVertPointer verts, double radius, DebugColor outlineColor, DebugColor fillColor, voidptr_t data)
168         {
169             IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
170
171             Vect[] vectors = NativeInterop.PtrToStructureArray<Vect>(verts, count);
172
173             debugDraw.DrawPolygon(vectors, radius, outlineColor, fillColor);
174         }
175
176         private static SpaceDebugDrawPolygonImpl spaceDebugDrawPolygonCallback = SpaceDebugDrawPolygonCallback;
177
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
182 #endif
183         private static void SpaceDebugDrawDotCallback(double size, Vect pos, DebugColor color, voidptr_t data)
184         {
185             IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
186
187             debugDraw.DrawDot(size, pos, color);
188         }
189
190         private static SpaceDebugDrawDotImpl spaceDebugDrawDotCallback = SpaceDebugDrawDotCallback;
191
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
196 #endif
197         private static DebugColor SpaceDebugDrawColorForShapeCallback(cpShape handleShape, voidptr_t data)
198         {
199             IDebugDraw debugDraw = NativeInterop.FromIntPtr<IDebugDraw>(data);
200             var shape = Shape.FromHandle(handleShape);
201
202             return debugDraw.ColorForShape(shape);
203         }
204
205         private static SpaceDebugDrawColorForShapeImpl spaceDebugDrawColorForShapeCallback = SpaceDebugDrawColorForShapeCallback;
206
207
208         public IntPtr AcquireDebugDrawOptions(IDebugDraw debugDraw, DebugDrawFlags flags, DebugDrawColors colors)
209         {
210             this.flags = (int)flags;
211             collisionPointColor = colors.CollisionPoint;
212             constraintColor = colors.Constraint;
213             shapeOutlineColor = colors.ShapeOutline;
214
215             drawCircle = spaceDebugDrawCircleCallback.ToFunctionPointer();
216             drawSegment = spaceDebugDrawSegmentCallback.ToFunctionPointer();
217             drawFatSegment = spaceDebugDrawFatSegmentCallback.ToFunctionPointer();
218             drawPolygon = spaceDebugDrawPolygonCallback.ToFunctionPointer();
219             drawDot = spaceDebugDrawDotCallback.ToFunctionPointer();
220             colorForShape = spaceDebugDrawColorForShapeCallback.ToFunctionPointer();
221
222             data = NativeInterop.RegisterHandle(debugDraw);
223
224             return ToPointer();
225         }
226
227         public void ReleaseDebugDrawOptions(IntPtr debugDrawOptionsPointer)
228         {
229             NativeInterop.ReleaseHandle(data);
230             NativeInterop.FreeStructure(debugDrawOptionsPointer);
231         }
232     }
233 }