Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 2e33e16

Browse files
committed
[[ Canvas ]] Added syntax for making a canvas and getting data, width and height.
[[ Engien ]] Added 'is a type' syntax.
1 parent 9a2fccd commit 2e33e16

File tree

6 files changed

+269
-1
lines changed

6 files changed

+269
-1
lines changed

engine/src/canvas.mlc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,10 @@ end syntax
24412441

24422442
public foreign handler MCCanvasThisCanvas(out rCanvas as Canvas) as undefined binds to "<builtin>"
24432443
public foreign handler MCCanvasPretendToAssignThisCanvas(in pCanvas as Canvas) as undefined binds to "<builtin>"
2444+
public foreign handler MCCanvasNewCanvasWithSize(in pSize as list, out rCanvas as Canvas) as undefined binds to "<builtin>"
2445+
public foreign handler MCCanvasGetPixelDataOfCanvas(in pCanvas as Canvas, out rData as data) as undefined binds to "<builtin>"
2446+
public foreign handler MCCanvasGetPixelHeightOfCanvas(in pCanvas as Canvas, out rHeight as uint) as undefined binds to "<builtin>"
2447+
public foreign handler MCCanvasGetPixelWidthOfCanvas(in pCanvas as Canvas, out rWidth as uint) as undefined binds to "<builtin>"
24442448

24452449
syntax ThisCanvas is expression
24462450
"this" "canvas"
@@ -2449,6 +2453,30 @@ begin
24492453
MCCanvasPretendToAssignThisCanvas(input)
24502454
end syntax
24512455

2456+
syntax NewCanvasWithSize is prefix operator with precedence 1
2457+
"a" "new" "canvas" "with" "size" <Size: Expression>
2458+
begin
2459+
MCCanvasNewCanvasWithSize(Size, output)
2460+
end syntax
2461+
2462+
syntax GetPixelDataOfCanvas is prefix operator with precedence 1
2463+
"the" "pixel" "data" "of" <Canvas: Expression>
2464+
begin
2465+
MCCanvasGetPixelDataOfCanvas(Canvas, output)
2466+
end syntax
2467+
2468+
syntax GetPixelWidthOfCanvas is prefix operator with precedence 1
2469+
"the" "pixel" "width" "of" <Canvas: Expression>
2470+
begin
2471+
MCCanvasGetPixelWidthOfCanvas(Canvas, output)
2472+
end syntax
2473+
2474+
syntax GetPixelHeightOfCanvas is prefix operator with precedence 1
2475+
"the" "pixel" "height" "of" <Canvas: Expression>
2476+
begin
2477+
MCCanvasGetPixelHeightOfCanvas(Canvas, output)
2478+
end syntax
2479+
24522480
////////////////////////////////////////////////////////////////////////////////
24532481

24542482
end module

engine/src/module-canvas.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,13 +5003,70 @@ void MCCanvasPop(uintptr_t p_cookie)
50035003

50045004
extern "C" MC_DLLEXPORT void MCCanvasThisCanvas(MCCanvasRef& r_canvas)
50055005
{
5006+
if (s_current_canvas == nil)
5007+
MCErrorThrowGeneric(MCSTR("no current canvas"));
50065008
r_canvas = MCValueRetain(s_current_canvas);
50075009
}
50085010

50095011
extern "C" MC_DLLEXPORT void MCCanvasPretendToAssignToThisCanvas(MCCanvasRef p_canvas)
50105012
{
50115013
}
50125014

5015+
extern "C" MC_DLLEXPORT void MCCanvasNewCanvasWithSize(MCProperListRef p_list, MCCanvasRef& r_canvas)
5016+
{
5017+
MCGPoint t_scale;
5018+
if (!MCProperListToScale(p_list, t_scale))
5019+
return;
5020+
5021+
MCGContextRef t_gcontext;
5022+
if (!MCGContextCreate(ceil(t_scale . x), ceil(t_scale . y), true, t_gcontext))
5023+
{
5024+
MCErrorThrowGeneric(MCSTR("could not create gcontext"));
5025+
return;
5026+
}
5027+
5028+
MCCanvasRef t_canvas;
5029+
if (!MCCanvasCreate(t_gcontext, t_canvas))
5030+
{
5031+
MCGContextRelease(t_gcontext);
5032+
return;
5033+
}
5034+
5035+
MCGContextRelease(t_gcontext);
5036+
5037+
r_canvas = t_canvas;
5038+
}
5039+
5040+
extern "C" MC_DLLEXPORT void MCCanvasGetPixelData(MCCanvasRef p_canvas, MCDataRef& r_data)
5041+
{
5042+
__MCCanvasImpl *t_canvas;
5043+
t_canvas = MCCanvasGet(p_canvas);
5044+
5045+
uint32_t t_width, t_height;
5046+
t_width = MCGContextGetWidth(t_canvas -> context);
5047+
t_height = MCGContextGetHeight(t_canvas -> context);
5048+
5049+
void *t_pixels;
5050+
t_pixels = MCGContextGetPixelPtr(t_canvas -> context);
5051+
5052+
if (!MCDataCreateWithBytes((const byte_t *)t_pixels, t_width * t_height * sizeof(uint32_t), r_data))
5053+
return;
5054+
}
5055+
5056+
extern "C" MC_DLLEXPORT void MCCanvasGetPixelWidth(MCCanvasRef p_canvas, uinteger_t& r_width)
5057+
{
5058+
__MCCanvasImpl *t_canvas;
5059+
t_canvas = MCCanvasGet(p_canvas);
5060+
r_width = MCGContextGetWidth(t_canvas -> context);
5061+
}
5062+
5063+
extern "C" MC_DLLEXPORT void MCCanvasGetPixelHeight(MCCanvasRef p_canvas, uinteger_t& r_height)
5064+
{
5065+
__MCCanvasImpl *t_canvas;
5066+
t_canvas = MCCanvasGet(p_canvas);
5067+
r_height = MCGContextGetWidth(t_canvas -> context);
5068+
}
5069+
50135070
////////////////////////////////////////////////////////////////////////////////
50145071

50155072
static MCValueCustomCallbacks kMCCanvasRectangleCustomValueCallbacks =

libgraphics/include/graphics.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,13 @@ bool MCGContextCreateWithRaster(const MCGRaster& raster, MCGContextRef& r_contex
785785
MCGContextRef MCGContextRetain(MCGContextRef context);
786786
void MCGContextRelease(MCGContextRef context);
787787

788+
// Return a pointer to the underlying pixel data
789+
void *MCGContextGetPixelPtr(MCGContextRef context);
790+
791+
// Return the width, and height.
792+
uint32_t MCGContextGetWidth(MCGContextRef context);
793+
uint32_t MCGContextGetHeight(MCGContextRef context);
794+
788795
// Returns whether the current context context is valid. If an error
789796
// occurs when calling any method on a context context, it will become
790797
// invalid and all further operations will be no-ops.

libgraphics/src/context.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,42 @@ bool MCGContextCreateWithPixels(uint32_t p_width, uint32_t p_height, uint32_t p_
383383
return MCGContextCreateWithBitmap(t_bitmap, r_context);
384384
}
385385

386+
void *MCGContextGetPixelPtr(MCGContextRef context)
387+
{
388+
MCGContextLayerRef t_layer;
389+
t_layer = context -> layer;
390+
while(t_layer -> parent != nil)
391+
t_layer = t_layer -> parent;
392+
393+
const SkBitmap& t_bitmap = t_layer -> canvas -> getTopDevice() -> accessBitmap(false);
394+
395+
return t_bitmap . getPixels();
396+
}
397+
398+
uint32_t MCGContextGetWidth(MCGContextRef context)
399+
{
400+
MCGContextLayerRef t_layer;
401+
t_layer = context -> layer;
402+
while(t_layer -> parent != nil)
403+
t_layer = t_layer -> parent;
404+
405+
const SkBitmap& t_bitmap = t_layer -> canvas -> getTopDevice() -> accessBitmap(false);
406+
407+
return t_bitmap . width();
408+
}
409+
410+
uint32_t MCGContextGetHeight(MCGContextRef context)
411+
{
412+
MCGContextLayerRef t_layer;
413+
t_layer = context -> layer;
414+
while(t_layer -> parent != nil)
415+
t_layer = t_layer -> parent;
416+
417+
const SkBitmap& t_bitmap = t_layer -> canvas -> getTopDevice() -> accessBitmap(false);
418+
419+
return t_bitmap . height();
420+
}
421+
386422
MCGContextRef MCGContextRetain(MCGContextRef self)
387423
{
388424
if (self != NULL)

libscript/src/module-type.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static bool MCTypeValueIsEmpty(MCValueRef p_value)
1010
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeString && MCStringIsEmpty((MCStringRef)p_value)) ||
1111
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeName && MCNameIsEmpty((MCNameRef)p_value)) ||
1212
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeData && MCDataIsEmpty((MCDataRef)p_value)) ||
13-
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeList && MCListIsEmpty((MCListRef)p_value)));
13+
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeProperList && MCProperListIsEmpty((MCProperListRef)p_value)));
1414
}
1515

1616
extern "C" MC_DLLEXPORT void MCTypeEvalIsEmpty(MCValueRef p_target, bool& r_output)
@@ -39,6 +39,54 @@ extern "C" MC_DLLEXPORT void MCTypeEvalIsNotDefined(MCValueRef *p_target, bool&
3939
r_output = !t_defined;
4040
}
4141

42+
extern "C" MC_DLLEXPORT void MCTypeEvalIsABoolean(MCValueRef p_value, bool& r_output)
43+
{
44+
if (p_value != nil)
45+
r_output = MCValueGetTypeCode(p_value) == kMCValueTypeCodeBoolean;
46+
else
47+
r_output = false;
48+
}
49+
50+
extern "C" MC_DLLEXPORT void MCTypeEvalIsANumber(MCValueRef p_value, bool& r_output)
51+
{
52+
if (p_value != nil)
53+
r_output = MCValueGetTypeCode(p_value) == kMCValueTypeCodeNumber;
54+
else
55+
r_output = false;
56+
}
57+
58+
extern "C" MC_DLLEXPORT void MCTypeEvalIsAString(MCValueRef p_value, bool& r_output)
59+
{
60+
if (p_value != nil)
61+
r_output = MCValueGetTypeCode(p_value) == kMCValueTypeCodeString;
62+
else
63+
r_output = false;
64+
}
65+
66+
extern "C" MC_DLLEXPORT void MCTypeEvalIsAData(MCValueRef p_value, bool& r_output)
67+
{
68+
if (p_value != nil)
69+
r_output = MCValueGetTypeCode(p_value) == kMCValueTypeCodeData;
70+
else
71+
r_output = false;
72+
}
73+
74+
extern "C" MC_DLLEXPORT void MCTypeEvalIsAnArray(MCValueRef p_value, bool& r_output)
75+
{
76+
if (p_value != nil)
77+
r_output = MCValueGetTypeCode(p_value) == kMCValueTypeCodeArray;
78+
else
79+
r_output = false;
80+
}
81+
82+
extern "C" MC_DLLEXPORT void MCTypeEvalIsAList(MCValueRef p_value, bool& r_output)
83+
{
84+
if (p_value != nil)
85+
r_output = MCValueGetTypeCode(p_value) == kMCValueTypeCodeProperList;
86+
else
87+
r_output = false;
88+
}
89+
4290
////////////////////////////////////////////////////////////////////////////////////////////////////
4391

4492
#ifdef _TEST

libscript/src/type.mlc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ public foreign handler MCTypeEvalIsEmpty(in Target as any, out Value as bool) as
44
public foreign handler MCTypeEvalIsNotEmpty(in Target as any, out Value as bool) as undefined binds to "<builtin>"
55
public foreign handler MCTypeEvalIsDefined(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
66
public foreign handler MCTypeEvalIsNotDefined(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
7+
public foreign handler MCTypeEvalIsABoolean(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
8+
public foreign handler MCTypeEvalIsANumber(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
9+
public foreign handler MCTypeEvalIsAString(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
10+
public foreign handler MCTypeEvalIsAData(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
11+
public foreign handler MCTypeEvalIsAnArray(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
12+
public foreign handler MCTypeEvalIsAList(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
713

814
/*
915
Summary: Determines whether <Target> is empty or not.
@@ -65,4 +71,90 @@ end syntax
6571

6672
--
6773

74+
/*
75+
Summary: Determines whether <Target> is a boolean or not.
76+
77+
Target: Any expression
78+
output: Returns true if the given expression <Target> is a boolean, and false if not.
79+
80+
*/
81+
82+
syntax IsABoolean is postfix operator with precedence 1
83+
<Target: Expression> "is" "a" "boolean"
84+
begin
85+
MCTypeEvalIsABoolean(Target, output)
86+
end syntax
87+
88+
/*
89+
Summary: Determines whether <Target> is a number or not.
90+
91+
Target: Any expression
92+
output: Returns true if the given expression <Target> is a number, and false if not.
93+
94+
*/
95+
96+
syntax IsANumber is postfix operator with precedence 1
97+
<Target: Expression> "is" "a" "number"
98+
begin
99+
MCTypeEvalIsANumber(Target, output)
100+
end syntax
101+
102+
/*
103+
Summary: Determines whether <Target> is a string or not.
104+
105+
Target: Any expression
106+
output: Returns true if the given expression <Target> is a string, and false if not.
107+
108+
*/
109+
110+
syntax IsAString is postfix operator with precedence 1
111+
<Target: Expression> "is" "a" "string"
112+
begin
113+
MCTypeEvalIsAString(Target, output)
114+
end syntax
115+
116+
/*
117+
Summary: Determines whether <Target> is a data or not.
118+
119+
Target: Any expression
120+
output: Returns true if the given expression <Target> is a data, and false if not.
121+
122+
*/
123+
124+
syntax IsAData is postfix operator with precedence 1
125+
<Target: Expression> "is" "a" "data"
126+
begin
127+
MCTypeEvalIsAData(Target, output)
128+
end syntax
129+
130+
/*
131+
Summary: Determines whether <Target> is an array or not.
132+
133+
Target: Any expression
134+
output: Returns true if the given expression <Target> is an array, and false if not.
135+
136+
*/
137+
138+
syntax IsAnArray is postfix operator with precedence 1
139+
<Target: Expression> "is" "an" "array"
140+
begin
141+
MCTypeEvalIsAData(Target, output)
142+
end syntax
143+
144+
/*
145+
Summary: Determines whether <Target> is a list or not.
146+
147+
Target: Any expression
148+
output: Returns true if the given expression <Target> is a list, and false if not.
149+
150+
*/
151+
152+
syntax IsAList is postfix operator with precedence 1
153+
<Target: Expression> "is" "a" "list"
154+
begin
155+
MCTypeEvalIsAList(Target, output)
156+
end syntax
157+
158+
--
159+
68160
end module

0 commit comments

Comments
 (0)