00001 #include "glwx.h" 00002 00003 GUICheckBox::GUICheckBox(const std::string &callback) : GUIAlphaElement(callback) 00004 { 00005 setMinAlphaMark(0.0f); 00006 setDimensions(20, 20); 00007 setAlphaMark(1.0f); 00008 setBGColor(0.8f, 0.8f, 0.8f); 00009 setChecked(false); 00010 widgetType = WT_CHECK_BOX; 00011 00012 secondaryTexDesc = WT_CHECK_BOX_MARK; 00013 primaryTexDesc = WT_CHECK_BOX; 00014 markOffset = 0; 00015 markRatio = 0.85f; 00016 extra = 0; 00017 } 00018 00019 bool GUICheckBox::loadXMLSettings(const TiXmlElement *element) 00020 { 00021 if(widgetType == WT_CHECK_BOX) 00022 if(!element || !element->Value() || strcmp(element->Value(), "CheckBox")) 00023 return Logger::writeErrorLog("Need a CheckBox node in the xml file"); 00024 00025 if(widgetType == WT_RADIO_BUTTON) 00026 if(!element || !element->Value() || strcmp(element->Value(), "RadioButton")) 00027 return Logger::writeErrorLog("Need a RadioButton node in the xml file"); 00028 00029 setChecked(XMLArbiter::analyzeBooleanAttr(element, "checked", false)); 00030 00031 for(const TiXmlElement *child = element->FirstChildElement(); 00032 child; 00033 child = child->NextSiblingElement() ) 00034 { 00035 const char * value = child->Value(); 00036 00037 if(value) 00038 if(!strcmp(value, "BGColor")) 00039 { 00040 setBGColor(XMLArbiter::fillComponents3f(child, bgColor)); 00041 break; 00042 } 00043 00044 } 00045 00046 return GUIAlphaElement::loadXMLSettings(element); 00047 } 00048 00049 void GUICheckBox::setMinAlphaMark(float mark) 00050 { 00051 minAlphaMark = clamp(mark, 0.0f, 1.0f); 00052 } 00053 00054 float GUICheckBox::getMinAlphaMark() 00055 { 00056 return minAlphaMark; 00057 } 00058 00059 void GUICheckBox::setAlphaMark(float mark) 00060 { 00061 alphaMark = clamp(mark, minAlphaMark, 1.0f); 00062 } 00063 00064 float GUICheckBox::getAlphaMark() 00065 { 00066 return alphaMark; 00067 } 00068 00069 void GUICheckBox::setChecked(bool enabledArg) 00070 { 00071 if(active) 00072 checked = enabledArg; 00073 } 00074 00075 bool GUICheckBox::isChecked() 00076 { 00077 return checked; 00078 } 00079 00080 void GUICheckBox::setBGColor(const Tuple3f& color) 00081 { 00082 setBGColor(color.x, color.y, color.z); 00083 } 00084 00085 void GUICheckBox::setBGColor(float x, float y, float z) 00086 { 00087 bgColor.set(clamp(x, 0.0f, 1.0f), 00088 clamp(y, 0.0f, 1.0f), 00089 clamp(z, 0.0f, 1.0f)); 00090 } 00091 00092 const Tuple3f &GUICheckBox::getBGColor() 00093 { 00094 return bgColor; 00095 } 00096 00097 void GUICheckBox::render(float clockTick) 00098 { 00099 if(!parent || !visible) 00100 return; 00101 00102 modifyCurrentAlpha(clockTick); 00103 getCenter(); 00104 00105 GUITexCoordDescriptor *descriptor = parent->getTexCoordsInfo(primaryTexDesc), 00106 *descriptorCB = parent->getTexCoordsInfo(secondaryTexDesc); 00107 00108 int yCenter = (windowBounds.y + windowBounds.w)/2; 00109 00110 glEnable(GL_BLEND); 00111 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00112 glColor4fv(color); 00113 00114 if(descriptor) 00115 { 00116 const Tuple4f &texCoords = descriptor->getTexCoords(); 00117 parent->enableGUITexture(); 00118 00119 glBegin(GL_TRIANGLE_STRIP); 00120 glTexCoord2f(texCoords.x, texCoords.y); 00121 glVertex2i(windowBounds.x, windowBounds.y); 00122 00123 glTexCoord2f(texCoords.x, texCoords.w); 00124 glVertex2i(windowBounds.x, windowBounds.w); 00125 00126 glTexCoord2f(texCoords.z, texCoords.y); 00127 glVertex2i(windowBounds.x + extra, windowBounds.y); 00128 00129 glTexCoord2f(texCoords.z, texCoords.w); 00130 glVertex2i(windowBounds.x + extra, windowBounds.w); 00131 glEnd(); 00132 00133 parent->disableGUITexture(); 00134 } 00135 else 00136 { 00137 glBegin(GL_TRIANGLE_STRIP); 00138 glVertex2i(windowBounds.x, windowBounds.y); 00139 glVertex2i(windowBounds.x, windowBounds.w); 00140 glVertex2i(windowBounds.x + extra, windowBounds.y); 00141 glVertex2i(windowBounds.x + extra, windowBounds.w); 00142 glEnd(); 00143 } 00144 00145 if(!checked) 00146 setAlphaMark(alphaMark - clockTick*alphaFadeScale*4.0f); 00147 else 00148 setAlphaMark(alphaMark + clockTick*alphaFadeScale*4.0f); 00149 00150 if(alphaMark != minAlphaMark) 00151 { 00152 glColor4f(1.0f, 1.0f, 1.0f, alphaMark); 00153 00154 if(descriptorCB) 00155 { 00156 const Tuple4f &texCoords = descriptorCB->getTexCoords(); 00157 parent->enableGUITexture(); 00158 00159 glBegin(GL_TRIANGLE_STRIP); 00160 glTexCoord2f(texCoords.x, texCoords.y); 00161 glVertex2i(windowBounds.x + markOffset, windowBounds.y + markOffset); 00162 00163 glTexCoord2f(texCoords.x, texCoords.w); 00164 glVertex2i(windowBounds.x + markOffset, windowBounds.w - markOffset); 00165 00166 glTexCoord2f(texCoords.z, texCoords.y); 00167 glVertex2i(windowBounds.x - markOffset + extra, windowBounds.y + markOffset); 00168 00169 glTexCoord2f(texCoords.z, texCoords.w); 00170 glVertex2i(windowBounds.x - markOffset + extra, windowBounds.w - markOffset); 00171 glEnd(); 00172 00173 parent->disableGUITexture(); 00174 } 00175 else 00176 { 00177 glBegin(GL_TRIANGLE_STRIP); 00178 glVertex2i(windowBounds.x + markOffset, windowBounds.y + markOffset); 00179 glVertex2i(windowBounds.x + markOffset, windowBounds.w - markOffset); 00180 glVertex2i(windowBounds.x - markOffset + extra, windowBounds.y + markOffset); 00181 glVertex2i(windowBounds.x - markOffset + extra, windowBounds.w - markOffset); 00182 glEnd(); 00183 } 00184 } 00185 00186 glDisable(GL_BLEND); 00187 label.printCenteredY(windowBounds.x + extra + 2, yCenter); 00188 } 00189 00190 const Tuple4i &GUICheckBox::getWindowBounds() 00191 { 00192 if(parent && update) 00193 { 00194 label.computeDimensions(); 00195 extra = int(float(label.getHeight())*1.25); 00196 dimensions.set(float(label.getWidth() + extra), float(extra)); 00197 markOffset = extra - int(dimensions.y*markRatio); 00198 GUIRectangle::computeWindowBounds(); 00199 } 00200 return windowBounds; 00201 } 00202 00203 void GUICheckBox::checkMouseEvents(MouseEvent &newEvent, int extraInfo, bool bits) 00204 { 00205 GUIRectangle::checkMouseEvents(newEvent, extraInfo, true); 00206 GUIEventListener *eventsListener = getEventsListener(); 00207 00208 if(clicked) 00209 { 00210 setChecked(!checked); 00211 if(eventsListener) 00212 { 00213 GUIEvent evt(this); 00214 eventsListener->actionPerformed(evt); 00215 } 00216 } 00217 released = false; 00218 clicked = false; 00219 }