]> Shamusworld >> Repos - virtualjaguar/blob - src/gui.cpp
e1bf5c1e15c450344d0d7c532a65f75b8a8f905d
[virtualjaguar] / src / gui.cpp
1 //
2 // GUI.CPP
3 //
4 // Graphical User Interface support
5 // by James L. Hammons
6 //
7
8 #include <dirent.h>
9 #include <SDL.h>
10 #include <string>
11 #include <vector>
12 #include <algorithm>
13 #include "types.h"
14 #include "tom.h"
15 #include "video.h"
16 #include "font1.h"
17 #include "gui.h"
18
19 using namespace std;                                                            // For STL stuff
20
21 // Private function prototypes
22
23
24 void InitGUI(void)
25 {
26 }
27
28 void GUIDone(void)
29 {
30 }
31
32 //
33 // Draw text at the given x/y coordinates. Can invert text as well.
34 //
35 void DrawString(int16 * screen, uint32 x, uint32 y, bool invert, const char * text, ...)
36 {
37         char string[4096];
38         va_list arg;
39
40         va_start(arg, text);
41         vsprintf(string, text, arg);
42         va_end(arg);
43
44         uint32 pitch = GetSDLScreenPitch() / 2;                 // Returns pitch in bytes but we need words...
45         uint32 length = strlen(string), address = x + (y * pitch);
46
47         for(uint32 i=0; i<length; i++)
48         {
49                 uint32 fontAddr = (uint32)string[i] * 64;
50
51                 for(uint32 yy=0; yy<8; yy++)
52                 {
53                         for(uint32 xx=0; xx<8; xx++)
54                         {
55                                 if ((font1[fontAddr] && !invert) || (!font1[fontAddr] && invert))
56                                         *(screen + address + xx + (yy * pitch)) = 0xFE00;
57                                 fontAddr++;
58                         }
59                 }
60
61                 address += 8;
62         }
63 }
64
65 //
66 // Very very crude GUI file selector
67 //
68 bool UserSelectFile(char * path, char * filename)
69 {
70         extern int16 * backbuffer;
71         vector<string> fileList;
72
73         // Read in the candidate files from the directory pointed to by "path"
74
75         DIR * dp = opendir(path);
76         dirent * de;
77
78         while ((de = readdir(dp)) != NULL)
79         {
80                 char * ext = strrchr(de->d_name, '.');
81
82                 if (ext != NULL)
83                         if (stricmp(ext, ".zip") == 0 || stricmp(ext, ".jag") == 0)
84                                 fileList.push_back(string(de->d_name));
85         }
86
87         closedir(dp);
88
89         // Main GUI selection loop
90
91         uint32 cursor = 0, startFile = 0;
92
93         if (fileList.size() > 1)        // Only go GUI if more than one possibility!
94         {
95                 sort(fileList.begin(), fileList.end());
96
97                 bool done = false;
98                 uint32 limit = (fileList.size() > 24 ? 24 : fileList.size());
99                 SDL_Event event;
100
101                 while (!done)
102                 {
103                         while (SDL_PollEvent(&event))
104                         {
105                                 // Draw the GUI...
106                                 memset(backbuffer, 0x11, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
107
108                                 for(uint32 i=0; i<limit; i++)
109                                 {
110                                         bool invert = (cursor == i ? true : false);
111                                         // Clip our strings to guarantee that they fit on the screen...
112                                         string s = fileList[startFile + i];
113                                         if (s.length() > 38)
114                                                 s[38] = 0;
115                                         DrawString(backbuffer, 0, i*8, invert, " %s ", s.c_str());
116                                 }
117
118                                 RenderBackbuffer();
119
120                                 if (event.type == SDL_KEYDOWN)
121                                 {
122                                         SDLKey key = event.key.keysym.sym;
123
124                                         if (key == SDLK_DOWN)
125                                         {
126                                                 if (cursor != limit - 1)        // Cursor is within its window
127                                                         cursor++;
128                                                 else                                            // Otherwise, scroll the window...
129                                                 {
130                                                         if (cursor + startFile != fileList.size() - 1)
131                                                                 startFile++;
132                                                 }
133                                         }
134                                         if (key == SDLK_UP)
135                                         {
136                                                 if (cursor != 0)
137                                                         cursor--;
138                                                 else
139                                                 {
140                                                         if (startFile != 0)
141                                                                 startFile--;
142                                                 }
143                                         }
144                                         if (key == SDLK_PAGEDOWN)
145                                         {
146                                         }
147                                         if (key == SDLK_PAGEUP)
148                                         {
149                                         }
150                                         if (key == SDLK_RETURN)
151                                                 done = true;
152                                         if (key == SDLK_ESCAPE)
153                                         {
154                                                 WriteLog("GUI: Aborting VJ by user request.\n");
155                                                 return false;                                           // Bail out!
156                                         }
157                                         if (key >= SDLK_a && key <= SDLK_z)
158                                         {
159                                                 // Advance cursor to filename with first letter pressed...
160                                                 uint8 which = (key - SDLK_a) + 65;      // Convert key to A-Z char
161
162                                                 for(uint32 i=0; i<fileList.size(); i++)
163                                                 {
164                                                         if ((fileList[i][0] & 0xDF) == which)
165                                                         {
166                                                                 cursor = i - startFile;
167                                                                 if (i > startFile + limit - 1)
168                                                                         startFile = i - limit + 1,
169                                                                         cursor = limit - 1;
170                                                                 if (i < startFile)
171                                                                         startFile = i,
172                                                                         cursor = 0;
173                                                                 break;
174                                                         }
175                                                 }
176                                         }
177                                 }
178                         }
179                 }
180         }
181
182         strcpy(filename, path);
183
184         if (strlen(path) > 0)
185                 if (path[strlen(path) - 1] != '/')
186                         strcat(filename, "/");
187
188         strcat(filename, fileList[startFile + cursor].c_str());
189
190         return true;
191 }