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 = 0x00FFFFFF; 00046 const colour ButtonBase::DefaultHoverLabelColour = 0x00FFFFFF; 00047 const colour ButtonBase::DefaultPushedLabelColour = 0x00FFFFFF; 00048 const colour ButtonBase::DefaultDisabledLabelColour = 0x007F7F7F; 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 // alpha part comes from window alpha 00122 d_normalColour = (colour & 0x00FFFFFF); 00123 requestRedraw(); 00124 } 00125 00126 } 00127 00128 00129 /************************************************************************* 00130 Set the colour to use for the label text when rendering in the 00131 hover / highlighted states. 00132 *************************************************************************/ 00133 void ButtonBase::setHoverTextColour(const colour& colour) 00134 { 00135 if (d_hoverColour != colour) 00136 { 00137 // alpha part comes from window alpha 00138 d_hoverColour = (colour & 0x00FFFFFF); 00139 requestRedraw(); 00140 } 00141 00142 } 00143 00144 00145 /************************************************************************* 00146 Set the colour to use for the label text when rendering in the 00147 pushed state. 00148 *************************************************************************/ 00149 void ButtonBase::setPushedTextColour(const colour& colour) 00150 { 00151 if (d_pushedColour != colour) 00152 { 00153 // alpha part comes from window alpha 00154 d_pushedColour = (colour & 0x00FFFFFF); 00155 requestRedraw(); 00156 } 00157 00158 } 00159 00160 00161 /************************************************************************* 00162 Set the colour to use for the label text when rendering in the 00163 disabled state. 00164 *************************************************************************/ 00165 void ButtonBase::setDisabledTextColour(const colour& colour) 00166 { 00167 if (d_disabledColour != colour) 00168 { 00169 // alpha part comes from window alpha 00170 d_disabledColour = (colour & 0x00FFFFFF); 00171 requestRedraw(); 00172 } 00173 00174 } 00175 00176 00177 /************************************************************************* 00178 Perform the rendering for this widget. 00179 *************************************************************************/ 00180 void ButtonBase::drawSelf(float z) 00181 { 00182 if (isHovering()) 00183 { 00184 drawHover(z); 00185 } 00186 else if (isPushed()) 00187 { 00188 drawPushed(z); 00189 } 00190 else if (isDisabled()) 00191 { 00192 drawDisabled(z); 00193 } 00194 else 00195 { 00196 drawNormal(z); 00197 } 00198 00199 } 00200 00201 00202 /************************************************************************* 00203 Handler for when the mouse moves 00204 *************************************************************************/ 00205 void ButtonBase::onMouseMove(MouseEventArgs& e) 00206 { 00207 // this is needed to discover whether mouse is in the widget area or not. 00208 // The same thing used to be done each frame in the rendering method, 00209 // but in this version the rendering method may not be called every frame 00210 // so we must discover the internal widget state here - which is actually 00211 // more efficient anyway. 00212 00213 // base class processing 00214 Window::onMouseMove(e); 00215 00216 updateInternalState(e.position); 00217 e.handled = true; 00218 } 00219 00220 00221 /************************************************************************* 00222 Handler for mouse button pressed events 00223 *************************************************************************/ 00224 void ButtonBase::onMouseButtonDown(MouseEventArgs& e) 00225 { 00226 // default processing 00227 Window::onMouseButtonDown(e); 00228 00229 if (e.button == LeftButton) 00230 { 00231 if (captureInput()) 00232 { 00233 d_pushed = true; 00234 updateInternalState(e.position); 00235 requestRedraw(); 00236 } 00237 00238 // event was handled by us. 00239 e.handled = true; 00240 } 00241 00242 } 00243 00244 00245 /************************************************************************* 00246 Handler for mouse button release events 00247 *************************************************************************/ 00248 void ButtonBase::onMouseButtonUp(MouseEventArgs& e) 00249 { 00250 // default processing 00251 Window::onMouseButtonUp(e); 00252 00253 if (e.button == LeftButton) 00254 { 00255 releaseInput(); 00256 00257 // event was handled by us. 00258 e.handled = true; 00259 } 00260 00261 } 00262 00263 /************************************************************************* 00264 Handler for when mouse capture is lost 00265 *************************************************************************/ 00266 void ButtonBase::onCaptureLost(WindowEventArgs& e) 00267 { 00268 // Default processing 00269 Window::onCaptureLost(e); 00270 00271 d_pushed = false; 00272 updateInternalState(MouseCursor::getSingleton().getPosition()); 00273 requestRedraw(); 00274 00275 // event was handled by us. 00276 e.handled = true; 00277 } 00278 00279 00280 /************************************************************************* 00281 Handler for when mouse leaves the widget 00282 *************************************************************************/ 00283 void ButtonBase::onMouseLeaves(MouseEventArgs& e) 00284 { 00285 // deafult processing 00286 Window::onMouseLeaves(e); 00287 00288 d_hovering = false; 00289 requestRedraw(); 00290 00291 e.handled = true; 00292 } 00293 00294 00295 /************************************************************************* 00296 Add properties for this class 00297 *************************************************************************/ 00298 void ButtonBase::addButtonBaseProperties(void) 00299 { 00300 addProperty(&d_normalTextColourProperty); 00301 addProperty(&d_hoverTextColourProperty); 00302 addProperty(&d_pushedTextColourProperty); 00303 addProperty(&d_disabledTextColourProperty); 00304 } 00305 00306 } // End of CEGUI namespace section