-
Notifications
You must be signed in to change notification settings - Fork 1
/
inventoryitem.js
85 lines (67 loc) · 2.09 KB
/
inventoryitem.js
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
/* Item constants */
const STYLE = {
sprite: { w: 0.05, h: 0.044 },
padding: { x: 0.005, y: 0.005 },
label: { h: 0.035 }
};
const SIZE = {
w: STYLE.sprite.w + STYLE.padding.x * 2,
h: STYLE.sprite.h + STYLE.label.h + STYLE.padding.y * 2
};
/* For default style */
function getInventoryItemSheetStyle() {
return `inventory-item {
background-color: #242730;
color: #FFFFF0;
font-family: 0;
font-size: 0.25;
}
inventory-item:active {
background-color: #893240;
}
inventory-item:disabled {
color: #4F5259;
}
inventory-item:hover {
background-color: #F21951;
}`;
}
function inventoryItem(text, spriteDict, spriteName) {
/* Get currently processing frame (window) */
const frame = exports.vein.getFrame();
const style = frame.getStyle();
/* 1. Decide item size and begin it */
frame.beginItem(SIZE.w, SIZE.h);
/* 2. Process user input */
const isClicked = frame.isItemClicked();
/* 3. Draw item */
/* Declare selector by considering user-defined styleId and item state */
const selector = frame.buildItemStyleSelector(
'inventory-item',
frame.isItemDisabled() ? 'disabled' : frame.isItemPressed() ? 'active' : frame.isItemHovered() ? 'hover' : null
);
const p = frame.getPainter();
/* Background */
const backgroundColor = style.getProperty(selector, 'background-color');
p.setColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]);
p.drawRect(SIZE.w, SIZE.h);
/* Sprite */
p.move(STYLE.padding.x, STYLE.padding.y);
p.setColor(254, 254, 254, 255);
p.drawSprite(spriteDict, spriteName, STYLE.sprite.w, STYLE.sprite.h);
p.move(-STYLE.padding.x, -STYLE.padding.y);
/* Text */
const font = style.getProperty(selector, 'font-family');
const scale = style.getProperty(selector, 'font-size');
p.move(
(SIZE.w - p.getTextWidth(text, font, scale)) / 2,
STYLE.sprite.h + STYLE.label.h / 2 + STYLE.padding.y
);
const color = style.getProperty(selector, 'color');
p.setColor(color[0], color[1], color[2], color[3]);
p.drawText(text, font, scale);
/* 4. End item */
frame.endItem();
/* 5. Return everything you need */
return isClicked;
}