Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tkvt100.py 7.3KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. #
  4. """Module to emulate a VT100 terminal in Tkinter.
  5. Maintainer: Paul Swartz
  6. """
  7. import string
  8. import tkinter as Tkinter
  9. import tkinter.font as tkFont
  10. from . import ansi
  11. ttyFont = None # tkFont.Font(family = 'Courier', size = 10)
  12. fontWidth, fontHeight = (
  13. None,
  14. None,
  15. ) # max(map(ttyFont.measure, string.letters+string.digits)), int(ttyFont.metrics()['linespace'])
  16. colorKeys = (
  17. "b",
  18. "r",
  19. "g",
  20. "y",
  21. "l",
  22. "m",
  23. "c",
  24. "w",
  25. "B",
  26. "R",
  27. "G",
  28. "Y",
  29. "L",
  30. "M",
  31. "C",
  32. "W",
  33. )
  34. colorMap = {
  35. "b": "#000000",
  36. "r": "#c40000",
  37. "g": "#00c400",
  38. "y": "#c4c400",
  39. "l": "#000080",
  40. "m": "#c400c4",
  41. "c": "#00c4c4",
  42. "w": "#c4c4c4",
  43. "B": "#626262",
  44. "R": "#ff0000",
  45. "G": "#00ff00",
  46. "Y": "#ffff00",
  47. "L": "#0000ff",
  48. "M": "#ff00ff",
  49. "C": "#00ffff",
  50. "W": "#ffffff",
  51. }
  52. class VT100Frame(Tkinter.Frame):
  53. def __init__(self, *args, **kw):
  54. global ttyFont, fontHeight, fontWidth
  55. ttyFont = tkFont.Font(family="Courier", size=10)
  56. fontWidth = max(map(ttyFont.measure, string.ascii_letters + string.digits))
  57. fontHeight = int(ttyFont.metrics()["linespace"])
  58. self.width = kw.get("width", 80)
  59. self.height = kw.get("height", 25)
  60. self.callback = kw["callback"]
  61. del kw["callback"]
  62. kw["width"] = w = fontWidth * self.width
  63. kw["height"] = h = fontHeight * self.height
  64. Tkinter.Frame.__init__(self, *args, **kw)
  65. self.canvas = Tkinter.Canvas(bg="#000000", width=w, height=h)
  66. self.canvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)
  67. self.canvas.bind("<Key>", self.keyPressed)
  68. self.canvas.bind("<1>", lambda x: "break")
  69. self.canvas.bind("<Up>", self.upPressed)
  70. self.canvas.bind("<Down>", self.downPressed)
  71. self.canvas.bind("<Left>", self.leftPressed)
  72. self.canvas.bind("<Right>", self.rightPressed)
  73. self.canvas.focus()
  74. self.ansiParser = ansi.AnsiParser(ansi.ColorText.WHITE, ansi.ColorText.BLACK)
  75. self.ansiParser.writeString = self.writeString
  76. self.ansiParser.parseCursor = self.parseCursor
  77. self.ansiParser.parseErase = self.parseErase
  78. # for (a, b) in colorMap.items():
  79. # self.canvas.tag_config(a, foreground=b)
  80. # self.canvas.tag_config('b'+a, background=b)
  81. # self.canvas.tag_config('underline', underline=1)
  82. self.x = 0
  83. self.y = 0
  84. self.cursor = self.canvas.create_rectangle(
  85. 0, 0, fontWidth - 1, fontHeight - 1, fill="green", outline="green"
  86. )
  87. def _delete(self, sx, sy, ex, ey):
  88. csx = sx * fontWidth + 1
  89. csy = sy * fontHeight + 1
  90. cex = ex * fontWidth + 3
  91. cey = ey * fontHeight + 3
  92. items = self.canvas.find_overlapping(csx, csy, cex, cey)
  93. for item in items:
  94. self.canvas.delete(item)
  95. def _write(self, ch, fg, bg):
  96. if self.x == self.width:
  97. self.x = 0
  98. self.y += 1
  99. if self.y == self.height:
  100. [self.canvas.move(x, 0, -fontHeight) for x in self.canvas.find_all()]
  101. self.y -= 1
  102. canvasX = self.x * fontWidth + 1
  103. canvasY = self.y * fontHeight + 1
  104. items = self.canvas.find_overlapping(canvasX, canvasY, canvasX + 2, canvasY + 2)
  105. if items:
  106. [self.canvas.delete(item) for item in items]
  107. if bg:
  108. self.canvas.create_rectangle(
  109. canvasX,
  110. canvasY,
  111. canvasX + fontWidth - 1,
  112. canvasY + fontHeight - 1,
  113. fill=bg,
  114. outline=bg,
  115. )
  116. self.canvas.create_text(
  117. canvasX, canvasY, anchor=Tkinter.NW, font=ttyFont, text=ch, fill=fg
  118. )
  119. self.x += 1
  120. def write(self, data):
  121. self.ansiParser.parseString(data)
  122. self.canvas.delete(self.cursor)
  123. canvasX = self.x * fontWidth + 1
  124. canvasY = self.y * fontHeight + 1
  125. self.cursor = self.canvas.create_rectangle(
  126. canvasX,
  127. canvasY,
  128. canvasX + fontWidth - 1,
  129. canvasY + fontHeight - 1,
  130. fill="green",
  131. outline="green",
  132. )
  133. self.canvas.lower(self.cursor)
  134. def writeString(self, i):
  135. if not i.display:
  136. return
  137. fg = colorMap[i.fg]
  138. bg = i.bg != "b" and colorMap[i.bg]
  139. for ch in i.text:
  140. b = ord(ch)
  141. if b == 7: # bell
  142. self.bell()
  143. elif b == 8: # BS
  144. if self.x:
  145. self.x -= 1
  146. elif b == 9: # TAB
  147. [self._write(" ", fg, bg) for index in range(8)]
  148. elif b == 10:
  149. if self.y == self.height - 1:
  150. self._delete(0, 0, self.width, 0)
  151. [
  152. self.canvas.move(x, 0, -fontHeight)
  153. for x in self.canvas.find_all()
  154. ]
  155. else:
  156. self.y += 1
  157. elif b == 13:
  158. self.x = 0
  159. elif 32 <= b < 127:
  160. self._write(ch, fg, bg)
  161. def parseErase(self, erase):
  162. if ";" in erase:
  163. end = erase[-1]
  164. parts = erase[:-1].split(";")
  165. [self.parseErase(x + end) for x in parts]
  166. return
  167. start = 0
  168. x, y = self.x, self.y
  169. if len(erase) > 1:
  170. start = int(erase[:-1])
  171. if erase[-1] == "J":
  172. if start == 0:
  173. self._delete(x, y, self.width, self.height)
  174. else:
  175. self._delete(0, 0, self.width, self.height)
  176. self.x = 0
  177. self.y = 0
  178. elif erase[-1] == "K":
  179. if start == 0:
  180. self._delete(x, y, self.width, y)
  181. elif start == 1:
  182. self._delete(0, y, x, y)
  183. self.x = 0
  184. else:
  185. self._delete(0, y, self.width, y)
  186. self.x = 0
  187. elif erase[-1] == "P":
  188. self._delete(x, y, x + start, y)
  189. def parseCursor(self, cursor):
  190. # if ';' in cursor and cursor[-1]!='H':
  191. # end = cursor[-1]
  192. # parts = cursor[:-1].split(';')
  193. # [self.parseCursor(x+end) for x in parts]
  194. # return
  195. start = 1
  196. if len(cursor) > 1 and cursor[-1] != "H":
  197. start = int(cursor[:-1])
  198. if cursor[-1] == "C":
  199. self.x += start
  200. elif cursor[-1] == "D":
  201. self.x -= start
  202. elif cursor[-1] == "d":
  203. self.y = start - 1
  204. elif cursor[-1] == "G":
  205. self.x = start - 1
  206. elif cursor[-1] == "H":
  207. if len(cursor) > 1:
  208. y, x = map(int, cursor[:-1].split(";"))
  209. y -= 1
  210. x -= 1
  211. else:
  212. x, y = 0, 0
  213. self.x = x
  214. self.y = y
  215. def keyPressed(self, event):
  216. if self.callback and event.char:
  217. self.callback(event.char)
  218. return "break"
  219. def upPressed(self, event):
  220. self.callback("\x1bOA")
  221. def downPressed(self, event):
  222. self.callback("\x1bOB")
  223. def rightPressed(self, event):
  224. self.callback("\x1bOC")
  225. def leftPressed(self, event):
  226. self.callback("\x1bOD")