forked from ThomasTheSpaceFox/SBTCVM-Mark-2
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcalc.py
executable file
·408 lines (381 loc) · 11.8 KB
/
calc.py
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env python
import pygame.event
import pygame.key
import pygame.display
import pygame.image
import pygame.mixer
import pygame
import time
import copy
import sys
import os
import subprocess
import VMSYSTEM.libvmui as vmui
import VMSYSTEM.libbaltcalc as libbaltcalc
from pygame.locals import *
import VMSYSTEM.libthemeconf as libthemeconf
pygame.display.init()
pygame.font.init()
bigfont = pygame.font.SysFont(None, 30)
simplefont = pygame.font.SysFont(None, 22)
btnfont = pygame.font.SysFont(None, 21)
pygame.event.set_allowed([QUIT, MOUSEBUTTONDOWN, KEYDOWN])
pygame.display.set_caption(("Ternary Calculator"), ("Ternary Calculator"))
windowicon=pygame.image.load(os.path.join("VMSYSTEM", "GFX", 'calc.png'))
pygame.display.set_icon(windowicon)
screensurf=pygame.display.set_mode((450, 450))
vmui.initui(screensurf, 1)
def makebtn(text, text2=None):
btn=pygame.Surface((60, 60))
btnlocrec=pygame.Rect(0, 0, 60, 60)
btn.fill(libthemeconf.calcpadbg)
pygame.draw.rect(btn, libthemeconf.calcpadline, btnlocrec, 1)
texgfx=btnfont.render(text, True, libthemeconf.calcpadtext, libthemeconf.calcpadbg)
btn.blit(texgfx, ((30 - (texgfx.get_width() // 2)), 2))
if text2!=None:
texgfx=btnfont.render(text2, True, libthemeconf.calcpadtext, libthemeconf.calcpadbg)
btn.blit(texgfx, ((30 - (texgfx.get_width() // 2)), 30))
return btn
#bg=pygame.image.load(os.path.join("VMSYSTEM", "GFX", "calc", 'bg.jpg')).convert()
bg=pygame.Surface((450, 500))
#lockon=pygame.image.load(os.path.join("VMSYSTEM", "GFX", "calc", 'lockon.png')).convert_alpha()
#lockoff=pygame.image.load(os.path.join("VMSYSTEM", "GFX", "calc", 'lockoff.png')).convert_alpha()
lockon=pygame.Surface((60, 60), SRCALPHA)
lockoff=pygame.Surface((60, 60), SRCALPHA)
lockrect=pygame.Rect(0, 48, 60, 12)
pygame.draw.rect(lockon, libthemeconf.calclockon, lockrect, 0)
pygame.draw.rect(lockon, libthemeconf.calcpadline, lockrect, 1)
pygame.draw.rect(lockoff, libthemeconf.calclockoff, lockrect, 0)
pygame.draw.rect(lockoff, libthemeconf.calcpadline, lockrect, 1)
#row 1 gfx
add=makebtn("ADD")
sub=makebtn("SUB")
div=makebtn("DIV")
mul=makebtn("MUL")
copyab=makebtn("COPY", "A>B")
inverta=makebtn("INVERT", "A")
#row 2 gfx
copyba=makebtn("COPY", "B>A")
invertb=makebtn("INVERT", "B")
mpi=makebtn("mpi")
mcv=makebtn("mcv")
resa=makebtn("res>A")
resb=makebtn("res>B")
#row 3 gfx
swap=makebtn("swap")
mni=makebtn("mni")
quitg=makebtn("QUIT")
helpg=makebtn("HELP")
scupdate=1
qflg=0
bgrect1=pygame.Rect(0, 0, 450, 256)
bg.fill(libthemeconf.hudbg)
pygame.draw.rect(bg, libthemeconf.deskcolor, bgrect1, 0)
#clickboxes for user text inputs
tritabx=pygame.Rect(20, 95, 404, 24)
decabx=pygame.Rect(20, 119, 404, 24)
tritbbx=pygame.Rect(20, 159, 404, 24)
decbbx=pygame.Rect(20, 183, 404, 24)
resbx=pygame.Rect(20, 19, 404, 64)
#draw input boxes and result box...
pygame.draw.rect(bg, libthemeconf.textboxbg, resbx, 0)
pygame.draw.rect(bg, libthemeconf.textboxline, resbx, 1)
pygame.draw.rect(bg, libthemeconf.textboxbg, tritabx, 0)
pygame.draw.rect(bg, libthemeconf.textboxline, tritabx, 1)
pygame.draw.rect(bg, libthemeconf.textboxbg, decabx, 0)
pygame.draw.rect(bg, libthemeconf.textboxline, decabx, 1)
pygame.draw.rect(bg, libthemeconf.textboxbg, tritbbx, 0)
pygame.draw.rect(bg, libthemeconf.textboxline, tritbbx, 1)
pygame.draw.rect(bg, libthemeconf.textboxbg, decbbx, 0)
pygame.draw.rect(bg, libthemeconf.textboxline, decbbx, 1)
#draw buttons onto background.
padx=22
pady=260
#row 1
addx=bg.blit(add, (padx, pady))
padx += 60
subx=bg.blit(sub, (padx, pady))
padx += 60
mulx=bg.blit(mul, (padx, pady))
padx += 60
divx=bg.blit(div, (padx, pady))
padx += 60
copyabx=bg.blit(copyab, (padx, pady))
padx += 60
invertax=bg.blit(inverta, (padx, pady))
padx = 22
pady += 60
#row 2
mpix=bg.blit(mpi, (padx, pady))
padx += 60
mcvx=bg.blit(mcv, (padx, pady))
padx += 60
resa=bg.blit(resa, (padx, pady))
resax=padx
resay=pady
padx += 60
resb=bg.blit(resb, (padx, pady))
resbx=padx
resby=pady
padx += 60
copybax=bg.blit(copyba, (padx, pady))
padx += 60
invertbx=bg.blit(invertb, (padx, pady))
padx = 22
pady += 60
#row 3
mnix=bg.blit(mni, (padx, pady))
padx += 60
swapx=bg.blit(swap, (padx, pady))
padx += 60
helpx=bg.blit(helpg, (padx, pady))
padx += 60
quitx=bg.blit(quitg, (padx, pady))
padx += 60
terncalc=simplefont.render("SBTCVM Ternary Calculator", True, libthemeconf.desktext)
bg.blit(terncalc, (2, 230))
slab=simplefont.render("s.", True, libthemeconf.desktext)
bg.blit(slab, (2, 20))
tlab=simplefont.render("T", True, libthemeconf.desktext)
bg.blit(tlab, (2, 40))
bg.blit(tlab, (2, 97))
bg.blit(tlab, (2, 161))
dlab=simplefont.render("D", True, libthemeconf.desktext)
bg.blit(dlab, (2, 60))
bg.blit(dlab, (2, 121))
bg.blit(dlab, (2, 186))
Alab=bigfont.render("A", True, libthemeconf.desktext)
Blab=bigfont.render("B", True, libthemeconf.desktext)
bg.blit(Alab, (430, 111))
bg.blit(Blab, (430, 176))
#inital register values
TA="0"
DA=0
TB="0"
DB=0
TR="0"
DR=0
#inital status
STAT="SBTCVM T.C. V2.0.3 Ready."
#limits the mpi, mcv, and mni calculations. too high numbers can take WAYYY too long
mcalclimit=36
#result copy locks
resalock=0
resblock=0
docopy=0
#inputtextcol=(0, 0, 0)
#bgcol=(255, 255, 255)
engtimer=pygame.time.Clock()
while qflg==0:
if scupdate==1:
scupdate=0
screensurf.blit(bg, (0, 0))
#readout text
texgfx=simplefont.render(TA, True, libthemeconf.textboxtext, libthemeconf.textboxbg)
screensurf.blit(texgfx, (22, 97))
texgfx=simplefont.render(str(DA), True, libthemeconf.textboxtext, libthemeconf.textboxbg)
screensurf.blit(texgfx, (22, 121))
texgfx=simplefont.render(TB, True, libthemeconf.textboxtext, libthemeconf.textboxbg)
screensurf.blit(texgfx, (22, 161))
texgfx=simplefont.render(str(DB), True, libthemeconf.textboxtext, libthemeconf.textboxbg)
screensurf.blit(texgfx, (22, 186))
texgfx=simplefont.render(STAT, True, libthemeconf.textboxtext, libthemeconf.textboxbg)
screensurf.blit(texgfx, (22, 20))
texgfx=simplefont.render(TR, True, libthemeconf.textboxtext, libthemeconf.textboxbg)
screensurf.blit(texgfx, (22, 40))
texgfx=simplefont.render(str(DR), True, libthemeconf.textboxtext, libthemeconf.textboxbg)
screensurf.blit(texgfx, (22, 60))
#result copy lock overlays:
if resalock==1:
screensurf.blit(lockon, (resax, resay))
else:
screensurf.blit(lockoff, (resax, resay))
if resblock==1:
screensurf.blit(lockon, (resbx, resby))
else:
screensurf.blit(lockoff, (resbx, resby))
pygame.display.update()
#time.sleep(0.1)
engtimer.tick(30)
#event handler
for event in pygame.event.get():
keymods=pygame.key.get_mods()
if event.type == QUIT:
qflg=1
break
if event.type == KEYDOWN and event.key == K_F1:
subprocess.Popen(["python", "helpview.py", "calc.xml"])
if event.type == KEYDOWN and event.key == K_F8:
pygame.image.save(screensurf, (os.path.join('CAP', 'SCREENSHOT-calc.png')))
break
if event.type==MOUSEBUTTONDOWN:
#button Operations code
#first ROW
if addx.collidepoint(event.pos)==1 and event.button==1:
DR=DA + DB
TR=libbaltcalc.DECTOBT(DR)
STAT="SUM A & B"
scupdate=1
docopy=1
if subx.collidepoint(event.pos)==1 and event.button==1:
DR=DA - DB
TR=libbaltcalc.DECTOBT(DR)
STAT="SUBTRACT A & B"
scupdate=1
docopy=1
if divx.collidepoint(event.pos)==1 and event.button==1:
try:
DR=DA // DB
STAT="DIVIDE A & B"
except ZeroDivisionError:
STAT="DIVIDE BY ZERO ERROR"
TR=libbaltcalc.DECTOBT(DR)
docopy=1
scupdate=1
if mulx.collidepoint(event.pos)==1 and event.button==1:
DR=DA * DB
TR=libbaltcalc.DECTOBT(DR)
STAT="MULTIPLY A & B"
scupdate=1
docopy=1
if copyabx.collidepoint(event.pos)==1 and event.button==1:
DB=DA
TB=libbaltcalc.DECTOBT(DB)
STAT="COPY A TO B"
scupdate=1
if invertax.collidepoint(event.pos)==1 and event.button==1:
DA=( - DA)
TA=libbaltcalc.DECTOBT(DA)
STAT="INVERT A"
scupdate=1
#row 2
if mpix.collidepoint(event.pos)==1 and event.button==1:
if abs(DA)<=mcalclimit:
DR=libbaltcalc.mpi(abs(DA))
TR=libbaltcalc.DECTOBT(DR)
STAT="mpi of A (DEC)"
scupdate=1
docopy=1
else:
STAT="mpi of over 36 is too large"
scupdate=1
if mcvx.collidepoint(event.pos)==1 and event.button==1:
if abs(DA)<=mcalclimit:
DR=libbaltcalc.mcv(abs(DA))
TR=libbaltcalc.DECTOBT(DR)
STAT="mcv of A (DEC)"
scupdate=1
docopy=1
else:
STAT="mcv of over 36 is too large"
scupdate=1
if resa.collidepoint(event.pos)==1 and event.button==1 and keymods & KMOD_SHIFT:
if resalock==1:
resalock=0
else:
resalock=1
scupdate=1
elif resa.collidepoint(event.pos)==1 and event.button==1:
DA=DR
TA=libbaltcalc.DECTOBT(DA)
STAT="copy RESULT to A"
scupdate=1
if resb.collidepoint(event.pos)==1 and event.button==1 and keymods & KMOD_SHIFT:
if resblock==1:
resblock=0
else:
resblock=1
scupdate=1
elif resb.collidepoint(event.pos)==1 and event.button==1:
DB=DR
TB=libbaltcalc.DECTOBT(DB)
STAT="copy RESULT to B"
scupdate=1
if copybax.collidepoint(event.pos)==1 and event.button==1:
DA=DB
TA=libbaltcalc.DECTOBT(DA)
STAT="COPY B TO A"
scupdate=1
if invertbx.collidepoint(event.pos)==1 and event.button==1:
DB=( - DB)
TB=libbaltcalc.DECTOBT(DB)
STAT="INVERT B"
scupdate=1
#row 3
if mnix.collidepoint(event.pos)==1 and event.button==1:
if abs(DA)<=mcalclimit:
DR=libbaltcalc.mni(abs(DA))
TR=libbaltcalc.DECTOBT(DR)
STAT="mni of A (DEC)"
scupdate=1
docopy=1
else:
STAT="mni of over 36 is too large"
scupdate=1
if swapx.collidepoint(event.pos)==1 and event.button==1:
TEMPD=DA
DA=DB
DB=TEMPD
TA=libbaltcalc.DECTOBT(DA)
TB=libbaltcalc.DECTOBT(DB)
STAT="swap A & B"
scupdate=1
if helpx.collidepoint(event.pos)==1 and event.button==1:
subprocess.Popen(["python", "helpview.py", "calc.xml"])
if quitx.collidepoint(event.pos)==1 and event.button==1:
qflg=1
break
if docopy==1:
docopy=0
if resalock==1:
DA=DR
TA=libbaltcalc.DECTOBT(DA)
if resblock==1:
DB=DR
TB=libbaltcalc.DECTOBT(DB)
#special button locks - event process
if resa.collidepoint(event.pos)==1 and event.button==3:
if resalock==1:
resalock=0
else:
resalock=1
scupdate=1
if resb.collidepoint(event.pos)==1 and event.button==3:
if resblock==1:
resblock=0
else:
resblock=1
scupdate=1
#data input handlers (powered by vmui's textinput function)
if tritabx.collidepoint(event.pos)==1 and event.button==1:
texgfx=simplefont.render(TA, True, (255, 255, 255), (255, 255, 255))
screensurf.blit(texgfx, (22, 97))
TA=vmui.textinput(22, 97, fontsize=22, textstring=TA, acceptchars="-0+")
scupdate=1
DA=libbaltcalc.BTTODEC(TA)
if decabx.collidepoint(event.pos)==1 and event.button==1:
texgfx=simplefont.render(str(DA), True, (255, 255, 255), (255, 255, 255))
screensurf.blit(texgfx, (22, 121))
try:
DA=int(vmui.textinput(22, 121, fontsize=22, textstring=str(DA), acceptchars="0987654321-"))
except ValueError:
print "SYNTAX ERROR IN DECIMAL INPUT A"
STAT="SYNTAX ERROR IN DECIMAL INPUT A"
scupdate=1
TA=libbaltcalc.DECTOBT(DA)
if tritbbx.collidepoint(event.pos)==1 and event.button==1:
texgfx=simplefont.render(TB, True, (255, 255, 255), (255, 255, 255))
screensurf.blit(texgfx, (22, 161))
TB=vmui.textinput(22, 161, fontsize=22, textstring=TB, acceptchars="-0+")
scupdate=1
DB=libbaltcalc.BTTODEC(TB)
if decbbx.collidepoint(event.pos)==1 and event.button==1:
texgfx=simplefont.render(str(DB), True, (255, 255, 255), (255, 255, 255))
screensurf.blit(texgfx, (22, 186))
try:
DB=int(vmui.textinput(22, 186, fontsize=22, textstring=str(DB), acceptchars="0987654321-"))
except ValueError:
print "SYNTAX ERROR IN DECIMAL INPUT B"
STAT="SYNTAX ERROR IN DECIMAL INPUT B"
scupdate=1
TB=libbaltcalc.DECTOBT(DB)