This shows you the differences between two versions of the page.
sample_code:image_array [2015/09/28 15:06] stevegarman created |
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( " | ||
- | |||
- | // | ||
- | var img = app.CreateImage( null, | ||
- | img.SetColor("# | ||
- | img.SetOnTouch(imgOnTouch); | ||
- | |||
- | lay.AddChild( img ); | ||
- | var btnSave=app.CreateButton(" | ||
- | btnSave.SetOnTouch(btnSaveOnTouch); | ||
- | lay.AddChild(btnSave); | ||
- | // | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | function imgOnTouch(ev) | ||
- | { | ||
- | var row, | ||
- | var cellWidth=0.1, | ||
- | if(ev.action===" | ||
- | { | ||
- | row = Math.floor(ev.Y/ | ||
- | col = Math.floor(ev.X/ | ||
- | x1 = col*cellWidth; | ||
- | y1 = row*cellHeight; | ||
- | x2 = x1 + cellWidth; | ||
- | y2 = y1 + cellHeight; | ||
- | val = getData(row, | ||
- | this.SetLineWidth(1); | ||
- | this.SetPaintStyle(" | ||
- | setData(row, | ||
- | if(val < | ||
- | { | ||
- | this.SetPaintColor("# | ||
- | this.SetPaintStyle(" | ||
- | this.DrawRectangle(x1, | ||
- | } | ||
- | else if(val === 0) | ||
- | { | ||
- | this.SetPaintColor("# | ||
- | this.DrawRectangle(x1, | ||
- | } | ||
- | else if(val === 1) | ||
- | { | ||
- | this.SetPaintColor("# | ||
- | this.DrawRectangle(x1, | ||
- | } | ||
- | app.ShowPopup(val); | ||
- | } | ||
- | } | ||
- | |||
- | function getData(row, | ||
- | { | ||
- | var ret; | ||
- | if (cellData.length < | ||
- | return -1; | ||
- | ret = cellData[row][col]; | ||
- | if(ret===undefined) | ||
- | 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("/ | ||
- | } | ||
- | |||
- | </ |