This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
sample_code:unit_convert [2014/10/21 06:48] stevegarman change comment |
sample_code:unit_convert [2014/11/01 12:55] 106.77.65.188 [The code] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | Simplistic demo of how a unit-conversion | + | Calculator+Converter |
===== The code ===== | ===== The code ===== | ||
- | <code javascript | + | <code javascript |
- | var unitval | + | // |
- | unitval | + | var unitval = []; |
- | unitval | + | unitval [" |
- | unitval | + | unitval [" |
+ | unitval [" | ||
+ | unitval [" | ||
+ | |||
+ | unitval [" | ||
+ | unitval [" | ||
+ | unitval [" | ||
+ | |||
+ | // | ||
+ | var sum = ""; | ||
+ | var π = 3.14; | ||
- | //Called when application is started. | + | //Called when application is started. |
- | function OnStart() | + | function OnStart() |
+ | { | ||
+ | // | ||
+ | lay = app.CreateLayout( " | ||
+ | lay.SetBackColor( "# | ||
+ | |||
+ | // | ||
+ | keys = [ 7, | ||
+ | |||
+ | // | ||
+ | abt = app.CreateButton( " | ||
+ | abt.SetOnTouch( abt_OnTouch ); | ||
+ | lay.AddChild( abt ); | ||
+ | |||
+ | // | ||
+ | clrs = app.CreateSpinner( " | ||
+ | clrs.SetOnTouch( clrs_OnTouch ); | ||
+ | lay.AddChild( clrs ); | ||
+ | |||
+ | // | ||
+ | txtSum = app.CreateText( "", | ||
+ | txtSum.SetTextSize( 42 ); | ||
+ | txtSum.SetBackColor( "# | ||
+ | txtSum.SetMargins( 0, | ||
+ | lay.AddChild( txtSum ); | ||
+ | |||
+ | // | ||
+ | lay1st = app.CreateLayout( " | ||
+ | for( i=0; | ||
+ | lay.AddChild( lay1st ); | ||
+ | |||
+ | // | ||
+ | lay2nd = app.CreateLayout( " | ||
+ | for( i=4; | ||
+ | lay.AddChild( lay2nd ); | ||
+ | |||
+ | // | ||
+ | lay3rd = app.CreateLayout( " | ||
+ | for( i=8; | ||
+ | lay.AddChild( lay3rd ); | ||
+ | |||
+ | // | ||
+ | lay4th = app.CreateLayout( " | ||
+ | for( i=12; | ||
+ | lay.AddChild( lay4th ); | ||
+ | |||
+ | // | ||
+ | lay5th = app.CreateLayout( " | ||
+ | for( i=16; | ||
+ | lay.AddChild( lay5th ); | ||
+ | |||
+ | // | ||
+ | cnvrtr = app.CreateButton( " | ||
+ | cnvrtr.SetOnTouch( cnvrtr_OnTouch ); | ||
+ | cnvrtr.SetMargins( 0, | ||
+ | lay.AddChild( cnvrtr ); | ||
+ | |||
+ | // | ||
+ | app.SetDebugEnabled( false ); | ||
+ | |||
+ | app.EnableBackKey( false ); | ||
+ | |||
+ | // | ||
+ | app.AddLayout( lay ); | ||
+ | } | ||
+ | function OnBack() | ||
{ | { | ||
- | | + | var yesno = app.CreateYesNoDialog( " |
- | | + | yesno.SetOnTouch( yesno_OnTouch ); |
+ | } | ||
+ | function yesno_OnTouch( result ) | ||
+ | { | ||
+ | if( result == " | ||
+ | } | ||
+ | function abt_OnTouch() | ||
+ | { | ||
+ | //Create a dialog window. | ||
+ | dlg = app.CreateDialog( "About" ); | ||
- | edt = app.CreateTextEdit("100", | + | // |
- | edt.SetTextSize(12); | + | laydlg = app.CreateLayout( "linear" |
- | | + | dlg.AddLayout( laydlg ); |
- | | + | |
- | | + | vrsn = app.CreateText( "Version : |
- | | + | vrsn.SetTextSize(24); |
- | | + | vrsn.SetMargins( 0, 0, 0, 0.02 ); |
- | | + | laydlg.AddChild(vrsn); |
- | txt.SetTextSize( | + | |
- | lay.AddChild(edt); | + | nm = app.CreateText( " |
- | lay.AddChild(spinfrom); | + | nm.SetTextSize(20); |
- | lay.AddChild(spinto); | + | nm.SetPadding(0.05, |
- | | + | laydlg.AddChild(nm); |
- | + | ||
- | //Add layout to app. | + | dlg.Show(); |
- | app.AddLayout( lay ); | + | |
} | } | ||
- | function docalc(){ | + | function clrs_OnTouch() |
- | qty = edt.GetText(); | + | { |
- | if(spinfrom.GetText() == "Unit" | + | if(clrs.GetText() == " |
- | spinto.GetText() == "Unit" | + | { |
- | !isNumber(qty)){ | + | lay.SetBackColor("# |
- | txt.SetText("????") | + | } |
- | | + | else if( clrs.GetText() == " |
- | } | + | { |
- | var a = unitval[spinfrom.GetText()]; | + | lay.SetBackColor("# |
- | var b = unitval[spinto.GetText()]; | + | } |
- | var res = qty * a / b; | + | else if( clrs.GetText() == " |
- | txt.SetText(res); | + | { |
+ | lay.SetBackColor( "# | ||
+ | } | ||
+ | else if( clrs.GetText() == " | ||
+ | { | ||
+ | lay.SetBackColor( "#aa770077" | ||
+ | } | ||
+ | else if( clrs.GetText() == " | ||
+ | { | ||
+ | lay.SetBackColor( "# | ||
+ | } | ||
+ | else if( clrs.GetText() == " | ||
+ | { | ||
+ | lay.SetBackColor( "# | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | lay.SetBackColor( "# | ||
+ | } | ||
} | } | ||
- | function isNumber(n) { | + | // |
- | | + | function AddButton( lay, |
+ | { | ||
+ | if( name==" | ||
+ | btn = app.CreateButton( name, | ||
+ | btn.SetOnTouch( btns_OnTouch ); | ||
+ | lay.AddChild( btn ); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | function btns_OnTouch() | ||
+ | { | ||
+ | app.Vibrate( " | ||
+ | |||
+ | // | ||
+ | btn = app.GetLastButton(); | ||
+ | var txt = btn.GetText(); | ||
+ | |||
+ | // | ||
+ | if( txt==" | ||
+ | |||
+ | // | ||
+ | else if( txt==" | ||
+ | |||
+ | else if( txt==" | ||
+ | { | ||
+ | |||
+ | sum = Math.sqrt( sum ); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | else sum += txt; | ||
+ | |||
+ | // | ||
+ | txtSum.SetText( sum ); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | function CalcResult() | ||
+ | { | ||
+ | try{ | ||
+ | // | ||
+ | sum = eval( sum ).toFixed(4); | ||
+ | } | ||
+ | catch(e) { sum = " | ||
+ | } | ||
+ | |||
+ | function cnvrtr_OnTouch() | ||
+ | { | ||
+ | // | ||
+ | txtSum.SetVisibility( " | ||
+ | lay1st.SetVisibility( " | ||
+ | lay2nd.SetVisibility( " | ||
+ | lay3rd.SetVisibility( " | ||
+ | lay4th.SetVisibility( " | ||
+ | lay5th.SetVisibility( " | ||
+ | cnvrtr.SetVisibility( " | ||
+ | |||
+ | // | ||
+ | qnt = app.CreateSpinner( " | ||
+ | qnt.SetOnTouch( qnt_OnTouch ); | ||
+ | qnt.SetMargins( 0, | ||
+ | lay.AddChild( qnt ); | ||
+ | |||
+ | // | ||
+ | lay1 = app.CreateLayout( " | ||
+ | lay.AddChild( lay1 ); | ||
+ | |||
+ | // | ||
+ | lay2 = app.CreateLayout( " | ||
+ | lay.AddChild( lay2 ); | ||
+ | |||
+ | / | ||
+ | to be same because if anyone is edited, | ||
+ | same result. So, | ||
+ | them, | ||
+ | |||
+ | // | ||
+ | unt1 = app.CreateSpinner( " | ||
+ | unt1.SetOnTouch( unt1_OnTouch ); | ||
+ | lay1.AddChild( unt1 ); | ||
+ | |||
+ | // | ||
+ | edt1 = app.CreateTextEdit( " | ||
+ | edt1.SetOnChange( unt1_OnTouch ); | ||
+ | lay1.AddChild( edt1 ); | ||
+ | |||
+ | // | ||
+ | unt2 = app.CreateSpinner( " | ||
+ | unt2.SetOnTouch( unt2_OnTouch ); | ||
+ | lay2.AddChild( unt2 ); | ||
+ | |||
+ | // | ||
+ | edt2 = app.CreateTextEdit( " | ||
+ | edt2.SetOnChange( unt2_OnTouch ); | ||
+ | lay2.AddChild( edt2 ); | ||
+ | |||
+ | // | ||
+ | clc = app.CreateButton( " | ||
+ | clc.SetOnTouch( clc_OnTouch ); | ||
+ | clc.SetMargins( 0, | ||
+ | lay.AddChild( clc ); | ||
} | } | ||
+ | |||
+ | function qnt_OnTouch() | ||
+ | { | ||
+ | if( qnt.GetText() == " | ||
+ | { | ||
+ | unt1.SetList( " | ||
+ | unt2.SetList( " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | function unt1_OnTouch() | ||
+ | { | ||
+ | // | ||
+ | var a = unitval[unt1.GetText()]; | ||
+ | var b = unitval[unt2.GetText()]; | ||
+ | qty = edt1.GetText(); | ||
+ | res = qty*a/ | ||
+ | edt2.SetText( res ); | ||
+ | |||
+ | } | ||
+ | |||
+ | function unt2_OnTouch() | ||
+ | { | ||
+ | // | ||
+ | var a = unitval[unt1.GetText()]; | ||
+ | var b = unitval[unt2.GetText()]; | ||
+ | qty = edt2.GetText(); | ||
+ | res = qty*b/ | ||
+ | edt1.SetText( res ); | ||
+ | |||
+ | } | ||
+ | |||
+ | function isNumber(n) | ||
+ | { | ||
+ | return !isNaN(parseFloat(n)) && | ||
+ | } | ||
+ | |||
+ | function clc_OnTouch() | ||
+ | { | ||
+ | // | ||
+ | qnt.SetVisibility( " | ||
+ | lay1.SetVisibility( " | ||
+ | lay2.SetVisibility( " | ||
+ | clc.SetVisibility( " | ||
+ | |||
+ | // | ||
+ | txtSum = app.CreateText( "", | ||
+ | txtSum.SetTextSize( 42 ); | ||
+ | txtSum.SetBackColor( "# | ||
+ | txtSum.SetMargins( 0, | ||
+ | lay.AddChild( txtSum ); | ||
+ | |||
+ | // | ||
+ | lay1st = app.CreateLayout( " | ||
+ | for( i=0; | ||
+ | lay.AddChild( lay1st ); | ||
+ | |||
+ | // | ||
+ | lay2nd = app.CreateLayout( " | ||
+ | for( i=4; | ||
+ | lay.AddChild( lay2nd ); | ||
+ | |||
+ | // | ||
+ | lay3rd = app.CreateLayout( " | ||
+ | for( i=8; | ||
+ | lay.AddChild( lay3rd ); | ||
+ | |||
+ | // | ||
+ | lay4th = app.CreateLayout( " | ||
+ | for( i=12; | ||
+ | lay.AddChild( lay4th ); | ||
+ | |||
+ | // | ||
+ | lay5th = app.CreateLayout( " | ||
+ | for( i=16; | ||
+ | lay.AddChild( lay5th ); | ||
+ | |||
+ | // | ||
+ | cnvrtr = app.CreateButton( " | ||
+ | cnvrtr.SetOnTouch( cnvrtr_OnTouch ); | ||
+ | cnvrtr.SetMargins( 0, | ||
+ | lay.AddChild( cnvrtr ); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
</ | </ | ||
===== Note ===== | ===== Note ===== |