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:45]
106.77.65.188 [Unit Converter]
sample_code:unit_convert [2014/11/01 20:55]
Line 1: Line 1:
-====== Unit Converter ====== 
-Simplistic demo of how a unit-converter app could be built.Supports two quantities. 
-===== The code ===== 
-<code javascript unit_convert.js> 
-var unitval = []; 
-unitval ["mm"] = 1; 
-unitval ["m"] = 1000; 
-unitval ["km"] =1000000; 
- 
-//Called when application is started. 
-function OnStart() 
-{ 
-    //Create a horizontal layout. 
-    lay = app.CreateLayout( "linear", "Horizontal,FillXY" );     
- 
-    edt = app.CreateTextEdit("100",0.23, -1, "Number"); 
-    edt.SetTextSize(12); 
-    edt.SetOnChange(docalc); 
-    spinfrom = app.CreateSpinner("Unit,mm,m,km",0.23); 
-    spinfrom.SetOnTouch(docalc); 
-    spinto = app.CreateSpinner("Unit,mm,m,km",0.23); 
-    spinto.SetOnTouch(docalc); 
-    txt = app.CreateText( "????",0.23 ); 
-    txt.SetTextSize( 12 ); 
-    lay.AddChild(edt); 
-    lay.AddChild(spinfrom); 
-    lay.AddChild(spinto); 
-    lay.AddChild( txt ); 
-     
-    //Add layout to app.     
-    app.AddLayout( lay ); 
-} 
- 
-function docalc(){ 
-  qty = edt.GetText(); 
-  if(spinfrom.GetText() == "Unit" || 
-     spinto.GetText() == "Unit" || 
-     !isNumber(qty)){ 
-     txt.SetText("????") 
-     return; 
-  } 
-  var a = unitval[spinfrom.GetText()]; 
-  var b = unitval[spinto.GetText()];  
-  var res = qty * a / b; 
-  txt.SetText(res); 
-} 
- 
-function isNumber(n) { 
-  return !isNaN(parseFloat(n)) && isFinite(n);  
-} 
-</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)