Last active
August 29, 2015 13:57
-
-
Save amosl/9794169 to your computer and use it in GitHub Desktop.
Sample Panel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game | |
{ | |
import feathers.controls.text.TextFieldTextRenderer; | |
import flash.geom.Rectangle; | |
import flash.text.TextFormat; | |
import starling.display.DisplayObject; | |
import starling.display.Sprite; | |
/** | |
* ... | |
*/ | |
public class SamplePanel extends Sprite | |
{ | |
protected var _boxWidth:Number; | |
protected var _boxHeight:Number; | |
protected var _layoutRect:Rectangle; | |
protected var baseFontSize:int; | |
protected var _scl:Number; | |
private var captFmt:TextFormat; | |
private var numFmt:TextFormat; | |
public function SamplePanel() | |
{ | |
super(); | |
_boxWidth = 190; | |
_boxHeight = 64; | |
createChildren(); | |
} | |
private static const FONT_NAME:String = "ArialBoldFont"; // "_sans" | |
private function createChildren():void | |
{ | |
_layoutRect = new Rectangle(0, 0, _boxWidth, _boxHeight); | |
baseFontSize = 26; | |
numFmt = new TextFormat(FONT_NAME, baseFontSize, 0xe0e0e0); | |
captFmt = new TextFormat(FONT_NAME, baseFontSize, 0xe0e0e0); | |
numFmt.align = "right"; | |
captFmt.align = "left"; | |
// Text fields | |
addTextField( "Min:", captFmt); | |
addTextField( "Max:", captFmt); | |
addTextField( "000,000", numFmt, 120); | |
addTextField( "999,999", numFmt, 120); | |
} | |
public function applyLayout(scl:Number):void | |
{ | |
_layoutRect.width = _boxWidth * scl; | |
_layoutRect.height = _boxHeight * scl; | |
_scl = scl; | |
for (var i:int = 0; i < 4; i++) { | |
resizeText(getChildAt(i), baseFontSize, _scl); | |
} | |
placeElement(getChildAt(0), 0, 0); // top left | |
placeElement(getChildAt(1), 0, 0.6); // bottom left | |
placeElement(getChildAt(2), 0.4, 0); // top right | |
placeElement(getChildAt(3), 0.4, 0.6); // bottom right | |
alignToRight(getChildAt(2)); | |
alignToRight(getChildAt(3)); | |
} | |
private function resizeText(fld:DisplayObject, baseSize:Number, sclFactor:Number):void | |
{ | |
var textFld:TextFieldTextRenderer = fld as TextFieldTextRenderer; | |
if (!textFld) return; | |
var fmt:TextFormat = textFld.textFormat; | |
fmt.size = Math.round(sclFactor * baseSize); | |
// Invalidate to force a redraw | |
textFld.invalidate(); | |
} | |
private function addTextField( caption:String, fmt:TextFormat, w:Number=0, h:Number=0):DisplayObject | |
{ | |
var fld:DisplayObject = createText(caption, fmt, w, h); | |
return addChild(fld); | |
} | |
private function createText(caption:String, fmt:TextFormat, wid:Number=-1, ht:Number=-1):TextFieldTextRenderer | |
{ | |
var label:TextFieldTextRenderer= new TextFieldTextRenderer(); | |
label.textFormat= fmt; | |
label.text= caption; | |
label.embedFonts= true; | |
if (wid > 0) label.width= wid; | |
if (ht > 0) label.height=ht; | |
return label; | |
} | |
protected function alignToRight(d:DisplayObject, pad:Number=0):void | |
{ | |
d.x = _layoutRect.width - d.width - pad; | |
} | |
protected function placeElement(d:DisplayObject, leftPerc:Number=0, topPerc:Number=0):void | |
{ | |
d.x = 0 + leftPerc*_layoutRect.width; | |
d.y = 0 + topPerc * _layoutRect.height; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment