4 // Floppy disk selector GUI
6 // © 2014 Underground Software
8 // JLH = James Hammons <jlhamm@acm.org>
11 // --- ---------- ------------------------------------------------------------
12 // JLH 10/13/2013 Created this file
18 #include "diskselector.h"
30 enum { DSS_SHOWING, DSS_HIDING, DSS_SHOWN, DSS_HIDDEN };
37 int diskSelectorState = DSS_HIDDEN;
38 int diskSelected = -1;
39 int lastDiskSelected = -1;
43 // Case insensitve string comparison voodoo
45 struct ci_char_traits : public std::char_traits<char>
47 static bool eq(char c1, char c2) { return toupper(c1) == toupper(c2); }
48 static bool ne(char c1, char c2) { return toupper(c1) != toupper(c2); }
49 static bool lt(char c1, char c2) { return toupper(c1) < toupper(c2); }
50 static int compare(const char * s1, const char * s2, size_t n)
54 if (toupper(*s1) < toupper(*s2)) return -1;
55 if (toupper(*s1) > toupper(*s2)) return 1;
60 static const char * find(const char * s, int n, char a)
62 while (n-- > 0 && toupper(*s) != toupper(a))
70 typedef std::basic_string<char, ci_char_traits> ci_string;
72 // END Case insensitve string comparison voodoo
76 static SDL_Texture * window = NULL;
77 static SDL_Texture * charStamp = NULL;
78 static uint32_t windowPixels[DS_WIDTH * DS_HEIGHT];
79 static uint32_t stamp[FONT_WIDTH * FONT_HEIGHT];
80 bool DiskSelector::showWindow = false;
81 std::vector<ci_string> imageList;
84 void DiskSelector::Init(SDL_Renderer * renderer)
86 window = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
87 SDL_TEXTUREACCESS_TARGET, DS_WIDTH, DS_HEIGHT);
88 charStamp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
89 SDL_TEXTUREACCESS_TARGET, FONT_WIDTH, FONT_HEIGHT);
93 WriteLog("GUI (DiskSelector): Could not create window!\n");
97 if (SDL_SetTextureBlendMode(window, SDL_BLENDMODE_BLEND) == -1)
98 WriteLog("GUI (DiskSelector): Could not set blend mode for window.\n");
100 if (SDL_SetTextureBlendMode(charStamp, SDL_BLENDMODE_BLEND) == -1)
101 WriteLog("GUI (DiskSelector): Could not set blend mode for charStamp.\n");
103 for(uint32_t i=0; i<DS_WIDTH*DS_HEIGHT; i++)
104 windowPixels[i] = 0xEF00FF00;
106 SDL_UpdateTexture(window, NULL, windowPixels, 128 * sizeof(Uint32));
108 DrawFilenames(renderer);
112 void DiskSelector::FindDisks(const char * path)
114 DIR * dir = opendir(settings.disksPath);
118 WriteLog("GUI (DiskSelector)::FindDisks: Could not open directory \"%s\%!\n", settings.disksPath);
125 while ((ent = readdir(dir)) != NULL)
127 if (HasLegalExtension(ent->d_name))
128 imageList.push_back(ci_string(ent->d_name));
132 std::sort(imageList.begin(), imageList.end());
135 std::vector<ci_string>::iterator i;
136 for(i=imageList.begin(); i!=imageList.end(); i++)
137 printf("GUI::DS::Found \"%s\"\n", (*i).c_str());
143 bool DiskSelector::HasLegalExtension(const char * name)
145 const char * ext = strrchr(name, '.');
147 if ((strcasecmp(ext, ".dsk") == 0) || (strcasecmp(ext, ".do") == 0)
148 || (strcasecmp(ext, ".po") == 0) || (strcasecmp(ext, ".nib") == 0))
155 void DiskSelector::DrawFilenames(SDL_Renderer * renderer)
157 if (SDL_SetRenderTarget(renderer, window) < 0)
159 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
163 // 3 columns of 16 chars apiece (with 8X16 font), 18 rows
164 // 3 columns of 18 chars apiece (with 7X12 font), 24 rows
165 // 3 columns of 21 chars apiece (with 6X11 font), 27 rows
167 unsigned int count = 0;
169 while (count < imageList.size())
171 // int currentX = (count / 18) * 17;
172 // int currentY = (count % 18);
173 // int currentX = (count / 24) * 19;
174 // int currentY = (count % 24);
175 int currentX = (count / 27) * 22;
176 int currentY = (count % 27);
178 // for(unsigned int i=0; i<16; i++)
179 // for(unsigned int i=0; i<18; i++)
180 for(unsigned int i=0; i<21; i++)
182 if (i >= imageList[count].length())
185 bool invert = (diskSelected == (int)count ? true : false);
186 DrawCharacter(renderer, currentX + i, currentY, imageList[count][i], invert);
191 // if (count >= (18 * 3))
192 // if (count >= (24 * 3))
193 if (count >= (27 * 3))
197 // Set render target back to default
198 SDL_SetRenderTarget(renderer, NULL);
202 void DiskSelector::DrawCharacter(SDL_Renderer * renderer, int x, int y, uint8_t c,
203 bool invert/*=false*/)
206 // uint32_t pixel = 0xFF7F0000;
207 uint8_t * ptr = (uint8_t *)&font2[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
209 for(int j=0; j<FONT_HEIGHT; j++)
211 for(int i=0; i<FONT_WIDTH; i++)
213 SDL_SetRenderDrawColor(renderer, 0xFF, 0x7F, 0x00, ptr[(j * FONT_WIDTH) + i]);
214 SDL_RenderDrawPoint(renderer, (x * FONT_WIDTH) + i, (y * FONT_HEIGHT) + j);
218 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
220 uint32_t inv = (invert ? 0x000000FF : 0x00000000);
221 uint32_t pixel = 0xFFFFC000; // RRGGBBAA
222 uint8_t * ptr = (uint8_t *)&font10pt[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
224 dst.x = x * FONT_WIDTH, dst.y = y * FONT_HEIGHT, dst.w = FONT_WIDTH, dst.h = FONT_HEIGHT;
226 for(int i=0; i<FONT_WIDTH*FONT_HEIGHT; i++)
227 stamp[i] = (pixel | ptr[i]) ^ inv;
229 SDL_UpdateTexture(charStamp, NULL, stamp, FONT_WIDTH * sizeof(Uint32));
230 SDL_RenderCopy(renderer, charStamp, NULL, &dst);
236 void DiskSelector::()
242 void DiskSelector::ShowWindow(int drive)
250 void DiskSelector::MouseDown(int32_t x, int32_t y, uint32_t buttons)
258 if (diskSelected != -1)
261 sprintf(buffer, "%s/%s", settings.disksPath, &imageList[diskSelected][0]);
262 // floppyDrive.LoadImage(&imageList[diskSelected][0], driveNumber);
263 floppyDrive.LoadImage(buffer, driveNumber);
270 void DiskSelector::MouseUp(int32_t x, int32_t y, uint32_t buttons)
278 #define DS_XPOS ((VIRTUAL_SCREEN_WIDTH - DS_WIDTH) / 2)
279 #define DS_YPOS ((VIRTUAL_SCREEN_HEIGHT - DS_HEIGHT) / 2)
280 void DiskSelector::MouseMove(int32_t x, int32_t y, uint32_t buttons)
285 if (!entered && ((x >= DS_XPOS) && (x <= (DS_XPOS + DS_WIDTH))
286 && (y >= DS_YPOS) && (y <= (DS_YPOS + DS_HEIGHT))))
289 if (entered && ((x < DS_XPOS) || (x > (DS_XPOS + DS_WIDTH))
290 || (y < DS_YPOS) || (y > (DS_YPOS + DS_HEIGHT))))
296 // prevDiskSelected = diskSelected;
297 int xChar = (x - DS_XPOS) / FONT_WIDTH;
298 int yChar = (y - DS_YPOS) / FONT_HEIGHT;
299 // int currentX = (count / 27) * 22;
300 // int currentY = (count % 27);
301 diskSelected = ((xChar / 22) * 27) + yChar;
303 if ((yChar >= 27) || (diskSelected >= (int)imageList.size()))
306 if (diskSelected != lastDiskSelected)
308 HandleSelection(sdlRenderer);
309 lastDiskSelected = diskSelected;
314 void DiskSelector::HandleSelection(SDL_Renderer * renderer)
316 // if (diskSelected == prevDiskSelected)
319 SDL_UpdateTexture(window, NULL, windowPixels, 128 * sizeof(Uint32));
320 DrawFilenames(renderer);
324 void DiskSelector::Render(SDL_Renderer * renderer)
326 if (!(window && showWindow))
329 // HandleSelection(renderer);
332 // dst.x = (VIRTUAL_SCREEN_WIDTH - DS_WIDTH) / 2, dst.y = (VIRTUAL_SCREEN_HEIGHT - DS_HEIGHT) / 2, dst.w = DS_WIDTH, dst.h = DS_HEIGHT;
333 dst.x = DS_XPOS, dst.y = DS_YPOS, dst.w = DS_WIDTH, dst.h = DS_HEIGHT;
335 SDL_RenderCopy(renderer, window, NULL, &dst);