-
Notifications
You must be signed in to change notification settings - Fork 26
/
cemu.py
113 lines (85 loc) · 2.99 KB
/
cemu.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
from PyQt5 import QtGui, QtCore, QtWidgets
class Cursor(object):
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, dx, dy):
self.x += dx
self.y += dy
def moveAbsolute(self, x, y):
self.x = x
self.y = y
def getPosition(self):
return self.x, self.y
def enum(**enums):
return type('Enum', (), enums)
Directions = enum(Left=1, Right=2, Up=3, Down=4, End=5, Home=6, CtrlEnd=7, CtrlHome=8)
class ConsoleEmulator():
def __init__(self, qp, rows, cols):
self.qp = qp
self._x = 0
self._y = 0
self._rows = rows
self._cols = cols
fm = QtGui.QFontMetrics(self.qp.font())
self.fontWidth = fm.width('a')
self.fontHeight = fm.height()
def incrementPosition(self):
if self._x < self._cols - 1:
self._x += 1
else:
self._x = 0
self._y += 1
def newLine(self):
self.LF()
self.CR()
def LF(self):
if self._y < self._rows:
self._y += 1
def CR(self):
self._x = 0
def _validatePosition(self, x, y):
if x >= self._cols:
raise Exception("x > cols")
if y >= self._rows:
raise Exception("y > rows")
return True
def write(self, s, noBackgroudOnSpaces=False):
# self.qp.drawText(self._x * self.fontWidth, self.fontHeight + self._y * self.fontHeight, s )
# x, y = self.getXY()
# self.gotoXY(x, y)
# return
background = self.qp.backgroundMode()
if type(s) == bytes:
s = s.decode('cp437')
for c in s:
if self._validatePosition(self._x, self._y):
if noBackgroudOnSpaces and c == ' ':
self.qp.setBackgroundMode(0)
self.qp.drawText(int(self._x * self.fontWidth), int(self.fontHeight + self._y * self.fontHeight), c)
self.incrementPosition()
self.qp.setBackgroundMode(background)
def write_c(self, c, noBackgroudOnSpaces=False):
background = self.qp.backgroundMode()
if self._validatePosition(self._x, self._y):
if noBackgroudOnSpaces and c == ' ':
self.qp.setBackgroundMode(0)
self.qp.drawText(self._x * self.fontWidth, self.fontHeight + self._y * self.fontHeight, c)
self.incrementPosition()
self.qp.setBackgroundMode(background)
def getXY(self):
return (self._x, self._y)
def writeAt(self, x, y, s, noBackgroudOnSpaces=False):
self.gotoXY(x, y)
self.write(s, noBackgroudOnSpaces)
def writeAt_c(self, x, y, c, noBackgroudOnSpaces=False):
self.gotoXY(x, y)
self.write_c(c, noBackgroudOnSpaces)
def writeLn(self):
if True:#self._validatePosition(self._x, self._y):
self._y += 1
self._x = 0
def gotoXY(self, x, y):
if self._validatePosition(x, y):
self._x = x
self._y = y