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