guiTextEditSliderCtrl.cpp
Engine/source/gui/controls/guiTextEditSliderCtrl.cpp
Public Functions
ConsoleDocClass(GuiTextEditSliderCtrl , "@brief GUI Control which displays <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> numerical <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> which can be increased or " "decreased using <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> pair of <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">arrows.\n\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguitexteditsliderctrl/">GuiTextEditSliderCtrl</a>()\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " format = \"%3.2f\";\n" " range = \"-1e+03 1e+03\";\n" " increment = \"0.1\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " focusOnMouseWheel = \"0\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " //Properties not specific <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> this <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> have been omitted from this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example.\n</a>" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiTextEditCtrl\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiCore\n</a>" )
Detailed Description
Public Functions
ConsoleDocClass(GuiTextEditSliderCtrl , "@brief GUI Control which displays <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> numerical <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> which can be increased or " "decreased using <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> pair of <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">arrows.\n\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguitexteditsliderctrl/">GuiTextEditSliderCtrl</a>()\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " format = \"%3.2f\";\n" " range = \"-1e+03 1e+03\";\n" " increment = \"0.1\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " focusOnMouseWheel = \"0\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " //Properties not specific <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> this <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> have been omitted from this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example.\n</a>" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiTextEditCtrl\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiCore\n</a>" )
IMPLEMENT_CONOBJECT(GuiTextEditSliderCtrl )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "gui/controls/guiTextEditSliderCtrl.h" 26 27#include "console/consoleTypes.h" 28#include "console/console.h" 29#include "gui/core/guiCanvas.h" 30#include "gfx/gfxDevice.h" 31#include "gfx/gfxDrawUtil.h" 32#include "console/engineAPI.h" 33 34IMPLEMENT_CONOBJECT(GuiTextEditSliderCtrl); 35 36ConsoleDocClass( GuiTextEditSliderCtrl, 37 "@brief GUI Control which displays a numerical value which can be increased or " 38 "decreased using a pair of arrows.\n\n" 39 40 "@tsexample\n" 41 "new GuiTextEditSliderCtrl()\n" 42 "{\n" 43 " format = \"%3.2f\";\n" 44 " range = \"-1e+03 1e+03\";\n" 45 " increment = \"0.1\";\n" 46 " focusOnMouseWheel = \"0\";\n" 47 " //Properties not specific to this control have been omitted from this example.\n" 48 "};\n" 49 "@endtsexample\n\n" 50 51 "@see GuiTextEditCtrl\n\n" 52 53 "@ingroup GuiCore\n" 54); 55 56GuiTextEditSliderCtrl::GuiTextEditSliderCtrl() 57{ 58 mRange.set(0.0f, 1.0f); 59 mIncAmount = 1.0f; 60 mValue = 0.0f; 61 mMulInc = 0; 62 mIncCounter = 0.0f; 63 mFormat = StringTable->insert("%3.2f"); 64 mTextAreaHit = None; 65 mFocusOnMouseWheel = false; 66 mMouseDownTime = 0.0f; 67} 68 69GuiTextEditSliderCtrl::~GuiTextEditSliderCtrl() 70{ 71} 72 73void GuiTextEditSliderCtrl::initPersistFields() 74{ 75 addField("format", TypeString, Offset(mFormat, GuiTextEditSliderCtrl), "Character format type to place in the control.\n"); 76 addField("range", TypePoint2F, Offset(mRange, GuiTextEditSliderCtrl), "Maximum vertical and horizontal range to allow in the control.\n"); 77 addField("increment", TypeF32, Offset(mIncAmount, GuiTextEditSliderCtrl), "How far to increment the slider on each step.\n"); 78 addField("focusOnMouseWheel", TypeBool, Offset(mFocusOnMouseWheel, GuiTextEditSliderCtrl), "If true, the control will accept giving focus to the user when the mouse wheel is used.\n"); 79 80 Parent::initPersistFields(); 81} 82 83void GuiTextEditSliderCtrl::getText(char *dest) 84{ 85 Parent::getText(dest); 86} 87 88void GuiTextEditSliderCtrl::setText(const char *txt) 89{ 90 mValue = dAtof(txt); 91 checkRange(); 92 setValue(); 93} 94 95bool GuiTextEditSliderCtrl::onKeyDown(const GuiEvent &event) 96{ 97 return Parent::onKeyDown(event); 98} 99 100void GuiTextEditSliderCtrl::checkRange() 101{ 102 if(mValue < mRange.x) 103 mValue = mRange.x; 104 else if(mValue > mRange.y) 105 mValue = mRange.y; 106} 107 108void GuiTextEditSliderCtrl::setValue() 109{ 110 char buf[20]; 111 // For some reason this sprintf is failing to convert 112 // a floating point number to anything with %d, so cast it. 113 if( dStricmp( mFormat, "%d" ) == 0 ) 114 dSprintf(buf,sizeof(buf),mFormat, (S32)mValue); 115 else 116 dSprintf(buf,sizeof(buf),mFormat, mValue); 117 Parent::setText(buf); 118} 119 120void GuiTextEditSliderCtrl::onMouseDown(const GuiEvent &event) 121{ 122 // If we're not active then skip out. 123 if ( !mActive || !mAwake || !mVisible ) 124 { 125 Parent::onMouseDown(event); 126 return; 127 } 128 129 char txt[20]; 130 Parent::getText(txt); 131 mValue = dAtof(txt); 132 133 mMouseDownTime = Sim::getCurrentTime(); 134 GuiControl *parent = getParent(); 135 if(!parent) 136 return; 137 Point2I camPos = event.mousePoint; 138 Point2I point = parent->localToGlobalCoord(getPosition()); 139 140 if(camPos.x > point.x + getExtent().x - 14) 141 { 142 if(camPos.y > point.y + (getExtent().y/2)) 143 { 144 mValue -=<a href="/coding/class/classguitexteditsliderctrl/#classguitexteditsliderctrl_1ac38769d6332a50f96cfc8087c39b1e9e">mIncAmount</a>; 145 mTextAreaHit = ArrowDown; 146 mMulInc = -0.15f; 147 } 148 else 149 { 150 mValue +=<a href="/coding/class/classguitexteditsliderctrl/#classguitexteditsliderctrl_1ac38769d6332a50f96cfc8087c39b1e9e">mIncAmount</a>; 151 mTextAreaHit = ArrowUp; 152 mMulInc = 0.15f; 153 } 154 155 checkRange(); 156 setValue(); 157 mouseLock(); 158 159 // We should get the focus and set the 160 // cursor to the start of the text to 161 // mimic the standard Windows behavior. 162 setFirstResponder(); 163 mCursorPos = mBlockStart = mBlockEnd = 0; 164 setUpdate(); 165 166 return; 167 } 168 169 Parent::onMouseDown(event); 170} 171 172void GuiTextEditSliderCtrl::onMouseDragged(const GuiEvent &event) 173{ 174 // If we're not active then skip out. 175 if ( !mActive || !mAwake || !mVisible ) 176 { 177 Parent::onMouseDragged(event); 178 return; 179 } 180 181 if(mTextAreaHit == None || mTextAreaHit == Slider) 182 { 183 mTextAreaHit = Slider; 184 GuiControl *parent = getParent(); 185 if(!parent) 186 return; 187 Point2I camPos = event.mousePoint; 188 Point2I point = parent->localToGlobalCoord(getPosition()); 189 F32 maxDis = 100; 190 F32 val; 191 if(camPos.y < point.y) 192 { 193 if((F32)point.y < maxDis) 194 maxDis = (F32)point.y; 195 196 val = point.y - maxDis; 197 198 if(point.y > 0) 199 mMulInc= 1.0f-(((float)camPos.y - val) / maxDis); 200 else 201 mMulInc = 1.0f; 202 203 checkIncValue(); 204 205 return; 206 } 207 else if(camPos.y > point.y + getExtent().y) 208 { 209 GuiCanvas *root = getRoot(); 210 val = (F32)(root->getHeight() - (point.y + getHeight())); 211 if(val < maxDis) 212 maxDis = val; 213 if( val > 0) 214 mMulInc= -(F32)(camPos.y - (point.y + getHeight()))/maxDis; 215 else 216 mMulInc = -1.0f; 217 checkIncValue(); 218 return; 219 } 220 mTextAreaHit = None; 221 Parent::onMouseDragged(event); 222 } 223} 224 225void GuiTextEditSliderCtrl::onMouseUp(const GuiEvent &event) 226{ 227 // If we're not active then skip out. 228 if ( !mActive || !mAwake || !mVisible ) 229 { 230 Parent::onMouseUp(event); 231 return; 232 } 233 234 mMulInc = 0.0f; 235 mouseUnlock(); 236 237 if ( mTextAreaHit != None ) 238 selectAllText(); 239 240 //if we released the mouse within this control, then the parent will call 241 //the mConsoleCommand other wise we have to call it. 242 Parent::onMouseUp(event); 243 244 //if we didn't release the mouse within this control, then perform the action 245 // if (!cursorInControl()) 246 execConsoleCallback(); 247 execAltConsoleCallback(); 248 249 //Set the cursor position to where the user clicked 250 mCursorPos = calculateCursorPos( event.mousePoint ); 251 252 mTextAreaHit = None; 253} 254 255bool GuiTextEditSliderCtrl::onMouseWheelUp(const GuiEvent &event) 256{ 257 if ( !mActive || !mAwake || !mVisible ) 258 return Parent::onMouseWheelUp(event); 259 260 if ( !isFirstResponder() && !mFocusOnMouseWheel ) 261 { 262 GuiControl *parent = getParent(); 263 if ( parent ) 264 return parent->onMouseWheelUp( event ); 265 266 return false; 267 } 268 269 mValue += mIncAmount; 270 271 checkRange(); 272 setValue(); 273 274 setFirstResponder(); 275 mCursorPos = mBlockStart = mBlockEnd = 0; 276 setUpdate(); 277 278 return true; 279} 280 281bool GuiTextEditSliderCtrl::onMouseWheelDown(const GuiEvent &event) 282{ 283 if ( !mActive || !mAwake || !mVisible ) 284 return Parent::onMouseWheelDown(event); 285 286 if ( !isFirstResponder() && !mFocusOnMouseWheel ) 287 { 288 GuiControl *parent = getParent(); 289 if ( parent ) 290 return parent->onMouseWheelUp( event ); 291 292 return false; 293 } 294 295 mValue -= mIncAmount; 296 297 checkRange(); 298 setValue(); 299 300 setFirstResponder(); 301 mCursorPos = mBlockStart = mBlockEnd = 0; 302 setUpdate(); 303 304 return true; 305} 306 307void GuiTextEditSliderCtrl::checkIncValue() 308{ 309 if(mMulInc > 1.0f) 310 mMulInc = 1.0f; 311 else if(mMulInc < -1.0f) 312 mMulInc = -1.0f; 313} 314 315void GuiTextEditSliderCtrl::timeInc(U32 elapseTime) 316{ 317 S32 numTimes = elapseTime / 750; 318 if(mTextAreaHit != Slider && numTimes > 0) 319 { 320 if(mTextAreaHit == ArrowUp) 321 mMulInc = 0.15f * numTimes; 322 else 323 mMulInc = -0.15f * numTimes; 324 325 checkIncValue(); 326 } 327} 328 329void GuiTextEditSliderCtrl::onRender(Point2I offset, const RectI &updateRect) 330{ 331 if(mTextAreaHit != None) 332 { 333 U32 elapseTime = Sim::getCurrentTime() - mMouseDownTime; 334 if(elapseTime > 750 || mTextAreaHit == Slider) 335 { 336 timeInc(elapseTime); 337 mIncCounter += mMulInc; 338 if(mIncCounter >= 1.0f || mIncCounter <= -1.0f) 339 { 340 mValue = (mMulInc > 0.0f) ? mValue+mIncAmount : mValue-mIncAmount; 341 mIncCounter = (mIncCounter > 0.0f) ? mIncCounter-1 : mIncCounter+1; 342 checkRange(); 343 setValue(); 344 mCursorPos = 0; 345 } 346 } 347 } 348 Parent::onRender(offset, updateRect); 349 350 Point2I start(offset.x + getWidth() - 14, offset.y); 351 Point2I midPoint(start.x + 7, start.y + (getExtent().y/2)); 352 353 GFX->getDrawUtil()->drawRectFill(Point2I(start.x+1,start.y+1), Point2I(start.x+13,start.y+getExtent().y-1) , mProfile->mFillColor); 354 355 GFX->getDrawUtil()->drawLine(start, Point2I(start.x, start.y+getExtent().y),mProfile->mFontColor); 356 GFX->getDrawUtil()->drawLine(Point2I(start.x,midPoint.y), 357 Point2I(start.x+14,midPoint.y), 358 mProfile->mFontColor); 359 360 GFXVertexBufferHandle<GFXVertexPCT> verts(GFX, 6, GFXBufferTypeVolatile); 361 verts.lock(); 362 363 verts[0].color.set( 0, 0, 0 ); 364 verts[1].color.set( 0, 0, 0 ); 365 verts[2].color.set( 0, 0, 0 ); 366 verts[3].color.set( 0, 0, 0 ); 367 verts[4].color.set( 0, 0, 0 ); 368 verts[5].color.set( 0, 0, 0 ); 369 370 if(mTextAreaHit == ArrowUp) 371 { 372 verts[0].point.set( (F32)midPoint.x, (F32)start.y + 1.0f, 0.0f ); 373 verts[1].point.set( (F32)start.x + 11.0f, (F32)midPoint.y - 2.0f, 0.0f ); 374 verts[2].point.set( (F32)start.x + 3.0f, (F32)midPoint.y - 2.0f, 0.0f ); 375 } 376 else 377 { 378 verts[0].point.set( (F32)midPoint.x, (F32)start.y + 2.0f, 0.0f ); 379 verts[1].point.set( (F32)start.x + 11.0f, (F32)midPoint.y - 1.0f, 0.0f ); 380 verts[2].point.set( (F32)start.x + 3.0f, (F32)midPoint.y - 1.0f, 0.0f ); 381 } 382 383 if(mTextAreaHit == ArrowDown) 384 { 385 verts[3].point.set( (F32)midPoint.x, (F32)(start.y + getExtent().y - 1), 0.0f ); 386 verts[4].point.set( (F32)start.x + 11.0f, (F32)midPoint.y + 3.0f, 0.0f ); 387 verts[5].point.set( (F32)start.x + 3.0f, (F32)midPoint.y + 3.0f, 0.0f ); 388 } 389 else 390 { 391 verts[3].point.set( (F32)midPoint.x, (F32)(start.y + getExtent().y - 2), 0.0f ); 392 verts[4].point.set( (F32)start.x + 11.0f, (F32)midPoint.y + 2.0f, 0.0f ); 393 verts[5].point.set( (F32)start.x + 3.0f, (F32)midPoint.y + 2.0f, 0.0f ); 394 } 395 396 verts.unlock(); 397 398 GFX->setVertexBuffer( verts ); 399 GFX->setupGenericShaders(); 400 GFX->drawPrimitive( GFXTriangleList, 0, 2 ); 401} 402 403void GuiTextEditSliderCtrl::onPreRender() 404{ 405 if (isFirstResponder()) 406 { 407 U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped; 408 mNumFramesElapsed++; 409 if ((timeElapsed > 500) && (mNumFramesElapsed > 3)) 410 { 411 mCursorOn = !mCursorOn; 412 mTimeLastCursorFlipped = Sim::getCurrentTime(); 413 mNumFramesElapsed = 0; 414 setUpdate(); 415 } 416 417 //update the cursor if the text is scrolling 418 if (mDragHit) 419 { 420 if ((mScrollDir < 0) && (mCursorPos > 0)) 421 { 422 mCursorPos--; 423 } 424 else if ((mScrollDir > 0) && (mCursorPos < (S32)dStrlen(mText))) 425 { 426 mCursorPos++; 427 } 428 } 429 } 430} 431 432 433