-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisplayer.h
466 lines (414 loc) · 14.7 KB
/
displayer.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#ifndef __DISPLAYER_H
#define __DISPLAYER_H
#pragma once
/*
DISPLAYER HEADER FILE
Help the main source file to run smoothly for the program and the user
Contains:
- font color
- font size
- coordinate to display the menu and data
- close cursor
*/
// mirin
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <fstream>
#include "dynamics.h"
#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70
// # Initialize the console (terminal) location
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
using std::cout;
using std::cin;
using std::endl;
// # A function to change the color of the font
// font color
void Color(int color){
// set the color
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
// menu displayer
void gotoxy(int x, int y){
// set the cursor position in x direction
CursorPosition.X = x;
// set the cursor position in y direction
CursorPosition.Y = y;
// detemine the cursor position
SetConsoleCursorPosition(console, CursorPosition);
}
// setfont for the program
void setfont(){
// declare font
CONSOLE_FONT_INFOEX font;
font.cbSize = sizeof(font);
font.nFont = 0;
font.dwFontSize.X = 0;
font.dwFontSize.Y = 24;
// font family
font.FontFamily = FF_DONTCARE;
// font weight
font.FontWeight = FW_NORMAL;
// type of font in the font family
wcscpy(font.FaceName, L"Consolas");
// set the font according to the settings
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &font);
}
// set cursor on/off
void ShowConsoleCursor(bool showFlag){
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = showFlag;
SetConsoleCursorInfo(out, &cursorInfo);
}
// menu items object
struct MenuItems {
// menu string
std::string choice;
// menu function
void (*callback)();
};
// question items object
struct questionItems {
std::string question[3];
std::string answer[3];
int type;
int size;
};
/**
* @brief A function to render arrow menu
*
* @param x Accepts coord-x start
* @param y Accepts coord-y start
* @param menuSize The max number of menu list
* @param menus A struct that contains menu option and callback Function
* @param menu A struct that contains menu option and callback
* @param menuTitle Menu Title
* @param isBack Boolean for determining is previous screen available
*/
// menu function to display menu and call function
void Exit();
bool Output(std::fstream &);
void inputData(int,questionItems, std::string);
double calcQuestion(int,double[3],double[3]);
// void inputMenu();
// double calcQuestion();
void showMenu(int menuSize, MenuItems menus[], std::string menuTitle = "", bool isBack = false)
{
// const location to display menu
const int xStart = 5, yStart = 4;
// menu item location in array object
int menu_item = 0;
// location of x in y direction
int x = yStart;
while(true) {
// display cursor
gotoxy(2, x); Color(4); std::cout << "\x1a"; Color(3);
// display title for menu items
gotoxy(xStart, yStart - 2); std::cout << menuTitle << std::endl;
// loop to display menu items
for (int i = 0; i < menuSize; i++)
{
gotoxy(xStart, yStart + i);
std::cout << menus[i].choice << std::endl;
}
// check if there is another page before the displaying page
// if there is, loop will break to go to previous page
// otherwise, loop will break and run exit function to exit the program
gotoxy(xStart, yStart + menuSize);
std::cout << (isBack ? "Back" : "Exit") << std::endl;
// display instructions
gotoxy(5, 15); std::cout << "\x18 - Up" << std::endl;
gotoxy(5, 16); std::cout << "\x19 - Down" << std::endl;
gotoxy(5, 17); std::cout << "[ENTER] - Select" << std::endl;
gotoxy(5, 18); std::cout << "[ESC] - Back" << std::endl;
// gettting input key
system("pause>nul");
// put the cursor down by 1 until the lowest choice of menu
if(GetAsyncKeyState(VK_DOWN) < 0 && x != yStart + menuSize) //down button pressed
{
gotoxy(2, x); std::cout << " ";
x++;
menu_item++;
continue;
}
// put the cursor up by 1 until the highest choice of menu
else if(GetAsyncKeyState(VK_UP) < 0 && x != yStart) //up button pressed
{
gotoxy(2, x); std::cout << " ";
x--;
menu_item--;
continue;
}
else if(GetAsyncKeyState(VK_RETURN) < 0) // Enter key pressed
{
// check if the selected menu is the last choice 'back'
if (menu_item == menuSize) {
if (isBack) {
system("cls"); break; // if yes, break the loop and go to prev page
} else {
Exit(); // if there is no prev page, exit function will be called
}
} else {
menus[menu_item].callback(); // call the function according to the choice
system("cls");
}
}
else if(GetAsyncKeyState(VK_ESCAPE) < 0)
{
if (isBack) { // if 'back' button has been chosen, break the loop and go to prev page
system("cls");
break;
} else {
Exit(); // if there is no prev page, exit function will be called
}
}
}
}
void ShowMenu(std::string menuTitle, std::string menuQuestion, std::string inputFile, questionItems Item){
system("cls");
// const location to display menu
const int xStart = 5, yStart = 4;
// menu item location in array object
int menu_item = 0;
// location of x in y direction
int x = yStart;
int menuSize = 2;
while(true) {
gotoxy(2,x); Color(4); std::cout << "\x1a"; Color(3);
// display title for menu items
gotoxy(xStart, yStart - 2); std::cout << menuTitle << " - " << menuQuestion << std::endl;
gotoxy(xStart, yStart); std::cout << "Enter data by typing" << endl;
gotoxy(xStart, yStart + 1); std::cout << "Enter data by input file " << inputFile << endl;
// check if there is another page before the displaying page
// if there is, loop will break to go to previous page
// otherwise, loop will break and run exit function to exit the program
gotoxy(xStart, yStart + menuSize); std::cout << "Back" << endl;
// display instructions
gotoxy(5, 15); std::cout << "\x18 - Up" << std::endl;
gotoxy(5, 16); std::cout << "\x19 - Down" << std::endl;
gotoxy(5, 17); std::cout << "[ENTER] - Select" << std::endl;
gotoxy(5, 18); std::cout << "[ESC] - Back" << std::endl;
// gettting input key
system("pause>nul");
// put the cursor down by 1 until the lowest choice of menu
if(GetAsyncKeyState(VK_DOWN) < 0 && x != yStart + menuSize) //down button pressed
{
gotoxy(2, x); std::cout << " ";
x++;
menu_item++;
continue;
}
// put the cursor up by 1 until the highest choice of menu
else if(GetAsyncKeyState(VK_UP) < 0 && x != yStart) //up button pressed
{
gotoxy(2, x); std::cout << " ";
x--;
menu_item--;
continue;
}
else if(GetAsyncKeyState(VK_RETURN) < 0) // Enter key pressed
{
// check if the selected menu is the last choice 'back'
if (menu_item == menuSize) {
system("cls");
break;
} else {
inputData(menu_item,Item,inputFile);
system("cls");
}
}
else if(GetAsyncKeyState(VK_ESCAPE) < 0)
{
system("cls");
break;
}
}
}
void inputData(int menu_item, questionItems Item, std::string inputFile)
{
bool output;
std::ifstream nFile;
std::fstream oFile;
formula Formula;
double input[5];
int i, space = 2;
int type = Item.type;
double answer3[3];
double answer;
// const location to display menu
const int xStart = 5, yStart = 4;
if(menu_item == 0)
{
output = Output(oFile);
system("cls");
for(i = 0; i < Item.size; i++){
gotoxy(xStart,yStart + i); cout << Item.question[i]; ShowConsoleCursor(true); cin >> input[i]; ShowConsoleCursor(false);
}
cin.ignore();
answer = calcQuestion(type,input,answer3);
if(answer == -3){
for(int j = 0; j < 3; j++){
gotoxy(xStart,yStart + i + j + 1); cout << Item.answer[j] << answer3[j] << endl;
}
} else {
gotoxy(xStart,yStart + i + 1); cout << Item.answer[0] << answer << endl;
}
if(output)
{
if(answer == -3){
for(int j = 0; j < 3; j++){
oFile << answer3[j] << endl;
}
} else {
oFile << answer << endl;
}
oFile.close();
}
space += Item.size;
} else if(menu_item == 1){
gotoxy(xStart, yStart - 2); cout << "Obtaining the file.." << endl;
nFile.open(inputFile);
output = Output(oFile);
system("cls");
if(nFile){
while(!nFile.eof()){
if(type <= 3){
nFile >> input[0] >> input[1] >> input[2];
}
else {
nFile >> input[0] >> input[1];
}
for(i = 0; i < Item.size; i++){
gotoxy(xStart,yStart + i); cout << Item.question[i] << input[i] << endl;
}
answer = calcQuestion(type,input,answer3);
if(answer == -3){
for(int j = 0; j < 3; j++){
gotoxy(xStart,yStart + i + j + 1); cout << Item.answer[j] << answer3[j] << endl;
}
} else {
gotoxy(xStart,yStart + i + 1); cout << Item.answer[0] << answer << endl;
}
if(output){
if(answer == -3){
for(int j = 0; j < 3; j++){
oFile << answer3[j] << endl;
}
} else {
oFile << answer << endl;
}
}
space += Item.size;
}
oFile.close();
nFile.close();
} else {
gotoxy(xStart,yStart); cout << "Error opening the file!" << endl;
}
}
gotoxy(xStart, yStart + i + space); system("pause");
system("cls");
}
double calcQuestion(int type, double input[3], double answer3[3])
{
formula Formula;
switch(type)
{
case 0: Formula.P_timeOfFlight(input[0],input[1],input[2]); return Formula.showTime();
case 1: Formula.P_range(input[0],input[1],input[2]); return Formula.showRange();
case 2: Formula.P_maxHeight(input[0],input[1],input[2]); return Formula.showMaxHeight();
case 3: Formula.P_finalVelocity(input[0],input[1],input[2]);
answer3[0] = Formula.showVfx();
answer3[1] = Formula.showVfy();
answer3[2] = Formula.showVelocity(); return -3;
case 4: Formula.F_Time_Velocity(input[0],input[1]); return Formula.showTime();
case 5: Formula.F_Time_Height(input[0],input[1]); return Formula.showTime();
case 6: Formula.F_Height_Velocity(input[0],input[1]); return Formula.showHeight();
case 7: Formula.F_Height_Time(input[0],input[1]); return Formula.showHeight();
case 8: Formula.F_Velocity_Height(input[0],input[1]); return Formula.showVelocity();
case 9: Formula.F_Velocity_Time(input[0],input[1]); return Formula.showVelocity();
case 10: Formula.H_TimeOfFlight(input[0],input[1]); return Formula.showTime();
case 11: Formula.H_Range(input[0],input[1]); return Formula.showRange();
}
return 404;
}
// exit
void Exit()
{
system("cls");
// ask comfirmation
gotoxy(5,3); std::cout << "Exit the program?" << std::endl;
gotoxy(5,5); std::cout << "[ENTER] - Yes" << std::endl;
gotoxy(5,6); std::cout << "Any Key - No" << std::endl;
system("pause>nul");
// exit program if the user comfirmed to exit
if(GetAsyncKeyState(VK_RETURN)){
system("cls");
exit(0);
}
system("cls");
}
// output file
// return a boolean which is true or false to the function
#include <string>
bool Output(std::fstream &oFile)
{
// char output;
system("cls");
// const location to display menu
const int xStart = 5, yStart = 4;
// menu item location in array object
int menu_item = 0;
// location of x in y direction
int x = yStart;
int menuSize = 1;
std::string path;
while(true) {
gotoxy(2,x); Color(4); std::cout << "\x1a"; Color(3);
// display title for menu items
gotoxy(xStart, yStart - 2); std::cout << "Do you want to put the results into an output file?" << std::endl;
gotoxy(xStart, yStart); std::cout << "Yes" << endl;
gotoxy(xStart, yStart + 1); std::cout << "No" << endl;
// display instructions
gotoxy(5, 15); std::cout << "\x18 - Up" << std::endl;
gotoxy(5, 16); std::cout << "\x19 - Down" << std::endl;
gotoxy(5, 17); std::cout << "[ENTER] - Select" << std::endl;
// gettting input key
system("pause>nul");
// put the cursor down by 1 until the lowest choice of menu
if(GetAsyncKeyState(VK_DOWN) < 0 && x != yStart + menuSize) //down button pressed
{
gotoxy(2, x); std::cout << " ";
x++;
menu_item++;
continue;
}
// put the cursor up by 1 until the highest choice of menu
else if(GetAsyncKeyState(VK_UP) < 0 && x != yStart) //up button pressed
{
gotoxy(2, x); std::cout << " ";
x--;
menu_item--;
continue;
}
else if(GetAsyncKeyState(VK_RETURN) < 0) // Enter key pressed
{
if(menu_item == 0){
system("cls");
ShowConsoleCursor(true);
gotoxy(xStart,yStart); cout << "Enter the name of file including file extension (.txt): "; getline(cin,path);
ShowConsoleCursor(false);
oFile.open(path, std::fstream::app);
return true;
} else if(menu_item == 1){
return false;
}
}
}
}
#endif