-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridO.h
340 lines (295 loc) · 8.82 KB
/
gridO.h
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*!
\file gridO.h
\brief A curses based library to handle text based grid output
\author Jochymski Hugo
\date 2021
*/
#include <curses.h>
/* Cell apparence */
#define CELL_CORNER "+" /*!< Cell corner apparence */
// (2y size recommended on default settings)
#define CELL_WIDTH 2 /*!< Cell x-edge size */
#define CELL_HEIGHT 1 /*!< Cell y-edge size */
#define CELL_SIZE (CELL_WIDTH*CELL_HEIGHT) /*!< Space within the cell */
#define CELL_XAXE "-" /*!< Cell x-axis edge apparence */
#define CELL_YAXE "|" /*!< Cell y-axis edge apparence */
/* Color related */
// #define BG_COLOR COLOR_BLACK
#define COLOR_ORANGE 60
#define COLOR_PINK 80
#define CLR_YLW 1
#define CLR_BLU 2
#define CLR_RED 3
#define CLR_GRN 4
#define CLR_WHT 5
#define CLR_ORG 6
#define CLR_VLT 7
#define CLR_PNK 8
#define CLR_CYN 9
/******************************************************************************/
/*********************************** UTILS ************************************/
/******************************************************************************/
/*!
\brief Converts a y position on the grid to the real position on the file
\param[in] y Y position to convert
\return The converted position (integer)
*/
int yGridToReal(int y)
{
if (y == 0) return 0;
else return (y*CELL_HEIGHT + y);
}
/*!
\brief Converts a x position on the grid to the real position on the file
\param[in] x X position to convert
\return The converted position (integer)
*/
int xGridToReal(int x)
{
if (x == 0) return 0;
else return (x*CELL_WIDTH + x);
}
/*!
\brief Mimic the strlen function from string.h
\param[in] str String to be examinated
\return Length of \p str
*/
int strLen(char* str)
{
int size = 0;
while (str[size])
size++;
return size;
}
/******************************************************************************/
/********************************* BASIC GRID *********************************/
/******************************************************************************/
/*!
\brief Prints a cell using the \p x and \p y grid coordinates
\param[in] x X coordinate
\param[in] y Y coordinate
\return -1 on error, 0 on succes
*/
int gp_cell(int x, int y)
{
if (x < 0 || y < 0)
return -1;
//Corners
mvprintw(yGridToReal(y), xGridToReal(x), CELL_CORNER);
mvprintw(yGridToReal(y), xGridToReal(x)+CELL_WIDTH+1, CELL_CORNER);
mvprintw(yGridToReal(y)+CELL_HEIGHT+1, xGridToReal(x), CELL_CORNER);
mvprintw(yGridToReal(y)+CELL_HEIGHT+1,
xGridToReal(x)+CELL_WIDTH+1, CELL_CORNER);
//x-axis edges
for (int i=0; i<CELL_WIDTH; i++)
{
//top
mvprintw(yGridToReal(y), xGridToReal(x)+i+1, CELL_XAXE);
//bot
mvprintw(yGridToReal(y)+CELL_HEIGHT+1,
xGridToReal(x)+i+1, CELL_XAXE);
}
//y-axis edges
for (int i=0; i<CELL_HEIGHT; i++)
{
//left
mvprintw(yGridToReal(y)+i+1, xGridToReal(x), CELL_YAXE);
//right
mvprintw(yGridToReal(y)+i+1,
xGridToReal(x)+CELL_WIDTH+1, CELL_YAXE);
}
return 0;
}
/*!
\brief Prints a \p length long line at the \p y coordinate starting at \
\p start on the x-axis
\param[in] start Starting point on the x-axis
\param[in] y Y coordinate
\param[in] len Line length
\return -1 on error, 0 on succes
*/
int gp_line(int start, int y, int len)
{
if (start < 0 || y < 0 || len < 0)
return -1;
for (int i=start; i<len; i++)
gp_cell(i, y);
return 0;
}
/*!
\brief Prints a \p len long row at the \p x coordinate starting at \
\p start on the y-axis
\param[in] x X coordinate
\param[in] start Starting point on the y-axis
\param[in] len Row length
\return -1 on error, 0 on succes
*/
int gp_row(int x, int start, int len)
{
if (start < 0 || x < 0 || len < 0)
return -1;
for (int i=start; i<len; i++)
gp_cell(x, i);
return 0;
}
/******************************************************************************/
/******************************** COLORED GRID ********************************/
/******************************************************************************/
/*!
\brief Prints a \color cell using the \p x and \p y grid coordinates
\param[in] x X coordinate
\param[in] y Y coordinate
\param[in] color The color to be used
\return -1 on error, 0 on succes
\note Color is enabled at the beginning and disabled at the end
*/
int gpc_cell(int x, int y, int color)
{
if (x < 0 || y < 0)
return -1;
//Turn color on
if (attron(COLOR_PAIR(color)) == ERR) return -1;
//Corners
mvprintw(yGridToReal(y), xGridToReal(x), CELL_CORNER);
mvprintw(yGridToReal(y), xGridToReal(x)+CELL_WIDTH+1, CELL_CORNER);
mvprintw(yGridToReal(y)+CELL_HEIGHT+1, xGridToReal(x), CELL_CORNER);
mvprintw(yGridToReal(y)+CELL_HEIGHT+1,
xGridToReal(x)+CELL_WIDTH+1, CELL_CORNER);
//x-axis edges
for (int i=0; i<CELL_WIDTH; i++)
{
//top
mvprintw(yGridToReal(y), xGridToReal(x)+i+1, CELL_XAXE);
//bot
mvprintw(yGridToReal(y)+CELL_HEIGHT+1,
xGridToReal(x)+i+1, CELL_XAXE);
}
//y-axis edges
for (int i=0; i<CELL_HEIGHT; i++)
{
//left
mvprintw(yGridToReal(y)+i+1, xGridToReal(x), CELL_YAXE);
//right
mvprintw(yGridToReal(y)+i+1,
xGridToReal(x)+CELL_WIDTH+1, CELL_YAXE);
}
//Turn color off
if (attroff(COLOR_PAIR(color)) == ERR) return -1;
return 0;
}
/*!
\brief Prints a \p length \p color long line at the \p y coordinate \
starting at \p start on the x-axis
\param[in] start Starting point on the x-axis
\param[in] y Y coordinate
\param[in] len Line length
\param[in] color The color to be used
\return -1 on error, 0 on succes
*/
int gpc_line(int start, int y, int len, int color)
{
if (start < 0 || y < 0 || len < 0)
return -1;
//Turn color on
if (attron(COLOR_PAIR(color)) == ERR) return -1;
for (int i=start; i<len; i++)
if (gp_cell(i, y) == -1) return -1;
//Turn color off
if (attroff(COLOR_PAIR(color)) == ERR) return -1;
return 0;
}
/*!
\brief Prints a \p len long \p color row at the \p x coordinate starting at \
\p start on the y-axis
\param[in] x X coordinate
\param[in] start Starting point on the y-axis
\param[in] len Row length
\param[in] color The color to be used
\return -1 on error, 0 on succes
*/
int gpc_row(int x, int start, int len, int color)
{
if (start < 0 || x < 0 || len < 0)
return -1;
//Turn color on
if (attron(COLOR_PAIR(color)) == ERR) return -1;
for (int i=start; i<len; i++)
if (gp_cell(x, i) == -1) return -1;
//Turn color off
if (attroff(COLOR_PAIR(color)) == ERR) return -1;
return 0;
}
/******************************************************************************/
/****************************** CELL MANAGEMENT *******************************/
/******************************************************************************/
/*!
\brief Prints \p content to a (\p x , \p y ) cell
\param[in] x X coordinate
\param[in] y Y coordinate
\param[in] content What to be print
\return -1 on error, 0 on succes
\note \p content length has to be smaller or egal to CELL_SIZE \
otherwhise it will be truncated
*/
int gp_cellPrint(int x, int y, char* content)
{
if (x < 0 || y < 0 || content == NULL)
return -1;
int len = strLen(content);
int istr = 0;
for (int iy=yGridToReal(y)+1; iy<CELL_HEIGHT+yGridToReal(y)+1; iy++)
{
for (int ix=xGridToReal(x)+1;
ix<CELL_WIDTH+xGridToReal(x)+1; ix++)
{
if (istr < len)
mvprintw(iy, ix, "%c", content[istr++]);
}
}
return 0;
}
/*!
\brief Erases cell content
\param[in] x X coordinate
\param[in] y Y coordinate
\return -1 on error, 0 on succes
*/
int gp_clearCell(int x, int y)
{
if (x < 0 || y < 0)
return -1;
//Through x-axis
for (int iy=yGridToReal(y)+1; iy<CELL_HEIGHT+yGridToReal(y)+1; iy++)
{
//Through y-axis
for (int ix=xGridToReal(x)+1;
ix<CELL_WIDTH+xGridToReal(x)+1; ix++)
{
mvprintw(iy, ix, " ");
}
}
return 0;
}
/******************************************************************************/
/******************************* COLOR HANDLING *******************************/
/******************************************************************************/
/*!
\brief Init color pairs to be used later
\return -1 on error, 0 on succes
*/
int init_colors()
{
if (start_color() == ERR) return -1;
if (init_pair(CLR_YLW, COLOR_YELLOW, COLOR_BLACK) == ERR) return -1;
if (init_pair(CLR_BLU, COLOR_BLUE, COLOR_BLACK) == ERR) return -1;
if (init_pair(CLR_RED, COLOR_RED, COLOR_BLACK) == ERR) return -1;
if (init_pair(CLR_GRN, COLOR_GREEN, COLOR_BLACK) == ERR) return -1;
if (init_pair(CLR_WHT, COLOR_WHITE, COLOR_BLACK) == ERR) return -1;
if (init_color(COLOR_ORANGE, 1000, 500, 0) == ERR) return -1;
if (init_pair(CLR_ORG, COLOR_ORANGE, COLOR_BLACK) == ERR) return -1;
if (init_pair(CLR_VLT, COLOR_MAGENTA, COLOR_BLACK) == ERR) return -1;
if (init_color(COLOR_PINK, 1000, 411, 705) == ERR) return -1;
if (init_pair(CLR_PNK, COLOR_PINK, COLOR_BLACK) == ERR) return -1;
if (init_pair(CLR_CYN, COLOR_CYAN, COLOR_BLACK) == ERR) return -1;
return 0;
}