This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
base.py
154 lines (124 loc) · 4.23 KB
/
base.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
""""
Simple classes for the PyHex Window and Application
"""
import curses
import time
class Window:
"""
Simple class for a curses Window
"""
def __init__(self, stdscr, application):
super(__class__, self).__init__()
# Saves the Screen of the Window and the parent application in a variable
self.stdscr: curses.window = stdscr
self.application: Application = application
# Creating the variables of the size of the Window
self.width = int
self.height = int
# Creating the variable for the Keyinput
self.key_pressed = int
def init_colors(self):
"""
Function for initiating the colors of a Window.
Must be called manually.
"""
def update(self):
"""
This function will be called every time the Window updates.
Mostly, you will do calculations of coordinates for the drawing in here.
"""
def late_update(self):
"""
This function will be called every time after the update function.
Mostly, you will draw things on the screen here.
"""
def check_keys(self):
"""
This function will be called every frame.
"""
def window_update(self):
"""
This function will be called from the Application and calls
functions like update or lateUpdate.
Do not change anything in here!
"""
self.height, self.width = self.stdscr.getmaxyx()
self.key_pressed = self.stdscr.getch()
self.check_keys()
self.update()
# Clear the Screen
self.stdscr.erase()
self.late_update()
# Refreshing the Screen at the end of the Frame
self._update_screen()
def _update_screen(self):
"""
This function just refreshes the Screen.
"""
self.stdscr.refresh()
def draw_text(self, y_coord, x_coord, text, color_code):
"""
This function draws text on the screen
:param y_coord: The y-Coordinate
:param x_coord: The x_coord-Coordinate
:param text: The text that will be drawn at the Coordinates
:param color_code: The Code of the Color Pair you want to use for the text
"""
self.stdscr.attron(curses.A_BOLD)
self.stdscr.attron(curses.color_pair(color_code))
self.stdscr.addstr(y_coord, x_coord, text)
self.stdscr.attroff(curses.A_BOLD)
self.stdscr.attroff(curses.color_pair(color_code))
@staticmethod
def set_cursor_state(state: int):
"""
Changes the Cursor in the window
:param state: 0 - Hides the Cursor
:param state: 1 - Shows the Cursor
:param state: 2 - Makes the Cursor highly visible
"""
curses.curs_set(state)
def exit(self):
"""
This function will be called, when the program closes
"""
class Application:
"""
Simple class for a application managing a window
"""
colormode: bool
no_delay: bool
def __init__(self):
super(__class__, self).__init__()
time.sleep(0.5)
# First, initiating the Screen and defining a variable for the MainWindow of the Application
self.stdscr = curses.initscr()
self.stdscr.keypad(True)
self.window = None
curses.noecho()
curses.cbreak()
# Check, if the terminal supports colors and activates the NoDelay mode, if enabled
self.colormode = curses.has_colors()
self.no_delay = False
if self.no_delay:
self.stdscr.nodelay(True)
def set_main_window(self, window: Window):
"""
This function sets the MainWindow of the application
:param window: The Window
"""
self.window = window
def run(self, time_wait=0.015):
"""
This function starts the Programm
:param time_wait: Time to wait between the frame updates
"""
# Draw the window at beginning
self.window.height, self.window.width = self.stdscr.getmaxyx()
self.window.update()
self.stdscr.erase()
self.window.late_update()
self.window._update_screen()
while True:
self.window.window_update()
time.sleep(time_wait)