-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
275 lines (269 loc) · 8.08 KB
/
main.ts
File metadata and controls
275 lines (269 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//% weight=100 color=#008080
//% groups=[ "Create", "Properties", "Actions"]
namespace circle {
//% group="Create"
//% block="destroy %circle=variables_get(myCircle)"
export function destroy(circle:Circle) {
circle.destroy()
}
//% group="Create"
//% block="create circle of radius %radius and color $color || fill=$filled"
//% blockSetVariable=myCircle
//% radius.min=5 radius.max=60 radius.defl=30
//% color.min=0 color.max=15 color.defl=2
//% filled.defl=false
export function createCircle(radius: number, color: number, filled:boolean = false): Circle {
return new Circle(radius, color, filled)
}
}
//% blockNamespace=circle
class Circle {
_sprite: Sprite = null
_img: Image = null
_radius: number = 0
_color: number = 0
_fillColor: number = 0
_filled: boolean = false
_dataText:string = ""
_dataNumber:number = 0
imageWH: number = 0
centerXY:number = 0
constructor(radius: number, color: number, filled: boolean = false) {
this._radius = radius
this._color = color
this._fillColor = 0;
this._dataText = ""
this._dataNumber =0
this._filled = filled
this._fillColor = this._color
this.imageWH = 2 * (this._radius + 2)
this.centerXY = this.imageWH / 2
this._img = image.create(this.imageWH, this.imageWH)
this._img.drawCircle(this.centerXY, this.centerXY, this._radius, this._color)
if (this._filled) {
this._img.fillCircle(this.centerXY, this.centerXY, this._radius, this._fillColor)
}
this._sprite = sprites.create(this._img)
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="data text"
get datatext(): string {
return this._dataText;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="data text"
set datatext(value: string) {
this._dataText = value;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="data number"
get dataNumber(): number {
return this._dataNumber;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="data number"
set dataNumber(value: number) {
this._dataNumber = value;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="color"
get color(): number {
return this._color;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="color"
set color(value: number) {
this._color = value;
this._img.drawCircle(this.centerXY, this.centerXY, this._radius, this._color);
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="radius"
get radius(): number {
return this._radius;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="fill color"
set fillColor(value: number) {
this._fillColor = value;
this._img.drawCircle(this.centerXY, this.centerXY, this._radius, this._color);
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="fill color"
get fillColor() {
return this._fillColor;
}
//% group="Properties"
//% blockSetVariable="myCircle"
//% blockCombine block="sprite"
get sprite(): Sprite {
return this._sprite;
}
//% group="Actions"
//% block="fill %Circle(myCircle) || with color $color"
fill(color: number = -1 ){
this._filled = true
if (color == -1)
{
this._fillColor = this._color
} else {
this._fillColor = color
}
this._img.fillCircle(this.centerXY, this.centerXY, this._radius, this._fillColor)
}
//% group="Actions"
//% block="erase fill from %Circle(myCircle)"
unfill() {
this._filled = false;
this._fillColor = 0;
this._img.fill(0); //clear anything in image
this._img.drawCircle(this.centerXY, this.centerXY, this._radius, this._color);
}
destroy(){
if(this._sprite != null){
this._sprite.destroy()
}
}
}
//% weight=100 color=#008080
//% groups=["List", "List Beginning", "List Middle", "List End"]
namespace circlelist {
//% group="Create" weight=90
//% blockSetVariable=myCircleList
//% block="empty circle list"
export function emptyCircleList(){
return new CircleList()
}
}
//% blockNamespace=circlelist
class CircleList{
_circles: Circle[] = []
constructor(){
}
//% group="List"
//% block=" reverse %myCircleList"
reverse (){
this._circles.reverse()
}
//% group="List"
//% block="%myCircleList find index of %value=variables_get(myCircle)"
findIndexOfCircle (value:Circle ):number{
for(let i = 0; i < this.length(); i++) {
if (value == this._circles[i]) return i
}
return -1
}
//% group="List"
//% block="length of array %myCircleList"
length(): number {
return this._circles.length
}
/*
beginning
*/
//% group="List Beginning"
//% block="%myCircleList remove and destroy first circle"
removeAndDestroyFirst(){
if(this.length() > 0){
let tmp = this._circles.removeAt(0)
tmp.destroy()
}
}
//% group="List Beginning"
//% block="get and remove first circle from %myCircleList"
getAndRemoveFirst():Circle {
if(this.length() > 0){
return this._circles.removeAt(0)
}
return null;
}
//% group="List Beginning"
//% block="%myCircleList insert %value=variables_get(myCircle) at beginning"
insertCircleToBeginning (value:Circle ){
this._circles.unshift(value)
this._circles.insertAt(0, null)
}
/*
end
*/
//% group="List End"
//% block="%myCircleList remove and destroy last circle"
removeAndDestroyLast(){
if(this.length() > 0)
{
let tmp = this._circles.removeAt(this.length()-1)
tmp.destroy()
}
}
//% group="List End"
//% block="get and remove last circle from %myCircleList"
getAndRemoveLast():Circle {
if(this.length() > 0){
return this._circles.removeAt(this.length()-1)
}
return null;
}
//% group="List End"
//% block="%myCircleList add %value=variables_get(myCircle) to end"
addCircleToEnd (value:Circle ){
this._circles.push(value)
}
/*
other
*/
//% group="List Other"
//% block="%myCircleList remove and destroy circle at %index"
removeAndDestroyCircleAt(index:number){
if(this.length() > 0)
{
let tmp = this._circles.removeAt(index)
tmp.destroy()
}
}
//% group="List Other"
//% block="%myCircleList get and remove circle at %index"
getAndRemoveCircleAt(index:number):Circle {
if(this.length() > 0)
{
return this._circles.removeAt(index)
}
return null
}
//% group="List Other"
//% block="%myCircleList get circle with sprite %mySprite"
getCircleWithSprite(s:Sprite):Circle {
for(let i = 0; i < this.length(); i++) {
if(this._circles[i].sprite == s) return this._circles[i]
}
return null
}
//% group="List Other"
//% block="%myCircleList get circle at %index"
getCircleAt(index:number):Circle {
if(index < this.length() )
{
return this._circles[index]
}
return null
}
//% group="List Other"
//% block="%myCircleList set circle at %index to %value=variables_get(myCircle)"
setCircleAt (index:number , value:Circle){
if(index < this.length()){
this._circles[index] = value;
}
}
//% group="List Other"
//% block="%myCircleList insert %value=variables_get(myCircle) at %index"
insertCircleAt (value:Circle , index:number){
this._circles.insertAt(index, value)
}
}