A Python module for text display using no library external import. Tinkered mainly for 90+e (fx-CG50) and 35+e II, works on any version of MicroPython/Python. https://tinyurl.com/locate2py
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.

104 lines
2.6KB

  1. class Screen:
  2. def __init__(self, width=21, height=6, patern=" ", copy=None):
  3. if isinstance(copy, Screen):
  4. self._width = copy._width
  5. self._height = copy._height
  6. self._mat = copy._mat
  7. else:
  8. self._width = width
  9. self._height = height
  10. self.fill(patern)
  11. def locate(self, x, y, string):
  12. string = str(string)
  13. i = -1
  14. for char in string:
  15. if i + x < self._width:
  16. self._mat[y - 1][x + i] = char
  17. i += 1
  18. def locate_v(self, x, y, string):
  19. string = str(string)
  20. i = -1
  21. for char in string:
  22. if i + y < self._height:
  23. self._mat[y + i][x - 1] = char
  24. i += 1
  25. def fill(self, patern=" "):
  26. self._mat = [[patern[0] for i in range(self._width)] for i in range(self._height)]
  27. def refresh(self, ask_for_input=True, endl="\n", fancy=False, fmargin=4):
  28. to_print = str()
  29. for line in self._mat[:-1] if fancy else self._mat:
  30. for cell in line:
  31. to_print += cell
  32. to_print += "\n"
  33. to_print = to_print[:-1]
  34. print(to_print)
  35. if ask_for_input or fancy:
  36. line = ""
  37. if fancy:
  38. for cell in self._mat[-1][:-fmargin]:
  39. line += cell
  40. return input(line)
  41. return input("> ")
  42. else:
  43. print("", end=endl)
  44. return None
  45. def get_cell(self, x, y):
  46. return self._mat[y - 1][x - 1]
  47. def get_dim(self):
  48. return self._width, self._height
  49. def export(self):
  50. result = str()
  51. for line in self._mat:
  52. for cell in line:
  53. result += cell
  54. return result
  55. def load(self, string):
  56. i = 0
  57. s = 0
  58. while i != self._height:
  59. self._mat[i] = list(string[s:s + self._width])
  60. i += 1
  61. s += self._width
  62. get_cell_content = get_cell # For retro-compability
  63. class Pad(Screen):
  64. def refresh(self, x=1, y=1, width=21, height=7, ask_for_input=True,\
  65. endl="\n", fancy=True, fmargin=4):
  66. ogwidth = width
  67. if width > self._width:
  68. width = self._width
  69. if height > self._height:
  70. height = self._height
  71. if x < 1:
  72. x = 1
  73. if y < 1:
  74. y = 1
  75. x -= 1
  76. y -= 1
  77. if x + width > self._width:
  78. x = self._width - width
  79. if y + height > self._height:
  80. y = self._height - height
  81. to_print = str()
  82. for dy in range(y, height+y):
  83. dy = dy
  84. for dx in range(x, width+x):
  85. to_print += self._mat[dy][dx]
  86. to_print += "\n"
  87. to_print = to_print[:-1]
  88. print(to_print[:-width], end="")
  89. if ask_for_input or fancy:
  90. return input(to_print[-width:-1]+ " " * (ogwidth - width - fmargin) if fancy else "> ")
  91. else:
  92. print(to_print[-width:], end=endl)
  93. return None