Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ;
- import nme.display.Sprite;
- import nme.events.Event;
- import nme.geom.Matrix;
- import nme.Lib;
- import nme.text.TextField;
- import nme.text.TextFieldAutoSize;
- import nme.text.TextFormat;
- /**
- * Illustrates a bug with textField.width/textField.height returned
- * if textField.autoSize is set.
- *
- * Replicating:
- * 1. Run example and resize window/movie
- * 2. Compare native and flash output:
- * flash: width varies between 150 and 200 depending on window size,
- * since fonts appear to be drawn only on integer sizes in flash.
- * native: width depends entirely on window size, falling to 77px for 0.5x
- * window scale, and rising to 800+px for 5x scaled window.
- *
- * It does look that native behaviour should not occur, since width/height
- * properties do not need to take transformation matrix into account.
- *
- * P.S.: Not talking about Jeas-- browser.* output here, since [of course]
- * that is much further from a pleasing / functional condition.
- *
- * @author YellowAfterlife
- */
- class Main extends Sprite {
- var sprite:Sprite;
- var field:TextField;
- /** Stage RESIZE event handler */
- function onResize(e) {
- // Calculate scale & size:
- var scale = Math.min(stage.stageWidth, stage.stageHeight) / 200;
- var posX = (stage.stageWidth - 200 * scale) / 2;
- var posY = (stage.stageHeight - 200 * scale) / 2;
- // Set position & scale:
- sprite.x = posX;
- sprite.y = posY;
- sprite.scaleX = sprite.scaleY = scale;
- // (around this moment, TextField gets resized)
- // Setting output text two times, since .width/.height seem to update
- // only on second text change.
- for (i in 0 ... 2) field.text =
- '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n'
- + 'Current size: \n'
- + 'Width: ' + field.width + 'px\n'
- + 'Height: ' + field.height + 'px\n'
- + 'Scale: ' + (Std.int(sprite.scaleX * 1000) / 1000) + 'x\n';
- }
- /** Initialization function */
- private function init(e) {
- // Create a sprite with a 200x200 rectangle:
- sprite = new Sprite();
- sprite.graphics.beginFill(0xf7f7f7);
- sprite.graphics.drawRect(0, 0, 200, 200);
- sprite.graphics.endFill();
- addChild(sprite);
- // Create a TextField that is going to be both a
- // test subject and output log at the same time:
- field = new TextField();
- field.defaultTextFormat = new TextFormat('_typewriter', 13); // has no functional effect
- field.background = true; // has no functional effect
- field.autoSize = TextFieldAutoSize.LEFT; // source of problems
- sprite.addChild(field);
- // Add a window/movie resize event and call it for first time:
- Lib.current.stage.addEventListener(Event.RESIZE, onResize);
- onResize(null);
- }
- /// Nothing interesting past this line.
- public function new() {
- super();
- #if iphone
- Lib.current.stage.addEventListener(Event.RESIZE, init);
- #else
- addEventListener(Event.ADDED_TO_STAGE, init);
- #end
- }
- @:keep static public function main() {
- var stage = Lib.current.stage;
- stage.scaleMode = nme.display.StageScaleMode.NO_SCALE;
- stage.align = nme.display.StageAlign.TOP_LEFT;
- Lib.current.addChild(new Main());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement