-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.mjs
205 lines (160 loc) · 8.28 KB
/
editor.mjs
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
import {EditorView, basicSetup} from "codemirror";
import { wast, wastLanguage } from "@codemirror/lang-wast";
import { Compartment, EditorSelection, EditorState, StateEffect } from "@codemirror/state";
import { oneDark } from "@codemirror/theme-one-dark";
const DEFAULT_CARTRIDGE = `;;
;; WASM-4: https://wasm4.org/docs
(import "env" "memory" (memory 1))
;; ┌───────────────────────────────────────────────────────────────────────────┐
;; │ │
;; │ Drawing Functions │
;; │ │
;; └───────────────────────────────────────────────────────────────────────────┘
(; Copies pixels to the framebuffer. ;)
(import "env" "blit" (func $blit (param i32 i32 i32 i32 i32 i32)))
(; Copies a subregion within a larger sprite atlas to the framebuffer. ;)
(import "env" "blitSub" (func $blitSub (param i32 i32 i32 i32 i32 i32 i32 i32 i32)))
(; Draws a line between two points. ;)
(import "env" "line" (func $line (param i32 i32 i32 i32)))
(; Draws a horizontal line. ;)
(import "env" "hline" (func $hline (param i32 i32 i32)))
(; Draws a vertical line. ;)
(import "env" "vline" (func $vline (param i32 i32 i32)))
(; Draws an oval (or circle). ;)
(import "env" "oval" (func $oval (param i32 i32 i32 i32)))
(; Draws a rectangle. ;)
(import "env" "rect" (func $rect (param i32 i32 i32 i32)))
(; Draws text using the built-in system font. ;)
(import "env" "text" (func $text (param i32 i32 i32)))
;; ┌───────────────────────────────────────────────────────────────────────────┐
;; │ │
;; │ Sound Functions │
;; │ │
;; └───────────────────────────────────────────────────────────────────────────┘
(; Plays a sound tone. ;)
(import "env" "tone" (func $tone (param i32 i32 i32 i32)))
;; ┌───────────────────────────────────────────────────────────────────────────┐
;; │ │
;; │ Storage Functions │
;; │ │
;; └───────────────────────────────────────────────────────────────────────────┘
(; Reads up to "size" bytes from persistent storage into the pointer "dest". ;)
(import "env" "diskr" (func $diskr (param i32 i32)))
(; Writes up to "size" bytes from the pointer "src" into persistent storage. ;)
(import "env" "diskw" (func $diskw (param i32 i32)))
(; Prints a message to the debug console. ;)
(import "env" "trace" (func $trace (param i32)))
(; Prints a message to the debug console. ;)
(import "env" "tracef" (func $tracef (param i32 i32)))
;; ┌───────────────────────────────────────────────────────────────────────────┐
;; │ │
;; │ Memory Addresses │
;; │ │
;; └───────────────────────────────────────────────────────────────────────────┘
(global $PALETTE0 i32 (i32.const 0x04))
(global $PALETTE1 i32 (i32.const 0x08))
(global $PALETTE2 i32 (i32.const 0x0c))
(global $PALETTE3 i32 (i32.const 0x10))
(global $DRAW_COLORS i32 (i32.const 0x14))
(global $GAMEPAD1 i32 (i32.const 0x16))
(global $GAMEPAD2 i32 (i32.const 0x17))
(global $GAMEPAD3 i32 (i32.const 0x18))
(global $GAMEPAD4 i32 (i32.const 0x19))
(global $MOUSE_X i32 (i32.const 0x1a))
(global $MOUSE_Y i32 (i32.const 0x1c))
(global $MOUSE_BUTTONS i32 (i32.const 0x1e))
(global $SYSTEM_FLAGS i32 (i32.const 0x1f))
(global $NETPLAY i32 (i32.const 0x20))
(global $FRAMEBUFFER i32 (i32.const 0xa0))
(global $BUTTON_1 i32 (i32.const 1))
(global $BUTTON_2 i32 (i32.const 2))
(global $BUTTON_LEFT i32 (i32.const 16))
(global $BUTTON_RIGHT i32 (i32.const 32))
(global $BUTTON_UP i32 (i32.const 64))
(global $BUTTON_DOWN i32 (i32.const 128))
(global $MOUSE_LEFT i32 (i32.const 1))
(global $MOUSE_RIGHT i32 (i32.const 2))
(global $MOUSE_MIDDLE i32 (i32.const 4))
(global $SYSTEM_PRESERVE_FRAMEBUFFER i32 (i32.const 1))
(global $SYSTEM_HIDE_GAMEPAD_OVERLAY i32 (i32.const 2))
(global $BLIT_2BPP i32 (i32.const 1))
(global $BLIT_1BPP i32 (i32.const 0))
(global $BLIT_FLIP_X i32 (i32.const 2))
(global $BLIT_FLIP_Y i32 (i32.const 4))
(global $BLIT_ROTATE i32 (i32.const 8))
(global $TONE_PULSE1 i32 (i32.const 0))
(global $TONE_PULSE2 i32 (i32.const 1))
(global $TONE_TRIANGLE i32 (i32.const 2))
(global $TONE_NOISE i32 (i32.const 3))
(global $TONE_MODE1 i32 (i32.const 0))
(global $TONE_MODE2 i32 (i32.const 4))
(global $TONE_MODE3 i32 (i32.const 8))
(global $TONE_MODE4 i32 (i32.const 12))
(global $TONE_PAN_LEFT i32 (i32.const 16))
(global $TONE_PAN_RIGHT i32 (i32.const 32))
;; smiley
(data (i32.const 0x19a0) "\\c3\\81\\24\\24\\00\\24\\99\\c3")
(data (i32.const 0x19a8) "Hello from Wat!\\00")
(data (i32.const 0x19b8) "Press X to blink\\00")
(func (export "start")
)
(func (export "update")
(local $gamepad i32)
;; *DRAW_COLORS = 2
(i32.store16 (global.get $DRAW_COLORS) (i32.const 2))
;; text("Hello from Wat!", 10, 10);
(call $text (i32.const 0x19a8) (i32.const 10) (i32.const 10))
;; uint8_t gamepad = *GAMEPAD1;
(local.set $gamepad (i32.load8_u (global.get $GAMEPAD1)))
;; if (gamepad & BUTTON_1) {
;; *DRAW_COLORS = 4;
;; }
(if (i32.and (local.get $gamepad) (global.get $BUTTON_1))
(then
(i32.store16 (global.get $DRAW_COLORS) (i32.const 4))
))
;; blit(smiley, 76, 76, 8, 8, BLIT_1BPP);
(call $blit (i32.const 0x19a0) (i32.const 76) (i32.const 76) (i32.const 8) (i32.const 8) (global.get $BLIT_1BPP))
;; text("Press X to blink", 16, 90);
(call $text (i32.const 0x19b8) (i32.const 16) (i32.const 90))
)`;
let readOnly = new Compartment;
function setEditable(editor, value) {
editor.dispatch({
effects: readOnly.reconfigure(EditorState.readOnly.of(!value))
});
}
let darkMode = new Compartment;
function setDarkMode(editor, val) {
editor.dispatch({
effects: darkMode.reconfigure(val? oneDark : [])
})
if (val) {
document.body.classList.add("dark-mode");
} else {
document.body.classList.remove("dark-mode");
}
localStorage.setItem("dark-mode", val);
}
let editor = new EditorView({
lineWrapping: true,
extensions: [basicSetup, wast(), readOnly.of(EditorState.readOnly.of(false)), EditorView.lineWrapping, darkMode.of([])],
parent: document.getElementById("text")
});
document.getElementById("dark-mode").oninput = (e) => {
setDarkMode(editor, e.target.checked);
};
let darkModeStorage;
if ((darkModeStorage = localStorage.getItem("dark-mode")) !== null) {
let value = darkModeStorage === "true";
setDarkMode(editor, value);
if (value){
document.getElementById("dark-mode").checked = true;
}
} else {
localStorage.setItem("dark-mode", false);
}
editor.dispatch({
changes: {from: 0, to: editor.state.doc.length, insert: DEFAULT_CARTRIDGE}
});
export {editor, setEditable};