Chapter 30 Example 5

by actionscriptbible
♥0 | Line 66 | Modified 2010-02-04 18:57:52 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tGx9
 */

package {
  import flash.display.Sprite;
  import flash.events.*;
  import flash.net.*;
  import flash.text.*;
  public class ch30ex5 extends Sprite {
    protected var tf:TextField;
    protected var fileRef:FileReference;
    public function ch30ex5() {
      var btn:TestButton;
      btn = new TestButton(100, 25, "Load");
      btn.addEventListener(MouseEvent.CLICK, onLoadClick);
      btn.x = btn.y = 20;
      addChild(btn);
      btn = new TestButton(100, 25, "Save");
      btn.addEventListener(MouseEvent.CLICK, onSaveClick);
      btn.x = 130; btn.y = 20;
      addChild(btn);
      
      tf = new TextField();
      tf.type = TextFieldType.INPUT;
      tf.defaultTextFormat = new TextFormat("_sans", 12, 0);
      tf.multiline = tf.wordWrap = tf.border = true;
      tf.width = stage.stageWidth - 40;
      tf.height = stage.stageHeight - 75;
      tf.x = 20; tf.y = 55;
      addChild(tf);
    }
    protected function onLoadClick(event:Event):void {
      fileRef = new FileReference();
      fileRef.browse([new FileFilter("Text files", "*.txt"),
        new FileFilter("All Files", "*")]);
      fileRef.addEventListener(Event.SELECT, onLoadSelect, false, 0, true);
    }
    protected function onLoadSelect(event:Event):void {
      fileRef.load();
      fileRef.addEventListener(Event.COMPLETE, onLoadComplete);
    }
    protected function onLoadComplete(event:Event):void {
      tf.text = fileRef.data.readUTFBytes(fileRef.data.length);
    }
    protected function onSaveClick(event:Event):void {
      fileRef = new FileReference();
      fileRef.save(tf.text, "yourfilesir.txt");
    }
  }
}
import flash.display.*;
import flash.text.*;
class TestButton extends Sprite {
  public var label:TextField;
  public function TestButton(w:Number, h:Number, labelText:String) {
    graphics.lineStyle(0.5, 0, 0, true);
    graphics.beginFill(0xa0a0a0);
    graphics.drawRoundRect(0, 0, w, h, 8);
    label = new TextField();
    addChild(label);
    label.defaultTextFormat = new TextFormat("_sans", 11, 0, true, false,
      false, null, null, "center");
    label.width = w;
    label.height = h;
    label.text = labelText;
    label.y = (h - label.textHeight)/2 - 2;
    buttonMode = true;
    mouseChildren = false;
  }
}