SHOW:
|
|
- or go back to the newest paste.
| 1 | typedef Xywh = {x:Float, y:Float, width:Float, height:Float};
| |
| 2 | ||
| 3 | using PlaceTools; | |
| 4 | ||
| 5 | class PlaceTools | |
| 6 | {
| |
| 7 | public static var HORIZONTAL:String = "horizontal"; | |
| 8 | ||
| 9 | public static function rightOf(target:Xywh, ref:Xywh, ?padding:Int=0, ?align:Bool=true, ?width:Float=0, ?height:Float=0):Void | |
| 10 | {
| |
| 11 | target.x = ref.x + ref.width + padding; | |
| 12 | if(align) target.y = ref.y; | |
| 13 | if(width!=0)target.width=width; | |
| 14 | if(height!=0)target.height=height; | |
| 15 | } | |
| 16 | - | public static function distribute(arr:Array<Dynamic>, ?padding:Int=0, ?dtype:String="horizontal") |
| 16 | + | public static function distribute(arr:Array<Xywh>, ?padding:Int=0, ?dtype:String="horizontal") |
| 17 | {
| |
| 18 | for(i in 1...arr.length) | |
| 19 | {
| |
| 20 | switch(dtype) | |
| 21 | {
| |
| 22 | case HORIZONTAL: arr[i].rightOf(arr[i-1], padding); | |
| 23 | default: trace(not implemented in this example); | |
| 24 | } | |
| 25 | } | |
| 26 | } | |
| 27 | - | } |
| 27 | + | } |
| 28 | ||
| 29 | //------------------------------------ | |
| 30 | //var btn1 = new MyButton(); //extends Sprite | |
| 31 | [btn1, btn2, btn3].distribute(20, PlaceTools.HORIZONTAL); | |
| 32 | ||
| 33 | //Compiler error: Array<MyButton> has no field distribute | |
| 34 | ||
| 35 | //changing | |
| 36 | public static function distribute(arr:Array<Xywh>, ?padding:Int=0, ?dtype:String="horizontal") | |
| 37 | //into: | |
| 38 | public static function distribute(arr:Array<Dynamic>, ?padding:Int=0, ?dtype:String="horizontal") | |
| 39 | //runtime error : btn1 has no field rightOf |