User Tools

Site Tools


sample_code:image_array

Differences

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

Link to this comparison view

Next revision
Previous revision
sample_code:image_array [2015/09/28 15:06]
stevegarman created
sample_code:image_array [2015/09/29 17:27] (current)
Line 10: Line 10:
 <code JavaScript imageArray.js> <code JavaScript imageArray.js>
  
-var cellData=[];+var cellData=[],img;
 //Called when application is started. //Called when application is started.
 function OnStart() function OnStart()
Line 17: Line 17:
     var lay = app.CreateLayout( "linear", "VCenter,FillXY" );         var lay = app.CreateLayout( "linear", "VCenter,FillXY" );    
  
-    //Create a text label and add it to layout+    //Create image control and add it to layout
-    var img = app.CreateImage( null, 0.9, 0.5 ); +    img = app.CreateImage( null, 0.9, 0.5 ); 
-    img.SetColor("#ff999999")+    img.cellWidth=0.1; 
 +    img.cellHeight=0.1; 
 + 
 +    img.SetColor("#ff999999");
     img.SetOnTouch(imgOnTouch);     img.SetOnTouch(imgOnTouch);
          
     lay.AddChild( img );     lay.AddChild( img );
 +
 +    // horizontal layout for buttons
 +    var hlay=app.CreateLayout("Linear","Horizontal");
 +    lay.AddChild(hlay);
 +    //buttons
     var btnSave=app.CreateButton("Save");     var btnSave=app.CreateButton("Save");
     btnSave.SetOnTouch(btnSaveOnTouch);     btnSave.SetOnTouch(btnSaveOnTouch);
-    lay.AddChild(btnSave);+    hlay.AddChild(btnSave); 
 +    var btnLoad=app.CreateButton("Load"); 
 +    btnLoad.SetOnTouch(btnLoadOnTouch); 
 +    hlay.AddChild(btnLoad); 
 +    var btnClear=app.CreateButton("Clear"); 
 +    btnClear.SetOnTouch(btnClearOnTouch); 
 +    hlay.AddChild(btnClear); 
 +    var btnFresh=app.CreateButton("Refresh"); 
 +    btnFresh.SetOnTouch(populate); 
 +    hlay.AddChild(btnFresh);  
     //Add layout to app.         //Add layout to app.    
     app.AddLayout( lay );     app.AddLayout( lay );
 +
 +    //force initial load fro file
 +    btnLoadOnTouch();
 } }
  
 +//user touched image - update cell
 function imgOnTouch(ev) function imgOnTouch(ev)
 { {
-     var row,col,val,x1,y1,x2,y2 +     var row,col,val ;
-     var cellWidth=0.1,cellHeight=0.1;+
      if(ev.action==="Down")      if(ev.action==="Down")
      {      {
-         row = Math.floor(ev.Y/cellHeight); +         row = Math.floor(ev.Y/this.cellHeight); 
-         col = Math.floor(ev.X/cellWidth)+         col = Math.floor(ev.X/this.cellWidth);
-         x1 = col*cellWidth; +
-         y1 = row*cellHeight; +
-         x2 = x1 + cellWidth; +
-         y2 = y1 + cellHeight;+
          val = getData(row,col);          val = getData(row,col);
-         this.SetLineWidth(1); +         val = (val+1)%2
-         this.SetPaintStyle("Fill") +         setData(row,col,val); 
-         setData(row,col,(val+1)%2); +         drawCell(row,col);
-         if(val < 0) +
-         { +
-            this.SetPaintColor("#ffff0000"); +
-            this.SetPaintStyle("Line"+
-            this.DrawRectangle(x1,y1,x2,y2); +
-         } +
-         else if(val === 0) +
-         { +
-            this.SetPaintColor("#ff00ff00"); +
-            this.DrawRectangle(x1,y1,x2,y2); +
-         } +
-         else if(val === 1) +
-         { +
-            this.SetPaintColor("#ff0000ff"); +
-            this.DrawRectangle(x1,y1,x2,y2); +
-         } +
-         app.ShowPopup(val);+
      }      }
 } }
  
 +//draw a single value to img
 +function drawCell(row,col)
 +{
 +    var val=getData(row,col)
 +    var x1 = col*img.cellWidth;
 +    var y1 = row*img.cellHeight;
 +    var x2 = x1 + img.cellWidth;
 +    var y2 = y1 + img.cellHeight;
 +    img.SetLineWidth(1);
 +    img.SetPaintStyle("Fill")
 +    if(val < 0)
 +    {
 +       img.SetPaintColor("#ffff0000");
 +       img.SetPaintStyle("Line")
 +       img.DrawRectangle(x1,y1,x2,y2);
 +    }
 +    else if(val === 0)
 +    {
 +       img.SetPaintColor("#ff00ff00");
 +       img.DrawRectangle(x1,y1,x2,y2);
 +    }
 +    else if(val === 1)
 +    {
 +       img.SetPaintColor("#ff0000ff");
 +       img.DrawRectangle(x1,y1,x2,y2);
 +    }
 +}
 +
 +// draw all data to img
 +function populate()
 +{
 +    var row, cell, dataLen, rowLen, i, j;
 +    dataLen = cellData.length;
 +    img.SetAutoUpdate(false);
 +    img.Clear();
 +    img.SetColor("#ff999999");
 +    for(i=0; i<dataLen ;i++) 
 +    {
 +        row = cellData[i];
 +        rowLen = row.length;
 +        if(rowLen===0) rowLen=1 ;
 +        for(j=0;j<rowLen;j++)
 +        {
 +            drawCell(i,j);
 +        }
 +    }
 +    img.Update();
 +    img.SetAutoUpdate(true);
 +}
 +
 +// get a value from 2d array
 function getData(row,col) function getData(row,col)
 { {
Line 72: Line 123:
       return -1;  // represents unset       return -1;  // represents unset
    ret = cellData[row][col];    ret = cellData[row][col];
-   if(ret===undefined)+   if(ret===undefined||ret===null)
       return -1;       return -1;
    return ret;    return ret;
 } }
 +
 +//write a value to 2d array
 function setData(row,col,value) function setData(row,col,value)
 { {
 +   // create any intervening rows
    while (cellData.length < row +1)    while (cellData.length < row +1)
    {    {
Line 85: Line 139:
 } }
  
 +//save data to file
 function btnSaveOnTouch() function btnSaveOnTouch()
 { {
-      
     var data = JSON.stringify(cellData);     var data = JSON.stringify(cellData);
-    app.WriteFile("/sdcard/picData",data)     +    app.WriteFile("/sdcard/picData",data);    
 +
 +  
 +// get data from file and redraw 
 +function btnLoadOnTouch() 
 +
 +    var data = app.ReadFile("/sdcard/picData"); 
 +    cellData = JSON.parse(data); 
 +    populate(); 
 +
 + 
 +// clear data and redraw 
 +function btnClearOnTouch() 
 +
 +    cellData = []; 
 +    populate();
 } }
  
 </code> </code>
sample_code/image_array.1443452774.txt.gz · Last modified: 2015/09/28 23:06 (external edit)