]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/button.cpp
052d19c2c3aac94621ba45107ac849c3942b017d
[virtualjaguar] / src / gui / button.cpp
1 //
2 // Button class
3 //
4 // by James L. Hammons
5 //
6
7 #include "button.h"
8
9 //#include "font14pt.h"
10 #include "guimisc.h"
11
12 // Various constructors
13
14 Button::Button(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/):
15         Element(x, y, w, h), activated(false), clicked(false), inside(false),
16         fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(NULL), elementToTell(NULL)
17 {
18 }
19
20 Button::Button(uint32 x, uint32 y, uint32 w, uint32 h, uint32 * p):
21         Element(x, y, w, h), activated(false), clicked(false), inside(false),
22         fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(p), elementToTell(NULL)
23 {
24 }
25
26 // Button::Button(uint32 x, uint32 y, uint32 * p): Element(x, y, 0, 0),
27
28 Button::Button(uint32 x, uint32 y, uint32 * p, uint32 * pH/*= NULL*/, uint32 * pD/*= NULL*/):
29         Element(x, y, 0, 0), activated(false), clicked(false), inside(false),
30         fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(p), picHover(pH), picDown(pD),
31         elementToTell(NULL)
32 {
33         if (pic)
34                 extents.w = pic[0], extents.h = pic[1];
35 }
36
37 Button::Button(uint32 x, uint32 y, uint32 w, uint32 h, std::string s):
38         Element(x, y, w, h), activated(false), clicked(false), inside(false),
39         fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(NULL), text(s), elementToTell(NULL)
40 {
41 }
42
43 Button::Button(uint32 x, uint32 y, std::string s):
44         Element(x, y, 0, FONT_HEIGHT), activated(false), clicked(false), inside(false),
45         fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(NULL), text(s), elementToTell(NULL)
46 {
47         extents.w = s.length() * FONT_WIDTH;
48 }
49
50 // Implementation
51
52 void Button::HandleMouseMove(uint32 x, uint32 y)
53 {
54         inside = Inside(x, y);
55 }
56
57 void Button::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
58 {
59         if (inside)
60         {
61                 if (mouseDown)
62                         clicked = true;
63
64                 if (clicked && !mouseDown)
65                 {
66                         clicked = false, activated = true;
67
68                         // Send a message that we're activated (if there's someone to tell, that is)
69                         if (elementToTell)
70                                 elementToTell->Notify(this);
71                 }
72         }
73         else
74                 clicked = activated = false;
75 }
76
77 void Button::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/)
78 {
79         uint32 addr = (extents.x + offsetX) + ((extents.y + offsetY) * pitch);
80
81         if (text.length() > 0)                                                  // Simple text button
82 //      if (pic == NULL)
83         {
84                 for(uint32 y=0; y<extents.h; y++)
85                 {
86                         for(uint32 x=0; x<extents.w; x++)
87                         {
88                                 // Doesn't clip in y axis! !!! FIX !!!
89                                 if (extents.x + x < pitch)
90                                         screenBuffer[addr + x + (y * pitch)]
91 //                                      = (clicked && inside ? fgColor : (inside ? 0x43F0 : bgColor));
92 //43F0 -> 010000 11111 10000 -> 0100 0001 1111 1111 1000 0100 -> 41 FF 84
93                                                 = (clicked && inside ? fgColor : (inside ? 0xFF84FF41 : bgColor));
94                         }
95                 }
96
97                 DrawString(screenBuffer, extents.x + offsetX, extents.y + offsetY, false, "%s", text.c_str());
98         }
99         else                                                                                    // Graphical button
100         {
101                 uint32 * picToShow = pic;
102
103                 if (picHover != NULL && inside && !clicked)
104                         picToShow = picHover;
105
106                 if (picDown != NULL && inside && clicked)
107                         picToShow = picDown;
108
109                 DrawTransparentBitmapDeprecated(screenBuffer, extents.x + offsetX, extents.y + offsetY, picToShow);
110         }
111 }
112
113 bool Button::ButtonClicked(void)
114 {
115         return activated;
116 }
117
118 void Button::SetNotificationElement(Element * e)
119 {
120         elementToTell = e;
121 }