4 // Graphical User Interface button class
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 02/02/2006 Created this file
15 #include "guimisc.h" // Various support functions
17 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
18 #define MASK_R 0xFF000000
19 #define MASK_G 0x00FF0000
20 #define MASK_B 0x0000FF00
21 #define MASK_A 0x000000FF
23 #define MASK_R 0x000000FF
24 #define MASK_G 0x0000FF00
25 #define MASK_B 0x00FF0000
26 #define MASK_A 0xFF000000
29 using namespace std; // For STL stuff
32 // Button class implementation
36 Some notes about this class:
38 - Button colors are hardwired
41 Button::Button(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
42 Element * parent/*= NULL*/):
43 Element(x, y, w, h, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, parent),
44 activated(false), clicked(false), inside(false),
45 buttonUp(NULL), buttonDown(NULL), buttonHover(NULL), surfacesAreLocal(false),
46 activatedSave(false), clickedSave(false), insideSave(false)
48 // Should we make a local button bitmap here?
51 Button::Button(uint32 x, uint32 y, uint32 w, uint32 h, SDL_Surface * upImg, Element * parent/*= NULL*/):
52 Element(x, y, w, h, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, parent),
53 activated(false), clicked(false), inside(false),
54 buttonUp(upImg), buttonDown(NULL), buttonHover(NULL), surfacesAreLocal(false),
55 activatedSave(false), clickedSave(false), insideSave(false)
60 // uint32 width = ((Bitmap *)upImg)->width, height = ((Bitmap *)upImg)->height;
62 // buttonUp = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
63 // 32, MASK_R, MASK_G, MASK_B, MASK_A);
64 // memcpy(buttonUp->pixels, ((Bitmap *)upImg)->pixelData, width * height * 4);
66 // Should we make a local button bitmap here? NO--it's passed in!
69 Button::Button(uint32 x, uint32 y, SDL_Surface * bU, SDL_Surface * bH/*= NULL*/,
70 SDL_Surface * bD/*= NULL*/, Element * parent/*= NULL*/):
71 Element(x, y, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, parent),
72 activated(false), clicked(false), inside(false),
73 buttonUp(bU), buttonDown(bD), buttonHover(bH), surfacesAreLocal(false),
74 activatedSave(false), clickedSave(false), insideSave(false)
77 extents.w = buttonUp->w,
78 extents.h = buttonUp->h;
81 Button::Button(uint32 x, uint32 y, uint32 w, uint32 h, string s, Element * parent/*= NULL*/):
82 Element(x, y, w, h, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, parent),
83 activated(false), clicked(false), inside(false),
84 buttonUp(NULL), buttonDown(NULL), buttonHover(NULL), surfacesAreLocal(true),
85 activatedSave(false), clickedSave(false), insideSave(false)
87 // Create the button surfaces here...
90 Button::Button(uint32 x, uint32 y, string s, Element * parent/*= NULL*/):
91 Element(x, y, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, parent),
92 activated(false), clicked(false), inside(false),
93 buttonUp(NULL), buttonDown(NULL), buttonHover(NULL), surfacesAreLocal(true),
94 activatedSave(false), clickedSave(false), insideSave(false)
96 extents.w = (s.length() + 2) * GetFontWidth();
97 extents.h = GetFontHeight();
99 // Create the button surfaces here...
101 buttonUp = SDL_CreateRGBSurface(SDL_SWSURFACE, extents.w, extents.h, 32,
102 MASK_R, MASK_G, MASK_B, MASK_A);
103 buttonDown = SDL_CreateRGBSurface(SDL_SWSURFACE, extents.w, extents.h, 32,
104 MASK_R, MASK_G, MASK_B, MASK_A);
105 buttonHover = SDL_CreateRGBSurface(SDL_SWSURFACE, extents.w, extents.h, 32,
106 MASK_R, MASK_G, MASK_B, MASK_A);
108 // Need to create backgrounds before we do this stuff...
109 SDL_FillRect(buttonUp, NULL, bgColor);
110 SDL_FillRect(buttonDown, NULL, fgColor);
111 SDL_FillRect(buttonHover, NULL, bgColor);
113 DrawStringTrans(buttonUp, GetFontWidth(), 0, fgColor, s.c_str());
114 DrawStringTrans(buttonDown, GetFontWidth(), 0, fgColor, s.c_str());
115 DrawStringTrans(buttonHover, GetFontWidth(), 0, fgColor, s.c_str());
120 if (surfacesAreLocal)
123 SDL_FreeSurface(buttonUp);
126 SDL_FreeSurface(buttonDown);
129 SDL_FreeSurface(buttonHover);
133 void Button::HandleKey(SDLKey key)
137 void Button::HandleMouseMove(uint32 x, uint32 y)
139 SaveStateVariables();
140 inside = Inside(x, y);
141 CheckStateAndRedrawIfNeeded();
144 void Button::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
146 SaveStateVariables();
153 if (clicked && !mouseDown)
155 clicked = false, activated = true;
157 // Send a message to our parent widget (if any) that we're activated
159 parent->Notify(this);
163 clicked = activated = false;
165 CheckStateAndRedrawIfNeeded();
168 void Button::Draw(void)
170 if (buttonUp == NULL)
171 return; // Bail out if no surface was created...
173 SDL_Rect rect = GetScreenCoords();
175 // Now, draw the appropriate button state!
177 SDL_Surface * picToShow = buttonUp;
179 if (buttonHover != NULL && inside && !clicked)
180 picToShow = buttonHover;
182 if (buttonDown != NULL && inside && clicked)
183 picToShow = buttonDown;
185 SDL_BlitSurface(picToShow, NULL, screen, &rect); // This handles alpha blending too! :-D
187 needToRefreshScreen = true;
190 void Button::Notify(Element *)
194 bool Button::ButtonClicked(void)
199 void Button::SaveStateVariables(void)
201 activatedSave = activated;
202 clickedSave = clicked;
206 void Button::CheckStateAndRedrawIfNeeded(void)
208 // Check to see if any of our state variables changed since we last saved them...
209 if (activated != activatedSave || clicked != clickedSave || inside != insideSave)