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

Both sides previous revision Previous revision
Next revision
Previous revision
sample_code:unit_convert [2014/11/01 12:45]
106.77.65.188 [Unit Converter]
sample_code:unit_convert [2014/11/01 20:55] (current)
Line 1: Line 1:
-====== Unit Converter ====== +====== Converterlator ====== 
-Simplistic demo of how a unit-converter app could be built.Supports two quantities.+Calculator+Converter app.
 ===== The code ===== ===== The code =====
-<code javascript unit_convert.js> +<code javascript converlator.js> 
-var unitval = []; +//variables for converter.  
-unitval ["mm"] = 1; +var unitval = []; 
-unitval ["m"] = 1000; +unitval ["mm"] = 1; 
-unitval ["km"] =1000000;+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+//Called when application is started.  
-function OnStart()+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()
 { {
-    //Create a horizontal layout+ var yesno = app.CreateYesNoDialog( "Exit Converlator?" ); 
-    lay = app.CreateLayout( "linear", "Horizontal,FillXY" );    + yesno.SetOnTouch( yesno_OnTouch );  
 +
 + function yesno_OnTouch( result ) 
 +
 + if( result == "Yes" ) app.Exit();  
 +}  
 + function abt_OnTouch() 
 +
 + //Create a dialog window
 + dlg = app.CreateDialog( "About" );
  
-    edt = app.CreateTextEdit("100",0.23, -1, "Number"); + //Create a layout for dialog. 
-    edt.SetTextSize(12); + laydlg = app.CreateLayout( "linear" ); 
-    edt.SetOnChange(docalc); + dlg.AddLayoutlaydlg ); 
-    spinfrom = app.CreateSpinner("Unit,mm,m,km",0.23); + 
-    spinfrom.SetOnTouch(docalc); + vrsn = app.CreateText( "Version : 1.0" ); 
-    spinto = app.CreateSpinner("Unit,mm,m,km",0.23); + vrsn.SetTextSize(24); 
-    spinto.SetOnTouch(docalc); + vrsn.SetMargins000, 0.02 );   
-    txt = app.CreateText( "????",0.23 ); + laydlg.AddChild(vrsn);  
-    txt.SetTextSize( 12 ); + 
-    lay.AddChild(edt); + nm = app.CreateText( "Developer : Sankarshan Dudhate" ); 
-    lay.AddChild(spinfrom); + nm.SetTextSize(20);  
-    lay.AddChild(spinto); + nm.SetPadding(0.05, 0, 0, 0 ); 
-    lay.AddChild( txt ); + laydlg.AddChild(nm);  
-     +   
-    //Add layout to app    + dlg.Show();
-    app.AddLayoutlay );+
 } }
  
-function docalc(){ + function clrs_OnTouch() 
-  qty = edt.GetText(); +
-  if(spinfrom.GetText() == "Unit|| + if(clrs.GetText() == "Blue"
-     spinto.GetText() == "Unit|| +
-     !isNumber(qty)){ + lay.SetBackColor("#ff0000ff" ); 
-     txt.SetText("????") +
-     return+ else ifclrs.GetText() == "Red") 
-  +
-  var a = unitval[spinfrom.GetText()]; + lay.SetBackColor("#ffff0000" ); 
-  var b unitval[spinto.GetText()];  +
-  var res qty * a / b+ else if( clrs.GetText() == "Yellow) 
-  txt.SetText(res);+
 + 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" ); 
 +}  
 } }
  
-function isNumber(n) { +//Add a button to a given layout.  
-  return !isNaN(parseFloat(n)) && isFinite(n); +function AddButtonlay, name  
 +{  
 +    ifname=="=" ) w = 0.2; else w=0.2;  
 +    btn = app.CreateButtonname, w, 0.1, "Alum" );  
 +    btn.SetOnTouch( btns_OnTouch )
 +    lay.AddChildbtn );  
 +}  
 + 
 +//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> </code>
 ===== Note ===== ===== Note =====
sample_code/unit_convert.1414845946.txt.gz · Last modified: 2014/11/01 20:45 (external edit)