package { import flash.display.Sprite; [SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "#FFFFFF")] /** * FileReference による画像ファイル操作処理テンプレート * テンプレなので、画像ファイルのロードと、読み込んだ画像のセーブしかおこないません * @author Aquioux(Yoshida, Akio) */ public class Main extends Sprite { public function Main():void { // model var model:Model = new Model(); // controller var controller:Controller = new Controller(model); addChild(controller); // _view var view:View = new View(model); addChild(view); // 参照のセット controller.view = view; } } } import com.adobe.images.PNGEncoder; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.Stage; import flash.events.Event; import flash.events.EventDispatcher; import flash.net.FileReference; import flash.utils.ByteArray; /** * Model * @author YOSHIDA, Akio (Aquioux) */ class Model extends EventDispatcher { // ---------- パブリックメンバ ---------- // // 完了した処理の種別 static public const LOAD:String = "load"; static public const SAVE:String = "save"; // 外部へ通知する完了した処理の種別(LOAD または SAVE) public function get process():String { return _process; } private var _process:String; // View へ渡すデータ public function get data():BitmapData { return _data; } private var _data:BitmapData; // ---------- ローカルメンバ ---------- // private var fileRef_:FileReference; private var loader_:Loader; // ---------- パブリックメソッド ---------- // /** * コンストラクタ */ public function Model() { fileRef_ = new FileReference(); loader_ = new Loader(); } /** * 画像ロード step 1 ファイル選択 * Controller 向けに開かれたメソッド */ public function loadHandler():void { fileRef_.addEventListener(Event.SELECT, load2Handler); fileRef_.browse(); } /** * 画像セーブ step 1 ファイル選択 * Controller 向けに開かれたメソッド * @param bmd 画像として保存する BitmapData */ public function saveHandler(bmd:BitmapData):void { var png:ByteArray = PNGEncoder.encode(bmd); fileRef_.addEventListener(Event.SELECT, save2Handler); fileRef_.save(png, ".png"); } // ---------- ローカルメソッド ---------- // // 画像ロード step 2 ファイル読込 private function load2Handler(e:Event):void { fileRef_.removeEventListener(Event.SELECT, arguments.callee); fileRef_.addEventListener(Event.COMPLETE, load3Handler); fileRef_.load(); } // 画像ロード step 3 ファイル読込完了 private function load3Handler(e:Event):void { fileRef_.removeEventListener(Event.COMPLETE, arguments.callee); loader_.loadBytes(fileRef_.data); loader_.contentLoaderInfo.addEventListener(Event.COMPLETE, load4Handler); } // 画像ロード step 4 ファイル読込後の処理 private function load4Handler(e:Event):void { loader_.contentLoaderInfo.removeEventListener(Event.COMPLETE, arguments.callee); _data = Bitmap(loader_.content).bitmapData; _process = LOAD; dispatchEvent(new Event(Event.CHANGE)); } // 画像セーブ step 2 private function save2Handler(event:Event):void { fileRef_.removeEventListener(Event.SELECT, arguments.callee); _process = SAVE; dispatchEvent(new Event(Event.CHANGE)); } } import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.geom.Matrix; /** * View * @author YOSHIDA, Akio (Aquioux) */ class View extends Sprite { // ---------- パブリックメンバ ---------- // // Model の参照 public function set model(value:Model):void { _model = value; } private var _model:Model; // Controller の参照 public function set controller(value:Controller):void { _controller = value; } private var _controller:Controller; // 表示領域の辺の最大値 // ドキュメントクラスからユーザが設定可能 public function set maxSize(value:uint):void { _maxSize = value; } private var _maxSize:uint = 256; // セーブする BitmapData // Controller 向けに開かれた変数 public function get canvas():BitmapData { return bmd_; } // ---------- ローカルメンバ ---------- // private var bm_:Bitmap; // メイン表示 Bitmap private var bmd_:BitmapData; // メイン表示 Bitmap 用の BitmapData // ---------- パブリックメソッド ---------- // /** * コンストラクタ * @param model Model */ public function View(model:Model) { _model = model; _model.addEventListener(Event.CHANGE, changeHandler); bm_ = new Bitmap(); addChild(bm_); } // ---------- ローカルメソッド ---------- // // Model から Event.CHANGE が発行されたときの処理(ロード時の処理) private function changeHandler(e:Event):void { if (_model.process == Model.LOAD) { var orgBmd:BitmapData = _model.data; var orgWidth:uint = orgBmd.width; var orgHeight:uint = orgBmd.height; var scale:Number = Math.min(_maxSize / orgWidth, _maxSize / orgHeight); if (bmd_) bmd_.dispose(); bmd_ = new BitmapData(orgWidth * scale, orgHeight * scale); bmd_.draw(orgBmd, new Matrix(scale, 0, 0, scale)); bm_.bitmapData = bmd_; bm_.smoothing = true; bm_.x = uint((stage.stageWidth - bm_.width) / 2); bm_.y = uint((stage.stageHeight - bm_.height) / 2); orgBmd.dispose(); } } } import com.bit101.components.PushButton; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; /** * Controller * @author YOSHIDA, Akio (Aquioux) */ class Controller extends Sprite { // ---------- パブリックメンバ ---------- // // Model の参照 public function set model(value:Model):void { _model = value; } private var _model:Model; // View の参照 public function set view(value:View):void { _view = value; } private var _view:View; // ---------- ローカルメンバ ---------- // private var loadButton_:PushButton; // ロードボタン private var saveButton_:PushButton; // セーブボタン // ---------- パブリックメソッド ---------- // /** * コンストラクタ * @param model Model */ public function Controller(model:Model) { _model = model; _model.addEventListener(Event.CHANGE, changeHandler); // ボタンの幅 var buttonWidth:uint = 50; // ロードのボタン loadButton_ = new PushButton(this, 0, 0, "LOAD", loadHandler); loadButton_.width = buttonWidth; // セーブのボタン saveButton_ = new PushButton(this, buttonWidth - 1, 0, "SAVE", saveHandler); saveButton_.width = buttonWidth; buttonEbled(saveButton_, false); // ボタン機能を無効に } // ---------- ローカルメソッド ---------- // // ロードボタンのイベントハンドラ private function loadHandler(e:MouseEvent):void { _model.loadHandler(); } // セーブボタンのイベントハンドラ private function saveHandler(e:MouseEvent):void { // View からセーブする領域の BitmapData を取得し、Model へ渡す _model.saveHandler(_view.canvas); } // Model から Event.CHANGE が発行されたときの処理 private function changeHandler(e:Event):void { if (_model.process == Model.LOAD) { // 画像ロード完了時 buttonEbled(loadButton_, false); buttonEbled(saveButton_, true); } if (_model.process == Model.SAVE) { // 画像セーブ完了時 buttonEbled(loadButton_, true); buttonEbled(saveButton_, false); } } // ボタンの使用可否切り替え private function buttonEbled(button:PushButton, flg:Boolean):void { button.mouseEnabled = flg; button.alpha = flg ? 1.0 : 0.5; } } FileReference による画像ファイル操作処理テンプレート