Advertisement
YellowAfterlife

nme.native textfield.autoSize bug

Jan 19th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.06 KB | None | 0 0
  1. package ;
  2.  
  3. import nme.display.Sprite;
  4. import nme.events.Event;
  5. import nme.geom.Matrix;
  6. import nme.Lib;
  7. import nme.text.TextField;
  8. import nme.text.TextFieldAutoSize;
  9. import nme.text.TextFormat;
  10.  
  11. /**
  12.  * Illustrates a bug with textField.width/textField.height returned
  13.  * if textField.autoSize is set.
  14.  *
  15.  * Replicating:
  16.  * 1. Run example and resize window/movie
  17.  * 2. Compare native and flash output:
  18.  * flash: width varies between 150 and 200 depending on window size,
  19.  *        since fonts appear to be drawn only on integer sizes in flash.
  20.  * native: width depends entirely on window size, falling to 77px for 0.5x
  21.  *        window scale, and rising to 800+px for 5x scaled window.
  22.  *
  23.  * It does look that native behaviour should not occur, since width/height
  24.  * properties do not need to take transformation matrix into account.
  25.  *
  26.  * P.S.: Not talking about Jeas-- browser.* output here, since [of course]
  27.  * that is much further from a pleasing / functional condition.
  28.  *
  29.  * @author YellowAfterlife
  30.  */
  31.  
  32. class Main extends Sprite {
  33.     var sprite:Sprite;
  34.     var field:TextField;
  35.     /** Stage RESIZE event handler */
  36.     function onResize(e) {
  37.         // Calculate scale & size:
  38.         var scale = Math.min(stage.stageWidth, stage.stageHeight) / 200;
  39.         var posX = (stage.stageWidth - 200 * scale) / 2;
  40.         var posY = (stage.stageHeight - 200 * scale) / 2;
  41.         // Set position & scale:
  42.         sprite.x = posX;
  43.         sprite.y = posY;
  44.         sprite.scaleX = sprite.scaleY = scale;
  45.         // (around this moment, TextField gets resized)
  46.         // Setting output text two times, since .width/.height seem to update
  47.         // only on second text change.
  48.         for (i in 0 ... 2) field.text =
  49.         '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n'
  50.         + 'Current size: \n'
  51.         + 'Width: ' + field.width + 'px\n'
  52.         + 'Height: ' + field.height + 'px\n'
  53.         + 'Scale: ' + (Std.int(sprite.scaleX * 1000) / 1000) + 'x\n';
  54.     }
  55.     /** Initialization function */
  56.     private function init(e) {
  57.         // Create a sprite with a 200x200 rectangle:
  58.         sprite = new Sprite();
  59.         sprite.graphics.beginFill(0xf7f7f7);
  60.         sprite.graphics.drawRect(0, 0, 200, 200);
  61.         sprite.graphics.endFill();
  62.         addChild(sprite);
  63.         // Create a TextField that is going to be both a
  64.         // test subject and output log at the same time:
  65.         field = new TextField();
  66.         field.defaultTextFormat = new TextFormat('_typewriter', 13); // has no functional effect
  67.         field.background = true; // has no functional effect
  68.         field.autoSize = TextFieldAutoSize.LEFT; // source of problems
  69.         sprite.addChild(field);
  70.         // Add a window/movie resize event and call it for first time:
  71.         Lib.current.stage.addEventListener(Event.RESIZE, onResize);
  72.         onResize(null);
  73.     }
  74.     /// Nothing interesting past this line.
  75.     public function new() {
  76.         super();
  77.         #if iphone
  78.         Lib.current.stage.addEventListener(Event.RESIZE, init);
  79.         #else
  80.         addEventListener(Event.ADDED_TO_STAGE, init);
  81.         #end
  82.     }
  83.    
  84.     @:keep static public function main() {
  85.         var stage = Lib.current.stage;
  86.         stage.scaleMode = nme.display.StageScaleMode.NO_SCALE;
  87.         stage.align = nme.display.StageAlign.TOP_LEFT;
  88.        
  89.         Lib.current.addChild(new Main());
  90.     }
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement