]> Shamusworld >> Repos - apple2/blob - src/gui/gui.cpp
Added icon selector.
[apple2] / src / gui / gui.cpp
1 //
2 // GUI.CPP
3 //
4 // Graphical User Interface support
5 // by James Hammons
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  02/03/2006  Created this file
12 // JLH  03/13/2006  Added functions to allow shutting down GUI externally
13 // JLH  03/22/2006  Finalized basic multiple window support
14 //
15 // STILL TO DO:
16 //
17 // - Memory leak on quitting with a window active [DONE]
18 // - Multiple window handling [DONE]
19 //
20
21 #include "gui.h"
22 #include "menu.h"                                                               // Element class methods are pulled in here...
23 #include "window.h"
24 #include "button.h"
25 #include "text.h"
26 #include "diskwindow.h"
27 #include "video.h"
28 #include "apple2.h"
29
30 // Debug support
31 //#define DEBUG_MAIN_LOOP
32
33 // New main screen buffering
34 // This works, but the colors are rendered incorrectly. Also, it seems that there's
35 // fullscreen blitting still going on--dragging the disk is fast at first but then
36 // gets painfully slow. Not sure what's going on there.
37 //#define USE_NEW_MAINBUFFERING
38
39 //#ifdef DEBUG_MAIN_LOOP
40 #include "log.h"
41 //#endif
42
43 /*
44 Work flow: Draw floppy drive.
45 If disk in drive, MO shows eject graphic, otherwise show load graphic.
46 If hit 'new blank image':
47         If disk in drive, ask if want to save if modified
48         else, load it
49 If hit 'swap disks', swap disks.
50 */
51
52
53 GUI::GUI(SDL_Surface * surface): menuItem(new MenuItems())
54 {
55         Element::SetScreen(surface);
56 //      windowList.push_back(new Menu());
57
58 // Create drive windows, and config windows here...
59         windowList.push_back(new Window(30, 30, 200, 100));
60         windowList.push_back(new Window(30, 140, 200, 100));
61         windowList.push_back(new Button(30, 250, "Click!"));
62         windowList.push_back(new Text(30, 20, floppyDrive.GetImageName(0)));
63         windowList.push_back(new Text(30, 130, floppyDrive.GetImageName(1)));
64         windowList.push_back(new DiskWindow(&floppyDrive, 240, 20));
65 }
66
67
68 GUI::~GUI()
69 {
70         // Clean up menuItem, if any
71
72         if (menuItem)
73                 delete menuItem;
74
75         // Clean up the rest
76
77         for(std::list<Element *>::iterator i=windowList.begin(); i!=windowList.end(); i++)
78                 if (*i)
79                         delete *i;
80 }
81
82
83 void GUI::AddMenuTitle(const char * title)
84 {
85         menuItem->title = title;
86         menuItem->item.clear();
87 }
88
89
90 void GUI::AddMenuItem(const char * item, Element * (* a)(void)/*= NULL*/, SDL_Scancode k/*= SDLK_UNKNOWN*/)
91 {
92         menuItem->item.push_back(NameAction(item, a, k));
93 }
94
95
96 void GUI::CommitItemsToMenu(void)
97 {
98 //We could just do a simple check here to see if more than one item is in the list,
99 //and if so fail. Make it so you build the menu first before allowing any other action. [DONE]
100
101 //Right now, we just silently fail...
102         if (windowList.size() > 1)
103         {
104                 WriteLog("GUI: Can't find menu--more than one item in windowList!\n");
105                 return;
106         }
107
108         ((Menu *)(*windowList.begin()))->Add(*menuItem);
109 }
110
111
112 void GUI::Run(void)
113 {
114         exitGUI = false;
115         showMouse = true;
116         SDL_Event event;
117         std::list<Element *>::iterator i;
118
119 // Not sure what replaces this in SDL2...
120 //      SDL_EnableKeyRepeat(150, 75);
121
122         // Also: Need to pick up backbuffer (for those windows that have them)
123         //       BEFORE drawing...
124
125         // Initial update... [Now handled correctly in the constructor]
126         // Uh, still needed here, though... Only makes sense that it should
127         for(i=windowList.begin(); i!=windowList.end(); i++)
128                 (*i)->Draw();
129
130 #ifndef USE_NEW_MAINBUFFERING
131         RenderScreenBuffer();
132 #else
133         FlipMainScreen();
134 #endif
135
136         // Main loop
137         while (!exitGUI)
138         {
139 //              if (SDL_PollEvent(&event))
140                 if (SDL_WaitEvent(&event))
141                 {
142 #ifdef DEBUG_MAIN_LOOP
143 WriteLog("An event was found!");
144 #endif
145                         if (event.type == SDL_USEREVENT)
146                         {
147 #ifdef DEBUG_MAIN_LOOP
148 WriteLog(" -- SDL_USEREVENT\n");
149 #endif
150 //Mebbe add another user event for screen refresh? Why not!
151                                 if (event.user.code == WINDOW_CLOSE)
152                                 {
153                                         for(i=windowList.begin(); i!=windowList.end(); i++)
154                                         {
155                                                 if (*i == (Element *)event.user.data1)
156                                                 {
157                                                         delete *i;
158                                                         windowList.erase(i);
159                                                         break;
160                                                 }
161                                         }
162                                 }
163                                 else if (event.user.code == MENU_ITEM_CHOSEN)
164                                 {
165                                         // Confused? Let me enlighten... What we're doing here is casting
166                                         // data1 as a pointer to a function which returns a Element pointer and
167                                         // which takes no parameters (the "(Element *(*)(void))" part), then
168                                         // derefencing it (the "*" in front of that) in order to call the
169                                         // function that it points to. Clear as mud? Yeah, I hate function
170                                         // pointers too, but what else are you gonna do?
171                                         Element * window = (*(Element *(*)(void))event.user.data1)();
172
173                                         if (window)
174                                                 windowList.push_back(window);
175
176                                         while (SDL_PollEvent(&event));  // Flush the event queue...
177
178                                         event.type = SDL_MOUSEMOTION;
179                                         int mx, my;
180                                         SDL_GetMouseState(&mx, &my);
181                                         event.motion.x = mx, event.motion.y = my;
182                                     SDL_PushEvent(&event);                      // & update mouse position...!
183
184                                         oldMouse.x = mouse.x, oldMouse.y = mouse.y;
185                                         mouse.x = mx, mouse.y = my;             // This prevents "mouse flash"...
186                                 }
187 //There's a *small* problem with the following approach--if a window and a bunch of
188 //child widgets send this message, we'll get a bunch of unnecessary refresh events...
189 //This could be controlled by having the main window refresh itself intelligently...
190
191 //What we could do instead is set a variable in Element and check it after the fact
192 //to see whether or not a refresh is needed.
193 //[This is what we do now.]
194
195 //Dirty rectangle is also possible...
196                                 else if (event.user.code == SCREEN_REFRESH_NEEDED)
197 #ifndef USE_NEW_MAINBUFFERING
198                                         RenderScreenBuffer();
199 #else
200                                         FlipMainScreen();
201 #endif
202                         }
203 //Not sure what to do here for SDL2...
204 #if 0
205                         else if (event.type == SDL_ACTIVEEVENT)
206                         {
207 //Need to do a screen refresh here...
208                                 if (event.active.state == SDL_APPMOUSEFOCUS)
209                                         showMouse = (event.active.gain ? true : false);
210
211 #ifndef USE_NEW_MAINBUFFERING
212                                 RenderScreenBuffer();
213 #else
214                                 FlipMainScreen();
215 #endif
216                         }
217 #endif
218                         else if (event.type == SDL_KEYDOWN)
219                         {
220 #ifdef DEBUG_MAIN_LOOP
221 WriteLog(" -- SDL_KEYDOWN\n");
222 #endif
223                                 if (event.key.keysym.sym == SDLK_F1)
224                                         exitGUI = true;
225
226 //Not sure that this is the right way to handle this...
227 //Probably should only give this to the top level window...
228 //                              for(i=windowList.begin(); i!=windowList.end(); i++)
229 //                                      (*i)->HandleKey(event.key.keysym.sym);
230                                 windowList.back()->HandleKey(event.key.keysym.scancode);
231                         }
232                         else if (event.type == SDL_MOUSEMOTION)
233                         {
234 #ifdef DEBUG_MAIN_LOOP
235 WriteLog(" -- SDL_MOUSEMOTION\n");
236 #endif
237 //This is for tracking a custom mouse cursor, which we're not doing--YET.
238                                 oldMouse.x = mouse.x, oldMouse.y = mouse.y;
239                                 mouse.x = event.motion.x, mouse.y = event.motion.y;
240
241 //Not sure that this is the right way to handle this...
242 //Right now, we should probably only do mouseover for the last item in the list...
243 //And now we do!
244 //Though, it seems to screw other things up. Maybe it IS better to pass it to all windows?
245 //Or maybe to just the ones that aren't completely obscured?
246 //Probably. Right now, a disk's close button that should be obscured by one sitting on
247 //top of it gets redrawn. Not good. !!! FIX !!!
248                                 for(i=windowList.begin(); i!=windowList.end(); i++)
249                                         (*i)->HandleMouseMove(mouse.x, mouse.y);
250 //                              windowList.back()->HandleMouseMove(mouse.x, mouse.y);
251                         }
252                         else if (event.type == SDL_MOUSEBUTTONDOWN)
253                         {
254 #ifdef DEBUG_MAIN_LOOP
255 WriteLog(" -- SDL_MOUSEBUTTONDOWN\n");
256 #endif
257 //Not sure that this is the right way to handle this...
258 // What we should do here is ensure that whatever has been clicked on gets moved to the
259 // highest priority--in our current data schema that would be the end of the list... !!! FIX !!!
260 //[DONE]
261
262 /*
263
264 We could do the following:
265
266 - Go through list and find which window has been clicked on (if any). If more
267   than one is clicked on, take the one highest in the Z order (closer to the end
268   of the list).
269
270 - If item is highest in Z order, pass click through to window and exit.
271
272 - Otherwise, restore backing store on each window in reverse order.
273
274 - Remove item clicked on from the list. Put removed item at the end of the list.
275
276 - Go through list and pass click through to each window in the list. Also do a
277   blit to backing store and a Draw() for each window.
278
279 Could also do a check (if not clicked on highest Z window) to see which windows
280 it overlaps and just do restore/redraw for those that overlap. To wit:
281
282 - Create new list containing only those windows that overlap the clicking on window.
283
284 - Go through list and do a blit to backing store and a Draw() for each window.
285
286 - Go through list and pass click through to each window in the list.
287
288 */
289
290 #if 0
291 #if 0
292                                 for(i=windowList.begin(); i!=windowList.end(); i++)
293                                         (*i)->HandleMouseButton(event.button.x, event.button.y, true);
294 #else
295 // We use the 1st algorithm here, since it's simpler. If we need to, we can optimize
296 // to the 2nd...
297
298                                 // Walk backward through the list and see if a window was hit.
299                                 // This will automagically return us the window with the highest Z.
300
301                                 std::list<Element *>::reverse_iterator ri;
302                                 std::list<Element *>::iterator hit;// = windowList.end();
303
304                                 for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
305                                 {
306                                         if ((*ri)->Inside(event.button.x, event.button.y))
307                                         {
308                                                 // Here's a bit of STL weirdness: Converting from a reverse
309                                                 // iterator to a regular iterator requires backing the iterator
310                                                 // up a position after grabbing it's base() OR going forward
311                                                 // one position with the reverse iterator before grabbing base().
312                                                 // Ugly, but it gets the job done...
313                                                 hit = (++ri).base();
314                                                 // Put it back where we found it, so the tests following this
315                                                 // don't fail...
316                                                 ri--;
317                                                 break;
318                                         }
319                                 }
320
321                                 // If we hit the highest in the list, then pass the event through
322                                 // to the window for handling. if we hit no windows, then pass the
323                                 // event to all windows. Otherwise, we need to shuffle windows.
324
325 //NOTE: We need to pass the click to all windows regardless of whether they're topmost or not...
326                                 if (ri == windowList.rbegin())
327                                 {
328                                         for(i=windowList.begin(); i!=windowList.end(); i++)
329                                                 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
330                                 }
331                                 else if (ri == windowList.rend())
332                                 {
333                                         for(i=windowList.begin(); i!=windowList.end(); i++)
334                                                 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
335                                 }
336                                 else
337                                 {
338 // - Otherwise, restore backing store on each window in reverse order.
339                                         for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
340                                                 (*ri)->RestoreScreenFromBackstore();
341                                         // At this point, the screen has been restored...
342
343 // - Remove item clicked on from the list. Put removed item at the end of the list.
344                                         windowList.push_back(*hit);
345                                         windowList.erase(hit);
346 // - Go through list and pass click through to each window in the list. Also do a
347 //  blit to backing store and a Draw() for each window.
348                                         for(i=windowList.begin(); i!= windowList.end(); i++)
349                                         {
350                                                 // Grab bg into backstore
351                                                 (*i)->SaveScreenToBackstore();
352                                                 // Pass click
353                                                 (*i)->HandleMouseButton(event.button.x, event.button.y, true);
354                                                 // Draw?
355                                                 (*i)->Draw();
356                                         }
357                                 }
358 #endif
359 #endif
360 /*
361 A slightly different way to handle this would be to loop through all windows, compare
362 all those above it to see if they obscure it; if so then subdivide it's update rectangle
363 to eliminate drawing the parts that aren't shown. The beauty of this approach is that
364 you don't have to care what order the windows are drawn in and you don't need to worry
365 about the order of restoring the backing store.
366
367 You *do* still need to determine the Z-order of the windows, in order to get the subdivisions
368 correct, but that's not too terrible.
369
370 Also, when doing a window drag, the coverage lists for all windows have to be regenerated.
371 */
372                                 std::list<Element *>::reverse_iterator ri;
373                                 bool movedWindow = false;
374
375                                 for(ri=windowList.rbegin(); ri!=windowList.rend(); ri++)
376                                 {
377                                         if ((*ri)->Inside(event.button.x, event.button.y))
378                                         {
379                                                 // Remove item clicked on from the list & put removed item at the
380                                                 // end of the list, thus putting the window at the top of the Z
381                                                 // order. But IFF window is not already topmost!
382                                                 if (ri != windowList.rbegin())
383                                                 {
384                                                         windowList.push_back(*ri);
385                                                         // Here's a bit of STL weirdness: Converting from a reverse
386                                                         // iterator to a regular iterator requires backing the iterator
387                                                         // up a position after grabbing it's base() OR going forward
388                                                         // one position with the reverse iterator before grabbing base().
389                                                         // Ugly, but it get the job done...
390                                                         windowList.erase((++ri).base());
391                                                         movedWindow = true;
392                                                 }
393
394                                                 break;
395                                         }
396                                 }
397
398 //Small problem here: we should only pass the *hit* to the topmost window and pass
399 //*misses* to everyone else... Otherwise, you can have overlapping draggable windows
400 //and be able to drag both by clicking on a point that intersects both...
401 //(though that may be an interesting way to handle things!)
402 //The thing is that you want to do it on purpose (like with a special grouping widget)
403 //instead of by accident. So, !!! FIX !!!
404                                 // Pass the click on to all windows
405 //                              for(i=windowList.begin(); i!=windowList.end(); i++)
406 //                                      (*i)->HandleMouseButton(event.button.x, event.button.y, true);
407                                 windowList.back()->HandleMouseButton(event.button.x, event.button.y, true);
408
409 //                              // & bail if nothing changed...
410                                 if (movedWindow)
411 //                                      return;
412 {
413                                 // Check for overlap/build coverage lists [O((n^2)/2) algorithm!]
414 //One way to optimize this would be to only reset coverage lists from the point in
415 //the Z order where the previous window was.
416                                 for(i=windowList.begin(); i!=windowList.end(); i++)
417                                 {
418 //One other little quirk: Probably need to clear the backing store as well!
419 //Not sure...
420                                         (*i)->ResetCoverageList();
421
422                                         // This looks odd, but it's just a consequence of iterator weirdness.
423                                         // Otherwise we could just stick a j+1 in the for loop below. :-P
424                                         std::list<Element *>::iterator j = i;
425                                         j++;
426
427                                         for(; j!=windowList.end(); j++)
428                                                 (*i)->AdjustCoverageList((*j)->GetExtents());
429
430 //                                      (*i)->HandleMouseButton(event.button.x, event.button.y, true);
431                                         (*i)->Draw();
432                                 }
433 }
434                         }
435                         else if (event.type == SDL_MOUSEBUTTONUP)
436                         {
437 #ifdef DEBUG_MAIN_LOOP
438 WriteLog(" -- SDL_MOUSEBUTTONUP\n");
439 #endif
440 //Not sure that this is the right way to handle this...
441                                 for(i=windowList.begin(); i!=windowList.end(); i++)
442                                         (*i)->HandleMouseButton(event.button.x, event.button.y, false);
443 //I think we should only do topmost here...
444 //Or should we???
445 //                              windowList.back()->HandleMouseButton(event.button.x, event.button.y, false);
446                         }
447 #ifdef DEBUG_MAIN_LOOP
448 else
449         WriteLog(" -- Unknown event\n");
450 #endif
451
452                         if (Element::ScreenNeedsRefreshing())
453                         {
454 #ifndef USE_NEW_MAINBUFFERING
455 #ifdef DEBUG_MAIN_LOOP
456 WriteLog("Screen refresh called!\n");
457 #endif
458                                 RenderScreenBuffer();
459                                 Element::ScreenWasRefreshed();
460 #else
461                                 FlipMainScreen();
462                                 Element::ScreenWasRefreshed();
463 #endif
464                         }
465                 }
466 //hm. Works, but slows things way down.
467 //Now we use WaitEvents() instead. Yay!
468 //SDL_Delay(10);
469         }
470
471 // Not sure what to do for this in SDL 2...
472 //      SDL_EnableKeyRepeat(0, 0);
473 //      return false;
474 }
475
476
477 void GUI::Stop(void)
478 {
479         exitGUI = true;
480 }
481
482
483
484 //
485 // NEW GUI STARTS HERE
486 //
487
488
489 // Okay, this is ugly but works and I can't think of any better way to handle
490 // this. So what we do when we pass the GIMP bitmaps into a function is pass
491 // them as a (void *) and then cast them as type (Bitmap *) in order to use
492 // them. Yes, it's ugly. Come up with something better!
493
494 struct Bitmap {
495         unsigned int width;
496         unsigned int height;
497         unsigned int bytesPerPixel;                                     // 3:RGB, 4:RGBA
498         unsigned char pixelData[];
499 };
500
501
502 // Icons, in GIMP "C" format
503 #include "gfx/icon-selection.c"
504 #include "gfx/disk-1-icon.c"
505 #include "gfx/disk-2-icon.c"
506 #include "gfx/power-off-icon.c"
507 #include "gfx/power-on-icon.c"
508
509
510 enum { SBS_SHOWING, SBS_HIDING, SBS_SHOWN, SBS_HIDDEN };
511
512
513 SDL_Texture * GUI2::overlay = NULL;
514 //SDL_Rect GUI2::olSrc;
515 SDL_Rect GUI2::olDst;
516 //bool GUI2::sidebarOut = false;
517 int GUI2::sidebarState = SBS_HIDDEN;
518 int32_t GUI2::dx = 0;
519 int32_t GUI2::iconSelected = -1;
520 int32_t lastIconSelected = -1;
521 SDL_Texture * iconSelection = NULL;
522 SDL_Texture * disk1Icon = NULL;
523 SDL_Texture * disk2Icon = NULL;
524 SDL_Texture * powerOnIcon = NULL;
525 SDL_Texture * powerOffIcon = NULL;
526 uint32_t texturePointer[128 * 380];
527
528
529 GUI2::GUI2(void)
530 {
531 }
532
533
534 GUI2::~GUI2(void)
535 {
536 }
537
538
539 void GUI2::Init(SDL_Renderer * renderer)
540 {
541 //      overlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
542 //              SDL_TEXTUREACCESS_STREAMING, 128, 256);
543         overlay = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
544                 SDL_TEXTUREACCESS_TARGET, 128, 380);
545
546         if (!overlay)
547         {
548                 WriteLog("GUI: Could not create overlay!\n");
549                 return;
550         }
551
552         if (SDL_SetTextureBlendMode(overlay, SDL_BLENDMODE_BLEND) == -1)
553                 WriteLog("GUI: Could not set blend mode for overlay.\n");
554
555 //      uint32_t * texturePointer = (uint32_t *)scrBuffer;
556
557         for(uint32_t i=0; i<128*380; i++)
558                 texturePointer[i] = 0xA0A000A0;
559
560         SDL_UpdateTexture(overlay, NULL, texturePointer, 128 * sizeof(Uint32));
561
562 //      olSrc.x = olSrc.y = 0;
563 //      olSrc.w = 128;
564 //      olSrc.h = 380;
565         olDst.x = VIRTUAL_SCREEN_WIDTH;
566         olDst.y = 2;
567         olDst.w = 128;
568         olDst.h = 380;
569
570         iconSelection = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
571                 SDL_TEXTUREACCESS_STATIC, 54, 54);
572         disk1Icon = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
573                 SDL_TEXTUREACCESS_STATIC, 40, 40);
574         disk2Icon = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
575                 SDL_TEXTUREACCESS_STATIC, 40, 40);
576         powerOffIcon = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
577                 SDL_TEXTUREACCESS_STATIC, 40, 40);
578         powerOnIcon = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
579                 SDL_TEXTUREACCESS_STATIC, 40, 40);
580         SDL_SetTextureBlendMode(iconSelection, SDL_BLENDMODE_BLEND);
581         SDL_SetTextureBlendMode(disk1Icon, SDL_BLENDMODE_BLEND);
582         SDL_SetTextureBlendMode(disk2Icon, SDL_BLENDMODE_BLEND);
583         SDL_SetTextureBlendMode(powerOffIcon, SDL_BLENDMODE_BLEND);
584         SDL_SetTextureBlendMode(powerOnIcon, SDL_BLENDMODE_BLEND);
585         Bitmap * bm1 = (Bitmap *)((void *)&icon_selection);
586         SDL_UpdateTexture(iconSelection, NULL, (Uint32 *)bm1->pixelData, 54 * sizeof(Uint32));
587         bm1 = (Bitmap *)((void *)&disk_1);
588         SDL_UpdateTexture(disk1Icon, NULL, (Uint32 *)bm1->pixelData, 40 * sizeof(Uint32));
589         bm1 = (Bitmap *)((void *)&disk_2);
590         SDL_UpdateTexture(disk2Icon, NULL, (Uint32 *)bm1->pixelData, 40 * sizeof(Uint32));
591         bm1 = (Bitmap *)((void *)&power_off);
592         SDL_UpdateTexture(powerOffIcon, NULL, (Uint32 *)bm1->pixelData, 40 * sizeof(Uint32));
593         bm1 = (Bitmap *)((void *)&power_on);
594         SDL_UpdateTexture(powerOnIcon, NULL, (Uint32 *)bm1->pixelData, 40 * sizeof(Uint32));
595
596         if (SDL_SetRenderTarget(renderer, overlay) < 0)
597         {
598                 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
599         }
600         else
601         {
602                 SDL_Texture * icons[7] = { powerOnIcon, disk1Icon, disk2Icon, powerOffIcon, powerOffIcon, powerOffIcon, powerOffIcon };
603                 SDL_Rect dst;
604                 dst.w = dst.h = 40;
605                 dst.x = 24;
606                 dst.y = 2 + 7;
607
608                 for(int i=0; i<7; i++)
609                 {
610                         SDL_RenderCopy(renderer, icons[i], NULL, &dst);
611                         dst.y += 54;
612                 }
613
614                 // Set render target back to default
615                 SDL_SetRenderTarget(renderer, NULL);
616         }
617
618         WriteLog("GUI: Successfully initialized.\n");
619 }
620
621
622 void GUI2::MouseDown(int32_t x, int32_t y, uint32_t buttons)
623 {
624 }
625
626
627 void GUI2::MouseUp(int32_t x, int32_t y, uint32_t buttons)
628 {
629 }
630
631
632 void GUI2::MouseMove(int32_t x, int32_t y, uint32_t buttons)
633 {
634         if (sidebarState != SBS_SHOWN)
635         {
636                 iconSelected = -1;
637
638                 if (x > (VIRTUAL_SCREEN_WIDTH - 100))
639                 {
640 //printf("GUI: sidebar showing (x = %i)...\n", x);
641                         sidebarState = SBS_SHOWING;
642                         dx = -8;
643                 }
644                 else
645                 {
646 //printf("GUI: sidebar hiding[1] (x = %i)...\n", x);
647                         sidebarState = SBS_HIDING;
648                         dx = 8;
649                 }
650         }
651         else
652         {
653                 if (x < (VIRTUAL_SCREEN_WIDTH - 100))
654                 {
655                         iconSelected = -1;
656                         lastIconSelected = -1;
657                         HandleIconSelection(sdlRenderer);
658 //printf("GUI: sidebar hiding[2] (x = %i)...\n", x);
659 //                      sidebarOut = false;
660                         sidebarState = SBS_HIDING;
661                         dx = 8;
662                 }
663                 // We're in the right zone, and the sidebar is shown, so let's select
664                 // something!
665                 else
666                 {
667                         if (y < 3 || y > 382)
668                         {
669                                 iconSelected = -1;
670                         }
671                         else
672                                 iconSelected = (y - 2) / 54;
673
674                         if (iconSelected != lastIconSelected)
675                         {
676                                 HandleIconSelection(sdlRenderer);
677                                 lastIconSelected = iconSelected;
678                         }
679                 }
680         }
681 }
682
683
684 void GUI2::HandleIconSelection(SDL_Renderer * renderer)
685 {
686         // Reload the background...
687         SDL_UpdateTexture(overlay, NULL, texturePointer, 128 * sizeof(Uint32));
688
689         if (SDL_SetRenderTarget(renderer, overlay) < 0)
690         {
691                 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
692                 return;
693         }
694
695         SDL_Rect dst;// = { 54, 54, 24 - 7, 2 };
696         dst.w = dst.h = 54;
697         dst.x = 24 - 7;
698         dst.y = 2;
699
700         if (iconSelected >= 0)
701         {
702                 dst.y += iconSelected * 54;
703                 SDL_RenderCopy(renderer, iconSelection, NULL, &dst);
704         }
705
706 #if 1
707         DrawSidebarIcons(renderer);
708 #else
709         SDL_Texture * icons[7] = { powerOnIcon, disk1Icon, disk2Icon, powerOffIcon, powerOffIcon, powerOffIcon, powerOffIcon };
710         dst.w = dst.h = 40;
711         dst.x = 24;
712         dst.y = 2 + 7;
713
714         for(int i=0; i<7; i++)
715         {
716                 SDL_RenderCopy(renderer, icons[i], NULL, &dst);
717                 dst.y += 54;
718         }
719 #endif
720
721         // Set render target back to default
722         SDL_SetRenderTarget(renderer, NULL);
723 }
724
725
726 void GUI2::HandleGUIState(void)
727 {
728         olDst.x += dx;
729
730         if (olDst.x < (VIRTUAL_SCREEN_WIDTH - 100) && sidebarState == SBS_SHOWING)
731         {
732                 olDst.x = VIRTUAL_SCREEN_WIDTH - 100;
733 //              sidebarOut = true;
734                 sidebarState = SBS_SHOWN;
735                 dx = 0;
736         }
737         else if (olDst.x > VIRTUAL_SCREEN_WIDTH && sidebarState == SBS_HIDING)
738         {
739                 olDst.x = VIRTUAL_SCREEN_WIDTH;
740                 sidebarState = SBS_HIDDEN;
741                 dx = 0;
742         }
743 }
744
745
746 void GUI2::DrawSidebarIcons(SDL_Renderer * renderer)
747 {
748         SDL_Texture * icons[7] = { powerOnIcon, disk1Icon, disk2Icon, powerOffIcon,
749                 powerOffIcon, powerOffIcon, powerOffIcon };
750
751         SDL_Rect dst;
752         dst.w = dst.h = 40;
753         dst.x = 24;
754         dst.y = 2 + 7;
755
756         for(int i=0; i<7; i++)
757         {
758                 SDL_RenderCopy(renderer, icons[i], NULL, &dst);
759                 dst.y += 54;
760         }
761 }
762
763
764 void GUI2::Render(SDL_Renderer * renderer)
765 {
766         if (!overlay)
767                 return;
768
769         HandleGUIState();
770         SDL_RenderCopy(renderer, overlay, NULL, &olDst);
771 }
772
773
774 /*
775 GUI Considerations:
776
777 screen is 560 x 384
778
779 cut into 7 pieces give ~54 pix per piece
780 So, let's try 40x40 icons, and see if that's good enough...
781 Selection is 54x54.
782
783 drive proportions: 1.62 : 1
784
785 Icon order:
786
787 +-----+
788 |     |
789 |Power|
790 |     |
791 +-----+
792
793 +-----+
794 |     |
795 |Disk1|
796 |    ^| <-- eject button
797 +-----+
798
799 +-----+
800 |     |
801 |Disk2|
802 |    ^|
803 +-----+
804
805 +-----+
806 |     |
807 |Swap |
808 |     |
809 +-----+
810
811 +-----+
812 |     |
813 |Confg|
814 |     |
815 +-----+
816
817 maybe state save/load
818
819 */
820