-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
191 lines (180 loc) · 4.12 KB
/
main.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <ncurses.h>
#include <stdbool.h>
#include "debugmalloc.h"
#include "edit.h"
#include "file.h"
#include "input.h"
#include "menu.h"
#include "pattern.h"
#include "play.h"
#include "status.h"
bool warn_dirty();
void event_loop();
int screen_size_x, screen_size_y;
int menu_items_n = 7;
char *menu_items[] = {"New pattern", "Load pattern", "Play", "Edit", "Save", "Refresh menu", "Exit"};
Pattern *pattern = NULL;
int main()
{
// Ncurses inicializácó
initscr();
cbreak();
noecho();
mouseinterval(0);
keypad(stdscr, TRUE);
mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
curs_set(0);
start_color();
init_status();
while (true)
{
event_loop();
}
}
bool warn_dirty()
{
clear();
print_status(warning, "Your pattern was modified since the last save. Continue?");
char *choices[] = {"Go back", "Continue"};
if (1 == show_menu(choices, 2, 2))
{
return true;
}
return false;
}
void event_loop()
{
int selection = show_menu(menu_items, menu_items_n, 2);
switch (selection)
{
case 0: // New pattern
if (pattern != NULL)
{
if (pattern->dirty)
{
if (warn_dirty())
{
free_pattern(pattern);
pattern = new_empty_pattern();
}
}
else
{
free_pattern(pattern);
pattern = new_empty_pattern();
}
}
else
pattern = new_empty_pattern();
break;
case 1: // Load pattern
if (pattern != NULL)
{
if (pattern->dirty)
{
if (warn_dirty())
{
free_pattern(pattern);
pattern = NULL;
pattern = load_file();
}
}
else
{
free_pattern(pattern);
pattern = NULL;
pattern = load_file();
}
}
else
{
pattern = load_file();
}
if (pattern != NULL)
{
print_status(successful, "Pattern (%s) loaded successfully!", pattern->name);
}
break;
case 2: // Play
if (pattern == NULL)
{
print_status(info, "No pattern loaded. Use 'New pattern' or 'Load pattern'");
}
else
{
if (pattern->dirty)
{
if (warn_dirty())
{
play_pattern(pattern);
}
}
else
{
play_pattern(pattern);
}
}
break;
case 3: // Edit
if (pattern != NULL)
{
if (pattern->dirty)
{
if (warn_dirty())
{
edit_pattern(pattern);
}
}
else
{
edit_pattern(pattern);
}
}
else
{
print_status(info, "No pattern loaded. Use 'New pattern' or 'Load pattern'");
}
break;
case 4: // Save
if (pattern == NULL)
{
print_status(info, "No pattern loaded. Use 'New pattern' or 'Load pattern'");
}
else
{
save_pattern(pattern);
}
break;
case 5: //
getmaxyx(stdscr, screen_size_y, screen_size_x);
wresize(stdscr, screen_size_y, screen_size_x);
clear();
// resizeterm(screen_size_y, screen_size_x);
refresh();
break;
case 6: // Exit
if (pattern != NULL)
{
if (pattern->dirty)
{
if (warn_dirty())
{
free_pattern(pattern);
}
else
{
break;
}
}
else
{
free_pattern(pattern);
}
}
endwin();
exit(EXIT_SUCCESS);
break;
default:
break;
}
}