]> Shamusworld >> Repos - apple2/blob - src/gui/gui.cpp
e79372bfc27246f24ced138498e45d390791922c
[apple2] / src / gui / gui.cpp
1 //
2 // gui.cpp
3 //
4 // Graphical User Interface support
5 // by James Hammons
6 // © 2014 Underground Software
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  02/03/2006  Created this file
13 // JLH  03/13/2006  Added functions to allow shutting down GUI externally
14 // JLH  03/22/2006  Finalized basic multiple window support
15 // JLH  03/03/2014  Refactored GUI to use SDL 2, more modern approach as well
16 //
17 // STILL TO DO:
18 //
19 // - Memory leak on quitting with a window active [DONE]
20 // - Multiple window handling [DONE]
21 //
22
23 #include "gui.h"
24 #include "apple2.h"
25 #include "applevideo.h"
26 #include "diskselector.h"
27 #include "log.h"
28 #include "video.h"
29
30 // Icons, in GIMP "C" format
31 #include "gfx/icon-selection.c"
32 #include "gfx/disk-icon.c"
33 #include "gfx/power-off-icon.c"
34 #include "gfx/power-on-icon.c"
35 #include "gfx/disk-swap-icon.c"
36 #include "gfx/disk-door-open.c"
37 #include "gfx/disk-door-closed.c"
38 #include "gfx/save-state-icon.c"
39 #include "gfx/load-state-icon.c"
40 #include "gfx/config-icon.c"
41
42
43 // Okay, this is ugly but works and I can't think of any better way to handle
44 // this. So what we do when we pass the GIMP bitmaps into a function is pass
45 // them as a (void *) and then cast them as type (Bitmap *) in order to use
46 // them. Yes, it's ugly. Come up with something better!
47
48 struct Bitmap {
49         unsigned int width;
50         unsigned int height;
51         unsigned int bytesPerPixel;                                     // 3:RGB, 4:RGBA
52         unsigned char pixelData[];
53 };
54
55
56 const char numeralOne[(7 * 7) + 1] =
57         "  @@   "
58         " @@@   "
59         "@@@@   "
60         "  @@   "
61         "  @@   "
62         "  @@   "
63         "@@@@@@ ";
64
65 const char numeralTwo[(7 * 7) + 1] =
66         " @@@@@ "
67         "@@   @@"
68         "    @@@"
69         "  @@@@ "
70         " @@@   "
71         "@@     "
72         "@@@@@@@";
73
74 const char ejectIcon[(8 * 7) + 1] =
75         "   @@   "
76         "  @@@@  "
77         " @@@@@@ "
78         "@@@@@@@@"
79         "        "
80         "@@@@@@@@"
81         "@@@@@@@@";
82
83 const char driveLight[(5 * 5) + 1] =
84         " @@@ "
85         "@@@@@"
86         "@@@@@"
87         "@@@@@"
88         " @@@ ";
89
90
91 enum { SBS_SHOWING, SBS_HIDING, SBS_SHOWN, SBS_HIDDEN };
92
93
94 SDL_Texture * GUI::overlay = NULL;
95 SDL_Rect GUI::olDst;
96 int GUI::sidebarState = SBS_HIDDEN;
97 int32_t GUI::dx = 0;
98 int32_t GUI::iconSelected = -1;
99 bool GUI::hasKeyboardFocus = false;
100 bool GUI::powerOnState = true;
101
102 int32_t lastIconSelected = -1;
103 SDL_Texture * iconSelection = NULL;
104 SDL_Texture * diskIcon = NULL;
105 SDL_Texture * disk1Icon = NULL;
106 SDL_Texture * disk2Icon = NULL;
107 SDL_Texture * powerOnIcon = NULL;
108 SDL_Texture * powerOffIcon = NULL;
109 SDL_Texture * diskSwapIcon = NULL;
110 SDL_Texture * stateSaveIcon = NULL;
111 SDL_Texture * stateLoadIcon = NULL;
112 SDL_Texture * configIcon = NULL;
113 SDL_Texture * doorOpen = NULL;
114 SDL_Texture * doorClosed = NULL;
115 uint32_t texturePointer[128 * 380];
116 const char iconHelp[7][80] = { "Turn emulated Apple off/on",
117         "Insert floppy image into drive #1", "Insert floppy image into drive #2",
118         "Swap disks", "Save emulator state", "Load emulator state",
119         "Configure Apple2" };
120 bool disk1EjectHovered = false;
121 bool disk2EjectHovered = false;
122
123
124 #define SIDEBAR_X_POS  (VIRTUAL_SCREEN_WIDTH - 80)
125
126
127 GUI::GUI(void)
128 {
129 }
130
131
132 GUI::~GUI(void)
133 {
134 }
135
136
137 void GUI::Init(SDL_Renderer * renderer)
138 {
139         overlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
140                 SDL_TEXTUREACCESS_TARGET, 128, 380);
141
142         if (!overlay)
143         {
144                 WriteLog("GUI: Could not create overlay!\n");
145                 return;
146         }
147
148         if (SDL_SetTextureBlendMode(overlay, SDL_BLENDMODE_BLEND) == -1)
149                 WriteLog("GUI: Could not set blend mode for overlay.\n");
150
151         for(uint32_t i=0; i<128*380; i++)
152                 texturePointer[i] = 0xB0A000A0;
153
154         SDL_UpdateTexture(overlay, NULL, texturePointer, 128 * sizeof(Uint32));
155
156         olDst.x = VIRTUAL_SCREEN_WIDTH;
157         olDst.y = 2;
158         olDst.w = 128;
159         olDst.h = 380;
160
161         iconSelection  = CreateTexture(renderer, &icon_selection);
162         diskIcon       = CreateTexture(renderer, &disk_icon);
163         doorOpen       = CreateTexture(renderer, &door_open);
164         doorClosed     = CreateTexture(renderer, &door_closed);
165         disk1Icon      = CreateTexture(renderer, &disk_icon);
166         disk2Icon      = CreateTexture(renderer, &disk_icon);
167         powerOffIcon   = CreateTexture(renderer, &power_off);
168         powerOnIcon    = CreateTexture(renderer, &power_on);
169         diskSwapIcon   = CreateTexture(renderer, &disk_swap);
170         stateSaveIcon  = CreateTexture(renderer, &save_state);
171         stateLoadIcon  = CreateTexture(renderer, &load_state);
172         configIcon     = CreateTexture(renderer, &config);
173
174         // Set up drive icons in their current states
175 //      AssembleDriveIcon(renderer, 0);
176 //      AssembleDriveIcon(renderer, 1);
177
178         if (SDL_SetRenderTarget(renderer, overlay) < 0)
179         {
180                 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
181         }
182         else
183         {
184                 DrawSidebarIcons(renderer);
185                 // Set render target back to default
186                 SDL_SetRenderTarget(renderer, NULL);
187         }
188
189         DiskSelector::Init(renderer);
190 //      DiskSelector::showWindow = true;
191
192         WriteLog("GUI: Successfully initialized.\n");
193 }
194
195
196 SDL_Texture * GUI::CreateTexture(SDL_Renderer * renderer, const void * source)
197 {
198         Bitmap * bitmap = (Bitmap *)source;
199         SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
200 //              SDL_TEXTUREACCESS_STATIC, bitmap->width, bitmap->height);
201                 SDL_TEXTUREACCESS_TARGET, bitmap->width, bitmap->height);
202         SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
203         SDL_UpdateTexture(texture, NULL, (Uint32 *)bitmap->pixelData,
204                 bitmap->width * sizeof(Uint32));
205
206         return texture;
207 }
208
209
210 void GUI::MouseDown(int32_t x, int32_t y, uint32_t buttons)
211 {
212         DiskSelector::MouseDown(x, y, buttons);
213
214         if (sidebarState != SBS_SHOWN)
215                 return;
216
217         switch (iconSelected)
218         {
219         // Power
220         case 0:
221                 powerOnState = !powerOnState;
222                 SetPowerState();
223
224                 if (!powerOnState)
225                         SpawnMessage("*** POWER OFF ***");
226
227                 break;
228         // Disk #1
229         case 1:
230                 SpawnMessage("*** DISK #1 ***");
231
232                 if (disk1EjectHovered && !floppyDrive.IsEmpty(0))
233                 {
234                         floppyDrive.EjectImage(0);
235                         SpawnMessage("*** DISK #1 EJECTED ***");
236                 }
237
238                 if (!disk1EjectHovered)
239                 {
240                         // Load the disk selector
241                         DiskSelector::ShowWindow(0);
242                 }
243
244                 break;
245         // Disk #2
246         case 2:
247                 SpawnMessage("*** DISK #2 ***");
248
249                 if (disk2EjectHovered && !floppyDrive.IsEmpty(1))
250                 {
251                         floppyDrive.EjectImage(1);
252                         SpawnMessage("*** DISK #2 EJECTED ***");
253                 }
254
255                 if (!disk2EjectHovered)
256                 {
257                         // Load the disk selector
258                         DiskSelector::ShowWindow(1);
259                 }
260
261                 break;
262         // Swap disks
263         case 3:
264                 floppyDrive.SwapImages();
265                 SpawnMessage("*** DISKS SWAPPED ***");
266                 break;
267         // Save state
268         case 4:
269                 SpawnMessage("*** SAVE STATE ***");
270                 break;
271         // Load state
272         case 5:
273                 SpawnMessage("*** LOAD STATE ***");
274                 break;
275         // Configuration
276         case 6:
277                 SpawnMessage("*** CONFIGURATION ***");
278                 break;
279         }
280 }
281
282
283 void GUI::MouseUp(int32_t x, int32_t y, uint32_t buttons)
284 {
285         DiskSelector::MouseUp(x, y, buttons);
286 }
287
288
289 void GUI::MouseMove(int32_t x, int32_t y, uint32_t buttons)
290 {
291         DiskSelector::MouseMove(x, y, buttons);
292
293         if (sidebarState != SBS_SHOWN)
294         {
295                 iconSelected = -1;
296
297                 if (x > SIDEBAR_X_POS)
298                 {
299 //printf("GUI: sidebar showing (x = %i)...\n", x);
300                         sidebarState = SBS_SHOWING;
301                         dx = -8;
302                 }
303                 else
304                 {
305 //printf("GUI: sidebar hiding[1] (x = %i)...\n", x);
306                         sidebarState = SBS_HIDING;
307                         dx = 8;
308                 }
309         }
310         else
311         {
312                 if (x < SIDEBAR_X_POS)
313                 {
314                         iconSelected = lastIconSelected = -1;
315                         HandleIconSelection(sdlRenderer);
316 //printf("GUI: sidebar hiding[2] (x = %i)...\n", x);
317                         sidebarState = SBS_HIDING;
318                         dx = 8;
319                 }
320                 // We're in the right zone, and the sidebar is shown, so let's select
321                 // something!
322                 else
323                 {
324                         if (y < 4 || y > 383)
325                         {
326                                 iconSelected = -1;
327                         }
328                         else
329                                 iconSelected = (y - 4) / 54;
330
331                         // It's y+2 because the sidebar sits at (SIDEBAR_X_POS, 2)
332                         disk1EjectHovered = ((x >= (SIDEBAR_X_POS + 24 + 29))
333                                 && (x < (SIDEBAR_X_POS + 24 + 29 + 8))
334                                 && (y >= (63 + 31 + 2))
335                                 && (y < (63 + 31 + 2 + 7)) ? true : false);
336
337                         disk2EjectHovered = ((x >= (SIDEBAR_X_POS + 24 + 29))
338                                 && (x < (SIDEBAR_X_POS + 24 + 29 + 8))
339                                 && (y >= (117 + 31 + 2))
340                                 && (y < (117 + 31 + 2 + 7)) ? true : false);
341
342                         if (iconSelected != lastIconSelected)
343                         {
344                                 HandleIconSelection(sdlRenderer);
345                                 lastIconSelected = iconSelected;
346
347                                 if ((iconSelected >= 0) && (iconSelected <= 6))
348                                         SpawnMessage("%s", iconHelp[iconSelected]);
349
350                                 // Show what's in the selected drive
351                                 if (iconSelected >= 1 && iconSelected <= 2)
352                                 {
353                                         if (!floppyDrive.IsEmpty(iconSelected - 1))
354                                                 SpawnMessage("\"%s\"", floppyDrive.ImageName(iconSelected - 1));
355                                 }
356                         }
357                 }
358         }
359 }
360
361
362 void GUI::HandleIconSelection(SDL_Renderer * renderer)
363 {
364         // Set up drive icons in their current states
365         AssembleDriveIcon(renderer, 0);
366         AssembleDriveIcon(renderer, 1);
367
368         // Reload the background...
369         SDL_UpdateTexture(overlay, NULL, texturePointer, 128 * sizeof(Uint32));
370
371         if (SDL_SetRenderTarget(renderer, overlay) < 0)
372         {
373                 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
374                 return;
375         }
376
377         // Draw the icon selector, if an icon is selected
378         if (iconSelected >= 0)
379         {
380                 SDL_Rect dst;// = { 54, 54, 24 - 7, 2 };
381                 dst.w = dst.h = 54, dst.x = 24 - 7, dst.y = 2 + (iconSelected * 54);
382                 SDL_RenderCopy(renderer, iconSelection, NULL, &dst);
383         }
384
385         DrawSidebarIcons(renderer);
386
387         // Set render target back to default
388         SDL_SetRenderTarget(renderer, NULL);
389 }
390
391
392 void GUI::AssembleDriveIcon(SDL_Renderer * renderer, int driveNumber)
393 {
394         SDL_Texture * drive[2] = { disk1Icon, disk2Icon };
395         const char * number[2] = { numeralOne, numeralTwo };
396
397         if (SDL_SetRenderTarget(renderer, drive[driveNumber]) < 0)
398         {
399                 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
400                 return;
401         }
402
403         SDL_RenderClear(renderer);
404         SDL_RenderCopy(renderer, diskIcon, NULL, NULL);
405
406         // Drive door @ (16, 7)
407         SDL_Rect dst;
408         dst.w = 8, dst.h = 10, dst.x = 16, dst.y = 7;
409         SDL_RenderCopy(renderer, (floppyDrive.IsEmpty(driveNumber) ?
410                 doorOpen : doorClosed), NULL, &dst);
411
412         // Numeral @ (30, 20)
413         DrawCharArray(renderer, number[driveNumber], 30, 20, 7, 7, 0xD0, 0xE0, 0xF0);
414         DrawDriveLight(renderer, driveNumber);
415         DrawEjectButton(renderer, driveNumber);
416
417         // Set render target back to default
418         SDL_SetRenderTarget(renderer, NULL);
419 }
420
421
422 void GUI::DrawEjectButton(SDL_Renderer * renderer, int driveNumber)
423 {
424         if (floppyDrive.IsEmpty(driveNumber))
425                 return;
426
427         uint8_t r = 0x00, g = 0xAA, b = 0x00;
428
429         if ((driveNumber == 0 && disk1EjectHovered)
430                 || (driveNumber == 1 && disk2EjectHovered))
431                 r = 0x20, g = 0xFF, b = 0x20;
432
433         DrawCharArray(renderer, ejectIcon, 29, 31, 8, 7, r, g, b);
434 }
435
436
437 void GUI::DrawDriveLight(SDL_Renderer * renderer, int driveNumber)
438 {
439         int lightState = floppyDrive.DriveLightStatus(driveNumber);
440         int r = 0x77, g = 0x00, b = 0x00;
441
442         if (lightState == DLS_READ)
443                 r = 0x20, g = 0xFF, b = 0x20;
444         else if (lightState == DLS_WRITE)
445                 r = 0xFF, g = 0x30, b = 0x30;
446
447         // Drive light @ (8, 21)
448         DrawCharArray(renderer, driveLight, 8, 21, 5, 5, r, g, b);
449 }
450
451
452 void GUI::DrawCharArray(SDL_Renderer * renderer, const char * array, int x,
453         int y, int w, int h, int r, int g, int b)
454 {
455         SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
456
457         for(int j=0; j<h; j++)
458         {
459                 for(int i=0; i<w; i++)
460                 {
461                         if (array[(j * w) + i] != ' ')
462                                 SDL_RenderDrawPoint(renderer, x + i, y + j);
463                 }
464         }
465
466         SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
467 }
468
469
470 void GUI::HandleGUIState(void)
471 {
472         olDst.x += dx;
473
474         if (olDst.x < SIDEBAR_X_POS && sidebarState == SBS_SHOWING)
475         {
476                 olDst.x = SIDEBAR_X_POS;
477                 sidebarState = SBS_SHOWN;
478                 dx = 0;
479         }
480         else if (olDst.x > VIRTUAL_SCREEN_WIDTH && sidebarState == SBS_HIDING)
481         {
482                 olDst.x = VIRTUAL_SCREEN_WIDTH;
483                 sidebarState = SBS_HIDDEN;
484                 dx = 0;
485         }
486 }
487
488
489 void GUI::DrawSidebarIcons(SDL_Renderer * renderer)
490 {
491         SDL_Texture * icons[7] = { powerOnIcon, disk1Icon, disk2Icon, diskSwapIcon,
492                 stateSaveIcon, stateLoadIcon, configIcon };
493
494         icons[0] = (powerOnState ? powerOnIcon : powerOffIcon);
495
496         SDL_Rect dst;
497         dst.w = dst.h = 40, dst.x = 24, dst.y = 2 + 7;
498
499         for(int i=0; i<7; i++)
500         {
501                 SDL_RenderCopy(renderer, icons[i], NULL, &dst);
502                 dst.y += 54;
503         }
504 }
505
506
507 void GUI::Render(SDL_Renderer * renderer)
508 {
509         if (!overlay)
510                 return;
511
512         HandleGUIState();
513
514         if (sidebarState != SBS_HIDDEN)
515                 HandleIconSelection(renderer);
516
517         SDL_RenderCopy(renderer, overlay, NULL, &olDst);
518
519         // Hmm.
520         DiskSelector::Render(renderer);
521 }
522
523
524 /*
525 GUI Considerations:
526
527 screen is 560 x 384
528
529 cut into 7 pieces give ~54 pix per piece
530 So, let's try 40x40 icons, and see if that's good enough...
531 Selection is 54x54.
532
533 drive proportions: 1.62 : 1
534
535 Icon order:
536
537 +-----+
538 |     |
539 |Power|
540 |     |
541 +-----+
542
543 +-----+
544 |     |
545 |Disk1|
546 |    ^| <-- eject button
547 +-----+
548
549 +-----+
550 |     |
551 |Disk2|
552 |    ^|
553 +-----+
554
555 +-----+
556 |     |
557 |Swap |
558 |     |
559 +-----+
560
561 +-----+
562 |     |
563 |Confg|
564 |     |
565 +-----+
566
567 maybe state save/load
568
569 */
570