-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfo-screens.ts
1757 lines (1584 loc) · 57 KB
/
info-screens.ts
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace SpriteKind {
export const InfoScreens = SpriteKind.create()
export const Moving = SpriteKind.create()
export const Static = SpriteKind.create()
}
/**
* Extension for creating information screens (like splash screens and options screens).
*/
/**
* Enumerations
*/
enum FooterLocation {
Previous,
Done,
Next
} // enum FooterLocation
/**
* Used to indicate if images for moving sprites are "pointing" in a particular direction.
* Images will be flipped so that they appear to be moving in the correct direction.
*/
enum SpriteDirection {
Both,
PointsLeft,
PointsRight
} // enum SpriteDirection
/**
* Indicates how moving sprites should be placed and animated on the screen.
*/
enum SpriteMode {
/**
* Sprites move horizontally, one at a time, within space between headlines and mid-screen text.
*/
BlankSpace,
/**
* Sprites are all placed on screen and move in random directions, bouncing off of walls.
*/
Random,
/**
* Sprites are all placed on screen and move in random directions;
* game.onUpdate() will handle off-screen sprites.
*/
RandomWillUpdate
} // enum SpriteMode
/**
* Constants
*/
const DEFAULT_COLOR_BG: number = 15 // Black
const DEFAULT_COLOR_CURSOR: number = 5 // Yellow
const DEFAULT_COLOR_FOOTER: number = 1 // White
const DEFAULT_COLOR_HEADLINE: number = 14 // Brown
const DEFAULT_COLOR_MID_TEXT: number = 9 // Light Blue
const DEFAULT_COLOR_TABS: number = 3 // Pink
const DEFAULT_COLOR_TITLE: number = 5 // Yellow
const DEFAULT_DELAY: number = 5000
const DEFAULT_FONT_SIZE: number = 8
const DEFAULT_FONT_SIZE_FOOTER: number = 8
const DEFAULT_FONT_SIZE_HEADLINE: number = 8
const DEFAULT_FONT_SIZE_MID_TEXT: number = 5
const DEFAULT_FONT_SIZE_TABS: number = 5
const DEFAULT_FONT_SIZE_TITLE: number = 16
const DEFAULT_SPACING: number = 1
const DEFAULT_SPRITE_SPEED: number = 100
const DEFAULT_TEXT_DONE: string = 'Done'
const DEFAULT_TEXT_FOOTER_SPLASH: string = 'Press any button to begin'
const DEFAULT_Y_TITLES: number = 2
// const TAB_TEXT_MARGIN: number = 5 // Pixels to the left and right of tab text
const TEXT_NEXT: string = 'Next >'
const TEXT_PREVIOUS: string = '< Prev'
/**
* Interfaces
*/
interface CursorOptions {
/**
* Color of cursor
*/
color: number
/**
* Current option group where cursor is located
*/
currGroup: number
/**
* Current option where cursor is located
*/
currOption: number
/**
* Whether cursor is in footer
*/
isInFooter: boolean
} // interface Cursor
interface MovingSpriteOptions {
/**
* Sprite images
*/
imgs: Image[]
/**
* Direction(s) that sprites "point"
*/
dir: SpriteDirection
/**
* Mode to use when displaying sprites
*/
mode: SpriteMode
/**
* Speed to use for moving sprites
*/
speed: number
/**
* Vertical coordinate for static sprites
*/
y: number
} // interface MovingSpriteOptions
interface StaticSpriteOptions {
/**
* Sprite image
*/
img: Image
/**
* Horizontal coordinate for sprite
*/
x: number
/**
* Vertical coordinate for sprite
*/
y: number
} // interface StaticSpriteOptions
/**
* Abstract class for creating text sprites with common attributes.
*/
abstract class TextSpriteFactory {
/**
* Border width
*/
public borderWidth: number
/**
* Color to use when printing data
*/
public color: number
/**
* Font size to use when printing data
*/
public fontSize: number
/**
* Number of pixels between text and border
*/
public padding: number
/**
* Number of pixels between lines of text
*/
public spacing: number
/**
* Vertical coordinate to use when printing data
*/
public y: number
public constructor(color: number, fontSize: number, y: number, spacing: number,
padding: number, borderWidth: number) {
this.borderWidth = borderWidth
this.color = color
this.fontSize = fontSize
this.padding = padding
this.spacing = spacing
this.y = y
}
} // abstract class TextSpriteFactory
class StringTextSpriteFactory extends TextSpriteFactory {
public data: string
protected _sprite: TextSprite = null
public constructor(
data: string,
color: number,
fontSize: number,
y: number,
spacing: number = 1,
padding: number = 0,
borderWidth: number = 0
) {
super(color, fontSize, y, spacing, padding, borderWidth)
this.data = data
this._sprite = null
}
public get sprite(): TextSprite {
return this._sprite
}
public DrawCenter(x: number = 80): void {
this.CreateSprite()
this._sprite.x = x
}
public DrawLeft(left: number = 1): void {
this.CreateSprite()
this._sprite.left = left
}
public DrawRight(right: number = 158): void {
this.CreateSprite()
this._sprite.right = right
}
protected CreateSprite(): void {
if (this._sprite) {
this._sprite.destroy()
}
this._sprite = textsprite.create(this.data, 0, this.color)
this._sprite.padding = this.padding
if (this.borderWidth > 0) {
this._sprite.borderWidth = this.borderWidth
this._sprite.borderColor = 0
}
this._sprite.setMaxFontHeight(this.fontSize)
this._sprite.top = this.y
}
} // class StringTextSpriteFactory
class StringArrayTextSpriteFactory extends TextSpriteFactory {
public data: string[]
protected _sprites: TextSprite[]
public constructor(
data: string[],
color: number,
fontSize: number,
y: number,
spacing: number = 1,
padding: number = 0,
borderWidth: number = 0
) {
super(color, fontSize, y, spacing, padding, borderWidth)
this.data = data
this._sprites = []
}
public DrawCenter(x: number = 80): void {
this.CreateSprites()
for (let s of this._sprites) {
s.x = x
}
}
public DrawHorizontal(left: number = 1, borders: boolean = false): void {
this.CreateSprites()
let currLeft: number = left
for (let s of this._sprites) {
s.setBorder(borders ? this.borderWidth : 0, borders ? this.color : 0, this.padding)
s.left = currLeft
s.top = this.y
currLeft += s.width + this.spacing
}
}
public DrawLeft(left: number = 1): void {
this.CreateSprites()
for (let s of this._sprites) {
s.left = left
}
}
public DrawRight(right: number = 158): void {
this.CreateSprites()
for (let s of this._sprites) {
s.right = right
}
}
public Redraw(item: number, reverse: boolean = false) {
if (this._sprites && this._sprites[item]) {
let ts: TextSprite = this._sprites[item]
if (reverse) {
ts.bg = this.color
ts.fg = 15
} else {
ts.bg = 0
ts.fg = this.color
}
ts.update()
}
}
protected CreateSprites(): void {
if (this._sprites) {
if (this._sprites.length > 0) {
for (let s of this._sprites) {
s.destroy()
}
}
this._sprites = []
} else {
this._sprites = []
}
let currY: number = this.y
for (let text of this.data) {
let ts: TextSprite = textsprite.create(text, 0, this.color)
ts.padding = this.padding
if (this.borderWidth > 0) {
ts.borderWidth = this.borderWidth
ts.borderColor = 0
}
ts.setMaxFontHeight(this.fontSize)
ts.top = currY
this._sprites.push(ts)
currY += ts.height + this.spacing
}
}
} // class StringArrayTextSpriteFactory
class String2dArrayTextSpriteFactory extends TextSpriteFactory {
public data: string[][]
protected _sprites: TextSprite[][] = []
public constructor(
data: string[][],
color: number,
fontSize: number,
y: number,
spacing: number = 1,
padding: number = 0,
borderWidth: number = 0
) {
super(color, fontSize, y, spacing, padding, borderWidth)
this.data = data
this._sprites = []
}
public DrawCenter(index: number = 0, x: number = 80, reset: boolean = false): void {
this.CreateSprites(index, reset)
for (let s of this._sprites[index]) {
s.x = x
}
}
public DrawLeft(index: number = 0, left: number = 1, reset: boolean = false): void {
this.CreateSprites(index, reset)
for (let s of this._sprites[index]) {
s.left = left
}
}
public DrawRight(index: number = 0, right: number = 158, reset: boolean = false): void {
this.CreateSprites(index, reset)
for (let s of this._sprites[index]) {
s.right = right
}
}
public Redraw(group: number, item: number, reverse: boolean = false) {
if (this._sprites && this._sprites[group] && this._sprites[group][item]) {
let ts: TextSprite = this._sprites[group][item]
if (reverse) {
ts.bg = this.color
ts.fg = 15
} else {
ts.bg = 0
ts.fg = this.color
}
ts.update()
}
}
public SetBorder(group: number, item: number, color: number, show: boolean = false) {
if (this._sprites && this._sprites[group] && this._sprites[group][item]) {
let ts: TextSprite = this._sprites[group][item]
if (show) {
ts.setBorder(this.borderWidth, color, this.padding)
} else {
ts.setBorder(this.borderWidth, 0, this.padding)
}
ts.update()
}
}
protected CreateSprites(index: number, reset: boolean): void {
if (!this.data || this.data.length == 0) {
return
}
if (this._sprites) {
if (reset && this._sprites.length > 0) {
for (let group of this._sprites) {
for (let s of group) {
s.destroy()
}
}
}
} else {
this._sprites = []
}
let currY: number = this.y
this._sprites[index] = []
for (let text of this.data[index]) {
let ts: TextSprite = textsprite.create(text, 0, this.color)
ts.padding = this.padding
if (this.borderWidth > 0) {
ts.borderWidth = this.borderWidth
ts.borderColor = 0
}
ts.setMaxFontHeight(this.fontSize)
ts.top = currY
this._sprites[index].push(ts)
currY += this.fontSize + 2 * this.padding + this.spacing
}
}
} // interface String2dArrayTextSpriteFactory
//% blockNamespace=infoScreens
class RotatingScreens {
/**
* Protected member variables
*/
protected _backColor: number // background color for screens
protected _backImage: Image // user-supplied background image
protected _currScreen: number // index of image currently shown
protected _currSprite: number
protected _footer: StringTextSpriteFactory
protected _headlines: String2dArrayTextSpriteFactory
protected _midText: String2dArrayTextSpriteFactory
protected _titles: StringArrayTextSpriteFactory
protected _movingSprites: MovingSpriteOptions
protected _movingSpritesSequential: boolean
protected _interval: number // interval in milliseconds for screens to rotate
protected _next: number // next scheduled time to rotate screens
protected _staticSprites: StaticSpriteOptions[] // container for static sprites
/**
* Constructor
* @param {string[]} titles - Array of strings to display as the title
* @param {number} titlesColor - Color to use when printing titles
* @param {string[][]} headlines - Sets of strings to display as headlines
* @param {number} headlinesColor - Color to use when printing headlines
* @param {string} footer - Text to print at the bottom of the screen
* @param {number} footerColor - Color to use when printing footer
* @param {string[][]} midText - Sets of strings (up to three sets) to display in the center of the screen
* @param {number} midTextColor - Color to use when printing the mid-text
* @param {number} backColor - Color to use for the background
* @param {number} delay - Interval in milliseconds when screens will rotate
*/
constructor(titles: string[], titlesColor?: number,
headlines?: string[][], headlinesColor?: number,
footer?: string, footerColor?: number,
midText?: string[][], midTextColor?: number,
backColor?: number, delay?: number
) {
this._currScreen = -1;
this._currSprite = -1;
this._backColor = backColor ? backColor : DEFAULT_COLOR_BG
this._backImage = null
this._interval = delay ? delay : DEFAULT_DELAY
this._movingSpritesSequential = true
this._staticSprites = []
this._footer = new StringTextSpriteFactory(
footer ? footer : '',
footerColor ? footerColor : DEFAULT_COLOR_FOOTER,
DEFAULT_FONT_SIZE_FOOTER,
0
)
this._headlines = new String2dArrayTextSpriteFactory(
headlines ? headlines : [],
headlinesColor ? headlinesColor : DEFAULT_COLOR_HEADLINE,
DEFAULT_FONT_SIZE_HEADLINE,
0
)
this._midText = new String2dArrayTextSpriteFactory(
midText ? midText : [],
midTextColor ? midTextColor : DEFAULT_COLOR_MID_TEXT,
DEFAULT_FONT_SIZE_MID_TEXT,
0
)
this._titles = new StringArrayTextSpriteFactory(
titles ? titles : [],
titlesColor ? titlesColor : DEFAULT_COLOR_TITLE,
DEFAULT_FONT_SIZE_TITLE,
DEFAULT_Y_TITLES,
)
this._movingSprites = {
imgs: [],
dir: SpriteDirection.Both,
mode: SpriteMode.Random,
speed: DEFAULT_SPRITE_SPEED,
y: 0,
}
} // constructor()
/**
* Getters and setters
*/
/**
* @return {Image} Image used as header or background image
*/
//% blockId="infoScreens_RotatingScreens_backImage_get"
//% block="header image"
//% blockCombine
//% callInDebugger
public get backImage(): Image {
return this._backImage
} // get backImage()
/**
* @param {Image} value - Image to use as header or background image
*/
//% blockId="infoScrens_RotatingScreens_backImage_set"
//% block="header image"
//% value.shadow="variables_get" value.defl="myImage"
//% blockCombine
public set backImage(value: Image) {
this._backImage = value
} // set backImage()
/**
* @return {number} Interval in milliseconds when screens will rotate
*/
//% blockId="infoScreens_RotatingScreens_delay_get"
//% block="delay (milliseconds)"
//% blockCombine
//% callInDebugger
public get delay(): number {
return this._interval
} // get delay()
/**
* @param {number} Interval in milliseconds when screens will rotate
*/
//% blockId="infoScreens_RotatingScreens_delay_set"
//% block="delay (milliseconds)"
//% value.defl=5000
//% blockCombine
public set delay(value: number) {
if (value >= 0) {
this._interval = value
} else {
this._interval = DEFAULT_DELAY
} // if (value)
} // set delay()
//% callInDebugger
public get footer(): StringTextSpriteFactory {
return this._footer;
} // get footer()
//% callInDebugger
public get headlines(): String2dArrayTextSpriteFactory {
return this._headlines;
} // get headlines()
//% callInDebugger
public get midText(): String2dArrayTextSpriteFactory {
return this._midText;
} // get midText()
/**
* @return {number} Number of moving sprites on screen.
*/
//% blockId="infoScreens_RotatingScreens_movingSpriteCount"
//% block="count of moving sprites on screen"
//% blockCombine
//% hidden
public get movingSpriteCount(): number {
return sprites.allOfKind(SpriteKind.Moving).length
} // get movingSpriteCount()
//% callInDebugger
public get movingSpriteOptions(): MovingSpriteOptions {
return this._movingSprites;
} // get movingSpriteOptions()
/**
* @return {SpriteMode} Mode for moving sprites.
*/
//% blockId="infoScreens_RotatingScreens_movingSpriteMode_get"
//% block="mode for moving sprites"
//% blockCombine
//% hidden
public get movingSpriteMode(): SpriteMode {
return this._movingSprites.mode
} // get movingSpriteMode()
/**
* @param {SpriteMode} value - Mode for moving sprites.
*/
//% blockId="infoScreens_RotatingScreens_movingSpriteMode_set"
//% block="mode for moving sprites"
//% blockCombine
//% hidden
public set movingSpriteMode(value: SpriteMode) {
this._movingSprites.mode = value
} // set movingSpriteMode()
/**
* @return {number} Next <code>game.runtime()</code> when screens should be rotated;
* call @see #rotate to do so
*/
//% blockId="infoScreens_RotatingScreens_nextTime"
//% block="time to rotate"
//% blockCombine
//% callInDebugger
public get nextTime(): number {
return this._next
} // get nextTime()
/**
* @return {boolean} Whether sprites will be displayed sequentially or randomly.
* True = in order, false = randomly.
* Only applies to Blank Space mode.
*/
//% callInDebugger
public get sequentialSprites(): boolean {
return this._movingSpritesSequential
} // get sequentialSprites()
/**
* @param {boolean} value - Whether sprites will be displayed sequentially or randomly.
* True = in order, false = randomly.
* Only applies to Blank Space mode.
*/
public set sequentialSprites(value: boolean) {
this._movingSpritesSequential = value
} // set sequentialSprites()
//% callInDebugger
public get titles(): StringArrayTextSpriteFactory {
return this._titles;
} // get titles()
/**
* Public methods
*/
/**
* @param {string[]} value - Headlines to add.
*/
//% blockId="infoScreens.RotatingScreens.addHeadlines"
//% block="%mySplashScreen|add headlines %value"
//% value.shadow="variables_get" value.defl="text list"
public addHeadlines(value: string[]): void {
this._headlines.data.push(value)
} // addHeadlines()
/**
* @param {Image} value - Image to add to the collection of moving sprites.
*/
//% blockId="infoScreens_RotatingScreens_addMovingSprite"
//% block="%mySplashScreen|add moving image %value"
//% value.shadow="variables_get" value.defl="myImage"
public addMovingSprite(value: Image): void {
this._movingSprites.imgs.push(value)
} // addMovingSprite()
/**
* @param {string[]} value - Mid-text group to add.
*/
public addMidText(value: string[]): void {
this._midText.data.push(value)
} // addMidText()
/**
* @param {StaticSpriteOptions} value - Sprite and related information to add to
* the collection of static sprites.
*/
public addStaticSprite(value: StaticSpriteOptions): void {
this._staticSprites.push(value)
} // addStaticSprite()
/**
* @param {Image} img - Image to use for new static sprite.
* @param {number} x - Horizontal coordinate for new sprite.
* @param {number} y - Vertical coordinate for new sprite.
*/
//% blockId="infoScreens_RotatingScreens_addStaticSprite"
//% block="%mySplashScreen|add image %img at location x %x y %y"
//% img.shadow="variables_get" img.defl="myImage"
//% x.defl=0 y.defl=0
//% hidden
public addStaticSpriteImage(img: Image, x: number, y: number): void {
this._staticSprites.push({
img: img,
x: x,
y: y
})
} // addStaticSpriteImage()
/**
* Initializes canvas and creates sprites.
* Call when ready to present rotating screens after object has been configured.
*/
//% blockId="infoScreens_RotatingScreens_build"
//% block="%mySplashScreen|show"
public build(): void {
this.rebuild()
this.refresh()
if (this._staticSprites.length > 0) {
this.addStaticSprites()
} // if (this._staticSprites)
switch (this._movingSprites.mode) {
case SpriteMode.BlankSpace:
this._currSprite = 0
this.showScrollingSprite()
break
case SpriteMode.Random:
this.addAllMovingSprites(true)
break
case SpriteMode.RandomWillUpdate:
this.addAllMovingSprites(false)
break
} // switch (this._movingSprites.mode)
} // build()
/**
* Clears the moving sprites collection.
*/
public clearMovingSprites(): void {
this._movingSprites.imgs = []
} // clearMovingSprites()
/**
* Destroy all moving and static sprites from the game.
* Call when removing the splash screen from the game.
*/
//% blockId="infoScreens_RotatingScreens_destroySprites"
//% block="%mySplashScreen|destroy sprites"
public destroySprites(): void {
for (let sprite of sprites.allOfKind(SpriteKind.InfoScreens)) {
sprite.destroy()
}
for (let sprite of sprites.allOfKind(SpriteKind.Moving)) {
sprite.destroy()
} // for (sprite)
for (let sprite of sprites.allOfKind(SpriteKind.Static)) {
sprite.destroy()
} // for (sprite)
for (let sprite of sprites.allOfKind(SpriteKind.Text)) {
sprite.destroy()
}
} // destroySprites()
/**
* Initialize canvas.
* Call after initial build() if configuration changes.
*/
public rebuild(): void {
this.createBase()
} // rebuild()
/**
* Update canvas with the current headline.
* Set time for next update.
*/
public refresh(): void {
if (!this._currScreen || this._currScreen < 0 || this._currScreen >= this._headlines.data.length) {
this._currScreen = 0
} // if (this._currScreen >= this._headlines.length)
this.headlines.DrawCenter(this._currScreen, 80, true)
this._next = game.runtime() + this._interval
} // refresh()
/**
* Destroy all sprites associated with the information screens.
*/
//% blockId="infoScreens_RotatingScreens_release"
//% block="%mySplashScreen|destroy resources"
public release(): void {
this.destroySprites()
} // release()
/**
* Update canvas with the next headline.
*/
//% blockId="infoScreens_RotatingScreens_rotate"
//% block="%mySplashScreen|rotate"
public rotate(): void {
this._currScreen++
this.refresh()
} // rotate()
/**
* Set the title text
*/
//% blockId="infoScreens_RotatingScreens_setTitles"
//% block="%mySplashScreen|set titles to %value"
//% value.shadow="variables_get" value.defl="text list"
//% hidden
public setTitles(value: string[]): void {
this._titles.data = value
} // setTitles()
/**
* Shows the next scrolling sprite in the collection.
* Call from game.onUpdate()
*/
//% blockId="infoScreens_RotatingScreens_showScrollingSprite"
//% block="%mySplashScreen|show next moving image"
public showScrollingSprite(): void {
if (this._movingSpritesSequential) {
this._currSprite++
if (this._currSprite < 0 || this._currSprite >= this._movingSprites.imgs.length) {
this._currSprite = 0
} // if (this._currSprite > this._movingSprites.length)
} else {
this._currSprite = Math.randomRange(0, this._movingSprites.imgs.length - 1)
} // if (this._movingSpritesSequential)
let newSprite: Sprite = sprites.create(this._movingSprites.imgs[this._currSprite].clone(),
SpriteKind.Moving)
newSprite.setFlag(SpriteFlag.Ghost, true)
newSprite.setFlag(SpriteFlag.AutoDestroy, true)
newSprite.y = this._movingSprites.y
if (Math.percentChance(50)) {
// Move sprite left to right
newSprite.x = -(newSprite.image.width / 2)
newSprite.vx = this._movingSprites.speed
if (this._movingSprites.dir === SpriteDirection.PointsLeft) {
newSprite.image.flipX()
} // if (this._movingSprites.dir...)
} else {
// Move sprite right to left
newSprite.x = screen.width + newSprite.image.width / 2
newSprite.vx = 0 - this._movingSprites.speed
if (this._movingSprites.dir === SpriteDirection.PointsRight) {
newSprite.image.flipX()
} // if (this._movingSprites.dir...)
} // if (Math.percentChance(50))
} // showScrollingSprite()
/**
* Protected methods
*/
/**
* Create moving sprites.
* @param {boolean} keepOnScreen - True = sprites will bounce off of walls
* False = game.onUpdate() will handle off-screen sprites
*/
protected addAllMovingSprites(keepOnScreen: boolean = true): void {
for (let img of this._movingSprites.imgs) {
let newSprite: Sprite = sprites.create(img.clone(), SpriteKind.Moving)
newSprite.setFlag(SpriteFlag.BounceOnWall, keepOnScreen)
newSprite.setFlag(SpriteFlag.Ghost, true)
newSprite.x = Math.randomRange(0, screen.width)
newSprite.y = Math.randomRange(0, screen.height)
newSprite.vx = Math.randomRange(0, this._movingSprites.speed)
newSprite.vy = this._movingSprites.speed - newSprite.vx
if (Math.percentChance(50)) {
newSprite.vx = 0 - newSprite.vx
} // if (Math.percentChance(50))
if (Math.percentChance(50)) {
newSprite.vy = 0 - newSprite.vy
} // if (Math.percentChance(50))
} // for (img)
} // addAllMovingSprites()
/**
* Create static sprites.
*/
protected addStaticSprites(): void {
for (let sprite of this._staticSprites) {
let newSprite: Sprite = sprites.create(sprite.img.clone(), SpriteKind.Static)
newSprite.setFlag(SpriteFlag.Ghost, true)
newSprite.x = sprite.x
newSprite.y = sprite.y
} // for (sprite)
} // addStaticSprites()
/**
* Draws text and background image on canvas.
*/
protected createBase(): void {
this.destroySprites()
scene.setBackgroundColor(this._backColor)
if (this._backImage) {
scene.setBackgroundImage(this._backImage)
} else {
scene.setBackgroundImage(null)
}
this._titles.DrawCenter()
this._footer.y = screen.height - (this._footer.fontSize + 2 * this._footer.padding + 2)
this._footer.DrawCenter()
// Set initial vertical coordinate for mid-text.
// Will be adjusted if mid-text exists.
this._midText.y = screen.height - 10
if (this._midText.data && this._midText.data.length > 0) {
this._midText.y = screen.height -
((this._midText.fontSize + this._midText.padding + 1) *
(this._midText.data[0].length + 1) + 4)
switch (this._midText.data.length) {
case 1:
this._midText.DrawCenter(0)
break;
case 2:
this._midText.DrawLeft(0)
this._midText.DrawLeft(1, screen.width / 2 + 1)
break;
default:
this._midText.DrawLeft(0)
this._midText.DrawCenter(1)
this._midText.DrawLeft(2, screen.width * 2 / 3 + 1)
break;
} // switch (this._midText.length)
} // if (this._midText)
// Calculate vertical coordinate for headlines.
if (this._backImage) {
let minY = this._midText.y - (this._headlines.fontSize + 1) * 2
if (this._backImage.height > minY) {
this._headlines.y = minY
} else {
this._headlines.y = this._backImage.height
} // if (this._backImage.height > minY)
} else {
if (this._titles.data) {
this._headlines.y = this._titles.fontSize * this._titles.data.length + this._titles.data.length +
DEFAULT_Y_TITLES + 2
} else {
this._headlines.y = DEFAULT_Y_TITLES + 2
} // if (this._titles.data)
} // if (this._backImage)
// Calculate vertical coordinate for moving sprites.
this._movingSprites.y = (this._headlines.y + this._headlines.fontSize * 2 + 1 + this._midText.y) / 2
} // createBase()
} // class RotatingScreens
//% blockNamespace=infoScreens
class SplashScreens extends RotatingScreens {
/**
* Protected constants
*/
/**
* Constructor
* @param {string[]} titles - Array of strings to display as the title
* @param {number} titlesColor - Color to use when printing titles
* @param {string[][]} headlines - Sets of strings to display as headlines
* @param {number} headlinesColor - Color to use when printing headlines
* @param {string[][]} instructions - Sets of strings (up to three sets) to display in the center of the screen
* @param {number} instructionsColor - Color to use when printing the mid-text
* @param {number} backColor - Color to use for the background
* @param {number} delay - Interval in milliseconds when screens will rotate
*/
constructor(titles: string[], titlesColor?: number,
headlines: string[][] = null, headlinesColor?: number,
instructions: string[][] = null, instructionsColor?: number,
backColor?: number, delay?: number) {
super(titles,
titlesColor,
headlines,
headlinesColor,
DEFAULT_TEXT_FOOTER_SPLASH,
1,
instructions,
instructionsColor,
backColor,
delay)
} // constructor()
/**
* Getters and setters
*/
/**
* @return {string[][]} Sets (up to three) of strings that will appear in the
* center of the screen
*/
public get instructions(): string[][] {