Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIPopupMenu.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIPopupMenu.cpp
00003         created:        1/4/2005
00004         author:         Tomas Lindquist Olsen (based on code by Paul D Turner)
00005         
00006         purpose:        Implementation of PopupMenu widget base class
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/CEGUIPopupMenu.h"
00027 #include "elements/CEGUIMenuItem.h"
00028 
00029 // Start of CEGUI namespace section
00030 namespace CEGUI
00031 {
00032 
00033 /*************************************************************************
00034         Definition of Properties for this class
00035 *************************************************************************/
00036 PopupMenuProperties::FadeInTime         PopupMenu::d_fadeInTimeProperty;
00037 PopupMenuProperties::FadeOutTime        PopupMenu::d_fadeOutTimeProperty;
00038 
00039 
00040 /*************************************************************************
00041         Constants
00042 *************************************************************************/
00043 const String PopupMenu::EventNamespace("PopupMenu");
00044 
00045 
00046 /*************************************************************************
00047         Constructor for PopupMenu base class.
00048 *************************************************************************/
00049 PopupMenu::PopupMenu(const String& type, const String& name)
00050         : MenuBase(type, name),
00051         d_origAlpha(d_alpha),
00052         d_fadeOutTime(0),
00053         d_fadeInTime(0),
00054         d_fading(false),
00055         d_fadingOut(false)
00056 {
00057         d_itemSpacing = 2;
00058         d_horzPadding = 10;
00059         d_vertPadding = 2;
00060         d_borderWidth = 0;
00061 
00062         addPopupMenuProperties();
00063 
00064         // enable auto resizing
00065         d_autoResize = true;
00066 
00067         // disable parent clipping
00068         setClippedByParent(false);
00069 
00070         // hide by default
00071         hide();
00072 }
00073 
00074 
00075 /*************************************************************************
00076         Destructor for PopupMenu base class.
00077 *************************************************************************/
00078 PopupMenu::~PopupMenu(void)
00079 {
00080 }
00081 
00082 
00083 /*************************************************************************
00084         Tells the popup menu to open.
00085 *************************************************************************/
00086 void PopupMenu::openPopupMenu(void)
00087 {
00088         // are we fading?
00089         if (d_fading)
00090         {
00091                 // fading in!
00092                 if (!d_fadingOut)
00093                 {
00094                         // dont want to restart fade in!
00095                         return;
00096                 }
00097                 // fading out!
00098                 else
00099                 {
00100                         // make sure the "fade back in" is smooth - if possible !
00101                         if (d_fadeInTime>0.0f&&d_fadeOutTime>0.0f)
00102                         {
00103                                 // jump to the point of the fade in that has the same alpha as right now - this keeps it smooth
00104                                 d_fadeElapsed = ((d_fadeOutTime-d_fadeElapsed)/d_fadeOutTime)*d_fadeInTime;
00105                         }
00106                         else
00107                         {
00108                                 // start the fade in from the beginning
00109                                 d_fadeElapsed = 0;
00110                         }
00111                         // change to fade in
00112                         d_fadingOut=false;
00113                 }
00114 
00115         }
00116         // start normal fade in!
00117         else if (d_fadeInTime>0.0f)
00118         {
00119                 d_fading = true;
00120                 d_fadingOut=false;
00121                 setAlpha(0.0f);
00122                 d_fadeElapsed = 0;
00123         }
00124         // should not fade!
00125         else
00126         {
00127                 d_fading = false;
00128                 setAlpha(d_origAlpha);
00129         }
00130         
00131         show();
00132         moveToFront();
00133 }
00134 
00135 
00136 /*************************************************************************
00137         Tells the popup menu to close.
00138 *************************************************************************/
00139 void PopupMenu::closePopupMenu(void)
00140 {
00141         // are we fading?
00142         if (d_fading)
00143         {
00144                 // fading out!
00145                 if (d_fadingOut)
00146                 {
00147                         // dont want to restart fade out!
00148                         return;
00149                 }
00150                 // fading in!
00151                 else
00152                 {
00153                         // make sure the "fade back out" is smooth - if possible !
00154                         if (d_fadeInTime>0.0f&&d_fadeOutTime>0.0f)
00155                         {
00156                                 // jump to the point of the fade in that has the same alpha as right now - this keeps it smooth
00157                                 d_fadeElapsed = ((d_fadeInTime-d_fadeElapsed)/d_fadeInTime)*d_fadeOutTime;
00158                         }
00159                         else
00160                         {
00161                                 // start the fade in from the beginning
00162                                 d_fadeElapsed = 0;
00163                         }
00164                         // change to fade in
00165                         d_fadingOut=true;
00166                 }
00167 
00168         }
00169         // start normal fade out!
00170         else if (d_fadeOutTime>0.0f)
00171         {
00172                 d_fading = true;
00173                 d_fadingOut=true;
00174                 setAlpha(d_origAlpha);
00175                 d_fadeElapsed = 0;
00176         }
00177         // should not fade!
00178         else
00179         {
00180                 d_fading = false;
00181                 hide();
00182         }
00183 
00184 }
00185 
00186 
00187 /*************************************************************************
00188     Perform actual update processing for this Window.
00189 *************************************************************************/
00190 void PopupMenu::updateSelf(float elapsed)
00191 {
00192     ItemListBase::updateSelf(elapsed);
00193     
00194     // handle fading
00195     if (d_fading)
00196     {
00197                 d_fadeElapsed+=elapsed;
00198 
00199                 // fading out
00200                 if (d_fadingOut)
00201                 {
00202                         if (d_fadeElapsed>=d_fadeOutTime)
00203                         {
00204                                 hide();
00205                                 d_fading=false;
00206                         }
00207                         else
00208                         {
00209                                 setAlpha(d_origAlpha*(d_fadeOutTime-d_fadeElapsed)/d_fadeOutTime);
00210                         }
00211 
00212                 }
00213 
00214                 // fading in
00215                 else
00216                 {
00217                         if (d_fadeElapsed>=d_fadeInTime)
00218                         {
00219                                 d_fading=false;
00220                                 setAlpha(d_origAlpha);
00221                         }
00222                         else
00223                         {
00224                                 setAlpha(d_origAlpha*d_fadeElapsed/d_fadeInTime);
00225                         }
00226 
00227                 }
00228 
00229         }
00230 
00231 }
00232 
00233 
00234 /*************************************************************************
00235         Sets up sizes and positions for attached ItemEntry children.
00236 *************************************************************************/
00237 void PopupMenu::layoutItemWidgets()
00238 {
00239         // get render area
00240         Rect render_rect = getItemRenderArea();
00241 
00242         // get starting position
00243         const float x0 = render_rect.d_left+d_borderWidth;
00244         float y0 = render_rect.d_top+d_borderWidth;
00245 
00246         Rect rect;
00247         Size sz(render_rect.getWidth()-d_borderWidth-d_borderWidth,0); // set item width
00248 
00249         // iterate through all items attached to this window
00250         ItemEntryList::iterator item = d_listItems.begin();
00251         while ( item != d_listItems.end() )
00252         {
00253                 // get the "optimal" height of the item and use that!
00254                 sz.d_height = (*item)->getItemPixelSize().d_height; // fix rounding errors
00255                 sz.d_height += d_vertPadding+d_vertPadding;
00256 
00257                 // set destination rect
00258                 rect.setPosition( Point(x0, y0) );
00259                 rect.setSize( sz );
00260                 (*item)->setRect(Absolute,rect);
00261 
00262                 // next position
00263                 y0 += sz.d_height + d_itemSpacing;
00264 
00265                 item++; // next item
00266         }
00267 }
00268 
00269 
00270 /*************************************************************************
00271         Returns the "optimal" size for the content in unclipped pixels
00272 *************************************************************************/
00273 Size PopupMenu::getContentSize()
00274 {
00275         // find the content sizes
00276         float widest = 0;
00277         float total_height = 0;
00278         
00279         size_t count = 0;
00280         size_t max = d_listItems.size();
00281         while (count < max)
00282         {
00283                 const Size sz = d_listItems[count]->getItemPixelSize();
00284                 if (sz.d_width > widest)
00285                         widest = sz.d_width;
00286                 total_height += sz.d_height;
00287 
00288                 count++;
00289         }
00290         
00291         const float dbl_border = d_borderWidth+d_borderWidth;
00292 
00293         // add vert padding
00294         total_height += 2.0f*count*d_vertPadding;
00295         // spacing
00296         total_height += (count-1)*d_itemSpacing;
00297         // border
00298         total_height += dbl_border;
00299 
00300         // add horz padding
00301         widest += d_horzPadding+d_horzPadding;
00302         // border
00303         widest += dbl_border;
00304 
00305         // return the content size
00306         return Size( widest, total_height );
00307 }
00308 
00309 
00310 /*************************************************************************
00311         Handler for when window alpha changes
00312 *************************************************************************/
00313 void PopupMenu::onAlphaChanged(WindowEventArgs& e)
00314 {
00315         ItemListBase::onAlphaChanged(e);
00316         
00317         // if we are not fading, this is a real alpha change request and we save a copy of the value
00318         if (!d_fading)
00319         {
00320                 d_origAlpha = d_alpha;
00321         }
00322 }
00323 
00324 
00325 /*************************************************************************
00326         Add PopupMenu specific properties
00327 *************************************************************************/
00328 void PopupMenu::addPopupMenuProperties(void)
00329 {
00330         addProperty(&d_fadeInTimeProperty);
00331     addProperty(&d_fadeOutTimeProperty);
00332 }
00333 
00334 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:33 2005 for Crazy Eddies GUI System by  doxygen 1.4.3