Description : |
Below is the source for a calculator, written completely using DAZ Script' intrinsic types |
Concepts Covered : |
|
Source : ./samples/ |
00001 // DAZ Studio version 3.0 filetype DAZ Script 00002 /********************************************************************** 00003 File: calculator.dsa 00004 00005 Copyright © 2002-2009 DAZ Productions. All Rights Reserved. 00006 00007 This file is part of the DAZ Script Documentation. 00008 00009 This file may be used only in accordance with the DAZ Script 00010 license provided with the DAZ Script Documentation. 00011 00012 The contents of this file may not be disclosed to third parties, 00013 copied or duplicated in any form, in whole or in part, without the 00014 prior written permission of DAZ Productions, except as explicitly 00015 allowed in the DAZ Script license. 00016 00017 See http://www.daz3d.com to contact DAZ Productions or for more 00018 information about DAZ Script. 00019 **********************************************************************/ 00020 /***************************** 00021 Script globals 00022 *****************************/ 00023 var g_oFILE = new DzFile( getScriptFileName() ); 00024 var g_sSCRIPT_NAME = String( "%1.%2" ).arg( g_oFILE.baseName() ).arg( g_oFILE.extension() ); 00025 00026 var g_bSHIFT_PRESSED = shiftPressed(); 00027 var g_bCONTROL_PRESSED = ctrlPressed(); 00028 00029 var g_nMARGIN = 5; 00030 var g_nSPACING = 5; 00031 var g_nMIN_BTN_WIDTH = 80; 00032 var g_nMAX_BTN_HEIGHT = 20; 00033 00034 // Modes: 00035 var g_nMNone = 0; 00036 var g_nMDivide = 1; 00037 var g_nMMultiply = 2; 00038 var g_nMSubtract = 3; 00039 var g_nMAdd = 4; 00040 00041 /*********************************************************************** 00042 ***** DsCalculator Prototype ***** 00043 ***********************************************************************/ 00044 function DsCalculator() 00045 { 00046 this.m_bMemoryIsSet = false; 00047 this.m_nValue = 0; 00048 this.m_nMemory = 0; 00049 this.m_nMode = g_nMNone; 00050 this.m_nPending = g_nMNone; 00051 this.m_bClearEntry = true; 00052 00053 this.m_wDlg = new DzDialog; 00054 00055 this.m_wEntry = new DzLineEdit( this.m_wDlg ); 00056 00057 this.m_wMode = new DzLabel( this.m_wDlg ); 00058 00059 this.m_wMemClear = new DzPushButton( this.m_wDlg ); 00060 this.m_wMemReplace = new DzPushButton( this.m_wDlg ); 00061 this.m_wMemStore = new DzPushButton( this.m_wDlg ); 00062 this.m_wMemAdd = new DzPushButton( this.m_wDlg ); 00063 00064 this.m_wClear = new DzPushButton( this.m_wDlg ); 00065 this.m_wClearEntry = new DzPushButton( this.m_wDlg ); 00066 00067 this.m_wZero = new DzPushButton( this.m_wDlg ); 00068 this.m_wOne = new DzPushButton( this.m_wDlg ); 00069 this.m_wTwo = new DzPushButton( this.m_wDlg ); 00070 this.m_wThree = new DzPushButton( this.m_wDlg ); 00071 this.m_wFour = new DzPushButton( this.m_wDlg ); 00072 this.m_wFive = new DzPushButton( this.m_wDlg ); 00073 this.m_wSix = new DzPushButton( this.m_wDlg ); 00074 this.m_wSeven = new DzPushButton( this.m_wDlg ); 00075 this.m_wEight = new DzPushButton( this.m_wDlg ); 00076 this.m_wNine = new DzPushButton( this.m_wDlg ); 00077 00078 this.m_wDivide = new DzPushButton( this.m_wDlg ); 00079 this.m_wMultiply = new DzPushButton( this.m_wDlg ); 00080 this.m_wSubtract = new DzPushButton( this.m_wDlg ); 00081 this.m_wAdd = new DzPushButton( this.m_wDlg ); 00082 00083 this.m_wSignBtn = new DzPushButton( this.m_wDlg ); 00084 this.m_wPointBtn = new DzPushButton( this.m_wDlg ); 00085 00086 this.m_wResult = new DzPushButton( this.m_wDlg ); 00087 00088 this.m_wDlgBtnsGB = new DzGroupBox( this.m_wDlg ); 00089 this.m_wHelpBtn = new DzPushButton( this.m_wDlgBtnsGB ); 00090 this.m_wAcceptBtn = new DzPushButton( this.m_wDlgBtnsGB ); 00091 } 00092 00093 /***********************************************************************/ 00094 DsCalculator.superclass = Object; 00095 00096 /*********************************************************************/ 00097 DsCalculator.prototype.doDialog = function() 00098 { 00099 var oHelpMgr = App.getHelpMgr(); 00100 00101 var oActionMgr = MainWindow.getActionMgr(); 00102 var oHelpAction = oActionMgr ? oActionMgr.findAction( "DzWhatsThisAction" ) : undefined; 00103 00104 var nRow = 0, nColumn = 0; 00105 00106 this.m_wDlg.caption = "DAZ Calculator"; 00107 00108 var wDlgLayout = new DzGridLayout( this.m_wDlg ); 00109 wDlgLayout.margin = g_nMARGIN; 00110 wDlgLayout.spacing = g_nSPACING; 00111 00112 this.m_wEntry.alignment = this.m_wEntry.AlignRight; 00113 this.m_wEntry.text = this.m_nValue; 00114 wDlgLayout.addMultiCellWidget( this.m_wEntry, nRow, nRow, nColumn, nColumn + 3 ); 00115 00116 nColumn += 4; 00117 00118 wDlgLayout.addWidget( this.m_wMode, nRow, nColumn++ ); 00119 00120 // --- next row 00121 nRow++; 00122 nColumn = 0; 00123 00124 this.m_wMemClear.text = "MC"; 00125 this.m_wMemClear.clicked.connect( this, "memClear" ); 00126 wDlgLayout.addWidget( this.m_wMemClear, nRow, nColumn++ ); 00127 00128 this.m_wMemReplace.text = "MR"; 00129 this.m_wMemReplace.clicked.connect( this, "memRecall" ); 00130 wDlgLayout.addWidget( this.m_wMemReplace, nRow, nColumn++ ); 00131 00132 this.m_wMemStore.text = "MS"; 00133 this.m_wMemStore.clicked.connect( this, "memSet" ); 00134 wDlgLayout.addWidget( this.m_wMemStore, nRow, nColumn++ ); 00135 00136 this.m_wMemAdd.text = "M+"; 00137 this.m_wMemAdd.clicked.connect( this, "memAdd" ); 00138 wDlgLayout.addWidget( this.m_wMemAdd, nRow, nColumn++ ); 00139 00140 this.m_wClear.text = "C"; 00141 this.m_wClear.clicked.connect( this, "clear" ); 00142 wDlgLayout.addWidget( this.m_wClear, nRow, nColumn++ ); 00143 00144 // --- next row 00145 nRow++; 00146 nColumn = 0; 00147 00148 this.m_wSeven.text = "7"; 00149 this.m_wSeven.clicked.connect( this, "seven" ); 00150 wDlgLayout.addWidget( this.m_wSeven, nRow, nColumn++ ); 00151 00152 this.m_wEight.text = "8"; 00153 this.m_wEight.clicked.connect( this, "eight" ); 00154 wDlgLayout.addWidget( this.m_wEight, nRow, nColumn++ ); 00155 00156 this.m_wNine.text = "9"; 00157 this.m_wNine.clicked.connect( this, "nine" ); 00158 wDlgLayout.addWidget( this.m_wNine, nRow, nColumn++ ); 00159 00160 this.m_wDivide.text = "/"; 00161 this.m_wDivide.clicked.connect( this, "divide" ); 00162 wDlgLayout.addWidget( this.m_wDivide, nRow, nColumn++ ); 00163 00164 this.m_wClearEntry.text = "CE"; 00165 this.m_wClearEntry.clicked.connect( this, "clearEntry" ); 00166 wDlgLayout.addWidget( this.m_wClearEntry, nRow, nColumn++ ); 00167 00168 // --- next row 00169 nRow++; 00170 nColumn = 0; 00171 00172 this.m_wFour.text = "4"; 00173 this.m_wFour.clicked.connect( this, "four" ); 00174 wDlgLayout.addWidget( this.m_wFour, nRow, nColumn++ ); 00175 00176 this.m_wFive.text = "5"; 00177 this.m_wFive.clicked.connect( this, "five" ); 00178 wDlgLayout.addWidget( this.m_wFive, nRow, nColumn++ ); 00179 00180 this.m_wSix.text = "6"; 00181 this.m_wSix.clicked.connect( this, "six" ); 00182 wDlgLayout.addWidget( this.m_wSix, nRow, nColumn++ ); 00183 00184 this.m_wMultiply.text = "*"; 00185 this.m_wMultiply.clicked.connect( this, "multiply" ); 00186 wDlgLayout.addWidget( this.m_wMultiply, nRow, nColumn++ ); 00187 00188 // --- next row 00189 nRow++; 00190 nColumn = 0; 00191 00192 this.m_wOne.text = "1"; 00193 this.m_wOne.clicked.connect( this, "one" ); 00194 wDlgLayout.addWidget( this.m_wOne, nRow, nColumn++ ); 00195 00196 this.m_wTwo.text = "2"; 00197 this.m_wTwo.clicked.connect( this, "two" ); 00198 wDlgLayout.addWidget( this.m_wTwo, nRow, nColumn++ ); 00199 00200 this.m_wThree.text = "3"; 00201 this.m_wThree.clicked.connect( this, "three" ); 00202 wDlgLayout.addWidget( this.m_wThree, nRow, nColumn++ ); 00203 00204 this.m_wSubtract.text = "-"; 00205 this.m_wSubtract.clicked.connect( this, "subtract" ); 00206 wDlgLayout.addWidget( this.m_wSubtract, nRow, nColumn++ ); 00207 00208 // --- next row 00209 nRow++; 00210 nColumn = 0; 00211 00212 this.m_wZero.text = "0"; 00213 this.m_wZero.clicked.connect( this, "zero" ); 00214 wDlgLayout.addWidget( this.m_wZero, nRow, nColumn++ ); 00215 00216 this.m_wSignBtn.text = "+/-"; 00217 this.m_wSignBtn.clicked.connect( this, "changeSign" ); 00218 wDlgLayout.addWidget( this.m_wSignBtn, nRow, nColumn++ ); 00219 00220 this.m_wPointBtn.text = "."; 00221 this.m_wPointBtn.clicked.connect( this, "point" ); 00222 wDlgLayout.addWidget( this.m_wPointBtn, nRow, nColumn++ ); 00223 00224 this.m_wAdd.text = "+"; 00225 this.m_wAdd.clicked.connect( this, "add" ); 00226 wDlgLayout.addWidget( this.m_wAdd, nRow, nColumn++ ); 00227 00228 this.m_wResult.text = "="; 00229 this.m_wResult.clicked.connect( this, "result" ); 00230 wDlgLayout.addWidget( this.m_wResult, nRow, nColumn++ ); 00231 00232 // --- next row 00233 nRow++; 00234 nColumn = 0; 00235 00236 this.m_wDlgBtnsGB.flat = true; 00237 var wDlgBtnsLyt = new DzGridLayout( this.m_wDlgBtnsGB ); 00238 wDlgBtnsLyt.margin = g_nMARGIN; 00239 wDlgBtnsLyt.spacing = g_nSPACING; 00240 00241 this.m_wHelpBtn.pixmap = new Pixmap( String( "%1/images/icons/whatsthissmallicon.png" ).arg( App.getResourcesPath() ) ); 00242 this.m_wHelpBtn.maxHeight = g_nMAX_BTN_HEIGHT; 00243 if( oHelpAction ) 00244 { 00245 this.m_wHelpBtn.clicked.connect( oHelpAction, "trigger()" ); 00246 } 00247 this.m_wHelpBtn.toolTip = oHelpMgr.getToolTip( "WhatsThis" ); 00248 this.m_wHelpBtn.whatsThis = oHelpMgr.getHelpString( "WhatsThis" ); 00249 wDlgBtnsLyt.addWidget( this.m_wHelpBtn, 0, 0 ); 00250 00251 wDlgBtnsLyt.setColStretch( 1, 1 ); 00252 00253 this.m_wAcceptBtn.text = "&Close"; 00254 this.m_wAcceptBtn.minWidth = g_nMIN_BTN_WIDTH; 00255 this.m_wAcceptBtn.maxHeight = g_nMAX_BTN_HEIGHT; 00256 this.m_wAcceptBtn.toolTip = oHelpMgr.getToolTip( "OKDialog" ); 00257 this.m_wAcceptBtn.whatsThis = oHelpMgr.getHelpString( "OKDialog" ).replace( "<b>OK:</b>", "<b>Close:</b>" ); 00258 this.m_wDlg.setAcceptButton( this.m_wAcceptBtn ); 00259 wDlgBtnsLyt.addWidget( this.m_wAcceptBtn, 0, 2 ); 00260 00261 wDlgLayout.addMultiCellWidget( this.m_wDlgBtnsGB, nRow, nRow, nColumn, nColumn + 4 ); 00262 00263 this.m_wDlg.exec(); 00264 } 00265 00266 /*********************************************************************/ 00267 DsCalculator.prototype.getNumber = function() 00268 { 00269 return Number( this.m_wEntry.text ); 00270 } 00271 00272 /*********************************************************************/ 00273 DsCalculator.prototype.setNumber = function( num ) 00274 { 00275 this.m_wEntry.text = num; 00276 } 00277 00278 /*********************************************************************/ 00279 DsCalculator.prototype.numPress = function() 00280 { 00281 if( this.m_bClearEntry ) 00282 { 00283 this.m_wEntry.text = ""; 00284 this.m_bClearEntry = false; 00285 } 00286 this.btnPress(); 00287 } 00288 00289 /*********************************************************************/ 00290 DsCalculator.prototype.btnPress = function() 00291 { 00292 if( this.m_nPending != this.m_nMode ) 00293 this.m_nPending = this.m_nMode; 00294 this.m_wMode.text = ""; 00295 } 00296 00297 /*********************************************************************/ 00298 DsCalculator.prototype.operatorPress = function() 00299 { 00300 if( this.m_nPending == g_nMNone ) 00301 this.m_nValue = this.getNumber(); 00302 else 00303 { 00304 var operand = this.getNumber(); 00305 switch( this.m_nPending ) 00306 { 00307 case g_nMDivide: 00308 this.m_nValue /= operand; 00309 break; 00310 00311 case g_nMMultiply: 00312 this.m_nValue *= operand; 00313 break; 00314 00315 case g_nMSubtract: 00316 this.m_nValue -= operand; 00317 break; 00318 00319 case g_nMAdd: 00320 this.m_nValue += operand; 00321 break; 00322 } 00323 this.m_nPending = g_nMNone; 00324 this.setNumber( this.m_nValue ); 00325 } 00326 00327 this.m_bClearEntry = true; 00328 } 00329 00330 /*********************************************************************/ 00331 DsCalculator.prototype.zero = function() 00332 { 00333 this.numPress(); 00334 this.m_wEntry.text += "0"; 00335 } 00336 00337 /*********************************************************************/ 00338 DsCalculator.prototype.one = function() 00339 { 00340 this.numPress(); 00341 this.m_wEntry.text += "1"; 00342 } 00343 00344 /*********************************************************************/ 00345 DsCalculator.prototype.two = function() 00346 { 00347 this.numPress(); 00348 this.m_wEntry.text += "2"; 00349 } 00350 00351 /*********************************************************************/ 00352 DsCalculator.prototype.three = function() 00353 { 00354 this.numPress(); 00355 this.m_wEntry.text += "3"; 00356 } 00357 00358 /*********************************************************************/ 00359 DsCalculator.prototype.four = function() 00360 { 00361 this.numPress(); 00362 this.m_wEntry.text += "4"; 00363 } 00364 00365 /*********************************************************************/ 00366 DsCalculator.prototype.five = function() 00367 { 00368 this.numPress(); 00369 this.m_wEntry.text += "5"; 00370 } 00371 00372 /*********************************************************************/ 00373 DsCalculator.prototype.six = function() 00374 { 00375 this.numPress(); 00376 this.m_wEntry.text += "6"; 00377 } 00378 00379 /*********************************************************************/ 00380 DsCalculator.prototype.seven = function() 00381 { 00382 this.numPress(); 00383 this.m_wEntry.text += "7"; 00384 } 00385 00386 /*********************************************************************/ 00387 DsCalculator.prototype.eight = function() 00388 { 00389 this.numPress(); 00390 this.m_wEntry.text += "8"; 00391 } 00392 00393 /*********************************************************************/ 00394 DsCalculator.prototype.nine = function() 00395 { 00396 this.numPress(); 00397 this.m_wEntry.text += "9"; 00398 } 00399 00400 /*********************************************************************/ 00401 DsCalculator.prototype.changeSign = function() 00402 { 00403 this.setNumber( -this.getNumber() ); 00404 } 00405 00406 /*********************************************************************/ 00407 DsCalculator.prototype.point = function() 00408 { 00409 this.btnPress(); 00410 if( this.m_wEntry.text.find( "." ) < 0 ) 00411 this.m_wEntry.text += "."; 00412 } 00413 00414 /*********************************************************************/ 00415 DsCalculator.prototype.clear = function() 00416 { 00417 this.clearEntry(); 00418 this.m_nPending = g_nMNone; 00419 this.m_nMode = g_nMNone; 00420 this.m_wMode.text = ""; 00421 } 00422 00423 /*********************************************************************/ 00424 DsCalculator.prototype.clearEntry = function() 00425 { 00426 this.setNumber( 0 ); 00427 this.m_bClearEntry = true; 00428 } 00429 00430 /*********************************************************************/ 00431 DsCalculator.prototype.divide = function() 00432 { 00433 this.operatorPress(); 00434 this.m_wMode.text = "/"; 00435 this.m_nMode = g_nMDivide; 00436 } 00437 00438 /*********************************************************************/ 00439 DsCalculator.prototype.multiply = function() 00440 { 00441 this.operatorPress(); 00442 this.m_wMode.text = "*"; 00443 this.m_nMode = g_nMMultiply; 00444 } 00445 00446 /*********************************************************************/ 00447 DsCalculator.prototype.subtract = function() 00448 { 00449 this.operatorPress(); 00450 this.m_wMode.text = "-"; 00451 this.m_nMode = g_nMSubtract; 00452 } 00453 00454 /*********************************************************************/ 00455 DsCalculator.prototype.add = function() 00456 { 00457 this.operatorPress(); 00458 this.m_wMode.text = "+"; 00459 this.m_nMode = g_nMAdd; 00460 } 00461 00462 /*********************************************************************/ 00463 DsCalculator.prototype.result = function() 00464 { 00465 this.operatorPress(); 00466 this.m_nMode = g_nMNone; 00467 } 00468 00469 /*********************************************************************/ 00470 DsCalculator.prototype.memClear = function() 00471 { 00472 this.m_nMemory = 0; 00473 this.m_bMemoryIsSet = false; 00474 } 00475 00476 /*********************************************************************/ 00477 DsCalculator.prototype.memRecall = function() 00478 { 00479 if( this.m_bMemoryIsSet ) 00480 { 00481 this.btnPress(); 00482 this.setNumber( this.m_nMemory ); 00483 } 00484 } 00485 00486 /*********************************************************************/ 00487 DsCalculator.prototype.memSet = function() 00488 { 00489 this.m_nMemory = this.getNumber(); 00490 this.m_bMemoryIsSet = true; 00491 } 00492 00493 /*********************************************************************/ 00494 DsCalculator.prototype.memAdd = function() 00495 { 00496 this.m_nMemory += this.getNumber(); 00497 this.m_bMemoryIsSet = true; 00498 } 00499 00500 /*********************************************************************/ 00501 var oCalc = new DsCalculator; 00502 oCalc.doDialog(); |