Skip to content

Commit 2361292

Browse files
Create blocks for inventory class and add weights
1 parent bf26c40 commit 2361292

File tree

5 files changed

+116
-10
lines changed

5 files changed

+116
-10
lines changed

.github/makecode/blocks.png

10.9 KB
Loading

.github/makecode/blocksdiff.png

-22 KB
Loading

inventory.ts

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ enum ItemTextAttribute {
2525
Description
2626
}
2727

28+
enum ToolbarNumberAttribute {
29+
//% block="selected index"
30+
SelectedIndex,
31+
//% block="max items"
32+
MaxItems
33+
}
34+
2835
//% color="#7732B3"
2936
//% group="['Item', 'Toolbar', 'Inventory']"
3037
namespace Inventory {
@@ -59,6 +66,7 @@ namespace Inventory {
5966
* @param value: The new text of the item.
6067
*/
6168
//% block="%Inventory(Item) set text of %attribute to %value"
69+
//% weight=20
6270
//% group="Item"
6371
set_text(attribute: ItemTextAttribute, value: string) {
6472
if (attribute == ItemTextAttribute.Name) {
@@ -68,16 +76,42 @@ namespace Inventory {
6876
}
6977
}
7078

79+
/**
80+
* Get the name of description of the item.
81+
*/
82+
//% block="%Inventory(Item) get text of %attribute"
83+
//% weight=10
84+
//% group="Item"
85+
get_text(attribute: ItemTextAttribute) {
86+
if (attribute == ItemTextAttribute.Name) {
87+
return this.name;
88+
} else if (attribute == ItemTextAttribute.Description) {
89+
return this.description;
90+
}
91+
return "";
92+
}
93+
7194
/**
7295
* Set the image of the item.
7396
* @param new_image: The new image.
7497
*/
7598
//% block="%Inventory(Item) set image to %new_image"
7699
//% new_image.shadow=screen_image_picker
100+
//% weight=40
77101
//% group="Item"
78102
set_image(new_image: Image) {
79103
this.image = new_image;
80104
}
105+
106+
/**
107+
* Get the image of the item.
108+
*/
109+
//% block="%Inventory(Item) get image"
110+
//% weight=30
111+
//% group="Item"
112+
get_image() {
113+
return this.image;
114+
}
81115
}
82116

83117
/**
@@ -89,6 +123,7 @@ namespace Inventory {
89123
//% image.shadow=screen_image_picker
90124
//% expandableArgumentMode="toggle"
91125
//% description.dfl="Description"
126+
//% weight=50
92127
//% group="Item"
93128
export function create_item(name: string, image: Image, description: string = null) {
94129
return new Item(name, image, description)
@@ -142,6 +177,16 @@ namespace Inventory {
142177
return this._items;
143178
}
144179

180+
/**
181+
* Get the items in the toolbar. Only rewrapped for blocks.
182+
*/
183+
//% block="%Inventory(toolbar) get items"
184+
//% weight=80
185+
//% group="Toolbar"
186+
public get_items() {
187+
return this.items;
188+
}
189+
145190
/**
146191
* Set the items in the toolbar.
147192
*/
@@ -150,6 +195,17 @@ namespace Inventory {
150195
this.update();
151196
}
152197

198+
/**
199+
* Set the items in the toolbar. Only rewrapped for blocks.
200+
*/
201+
//% block="%Inventory(toolbar) set items to %new_items"
202+
//% new_items.shadow="lists_create_with"
203+
//% weight=90
204+
//% group="Toolbar"
205+
public set_items(new_items: Item[]) {
206+
this.items = new_items;
207+
}
208+
153209
/**
154210
* Get the maximum amount of items in the toolbar.
155211
*/
@@ -165,11 +221,44 @@ namespace Inventory {
165221
this.update();
166222
}
167223

224+
/**
225+
* Set the selected index or max items. Only rewrapped for blocks.
226+
*/
227+
//% block="%Inventory(toolbar) set %attribute to %value"
228+
//% weight=70
229+
//% group="Toolbar"
230+
public set_number(attribute: ToolbarNumberAttribute, value: number) {
231+
if (attribute == ToolbarNumberAttribute.SelectedIndex) {
232+
this.selected = value;
233+
} else if (attribute == ToolbarNumberAttribute.MaxItems) {
234+
this.max_items = value;
235+
}
236+
}
237+
238+
/**
239+
* Get the selected index or max items. Only rewrapped for blocks.
240+
*/
241+
//% block="%Inventory(toolbar) get %attribute"
242+
//% weight=60
243+
//% group="Toolbar"
244+
public get_number(attribute: ToolbarNumberAttribute) {
245+
if (attribute == ToolbarNumberAttribute.SelectedIndex) {
246+
return this.selected;
247+
} else if (attribute == ToolbarNumberAttribute.MaxItems) {
248+
return this.max_items;
249+
}
250+
return -1;
251+
}
252+
168253
/**
169254
* Set a specific part of the toolbar to a specific color.
170255
* @param attribute: A property of the ToolbarColorAttribute enum.
171256
* @param color: A number which should be the new color of the attribute.
172257
*/
258+
//% block="%Inventory(toolbar) set color of %attribute to %color"
259+
//% color.shadow=colorindexpicker
260+
//% weight=50
261+
//% group="Toolbar"
173262
public set_color(attribute: ToolbarColorAttribute, color: number) {
174263
if (attribute == ToolbarColorAttribute.BoxOutline) {
175264
this._box_outline_color = color;
@@ -186,6 +275,9 @@ namespace Inventory {
186275
* @param attribute: A property of the ToolbarColorAttribute enum.
187276
* @return: The color (which is a number) of the attribute, otherwise -1.
188277
*/
278+
//% block="%Inventory(toolbar) get color of %attribute"
279+
//% weight=40
280+
//% group="Toolbar"
189281
public get_color(attribute: ToolbarColorAttribute) {
190282
if (attribute == ToolbarColorAttribute.BoxOutline) {
191283
return this._box_outline_color;
@@ -200,7 +292,9 @@ namespace Inventory {
200292
/**
201293
* Update the image of the toolbar.
202294
*/
203-
protected update() {
295+
//% block="%Inventory(toolbar) force redraw of toolbar"
296+
//% weight=30
297+
public update() {
204298
let image_size: number = 16;
205299
let padding: number = 2;
206300
let box_size: number = image_size + (padding * 2);
@@ -238,6 +332,19 @@ namespace Inventory {
238332
}
239333
}
240334

335+
/**
336+
* Create a new toolbar - for blocks.
337+
*/
338+
//% block="create toolbar with items %items and max items %max_items"
339+
//% blockSetVariable=toolbar
340+
//% items.shadow="lists_create_with"
341+
//% max_items.dfl=3
342+
//% weight=100
343+
//% group="Toolbar"
344+
export function create_toolbar(items: Item[], max_items: number) {
345+
return new Toolbar(items, max_items);
346+
}
347+
241348
/**
242349
* Create an inventory sprite which is just a regular sprite.
243350
*/
@@ -365,7 +472,7 @@ namespace Inventory {
365472
/**
366473
* Update the image of the inventory.
367474
*/
368-
protected update() {
475+
public update() {
369476
let image_size: number = 16;
370477
let padding: number = 1;
371478
let box_size: number = image_size + (padding * 2);

main.blocks

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="9Cf@8V`E:WHDR~m2B^,U">tb</variable><variable id=",(uOq{lxakF;nPDwx4c]">stuff</variable><variable id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</variable><variable id="M~w)hn!2hv3g!ud^v)r0">inv</variable></variables><block type="pxt-on-start" id="ciDH1w$_E|!vhd`j^0sG" x="0" y="0"><statement name="HANDLER"><block type="spriteutilextsetconsolevisible" id="|T*_ny1D!z/Yh^A{]U$G"><value name="on"><shadow type="toggleOnOff" id="nE`=msm,zJ8ER}cl5v!!"><field name="on">true</field></shadow></value><next><block type="gamesetbackgroundcolor" id="e!G^88Ni_#|ccyMk)[2g"><value name="color"><shadow type="colorindexpicker" id="XU]8DOaDwJ`7sTrN^{8I"><field name="index">7</field></shadow></value><next><block type="variables_set" id="e,[csqyD!gHV9a79%RhO"><field name="VAR" id=",(uOq{lxakF;nPDwx4c]">stuff</field><value name="VALUE"><shadow type="math_number" id="ZjMx93[t${{6O6X$?Y3{"><field name="NUM">0</field></shadow><block type="lists_create_with" id="/^Bj^T,}KepfeRcLoFPz"><mutation items="3"/><value name="ADD0"><block type="Inventory_create_item" id="@]Aa$x`#WYQtR7)nD!TD"><mutation xmlns="http://www.w3.org/1999/xhtml" _expanded="0" _input_init="false"></mutation><value name="name"><shadow type="text" id="=Gi{Lp);Uh+]^0}c*Omj"><field name="TEXT">Apple</field></shadow></value><value name="image"><shadow type="screen_image_picker" id="C*Jj0V6f(`UxDmM[KZQr"><field name="img">img`
1+
<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</variable><variable id="M~w)hn!2hv3g!ud^v)r0">inv</variable><variable id="%8P,Y?##mLxxx6@=UhAU">toolbar</variable></variables><block type="pxt-on-start" id="ciDH1w$_E|!vhd`j^0sG" x="0" y="0"><statement name="HANDLER"><block type="spriteutilextsetconsolevisible" id="|T*_ny1D!z/Yh^A{]U$G"><value name="on"><shadow type="toggleOnOff" id="nE`=msm,zJ8ER}cl5v!!"><field name="on">true</field></shadow></value><next><block type="gamesetbackgroundcolor" id="e!G^88Ni_#|ccyMk)[2g"><value name="color"><shadow type="colorindexpicker" id="XU]8DOaDwJ`7sTrN^{8I"><field name="index">7</field></shadow></value><next><block type="variables_set" id="KZ{yU=MsC|-b0lzS|jQe"><field name="VAR" id="%8P,Y?##mLxxx6@=UhAU">toolbar</field><value name="VALUE"><shadow type="math_number" id="lkHS#qy-$u}oN5upI2z{" disabled="true"><field name="NUM">0</field></shadow><block type="Inventory_create_toolbar" id="%o*fP`cGc*8aS1oG-Zap"><value name="items"><block type="lists_create_with" id="/^Bj^T,}KepfeRcLoFPz"><mutation items="3"/><value name="ADD0"><block type="Inventory_create_item" id="@]Aa$x`#WYQtR7)nD!TD"><mutation xmlns="http://www.w3.org/1999/xhtml" _expanded="0" _input_init="false"></mutation><value name="name"><shadow type="text" id="=Gi{Lp);Uh+]^0}c*Omj"><field name="TEXT">Apple</field></shadow></value><value name="image"><shadow type="screen_image_picker" id="C*Jj0V6f(`UxDmM[KZQr"><field name="img">img`
22
. . . . . . . e c 7 . . . . . .
33
. . . . e e e c 7 7 e e . . . .
44
. . c e e e e c 7 e 2 2 e e . .
@@ -49,7 +49,7 @@ b d 3 2 d 5 5 5 d d d 4 4 . . .
4949
b 5 5 5 5 d d 4 4 4 4 . . . . .
5050
4 d d d 4 4 4 . . . . . . . . .
5151
4 4 4 4 . . . . . . . . . . . .
52-
`</field><data>{"commentRefs":[],"fieldData":{"img":null}}</data></shadow></value></block></value></block></value><next><block type="typescript_statement" id="K~c~d/q1Ob.9W2txO6x:" editable="false"><mutation xmlns="http://www.w3.org/1999/xhtml" line0="let tb = new Inventory.Toolbar(stuff, 3);" numlines="1" declaredvars="tb"></mutation><next><block type="Sprite_blockCombine_set" id="U2Z201f5R!=x(ZhOTlHB"><field name="property">Sprite.left@set</field><value name="mySprite"><block type="variables_get" id="E[{41[wBC#oLj%`.VRIq"><field name="VAR" id="9Cf@8V`E:WHDR~m2B^,U">tb</field></block></value><value name="value"><shadow type="math_number" id="ivw[Wt)/oP%xQr]G9=0;"><field name="NUM">4</field></shadow></value><next><block type="Sprite_blockCombine_set" id="l(j#*`pDE`X%rK]e[3|L"><field name="property">Sprite.bottom@set</field><value name="mySprite"><block type="variables_get" id="|mwBV1q81)Tx{lqf7OQM"><field name="VAR" id="9Cf@8V`E:WHDR~m2B^,U">tb</field></block></value><value name="value"><block type="math_arithmetic" id=":~#5-/8Dby@q}6+%~0_!"><field name="OP">MINUS</field><value name="A"><shadow type="math_number"><field name="NUM">0</field></shadow><block type="scenescreenheight" id="NUC!|c:qc8|Ah_H-PZR`"/></value><value name="B"><shadow type="math_number" id="X+k0!HwYSIwoCw:t6_bi"><field name="NUM">4</field></shadow></value></block></value><next><block type="variables_set" id="Kq-=*):0s#6OWv~z=koQ"><field name="VAR" id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</field><value name="VALUE"><shadow type="math_number" id=";01CIAdyET|r1C%Mz~DA" disabled="true"><field name="NUM">0</field></shadow><block type="lists_create_with" id="p(B0J%*p~XC:SElB4hYX"><mutation items="0"/></block></value><next><block type="controls_repeat_ext" id="j1l_O;JtRmp2sE}VFEbj"><value name="TIMES"><shadow type="math_whole_number" id="!nS4jz/3)M/(.l#|J!1!"><field name="NUM">30</field></shadow></value><statement name="DO"><block type="array_push" id="MylATnWYLGSva.ri@4aP"><value name="list"><block type="variables_get" id="NEGf/75}%fl3V/?EH?o?"><field name="VAR" id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</field></block></value><value name="value"><block type="Inventory_create_item" id="xg$Ml=),@C?-/5]xsXiq"><mutation xmlns="http://www.w3.org/1999/xhtml" _expanded="0" _input_init="false"></mutation><value name="name"><shadow type="text" id="7K3=hJ}UsuWS@CslxuWc"><field name="TEXT">Apple</field></shadow></value><value name="image"><shadow type="screen_image_picker" id="1ypAW:UuJ0y60RuD~]k0"><field name="img">img`
52+
`</field><data>{"commentRefs":[],"fieldData":{"img":null}}</data></shadow></value></block></value></block></value><value name="max_items"><shadow type="math_number" id=";{XJv*oFCG(A8lgxD[xN"><field name="NUM">3</field></shadow></value></block></value><next><block type="Sprite_blockCombine_set" id="U2Z201f5R!=x(ZhOTlHB"><field name="property">Sprite.left@set</field><value name="mySprite"><block type="variables_get" id="E[{41[wBC#oLj%`.VRIq"><field name="VAR" id="%8P,Y?##mLxxx6@=UhAU">toolbar</field></block></value><value name="value"><shadow type="math_number" id="ivw[Wt)/oP%xQr]G9=0;"><field name="NUM">4</field></shadow></value><next><block type="Sprite_blockCombine_set" id="l(j#*`pDE`X%rK]e[3|L"><field name="property">Sprite.bottom@set</field><value name="mySprite"><block type="variables_get" id="|mwBV1q81)Tx{lqf7OQM"><field name="VAR" id="%8P,Y?##mLxxx6@=UhAU">toolbar</field></block></value><value name="value"><block type="math_arithmetic" id=":~#5-/8Dby@q}6+%~0_!"><field name="OP">MINUS</field><value name="A"><shadow type="math_number"><field name="NUM">0</field></shadow><block type="scenescreenheight" id="NUC!|c:qc8|Ah_H-PZR`"/></value><value name="B"><shadow type="math_number" id="X+k0!HwYSIwoCw:t6_bi"><field name="NUM">4</field></shadow></value></block></value><next><block type="variables_set" id="Kq-=*):0s#6OWv~z=koQ"><field name="VAR" id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</field><value name="VALUE"><shadow type="math_number" id=";01CIAdyET|r1C%Mz~DA" disabled="true"><field name="NUM">0</field></shadow><block type="lists_create_with" id="p(B0J%*p~XC:SElB4hYX"><mutation items="0"/></block></value><next><block type="controls_repeat_ext" id="j1l_O;JtRmp2sE}VFEbj"><value name="TIMES"><shadow type="math_whole_number" id="!nS4jz/3)M/(.l#|J!1!"><field name="NUM">30</field></shadow></value><statement name="DO"><block type="array_push" id="MylATnWYLGSva.ri@4aP"><value name="list"><block type="variables_get" id="NEGf/75}%fl3V/?EH?o?"><field name="VAR" id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</field></block></value><value name="value"><block type="Inventory_create_item" id="xg$Ml=),@C?-/5]xsXiq"><mutation xmlns="http://www.w3.org/1999/xhtml" _expanded="0" _input_init="false"></mutation><value name="name"><shadow type="text" id="7K3=hJ}UsuWS@CslxuWc"><field name="TEXT">Apple</field></shadow></value><value name="image"><shadow type="screen_image_picker" id="1ypAW:UuJ0y60RuD~]k0"><field name="img">img`
5353
. . . . . . . e c 7 . . . . . .
5454
. . . . e e e c 7 7 e e . . . .
5555
. . c e e e e c 7 e 2 2 e e . .
@@ -66,4 +66,4 @@ c e e 2 2 2 2 2 2 2 2 2 2 4 2 e
6666
. . 2 e e 2 2 2 2 2 4 4 2 e . .
6767
. . . 2 2 e e 4 4 4 2 e e . . .
6868
. . . . . 2 2 e e e e . . . . .
69-
`</field><data>{"commentRefs":[],"fieldData":{"img":null}}</data></shadow></value></block></value></block></statement><next><block type="typescript_statement" id="Ep?G!P=daZi?#cc|D~Q{" editable="false"><mutation xmlns="http://www.w3.org/1999/xhtml" line0="let inv = new Inventory.Inventory(inv_contents, 32);" numlines="1" declaredvars="inv"></mutation><next><block type="Sprite_blockCombine_set" id=";QnO;`_85r}zr={!EbzJ"><field name="property">Sprite.left@set</field><value name="mySprite"><block type="variables_get" id=";EsAn;RYMrU=KO7jOJuf"><field name="VAR" id="M~w)hn!2hv3g!ud^v)r0">inv</field></block></value><value name="value"><shadow type="math_number" id="IoY1s#NPn/:~H0KGR4t3"><field name="NUM">4</field></shadow></value><next><block type="Sprite_blockCombine_set" id="1b[P,lbS@A`ddAx@3-No"><field name="property">Sprite.top@set</field><value name="mySprite"><block type="variables_get" id="#Y;O5:@lisQq#8*z.SA#"><field name="VAR" id="M~w)hn!2hv3g!ud^v)r0">inv</field></block></value><value name="value"><shadow type="math_number" id="{D9zZZIi@-mfdtD}#+UX"><field name="NUM">4</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block></xml>
69+
`</field><data>{"commentRefs":[],"fieldData":{"img":null}}</data></shadow></value></block></value></block></statement><next><block type="typescript_statement" id="Ep?G!P=daZi?#cc|D~Q{" editable="false"><mutation xmlns="http://www.w3.org/1999/xhtml" line0="let inv = new Inventory.Inventory(inv_contents, 32);" numlines="1" declaredvars="inv"></mutation><next><block type="Sprite_blockCombine_set" id=";QnO;`_85r}zr={!EbzJ"><field name="property">Sprite.left@set</field><value name="mySprite"><block type="variables_get" id=";EsAn;RYMrU=KO7jOJuf"><field name="VAR" id="M~w)hn!2hv3g!ud^v)r0">inv</field></block></value><value name="value"><shadow type="math_number" id="IoY1s#NPn/:~H0KGR4t3"><field name="NUM">4</field></shadow></value><next><block type="Sprite_blockCombine_set" id="1b[P,lbS@A`ddAx@3-No"><field name="property">Sprite.top@set</field><value name="mySprite"><block type="variables_get" id="#Y;O5:@lisQq#8*z.SA#"><field name="VAR" id="M~w)hn!2hv3g!ud^v)r0">inv</field></block></value><value name="value"><shadow type="math_number" id="{D9zZZIi@-mfdtD}#+UX"><field name="NUM">4</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block></xml>

main.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spriteutils.setConsoleOverlay(true)
22
scene.setBackgroundColor(7)
3-
let stuff = [Inventory.create_item("Apple", img`
3+
let toolbar = Inventory.create_toolbar([Inventory.create_item("Apple", img`
44
. . . . . . . e c 7 . . . . . .
55
. . . . e e e c 7 7 e e . . . .
66
. . c e e e e c 7 e 2 2 e e . .
@@ -51,10 +51,9 @@ let stuff = [Inventory.create_item("Apple", img`
5151
b 5 5 5 5 d d 4 4 4 4 . . . . .
5252
4 d d d 4 4 4 . . . . . . . . .
5353
4 4 4 4 . . . . . . . . . . . .
54-
`)]
55-
let tb = new Inventory.Toolbar(stuff, 3);
56-
tb.left = 4
57-
tb.bottom = scene.screenHeight() - 4
54+
`)], 3)
55+
toolbar.left = 4
56+
toolbar.bottom = scene.screenHeight() - 4
5857
let inv_contents: Inventory.Item[] = []
5958
for (let index = 0; index < 30; index++) {
6059
inv_contents.push(Inventory.create_item("Apple", img`

0 commit comments

Comments
 (0)