X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fgui%2Fbutton.cpp;fp=src%2Fgui%2Fbutton.cpp;h=052d19c2c3aac94621ba45107ac849c3942b017d;hb=f9098d0570ae6462781e8189518085cb1c8c00ef;hp=0000000000000000000000000000000000000000;hpb=836c7fa1f3e2dc3ec9849cac2584d4544bf2fba4;p=virtualjaguar diff --git a/src/gui/button.cpp b/src/gui/button.cpp new file mode 100644 index 0000000..052d19c --- /dev/null +++ b/src/gui/button.cpp @@ -0,0 +1,121 @@ +// +// Button class +// +// by James L. Hammons +// + +#include "button.h" + +//#include "font14pt.h" +#include "guimisc.h" + +// Various constructors + +Button::Button(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/): + Element(x, y, w, h), activated(false), clicked(false), inside(false), + fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(NULL), elementToTell(NULL) +{ +} + +Button::Button(uint32 x, uint32 y, uint32 w, uint32 h, uint32 * p): + Element(x, y, w, h), activated(false), clicked(false), inside(false), + fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(p), elementToTell(NULL) +{ +} + +// Button::Button(uint32 x, uint32 y, uint32 * p): Element(x, y, 0, 0), + +Button::Button(uint32 x, uint32 y, uint32 * p, uint32 * pH/*= NULL*/, uint32 * pD/*= NULL*/): + Element(x, y, 0, 0), activated(false), clicked(false), inside(false), + fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(p), picHover(pH), picDown(pD), + elementToTell(NULL) +{ + if (pic) + extents.w = pic[0], extents.h = pic[1]; +} + +Button::Button(uint32 x, uint32 y, uint32 w, uint32 h, std::string s): + Element(x, y, w, h), activated(false), clicked(false), inside(false), + fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(NULL), text(s), elementToTell(NULL) +{ +} + +Button::Button(uint32 x, uint32 y, std::string s): + Element(x, y, 0, FONT_HEIGHT), activated(false), clicked(false), inside(false), + fgColor(0xFFFFFFFF), bgColor(0xFF00FF00), pic(NULL), text(s), elementToTell(NULL) +{ + extents.w = s.length() * FONT_WIDTH; +} + +// Implementation + +void Button::HandleMouseMove(uint32 x, uint32 y) +{ + inside = Inside(x, y); +} + +void Button::HandleMouseButton(uint32 x, uint32 y, bool mouseDown) +{ + if (inside) + { + if (mouseDown) + clicked = true; + + if (clicked && !mouseDown) + { + clicked = false, activated = true; + + // Send a message that we're activated (if there's someone to tell, that is) + if (elementToTell) + elementToTell->Notify(this); + } + } + else + clicked = activated = false; +} + +void Button::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/) +{ + uint32 addr = (extents.x + offsetX) + ((extents.y + offsetY) * pitch); + + if (text.length() > 0) // Simple text button +// if (pic == NULL) + { + for(uint32 y=0; y 010000 11111 10000 -> 0100 0001 1111 1111 1000 0100 -> 41 FF 84 + = (clicked && inside ? fgColor : (inside ? 0xFF84FF41 : bgColor)); + } + } + + DrawString(screenBuffer, extents.x + offsetX, extents.y + offsetY, false, "%s", text.c_str()); + } + else // Graphical button + { + uint32 * picToShow = pic; + + if (picHover != NULL && inside && !clicked) + picToShow = picHover; + + if (picDown != NULL && inside && clicked) + picToShow = picDown; + + DrawTransparentBitmapDeprecated(screenBuffer, extents.x + offsetX, extents.y + offsetY, picToShow); + } +} + +bool Button::ButtonClicked(void) +{ + return activated; +} + +void Button::SetNotificationElement(Element * e) +{ + elementToTell = e; +}