User Tools

Site Tools


sample_code:unit_convert

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:unit_convert [2014/11/01 12:52]
106.77.65.188 [The code]
sample_code:unit_convert [2014/11/01 20:55]
Line 1: Line 1:
-====== Converterlator ====== 
-Calculator+Converter app. 
-===== The code ===== 
-<code javascript unit_convert.js> 
-//variables for converter.  
-var unitval = []; 
-unitval ["mm"] = 1; 
-unitval ["cm"] = 10 
-unitval ["m"] = 1000; 
-unitval ["km"] =1000000; 
-  
-unitval ["g"] = 1; 
-unitval ["pound"] = 453.59; 
-unitval ["kg"] = 1000; 
-    
-//variables for calculator.  
-var sum = "";  
-var π = 3.14; 
- 
-//Called when application is started.  
-function OnStart()  
- 
-    //Create a main layout with objects vertically centered.  
-    lay = app.CreateLayout( "linear", "FillXY" );      
-    lay.SetBackColor( "#ff000000" ); 
- 
-    //Create array to hold number buttons.  
-    keys = [ 7,8,9,"/", 4,5,6,"*", 1,2,3,"-", 0,".","C","+","√","π", "=", "%" ]; 
- 
-    //Create an about button. 
-    abt = app.CreateButton( "About" ); 
-    abt.SetOnTouch( abt_OnTouch ); 
-    lay.AddChild( abt );    
- 
-    //Create a spinner for Background colour. 
-    clrs = app.CreateSpinner( "Background Color-Black,Blue,Red,Yellow,Purple,Orange,White" ); 
-    clrs.SetOnTouch( clrs_OnTouch ); 
-    lay.AddChild( clrs ); 
- 
-    //Create text control for displaying sum.  
-    txtSum = app.CreateText( "", 1.0, 0.1 );  
-    txtSum.SetTextSize( 42 );  
-    txtSum.SetBackColor( "#ff222222" );  
-    txtSum.SetMargins( 0, 0.05, 0, 0.05 );  
-    lay.AddChild( txtSum );  
-      
-    //Create first row of buttons.  
-    lay1st = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=0; i<4; i++ ) AddButton( lay1st, keys[i] );  
-    lay.AddChild( lay1st );  
-      
-    //Create second row of buttons.  
-    lay2nd = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=4; i<8; i++ ) AddButton( lay2nd, keys[i] );  
-    lay.AddChild( lay2nd );  
-      
-    //Create third row of buttons.  
-    lay3rd = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=8; i<12; i++ ) AddButton( lay3rd, keys[i] );  
-    lay.AddChild( lay3rd );  
-      
-    //Create fourth row of buttons.  
-    lay4th = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=12; i<16; i++ ) AddButton( lay4th, keys[i] );  
-    lay.AddChild( lay4th );  
-      
-    //Create fifth row of buttons.  
-    lay5th = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=16; i<20; i++ ) AddButton( lay5th, keys[i] );  
-    lay.AddChild( lay5th );  
- 
-    //Create a converter button. 
-    cnvrtr = app.CreateButton( "Converter", 0.4, 0.1 ); 
-    cnvrtr.SetOnTouch( cnvrtr_OnTouch ); 
-    cnvrtr.SetMargins( 0, 0.02, 0, 0 );  
-    lay.AddChild( cnvrtr ); 
-  
-    //Set Debug off for max performance. 
-    app.SetDebugEnabled( false ); 
- 
-    app.EnableBackKey( false );  
- 
-    //Add layout to app.      
-    app.AddLayout( lay );  
- 
- function OnBack() 
-{ 
- var yesno = app.CreateYesNoDialog( "Exit Converlator?" ); 
- yesno.SetOnTouch( yesno_OnTouch );  
-} 
- function yesno_OnTouch( result ) 
-{ 
- if( result == "Yes" ) app.Exit();  
- 
- function abt_OnTouch() 
-{ 
- //Create a dialog window. 
- dlg = app.CreateDialog( "About" ); 
- 
- //Create a layout for dialog. 
- laydlg = app.CreateLayout( "linear" ); 
- dlg.AddLayout( laydlg ); 
- 
- vrsn = app.CreateText( "Version : 1.0" ); 
- vrsn.SetTextSize(24); 
- vrsn.SetMargins( 0, 0, 0, 0.02 );   
- laydlg.AddChild(vrsn);  
- 
- nm = app.CreateText( "Developer : Sankarshan Dudhate" ); 
- nm.SetTextSize(20);  
- nm.SetPadding(0.05, 0, 0, 0 ); 
- laydlg.AddChild(nm);  
-   
- dlg.Show(); 
-} 
- 
- function clrs_OnTouch() 
-{ 
- if(clrs.GetText() == "Blue") 
-{ 
- lay.SetBackColor("#ff0000ff" ); 
-} 
- else if( clrs.GetText() == "Red") 
-{ 
- lay.SetBackColor("#ffff0000" ); 
-} 
- else if( clrs.GetText() == "Yellow" ) 
-{ 
- lay.SetBackColor( "#ffffff00" ); 
-} 
- else if( clrs.GetText() == "Purple" ) 
-{ 
- lay.SetBackColor( "#aa770077" ); 
-} 
-else if( clrs.GetText() == "Orange" ) 
-{ 
- lay.SetBackColor( "#ffff6700" ); 
-} 
- else if( clrs.GetText() == "White" ) 
-{ 
- lay.SetBackColor( "#ffffffff" ); 
-} 
- else 
-{ 
- lay.SetBackColor( "#ff000000" ); 
-}   
-} 
- 
-//Add a button to a given layout.  
-function AddButton( lay, name )  
- 
-    if( name=="=" ) w = 0.2; else w=0.2;  
-    btn = app.CreateButton( name, w, 0.1, "Alum" );  
-    btn.SetOnTouch( btns_OnTouch ); 
-    lay.AddChild( btn );  
- 
- 
-//Called when user presses number buttons.  
-function btns_OnTouch()  
- {  
-    app.Vibrate( "0,100" );  
-      
-    //Get button text.  
-    btn = app.GetLastButton();  
-    var txt = btn.GetText();  
-      
-    //Handle equals button.  
-    if( txt=="=" ) CalcResult();  
-      
-    //Handle clear button.  
-    else if( txt=="C" ) sum = "";  
-      
-    else if( txt=="√" ) 
-  { 
-         
-        sum = Math.sqrt( sum ); 
-  } 
- 
-    //Handle other buttons.  
-    else sum += txt;  
-      
-    //Update display.  
-    txtSum.SetText( sum );  
- 
- 
-//Calculate sum.  
-function CalcResult()  
- 
-    try{  
-        //Evaluate sum (and catch errors).  
-        sum = eval( sum ).toFixed(4);  
-    }  
-    catch(e) { sum = "Error" }  
- 
- 
- function cnvrtr_OnTouch() 
-{ 
- //Hide the objects from calculator. 
- txtSum.SetVisibility( "Gone" ); 
- lay1st.SetVisibility( "Gone" ); 
- lay2nd.SetVisibility( "Gone" ); 
- lay3rd.SetVisibility( "Gone" ); 
- lay4th.SetVisibility( "Gone" ); 
- lay5th.SetVisibility( "Gone" ); 
- cnvrtr.SetVisibility( "Gone" ); 
-   
- //Create a spinner for quantities. 
- qnt = app.CreateSpinner( "Length,Mass", 0.4, 0.1 ); 
- qnt.SetOnTouch( qnt_OnTouch ); 
- qnt.SetMargins( 0, 0.05, 0, 0.02 ); 
- lay.AddChild( qnt ); 
- 
- //Create 1st layout for unit and text edit. 
- lay1 = app.CreateLayout( "Linear", "Horizontal" ); 
- lay.AddChild( lay1 ); 
- 
- //Create 2nd layout for unit and text edit. 
- lay2 = app.CreateLayout( "Linear", "Horizontal" ); 
- lay.AddChild( lay2 ); 
- 
- /* We will set SetOnTouch callbacks of unt as well as edt 
- to be same because if anyone is edited, we should calculate  
- same result. So, instead of doing two different callbacks for 
- them, we will set same for both of them. */ 
- 
- //Create a spinner for 1st units. 
- unt1 = app.CreateSpinner( "mm,cm,m,km", 0.3, 0.1); 
- unt1.SetOnTouch( unt1_OnTouch ); 
- lay1.AddChild( unt1 ); 
- 
- //Create text edit to take value. 
- edt1 = app.CreateTextEdit( "0", 0.7, 0.1 ); 
- edt1.SetOnChange( unt1_OnTouch ); 
- lay1.AddChild( edt1 ); 
- 
- //Create a spinner for 2nd units. 
- unt2 = app.CreateSpinner( "mm,cm,m,km", 0.3, 0.1 ); 
- unt2.SetOnTouch( unt2_OnTouch ); 
- lay2.AddChild( unt2 ); 
- 
- //Create text edit to take value. 
- edt2 = app.CreateTextEdit( "0", 0.7, 0.1 ); 
- edt2.SetOnChange( unt2_OnTouch ); 
- lay2.AddChild( edt2 ); 
-  
- //Create a calculator button. 
- clc = app.CreateButton( "Calculator", 0.4, 0.1 ); 
- clc.SetOnTouch( clc_OnTouch ); 
- clc.SetMargins( 0, 0.1, 0, 0 ); 
- lay.AddChild( clc ); 
- 
-} 
- 
- function qnt_OnTouch() 
-{ 
- if( qnt.GetText() == "Mass" ) 
-{ 
- unt1.SetList( "g,pound,kg" ); 
- unt2.SetList( "g,pound,kg" ); 
-} 
-} 
- 
- function unt1_OnTouch() 
-{ 
- //Create variables to hold values of inputs. 
- var a = unitval[unt1.GetText()]; 
- var b = unitval[unt2.GetText()]; 
- qty = edt1.GetText(); 
- res = qty*a/b; 
- edt2.SetText( res ); 
-  
- 
- 
- function unt2_OnTouch() 
-{ 
- //Create variables to hold values of inputs. 
- var a = unitval[unt1.GetText()]; 
- var b = unitval[unt2.GetText()]; 
- qty = edt2.GetText(); 
- res = qty*b/a; 
- edt1.SetText( res ); 
-  
- 
- 
- function isNumber(n)  
-{ 
-  return !isNaN(parseFloat(n)) && isFinite(n);  
-} 
- 
- function clc_OnTouch() 
-{ 
-    //Hide the objects of converter. 
-    qnt.SetVisibility( "Gone" ); 
-    lay1.SetVisibility( "Gone" ); 
-    lay2.SetVisibility( "Gone" ); 
-    clc.SetVisibility( "Gone" ); 
-  
-    //Create text control for displaying sum.  
-    txtSum = app.CreateText( "", 1.0, 0.1 );  
-    txtSum.SetTextSize( 42 );  
-    txtSum.SetBackColor( "#ff222222" );  
-    txtSum.SetMargins( 0, 0.05, 0, 0.05 );  
-    lay.AddChild( txtSum );  
-      
-    //Create first row of buttons.  
-    lay1st = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=0; i<4; i++ ) AddButton( lay1st, keys[i] );  
-    lay.AddChild( lay1st );  
-      
-    //Create second row of buttons.  
-    lay2nd = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=4; i<8; i++ ) AddButton( lay2nd, keys[i] );  
-    lay.AddChild( lay2nd );  
-      
-    //Create third row of buttons.  
-    lay3rd = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=8; i<12; i++ ) AddButton( lay3rd, keys[i] );  
-    lay.AddChild( lay3rd );  
-      
-    //Create fourth row of buttons.   
-    lay4th = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=12; i<16; i++ ) AddButton( lay4th, keys[i] );  
-    lay.AddChild( lay4th );  
-      
-    //Create fifth row of buttons.  
-    lay5th = app.CreateLayout( "linear", "Horizontal" );      
-    for( i=16; i<20; i++ ) AddButton( lay5th, keys[i] );  
-    lay.AddChild( lay5th );  
- 
-    //Create a converter button. 
-    cnvrtr = app.CreateButton( "Converter", 0.4, 0.1 ); 
-    cnvrtr.SetOnTouch( cnvrtr_OnTouch ); 
-    cnvrtr.SetMargins( 0, 0.02, 0, 0 ); 
-    lay.AddChild( cnvrtr ); 
- 
-} 
- 
- 
- 
-</code> 
-===== Note ===== 
-This app is in no way the sort that anyone might wish to use in real life. 
- 
-It is just a very simplistic demo of the type of approach that might be taken. 
  
sample_code/unit_convert.txt · Last modified: 2014/11/01 20:55 (external edit)