00001 #include "glwx.h" 00002 00003 GUITabbedPanel::GUITabbedPanel(const std::string &callback) : GUIRectangle(callback) 00004 { 00005 setTabButtonsBordersColor(0.0f, 0.0f, 0.0f); 00006 setTabButtonsColor(100, 150, 190); 00007 setFontScales(1.0f, 1.0f); 00008 setPosition(0.5, 0.5); 00009 00010 upperPanel = new GUIPanel("tpup"); 00011 lowerPanel = new GUIPanel("tpdown"); 00012 mainPanel = new GUIPanel("tpmp"); 00013 00014 mainPanel->setLayout(PL_YAXIS_LAYOUT); 00015 mainPanel->setInterval(0, 0); 00016 00017 upperPanel->setVisibleBounds(true); 00018 upperPanel->enableBGColor(true); 00019 upperPanel->setLayout(PL_XAXIS_LAYOUT); 00020 upperPanel->setClipSize(0); 00021 00022 lowerPanel->setVisibleBounds(true); 00023 lowerPanel->enableBGColor(true); 00024 lowerPanel->setInterval(0,0); 00025 lowerPanel->setClipSize(0); 00026 00027 mainPanel->addWidget(upperPanel); 00028 mainPanel->addWidget(lowerPanel); 00029 00030 widgetType = WT_TABBED_PANEL; 00031 fontIndex = 0; 00032 lockItems = false; 00033 } 00034 00035 GUITabbedPanel::~GUITabbedPanel() 00036 { 00037 deleteObject(mainPanel); 00038 } 00039 00040 GUIEventListener *GUITabbedPanel::getEventsListener() 00041 { 00042 return this; 00043 } 00044 00045 void GUITabbedPanel::setFontScales(const Tuple2f &scales) 00046 { 00047 setFontScales(scales.x, scales.y); 00048 } 00049 00050 void GUITabbedPanel::setFontScales(float wScale, float hScale) 00051 { 00052 fontScales.x = clamp(hScale, 0.01f, 20.0f); 00053 fontScales.y = clamp(wScale, 0.01f, 20.0f); 00054 } 00055 00056 const Tuple2f &GUITabbedPanel::getFontScales() 00057 { 00058 return fontScales; 00059 } 00060 00061 bool GUITabbedPanel::loadXMLSettings(const TiXmlElement *element) 00062 { 00063 if(!XMLArbiter::inspectElementInfo(element, "TabbedPanel")) 00064 return Logger::writeErrorLog("Need a TabbedPanel node in the xml file"); 00065 00066 Tuple3f bordersColor = upperPanel->getBordersColor(); 00067 Tuple4f bgColor = upperPanel->getBGColor(); 00068 int count = 0; 00069 00070 fontScales.y = XMLArbiter::fillComponents1f(element, "hScale", fontScales.y); 00071 fontScales.x = XMLArbiter::fillComponents1f(element, "wScale", fontScales.x); 00072 fontIndex = XMLArbiter::fillComponents1i(element, "fontIndex", fontIndex); 00073 00074 for(const TiXmlElement *child = element->FirstChildElement(); 00075 child; 00076 child = child->NextSiblingElement() ) 00077 { 00078 const char *value = child->Value(); 00079 if(!value) 00080 continue; 00081 00082 if(!strcmp(value, "Panel")) 00083 { 00084 GUIPanel *panel = new GUIPanel(); 00085 if(!panel->loadXMLSettings(child) || !addPanel(panel)) 00086 deleteObject(panel); 00087 continue; 00088 } 00089 00090 if(!strcmp(value, "TabButtonsBordersColor")) 00091 XMLArbiter::fillComponents3f(child, tabButtonsBordersColor); 00092 00093 if(!strcmp(value, "TabButtonsColor")) 00094 XMLArbiter::fillComponents3f(child, tabButtonsColor); 00095 00096 if(!strcmp(value, "BordersColor")) 00097 XMLArbiter::fillComponents3f(child, bordersColor); 00098 00099 if(!strcmp(value, "BGColor")) 00100 XMLArbiter::fillComponents4f(child, bgColor); 00101 } 00102 00103 upperPanel->setBordersColor(bordersColor); 00104 lowerPanel->setBordersColor(bordersColor); 00105 00106 upperPanel->setBGColor(bgColor); 00107 lowerPanel->setBGColor(bgColor); 00108 00109 setFontScales(fontScales); 00110 00111 return GUIRectangle::loadXMLSettings(element) && lowerPanel->getWidgets().size(); 00112 } 00113 00114 bool GUITabbedPanel::addPanel(GUIPanel *panel) 00115 { 00116 if(lockItems) 00117 return false; 00118 00119 if(lowerPanel->addWidget(panel)) 00120 { 00121 char buffer[256]; 00122 int count = int(lowerPanel->getWidgets().size()) - 1; 00123 sprintf(buffer, "count_tb %d", count); 00124 GUIButton *tabButton = new GUIButton(buffer); 00125 tabButton->setBordersColor(tabButtonsBordersColor); 00126 tabButton->setColor(tabButtonsColor); 00127 tabButton->setLabelString(panel->getCallbackString()); 00128 tabButton->getLabel()->setFontIndex(fontIndex); 00129 tabButton->getLabel()->setScales(fontScales); 00130 tabButton->setMinAlpha((count == 0) ? 1.0f : 0.5f); 00131 00132 upperPanel->addWidget(tabButton); 00133 panel->setVisible((count == 0)); 00134 return true; 00135 } 00136 return false; 00137 } 00138 00139 const GUIPanel* GUITabbedPanel::getCurrentPanel() const 00140 { 00141 const Widgets &widgets = lowerPanel->getWidgets(); 00142 int count = int(widgets.size()); 00143 00144 for(int t = 0; t < count; t++) 00145 if(widgets[t]->isVisible()) 00146 return (GUIPanel*)widgets[t]; 00147 00148 return NULL; 00149 } 00150 00151 const int GUITabbedPanel::getCurrentPanelIndex() const 00152 { 00153 const Widgets &widgets = lowerPanel->getWidgets(); 00154 int count = int(widgets.size()); 00155 00156 for(int t = 0; t < count; t++) 00157 if(widgets[t]->isVisible()) 00158 return t; 00159 00160 return -1; 00161 } 00162 00163 void GUITabbedPanel::checkKeyboardEvents(KeyEvent &evt, int extraInfo) 00164 { 00165 upperPanel->checkKeyboardEvents(evt, extraInfo); 00166 lowerPanel->checkKeyboardEvents(evt, extraInfo); 00167 } 00168 00169 void GUITabbedPanel::checkMouseEvents(MouseEvent &evt, int extraInfo, bool rBits) 00170 { 00171 upperPanel->checkMouseEvents(evt, extraInfo, rBits); 00172 lowerPanel->checkMouseEvents(evt, extraInfo, rBits); 00173 } 00174 00175 void GUITabbedPanel::actionPerformed(GUIEvent &evt) 00176 { 00177 const std::string &cbs = evt.getCallbackString(); 00178 GUIRectangle *sourceRectangle = evt.getEventSource(); 00179 int widgetType = sourceRectangle->getWidgetType(); 00180 00181 if((widgetType == WT_BUTTON) && sourceRectangle->isClicked()) 00182 { 00183 const Widgets &widgets = lowerPanel->getWidgets(), 00184 &buttons = upperPanel->getWidgets(); 00185 int target = atoi(sourceRectangle->getCallbackString().c_str() + 9), 00186 count = int(widgets.size()); 00187 00188 for(int t = 0; t < count; t++) 00189 { 00190 widgets[t]->setVisible(t == target); 00191 ((GUIAlphaElement*)buttons[t])->setMinAlpha((t == target) ? 1.0f : 0.5f); 00192 } 00193 } 00194 } 00195 00196 GUIButton *GUITabbedPanel::getTabButton(int index) 00197 { 00198 const Widgets &widgets = upperPanel->getWidgets(); 00199 int count = int(widgets.size()); 00200 00201 for(int t = 0; t < count; t++) 00202 if(t == index) 00203 return (GUIButton*)widgets[t]; 00204 00205 return NULL; 00206 } 00207 00208 void GUITabbedPanel::render(float clockTick) 00209 { 00210 if(!parent || !visible) 00211 return; 00212 lockItems = true; 00213 mainPanel->render(clockTick); 00214 } 00215 00216 const void GUITabbedPanel::computeWindowBounds() 00217 { 00218 if(parent && update) 00219 { 00220 upperPanel->setParent(mainPanel); 00221 00222 mainPanel->setAnchorPoint(getAnchorPoint()); 00223 mainPanel->setPosition(getPosition()); 00224 mainPanel->setParent(parent); 00225 mainPanel->forceUpdate(true); 00226 00227 windowBounds = mainPanel->getWindowBounds(); 00228 00229 const Widgets & widgets = lowerPanel->getWidgets(); 00230 float height = 0; 00231 int widgetCount = (int)widgets.size(); 00232 00233 for(int i = 0; i < widgetCount; i++) 00234 height = height < widgets[i]->getHeight() ? widgets[i]->getHeight() : height; 00235 lowerPanel->setDimensions(1.0f, height); 00236 upperPanel->setParent(this); 00237 } 00238 } 00239 00240 void GUITabbedPanel::setTabButtonsColor(const Tuple3f& color) 00241 { 00242 setTabButtonsColor(color.x, color.y, color.z); 00243 } 00244 00245 void GUITabbedPanel::setTabButtonsColor(float r, float g, float b) 00246 { 00247 tabButtonsColor.set(clamp(r, 0.0f, 255.0f), 00248 clamp(g, 0.0f, 255.0f), 00249 clamp(b, 0.0f, 255.0f)); 00250 00251 tabButtonsColor.x /= (tabButtonsColor.x > 1.0) ? 255.0f : 1.0f; 00252 tabButtonsColor.y /= (tabButtonsColor.y > 1.0) ? 255.0f : 1.0f; 00253 tabButtonsColor.z /= (tabButtonsColor.z > 1.0) ? 255.0f : 1.0f; 00254 } 00255 00256 const Tuple3f &GUITabbedPanel::getTabButtonsColor() const 00257 { 00258 return tabButtonsColor; 00259 } 00260 00261 void GUITabbedPanel::setTabButtonsBordersColor(const Tuple3f& color) 00262 { 00263 setTabButtonsBordersColor(color.x, color.y, color.z); 00264 } 00265 00266 void GUITabbedPanel::setTabButtonsBordersColor(float r, float g, float b) 00267 { 00268 tabButtonsBordersColor.set(clamp(r, 0.0f, 255.0f), 00269 clamp(g, 0.0f, 255.0f), 00270 clamp(b, 0.0f, 255.0f)); 00271 tabButtonsBordersColor.x /= (tabButtonsBordersColor.x > 1.0) ? 255.0f : 1.0f; 00272 tabButtonsBordersColor.y /= (tabButtonsBordersColor.y > 1.0) ? 255.0f : 1.0f; 00273 tabButtonsBordersColor.z /= (tabButtonsBordersColor.z > 1.0) ? 255.0f : 1.0f; 00274 } 00275 00276 GUIPanel* GUITabbedPanel::getLowerPanel() 00277 { 00278 return lowerPanel; 00279 } 00280 00281 const Tuple3f &GUITabbedPanel::getTabButtonsBordersColor() const 00282 { 00283 return tabButtonsBordersColor; 00284 } 00285