-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.c
73 lines (70 loc) · 2.27 KB
/
edit.c
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
#include <ncurses.h>
#include "pattern.h"
#include "status.h"
static void print_pattern(WINDOW *pattern_win, Pattern *pattern)
{
for (size_t y = 0; y < pattern->size.y; y++)
{
for (size_t x = 0; x < pattern->size.x; x++)
{
if (pattern->cells[y][x] == state_alive)
{
wattron(pattern_win, A_STANDOUT);
}
mvwprintw(pattern_win, y + 1, x * 2 + 1, " ");
wattroff(pattern_win, A_STANDOUT);
}
waddch(pattern_win, ACS_VLINE);
}
wrefresh(pattern_win);
}
void edit_pattern(Pattern *pattern)
{
size_t screen_size_y, screen_size_x; // = Terminal magassag, szelesseg
getmaxyx(stdscr, screen_size_y, screen_size_x);
if (screen_size_y - 2 < pattern->size.y || screen_size_x / 2 < pattern->size.x)
{
print_status(warning, "The pattern is bigger than your terminal window.");
return;
}
WINDOW *pattern_win = newwin(pattern->size.y + 2, ((pattern->size.x + 1) * 2), ((screen_size_y - pattern->size.y) - 2) / 2, ((screen_size_x - (pattern->size.x + 1) * 2)) / 2);
if (pattern_win == NULL)
{
print_status(error, "Pattern window allocation failed.");
return;
}
clear();
print_status(info, "Loaded.");
keypad(pattern_win, TRUE);
box(pattern_win, ACS_VLINE, ACS_HLINE);
print_pattern(pattern_win, pattern);
wrefresh(pattern_win);
int c;
size_t mouse_pos_y, mouse_pos_x;
MEVENT event;
while ((c = wgetch(stdscr)) != 'x')
{
switch (c)
{
case KEY_MOUSE:
#if defined(__MINGW64__) || (__MINGW32__)
if (nc_getmouse(&event) == OK)
#else
if (getmouse(&event) == OK)
#endif
{
mouse_pos_y = event.y - ((screen_size_y - pattern->size.y) - 2) / 2 - 1;
mouse_pos_x = (event.x - ((screen_size_x - (pattern->size.x + 1) * 2)) / 2 + 1) / 2 - 1;
}
if ((event.bstate & (BUTTON1_PRESSED)) && (mouse_pos_y < pattern->size.y) && (mouse_pos_x < pattern->size.x))
{
flip_cell(pattern, mouse_pos_y, mouse_pos_x);
}
print_pattern(pattern_win, pattern);
wrefresh(pattern_win);
break;
default:
break;
}
}
}