Skip to content

Commit c0ba5a2

Browse files
committed
Added gui text input and output and OS switch
Dirent is only partially ported to windows. I created a windows load function that has no filtering of file extensions. The function I wrote for windows should compile and work on linux as well.
1 parent 76222f5 commit c0ba5a2

File tree

3 files changed

+122
-15
lines changed

3 files changed

+122
-15
lines changed

gui.h

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mazeBase.h"
1313
#include <opencv2/core/core.hpp>
1414
#include <opencv2/highgui/highgui.hpp>
15+
#include<string>
1516

1617
using namespace cv;
1718

@@ -21,6 +22,60 @@ struct callbackWrapper
2122
Mat *img;
2223
};
2324

25+
void clearUiInputArea(Mat *img)
26+
{
27+
rectangle(*img,Point(0,MAZE_HEIGHT_PX-PX_PER_UNIT), Point(MAZE_WIDTH_PX,MAZE_HEIGHT_PX), CV_RGB(0,0,0),CV_FILLED);
28+
}
29+
30+
void clearUiPrintArea(Mat *img)
31+
{
32+
rectangle(*img,Point(0,0), Point(MAZE_WIDTH_PX,MAZE_HEIGHT_PX-PX_PER_UNIT), CV_RGB(0,0,0),CV_FILLED);
33+
}
34+
35+
int cvPrint(Mat *img, std::string text)
36+
{
37+
static int lineNum =0;
38+
if(!text.compare(""))
39+
{
40+
lineNum =0;
41+
clearUiPrintArea(img);
42+
}
43+
else if(lineNum++ < GUI_MAXLINE) //if there's space print the line and increment the line number
44+
{
45+
putText(*img, text.c_str(), Point(0,lineNum*PX_PER_UNIT), FONT_HERSHEY_SIMPLEX, 1,
46+
CV_RGB(0,0,255), 2);
47+
}
48+
else
49+
{
50+
return -1; //if the line wasn't printed return -1
51+
}
52+
return lineNum;
53+
}
54+
55+
std::string cvIn(Mat *img)
56+
{
57+
char inputKey;
58+
std::string inputString ("");
59+
60+
while((inputKey = waitKey(100)) != '\r' && cvGetWindowHandle(DISPLAY_WINDOW_NAME))
61+
{
62+
if (inputKey != -1)
63+
{
64+
if (inputKey == '\b')
65+
{
66+
if (inputString.size() > 0)
67+
inputString.resize(inputString.size() - 1);
68+
} else
69+
inputString += inputKey;
70+
}
71+
//redraw text
72+
clearUiInputArea(img);
73+
putText(*img, inputString, UI_INPUT_LOCATION, FONT_HERSHEY_SIMPLEX, 1,
74+
CV_RGB(0,0,255), 2);
75+
imshow(DISPLAY_WINDOW_NAME, *img);
76+
}
77+
return inputString;
78+
}
2479

2580
void redrawMaze(Mat *img, struct baseMapNode startNode[][MAZE_HEIGHT], struct mouseData *mouse)
2681
{
@@ -273,8 +328,6 @@ void mouseCallBackFunc(int event, int x, int y, int flags, void* ptr)
273328
}
274329
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].wallRight = false;
275330
data->startNode[(int)(x/PX_PER_UNIT)][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)].right = &(data->startNode[(int)(x/PX_PER_UNIT)+1][(int)((MAZE_HEIGHT_PX-y)/PX_PER_UNIT)]);
276-
277-
278331
}
279332
else
280333
{

mazeConst.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#define MOUSE_OUTLINE_THICKNESS 9
2323
#define MOUSE_RADIUS 7
2424

25+
#define UI_INPUT_LOCATION Point(0,MAZE_HEIGHT_PX-PX_PER_UNIT/4)
26+
#define GUI_MAXLEN 40
27+
#define GUI_MAXLINE 18
28+
2529
#define TOP 0
2630
#define RIGHT 1
2731
#define BOTTOM 2

mmSim.cpp

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
// Description : Micro mouse simulation software for SFHS robotics students
77
//============================================================================
88

9+
910
#include"mazeBase.h"
1011
#include"mazeConst.h"
1112
#include"gui.h"
1213
#include"studentAi.h"
1314

15+
#define win32 1
16+
17+
#include <dirent.h>
1418
#include<iostream>
1519
#include <string>
16-
#include <dirent.h>
20+
21+
1722

1823
using namespace cv;
1924

@@ -49,6 +54,9 @@ int main()
4954

5055
//create game board
5156
Mat image(MAZE_WIDTH*PX_PER_UNIT, MAZE_HEIGHT*PX_PER_UNIT, CV_8UC3, Scalar(0,0,0));
57+
Mat guiText(MAZE_WIDTH*PX_PER_UNIT, MAZE_HEIGHT*PX_PER_UNIT, CV_8UC3, Scalar(0,0,0));
58+
59+
namedWindow( DISPLAY_WINDOW_NAME, WINDOW_NORMAL);
5260
redrawMaze(&image, start);
5361

5462
//create mouse call back
@@ -66,29 +74,70 @@ int main()
6674
std::cout << "Left click to add a wall"<<endl;
6775
std::cout << "Right click to remove a wall"<<endl;
6876

69-
7077
while((userInput =waitKey(1))!= 'q')
7178
{
72-
if(userInput == 's')
79+
80+
if (!cvGetWindowHandle(DISPLAY_WINDOW_NAME))//if window is closed clean up and exit
81+
{
82+
break;
83+
}
84+
else if (userInput == 's')
7385
{
74-
string input; const char* c_input;
75-
86+
string input;
87+
7688
//save file
7789
cout << "Enter filename to save to (without extension): ";
78-
cin >> input;
90+
cvPrint(&guiText,"Enter filename to save to (without extension): " );
91+
input = cvIn(&guiText);
7992
input.append(".maz");
80-
c_input = input.c_str();
81-
82-
mazeFile = fopen(c_input, "w");
93+
94+
mazeFile = fopen(input.c_str(), "w");
8395
if(mazeFile == NULL)
8496
{
8597
exit(100);
8698
}
8799
saveMaze2File(mazeFile, start);
88100
fclose(mazeFile);
101+
redrawMaze(&image, start);
89102
}
90103
else if(userInput == 'l')
91104
{
105+
#ifdef win32
106+
DIR *local = opendir(".");
107+
struct dirent *dircontents = readdir(local);
108+
int selection;
109+
std::ostringstream buff ("");
110+
111+
do{
112+
buff << telldir(local) << ": " << dircontents->d_name;
113+
cvPrint(&guiText,buff.str());
114+
cout << buff.str() <<endl;
115+
buff.str("");
116+
buff.clear();
117+
dircontents = readdir(local);
118+
}while(dircontents != NULL);
119+
120+
do {
121+
cout << endl << "Select file to load: " << endl;
122+
selection = atoi(cvIn(&guiText).c_str());
123+
rewinddir(local);
124+
seekdir(local, selection - 1);
125+
dircontents = readdir(local);
126+
127+
if(!cvGetWindowHandle(DISPLAY_WINDOW_NAME)) break; //if the user closed the window break
128+
} while (dircontents == NULL || dircontents->d_type != 0);
129+
cvPrint(&guiText, "");
130+
mazeFile = fopen(dircontents->d_name, "r");
131+
if(mazeFile == NULL)
132+
{
133+
exit(101);
134+
}
135+
readMazeFromFile(mazeFile, start);
136+
cout << "Maze loaded" << endl;
137+
138+
fclose(mazeFile);
139+
redrawMaze(&image, start);
140+
#else
92141
struct dirent **dircontents;
93142
string choices[20];
94143
int dirnum, selection;
@@ -104,7 +153,7 @@ int main()
104153
cout << i << ") " << choices[i] << endl;
105154
}
106155
}
107-
156+
108157
cout << endl;
109158

110159
//load file
@@ -124,8 +173,9 @@ int main()
124173

125174
else
126175
cout << "Invalid selection, try another action" << endl;
176+
#endif
127177
}
128-
178+
129179
else if(userInput == 'r')
130180
{
131181
//reset maze area
@@ -146,7 +196,7 @@ int main()
146196
}
147197
cout << "The AI says the mouse has finished the maze!"<<endl;
148198
}
149-
}
150199

200+
}
151201
return 0;
152-
}
202+
}

0 commit comments

Comments
 (0)