User Tools

Site Tools


sample_code:layout_children

Differences

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

Link to this comparison view

sample_code:layout_children [2014/12/01 06:32]
stevegarman
sample_code:layout_children [2014/12/29 02:20]
Line 1: Line 1:
-====== Layout Children ====== 
-DroidScript's [[built_in:layouts|Layout]] comes complete with methods ChildToFront and GetChildOrder but some people seem to think it is missing a couple of methods, so I offer these functions in the hopr that they may be useful. 
-===== ChildToBack ===== 
-<code javascript childToBack.js> 
-function childToBack(layt, obj){ 
-  if( layt.GetChildOrder( obj ) > 0 ){ 
-    layt.RemoveChild(obj); 
-    layt.AddChild(obj,0); 
-  } 
-} 
-</code> 
-===== setChildOrder ===== 
-<code javascript setChildOrder.js> 
-function setChildOrder(layt, obj, order){ 
-  var tst = layt.GetChildOrder( obj ); 
-  if ((tst >= 0) && (tst != order)){ 
-    layt.RemoveChild(obj); 
-    layt.AddChild(obj,order); 
-  } 
-} 
-</code> 
-===== 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;  
- 
-</code> 
-===== Sample app ===== 
-This sample shows how to use the childToBack function 
-<code javascript toback.js> 
-//Called when application is started. 
-function OnStart() 
-{ 
-    app.SetOrientation("Portrait"); 
-    //Create a layout with objects vertically centered. 
-    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );     
-     
-    //Create an absolute layout so we can position objects. 
-    layAbs = app.CreateLayout( "Absolute" );     
-    lay.AddChild( layAbs ); 
  
-    //Create first image. 
-    img1 = app.CreateImage( null, 0.5, 0.3 ); 
-    img1.SetColor("#66000088"); 
-    img1.SetOnTouchDown(img_OnTouch); 
-    layAbs.AddChild( img1 ); 
-     
-    //Create second image. 
-    img2 = app.CreateImage( null, 0.5, 0.3 ); 
-    img2.SetColor("#66880000"); 
-    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("#66008800"); 
-    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, this) 
-} 
- 
-function childToBack(layt, obj){ 
-  if( layt.GetChildOrder( obj ) > 0 ){ 
-    layt.RemoveChild(obj); 
-    layt.AddChild(obj,0); 
-  } 
-} 
- 
-</code> 
sample_code/layout_children.txt ยท Last modified: 2014/12/29 02:20 (external edit)