-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.js
270 lines (189 loc) · 8.09 KB
/
UI.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// global variables
let codeInput, assembledCode, inputBox, outputtedCode // textboxes
let titleBox, saveCodeButton, loadCodeButton //saving and loading buttons and the title textbox
let assembleButton, runButton, inputSubmit // buttons
let registers, memory, cir, pc, mdr, mar, acc, cu, alu // memory, cu and alu are not registers in real life but in this simulation they act similar.
let speedSlider
function setupUI() {
setupRegisters()
setupMenuBar()
setupLMC()
sizeUI()
}
// all the UI setting up
function setupMenuBar() { // all the setup for the top bar
titleBox = new TextBox(createElement("textarea"), "titleBox") // for inputting the title of the program
titleBox.element.placeholder = "Title"
titleBox.textbox.class("LMCBox") // each blue box
saveCodeButton = new Button(createButton("Save Code"), "saveCodeButton", saveCode) // saving code
saveCodeButton.button.class("optionButton") // different classes for making the css the same for the option buttons
loadCodeButton = new Button(createButton("Load Code"), "loadCodeButton", loadCode) // loading code
loadCodeButton.button.class("optionButton")
assembleButton = new Button(createButton("Assemble"), "assembleButton", assembleCode) // assembles the instructions
assembleButton.button.class("menuButton")
runButton = new Button(createButton("Run"), "runButton", runCode) // for actually running the programs
runButton.button.class("menuButton")
speedSlider = createSlider(10, 300, 35)
speedSlider.id("speedSlider")
}
function setupLMC() { // all the boxes for typing and inputting
codeInput = new TextBox(createElement("textarea"), "codeInput") // where the user types their code
codeInput.element.placeholder = "Enter code here...";
codeInput.textbox.class("LMCBox")
assembledCode = new TextBox(createElement("textarea"), "assembledCode")
assembledCode.element.readOnly = true;
assembledCode.element.placeholder = "Assembled"
assembledCode.textbox.class("LMCBox")
inputBox = new TextBox(createElement("textarea"), "inputBox")
inputBox.value = 0
inputBox.element.placeholder = "Input"
inputBox.textbox.class("LMCBox")
inputBox.element.addEventListener("keypress", function(event) {
if (event.keyCode == 13) {
event.preventDefault()
submitInput()
}
})
outputtedCode = new TextBox(createElement("textarea"), "outputtedCode")
outputtedCode.element.readOnly = true;
outputtedCode.element.placeholder = "Output"
outputtedCode.textbox.class("LMCBox")
inputSubmit = new Button(createButton("Submit"), "inputSubmit", submitInput)
}
function setupRegisters() { // all the components the player cannot edit directly
memory = new MemoryRegister("RAM")
cir = new Register("CIR")
pc = new Register("PC")
mdr = new Register("MDR")
mar = new Register("MAR")
acc = new Register("ACC")
cu = new CU("CU", "...")
cu.instructionFound = null
alu = new ALU("ALU", "000")
registers = [memory, cir, pc, mdr, mar, acc, cu, alu]
}
function sizeUI() { // sizing all the UI elements upon setup and when the screen gets resized.
// sets all the sizes of the HTML elements
textFont("Monospace")
textSize(windowWidth * 0.0125)
sizeMenuBar()
sizeLMC()
sizeRegisters()
}
function sizeMenuBar() {
titleBox.textbox.position(0, windowHeight / 75)
saveCodeButton.button.position(titleBox.element.offsetWidth + windowWidth / 35, windowHeight / 75)
loadCodeButton.button.position(titleBox.element.offsetWidth + windowWidth / 35 + saveCodeButton.element.offsetWidth + windowWidth / 75, windowHeight / 75)
assembleButton.button.position(windowWidth / 2 - windowWidth * 0.080, windowHeight / 75)
runButton.button.position(windowWidth / 2 + windowWidth * 0.005, windowHeight / 75)
speedSlider.position(windowWidth / 2 - windowWidth * 0.005, windowHeight / 75 + runButton.element.offsetHeight)
}
function sizeLMC() {
codeInput.textbox.position(0, windowHeight / 12.5)
assembledCode.textbox.position(codeInput.element.offsetWidth + windowWidth / 75, windowHeight / 12.5)
outputtedCode.textbox.position(windowWidth - outputtedCode.element.offsetWidth - windowWidth * 0.02, windowHeight * 0.66)
inputBox.textbox.position(windowWidth - outputtedCode.element.offsetWidth - inputBox.element.offsetWidth - inputSubmit.element.offsetWidth - windowWidth * 0.06, windowHeight * 0.93)
inputSubmit.button.position(windowWidth - outputtedCode.element.offsetWidth - inputSubmit.element.offsetWidth - windowWidth * 0.05, windowHeight * 0.93)
inputSubmit.button.size(AUTO, inputBox.element.offsetHeight)
}
function sizeRegisters() {
memory.resize(windowWidth - windowWidth * 0.38 - windowWidth * 0.01, windowHeight / 12.5 + windowWidth * 0.01, windowWidth * 0.38, windowHeight * 0.55)
let x = codeInput.element.offsetWidth + assembledCode.element.offsetWidth + windowWidth * 0.06
let w = windowWidth * 0.05
let h = windowHeight * 0.07
pc.resize(x, windowHeight * 0.16, w, h)
mar.resize(x, windowHeight * 0.24, w, h)
mdr.resize(x, windowHeight * 0.32, w, h)
cir.resize(x, windowHeight * 0.40, w, h)
acc.resize(x, windowHeight * 0.48, w, h)
cu.resize(x, windowHeight * 0.7, w, h * 1.5)
alu.resize(x + windowWidth * 0.07, windowHeight * 0.7, w * 2, h * 2)
animCon.resizePoints()
}
function displayAssembledCode(valueList) { // turns memory.value into a string to display
let valueString = ""
let rows = codeInput.element.value.toUpperCase().split("\n") // split codeInput into rows
for (let i = 0; i < min(rows.length, 100); i++) { // loop for number of rows the user has
let value = addZeros(valueList[i])
valueString += value + "\n" // add to big string with new line
}
assembledCode.element.value = valueString // put on webpage
memory.update()
}
function saveCode() { // saving the user's program (text from codeInput)
let code = codeInput.element.value
let blob = new Blob([code], { type: "text/plain;charset=utf-8" })
let fileName = titleBox.element.value
if (fileName === "") {
fileName = "code"
}
saveAs(blob, fileName + ".txt")
}
function loadCode() { // when the load code button is pressed, the file open box pops up for the user to input their text file
let input = document.createElement("input") // creating a file input
input.type = "file"
input.accept = ".txt"
input.addEventListener("change", readFile) // read it when it is inputted
input.click() // activate the element
}
function readFile(event) {
file = event.target.files[0] // the file that the input got (event.target = input)
let reader = new FileReader() // use a fileReader to read the contents of the file
reader.readAsText(file) // reading the file
reader.addEventListener("load", function() { // when it is read
titleBox.element.value = file.name.slice(0, file.name.length - 4)
codeInput.element.value = reader.result
})
}
function outputValue() {
let num
if (cu.value === "OUT") {
num = true
} else if (cu.value === "OTC") {
num = false
}
let toAdd = "\n" // \n = new line
if (outputtedCode.element.value == "" || !num) { // if first line or a new line is not wanted
toAdd = ""
}
if (num) {
toAdd += acc.value
} else {
toAdd += String.fromCharCode(acc.value)
}
outputtedCode.element.value += toAdd // add to element
outputtedCode.element.scrollTop = outputtedCode.element.scrollHeight
}
function clearOutput() { // clears the contents of the output box
outputtedCode.element.value = ""
}
function submitInput() { // when the submit button is pressed
inputValue(inputBox.element.value) // LMC.js
}
function clearInputBox() { // clears the contents of the input box
inputBox.element.value = ""
}
function drawUI() { // for displaying UI elements from p5 functions at 60 fps
noStroke()
fill(10, 25, 48)
rect(0, 0, windowWidth, windowHeight / 12.5)
strokeWeight(1)
fill(255)
textAlign(LEFT, TOP)
if (!debug) {
animCon.drawPoints()
animCon.updateAnimation()
}
for (let r of registers) {
r.update()
}
if (debug) {
animCon.drawPoints()
animCon.updateAnimation()
}
}
function windowResized() { // runs when the window size gets changed
// resize UI
resizeCanvas(windowWidth, windowHeight)
sizeUI()
}