-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinventory.ts
722 lines (671 loc) · 24.4 KB
/
inventory.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
enum ToolbarColorAttribute {
//% block="outline"
BoxOutline,
//% block="selected outline"
BoxSelectedOutline,
//% block="background"
BoxBackground,
//% block="text"
BoxText
}
enum InventoryColorAttribute {
//% block="outline"
InventoryOutline,
//% block="selected outline"
InventorySelectedOutline,
//% block="background"
InventoryBackground,
//% block="text"
InventoryText
}
enum ItemTextAttribute {
//% block="name"
Name,
//% block="description"
Description,
//% block="tooltip"
Tooltip
}
enum ToolbarNumberAttribute {
//% block="selected index"
SelectedIndex,
//% block="max items"
MaxItems
}
enum InventoryNumberAttribute {
//% block="selected index"
SelectedIndex,
//% block="max items"
MaxItems
}
//% color="#7732B3"
//% group="['Item', 'Toolbar', 'Inventory']"
namespace Inventory {
/**
* Create a simple item which holds a single image and some text.
* Can also contain a description which isn't used by the Inventory extension.
*/
export class Item {
/**
* Define variables that are part of the class.
*/
public name: string;
public image: Image;
public description: string;
public tooltip: string = ""
/**
* Make a simple item.
* @param name: The name of the item.
* @param image: The image of the item. Must be 16x16, otherwise it will not draw
* properly.
* @param description: The description of the item. Not required and defaults to "".
*/
constructor(name: string, image: Image, description: string = null) {
this.name = name;
this.image = image;
this.description = description;
}
/**
* Set the name or description of the item.
* @param attribute: A property of the ItemTextAttribute enum.
* @param value: The new text of the item.
*/
//% block="item %Inventory(Item) set %attribute to %value"
//% weight=20
//% group="Item"
//% hidden
set_text(attribute: ItemTextAttribute, value: string) {
if (attribute == ItemTextAttribute.Name) {
this.name = value;
} else if (attribute == ItemTextAttribute.Description) {
this.description = value;
} else if (attribute == ItemTextAttribute.Tooltip) {
this.tooltip = value;
}
}
/**
* Get the name of description of the item.
* @return: The text.
*/
//% block="item %Inventory(Item) get %attribute"
//% weight=10
//% group="Item"
//% hidden
get_text(attribute: ItemTextAttribute) {
if (attribute == ItemTextAttribute.Name) {
return this.name;
} else if (attribute == ItemTextAttribute.Description) {
return this.description;
} else if (attribute == ItemTextAttribute.Tooltip) {
return this.tooltip;
}
return "";
}
/**
* Set the image of the item.
* @param new_image: The new image.
*/
//% block="item %Inventory(Item) set image to %new_image"
//% new_image.shadow=screen_image_picker
//% weight=40
//% group="Item"
//% hidden
set_image(new_image: Image) {
this.image = new_image;
}
/**
* Get the image of the item.
* @return: The image.
*/
//% block="item %Inventory(Item) get image"
//% weight=30
//% group="Item"
//% hidden
get_image() {
return this.image;
}
}
/**
* Create a new item - for blocks. Only rewrapped for blocks.
* @return: A new Inventory.Item.
*/
//% block="create item with name %name and %image || with description %description"
//% blockSetVariable=item
//% name.dfl="Name"
//% image.shadow=screen_image_picker
//% expandableArgumentMode="toggle"
//% description.dfl="Description"
//% weight=50
//% group="Item"
//% hidden
export function create_item(name: string, image: Image, description: string = null) {
return new Item(name, image, description)
}
/**
* Create a toolbar sprite which is just a regular sprite.
*/
export class Toolbar extends Sprite {
/**
* Define (extra) variables that are part of the class.
*/
private _items: Item[];
private _selected: number = 0;
private _max_items: number;
private _box_outline_color: number = 12;
private _box_selected_outline_color: number = 5;
private _box_background_color: number = 13;
private _box_text_color: number = 12;
/**
* Make a toolbar.
*/
constructor(items: Item[] = [], max_items: number) {
super(img`
.
`)
this._items = items;
this._max_items = max_items;
this.update();
}
/**
* Get the selected item.
*/
public get selected() {
return this._selected;
}
/**
* Set the selected item.
*/
public set selected(index: number) {
this._selected = index;
this.update();
}
/**
* Get the items in the toolbar.
*/
public get items() {
return this._items;
}
/**
* Get the items in the toolbar. Only rewrapped for blocks.
* @return: The items, as an array of Item.
*/
//% block="toolbar %Inventory(toolbar) get items"
//% weight=80
//% group="Toolbar"
//% hidden
public get_items() {
return this.items;
}
/**
* Set the items in the toolbar.
*/
public set items(new_items: Item[]) {
this._items = new_items;
this.update();
}
/**
* Set the items in the toolbar. Only rewrapped for blocks.
*/
//% block="toolbar %Inventory(toolbar) set items to %new_items"
//% new_items.shadow="lists_create_with"
//% weight=90
//% group="Toolbar"
//% hidden
public set_items(new_items: Item[]) {
this.items = new_items;
}
/**
* Get the maximum amount of items in the toolbar.
*/
public get max_items() {
return this._max_items;
}
/**
* Set the maximum amount of items in the toolbar.
*/
public set max_items(new_max: number) {
this._max_items = new_max;
this.update();
}
/**
* Set the selected index or max items. Only rewrapped for blocks.
* @attribute: A property of the ToolbarNumberAttribute enum.
* @value: The new value.
*/
//% block="toolbar %Inventory(toolbar) set %attribute to %value"
//% weight=70
//% group="Toolbar"
//% hidden
public set_number(attribute: ToolbarNumberAttribute, value: number) {
if (attribute == ToolbarNumberAttribute.SelectedIndex) {
this.selected = value;
} else if (attribute == ToolbarNumberAttribute.MaxItems) {
this.max_items = value;
}
}
/**
* Change the selected index or max items. Only rewrapped for blocks.
* @attribute: A property of the ToolbarNumberAttribute enum.
* @change: The value to change the number by.
*/
//% block="toolbar %Inventory(toolbar) change %attribute by %change"
//% weight=65
//% group="Toolbar"
//% hidden
public change_number(attribute: ToolbarNumberAttribute, change: number) {
this.set_number(attribute, this.get_number(attribute) + change);
}
/**
* Get the selected index or max items. Only rewrapped for blocks.
* @attribute: A property of the ToolbarNumberAttribute enum.
* @return: A number.
*/
//% block="toolbar %Inventory(toolbar) get %attribute"
//% weight=60
//% group="Toolbar"
//% hidden
public get_number(attribute: ToolbarNumberAttribute) {
if (attribute == ToolbarNumberAttribute.SelectedIndex) {
return this.selected;
} else if (attribute == ToolbarNumberAttribute.MaxItems) {
return this.max_items;
}
return -1;
}
/**
* Set a specific part of the toolbar to a specific color.
* @param attribute: A property of the ToolbarColorAttribute enum.
* @param color: A number which should be the new color of the attribute.
*/
//% block="toolbar %Inventory(toolbar) set color of %attribute to %color"
//% color.shadow=colorindexpicker
//% weight=50
//% group="Toolbar"
public set_color(attribute: ToolbarColorAttribute, color: number) {
if (attribute == ToolbarColorAttribute.BoxOutline) {
this._box_outline_color = color;
} else if (attribute == ToolbarColorAttribute.BoxSelectedOutline) {
this._box_selected_outline_color = color;
} else if (attribute == ToolbarColorAttribute.BoxBackground) {
this._box_background_color = color;
} else if (attribute == ToolbarColorAttribute.BoxText) {
this._box_text_color = color;
}
this.update();
}
/**
* Get the color of a specific part of the toolbar.
* @param attribute: A property of the ToolbarColorAttribute enum.
* @return: The color (which is a number) of the attribute, otherwise -1.
*/
//% block="toolbar %Inventory(toolbar) get color of %attribute"
//% weight=40
//% group="Toolbar"
public get_color(attribute: ToolbarColorAttribute) {
if (attribute == ToolbarColorAttribute.BoxOutline) {
return this._box_outline_color;
} else if (attribute == ToolbarColorAttribute.BoxSelectedOutline) {
return this._box_selected_outline_color;
} else if (attribute == ToolbarColorAttribute.BoxBackground) {
return this._box_background_color;
} else if (attribute == ToolbarColorAttribute.BoxText) {
return this._box_text_color;
}
return -1;
}
/**
* Update the image of the toolbar.
*/
//% block="toolbar %Inventory(toolbar) force redraw"
//% weight=30
public update() {
let image_size: number = 16;
let padding: number = 2;
let box_size: number = image_size + (padding * 2);
let new_image = image.create(
((box_size + 2) * this.max_items) - padding,
box_size
)
for (let index = 0; index < this.max_items; index++) {
if (index > this.max_items - 1) {
return;
}
new_image.fillRect(
(box_size + padding) * index,
0,
box_size,
box_size,
this._box_background_color
)
let x: number = ((box_size + padding) * index) + 2;
let y: number = 2;
if (index < this.items.length) {
spriteutils.drawTransparentImage(this.items[index].image, new_image, x, y)
}
new_image.drawRect(
(box_size + padding) * index,
0,
box_size,
box_size,
index == this.selected ? this._box_selected_outline_color : this._box_outline_color
)
if (index < this.items.length) {
this.print_right_aligned(new_image, this.items[index].tooltip,
x + box_size - 3, y + (box_size - 5) - 4, this._box_text_color,
true);
}
}
this.setImage(new_image);
}
/**
* Print some text right-aligned
* @param target: The image to draw too.
* @param text: The text to draw.
* @param right: the X coordinate of the right of the text.
* @param y: The Y coordinate of the text.
* @param color: The color of the text.
* @param use_small_font: Whether to use the 5x5 instead of the 5x8 font.
*/
private print_right_aligned(target: Image, text: string,
right: number, y: number,
color: number, use_small_font: boolean = false) {
let text_length_px: number = text.length * 6;
let label_x: number = right - text_length_px;
target.print(text, label_x, y, color, use_small_font? image.font5: image.font8);
}
}
/**
* Create a new toolbar - for blocks.
* @return: A new Toolbar.
*/
//% block="create toolbar with items %items and max items %max_items"
//% blockSetVariable=toolbar
//% items.shadow="lists_create_with"
//% max_items.dfl=3
//% weight=100
//% group="Toolbar"
//% hidden
export function create_toolbar(items: Item[], max_items: number) {
return new Toolbar(items, max_items);
}
/**
* Create an inventory sprite which is just a regular sprite.
*/
export class Inventory extends Sprite {
/**
* Define (extra) variables that are part of the class.
*/
private _items: Item[];
private _selected: number = 0;
private _max_items: number;
private _text: string = "Inventory";
private _inv_outline_color: number = 12;
private _inv_selected_outline_color: number = 5;
private _inv_background_color: number = 13;
private _inv_text_color: number = 12;
/**
* Make an inventory.
*/
constructor(items: Item[] = [], max_items: number) {
super(img`
.
`)
this._items = items;
this._max_items = max_items;
this.update();
}
/**
* Get the selected item.
*/
public get selected() {
return this._selected;
}
/**
* Set the selected item.
*/
public set selected(index: number) {
this._selected = index;
this.update();
}
/**
* Get the items in the inventory.
*/
public get items() {
return this._items;
}
/**
* Get the items in the inventory. Only rewrapped for blocks.
* @return: The items - as an array of Item.
*/
//% block="inventory %Inventory(inventory) get items"
//% weight=80
//% group="Inventory"
//% hidden
public get_items() {
return this.items;
}
/**
* Set the items in the inventory.
*/
public set items(new_items: Item[]) {
this._items = new_items;
this.update();
}
/**
* Set the items in the inventory. Only rewrapped for blocks.
* @new_items: A list of Item.
*/
//% block="inventory %Inventory(inventory) set items to %new_items"
//% new_items.shadow="lists_create_with"
//% weight=90
//% group="Inventory"
//% hidden
public set_items(new_items: Item[]) {
this.items = new_items;
}
/**
* Get the maximum amount of items in the inventory.
*/
public get max_items() {
return this._max_items;
}
/**
* Set the maximum amount of items in the inventory.
*/
public set max_items(new_max: number) {
this._max_items = new_max;
this.update();
}
/**
* Set the selected index or max items. Only rewrapped for blocks.
* @attribute: A property of the InventoryNumberAttribute enum.
* @value: The new number.
*/
//% block="inventory %Inventory(inventory) set %attribute to %value"
//% weight=70
//% group="Inventory"
//% hidden
public set_number(attribute: InventoryNumberAttribute, value: number) {
if (attribute == InventoryNumberAttribute.SelectedIndex) {
this.selected = value;
} else if (attribute == InventoryNumberAttribute.MaxItems) {
this.max_items = value;
}
}
/**
* Change the selected index or max items. Only rewrapped for blocks.
* @attribute: A property of the InventoryNumberAttribute enum.
* @change: The value to change the number by.
*/
//% block="inventory %Inventory(inventory) change %attribute by %change"
//% weight=65
//% group="Inventory"
//% hidden
public change_number(attribute: InventoryNumberAttribute, change: number) {
this.set_number(attribute, this.get_number(attribute) + change);
}
/**
* Get the selected index or max items. Only rewrapped for blocks.
* @attribute: A property of the InventoryNumberAttribute enum.
* @return: The number.
*/
//% block="inventory %Inventory(inventory) get %attribute"
//% weight=60
//% group="Inventory"
//% hidden
public get_number(attribute: InventoryNumberAttribute) {
if (attribute == InventoryNumberAttribute.SelectedIndex) {
return this.selected;
} else if (attribute == InventoryNumberAttribute.MaxItems) {
return this.max_items;
}
return -1;
}
/**
* Get the text in the inventory.
*/
public get text() {
return this._text;
}
/**
* Get the text in the inventory. Only rewrapped for blocks.
* @return: A string.
*/
//% block="inventory %Inventory(inventory) get text"
//% weight=40
//% group="Inventory"
//% hidden
public get_text() {
return this.text;
}
/**
* Set the text in the inventory.
*/
public set text(new_text: string) {
this._text = new_text;
this.update();
}
/**
* Set the text in the inventory. Only rewrapped for blocks.
* @new_text: The new text.
*/
//% block="inventory %Inventory(inventory) set text to %new_text"
//% weight=50
//% group="Inventory"
//% hidden
public set_text(new_text: string) {
this.text = new_text;
}
/**
* Set a specific part of the inventory to a specific color.
* @param attribute: A property of the InventoryColorAttribute enum.
* @param color: A number which should be the new color of the attribute.
*/
//% block="inventory %Inventory(inventory) set color of %attribute to %color"
//% color.shadow=colorindexpicker
//% weight=30
//% group="Inventory"
public set_color(attribute: InventoryColorAttribute, color: number) {
if (attribute == InventoryColorAttribute.InventoryOutline) {
this._inv_outline_color = color;
} else if (attribute == InventoryColorAttribute.InventorySelectedOutline) {
this._inv_selected_outline_color = color;
} else if (attribute == InventoryColorAttribute.InventoryBackground) {
this._inv_background_color = color;
} else if (attribute == InventoryColorAttribute.InventoryText) {
this._inv_text_color = color;
}
this.update();
}
/**
* Get the color of a specific part of the inventory.
* @param attribute: A property of the InventoryColorAttribute enum.
* @return: The color (which is a number) of the attribute, otherwise -1.
*/
//% block="inventory %Inventory(inventory) get color of %attribute"
//% weight=20
//% group="Inventory"
public get_color(attribute: InventoryColorAttribute) {
if (attribute == InventoryColorAttribute.InventoryOutline) {
return this._inv_outline_color;
} else if (attribute == InventoryColorAttribute.InventorySelectedOutline) {
return this._inv_selected_outline_color;
} else if (attribute == InventoryColorAttribute.InventoryBackground) {
return this._inv_background_color;
} else if (attribute == InventoryColorAttribute.InventoryText) {
return this._inv_text_color;
}
return -1;
}
/**
* Update the image of the inventory.
*/
//% block="inventory %Inventory(Inventory) force redraw"
//% weight=10
//% group="Inventory"
public update() {
let image_size: number = 16;
let padding: number = 1;
let box_size: number = image_size + (padding * 2);
let outside_padding: number = 4;
let width: number = scene.screenWidth() - (outside_padding * 2);
let height: number = scene.screenHeight() - (outside_padding * 2) - 24;
let new_image = image.create(width, height);
new_image.fill(this._inv_background_color);
new_image.drawRect(0, 0, width, height, this._inv_outline_color);
new_image.print(this.text, 2, 2, this._inv_text_color);
if (this.selected < this.items.length && this.selected != -1) {
let text: string = this.items[this.selected].name;
this.print_right_aligned(new_image, text, width - 2, 2, this._inv_text_color, false);
}
new_image.drawLine(2, 11, width - 3, 11, this._inv_outline_color);
for (let index = 0; index < this.max_items; index++) {
if (index > this.max_items - 1) {
return;
}
let x: number = ((index % 8) * box_size) + 4;
let y: number = (Math.idiv(index, 8) * box_size) + 14;
if (index < this.items.length) {
if (index == this.selected) {
new_image.fillRect(x - 1, y - 1, box_size, box_size,
this._inv_selected_outline_color);
}
spriteutils.drawTransparentImage(this.items[index].image, new_image, x, y);
this.print_right_aligned(new_image, this.items[index].tooltip,
x + box_size, y + (box_size - 5) - 1, this._inv_text_color,
true);
}
}
this.setImage(new_image);
}
/**
* Print some text right-aligned
* @param target: The image to draw too.
* @param text: The text to draw.
* @param right: the X coordinate of the right of the text.
* @param y: The Y coordinate of the text.
* @param color: The color of the text.
* @param use_small_font: Whether to use the 5x5 instead of the 5x8 font.
*/
private print_right_aligned(target: Image, text: string,
right: number, y: number,
color: number, use_small_font: boolean = false) {
let text_length_px: number = text.length * 6;
let label_x: number = right - text_length_px;
target.print(text, label_x, y, color, use_small_font? image.font5: image.font8);
}
}
/**
* Create a new inventory - for blocks.
*/
//% block="create inventory with items %items and max items %max_items"
//% blockSetVariable=inventory
//% items.shadow="lists_create_with"
//% max_items.dfl=3
//% weight=100
//% group="Inventory"
//% hidden
export function create_inventory(items: Item[], max_items: number) {
return new Inventory(items, max_items);
}
}