Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreScrollBarGuiElement.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 #include "OgreScrollBarGuiElement.h"    
00027 #include "OgreStringConverter.h"    
00028 #include "OgreGuiManager.h" 
00029 #include "OgreResource.h"   
00030 #include "OgreException.h"  
00031 
00032 
00033 namespace Ogre {
00034 
00035     String ScrollBarGuiElement::msTypeName = "ScrollBar";
00036     ScrollBarGuiElement::CmdUpButton ScrollBarGuiElement::msCmdUpButton;
00037     ScrollBarGuiElement::CmdDownButton ScrollBarGuiElement::msCmdDownButton;
00038     ScrollBarGuiElement::CmdScrollBit ScrollBarGuiElement::msCmdScrollBit;
00039 
00040     ScrollBarGuiElement::ScrollBarGuiElement(const String& name) :
00041         PanelGuiElement(name)
00042     {
00043         if (createParamDictionary("ScrollBarGuiElement"))
00044         {
00045             addBaseParameters();
00046         }
00047         mUpButton = 0;
00048         mDownButton = 0;
00049         mScrollBit = 0;
00050         mTotalItems = 0;
00051         mStartingItem = 0;
00052         mVisibilityRange = 0;
00053         mouseHeldAtY = -1;
00054 
00055         mSpacing = 0.001;
00056     }
00057 
00058     //---------------------------------------------------------------------
00059     void ScrollBarGuiElement::addBaseParameters(void)
00060     {
00061         PanelGuiElement::addBaseParameters();
00062         ParamDictionary* dict = getParamDictionary();
00063 
00064         dict->addParameter(ParameterDef("up_button", 
00065             "The template of Up Button."
00066             , PT_STRING),
00067             &ScrollBarGuiElement::msCmdUpButton);
00068 
00069         dict->addParameter(ParameterDef("down_button", 
00070             "The template of Down Button."
00071             , PT_STRING),
00072             &ScrollBarGuiElement::msCmdDownButton);
00073 
00074         dict->addParameter(ParameterDef("scroll_bit", 
00075             "The template of Scroll Bit."
00076             , PT_STRING),
00077             &ScrollBarGuiElement::msCmdScrollBit);
00078     }
00079     //---------------------------------------------------------------------
00080     // Command objects
00081     //---------------------------------------------------------------------
00082 
00083     //-----------------------------------------------------------------------
00084     String ScrollBarGuiElement::CmdUpButton::doGet(const void* target) const
00085     {
00086         return static_cast<const ScrollBarGuiElement*>(target)->getUpButtonName();
00087     }
00088     void ScrollBarGuiElement::CmdUpButton::doSet(void* target, const String& val)
00089     {
00090         std::vector<String> vec = val.split();
00091 
00092         static_cast<ScrollBarGuiElement*>(target)->setUpButtonName(val);
00093     }
00094     //-----------------------------------------------------------------------
00095     String ScrollBarGuiElement::CmdDownButton::doGet(const void* target) const
00096     {
00097         return static_cast<const ScrollBarGuiElement*>(target)->getDownButtonName();
00098     }
00099     void ScrollBarGuiElement::CmdDownButton::doSet(void* target, const String& val)
00100     {
00101         std::vector<String> vec = val.split();
00102 
00103         static_cast<ScrollBarGuiElement*>(target)->setDownButtonName(val);
00104     }
00105     //-----------------------------------------------------------------------
00106     String ScrollBarGuiElement::CmdScrollBit::doGet(const void* target) const
00107     {
00108         return static_cast<const ScrollBarGuiElement*>(target)->getScrollBitName();
00109     }
00110     void ScrollBarGuiElement::CmdScrollBit::doSet(void* target, const String& val)
00111     {
00112         std::vector<String> vec = val.split();
00113 
00114         static_cast<ScrollBarGuiElement*>(target)->setScrollBitName(val);
00115     }
00116     //-----------------------------------------------------------------------
00117 
00118     String ScrollBarGuiElement::getUpButtonName() const
00119     {
00120         return mUpButtonName;
00121     }
00122     String ScrollBarGuiElement::getDownButtonName() const
00123     {
00124         return mDownButtonName;
00125     }
00126     String ScrollBarGuiElement::getScrollBitName() const
00127     {
00128         return mScrollBitName;
00129     }
00130 //-----------------------------------------------------------------------
00131 
00132     void ScrollBarGuiElement::setUpButtonName(const String& val)
00133     {
00134         mUpButtonName = val;
00135         Real buttonSize = getWidth();
00136         mUpButton = static_cast<ButtonGuiElement*> (
00137             GuiManager::getSingleton().createGuiElementFromTemplate(mUpButtonName, "", mName + "/" + "UpButton"));
00138 
00139 
00140         // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned,
00141         // one copy when the children are copied, and another copy when setUpButtonName is set.
00142         mUpButton->setCloneable(false);
00143 
00144         addChild(mUpButton);
00145 //      mUpButton->setButtonCaption("SS/Templates/BasicText", "UP");
00146         mUpButton->addActionListener(this);
00147     }
00148     void ScrollBarGuiElement::setDownButtonName(const String& val)
00149     {
00150         Real buttonSize = getWidth();
00151         mDownButtonName = val;
00152         mDownButton = static_cast<ButtonGuiElement*> (
00153             GuiManager::getSingleton().createGuiElementFromTemplate(mDownButtonName, "", mName + "/" + "DownButton"));
00154 
00155         // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned,
00156         // one copy when the children are copied, and another copy when setDownButtonName is set.
00157         mDownButton->setCloneable(false);
00158         addChild(mDownButton);
00159 //      mDownButton->setButtonCaption("SS/Templates/BasicText", "DOWN");
00160         mDownButton->addActionListener(this);
00161     }
00162     void ScrollBarGuiElement::setScrollBitName(const String& val)
00163     {
00164         Real buttonSize = getWidth();
00165         mScrollBitName = val;
00166         mScrollBit = static_cast<PanelGuiElement*> (
00167             GuiManager::getSingleton().createGuiElementFromTemplate(mScrollBitName, "", mName + "/" + "ScrollBit"));
00168         // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned,
00169         // one copy when the children are copied, and another copy when setScrollBitName is set.
00170         mScrollBit->setCloneable(false);
00171         mScrollBit->addMouseMotionListener(this);
00172         mScrollBit->addMouseListener(this);
00173         addMouseListener(this);
00174 
00175         addChild(mScrollBit);
00176 
00177     }
00178 //-----------------------------------------------------------------------
00179 
00180     void ScrollBarGuiElement::setLimits(size_t first, size_t visibleRange, size_t total)
00181     {
00182         mTotalItems = total;
00183         mStartingItem = first;
00184         mVisibilityRange = visibleRange;
00185 
00186         layoutItems();
00187 
00188     }
00189     void ScrollBarGuiElement::layoutItems()
00190     {
00191         Real buttonWidth = getWidth() - (mSpacing * 2);
00192         Real buttonHeight = (buttonWidth * 4.0F) / 3.0F;    // adjust for screen ratio
00193         Real horzSpacing = mSpacing;
00194         Real vertSpacing = (mSpacing * 4.0F) / 3.0F;
00195         Real bitTop    = buttonHeight + vertSpacing;
00196         Real bitHeight = getHeight() - (2 * bitTop);
00197 
00198         mUpButton->setLeft(horzSpacing);
00199         mUpButton->setTop(vertSpacing);
00200         mUpButton->setWidth(buttonWidth);
00201         mUpButton->setHeight(buttonHeight); // buttons are square
00202 
00203         mDownButton->setLeft(horzSpacing);
00204         mDownButton->setTop(getHeight() - (buttonHeight + vertSpacing));
00205         mDownButton->setWidth(buttonWidth);
00206         mDownButton->setHeight(buttonHeight);   // buttons are square
00207 
00208         mScrollBit->setLeft(horzSpacing);
00209         mScrollBit->setTop(buttonHeight + vertSpacing);
00210         mScrollBit->setWidth(buttonWidth);
00211 
00212         if (mTotalItems == 0)
00213         {
00214             mScrollBit->setTop(bitTop);
00215             mScrollBit->setHeight(bitHeight);           
00216         }
00217         else
00218         {
00219             mScrollBit->setTop(bitTop + (bitHeight * ((Real)mStartingItem / (Real)mTotalItems)));
00220             mScrollBit->setHeight(bitHeight * ((Real)mVisibilityRange / (Real)mTotalItems));
00221         }
00222     }
00223 
00224     void ScrollBarGuiElement::updateScrollBit()
00225     {
00226         Real buttonWidth = getWidth() - (mSpacing * 2);
00227         Real buttonHeight = buttonWidth * (4.0F / 3.0F);    // adjust for screen ratio
00228         Real vertSpacing = mSpacing * (4.0F / 3.0F);
00229     Real bitTop    = buttonHeight + vertSpacing;
00230     Real bitHeight = getHeight() - (2 * bitTop);
00231 
00232         if (mTotalItems == 0)
00233         {
00234             mScrollBit->setTop(bitTop);
00235         }
00236         else
00237         {
00238             mScrollBit->setTop(bitTop + (bitHeight * ((Real)mStartingItem / (Real)mTotalItems)));
00239         }
00240     }
00241 
00242     //---------------------------------------------------------------------
00243     const String& ScrollBarGuiElement::getTypeName(void) const
00244     {
00245         return msTypeName;
00246     }
00247 
00248     void ScrollBarGuiElement::actionPerformed(ActionEvent* e) 
00249     {
00250         if (e->getActionCommand() == mUpButton->getName())
00251         {
00252             if (mStartingItem >0)
00253             {
00254                 mStartingItem--;
00255         updateScrollBit();
00256                 fireScrollPerformed();
00257             }
00258         }
00259         else if (e->getActionCommand() == mDownButton->getName())
00260         {
00261             if (mStartingItem < mTotalItems-mVisibilityRange)
00262             {
00263                 mStartingItem++;
00264         updateScrollBit();
00265                 fireScrollPerformed();
00266             }
00267         }
00268 
00269 
00270     }
00271     //-----------------------------------------------------------------------
00272     void ScrollBarGuiElement::fireScrollPerformed()
00273     {
00274         ScrollEvent* se = new ScrollEvent(this, ScrollEvent::SE_SCROLL_PERFORMED, 0, 0, mStartingItem, mVisibilityRange, mTotalItems);
00275         processEvent(se);
00276         delete se;
00277     }
00278 
00279     //-----------------------------------------------------------------------
00280     void ScrollBarGuiElement::processEvent(InputEvent* e) 
00281     {
00282         PanelGuiElement::processEvent(e);
00283 
00284         if (!e->isConsumed())
00285         {
00286             switch(e->getID()) 
00287             {
00288             case ScrollEvent::SE_SCROLL_PERFORMED:
00289                 processScrollEvent(static_cast<ScrollEvent*>(e));
00290                 break;
00291             default:
00292                 break;
00293             }
00294         }
00295     }
00296     void ScrollBarGuiElement::mouseMoved(MouseEvent* e)
00297     {
00298 
00299 
00300     }
00301     void ScrollBarGuiElement::mouseDragged(MouseEvent* e)
00302     {
00303 
00304         if (mouseHeldAtY == -1)
00305         {
00306             int err =1;
00307 
00308         }
00309         else
00310         {
00311             Real moveY = e->getY() - mouseHeldAtY + mScrollBit->getTop();
00312             moveScrollBitTo(moveY);
00313         }
00314 
00315     }
00316     void ScrollBarGuiElement::mousePressed(MouseEvent* e) 
00317     {
00318         Real buttonHeight = (getWidth() * 4.0F) / 3.0F; // adjust for screen ratio
00319         Real vertSpacing = (mSpacing * 4.0F) / 3.0F;
00320 
00321         Real mouseY = e->getY() - mDerivedTop;
00322         if ((MouseTarget*)e->getSource() == (GuiElement*)(mScrollBit))
00323         {
00324             mouseHeldAtY = mouseY;
00325         }
00326         else if ((MouseTarget*)e->getSource() == (GuiElement*)this)
00327         {
00328             Real topToScroll = mouseY;
00329             if (mouseY > mScrollBit->getTop())
00330             {
00331                 // always take scroll point from the top of scrollBit
00332                 topToScroll -= mScrollBit->getHeight();
00333 
00334             }
00335 
00336             moveScrollBitTo(topToScroll - buttonHeight + vertSpacing);
00337 
00338             mouseHeldAtY = mouseY;
00339 
00340         }
00341 
00342     }
00343     void ScrollBarGuiElement::mouseReleased(MouseEvent* e) 
00344     {
00345         mouseHeldAtY = -1;
00346 
00347     }
00348     void ScrollBarGuiElement::scrollToIndex(size_t index)
00349     {
00350         if (index >= mStartingItem + mVisibilityRange)
00351         {
00352             // scroll down
00353             mStartingItem = index - mVisibilityRange;
00354         }
00355         else if (index < mStartingItem)
00356         {
00357             // scroll up
00358             mStartingItem = index;
00359         }
00360         layoutItems();
00361         fireScrollPerformed();
00362     }
00363 
00364     void ScrollBarGuiElement::moveScrollBitTo(Real moveY)
00365     {
00366         Real buttonHeight = (getWidth() * 4.0F) / 3.0F; // adjust for screen ratio
00367         Real vertSpacing = (mSpacing * 4.0F) / 3.0F;
00368 
00369         if (moveY <0)
00370         {
00371             moveY = 0;
00372         }
00373         if (moveY > getHeight() - 2*buttonHeight - vertSpacing*2 - mScrollBit->getHeight())
00374         {
00375             moveY = getHeight() - 2*buttonHeight - vertSpacing*2 - mScrollBit->getHeight();
00376         }
00377         mScrollBit->setTop(buttonHeight + vertSpacing + moveY);
00378         mStartingItem = ((mScrollBit->getTop() - buttonHeight - vertSpacing) * mTotalItems) / (getHeight() - 2*buttonHeight - vertSpacing*2);
00379         fireScrollPerformed();
00380     }
00381 }
00382 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:27 2004