-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.ts
More file actions
156 lines (151 loc) · 5.43 KB
/
notifications.ts
File metadata and controls
156 lines (151 loc) · 5.43 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
namespace SpriteKind {
export const Notification = SpriteKind.create();
}
//% color="#0000cc" icon="\uf075"
namespace Notification {
let notification: Sprite = null;
let moving_notification: boolean = false;
/**
* Display some text with an optional icon at the top of the screen.
* @param rawText: A string of text to display.
* @param speed: The speed multiplier for how fast the notification shows.
* @param icon: A 8x8 image as the icon. If the dimensions do not fit, no icon will be displayed.
*/
//% block="Notify with text $rawText || at speed $speed | with icon $icon"
//% rawText.defl="Notification!"
//% speed.defl=1
//% icon.shadow=screen_image_picker
//% expandableArgumentMode="enabeled"
//% weight=100
export function notify(rawText: string, speed?: number, icon?: Image) {
// Replace newlines with spaces
const text = console.inspect(rawText).split("\n").join(" ");
let font = image.getFontForText(text);
let padding = 4;
let holdTime = 1000; // ms
let textTime = ((text.length * font.charWidth) / 40) * 1000;
let textOffset = 0;
let textTimeMultiplier = 1;
if (speed) {
textTimeMultiplier = speed;
}
let imageWidth = 156;
let textLength = Math.max(text.length * font.charWidth, 24 * font.charWidth);
let imageHeight = font.charHeight + padding;
let bubble = image.create(imageWidth, imageHeight);
let hasIcon = false;
if (icon && icon.width == 8 && icon.height == 8) {
hasIcon = true;
}
notification = sprites.create(img`
.
`, SpriteKind.Notification);
notification.setFlag(SpriteFlag.Ghost, true);
notification.setFlag(SpriteFlag.RelativeToCamera, true);
function clearBubble() {
bubble.fill(1);
}
function printToBubble(str: string, x: number) {
if (hasIcon) {
bubble.print(str, x+10, 2, 15, font);
} else {
bubble.print(str, x, 2, 15, font);
}
}
function padBubble() {
// Left padding
if (hasIcon) {
bubble.fillRect(0, 0, padding+10, imageHeight, 1);
spriteutils.drawTransparentImage(icon, bubble, padding-1, 2);
} else {
bubble.fillRect(0, 0, padding, imageHeight, 1);
}
// Right side padding
bubble.fillRect(imageWidth - padding, 0, padding, font.charHeight + padding, 1);
}
function roundBubbleEdges() {
bubble.setPixel(0, 0, 0);
bubble.setPixel(imageWidth - 1, 0, 0);
bubble.setPixel(0, font.charHeight + padding - 1, 0);
bubble.setPixel(imageWidth - 1, font.charHeight + padding - 1, 0);
}
clearBubble();
printToBubble(text, padding);
padBubble();
roundBubbleEdges();
notification.setImage(bubble);
notification.left = 2;
notification.z = 100000000000;
moving_notification = true;
for (notification.bottom = -2; notification.top < 2; notification.top++) {
pause(50);
}
notification.top = 2;
moving_notification = false;
let totalLength;
pause(holdTime / textTimeMultiplier);
for (let i = 0; i < Math.abs(text.length * font.charWidth); i++) {
totalLength = Math.abs(text.length * font.charWidth) + (padding * 2) + textOffset;
if (hasIcon) {
totalLength += 10;
}
if (text.length > 24 && totalLength > imageWidth) {
clearBubble();
printToBubble(text, padding + textOffset);
padBubble();
roundBubbleEdges();
textOffset -= 1;
}
pause(textTime / Math.abs(text.length * font.charWidth) / textTimeMultiplier);
}
pause(holdTime / textTimeMultiplier);
moving_notification = true;
for (notification.top = 2; notification.bottom > -2; notification.bottom--) {
pause(50);
}
notification.bottom = -2;
moving_notification = false;
notification.destroy();
}
/**
* Returns whether we are displaying a notification or not.
*/
//% block="Showing notification"
//% weight=70
export function isNotifying(): boolean {
return !(spriteutils.isDestroyed(notification));
}
/**
* Blocks until no notifications are displaying; returns immediately if no notification is displaying.
*/
//% block="Wait for notification to finish"
//% weight=80
export function waitForNotificationFinish() {
while (Notification.isNotifying()) {
pause(0);
}
}
/**
* Cancels the current notification.
* (Technically, the last notification cause we only keep a pointer to the last one)
*/
//% block="Cancel the current notification"
//% weight=90
export function cancelNotification() {
if (!isNotifying()) {
return;
}
while (moving_notification) {
pause(0);
}
pause(50);
for ( ; notification.bottom > -2; notification.bottom--) {
while (moving_notification) {
pause(0);
}
pause(50);
}
notification.bottom = -2;
notification.destroy();
}
}