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 static Real mouseDragBitOffset = 0; 00040 00041 ScrollBarGuiElement::ScrollBarGuiElement(const String& name) : 00042 PanelGuiElement(name) 00043 { 00044 if (createParamDictionary("ScrollBarGuiElement")) 00045 { 00046 addBaseParameters(); 00047 } 00048 mUpButton = 0; 00049 mDownButton = 0; 00050 mScrollBit = 0; 00051 mTotalItems = 0; 00052 mStartingItem = 0; 00053 mVisibilityRange = 0; 00054 mouseHeldAtY = -1; 00055 00056 mSpacing = 0.001; 00057 } 00058 00059 //--------------------------------------------------------------------- 00060 void ScrollBarGuiElement::addBaseParameters(void) 00061 { 00062 PanelGuiElement::addBaseParameters(); 00063 ParamDictionary* dict = getParamDictionary(); 00064 00065 dict->addParameter(ParameterDef("up_button", 00066 "The template of Up Button." 00067 , PT_STRING), 00068 &ScrollBarGuiElement::msCmdUpButton); 00069 00070 dict->addParameter(ParameterDef("down_button", 00071 "The template of Down Button." 00072 , PT_STRING), 00073 &ScrollBarGuiElement::msCmdDownButton); 00074 00075 dict->addParameter(ParameterDef("scroll_bit", 00076 "The template of Scroll Bit." 00077 , PT_STRING), 00078 &ScrollBarGuiElement::msCmdScrollBit); 00079 } 00080 //--------------------------------------------------------------------- 00081 // Command objects 00082 //--------------------------------------------------------------------- 00083 00084 //----------------------------------------------------------------------- 00085 String ScrollBarGuiElement::CmdUpButton::doGet(const void* target) const 00086 { 00087 return static_cast<const ScrollBarGuiElement*>(target)->getUpButtonName(); 00088 } 00089 void ScrollBarGuiElement::CmdUpButton::doSet(void* target, const String& val) 00090 { 00091 std::vector<String> vec = val.split(); 00092 00093 static_cast<ScrollBarGuiElement*>(target)->setUpButtonName(val); 00094 } 00095 //----------------------------------------------------------------------- 00096 String ScrollBarGuiElement::CmdDownButton::doGet(const void* target) const 00097 { 00098 return static_cast<const ScrollBarGuiElement*>(target)->getDownButtonName(); 00099 } 00100 void ScrollBarGuiElement::CmdDownButton::doSet(void* target, const String& val) 00101 { 00102 std::vector<String> vec = val.split(); 00103 00104 static_cast<ScrollBarGuiElement*>(target)->setDownButtonName(val); 00105 } 00106 //----------------------------------------------------------------------- 00107 String ScrollBarGuiElement::CmdScrollBit::doGet(const void* target) const 00108 { 00109 return static_cast<const ScrollBarGuiElement*>(target)->getScrollBitName(); 00110 } 00111 void ScrollBarGuiElement::CmdScrollBit::doSet(void* target, const String& val) 00112 { 00113 std::vector<String> vec = val.split(); 00114 00115 static_cast<ScrollBarGuiElement*>(target)->setScrollBitName(val); 00116 } 00117 //----------------------------------------------------------------------- 00118 00119 String ScrollBarGuiElement::getUpButtonName() const 00120 { 00121 return mUpButtonName; 00122 } 00123 String ScrollBarGuiElement::getDownButtonName() const 00124 { 00125 return mDownButtonName; 00126 } 00127 String ScrollBarGuiElement::getScrollBitName() const 00128 { 00129 return mScrollBitName; 00130 } 00131 //----------------------------------------------------------------------- 00132 00133 void ScrollBarGuiElement::setUpButtonName(const String& val) 00134 { 00135 mUpButtonName = val; 00136 Real buttonSize = getWidth(); 00137 mUpButton = static_cast<ButtonGuiElement*> ( 00138 GuiManager::getSingleton().createGuiElementFromTemplate(mUpButtonName, "", mName + "/" + "UpButton")); 00139 00140 00141 // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned, 00142 // one copy when the children are copied, and another copy when setUpButtonName is set. 00143 mUpButton->setCloneable(false); 00144 00145 addChild(mUpButton); 00146 // mUpButton->setButtonCaption("SS/Templates/BasicText", "UP"); 00147 mUpButton->addActionListener(this); 00148 } 00149 void ScrollBarGuiElement::setDownButtonName(const String& val) 00150 { 00151 Real buttonSize = getWidth(); 00152 mDownButtonName = val; 00153 mDownButton = static_cast<ButtonGuiElement*> ( 00154 GuiManager::getSingleton().createGuiElementFromTemplate(mDownButtonName, "", mName + "/" + "DownButton")); 00155 00156 // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned, 00157 // one copy when the children are copied, and another copy when setDownButtonName is set. 00158 mDownButton->setCloneable(false); 00159 addChild(mDownButton); 00160 // mDownButton->setButtonCaption("SS/Templates/BasicText", "DOWN"); 00161 mDownButton->addActionListener(this); 00162 } 00163 void ScrollBarGuiElement::setScrollBitName(const String& val) 00164 { 00165 Real buttonSize = getWidth(); 00166 mScrollBitName = val; 00167 mScrollBit = static_cast<PanelGuiElement*> ( 00168 GuiManager::getSingleton().createGuiElementFromTemplate(mScrollBitName, "", mName + "/" + "ScrollBit")); 00169 // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned, 00170 // one copy when the children are copied, and another copy when setScrollBitName is set. 00171 mScrollBit->setCloneable(false); 00172 mScrollBit->addMouseMotionListener(this); 00173 mScrollBit->addMouseListener(this); 00174 addMouseListener(this); 00175 00176 addChild(mScrollBit); 00177 00178 } 00179 //----------------------------------------------------------------------- 00180 00181 void ScrollBarGuiElement::setLimits(size_t first, size_t visibleRange, size_t total) 00182 { 00183 mTotalItems = total; 00184 mStartingItem = first; 00185 mVisibilityRange = visibleRange; 00186 00187 layoutItems(); 00188 00189 } 00190 void ScrollBarGuiElement::layoutItems() 00191 { 00192 Real buttonWidth = getWidth() - (mSpacing * 2); 00193 Real buttonHeight = (buttonWidth * 4.0F) / 3.0F; // adjust for screen ratio 00194 Real horzSpacing = mSpacing; 00195 Real vertSpacing = (mSpacing * 4.0F) / 3.0F; 00196 Real bitTop = buttonHeight + vertSpacing; 00197 Real bitHeight = getHeight() - (2 * bitTop); 00198 00199 mUpButton->setLeft(horzSpacing); 00200 mUpButton->setTop(vertSpacing); 00201 mUpButton->setWidth(buttonWidth); 00202 mUpButton->setHeight(buttonHeight); // buttons are square 00203 00204 mDownButton->setLeft(horzSpacing); 00205 mDownButton->setTop(getHeight() - (buttonHeight + vertSpacing)); 00206 mDownButton->setWidth(buttonWidth); 00207 mDownButton->setHeight(buttonHeight); // buttons are square 00208 00209 mScrollBit->setLeft(horzSpacing); 00210 mScrollBit->setTop(buttonHeight + vertSpacing); 00211 mScrollBit->setWidth(buttonWidth); 00212 00213 if (mTotalItems == 0) 00214 { 00215 mScrollBit->setTop(bitTop); 00216 mScrollBit->setHeight(bitHeight); 00217 } 00218 else 00219 { 00220 mScrollBit->setTop(bitTop + (bitHeight * ((Real)mStartingItem / (Real)mTotalItems))); 00221 mScrollBit->setHeight(bitHeight * ((Real)mVisibilityRange / (Real)mTotalItems)); 00222 } 00223 } 00224 00225 void ScrollBarGuiElement::updateScrollBit() 00226 { 00227 Real buttonWidth = getWidth() - (mSpacing * 2); 00228 Real buttonHeight = buttonWidth * (4.0F / 3.0F); // adjust for screen ratio 00229 Real vertSpacing = mSpacing * (4.0F / 3.0F); 00230 Real bitTop = buttonHeight + vertSpacing; 00231 Real bitHeight = getHeight() - (2 * bitTop); 00232 00233 if (mTotalItems == 0) 00234 { 00235 mScrollBit->setTop(bitTop); 00236 } 00237 else 00238 { 00239 mScrollBit->setTop(bitTop + (bitHeight * ((Real)mStartingItem / (Real)mTotalItems))); 00240 } 00241 } 00242 00243 //--------------------------------------------------------------------- 00244 const String& ScrollBarGuiElement::getTypeName(void) const 00245 { 00246 return msTypeName; 00247 } 00248 00249 void ScrollBarGuiElement::actionPerformed(ActionEvent* e) 00250 { 00251 if (e->getActionCommand() == mUpButton->getName()) 00252 { 00253 if (mStartingItem >0) 00254 { 00255 mStartingItem--; 00256 updateScrollBit(); 00257 fireScrollPerformed(); 00258 } 00259 } 00260 else if (e->getActionCommand() == mDownButton->getName()) 00261 { 00262 if (mStartingItem < mTotalItems-mVisibilityRange) 00263 { 00264 mStartingItem++; 00265 updateScrollBit(); 00266 fireScrollPerformed(); 00267 } 00268 } 00269 00270 00271 } 00272 //----------------------------------------------------------------------- 00273 void ScrollBarGuiElement::fireScrollPerformed() 00274 { 00275 ScrollEvent* se = new ScrollEvent(this, ScrollEvent::SE_SCROLL_PERFORMED, 0, 0, mStartingItem, mVisibilityRange, mTotalItems); 00276 processEvent(se); 00277 delete se; 00278 } 00279 00280 //----------------------------------------------------------------------- 00281 void ScrollBarGuiElement::processEvent(InputEvent* e) 00282 { 00283 PanelGuiElement::processEvent(e); 00284 00285 if (!e->isConsumed()) 00286 { 00287 switch(e->getID()) 00288 { 00289 case ScrollEvent::SE_SCROLL_PERFORMED: 00290 processScrollEvent(static_cast<ScrollEvent*>(e)); 00291 break; 00292 default: 00293 break; 00294 } 00295 } 00296 } 00297 void ScrollBarGuiElement::mouseMoved(MouseEvent* e) 00298 { 00299 00300 00301 } 00302 void ScrollBarGuiElement::mouseDragged(MouseEvent* e) 00303 { 00304 if ( mouseHeldAtY == -1 || mouseDragBitOffset == -1 ) 00305 { 00306 int err =1; 00307 } 00308 else 00309 { 00310 Real buttonHeight = (getWidth() * 4.0F) / 3.0F; // adjust for screen ratio 00311 Real vertSpacing = (mSpacing * 4.0F) / 3.0F; 00312 Real moveY = (e->getY()-getTop()-vertSpacing-buttonHeight) - mouseDragBitOffset; 00313 moveScrollBitTo(moveY); 00314 } 00315 00316 } 00317 void ScrollBarGuiElement::mousePressed(MouseEvent* e) 00318 { 00319 Real buttonHeight = (getWidth() * 4.0F) / 3.0F; // adjust for screen ratio 00320 Real vertSpacing = (mSpacing * 4.0F) / 3.0F; 00321 00322 Real mouseY = e->getY() - mDerivedTop; 00323 if ((MouseTarget*)e->getSource() == (GuiElement*)(mScrollBit)) 00324 { 00325 mouseHeldAtY = mouseY; 00326 mouseDragBitOffset = e->getY() - getTop() - mScrollBit->getTop(); 00327 } 00328 else if ((MouseTarget*)e->getSource() == (GuiElement*)this) 00329 { 00330 size_t newStartingItem = (int)mStartingItem; 00331 if ( mouseY < mScrollBit->getTop() ) 00332 { 00333 if ( newStartingItem < 5 ) 00334 newStartingItem = 0; 00335 else 00336 newStartingItem -= 5; 00337 } 00338 else 00339 { 00340 newStartingItem += 5; 00341 size_t maxStartingItem = mTotalItems - mVisibilityRange; 00342 if ( newStartingItem > maxStartingItem ) 00343 newStartingItem = maxStartingItem; 00344 } 00345 if ( newStartingItem != mStartingItem ) 00346 { 00347 mStartingItem = newStartingItem; 00348 updateScrollBit(); 00349 fireScrollPerformed(); 00350 } 00351 /*Real topToScroll = mouseY; 00352 if (mouseY > mScrollBit->getTop()) 00353 { 00354 // always take scroll point from the top of scrollBit 00355 topToScroll -= mScrollBit->getHeight(); 00356 00357 } 00358 00359 moveScrollBitTo(topToScroll - buttonHeight + vertSpacing);*/ 00360 00361 //mouseHeldAtY = mouseY; 00362 } 00363 } 00364 void ScrollBarGuiElement::mouseReleased(MouseEvent* e) 00365 { 00366 mouseHeldAtY = -1; 00367 mouseDragBitOffset = -1; 00368 } 00369 void ScrollBarGuiElement::scrollToIndex(size_t index) 00370 { 00371 if (index >= mStartingItem + mVisibilityRange) 00372 { 00373 // scroll down 00374 mStartingItem = index - mVisibilityRange; 00375 } 00376 else if (index < mStartingItem) 00377 { 00378 // scroll up 00379 mStartingItem = index; 00380 } 00381 layoutItems(); 00382 fireScrollPerformed(); 00383 } 00384 00385 void ScrollBarGuiElement::moveScrollBitTo(Real moveY) 00386 { 00387 Real buttonHeight = (getWidth() * 4.0F) / 3.0F; // adjust for screen ratio 00388 Real vertSpacing = (mSpacing * 4.0F) / 3.0F; 00389 00390 if (moveY <0) 00391 { 00392 moveY = 0; 00393 } 00394 Real maxY = getHeight() - buttonHeight - vertSpacing*2.f - mScrollBit->getHeight(); 00395 if (moveY > maxY) 00396 { 00397 moveY = maxY; 00398 } 00399 mScrollBit->setTop(buttonHeight + vertSpacing + moveY); 00400 mStartingItem = ((mScrollBit->getTop() - buttonHeight - vertSpacing) * mTotalItems) / (getHeight() - buttonHeight - vertSpacing*2); 00401 fireScrollPerformed(); 00402 } 00403 } 00404
Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:45 2004