This shows you the differences between two versions of the page.
sample_code:layout_children [2014/12/28 18:20] stevegarman [clearLayout] use app.GetObjects |
sample_code:layout_children [2014/12/29 02:20] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Layout Children ====== | ||
- | DroidScript' | ||
- | ===== ChildToBack ===== | ||
- | <code javascript childToBack.js> | ||
- | function childToBack(layt, | ||
- | if( layt.GetChildOrder( obj ) > 0 ){ | ||
- | layt.RemoveChild(obj); | ||
- | layt.AddChild(obj, | ||
- | } | ||
- | } | ||
- | </ | ||
- | ===== setChildOrder ===== | ||
- | <code javascript setChildOrder.js> | ||
- | function setChildOrder(layt, | ||
- | var tst = layt.GetChildOrder( obj ); | ||
- | if ((tst >= 0) && (tst != order)){ | ||
- | layt.RemoveChild(obj); | ||
- | layt.AddChild(obj, | ||
- | } | ||
- | } | ||
- | </ | ||
- | ===== getChildren ===== | ||
- | <code javascript getChildren.js> | ||
- | function getChildren(layt){ | ||
- | //takes a layout as an argument | ||
- | var children = []; | ||
- | var obj, order; | ||
- | //loop through all controls | ||
- | for (var id in _map) { | ||
- | obj = _map[id] | ||
- | //check if control is on our layout | ||
- | order = layt.GetChildOrder( obj ); | ||
- | if( order > -1 ) { | ||
- | //got one! | ||
- | //zorder propery wll persist until getChildren called again | ||
- | //or manually overwritten by coder | ||
- | obj.zorder = order; | ||
- | children.push( obj ); | ||
- | } | ||
- | } | ||
- | children.sort(function (a, b) { return a.zorder - b.zorder}); | ||
- | return children; | ||
- | } | ||
- | </ | ||
- | ===== clearLayout ===== | ||
- | <code JavaScript clearlayout.js> | ||
- | function clearLayout(layt) | ||
- | { | ||
- | // | ||
- | var allobjs = app.GetObjects(); | ||
- | var obj, | ||
- | // | ||
- | for (var id in allobjs) | ||
- | { | ||
- | obj = allobjs[id] | ||
- | // | ||
- | order = layt.GetChildOrder( obj ); | ||
- | if( order > | ||
- | { | ||
- | // | ||
- | layt.DestroyChild( obj ); | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | ===== Sample app ===== | ||
- | This sample shows how to use the childToBack function | ||
- | <code javascript toback.js> | ||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | app.SetOrientation(" | ||
- | //Create a layout with objects vertically centered. | ||
- | lay = app.CreateLayout( " | ||
- | | ||
- | //Create an absolute layout so we can position objects. | ||
- | layAbs = app.CreateLayout( " | ||
- | lay.AddChild( layAbs ); | ||
- | //Create first image. | ||
- | img1 = app.CreateImage( null, 0.5, 0.3 ); | ||
- | img1.SetColor("# | ||
- | img1.SetOnTouchDown(img_OnTouch); | ||
- | layAbs.AddChild( img1 ); | ||
- | | ||
- | //Create second image. | ||
- | img2 = app.CreateImage( null, 0.5, 0.3 ); | ||
- | img2.SetColor("# | ||
- | img2.SetPosition( 0.1,0.1 ); | ||
- | img2.SetOnTouchDown(img_OnTouch); | ||
- | layAbs.AddChild( img2 ); | ||
- | |||
- | //Create third image in middle | ||
- | img3 = app.CreateImage( null, 0.5, 0.3 ); | ||
- | img3.SetColor("# | ||
- | img3.SetPosition( 0.2,0.2 ); | ||
- | img3.SetOnTouchDown(img_OnTouch); | ||
- | layAbs.AddChild( img3,1 ); | ||
- | | ||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | //Called when user touches image | ||
- | function img_OnTouch(){ | ||
- | childToBack(layAbs, | ||
- | } | ||
- | |||
- | function childToBack(layt, | ||
- | if( layt.GetChildOrder( obj ) > 0 ){ | ||
- | layt.RemoveChild(obj); | ||
- | layt.AddChild(obj, | ||
- | } | ||
- | } | ||
- | |||
- | </ |