This shows you the differences between two versions of the page.
sample_code:image_array [2015/09/29 09:27] stevegarman tidy code, add buttons |
sample_code:image_array [2015/09/29 17:27] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Image array ====== | ||
- | Click part of the image to store a number in a 2d array representing the rectangles of the image. | ||
- | Click a rectangle multiple times and it will toggle between 1 and 2. | ||
- | |||
- | A colour-coded rectangle appears on the image to show what value was in the array before it was updated. | ||
- | |||
- | This is intended as a demo of one way to treat an image as a grid. | ||
- | |||
- | <code JavaScript imageArray.js> | ||
- | |||
- | var cellData=[], | ||
- | // | ||
- | function OnStart() | ||
- | { | ||
- | // | ||
- | var lay = app.CreateLayout( " | ||
- | |||
- | // | ||
- | img = app.CreateImage( null, | ||
- | img.cellWidth=0.1; | ||
- | img.cellHeight=0.1; | ||
- | |||
- | img.SetColor("# | ||
- | img.SetOnTouch(imgOnTouch); | ||
- | |||
- | lay.AddChild( img ); | ||
- | |||
- | // | ||
- | var hlay=app.CreateLayout(" | ||
- | lay.AddChild(hlay); | ||
- | // | ||
- | var btnSave=app.CreateButton(" | ||
- | btnSave.SetOnTouch(btnSaveOnTouch); | ||
- | hlay.AddChild(btnSave); | ||
- | var btnLoad=app.CreateButton(" | ||
- | btnLoad.SetOnTouch(btnLoadOnTouch); | ||
- | hlay.AddChild(btnLoad); | ||
- | var btnClear=app.CreateButton(" | ||
- | btnClear.SetOnTouch(btnClearOnTouch); | ||
- | hlay.AddChild(btnClear); | ||
- | var btnFresh=app.CreateButton(" | ||
- | btnFresh.SetOnTouch(populate); | ||
- | hlay.AddChild(btnFresh); | ||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | |||
- | // | ||
- | btnLoadOnTouch(); | ||
- | } | ||
- | |||
- | // | ||
- | function imgOnTouch(ev) | ||
- | { | ||
- | var row, | ||
- | if(ev.action===" | ||
- | { | ||
- | row = Math.floor(ev.Y/ | ||
- | col = Math.floor(ev.X/ | ||
- | val = getData(row, | ||
- | val = (val+1)%2; | ||
- | setData(row, | ||
- | drawCell(row, | ||
- | } | ||
- | } | ||
- | |||
- | // | ||
- | function drawCell(row, | ||
- | { | ||
- | var val=getData(row, | ||
- | 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(" | ||
- | if(val < | ||
- | { | ||
- | img.SetPaintColor("# | ||
- | img.SetPaintStyle(" | ||
- | img.DrawRectangle(x1, | ||
- | } | ||
- | else if(val === 0) | ||
- | { | ||
- | img.SetPaintColor("# | ||
- | img.DrawRectangle(x1, | ||
- | } | ||
- | else if(val === 1) | ||
- | { | ||
- | img.SetPaintColor("# | ||
- | img.DrawRectangle(x1, | ||
- | } | ||
- | } | ||
- | |||
- | // | ||
- | function populate() | ||
- | { | ||
- | var row, | ||
- | dataLen = cellData.length; | ||
- | img.SetAutoUpdate(false); | ||
- | img.Clear(); | ||
- | img.SetColor("# | ||
- | for(i=0; | ||
- | { | ||
- | row = cellData[i]; | ||
- | rowLen = row.length; | ||
- | if(rowLen===0) rowLen=1 ; | ||
- | for(j=0; | ||
- | { | ||
- | drawCell(i, | ||
- | } | ||
- | } | ||
- | img.Update(); | ||
- | img.SetAutoUpdate(true); | ||
- | } | ||
- | |||
- | // | ||
- | function getData(row, | ||
- | { | ||
- | var ret; | ||
- | if (cellData.length < | ||
- | return -1; | ||
- | ret = cellData[row][col]; | ||
- | if(ret===undefined||ret===null) | ||
- | return -1; | ||
- | return ret; | ||
- | } | ||
- | |||
- | // | ||
- | function setData(row, | ||
- | { | ||
- | // | ||
- | while (cellData.length < | ||
- | { | ||
- | cellData.push([]); | ||
- | } | ||
- | cellData[row][col]=value; | ||
- | } | ||
- | |||
- | // | ||
- | function btnSaveOnTouch() | ||
- | { | ||
- | var data = JSON.stringify(cellData); | ||
- | app.WriteFile("/ | ||
- | } | ||
- | |||
- | // | ||
- | function btnLoadOnTouch() | ||
- | { | ||
- | var data = app.ReadFile("/ | ||
- | cellData = JSON.parse(data); | ||
- | populate(); | ||
- | } | ||
- | |||
- | // | ||
- | function btnClearOnTouch() | ||
- | { | ||
- | cellData = []; | ||
- | populate(); | ||
- | } | ||
- | |||
- | </ |