00001 /************************************************************************ 00002 filename: CEGUIButtonBase.cpp 00003 created: 13/4/2004 00004 author: Paul D Turner 00005 00006 purpose: Implementation of ButtonBase widget 00007 *************************************************************************/ 00008 /************************************************************************* 00009 Crazy Eddie's GUI System (http://www.cegui.org.uk) 00010 Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk) 00011 00012 This library is free software; you can redistribute it and/or 00013 modify it under the terms of the GNU Lesser General Public 00014 License as published by the Free Software Foundation; either 00015 version 2.1 of the License, or (at your option) any later version. 00016 00017 This library is distributed in the hope that it will be useful, 00018 but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 Lesser General Public License for more details. 00021 00022 You should have received a copy of the GNU Lesser General Public 00023 License along with this library; if not, write to the Free Software 00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00025 *************************************************************************/ 00026 #include "elements/CEGUIButtonBase.h" 00027 #include "CEGUIMouseCursor.h" 00028 00029 // Start of CEGUI namespace section 00030 namespace CEGUI 00031 { 00032 /************************************************************************* 00033 Definition of Properties for this class 00034 *************************************************************************/ 00035 ButtonBaseProperties::NormalTextColour ButtonBase::d_normalTextColourProperty; 00036 ButtonBaseProperties::HoverTextColour ButtonBase::d_hoverTextColourProperty; 00037 ButtonBaseProperties::PushedTextColour ButtonBase::d_pushedTextColourProperty; 00038 ButtonBaseProperties::DisabledTextColour ButtonBase::d_disabledTextColourProperty; 00039 00040 00041 /************************************************************************* 00042 Constants 00043 *************************************************************************/ 00044 // default colours for text label rendering 00045 const colour ButtonBase::DefaultNormalLabelColour = 0xFFFFFFFF; 00046 const colour ButtonBase::DefaultHoverLabelColour = 0xFFFFFFFF; 00047 const colour ButtonBase::DefaultPushedLabelColour = 0xFFFFFFFF; 00048 const colour ButtonBase::DefaultDisabledLabelColour = 0xFF7F7F7F; 00049 00050 00051 /************************************************************************* 00052 Constructor 00053 *************************************************************************/ 00054 ButtonBase::ButtonBase(const String& type, const String& name) : 00055 Window(type, name), 00056 d_pushed(false), 00057 d_hovering(false), 00058 d_normalColour(DefaultNormalLabelColour), 00059 d_hoverColour(DefaultHoverLabelColour), 00060 d_pushedColour(DefaultPushedLabelColour), 00061 d_disabledColour(DefaultDisabledLabelColour) 00062 { 00063 addButtonBaseProperties(); 00064 } 00065 00066 00067 /************************************************************************* 00068 Destructor 00069 *************************************************************************/ 00070 ButtonBase::~ButtonBase(void) 00071 { 00072 } 00073 00074 00075 /************************************************************************* 00076 Update the internal state of the Widget 00077 *************************************************************************/ 00078 void ButtonBase::updateInternalState(const Point& mouse_pos) 00079 { 00080 bool oldstate = d_hovering; 00081 00082 // assume not hovering 00083 d_hovering = false; 00084 00085 // if input is captured, but not by 'this', then we never hover highlight 00086 const Window* capture_wnd = getCaptureWindow(); 00087 00088 if ((capture_wnd == NULL) || (capture_wnd == this)) 00089 { 00090 Window* sheet = System::getSingleton().getGUISheet(); 00091 00092 if (sheet != NULL) 00093 { 00094 // check if hovering highlight is required, which is basically ("mouse over widget" XOR "widget pushed"). 00095 if ((this == sheet->getChildAtPosition(mouse_pos)) != d_pushed) 00096 { 00097 d_hovering = true; 00098 } 00099 00100 } 00101 00102 } 00103 00104 // if state has changed, trigger a re-draw 00105 if (oldstate != d_hovering) 00106 { 00107 requestRedraw(); 00108 } 00109 00110 } 00111 00112 00113 /************************************************************************* 00114 Set the colour to use for the label text when rendering in the 00115 normal state. 00116 *************************************************************************/ 00117 void ButtonBase::setNormalTextColour(const colour& colour) 00118 { 00119 if (d_normalColour != colour) 00120 { 00121 d_normalColour = colour; 00122 requestRedraw(); 00123 } 00124 00125 } 00126 00127 00128 /************************************************************************* 00129 Set the colour to use for the label text when rendering in the 00130 hover / highlighted states. 00131 *************************************************************************/ 00132 void ButtonBase::setHoverTextColour(const colour& colour) 00133 { 00134 if (d_hoverColour != colour) 00135 { 00136 d_hoverColour = colour; 00137 requestRedraw(); 00138 } 00139 00140 } 00141 00142 00143 /************************************************************************* 00144 Set the colour to use for the label text when rendering in the 00145 pushed state. 00146 *************************************************************************/ 00147 void ButtonBase::setPushedTextColour(const colour& colour) 00148 { 00149 if (d_pushedColour != colour) 00150 { 00151 d_pushedColour = colour; 00152 requestRedraw(); 00153 } 00154 00155 } 00156 00157 00158 /************************************************************************* 00159 Set the colour to use for the label text when rendering in the 00160 disabled state. 00161 *************************************************************************/ 00162 void ButtonBase::setDisabledTextColour(const colour& colour) 00163 { 00164 if (d_disabledColour != colour) 00165 { 00166 d_disabledColour = colour; 00167 requestRedraw(); 00168 } 00169 00170 } 00171 00172 00173 /************************************************************************* 00174 Perform the rendering for this widget. 00175 *************************************************************************/ 00176 void ButtonBase::drawSelf(float z) 00177 { 00178 if (isHovering()) 00179 { 00180 drawHover(z); 00181 } 00182 else if (isPushed()) 00183 { 00184 drawPushed(z); 00185 } 00186 else if (isDisabled()) 00187 { 00188 drawDisabled(z); 00189 } 00190 else 00191 { 00192 drawNormal(z); 00193 } 00194 00195 } 00196 00197 00198 /************************************************************************* 00199 Handler for when the mouse moves 00200 *************************************************************************/ 00201 void ButtonBase::onMouseMove(MouseEventArgs& e) 00202 { 00203 // this is needed to discover whether mouse is in the widget area or not. 00204 // The same thing used to be done each frame in the rendering method, 00205 // but in this version the rendering method may not be called every frame 00206 // so we must discover the internal widget state here - which is actually 00207 // more efficient anyway. 00208 00209 // base class processing 00210 Window::onMouseMove(e); 00211 00212 updateInternalState(e.position); 00213 e.handled = true; 00214 } 00215 00216 00217 /************************************************************************* 00218 Handler for mouse button pressed events 00219 *************************************************************************/ 00220 void ButtonBase::onMouseButtonDown(MouseEventArgs& e) 00221 { 00222 // default processing 00223 Window::onMouseButtonDown(e); 00224 00225 if (e.button == LeftButton) 00226 { 00227 if (captureInput()) 00228 { 00229 d_pushed = true; 00230 updateInternalState(e.position); 00231 requestRedraw(); 00232 } 00233 00234 // event was handled by us. 00235 e.handled = true; 00236 } 00237 00238 } 00239 00240 00241 /************************************************************************* 00242 Handler for mouse button release events 00243 *************************************************************************/ 00244 void ButtonBase::onMouseButtonUp(MouseEventArgs& e) 00245 { 00246 // default processing 00247 Window::onMouseButtonUp(e); 00248 00249 if (e.button == LeftButton) 00250 { 00251 releaseInput(); 00252 00253 // event was handled by us. 00254 e.handled = true; 00255 } 00256 00257 } 00258 00259 /************************************************************************* 00260 Handler for when mouse capture is lost 00261 *************************************************************************/ 00262 void ButtonBase::onCaptureLost(WindowEventArgs& e) 00263 { 00264 // Default processing 00265 Window::onCaptureLost(e); 00266 00267 d_pushed = false; 00268 updateInternalState(MouseCursor::getSingleton().getPosition()); 00269 requestRedraw(); 00270 00271 // event was handled by us. 00272 e.handled = true; 00273 } 00274 00275 00276 /************************************************************************* 00277 Handler for when mouse leaves the widget 00278 *************************************************************************/ 00279 void ButtonBase::onMouseLeaves(MouseEventArgs& e) 00280 { 00281 // deafult processing 00282 Window::onMouseLeaves(e); 00283 00284 d_hovering = false; 00285 requestRedraw(); 00286 00287 e.handled = true; 00288 } 00289 00290 00291 /************************************************************************* 00292 Add properties for this class 00293 *************************************************************************/ 00294 void ButtonBase::addButtonBaseProperties(void) 00295 { 00296 addProperty(&d_normalTextColourProperty); 00297 addProperty(&d_hoverTextColourProperty); 00298 addProperty(&d_pushedTextColourProperty); 00299 addProperty(&d_disabledTextColourProperty); 00300 } 00301 00302 } // End of CEGUI namespace section