home · contact · privacy
31d66f085c70cfd7d6a434d7a65bbffaa8969968
[plomrogue] / client_prototype.py
1 import curses
2 import signal
3
4
5 def set_window_geometries():
6
7     def set_window_size():
8         win["size"], win["start"] = [0, 0], [0, 0]
9         win["size"][0] = win["config"][0]
10         if (win["config"][0] == 0):
11             win["size"][0] = screen_size[0] - sep_size
12         elif (win["config"][0] < 0):
13             win["size"][0] = screen_size[0] + win["config"][0] - sep_size
14         win["size"][1] = win["config"][1]
15         if (win["config"][1] == 0):
16             win["size"][1] = screen_size[1]
17         elif (win["config"][1] < 0):
18             win["size"][1] = screen_size[1] + win["config"][1]
19
20     def place_window():
21         win_i = windows.index(win)
22
23         # If win is first window, it goes into the top left corner.
24         win["start"][0] = 0 + sep_size
25         win["start"][1] = 0
26         if (win_i > 0):
27
28             # If not, get win's closest predecessor starting a new stack on the
29             # screen top,fit win's top left to that win_top's top right corner.
30             win_top = None
31             for i in range(win_i - 1, -1, -1):
32                 win_top = windows[i]
33                 if (win_top["start"][0] == 0 + sep_size):
34                     break
35             win["start"][1] = win_top["start"][1] + win_top["size"][1] \
36                 + sep_size
37
38             # If enough space is found below win's predecessor, fit win's top
39             # left corner to that predecessor's bottom left corner.
40             win_prev = windows[win_i - 1]
41             next_free_y = win_prev["start"][0] + win_prev["size"][0] + sep_size
42             if (win["size"][1] <= win_prev["size"][1] and
43                     win["size"][0] <= screen_size[0] - next_free_y):
44                 win["start"][1] = win_prev["start"][1]
45                 win["start"][0] = next_free_y
46
47             # If that fails, try to fit win's top left corner to the top right
48             # corner of its closest predecessor win_test 1) below win_top (see
49             # above) 2) and with enough space open to its right between its
50             # right edge and the lower edge of a win_high located directly
51             # above win_test to fit win there (without growing further to the
52             # right than win_high does or surpassing the screen's lower edge).
53             else:
54                 win_test = win_prev
55                 win_high = None
56                 while (win_test != win_top):
57                     for i in range(win_i - 2, -1, -1):
58                         win_high = windows[i]
59                         if win_test["start"][0] > win_high["start"][0]:
60                             break
61                     next_free_y = win_high["start"][0] + win_high["size"][0] \
62                         + sep_size
63                     first_free_x = win_test["start"][1] + win_test["size"][1] \
64                         + sep_size
65                     last_free_x = win_high["start"][1] + win_high["size"][1]
66                     if (win["size"][0] <= screen_size[0] - next_free_y and
67                             win["size"][1] <= last_free_x - first_free_x):
68                         win["start"][1] = first_free_x
69                         win["start"][0] = next_free_y
70                         break
71                     win_test = win_high
72
73     global screen_size, stdscr
74     curses.endwin()
75     stdscr = curses.initscr()
76     screen_size = stdscr.getmaxyx()
77     for win in windows:
78         set_window_size()
79         place_window()
80
81
82 def draw_screen():
83
84     def draw_window_border_lines():
85         for win in windows:
86             for k in range(2):
87                 j = win["start"][int(k == 0)] - sep_size
88                 if (j >= 0 and j < screen_size[int(k == 0)]):
89                     start = win["start"][k]
90                     start = start if start >= 0 else 0
91                     end = win["start"][k] + win["size"][k]
92                     end = end if end < screen_size[k] else screen_size[k]
93                     if k:
94                         [stdscr.addch(j, i, '-') for i in range(start, end)]
95                     else:
96                         [stdscr.addch(i, j, '|') for i in range(start, end)]
97
98     def draw_window_border_corners():
99         for win in windows:
100             up = win["start"][0] - sep_size
101             down = win["start"][0] + win["size"][0]
102             left = win["start"][1] - sep_size
103             right = win["start"][1] + win["size"][1]
104             if (up >= 0 and up < screen_size[0]):
105                 if (left >= 0 and left < screen_size[1]):
106                     stdscr.addch(up, left, '+')
107                 if (right >= 0 and right < screen_size[1]):
108                     stdscr.addch(up, right, '+')
109             if (down >= 0 and down < screen_size[0]):
110                 if (left >= 0 and left < screen_size[1]):
111                     stdscr.addch(down, left, '+')
112                 if (right >= 0 and right < screen_size[1]):
113                     stdscr.addch(down, right, '+')
114
115     def draw_window_contents():
116         for win in windows:
117             offset, size, winmap = win["func"]()
118             stop = [0, 0]
119             for i in range(2):
120                 stop[i] = win["size"][i] + offset[i]
121                 stop[i] = stop[i] if stop[i] < size[i] else size[i]
122             for y in range(offset[0], stop[0]):
123                 for x in range(offset[1], stop[1]):
124                     cell = winmap[y * size[1] + x]
125                     y_in_screen = win["start"][0] + (y - offset[0])
126                     x_in_screen = win["start"][1] + (x - offset[1])
127                     if (y_in_screen < screen_size[0]
128                             and x_in_screen < screen_size[1]):
129                         stdscr.addch(y_in_screen, x_in_screen, cell)
130
131     stdscr.clear()
132     draw_window_border_lines()
133     draw_window_border_corners()
134     draw_window_contents()
135     stdscr.refresh()
136
137
138 def main(stdscr):
139     curses.noecho()
140     curses.curs_set(False)
141     # stdscr.keypad(True)
142     signal.signal(signal.SIGWINCH,
143         lambda ignore_1, ignore_2: set_window_geometries())
144     set_window_geometries()
145     while True:
146         draw_screen()
147         char = stdscr.getch()
148         if (char >= 0 and chr(char) == 'Q'):
149             exit()
150
151 def foo():
152     winmap = ['.', 'o', '.', 'o', 'O', 'o', '.', 'o', '.', 'x', 'y', 'x']
153     size = [4, 3]
154     offset = [0, 0]
155     return offset, size, winmap
156
157
158 windows = [
159     {"config": [1, 33], "func": foo},
160     {"config": [-7, 33], "func": foo},
161     {"config": [4, 16], "func": foo},
162     {"config": [4, 16], "func": foo},
163     {"config": [0, -34], "func": foo}
164 ]
165
166 sep_size = 1  # Width of inter-window borders and title bars.
167 stdscr = None
168 screen_size = [0,0]
169
170 curses.wrapper(main)