-
Notifications
You must be signed in to change notification settings - Fork 14
/
venusLedMatrixUI.ts
235 lines (192 loc) · 6.06 KB
/
venusLedMatrixUI.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
import * as vscode from 'vscode'
import fs from 'fs'
/**
* Manages a LED Matrix
*/
export class VenusLedMatrixUI {
/**
* Track the currently panel. Only allow a single panel to exist at a time.
*/
public static instance: VenusLedMatrixUI | undefined;
public static readonly viewType = 'VenusLedMatrixUI';
private _panel: vscode.WebviewPanel;
private static _extensionUri: vscode.Uri;
private _disposables: vscode.Disposable[] = [];
private static _uiState: UIState;
public static getInstance(): VenusLedMatrixUI {
if (VenusLedMatrixUI.instance) {
return VenusLedMatrixUI.instance
} else {
VenusLedMatrixUI.instance = new VenusLedMatrixUI(undefined, VenusLedMatrixUI._uiState)
return VenusLedMatrixUI.instance
}
}
/** Closes the old instance if available and opens a new one */
public static createNewInstance(extensionUri?: vscode.Uri, uiState?: UIState): VenusLedMatrixUI {
if (VenusLedMatrixUI.instance) {
VenusLedMatrixUI.instance.dispose();
VenusLedMatrixUI.instance = new VenusLedMatrixUI(extensionUri, uiState)
} else {
VenusLedMatrixUI.instance = new VenusLedMatrixUI(extensionUri, uiState)
}
return VenusLedMatrixUI.instance
}
private constructor(extensionUri?: vscode.Uri, uiState?: UIState) {
if (extensionUri)
VenusLedMatrixUI._extensionUri = extensionUri
if (uiState) {
VenusLedMatrixUI._uiState = uiState;
} else {
VenusLedMatrixUI._uiState = new UIState(new LedMatrix(10, 10))
}
}
public dispose() {
VenusLedMatrixUI.instance = undefined;
// Clean up our resources
if (this._panel != null) {
this._panel.dispose();
}
while (this._disposables.length) {
const x = this._disposables.pop();
if (x) {
x.dispose();
}
}
}
public show(column? : vscode.ViewColumn) {
// If we already have a panel, show it.
if (VenusLedMatrixUI.instance?._panel) {
VenusLedMatrixUI.instance._panel.reveal();
} else {
this._addPanel(column);
}
}
private _addPanel(column? : vscode.ViewColumn) {
// Otherwise, create a new panel.
const panel = vscode.window.createWebviewPanel(
VenusLedMatrixUI.viewType,
'LED Matrix',
column ? column : vscode.ViewColumn.Beside,
{
// Enable javascript in the webview
enableScripts: true,
// And restrict the webview to only loading content from our extension's `ledmatrix` directory.
localResourceRoots: [vscode.Uri.joinPath(VenusLedMatrixUI._extensionUri, 'src', 'ledmatrix')]
}
);
this._panel = panel;
// Set the webview's initial html content
const webview = this._panel.webview;
this._panel.webview.html = this._getHtmlForWebview(webview);
// Listen for when the panel is disposed
// This happens when the user closes the panel or when the panel is closed programatically
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
// Update the content based on view changes
this._panel.onDidChangeViewState(
e => {
if (this._panel.visible) {
this._update();
}
},
null,
this._disposables
);
// Handle messages from the webview
this._panel.webview.onDidReceiveMessage(
message => {
switch (message.command) {
case 'alert':
vscode.window.showErrorMessage(message.text);
return;
}
},
null,
this._disposables
);
}
private _update() {
this._panel?.webview.postMessage({command: "loadState", uiState: VenusLedMatrixUI._uiState})
}
private _getHtmlForWebview(webview: vscode.Webview, ) {
const htmlPathOnDisk = vscode.Uri.joinPath(VenusLedMatrixUI._extensionUri, '/src/ledmatrix/venusLedMatrixUI.html');
var htmlpath = htmlPathOnDisk.fsPath;
var html = fs.readFileSync(htmlpath).toString();
const onDiskPath = vscode.Uri.joinPath(VenusLedMatrixUI._extensionUri, '/src/ledmatrix/venusLedMatrixUI.js');
const scriptSrc = webview.asWebviewUri(onDiskPath);
html = html.replace('${scriptSrc}', scriptSrc.toString());
const stylePath = vscode.Uri.joinPath(VenusLedMatrixUI._extensionUri, '/src/ledmatrix/venusLedMatrixUI.css');
const styleSrc = webview.asWebviewUri(stylePath);
html = html.replace('${styleSrc}', styleSrc.toString());
return html;
}
public ecall(id: number, params: any) : object {
if (id == 0x100) {
let x = (params.a1 >> 16) & 0xFFFF
let y = params.a1 & 0xFFFF
let red = (params.a2 >> 16) & 0xFF
let green = (params.a2 >> 8) & 0xFF
let blue = params.a2 & 0xFF
var color = new Color(red, green, blue)
VenusLedMatrixUI._uiState.getLedMatrix().setLed(x, y, color)
} else if (id == 0x101) {
let red = (params.a1 >> 16) & 0xFF
let green = (params.a1 >> 8) & 0xFF
let blue = params.a1 & 0xFF
var color = new Color(red, green, blue)
VenusLedMatrixUI._uiState.getLedMatrix().setAllLed(color)
}
this._update()
return {}
}
public resetLedMatrix() {
VenusLedMatrixUI._uiState.getLedMatrix().resetMatrix();
this._update();
}
}
export class UIState {
private ledMatrix: LedMatrix
constructor(ledMatrix: LedMatrix){
this.ledMatrix = ledMatrix;
};
public getLedMatrix(): LedMatrix {
return this.ledMatrix
}
}
export class LedMatrix {
private ledState: Color[]
readonly xCount: number
readonly yCount: number
setLed(x: number, y: number, color: Color) {
if ((x + y*this.xCount) < this.ledState.length){
this.ledState[x + y*this.xCount] = color
}
}
setAllLed(color: Color) {
this.ledState = Array<Color>(this.xCount * this.yCount).fill(color)
}
resetMatrix() {
this.ledState = Array<Color>(this.xCount * this.yCount).fill(new Color(0,0,0))
}
/** The length of the led state needs to be xSize * ySize.
* Otherwise the leds are initialized as all off/black.
*/
constructor(xSize: number, ySize: number, ledState?: Color[]){
this.xCount = xSize;
this.yCount = ySize;
if (ledState == null || ledState.length != (xSize * ySize)) {
this.resetMatrix();
} else {
this.ledState = ledState;
}
};
}
export class Color {
readonly red: number;
readonly green: number;
readonly blue: number;
constructor(red: number, green: number, blue: number) {
this.red = (red > 255) ? 255 : red;
this.green = (green > 255) ? 255 : green;
this.blue = (blue > 255) ? 255 : blue;
}
}