Skip to content

Commit e0ee474

Browse files
committed
Added node helpers
Removed redundant checks in graphics methods
1 parent c5e4752 commit e0ee474

File tree

6 files changed

+130
-35
lines changed

6 files changed

+130
-35
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ You can check the table below to see which version of SciterAPI is compatible wi
7272
* ~~Binding for Request API~~
7373
* ~~Get/Set Variables~~
7474
* ~~Helpers for Window Management~~
75+
* ~~Helpers for Node API~~
7576
* Support SOM mapping
7677
* Helpers for Graphics API

src/EmptyFlow.SciterAPI/Client/HostGraphicsAPI.cs

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using EmptyFlow.SciterAPI.Enums;
33
using EmptyFlow.SciterAPI.Structs;
44
using System.Numerics;
5+
using System.Runtime.CompilerServices;
56
using System.Runtime.InteropServices;
67

78
namespace EmptyFlow.SciterAPI {
@@ -12,12 +13,14 @@ public partial class SciterAPIHost {
1213
/// Redraw element.
1314
/// </summary>
1415
/// <param name="element">Element.</param>
16+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
1517
public void RedrawElement ( nint element ) => m_basicApi.SciterUpdateElement ( element, false );
1618

1719
/// <summary>
1820
/// Force rendering element.
1921
/// </summary>
2022
/// <param name="element">Element.</param>
23+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
2124
public void ForceRenderElement ( nint element ) => m_basicApi.SciterUpdateElement ( element, true );
2225

2326
private bool CheckGraphics () {
@@ -64,9 +67,8 @@ public void GraphicsPerformInState ( nint hgfx, Action callback ) {
6467
/// </summary>
6568
/// <param name="hgfx">Pointer on graphics surface.</param>
6669
/// <param name="color">Line color. You need to get color from GraphicsGetColor method.</param>
70+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
6771
public void GraphicsLineColor ( nint hgfx, uint color ) {
68-
if ( !CheckGraphics () ) return;
69-
7072
m_graphicsApi.gLineColor ( hgfx, color );
7173
}
7274

@@ -80,9 +82,8 @@ public void GraphicsLineColor ( nint hgfx, uint color ) {
8082
/// <param name="y2">Y2 coordinate.</param>
8183
/// <param name="stops">Color stops.</param>
8284
/// <param name="nstops">No idea what is it.</param>
85+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
8386
public void GraphicsLineGradientLinear ( nint hgfx, float x1, float y1, float x2, float y2, ColorStop[] stops, uint nstops ) {
84-
if ( !CheckGraphics () ) return;
85-
8687
m_graphicsApi.gLineGradientLinear ( hgfx, x1, y1, x2, y2, stops, nstops );
8788
}
8889

@@ -96,9 +97,8 @@ public void GraphicsLineGradientLinear ( nint hgfx, float x1, float y1, float x2
9697
/// <param name="y2">Y2 coordinate.</param>
9798
/// <param name="stops">Color stops.</param>
9899
/// <param name="nstops">No idea what is it.</param>
100+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
99101
public void GraphicsLineGradientRadial ( nint hgfx, float x1, float y1, float x2, float y2, ColorStop[] stops, uint nstops ) {
100-
if ( !CheckGraphics () ) return;
101-
102102
m_graphicsApi.gLineGradientRadial ( hgfx, x1, y1, x2, y2, stops, nstops );
103103
}
104104

@@ -107,9 +107,8 @@ public void GraphicsLineGradientRadial ( nint hgfx, float x1, float y1, float x2
107107
/// </summary>
108108
/// <param name="hgfx">Pointer on graphics surface.</param>
109109
/// <param name="color">Line color. You need to get color from GraphicsGetColor method.</param>
110+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
110111
public void GraphicsFillColor ( nint hgfx, uint color ) {
111-
if ( !CheckGraphics () ) return;
112-
113112
m_graphicsApi.gFillColor ( hgfx, color );
114113
}
115114

@@ -123,9 +122,8 @@ public void GraphicsFillColor ( nint hgfx, uint color ) {
123122
/// <param name="y2">Y2 coordinate.</param>
124123
/// <param name="stops">Color stops.</param>
125124
/// <param name="nstops">No idea what is it.</param>
125+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
126126
public void GraphicsFillGradientLinear ( nint hgfx, float x1, float y1, float x2, float y2, ColorStop[] stops, uint nstops ) {
127-
if ( !CheckGraphics () ) return;
128-
129127
m_graphicsApi.gFillGradientLinear ( hgfx, x1, y1, x2, y2, stops, nstops );
130128
}
131129

@@ -139,9 +137,8 @@ public void GraphicsFillGradientLinear ( nint hgfx, float x1, float y1, float x2
139137
/// <param name="y2">Y2 coordinate.</param>
140138
/// <param name="stops">Color stops.</param>
141139
/// <param name="nstops">No idea what is it.</param>
140+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
142141
public void GraphicsFillGradientRadial ( nint hgfx, float x1, float y1, float x2, float y2, ColorStop[] stops, uint nstops ) {
143-
if ( !CheckGraphics () ) return;
144-
145142
m_graphicsApi.gFillGradientRadial ( hgfx, x1, y1, x2, y2, stops, nstops );
146143
}
147144

@@ -150,9 +147,8 @@ public void GraphicsFillGradientRadial ( nint hgfx, float x1, float y1, float x2
150147
/// </summary>
151148
/// <param name="hgfx">Pointer on graphics surface.</param>
152149
/// <param name="width">New width for the line.</param>
150+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
153151
public void GraphicsSetLineWidth ( nint hgfx, float width ) {
154-
if ( !CheckGraphics () ) return;
155-
156152
m_graphicsApi.gLineWidth ( hgfx, width );
157153
}
158154

@@ -164,12 +160,54 @@ public void GraphicsSetLineWidth ( nint hgfx, float width ) {
164160
/// <param name="y1">Y1 coordinate.</param>
165161
/// <param name="x2">X2 coordinate.</param>
166162
/// <param name="y2">Y2 coordinate.</param>
163+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
167164
public void GraphicsDrawLine ( nint hgfx, float x1, float y1, float x2, float y2 ) {
168-
if ( !CheckGraphics () ) return;
169-
170165
m_graphicsApi.gLine ( hgfx, x1, y1, x2, y2 );
171166
}
172167

168+
public nint GraphicsPath ( Action<nint> callback ) {
169+
var result = m_graphicsApi.pathCreate ( out var path );
170+
if ( result != GraphInResult.Ok ) return path;
171+
172+
callback ( path );
173+
174+
m_graphicsApi.pathClosePath ( path );
175+
176+
return nint.Zero;
177+
}
178+
179+
public bool GraphicsDrawPath ( nint hgfx, nint path, DrawPathMode pathMode ) {
180+
var result = m_graphicsApi.gDrawPath ( hgfx, path, pathMode );
181+
if ( result == GraphInResult.Ok ) return true;
182+
183+
return false;
184+
}
185+
186+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
187+
public void GraphicsPathLineTo ( nint path, SciterPoint position, bool relative ) {
188+
m_graphicsApi.pathLineTo ( path, position.X, position.Y, relative );
189+
}
190+
191+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
192+
public void GraphicsPathMoveTo ( nint path, SciterPoint position, bool relative ) {
193+
m_graphicsApi.pathMoveTo ( path, position.X, position.Y, relative );
194+
}
195+
196+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
197+
public void GraphicsPathArcTo ( nint path, SciterPoint position, float angle, SciterPoint radius, bool largeArc, bool clockwise, bool relative ) {
198+
m_graphicsApi.pathArcTo ( path, position.X, position.Y, angle, radius.X, radius.Y, largeArc, clockwise, relative );
199+
}
200+
201+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
202+
public void GraphicsPathQuadraticCurveTo ( nint path, float xc, float yc, float x, float y, bool relative ) {
203+
m_graphicsApi.pathQuadraticCurveTo ( path, xc, yc, x, y, relative );
204+
}
205+
206+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
207+
public void GraphicsPathQuadraticCurveTo ( nint path, float xc1, float yc1, float xc2, float yc2, float x, float y, bool relative ) {
208+
m_graphicsApi.pathBezierCurveTo ( path, xc1, yc1, xc2, yc2, x, y, relative );
209+
}
210+
173211
/// <summary>
174212
/// Draws line from x1,y1 to x2,y2 with width and color.
175213
/// </summary>
@@ -179,8 +217,6 @@ public void GraphicsDrawLine ( nint hgfx, float x1, float y1, float x2, float y2
179217
/// <param name="x2">X2 coordinate.</param>
180218
/// <param name="y2">Y2 coordinate.</param>
181219
public void GraphicsDrawLine ( nint hgfx, Vector2 start, Vector2 end, uint color, float width ) {
182-
if ( !CheckGraphics () ) return;
183-
184220
m_graphicsApi.gLineColor ( hgfx, color );
185221
m_graphicsApi.gLineWidth ( hgfx, width );
186222
m_graphicsApi.gLine ( hgfx, start.X, start.Y, end.X, end.Y );
@@ -194,9 +230,8 @@ public void GraphicsDrawLine ( nint hgfx, Vector2 start, Vector2 end, uint color
194230
/// <param name="y1">Y1 coordinate.</param>
195231
/// <param name="rx">Radius X.</param>
196232
/// <param name="ry">Radius Y.</param>
233+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
197234
public void GraphicsDrawEllipse ( nint hgfx, float x1, float y1, float rx, float ry ) {
198-
if ( !CheckGraphics () ) return;
199-
200235
m_graphicsApi.gEllipse ( hgfx, x1, y1, rx, ry );
201236
}
202237

@@ -208,9 +243,8 @@ public void GraphicsDrawEllipse ( nint hgfx, float x1, float y1, float rx, float
208243
/// <param name="y1">Y1 coordinate.</param>
209244
/// <param name="x2">X2 coordinate.</param>
210245
/// <param name="y2">Y2 coordinate.</param>
246+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
211247
public void GraphicsDrawRectangle ( nint hgfx, float x1, float y1, float x2, float y2 ) {
212-
if ( !CheckGraphics () ) return;
213-
214248
m_graphicsApi.gRectangle ( hgfx, x1, y1, x2, y2 );
215249
}
216250

@@ -223,9 +257,8 @@ public void GraphicsDrawRectangle ( nint hgfx, float x1, float y1, float x2, flo
223257
/// <param name="x2">X2 coordinate.</param>
224258
/// <param name="y2">Y2 coordinate.</param>
225259
/// <param name="radii8">8 pair array.</param>
260+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
226261
public void GraphicsDrawRoundedRectangle ( nint hgfx, float x1, float y1, float x2, float y2, float[] radii8 ) {
227-
if ( !CheckGraphics () ) return;
228-
229262
m_graphicsApi.gRoundedRectangle ( hgfx, x1, y1, x2, y2, radii8 );
230263
}
231264

@@ -269,21 +302,17 @@ public void GraphicsReleaseText ( GraphicsTextModel text ) {
269302
/// <param name="y">Y coordinate.</param>
270303
/// <param name="coordinates">Position (1..9).</param>
271304
public void GraphicsDrawText ( nint hgfx, GraphicsTextModel text, Vector2 coordinates, SciterTextPosition position ) {
272-
if ( !CheckGraphics () ) return;
273-
274305
var graphResult = m_graphicsApi.gDrawText ( hgfx, text.Id, coordinates.X, coordinates.Y, (uint) position );
275306
if ( graphResult != GraphInResult.Ok ) Console.WriteLine ( "GraphicsDrawText resulted with error, actual result - " + graphResult );
276307
}
277308

309+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
278310
public void GraphicsDrawImage ( nint hgfx, nint image, float x, float y, float w, float h, uint ix, uint iy, uint iw, uint ih, float opacity ) {
279-
if ( !CheckGraphics () ) return;
280-
281311
m_graphicsApi.gDrawImage ( hgfx, image, x, y, w, h, ix, iy, iw, ih, opacity );
282312
}
283313

314+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
284315
public void GraphicsDrawImage ( nint hgfx, nint image, Vector2 position, Vector2 size, uint ix, uint iy, uint iw, uint ih, float opacity ) {
285-
if ( !CheckGraphics () ) return;
286-
287316
m_graphicsApi.gDrawImage ( hgfx, image, position.X, position.Y, position.X, position.Y, ix, iy, iw, ih, opacity );
288317
}
289318

@@ -294,10 +323,9 @@ public void GraphicsDrawImage ( nint hgfx, nint image, Vector2 position, Vector2
294323
/// <param name="g">Green.</param>
295324
/// <param name="b">Blue.</param>
296325
/// <param name="a">Alpha, be default is 255.</param>
326+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
297327
public uint GraphicsGetColor ( uint r, uint g, uint b, uint a = 255 ) {
298-
if ( CheckGraphics () ) return m_graphicsApi.RGBA ( r, g, b, a );
299-
300-
return 0;
328+
return m_graphicsApi.RGBA ( r, g, b, a );
301329
}
302330

303331
}

src/EmptyFlow.SciterAPI/Client/HostNodeAPI.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using EmptyFlow.SciterAPI.Structs;
1+
using EmptyFlow.SciterAPI.Enums;
2+
using EmptyFlow.SciterAPI.Structs;
23
using System.Runtime.InteropServices;
34
using System.Text;
45

@@ -139,6 +140,52 @@ public int NodeChildrenCount ( nint node ) {
139140
return 0;
140141
}
141142

143+
/// <summary>
144+
/// Cast node from element.
145+
/// </summary>
146+
/// <param name="element">Element.</param>
147+
public nint NodeCastFromElement ( nint element ) {
148+
var domResult = m_basicApi.SciterNodeCastFromElement ( element, out var node );
149+
if ( domResult == DomResult.SCDOM_OK ) return node;
150+
151+
return 0;
152+
}
153+
154+
/// <summary>
155+
/// Insert node to another node.
156+
/// </summary>
157+
/// <param name="node">Node/Element.</param>
158+
/// <param name="target">Target location.</param>
159+
/// <param name="what">What node/element need to insert.</param>
160+
public bool NodeInsert ( nint node, NodeInsertTarget target, nint what ) {
161+
var domResult = m_basicApi.SciterNodeInsert ( node, (uint) target, what );
162+
if ( domResult == DomResult.SCDOM_OK ) return true;
163+
164+
return false;
165+
}
166+
167+
/// <summary>
168+
/// Create new text node.
169+
/// </summary>
170+
/// <param name="element">Element.</param>
171+
public nint NodeCreateTextNode ( string content ) {
172+
var domResult = m_basicApi.SciterCreateTextNode ( content, (uint)content.Length, out var node );
173+
if ( domResult == DomResult.SCDOM_OK ) return node;
174+
175+
return nint.Zero;
176+
}
177+
178+
/// <summary>
179+
/// Create new comment node.
180+
/// </summary>
181+
/// <param name="element">Element.</param>
182+
public nint NodeCreateCommentNode ( string content ) {
183+
var domResult = m_basicApi.SciterCreateCommentNode ( content, (uint) content.Length, out var node );
184+
if ( domResult == DomResult.SCDOM_OK ) return node;
185+
186+
return nint.Zero;
187+
}
188+
142189
}
143190

144191
}

src/EmptyFlow.SciterAPI/EmptyFlow.SciterAPI.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
SciterAPIHost.NodeParent get node type
2929
SciterAPIHost.NodeNthChild get node type
3030
SciterAPIHost.NodeChildrenCount get node type
31+
SciterAPIHost.NodeCastFromElement cast element to node
32+
SciterAPIHost.NodeCastToElement cast node to element
33+
SciterAPIHost.NodeInsert
34+
SciterAPIHost.NodeCreateTextNode
3135
SciterValue.ToBoolean convert to C# boolean value
3236
</PackageReleaseNotes>
3337
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace EmptyFlow.SciterAPI.Enums {
2+
3+
public enum NodeInsertTarget { // original name NODE_INS_TARGET
4+
5+
Before = 0, // NIT_BEFORE
6+
7+
After, // NIT_AFTER
8+
9+
Append, // NIT_APPEND
10+
11+
Prepend, // NIT_PREPEND
12+
13+
}
14+
15+
}

src/EmptyFlow.SciterAPI/Structs/GraphicsApiStruct.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace EmptyFlow.SciterAPI.Structs {
2626
public delegate GraphInResult GraphicsGPolygon ( IntPtr hgfx, float[] xy, uint numPoints );
2727
public delegate GraphInResult GraphicsGPolyline ( IntPtr hgfx, float[] xy, uint numPoints );
2828

29-
public delegate GraphInResult GraphicsPathCreate ( IntPtr path );
29+
public delegate GraphInResult GraphicsPathCreate ( out nint path );
3030
public delegate GraphInResult GraphicsPathAddRef ( IntPtr path );
3131
public delegate GraphInResult GraphicsPathRelease ( IntPtr path );
3232
public delegate GraphInResult GraphicsPathMoveTo ( IntPtr path, float x, float y, bool relative );

0 commit comments

Comments
 (0)