ActionScript Learning 110414-2

4.14.2011

Flashで大量のデータをEmbedするときに便利なAIRプログラム。

仕事で大量に画像データを埋め込む必要があったので選択したフォルダ内のデータ一覧を [Embed(source = "sample.jpg")] public var classname:Class;のように書き出すAIRプログラム。

flash.filesystemはAIRのみなのでAIRプログラムとして書き出した。 同一データの埋め込み時の問題を回避するためスケール9の設定もしている。

VIEW CODE

package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import fl.controls.*;
	import flash.text.*;
	
	import flash.filesystem.*;

	public class NMain extends MovieClip {
		
		//-------------------------------------------------------------------------------- Properties

		private var _this:*;
		private var _stage:Stage;
		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function NMain() {
			
			_this = this;
			_stage = stage;
			
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.align = StageAlign.TOP_LEFT;
			
			Init();
		}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * initialize
		 */
		public function Init():void {

			setup();
		}
		
		/**
		 * setup
		 */
		private function setup():void {

			setup_button_select();
			setup_button_makeit();
			setup_text_l();
			setup_text_r();
			setup_input_path();
			setup_input_clname();
			
			setup_action();
		}
		
		/**
		 * make
		 */
		private var directory:File;
		//
		private function setup_action():void {
			
			directory = File.desktopDirectory;
		}		
		/** */
		private function directoryselect(eo:*):void {
			
			try {
				
				directory.browseForDirectory("Select Directory");
				directory.addEventListener(Event.SELECT, directoryselectaction);
			}
			catch(error:Error) {
				
				trace(error.message);
			}
		}
		//
		private function directoryselectaction(eo:*):void {
			
			directory = eo.target as File;
			
			var files:Array = directory.getDirectoryListing();
			
			var text_l = new String();
			
			for(var i:int = 0; i < files.length; i++) {
				
				var filename = files[i].name;
				
				text_l += filename + "\n";
			}
			
			_text_l.text = text_l;
		}
		/** */
		private function makeit(eo:*):void {			
			
			var files:Array = directory.getDirectoryListing();
			
			var text_r = new String();
			
			for(var i:int = 0; i < files.length; i++) {
				
				var filename = files[i].name;
				
				text_r += "[Embed(source='" + (_input_path.text + filename) + "', scaleGridTop='0', scaleGridLeft='0', scaleGridRight='1', scaleGridBottom='1')]\n";
				text_r += "public var " + (_input_clname.text + filename.split(".")[0]) + ":Class;\n"
			}
			
			_text_r.text = text_r;
		}
		
		/**
		 * setup ui
		 */
		/** */
		private var _button_select:Button;
		
		private function setup_button_select():void {
			
			_button_select = new Button();
			_button_select.label = "Select";
			_button_select.useHandCursor = true;
			_button_select.width = 100;
			_button_select.height = 20;
			_button_select.x = 10;
			_button_select.y = 445;
			
			_this.addChild(_button_select);
			
			_button_select.addEventListener(MouseEvent.CLICK, directoryselect);
		}
		/** */
		private var _button_makeit:Button;
		
		private function setup_button_makeit():void {
			
			_button_makeit = new Button();
			_button_makeit.label = "Make it !";
			_button_makeit.useHandCursor = true;
			_button_makeit.width = 100;
			_button_makeit.height = 20;
			_button_makeit.x = 850;
			_button_makeit.y = 445;
			
			_this.addChild(_button_makeit);
			
			_button_makeit.addEventListener(MouseEvent.CLICK, makeit);
		}
		/** */
		private var _text_l:TextArea;
		
		private function setup_text_l():void {
			
			_text_l = new TextArea();
			_text_l.x = 10;
			_text_l.y = 10;
			_text_l.width = 460;
			_text_l.height = 420;
			
			_text_l.wordWrap = false;
			
			_this.addChild(_text_l);
		}
		/** */
		private var _text_r:TextArea;
		
		private function setup_text_r():void {
			
			_text_r = new TextArea();
			_text_r.x = 490;
			_text_r.y = 10;
			_text_r.width = 460;
			_text_r.height = 420;
			
			_text_r.wordWrap = false;
			
			_this.addChild(_text_r);
		}
		/** */
		private var _label_path:TextField;
		private var _input_path:TextInput;
		
		private function setup_input_path():void {
			
			_label_path = new TextField();
			_label_path.width = 200;
			_label_path.text = "path:";
			_label_path.x = 120;
			_label_path.y = 445;
			
			_this.addChild(_label_path);
			
			
			_input_path = new TextInput();
			_input_path.x = _label_path.x + _label_path.textWidth + 2;
			_input_path.y = _label_path.y;
			_input_path.width = 305;
			_input_path.height = 20;
			
			_this.addChild(_input_path);
		}
		/** */
		private var _label_clname:TextField;
		private var _input_clname:TextInput;
		
		private function setup_input_clname():void {
			
			_label_clname = new TextField();
			_label_clname.width = 200;
			_label_clname.text = "classname:";
			_label_clname.x = _input_path.x + _input_path.width + 15;
			_label_clname.y = _input_path.y;
			
			_this.addChild(_label_clname);
			
			
			_input_clname = new TextInput();
			_input_clname.x = _label_clname.x + _label_clname.textWidth + 2;
			_input_clname.y = _label_clname.y;
			_input_clname.width = 305;
			_input_clname.height = 20;
			
			_this.addChild(_input_clname);
		}
	}
}






category : AIR / ActionScript / Flash

Demonstrations

Feature Samples

Author

虹村 マキオウ (nizimura makiou)

猫と太極拳を愛する横浜在住のフリーランスクリエイターです。

logo

Demo and Sample

Category