View difference between Paste ID: qeKK4LNX and ZAkFvfbg
SHOW: | | - or go back to the newest paste.
1
--[[ Variables ]]--
2
calc = ""
3
screenX, screenY = term.getSize()
4
5
if fs.exists("calcColor") == true then
6
 h = fs.open("calcColor", "r")
7
 borderColour = loadstring("return "..h.readLine())()
8
 h.close()
9
else
10
 h = fs.open("calcColor", "w")
11
 h.write("colors.white")
12
 h.close()
13
 borderColour = colors.white
14
end --if true then read file and set color to what was set before, if not then set to default of white
15
 
16
17
-- Tables
18
buttonsMain = {}
19
buttonsExtra = {}
20
buttonsColors = {}
21
input = {nil, nil, nil}
22
--[[ End of variables ]]--
23
 
24
--[[ Functions ]]--
25
 
26
function printVerticalLine(lineX, startingY, endingY)
27
        term.setBackgroundColor(borderColour)
28
        for lineY = startingY, endingY do
29
                term.setCursorPos(lineX, lineY)
30
                term.write(" ")
31
        end
32
end
33
 
34
function printHorizontalLine(yLine, startingX, endingX)
35
        term.setBackgroundColor(borderColour)
36
        term.setCursorPos(startingX, yLine)
37
        write(string.rep(" ", endingX - startingX + 1))
38
end
39
 
40
function drawMain()
41
        -- Left part (for numbers)
42
                printVerticalLine(2,2,18)
43
                --Left Border
44
                printVerticalLine(20,2,18)
45
                --Right Border
46
                printVerticalLine(8,6,18)
47
                --Left Seperator
48
                printVerticalLine(14,6,18)
49
                --Right Seperator
50
                printHorizontalLine(2,2,20)
51
                --Top
52
                printHorizontalLine(6,2, 20)
53
                --Upper Before Keys
54
                printHorizontalLine(18,2,20)
55
                --Bottom
56
                printHorizontalLine(10,2,20)
57
                --Upper Seperator
58
                printHorizontalLine(14,2,20)
59
                --Lower Seperator
60
                term.setBackgroundColor(colors.black)
61
               
62
        -- Middle area (F, H, c)
63
                printVerticalLine(23,6,18)
64
                --Left Border
65
                printVerticalLine(29,6,18)
66
                --Right Border
67
                printHorizontalLine(6,23,29)
68
                --Top
69
                printHorizontalLine(10,23,29)
70
                printHorizontalLine(14,23,29)
71
                printHorizontalLine(18,23,29)
72
               
73
        -- Right side (0, math synbols)
74
                printHorizontalLine(6,32,50)
75
                --Top
76
                printHorizontalLine(10,32,50)
77
                printHorizontalLine(14,32,50)
78
                printHorizontalLine(18,32,50)
79
                --Bottom
80
                printVerticalLine(32,6,18)
81
                --Left Border
82
                printVerticalLine(38,6,14)
83
                printVerticalLine(44,6,18)
84
                printVerticalLine(50,6,18)
85
       
86
        term.setBackgroundColor(colors.black)
87
end
88
 
89
function drawColorsTable()
90
        printVerticalLine(2,2,18)
91
        printVerticalLine(14,2,18)
92
        printVerticalLine(26,2,18)
93
        printVerticalLine(40,2,18)
94
        printVerticalLine(50,2,18)
95
       
96
        printHorizontalLine(2, 3, screenX-2)
97
        printHorizontalLine(6, 3, screenX-2)
98
        printHorizontalLine(10, 3, screenX-2)
99
        printHorizontalLine(14, 3, screenX-2)
100
        printHorizontalLine(18, 3, screenX-2)
101
       
102
        term.setBackgroundColor(colors.black)
103
end
104
 
105
function setTable(_table, name, xMin, xMax, yMin, yMax, customX, col)
106
        table.insert(_table, {
107
                numb = name,
108
                xmin = xMin,
109
                xmax = xMax,
110
                ymin = yMin,
111
                ymax = yMax,
112
                cX = customX or nil,
113
                txtCol = col or colours.white
114
        })
115
end
116
 
117
function fillTable()
118
        setTable(buttonsMain, "1", 3, 7, 15, 17)
119
        setTable(buttonsMain, "2", 9, 13, 15, 17)
120
        setTable(buttonsMain, "3", 15, 19, 15, 17)
121
        setTable(buttonsMain, "4", 3, 7, 11, 13)
122
        setTable(buttonsMain, "5", 9, 13, 11, 13)
123
        setTable(buttonsMain, "6", 15, 19, 11, 13)
124
        setTable(buttonsMain, "7", 3, 7, 7, 9)
125
        setTable(buttonsMain, "8", 9, 13, 7, 9)
126
        setTable(buttonsMain, "9", 15, 19, 7, 9)
127
        setTable(buttonsExtra, "1", 3, 7, 15, 17)
128
        setTable(buttonsExtra, "2", 9, 13, 15, 17)
129
        setTable(buttonsExtra, "3", 15, 19, 15, 17)
130
        setTable(buttonsExtra, "4", 3, 7, 11, 13)
131
        setTable(buttonsExtra, "5", 9, 13, 11, 13)
132
        setTable(buttonsExtra, "6", 15, 19, 11, 13)
133
        setTable(buttonsExtra, "7", 3, 7, 7, 9)
134
        setTable(buttonsExtra, "8", 9, 13, 7, 9)
135
        setTable(buttonsExtra, "9", 15, 19, 7, 9)
136
end
137
 
138
function fillFunctionsTable()
139
        setTable(buttonsMain, "C", 33,37,7,9)
140
        setTable(buttonsMain, ".", 39,43,7,9)
141
        setTable(buttonsMain, "/", 45,49,7,9)
142
        setTable(buttonsMain, "+", 33,37,11,13)
143
        setTable(buttonsMain, "-", 39,43,11,13)
144
        setTable(buttonsMain, "*", 45,49,11,13)
145
        setTable(buttonsMain, "0", 33,43,15,17)
146
        setTable(buttonsMain, "=", 45,49,15,17)
147
end
148
 
149
function fillExtraFunctions()
150
        setTable(buttonsExtra, "^", 33,37,7,9)
151
        setTable(buttonsExtra, "rt", 39,43,7,9)
152
        setTable(buttonsExtra, "/", 45,49,7,9)
153
        setTable(buttonsExtra, "+", 33,37,11,13)
154
        setTable(buttonsExtra, "-", 39,43,11,13)
155
        setTable(buttonsExtra, "*", 45,49,11,13)
156
        setTable(buttonsExtra, "0", 33,43,15,17)
157
        setTable(buttonsExtra, "=", 45,49,15,17)
158
end
159
 
160
function fillExtraButtonsTable()
161
        setTable(buttonsExtra, "F",24,28,7,9)
162
        setTable(buttonsExtra, "H",24,28,11,13)
163
        setTable(buttonsExtra, "c",24,28,15,17)
164
        setTable(buttonsMain, "F",24,28,7,9)
165
        setTable(buttonsMain, "H",24,28,11,13)
166
        setTable(buttonsMain, "c",24,28,15,17)
167
end
168
 
169
function fillColorOptions()
170
        setTable(buttonsColors,"White",3,13,3,5, 6, colours.white)
171
        setTable(buttonsColors,"Orange",3,13,7,9, 6, colours.orange)
172
        setTable(buttonsColors,"Magenta",3,13,11,13, 5, colours.magenta)
173
        setTable(buttonsColors,"Light Blue",3,13,15,17, 4, colours.lightBlue)
174
        setTable(buttonsColors,"Yellow",15,25,3,5, 17, colours.yellow)
175
        setTable(buttonsColors,"Lime",15,25,7,9, 18, colours.lime)
176
        setTable(buttonsColors,"Pink",15,25,11,13, 18, colours.pink)
177
        setTable(buttonsColors,"Gray",15,25,15,17, 18, colours.grey)
178
        setTable(buttonsColors,"Light Gray",27,37,3,5, 28, colours.lightGrey)
179
        setTable(buttonsColors,"Cyan",27,37,7,9, 32, colours.cyan)
180
        setTable(buttonsColors,"Purple",27,37,11,13, 30, colours.purple)
181
        setTable(buttonsColors,"Blue",27,37,15,17, 31, colours.blue)
182
        setTable(buttonsColors,"Brown",39,49,3,5, 43, colours.brown)
183
        setTable(buttonsColors,"Green",39,49,7,9, 43, colours.green)
184
        setTable(buttonsColors,"Red",39,49,11,13, 44, colours.red)
185
        setTable(buttonsColors,"Black",39,49,15,17, 43, colours.white)
186
end
187
 
188
function checkClick(tab, mx, my)
189
        for i, v in pairs(tab) do
190
                if mx >= v.xmin and mx <= v.xmax and my >= v.ymin and my <= v.ymax then
191
                        return true, v.numb
192
                end
193
        end
194
        return false, nil
195
end
196
 
197
function screen(tab)
198
        term.setBackgroundColour(colours.black)
199
        for i, data in pairs(tab) do
200
                local yspot = (data.ymin + data.ymax) / 2
201
                local xspot = data.cX or (data.xmin + data.xmax) / 2
202
                term.setCursorPos(xspot, yspot)
203
                term.setTextColour(data.txtCol)
204
                write(data.numb)
205
        end
206
end
207
 
208
function writeColor()
209
 h = fs.open("calcColor", "w")
210
 h.write(borderColour)
211
 h.close()
212
end
213
214
function redraw(t)
215
        term.setCursorPos(4, 4)
216
        write(string.rep(" ", 15))
217
        term.setCursorPos(4, 4)
218
        write(t or "")
219
end
220
 
221
function doSum()
222
        input[1] = tonumber(calc) or input[1]
223
        input[2] = numb
224
        redraw()
225
        calc = ""
226
        numb = ""
227
end
228
 
229
function enterPress()
230
        input[3] = tonumber(calc)
231
        if input[2] == "+" then
232
                Output = input[1] + input[3]
233
        elseif input[2] == "-" then
234
                Output = input[1] - input[3]
235
        elseif input[2] == "*" then
236
                Output = input[1] * input[3]
237
        elseif input[2] == "/" then
238
                Output = input[1] / input[3]
239
        elseif input[2] == "^" then
240
                Output = input[1] ^ input[3]
241
        elseif input[2] == "rt" then
242
                Output = math.sqrt(input[1])
243
        end
244
        redraw(Output)
245
        calc = tostring(Output)
246
        input[1] = Output  
247
end
248
 
249
--[[ Functions for the middle section: Choosing a new border colour
250
          and to display help.
251
--]]
252
function chooseColour()
253
        term.setBackgroundColour(colours.black)
254
        term.setCursorPos(1, 1)
255
        term.clear()
256
       
257
        term.setBackgroundColour(colours.grey)
258
        write(string.rep(" ", screenX))
259
       
260
        term.setBackgroundColour(colours.red)
261
        term.setCursorPos(screenX, 1)
262
        write("X")
263
       
264
        while true do
265
                screen(buttonsColors)
266
                drawColorsTable()
267
                local e, but, x, y = os.pullEvent("mouse_click")
268
                if but == 1 then
269
                        if x == screenX and y == 1 then
270
                                term.clear()
271
                                screen(buttonsMain)
272
                                drawMain()
273
                                redraw(calc)
274
                                break
275
                        else
276
                                bValid, colChoice = checkClick(buttonsColors, x, y)
277
                                if bValid then
278
                                        if colChoice == "White" then
279
                                                borderColour = colours.white
280
                                                writeColor()
281
                                        elseif colChoice == "Orange" then
282
                                                borderColour = colours.orange
283
                                                writeColor()
284
                                        elseif colChoice == "Magenta" then
285
                                                borderColour = colours.magenta
286
                                                writeColor()
287
                                        elseif colChoice == "Light Blue" then
288
                                                borderColour = colours.lightBlue
289
                                                writeColor()
290
                                        elseif colChoice == "Yellow" then
291
                                                borderColour = colours.yellow
292
                                                writeColor()
293
                                        elseif colChoice == "Lime" then
294
                                                borderColour = colours.lime
295
                                                writeColor()
296
                                        elseif colChoice == "Pink" then
297
                                                borderColour = colours.pink
298
                                                writeColor()
299
                                        elseif colChoice == "Gray" then
300
                                                borderColour = colours.grey
301
                                                writeColor()
302
                                        elseif colChoice == "Light Gray" then
303
                                                borderColour = colours.lightGrey
304
                                                writeColor()
305
                                        elseif colChoice == "Cyan" then
306
                                                borderColour = colours.cyan
307
                                                writeColor()
308
                                        elseif colChoice == "Purple" then
309
                                                borderColour = colours.purple
310
                                                writeColor()
311
                                        elseif colChoice == "Blue" then
312
                                                borderColour = colours.blue
313
                                                writeColor()
314
                                        elseif colChoice == "Brown" then
315
                                                borderColour = colours.brown
316
                                                writeColor()
317
                                        elseif colChoice == "Green" then
318
                                                borderColour = colours.green
319
                                                writeColor()
320
                                        elseif colChoice == "Red" then
321
                                                borderColour = colours.red
322
                                                writeColor()
323
                                        elseif colChoice == "Black" then
324
                                                borderColour = colours.black
325
                                                writeColor()
326
                                        end
327
                                end
328
                        end
329
                end
330
        end
331
end
332
 
333
function displayHelp()
334
        term.setBackgroundColour(colours.white)
335
        term.setCursorPos(1, 1)
336
        term.clear()
337
       
338
        term.setBackgroundColour(colours.grey)
339
        write(string.rep(" ", screenX))
340
       
341
        term.setBackgroundColour(colours.red)
342
        term.setCursorPos(screenX, 1)
343
        write("X")
344
       
345
        term.setBackgroundColour(colours.white)
346
        term.setTextColour(colours.black)
347
        term.setCursorPos(1, 2)
348
        print("There will be help here once I decide to not be lazy and fill it out, for now check the forums!")
349
       
350
        repeat local _, but, x, y = os.pullEvent("mouse_click") until but == 1 and x == screenX and y == 1
351
       
352
        term.setBackgroundColour(colours.black)
353
        term.clear()
354
       
355
        drawMain()
356
        screen(buttonsMain)
357
end
358
--[[ End of functions ]]--
359
 
360
--[[ Main ]]--
361
 
362
-- Run the functions to fill the table with math functions
363
fillExtraFunctions()
364
fillTable()
365
fillFunctionsTable()
366
fillExtraButtonsTable()
367
fillColorOptions()
368
 
369
-- Run the functions to draw the tables
370
term.clear()
371
drawMain()
372
 
373
screen(buttonsMain)
374
state = "main"
375
 
376
while true do
377-
        local e, but, x, y = os.pullEvent("mouse_click")
377+
        local e, but, x, y = sapphUtils.pullEvent("mouse_click")
378
        term.setBackgroundColor(colors.black)
379
        term.setTextColor(colors.white)
380
        if but == 1 then
381
                bValid, numb = checkClick(state == "main" and buttonsMain or state == "extra" and buttonsExtra, x, y)
382
                if bValid then
383
                        if numb == "+" or numb == "-" or numb == "*" or numb == "/" or
384
                        numb == "^" or numb == "rt" then
385
                                doSum()
386
                        elseif numb == "H" then
387
                                displayHelp()
388
                        elseif numb == "F" then
389
                                term.clear()
390
                                redraw(calc)
391
                                drawMain()
392
                                if state == "main" then
393
                                        state = "extra"
394
                                        screen(buttonsExtra)
395
                                elseif state == "extra" then
396
                                        state = "main"
397
                                        screen(buttonsMain)
398
                                end
399
                        elseif numb == "=" then
400
                        enterPress()
401
                        elseif numb == "c" then
402
                                chooseColour()
403
                        elseif numb == "C" then
404
                                for i = 1,3 do
405
                                        input[i] = nil
406
                                end
407
                                calc = ""
408
                                Output = ""
409
                                redraw()
410
                        elseif numb == "." then
411
                            calc = calc .. numb
412
                            term.setCursorPos(4,4)
413
                            if #calc < 14 then
414
                                write(calc)
415
                            else
416
                    write(calc:sub(#calc-14))
417
                end
418
                        elseif tonumber(numb) then -- if it is a number
419
                                calc = calc .. numb
420
                                term.setCursorPos(4, 4)
421
                                if #calc < 14 then
422
                                        write(calc)
423
                                else
424
                                        write(calc:sub(#calc-14))
425
                                end
426
                        end
427
                end
428
        end
429
end