Skip to content

Commit cc889c5

Browse files
committed
Added latest tests
kept writing on API
1 parent 79f7264 commit cc889c5

File tree

4 files changed

+171
-19
lines changed

4 files changed

+171
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ $RECYCLE.BIN/
4545
Network Trash Folder
4646
Temporary Items
4747
.apdisk
48+
49+
# Tests for testdriven development
50+
tdd*.ahk

Mail Test.ahk

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#Include classGDIp.ahk
2+
#NoTrayIcon
3+
API := new GDIp()
4+
5+
size := [ 1000, 600 ]
6+
rect := size.clone(), rect.insertAt( 1, 0, 0 )
7+
8+
GUI, new
9+
GUI, +hwndGUI1 -Caption +ToolWindow + Border
10+
Gui,Show,% "w" . size.1 . " h" . size.2
11+
SetFormat ,IntegerFast ,H
12+
13+
hDC := new GDI.DC( GUI1 )
14+
testGraphics := hDC.getGraphics()
15+
testGraphics.setSmoothingMode( 4 )
16+
testGraphics.setInterpolationMode( 7 )
17+
18+
testBrush := new GDIp.Brush.LinearGradientBrush( [ 0, 0 ], [ 0, 0, size.1 / 2, size.2 / 2 ], 0, 1 )
19+
testPen := new GDIp.Pen( testBrush, 5 )
20+
GoTo,Paint
21+
22+
F5::
23+
Paint:
24+
Critical
25+
testBrush.setColor( color := [ randomColor(), randomColor() ] )
26+
testPen.setBrush( testBrush )
27+
testGraphics.clear( 0xFF000000 )
28+
testGraphics.drawLines( testPen, [[0,0],[size.1/2,size.2/2],[ size.1, 0 ]] )
29+
return
30+
31+
~LButton::
32+
If WinActive( "ahk_id " . GUI1 )
33+
GoTo, Paint
34+
esc::
35+
GUIClose:
36+
ExitApp
37+
38+
randomColor( A := 255 )
39+
{
40+
if ( A == "" )
41+
{
42+
Msgbox % A
43+
Random,A,0,255
44+
A := Round( A * A/255 * A/255 * A/255 )
45+
}
46+
Random,R,0,255
47+
Random,G,0,255
48+
Random,B,0,255
49+
return ( A << 24 ) | ( R << 16 ) | ( G << 8 ) | B
50+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ With the exception of those that make no sense in AutoHotkeys context.
1818
* __class nesting__ all classes should either be nested within the class GDIp or any of it's subclasses.
1919
* __the ptr field and getp*ObjectType*__ Any class that represents a GDIp Object should store it's objects pointer in `This.ptr`.
2020
It should also return this pointer upon calling `getpObjectType()` where `ObjectType` get's replaced by the Objects type ( e.g. `getpBitmap()` for Bitmaps ).
21-
* __parameters__ The biggest issue is making parameters both consistent and easy to use. Parameters should represent attributes that can be set e.g. Size
21+
* __parameters__ The biggest issue is making parameters both consistent and easy to use. Parameters should represent attributes that can be set e.g. Size.
22+
If the parameter covers more than one value combine them to an array. Never split parameters. Never use byref or global.
23+
* __getters and setters__ Every attribute that needs to be set or get dynamically shouls be set and get by using setter and getter methods.

classGDIp.ahk

Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,15 @@ class GDIp
6464
class Bitmap
6565
{
6666

67-
__New( filePathOrW, h = "" )
67+
__New( filePathOrSize )
6868
{
69-
if fileExist( filePathOrW )
70-
{
71-
ret := DllCall( "gdiplus\GdipCreateBitmapFromFile", "WStr", filePathOrW, "Ptr*", pBitmap )
72-
DllCall( "gdiplus\GdipGetImageWidth", "Ptr", pBitmap, "UInt*", w )
73-
DllCall( "gdiplus\GdipGetImageHeight", "Ptr", pBitmap, "UInt*", h )
74-
}
75-
else if ( ( w := filePathOrW ) > 0 && h > 0 )
76-
ret := DllCall( "gdiplus\GdipCreateBitmapFromScan0", "UInt", w, "UInt", h, "UInt", 0, "UInt", 0x26200A, "Ptr", 0, "Ptr*", pBitmap )
77-
if !( ret = 0 )
69+
if isObject( filePathOrSize )
70+
ret := DllCall( "gdiplus\GdipCreateBitmapFromScan0", "UInt", filePathOrSize.1, "UInt", filePathOrSize.2, "UInt", 0, "UInt", 0x26200A, "Ptr", 0, "Ptr*", pBitmap )
71+
else if fileExist( filePathOrSize )
72+
ret := DllCall( "gdiplus\GdipCreateBitmapFromFile", "WStr", filePathOrSize, "Ptr*", pBitmap )
73+
if ret
7874
return ret
7975
This.ptr := pBitmap
80-
This.w := w
81-
This.h := h
8276
GDIp.registerObject( This )
8377
}
8478

@@ -107,7 +101,9 @@ class GDIp
107101

108102
getSize()
109103
{
110-
return [ This.w, This.h ]
104+
DllCall( "gdiplus\GdipGetImageWidth", "Ptr", pBitmap, "UInt*", w )
105+
DllCall( "gdiplus\GdipGetImageHeight", "Ptr", pBitmap, "UInt*", h )
106+
return [ w, h ]
111107
}
112108

113109
saveToFile( fileName )
@@ -220,12 +216,12 @@ class GDIp
220216

221217
fillRectangle( brush, rect )
222218
{
223-
return DllCall( "gdiplus\GdipFillRectangle", "Ptr", This.ptr, "Ptr", isObject( brush ) ? brush.getpBrush() : brush, "float", rect.1, "float", rect.2, "float", rect.3, "float", rect.4 )
219+
return DllCall( "gdiplus\GdipFillRectangle", "Ptr", This.ptr, "Ptr", ? brush.getpBrush(), "float", rect.1, "float", rect.2, "float", rect.3, "float", rect.4 )
224220
}
225221

226222
fillElipse( brush, rect )
227223
{
228-
return DllCall("gdiplus\GdipFillEllipse", "Ptr", This.ptr, "Ptr", isObject( brush ) ? brush.getpBrush() : brush, "float", rect.1, "float", rect.2, "float", rect.3, "float", rect.4 )
224+
return DllCall("gdiplus\GdipFillEllipse", "Ptr", This.ptr, "Ptr", brush.getpBrush(), "float", rect.1, "float", rect.2, "float", rect.3, "float", rect.4 )
229225
}
230226

231227
/*
@@ -244,9 +240,40 @@ class GDIp
244240
NumPut( point.1, pointBuffer, pointNr * 8 - 8, "float" )
245241
NumPut( point.2, pointBuffer, pointNr * 8 - 4, "float" )
246242
}
247-
return DllCall( "gdiplus\GdipFillPolygon", "Ptr", This.ptr, "Ptr", isObject( brush ) ? brush.getpBrush() : brush, "Ptr", &pointBuffer, "int", points.Length(), "int", fillMode )
243+
return DllCall( "gdiplus\GdipFillPolygon", "Ptr", This.ptr, "Ptr", brush.getpBrush(), "Ptr", &pointBuffer, "int", points.Length(), "int", fillMode )
248244
}
249245

246+
/*
247+
Brush: the brush used to fill the Pie
248+
249+
Rect: a 4 value array defining the Area the pie occupies
250+
251+
Angles: a 2 value array defining the starting and the ending angle of the pie
252+
*/
253+
254+
fillPie( brush, rect, angles )
255+
{
256+
return DllCall("gdiplus\GdipFillPie", "Ptr", This.ptr, "Ptr", brush.getpBrush(), "float", rect.1, "float", rect.2, "float", rect.3, "float", rect.4, "float", angles.1, "float", angles.2 )
257+
}
258+
259+
/*
260+
Pen: the pen used to draw the line
261+
262+
Points: the starting and the end point in a 2x2 array
263+
264+
*/
265+
266+
drawLine( pen, points )
267+
{
268+
return DllCall( "gdiplus\GdipDrawLine", "UPtr", This.ptr, "UPtr", pen.getpPen(), "float", points.1.1, "float", points.1.2, "float", points.2.1, "float", points.2.2 )
269+
}
270+
271+
drawLines( pen, points )
272+
{
273+
points := points.clone()
274+
Loop % points.Length() - 1
275+
This.drawLine( pen, points ), points.removeAt( 1 )
276+
}
250277

251278
}
252279

@@ -278,7 +305,7 @@ class GDIp
278305
DllCall( "gdiplus\GdipSetImageAttributesColorMatrix", "UPtr", This.ptr, "Int", 1, "Int", 1, "UPtr", &colourMatrix, "UPtr", 0, "Int", 0 )
279306
}
280307

281-
getpImageAttribute()
308+
getpImageAttributes()
282309
{
283310
return This.ptr
284311
}
@@ -393,7 +420,7 @@ class GDIp
393420
getColor()
394421
{
395422
VarSetCapacity( colors, 8, 0 )
396-
DllCall( "gdiplus\GdipGetLineColors", "UPtr", This.ptr, "Ptr", colors )
423+
DllCall( "gdiplus\GdipGetLineColors", "UPtr", This.ptr, "Ptr", &colors )
397424
return [ numGet( colors, 0, "UInt" ), numGet( colors, 4, "UInt" ) ]
398425
}
399426

@@ -402,6 +429,76 @@ class GDIp
402429

403430
}
404431

432+
class Pen
433+
{
434+
__New( brushOrColor, width )
435+
{
436+
if isObject( brushOrColor )
437+
ret := DllCall( "gdiplus\GdipCreatePen2", "UPtr", brushOrColor.getpBrush(), "float", width, "int", 2, "UPtr*", pPen )
438+
else
439+
ret := DllCall( "gdiplus\GdipCreatePen1", "UInt", ARGB, "float", width, "Int", 2, "UPtr*", pPen )
440+
if ret
441+
return ret
442+
This.ptr := pPen
443+
GDIp.registerObject( This )
444+
}
445+
446+
__Delete()
447+
{
448+
DllCall( "gdiplus\GdipDeletePen", "UPtr", This.ptr )
449+
GDIp.unregisterObject( This )
450+
This.base := ""
451+
}
452+
453+
clone()
454+
{
455+
ret := DllCall( "gdiplus\GdipClonePen", "UPtr", This.ptr, "UPtr*", pPen )
456+
if ret
457+
return ret
458+
newPen := { ptr: pPen ,base: GDIp.pen }
459+
GDIp.registerObject( newPen )
460+
return newPen
461+
}
462+
463+
getpPen()
464+
{
465+
return This.ptr
466+
}
467+
468+
setWidth( width )
469+
{
470+
return DllCall( "gdiplus\GdipSetPenWidth", "UPtr", This.ptr, "float", width )
471+
}
472+
473+
getWidth()
474+
{
475+
DllCall( "gdiplus\GdipGetPenWidth", "UPtr", This.ptr, "float*", width )
476+
return width
477+
}
478+
479+
setColor( color )
480+
{
481+
return DllCall( "gdiplus\GdipSetPenColor", "UPtr", Ths.ptr, "UInt", color )
482+
}
483+
484+
getColor()
485+
{
486+
DllCall( "gdiplus\GdipGetPenColor", "UPtr", This.ptr, "UInt*", color )
487+
return color
488+
}
489+
490+
setBrush( brush )
491+
{
492+
return DllCall( "gdiplus\GdipSetPenBrushFill", "UPtr", This.ptr, "UPtr", brush.getpBrush() )
493+
}
494+
495+
getBrush()
496+
{
497+
return
498+
}
499+
500+
}
501+
405502
registerObject( Object )
406503
{
407504
This.openObjects[ &Object ] := new indirectReference( Object , { __Delete: 1 } )

0 commit comments

Comments
 (0)