]> Shamusworld >> Repos - apple2/blob - src/gui/element.cpp
Undoing changes (accidentally) committed from r31.
[apple2] / src / gui / element.cpp
1 //
2 // ELEMENT.CPP
3 //
4 // Graphical User Interface base class
5 // by James L. Hammons
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  02/02/2006  Created this file
12 // JLH  02/13/2006  Added backbuffer and rendering functions
13 // JLH  03/02/2006  Moved backbuffer destruction to destructor, added parent
14 //                  corner discovery
15 //
16
17 #include "element.h"
18 #include "guimisc.h"                                                            // Various support functions
19
20 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
21 #define MASK_R 0xFF000000
22 #define MASK_G 0x00FF0000
23 #define MASK_B 0x0000FF00
24 #define MASK_A 0x000000FF
25 #else
26 #define MASK_R 0x000000FF
27 #define MASK_G 0x0000FF00
28 #define MASK_B 0x00FF0000
29 #define MASK_A 0xFF000000
30 #endif
31
32 //#define DEBUG_ELEMENT
33
34 #ifdef DEBUG_ELEMENT
35 #include "log.h"
36 #endif
37
38 // Initialize class variables
39
40 SDL_Surface * Element::screen = NULL;
41 bool Element::needToRefreshScreen = false;
42
43 Element::Element(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
44         Element * parentElement/*= NULL*/):     parent(parentElement), backstore(NULL)
45 {
46         extents.x = x,
47         extents.y = y,
48         extents.w = w,
49         extents.h = h;
50         coverList.push_back(extents);
51 }
52
53 Element::Element(uint32 x, uint32 y, uint32 w, uint32 h,
54         uint8 fgR/*= 0xFF*/, uint8 fgG/*= 0xFF*/, uint8 fgB/*= 0xFF*/, uint8 fgA/*= 0xFF*/,
55         uint8 bgR/*= 0x00*/, uint8 bgG/*= 0x00*/, uint8 bgB/*= 0x00*/, uint8 bgA/*= 0xFF*/,
56         Element * parentElement/*= NULL*/): parent(parentElement), backstore(NULL)
57 {
58         extents.x = x,
59         extents.y = y,
60         extents.w = w,
61         extents.h = h;
62         coverList.push_back(extents);
63
64         // This *should* allow us to store our colors in an endian safe way... :-/
65         uint8 * c = (uint8 *)&fgColor;
66         c[0] = fgR, c[1] = fgG, c[2] = fgB, c[3] = fgA;
67         c = (uint8 *)&bgColor;
68         c[0] = bgR, c[1] = bgG, c[2] = bgB, c[3] = bgA;
69 }
70
71 Element::~Element()
72 {
73         if (backstore)
74         {
75                 RestoreScreenFromBackstore();
76                 SDL_FreeSurface(backstore);
77                 needToRefreshScreen = true;
78         }
79 }
80
81 bool Element::Inside(uint32 x, uint32 y)
82 {
83         return (x >= (uint32)extents.x && x < (uint32)(extents.x + extents.w)
84                 && y >= (uint32)extents.y && y < (uint32)(extents.y + extents.h) ? true : false);
85 }
86
87 //Badly named--!!! FIX !!! [DONE]
88 //SDL_Rect Element::GetParentCorner(void)
89 SDL_Rect Element::GetScreenCoords(void)
90 {
91         SDL_Rect rect;
92         rect.x = extents.x, rect.y = extents.y;
93
94         // First, traverse the parent tree to get the absolute screen address...
95
96         Element * currentParent = parent;
97
98         while (currentParent)
99         {
100                 rect.x += currentParent->extents.x;
101                 rect.y += currentParent->extents.y;
102                 currentParent = currentParent->parent;
103         }
104
105         return rect;
106 }
107
108 #if 1
109 //May use this in the future...
110 SDL_Rect Element::GetParentRect(void)
111 {
112         // If there is no parent, then return the entire screen as the parent's
113         // rectangle.
114
115         SDL_Rect rect;
116         rect.x = 0, rect.y = 0, rect.w = screen->w, rect.h = screen->h;
117
118         if (parent)
119         {
120                 rect.x = parent->extents.x;
121                 rect.y = parent->extents.y;
122                 rect.w = parent->extents.w;
123                 rect.h = parent->extents.h;
124         }
125
126         return rect;
127 }
128 #endif
129
130 SDL_Rect Element::GetExtents(void)
131 {
132         return extents;
133 }
134
135 void Element::CreateBackstore(void)
136 {
137         backstore = SDL_CreateRGBSurface(SDL_SWSURFACE, extents.w, extents.h, 32,
138                 MASK_R, MASK_G, MASK_B, 0x00);
139         SDL_BlitSurface(screen, &extents, backstore, NULL);
140 }
141
142 void Element::RestoreScreenFromBackstore(void)
143 {
144         SDL_Rect r;
145
146         r.x = extents.x;
147         r.y = extents.y;
148         SDL_BlitSurface(backstore, NULL, screen, &r);
149 }
150
151 void Element::SaveScreenToBackstore(void)
152 {
153         SDL_BlitSurface(screen, &extents, backstore, NULL);
154 }
155
156 void Element::ResetCoverageList(void)
157 {
158         // Setup our coverage list with the entire window area
159         coverList.empty();
160         coverList.push_back(extents);
161 }
162
163 void Element::AdjustCoverageList(SDL_Rect r)
164 {
165 //Prolly should have a bool here to set whether or not to do this crap, since it
166 //takes a little time...
167
168         // Here's where we do the coverage list voodoo... :-)
169
170 /*
171 Steps:
172   o Check for intersection. If no intersection, then no need to divide rects.
173   o Loop through current rects. If rect is completely inside passed in rect, remove from list.
174   o Loop through remaining rects. If rect intersects, decompose to four rects and
175     exclude degenerate rects, push rest into the coverage list.
176
177 */
178 //      std::list<Element *>::reverse_iterator ri;
179 //      std::list<SDL_Rect>::iterator i;
180
181         // Loop through rects and remove those completely covered by passed in rect.
182 /*      for(i=coverList.begin(); i!=coverList.end(); i++)
183         {
184 //              if (RectanglesIntersect(r, *i))
185                 if (RectangleFirstInsideSecond(*i, r))
186                 {
187 //This is not right--do a while loop instead of a for loop?
188                         // Remove it from the list...
189                         std::list<SDL_Rect>::iterator next = coverList.erase(i);
190                 }
191         }
192 */
193         // Loop through rects and remove those completely covered by passed in rect.
194         std::list<SDL_Rect>::iterator i = coverList.begin();
195
196         while (i != coverList.end())
197         {
198                 if (RectangleFirstInsideSecond(*i, r))
199                         i = coverList.erase(i);                         // This will also advance i to the next item!
200                 else
201                         i++;
202         }
203
204 //This may not be needed if nothing follows the loop below...!
205 //      if (coverList.empty())
206 //              return;
207
208         // Check for intersection. If no intersection, then no need to divide rects.
209         i = coverList.begin();
210
211         while (i != coverList.end())
212         {
213                 if (RectanglesIntersect(r, *i))
214                 {
215                         // Do the decomposition here. There will always be at least *one* rectangle
216                         // generated by this algorithm, so we know we're OK in removing the original
217                         // from the list. The general pattern looks like this:
218                         //
219                         // +------+
220                         // |1     |
221                         // +-+--+-+
222                         // |2|//|3|  <- Rectangle "r" is in the center
223                         // +-+--+-+
224                         // |4     |
225                         // +------+
226                         //
227                         // Even if r extends beyond the bounds of the rectangle under consideration,
228                         // that's OK because we test to see that the rectangle isn't degenerate
229                         // before adding it to the list.
230
231 //Should probably use a separate list here and splice it in when we're done here...
232 //Or, could use push_front() to avoid the problem... Neat! Doesn't require a separate list!
233 //But, we need to remove the currently referenced rect... Another while loop!
234
235 //This approach won't work--if no rect1 then we're screwed! [FIXED]
236 //Now *that* will work...
237                         SDL_Rect current = *i;
238                         uint32 bottomOfRect1 = current.y;
239 //                      uint32 rightOfRect2 = current.x;
240 //                      uint32 leftOfRect3 = current.x + current.w;
241                         uint32 topOfRect4 = current.y + current.h;
242
243                         // Rectangle #1 (top)
244                         if (r.y > current.y)                            // Simple rectangle degeneracy test...
245                         {
246                                 bottomOfRect1 = r.y;
247                                 SDL_Rect rect = current;
248                                 rect.h = r.y - current.y;
249                                 coverList.push_front(rect);
250                         }
251
252                         // Rectangle #4 (bottom)
253                         if (r.y + r.h < current.y + current.h)
254                         {
255                                 topOfRect4 = r.y + r.h;
256                                 SDL_Rect rect = current;
257                                 rect.y = r.y + r.h;
258                                 rect.h = (current.y + current.h) - (r.y + r.h);
259                                 coverList.push_front(rect);
260                         }
261
262                         // Rectangle #2 (left side)
263                         if (r.x > current.x)
264                         {
265                                 SDL_Rect rect = current;
266                                 rect.w = r.x - current.x;
267                                 rect.y = bottomOfRect1;
268                                 rect.h = topOfRect4 - bottomOfRect1;
269                                 coverList.push_front(rect);
270                         }
271
272                         // Rectangle #3 (right side)
273                         if (r.x + r.w < current.x + current.w)
274                         {
275                                 SDL_Rect rect;
276                                 rect.x = r.x + r.w;
277                                 rect.w = (current.x + current.w) - (r.x + r.w);
278                                 rect.y = bottomOfRect1;
279                                 rect.h = topOfRect4 - bottomOfRect1;
280                                 coverList.push_front(rect);
281                         }
282
283                         i = coverList.erase(i);                         // This will also advance i to the next item!
284                 }
285                 else
286                         i++;
287         }
288 }
289
290 //
291 // Class methods
292 //
293
294 void Element::SetScreen(SDL_Surface * s)
295 {
296         screen = s;
297 }
298
299 bool Element::ScreenNeedsRefreshing(void)
300 {
301         return needToRefreshScreen;
302 }
303
304 void Element::ScreenWasRefreshed(void)
305 {
306         needToRefreshScreen = false;
307 }