castlecore/
window.rs

1use crossterm::terminal::size;
2use crate::functions::*;
3use crate::render::*;
4use crate::*;
5
6/// Screen render variations
7pub enum Screen {
8    Empty,
9    RenderLayer(render::RenderInterface)
10}
11
12pub enum Window {
13    TopLeft,
14    TopRight,
15    DownLeft,
16    DownCentral,
17    DownRight
18}
19
20/// Border full window (actually without loot thread)
21/// Only dev window, not for users and release at all
22pub fn write_full_window(screen: Screen) {
23
24    // Interface layer
25
26    let Ok((_cols, _rows)) = size() else { return; };
27
28    if _cols < 10 || _rows < 10 {
29        println!("[{} x {}]\nToo small", _cols, _rows);
30        return;
31    }
32
33    match screen {
34        Screen::Empty => (),
35        Screen::RenderLayer(ri) => render_layer(1, 1, _cols-1, _rows-1, ri)
36    };
37
38    // Border layer
39
40    for i in 0.._cols {
41        for j in 0.._rows {
42
43            if i == 0 && j == 0
44                { printch(i, j, &LU_CORNER); }
45            else if i == 0 && j == (_rows-1)
46                { printch(i, j, &LD_CORNER); }
47            else if i == (_cols-1) && j == 0
48                { printch(i, j, &RU_CORNER); }
49            else if i == (_cols-1) && j == (_rows-1)
50                { printch(i, j, &RD_CORNER); }
51            else if i != 0 && i != (_cols-1) && (j == 0 || j == (_rows-1))
52                { printch(i, j, &UD_LINE); }
53            else if (i == 0 || i == (_cols-1)) && j != 0 && j != (_rows-1)
54                { printch(i, j, &LR_LINE); }
55            //else
56                //{ printch(i, j, &' '); }
57
58        }
59    }
60
61    // Info layer
62
63    if _cols >= 26 { mv_print_hello(_cols/2 - 12, _rows); }
64
65}
66
67/// The border of the window that is split vertically (without loot thread now)
68/// Only dev window, not for users and release at all
69// TODO: make split_ratio changable after init window
70pub fn write_vertical_split_window(left_screen: Screen, right_screen: Screen, split_ratio: f32) {
71    
72    // Interface layer
73
74    let Ok((_cols, _rows)) = size() else { return; };
75
76    let bar_pos = ((_cols as f32 - 2.0) * split_ratio) as u16;
77
78    if split_ratio <= 0.0 { write_full_window(right_screen); return; }
79    else if split_ratio >= 1.0 {  write_full_window(left_screen); return; } 
80
81    let r1_x = bar_pos - 1;
82    let r2_x = bar_pos + 1;
83    let r1_y = _rows - 1;
84    let r2_y: u16 = 1;
85
86    // +----+-----+
87    // |    |r2   |
88    // |    |     |
89    // |  r1|     |
90    // +----+-----+
91
92    if _cols < 10 || _rows < 10 {
93        println!("[{} x {}]\nToo small", _cols, _rows);
94        return;
95    }
96
97    match left_screen {
98        Screen::Empty => (),
99        Screen::RenderLayer(left_screen_rl) => render_layer(
100            1,
101            1,
102            r1_x,
103            r1_y,
104            left_screen_rl
105        )
106    };
107
108    match right_screen {
109        Screen::Empty => (),
110        Screen::RenderLayer(right_screen_rl) => render_layer(
111            r2_x,
112            r2_y,
113            _cols-1,
114            _rows-1,
115            right_screen_rl
116        )
117    };
118
119    // Border layer
120
121    for col in 0.._cols {
122        for row in 0.._rows {
123
124            if col == 0 && row == 0
125                { printch(col, row, &LU_CORNER); }
126            else if col == 0 && row == (_rows-1)
127                { printch(col, row, &LD_CORNER); }
128            else if col == (_cols-1) && row == 0
129                { printch(col, row, &RU_CORNER); }
130            else if col == (_cols-1) && row == (_rows-1)
131                { printch(col, row, &RD_CORNER); }
132            else if col == bar_pos && row == 0
133                { printch(col, row, &UD_T); }
134            else if col == bar_pos && row == (_rows-1)
135                { printch(col, row, &DU_T); }
136            else if col == bar_pos
137                { printch(col, row, &LR_LINE); }
138            else if col != 0 && col != (_cols-1) && (row == 0 || row == (_rows-1))
139                { printch(col, row, &UD_LINE); }
140            else if (col == 0 || col == (_cols-1)) && row != 0 && row != (_rows-1)
141                { printch(col, row, &LR_LINE); }
142            //else
143                //{ printch(i, j, &' '); }
144
145        }
146    }
147
148    // Info layer
149
150    if _cols >= 26 { mv_print_hello(_cols/2 - 12, _rows); }
151
152}
153
154/// Default (user usable) interface from Altenstein and same Text RPG games
155pub fn write_default_game_window(
156    screen_1: Screen,
157    screen_2: Screen,
158    screen_3: Screen,
159    screen_4: Screen,
160    screen_5: Screen,
161    split_ratio_1: f32,
162    split_ratio_2: f32,
163    split_ratio_3: f32,
164    split_ratio_4: f32) {
165    
166    // Define layer
167
168    let split_ratio_1 = if split_ratio_1 < 0.0 {0.0} else if split_ratio_1 > 1.0 {1.0} else {split_ratio_1};
169    let split_ratio_2 = if split_ratio_2 < 0.0 {0.0} else if split_ratio_2 > 0.97 {1.0} else {split_ratio_2};
170    let split_ratio_3 = if split_ratio_3 < 0.0 {0.0} else if split_ratio_3 > 1.0 {1.0} else {split_ratio_3};
171    let split_ratio_4 = if split_ratio_4 < 0.0 {0.0} else if split_ratio_4 > 1.0 {1.0} else {split_ratio_4};
172
173
174    let Ok((_cols, _rows)) = size() else { return; };
175
176
177    let bar_sr1_x: u16 = ((_cols as f32 - 2.0) * split_ratio_1) as u16;
178    
179    let bar_sr2_y: u16 = ((_rows as f32 - 2.0) * split_ratio_2) as u16;
180
181    let bar_sr3_x: u16 = ((_cols as f32 - 2.0)/2.0 * split_ratio_3) as u16;
182
183    let bar_sr4_x: u16 = (((_cols as f32 - 2.0)/2.0) + ((_cols as f32 - 2.0)/2.0 * split_ratio_4)) as u16;
184
185
186    let s11_x: u16 = if split_ratio_1 == 0.0 {0} else {1};
187    let s11_y: u16 = if split_ratio_1 == 0.0 {0} else {1};
188
189    let s12_x: u16 = if split_ratio_1 == 0.0 {0} else if split_ratio_1 == 1.0 {bar_sr1_x + 1} else {bar_sr1_x};
190    let s12_y: u16 = if split_ratio_2 == 1.0 {bar_sr2_y+1} else {bar_sr2_y};
191
192
193    let s21_x: u16 = if split_ratio_1 == 0.0 {1} else if split_ratio_1 == 1.0 {bar_sr1_x + 1} else {bar_sr1_x + 1};
194    let s21_y: u16 = 1;
195
196    let s22_x: u16 = if split_ratio_1 == 1.0 {_cols} else {_cols - 1};
197    let s22_y: u16 = if split_ratio_2 == 1.0 {bar_sr2_y+1} else {bar_sr2_y};
198
199
200    let s31_x: u16 = 1;
201    let s31_y: u16 = bar_sr2_y + 1;
202
203    let s32_x: u16 = bar_sr3_x;
204    let s32_y: u16 = _rows - 1;
205
206
207    let s41_x: u16 = bar_sr3_x + 1;
208    let s41_y: u16 = bar_sr2_y + 1;
209
210    let s42_x: u16 = if split_ratio_4 == 1.0 {bar_sr4_x+1} else {bar_sr4_x};
211    let s42_y: u16 = _rows - 1;
212
213    
214    let s51_x: u16 = bar_sr4_x + 1;
215    let s51_y: u16 = bar_sr2_y + 1;
216
217    let s52_x: u16 = _cols - 1;
218    let s52_y: u16 = _rows - 1;
219
220    //                                      (SR1)
221    //                                        |
222    // +--------------------------------------+------------------+
223    // |s11                                   |s21               |
224    // |                                      |                  |
225    // |                                      |                  |
226    // |                                      |                  |
227    // |                                      |                  |
228    // |              Screen 1                |     Screen 2     |
229    // |                                      |                  |
230    // |                                      |                  |
231    // |                                      |                  |
232    // |                                      |                  |
233    // |                                      |                  |
234    // |                            .      s12|               s22|
235    // +----------+-----------------.-------+-+------------------+--(SR2)  
236    // |s31       |s41              .       |s51                 |
237    // |          |                 .       |                    |
238    // | Screen 3 |         Screen 4.       |      Screen 5      |
239    // |          |                 .       |                    |
240    // |       s32|                 .    s42|                 s52|
241    // +----------+-----------------.-------+--------------------+  
242    //            |                 .       |
243    //          (SR3)                     (SR4)
244
245    // Interface layer
246
247    if _cols < 45 || _rows < 15 {
248        println!("[{} x {}]\nToo small", _cols, _rows);
249        return;
250    }
251
252    if split_ratio_1 > 0.02 { match screen_1 {
253        Screen::Empty => (),
254        Screen::RenderLayer(screen_1_rl) => render_layer(
255            s11_x,
256            s11_y,
257            s12_x,
258            s12_y,
259            screen_1_rl
260        )
261    }; }
262
263    if split_ratio_1 < 0.98 { match screen_2 {
264        Screen::Empty => (),
265        Screen::RenderLayer(screen_2_rl) => render_layer(
266            s21_x,
267            s21_y,
268            s22_x,
269            s22_y,
270            screen_2_rl
271        )
272    }; }
273
274    match screen_3 {
275        Screen::Empty => (),
276        Screen::RenderLayer(screen_3_rl) => render_layer(
277            s31_x,
278            s31_y,
279            s32_x,
280            s32_y,
281            screen_3_rl
282        )
283    };
284
285    match screen_4 {
286        Screen::Empty => (),
287        Screen::RenderLayer(screen_4_rl) => render_layer(
288            s41_x,
289            s41_y,
290            s42_x,
291            s42_y,
292            screen_4_rl
293        )
294    };
295
296    match screen_5 {
297        Screen::Empty => (),
298        Screen::RenderLayer(screen_5_rl) => render_layer(
299            s51_x,
300            s51_y,
301            s52_x,
302            s52_y,
303            screen_5_rl
304        )
305    };
306
307    // Border layer
308    
309    for col in 0.._cols {
310        for row in 0.._rows {
311
312            if col == 0 && row == 0
313                { printch(col, row, &LU_CORNER); }
314            else if col == 0 && row == (_rows-1)
315                { printch(col, row, &LD_CORNER); }
316            else if col == (_cols-1) && row == 0
317                { printch(col, row, &RU_CORNER); }
318            else if col == (_cols-1) && row == (_rows-1)
319                { printch(col, row, &RD_CORNER); }
320            else if col != 0 && col != (_cols-1) && (row == 0 || row == (_rows-1))
321                { printch(col, row, &UD_LINE); }
322            else if (col == 0 || col == (_cols-1)) && row != 0 && row != (_rows-1)
323                { printch(col, row, &LR_LINE); }
324
325            if split_ratio_2 > 0.02 && split_ratio_1 != 0.0 && split_ratio_1 != 1.0 && row != 0 && row <= (bar_sr2_y-1)
326                { printch(bar_sr1_x, row, &LR_LINE); }
327            else if split_ratio_2 > 0.02 && split_ratio_1 != 0.0 && split_ratio_1 != 1.0 && row == 0
328                { printch(bar_sr1_x, row, &UD_T); }
329            else if split_ratio_2 > 0.02 && split_ratio_1 != 0.0 && split_ratio_1 != 1.0 && split_ratio_2 < 0.98 && row <= bar_sr2_y
330                { printch(bar_sr1_x, row, &DU_T); }
331            else if split_ratio_2 >= 0.98 && split_ratio_1 > 0.0 && split_ratio_1 < 1.0
332                { printch(bar_sr1_x, _rows-2, &LR_LINE); printch(bar_sr1_x, _rows-1, &DU_T); }
333
334            if split_ratio_2 < 0.98 && bar_sr2_y != 0 && bar_sr2_y != _rows && col != 0 && col != (_cols-1)
335                { printch(col, bar_sr2_y, &UD_LINE); }
336            else if bar_sr2_y != 0 && bar_sr2_y != _rows && col == 0
337                { printch(col, bar_sr2_y, &LR_T); }
338            else if bar_sr2_y != 0 && bar_sr2_y != _rows && col == (_cols-1)
339                { printch(col, bar_sr2_y, &RL_T); }
340
341            if bar_sr3_x != 0 && bar_sr3_x != _cols && row != (_rows-1) && row >= (bar_sr2_y+1)
342                { printch(bar_sr3_x, row, &LR_LINE); }
343            else if bar_sr3_x != 0 && bar_sr3_x != _cols && split_ratio_2 < 0.98 && row == (bar_sr2_y)
344                { printch(bar_sr3_x, row, &UD_T); }
345            else if bar_sr3_x != 0 && bar_sr3_x != _cols && split_ratio_2 < 0.98 && row == (_rows-1)
346                { printch(bar_sr3_x, row, &DU_T); }
347
348            if split_ratio_4 != 1.0 && bar_sr4_x != 0 && bar_sr4_x != _cols && row != (_rows-1) && row >= (bar_sr2_y+1)
349                { printch(bar_sr4_x, row, &LR_LINE); }
350            else if split_ratio_4 != 1.0 && bar_sr4_x != 0 && bar_sr4_x != _cols && split_ratio_2 < 0.98 && row == (bar_sr2_y)
351                { printch(bar_sr4_x, row, &UD_T); }
352            else if split_ratio_4 != 1.0 && bar_sr4_x != 0 && bar_sr4_x != _cols && split_ratio_2 < 0.98 && row == (_rows-1)
353                { printch(bar_sr4_x, row, &DU_T); }
354
355            if bar_sr1_x == bar_sr4_x && bar_sr1_x != 0 && bar_sr1_x != _cols
356                { printch(bar_sr1_x, bar_sr2_y, &CROSS); }
357
358        }
359    }
360    
361    // Info layer
362
363    //if _cols >= 26 { mv_print_hello(1, 0); }
364
365}