]> Shamusworld >> Repos - apple2/blob - src/gui/draggablewindow2.cpp
Converted to SDL 2, added fullscreen toggle.
[apple2] / src / gui / draggablewindow2.cpp
1 //
2 // DRAGGABLEWINDOW2.CPP
3 //
4 // Graphical User Interface draggable window class
5 // by James L. Hammons
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  03/17/2006  Created this file
12 // JLH  03/17/2006  Added clipping against parent extents
13 //
14 // STILL TO DO:
15 //
16 // - Check for parent's extents and clip movement against those extents [DONE]
17 //
18
19 #include "draggablewindow2.h"
20 #include "guimisc.h"                                                            // Various support functions
21 #include <algorithm>
22
23 // Debugging support
24
25 //#define DESTRUCTOR_TESTING
26 #define BACKGROUND_IMG_TEST
27 #define USE_COVERAGE_LISTS
28
29 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
30 #define MASK_R 0xFF000000
31 #define MASK_G 0x00FF0000
32 #define MASK_B 0x0000FF00
33 #define MASK_A 0x000000FF
34 #else
35 #define MASK_R 0x000000FF
36 #define MASK_G 0x0000FF00
37 #define MASK_B 0x00FF0000
38 #define MASK_A 0xFF000000
39 #endif
40
41 //
42 // DraggableWindow class implementation
43 //
44 // NOTE: FG/BG colors are hard-wired
45 //
46
47 DraggableWindow2::DraggableWindow2(uint32_t x/*= 0*/, uint32_t y/*= 0*/, uint32_t w/*= 0*/, uint32_t h/*= 0*/,
48         void (* f)(Element *)/*= NULL*/):
49         Window(x, y, w, h, f), clicked(false)
50 {
51 #ifdef BACKGROUND_IMG_TEST
52         uint16_t imgWidth = (floppyDiskImg[0] << 8) | floppyDiskImg[1];
53         uint16_t imgHeight = (floppyDiskImg[2] << 8) | floppyDiskImg[3];
54         img = SDL_CreateRGBSurfaceFrom(&floppyDiskImg[4], imgWidth, imgHeight, 32, imgWidth * 4,
55                 MASK_R, MASK_G, MASK_B, MASK_A);
56 //      label = SDL_CreateRGBSurface(SDL_SWSURFACE, 16*7, 32, 32,
57 //              MASK_R, MASK_G, MASK_B, MASK_A);
58
59 //Prolly should draw this in the constructor...
60 //Now is! :-D
61         extern char textChar2e[];
62         uint8_t * fontAddr = (uint8_t *)textChar2e + ((128 + 32) * 7 * 8);
63         SetNewFont(Font(fontAddr, 7, 8));
64 //      DrawStringOpaque(label, 0,  0, 0xFF000000, 0xFFFFFFFF, "Ultima III - Boo");
65 //      DrawStringOpaque(label, 0,  8, 0xFF000000, 0xFFFFFFFF, "0123456789012345");
66 //      DrawStringOpaque(label, 0, 16, 0xFF000000, 0xFFFFFFFF, "1234567890123456");
67 //      DrawStringOpaque(label, 0, 24, 0xFF000000, 0xFFFFFFFF, "2345678901234567");
68         DrawStringOpaque(img, 8,  6, 0xFF000000, 0xFFFFFFFF, "Ultima III - Boo");
69         DrawStringOpaque(img, 8, 14, 0xFF000000, 0xFFFFFFFF, "t Disk6789012345");
70         DrawStringOpaque(img, 8, 22, 0xFF000000, 0xFFFFFFFF, "1234567890123456");
71         DrawStringOpaque(img, 6, 30, 0xFF000000, 0xFFFFFFFF, "2345678901234567");
72         RestoreOldFont();
73 #endif
74
75 //      CreateBackstore();
76 //Bleh. We only have to do this here because Window's constructor is doing it too. !!! FIX !!!
77 //Mebbe put this shiaut into Element? What about stuff that doesn't draw itself right away?
78 //Perhaps we should just hit needToRefreshScreen or some such--but will that trigger a redraw?
79         RestoreScreenFromBackstore();
80         Draw(); // Can we do this in the constructor??? Mebbe.
81 }
82
83 DraggableWindow2::~DraggableWindow2()
84 {
85 #ifdef DESTRUCTOR_TESTING
86 printf("Inside ~DraggableWindow2()...\n");
87 #endif
88
89 #ifdef BACKGROUND_IMG_TEST
90         SDL_FreeSurface(img);
91 //      SDL_FreeSurface(label);
92 #endif
93 }
94
95 void DraggableWindow2::HandleMouseMove(uint32_t x, uint32_t y)
96 {
97         if (clicked)
98         {
99 //Need to check whether or not we've run into the extents of the screen... !!! FIX !!!
100 //[DONE]
101                 int32_t newX = x - offset.x;
102                 int32_t newY = y - offset.y;
103                 SDL_Rect clip = GetParentRect();
104
105                 if (newX < clip.x)
106                         newX = clip.x;
107                 else if (newX > (clip.w - extents.w))
108                         newX = clip.w - extents.w;
109
110                 if (newY < clip.y)
111                         newY = clip.y;
112                 else if (newY > (clip.h - extents.h))
113                         newY = clip.h - extents.h;
114
115                 RestoreScreenFromBackstore();
116                 extents.x = newX;
117                 extents.y = newY;
118                 SaveScreenToBackstore();
119 #ifdef USE_COVERAGE_LISTS
120 //If we don't do this, the coverage list doesn't move with the window...!
121                 ResetCoverageList();
122 #endif
123 //              SDL_BlitSurface(screen, &extents, backstore, NULL);
124                 Draw();
125
126                 return;
127         }
128
129         // Handle the items this window contains...
130 //      for(uint32_t i=0; i<list.size(); i++)
131                 // Make coords relative to upper right corner of this window...
132 //              list[i]->HandleMouseMove(x - extents.x, y - extents.y);
133         Window::HandleMouseMove(x, y);
134 }
135
136 void DraggableWindow2::HandleMouseButton(uint32_t x, uint32_t y, bool mouseDown)
137 {
138         clicked = false;
139
140         if (mouseDown && Inside(x, y))
141         {
142                 clicked = true;
143                 offset.x = x - extents.x;
144                 offset.y = y - extents.y;
145         }
146
147         // Handle the items this window contains...
148         for(uint32_t i=0; i<list.size(); i++)
149         {
150                 // Make coords relative to upper right corner of this window...
151                 list[i]->HandleMouseButton(x - extents.x, y - extents.y, mouseDown);
152
153                 if (list[i]->Inside(x - extents.x, y - extents.y))
154                         clicked = false;
155         }
156 }
157
158 void DraggableWindow2::Draw(void)
159 {
160 //NOTE: What we need to do here is render into a surface THEN do the blits from the coverage list. !!! FIX !!!
161 #ifdef USE_COVERAGE_LISTS
162         // These are *always* top level and parentless, so no need to traverse up through
163         // the parent chain...
164         for(std::list<SDL_Rect>::iterator i=coverList.begin(); i!=coverList.end(); i++)
165         {
166                 SDL_Rect src, dst;
167                 src.x = (*i).x - extents.x, src.y = (*i).y - extents.y, src.w = (*i).w, src.h = (*i).h;
168                 dst.x = (*i).x, dst.y = (*i).y;
169                 SDL_BlitSurface(img, &src, screen, &dst);
170         }
171
172 // HUH??!?!? The label should have been drawn into img already!!! !!! FIX !!! [DONE]
173 //This doesn't get clipped at all... !!! FIX !!!
174 //      SDL_Rect src, dst;
175 //      src.x = 0, src.y = 0, src.w = label->w, src.h = label->h;
176 //      dst.x = extents.x + 8, dst.y = extents.y + 6;
177 //      SDL_BlitSurface(label, &src, screen, &dst);
178
179         // Handle the items this window contains...
180         for(uint32_t i=0; i<list.size(); i++)
181                 list[i]->Draw();
182 #else
183         // These are *always* top level and parentless, so no need to traverse up through
184         // the parent chain...
185 //Perhaps we can make these parentable, put the parent traversal in the base class?
186 //Prolly.
187 #ifdef BACKGROUND_IMG_TEST
188         SDL_Rect src, dst;
189         src.x = 0, src.y = 0, src.w = extents.w, src.h = extents.h;
190         dst.x = extents.x, dst.y = extents.y;
191         SDL_BlitSurface(img, &src, screen, &dst);
192
193 //WTF? Unnecessary!
194 //      extern char textChar2e[];
195 //      uint8_t * fontAddr = (uint8_t *)textChar2e + ((128 + 32) * 7 * 8);
196 //      SetNewFont(Font(fontAddr, 7, 8));
197 //      DrawStringOpaque(screen, extents.x + 8, extents.y +  6, 0xFF000000, 0xFFFFFFFF, "Ultima III - Boo");
198 //      DrawStringOpaque(screen, extents.x + 8, extents.y + 14, 0xFF000000, 0xFFFFFFFF, "0123456789012345");
199 //      DrawStringOpaque(screen, extents.x + 8, extents.y + 22, 0xFF000000, 0xFFFFFFFF, "1234567890123456");
200 //      DrawStringOpaque(screen, extents.x + 8, extents.y + 30, 0xFF000000, 0xFFFFFFFF, "2345678901234567");
201 //      RestoreOldFont();
202 #else
203         SDL_FillRect(screen, &extents, bgColor);
204 #endif
205
206         // Handle the items this window contains...
207         for(uint32_t i=0; i<list.size(); i++)
208                 list[i]->Draw();
209 #endif
210
211 //Prolly don't need this since the close button will do this for us...
212         needToRefreshScreen = true;
213 }