00001 #include "glwx.h" 00002 00003 GUIButton::GUIButton(const std::string &callback) : GUIAlphaElement(callback), GUIClippedRectangle() 00004 { 00005 setBordersColor(0.0f, 0.0f, 0.0f); 00006 setDimensions(40, 22); 00007 setPosition(0.5, 0.5); 00008 setColor(100, 150, 190); 00009 00010 widgetType = WT_BUTTON; 00011 00012 drawBackground = true; 00013 drawBounds = true; 00014 bounce = true; 00015 } 00016 00017 bool GUIButton::loadXMLSettings(const TiXmlElement *element) 00018 { 00019 if(!XMLArbiter::inspectElementInfo(element, "Button")) 00020 return Logger::writeErrorLog("Need a Button node in the xml file"); 00021 00022 enableBounce(XMLArbiter::analyzeBooleanAttr(element, "bounce", true)); 00023 return GUIAlphaElement::loadXMLSettings(element) && 00024 GUIClippedRectangle::loadXMLClippedRectangleInfo(element); 00025 } 00026 00027 void GUIButton::enableBounce(bool bounce_){ bounce = bounce_; } 00028 bool GUIButton::bounceEnabled() { return bounce; } 00029 00030 void GUIButton::render(float clockTick) 00031 { 00032 if(!parent || !visible) 00033 return; 00034 00035 modifyCurrentAlpha(clockTick); 00036 bgColor = color; 00037 00038 Tuple3f tempColor = label.getColor(); 00039 float displacement = 2.0f*(pressed || clicked)*bounce; 00040 int xCenter = (windowBounds.x + windowBounds.z)/2, 00041 yCenter = (windowBounds.y + windowBounds.w)/2; 00042 00043 glTranslatef(displacement, displacement, 0.0); 00044 renderClippedBounds(); 00045 label.printCenteredXY(xCenter, yCenter); 00046 glTranslatef(-displacement, -displacement, 0.0f); 00047 } 00048 00049 const void GUIButton::computeWindowBounds() 00050 { 00051 if(parent && update) 00052 { 00053 GUIRectangle::computeWindowBounds(); 00054 label.computeDimensions(); 00055 00056 int width = windowBounds.z - windowBounds.x, 00057 height = windowBounds.w - windowBounds.y; 00058 00059 if(width <= label.getWidth() + 2*clipSize) 00060 { 00061 if(anchor == AT_CENTER) 00062 { 00063 width = (label.getWidth() - width)/2 + clipSize + 2; 00064 windowBounds.x -=width; 00065 windowBounds.z +=width; 00066 } 00067 if((anchor == AT_CORNERLU) || (anchor == AT_CORNERLD)) 00068 { 00069 width = (label.getWidth() - width)/2 + clipSize + 2; 00070 windowBounds.z +=2*width; 00071 } 00072 } 00073 00074 if(height + 2*clipSize < label.getHeight()) 00075 { 00076 00077 height = (label.getHeight() - height)/2 + clipSize + 2; 00078 windowBounds.y -= height; 00079 windowBounds.w += height; 00080 } 00081 00082 computeClippedBounds(windowBounds); 00083 } 00084 }