/*
*/
package com.kamon.study.editmap
{
import com.kamon.study.utils.mapDomain;
import com.kamon.study.utils.Windows;
import com.kamon.study.utils.page;
import com.kamon.study.utils.MapEditorConstant;
import com.kamon.study.helpers.*;
import com.kamon.study.TotalData;
import com.kamon.study.utils.xmlWriter;
import com.kamon.study.MapProgram;
import com.kamon.study.Map.mapRect;
import com.kamon.study.events.getXmlEvent;
import mx.graphics.codec.IImageEncoder;
import mx.graphics.codec.JPEGEncoder;
import mx.graphics.codec.PNGEncoder;
import flash.display.BitmapData;
import flash.utils.ByteArray;
import flash.geom.Matrix;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.LocalConnection;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.MovieClip;
import fl.containers.ScrollPane;
import flash.geom.Rectangle;
import flash.geom.Point;
public class libListLayer extends Sprite{
//加载进来的元素OBJECT
private var elementObject:Object=new Object();
//加载
private var sources:Sprite = null;
private var _mapEdit:mapEdit=null; //装饰房间
private var _toolBar:*=null;
private var _shopmc:*=null; //我的物品面板
private var _uId:int; //用户ID
public var _mId:int; //地图ID
private var _sp:ScrollPane = null;
private var _selectZoomElementList:elementZoomList=null; //在原大上显示物品
private var _selectElementList:elementList=null; //但前选中的元素
public static const LOAD_COMPLETE:String = 'lib_list_load_complete';
public static const LOADJPG_COMPLETE:String = 'load_jpg_complete';
private var mapProgram:MapProgram=null; //地图管理器
private var _mapRect:mapRect=null; //小地图
private var _page:page; //分页类
//control
public function libListLayer(mapp:MapProgram,obj:Object,uId:int,mId:int):void {
//return;
this.mapProgram = mapp;
for(var i:String in obj) {
this.elementObject[i] = obj[i];
}
this._uId = uId;
this._mId = mId;
Init();
}
private function Init():void {
this.sources = new Sprite();
var w:Windows = new Windows();
w.sources(this.sources);
this.createMapEdit();
}
//创建我的物品列表 开始加载数据
private function createList():void {
var c:Class = mapDomain.getDefinition("shopmc") as Class;
if(c){
this._shopmc = new c();
var w:Windows = new Windows();
w.sources(this._shopmc);
}
var obj:Object = new Object();
obj['limit'] = 5;
//要求数据库输出CONTENT
obj['content'] = 1;
obj['account_id'] = this._uId ;
obj['namespace'] = 'mylib';
obj['file'] = this.mapProgram.getKeyValue('GET_ELEMENT_XML');
this._page = new page(obj);
this._page.addEventListener(getXmlEvent.COMPLETE_XML,completeXmlHandler);
this._page.firstPage();
}
//加载XML成功
private function completeXmlHandler(event:getXmlEvent):void {
trace('event.xml',event.xml)
var xml:XML = event.xml.copy();/*
for(var i:int=0;i<temp.length();i++) {
trace(i,'===>',temp[i].content);
}
*/
var p:page = page(event.currentTarget);
//add resources
var array:Array = this.mapProgram.addResouse(xml,p.getKeyValue('namespace'));
//设置总数
p.totalCount = int(xml.count);
this._shopmc.pagenum_txt.text = p._thisPage + '/' + p._pagecount;
trace(p._thisTotal ,'p._thisTotal ',p._thisPage,'_thisPage');
//add eventlistener
if(!this._shopmc.page_mc.hasEventListener(MouseEvent.MOUSE_DOWN)){
this._shopmc.page_mc.addEventListener(MouseEvent.MOUSE_DOWN,changPageHandler);
}
this.showElementList(array);
//array=null;
}
//创建我的物品列表 加载数据成功 显示 我的物品
private function showElementList(array:Array):void {
this.clearList();
var el:elementList;
for(var i:int=0;i<array.length;i++) {
el = new elementList(array[i]);
el.addEventListener(elementList.SET_ELEMENT,showZoom);
el.y = i * 60;
this._shopmc.left_sp.addChild(el);
}
}
//chage handler 切换页码
private function changPageHandler(event:MouseEvent) :void {
var name:String = event.target.name.toString();
switch( name ) {
case 'pre_btn':
this._page.prevPage();
break;
case 'next_btn':
this._page.nextPage();
break;
default : break;
}
}
//清除所有列表
private function clearList(event:Event=null):void {
if(this._shopmc) {
for(var i:int =0; i<this._shopmc.left_sp.numChildren ;i++) {
var el = this._shopmc.left_sp.getChildAt(i);
if(el is elementList){
el.dispose();
el.removeEventListener(elementList.SET_ELEMENT,showZoom);
this._shopmc.left_sp.removeChild(el);
i--;
}
}
}
}
public function setElementHandler(event:Event) :void {
this._selectZoomElementList.visible =false;
var el:elementZoomList = elementZoomList(event.currentTarget);
this._mapEdit.buildElement(el.eObject,el.postion);
}
//在原大上显示 创建
public function showZoom(event:Event):void {
this._selectElementList = elementList(event.currentTarget);
if(this._selectZoomElementList != null) {
this._selectZoomElementList.dispose();
this._selectZoomElementList.dispatchEvent(new Event(Windows.DISPOSE));
this._selectZoomElementList =null;
}
this._selectZoomElementList = new elementZoomList(this._selectElementList.eObject);
this._selectZoomElementList.addEventListener(elementZoomList.SET_ZOOM_ELEMENT,setElementHandler);
this._selectZoomElementList.visible =true;
var w:Windows = new Windows();
w.sources(this._selectZoomElementList);
}
//创建编辑面板
private function createMapEdit():void{
this._mapEdit = new mapEdit(this);
var path:String = this.mapProgram.getKeyValue('myMapsPath');
this._mapEdit.selectMapData(path + this._mId);
this._mapEdit.addEventListener(Event.COMPLETE,completeHandler);
this._mapEdit.addEventListener(mapEdit.ADD_TO_ELEMENT,addElementComplete);
this._mapEdit.addEventListener(mapEdit.DEL_TO_ELEMENT,delElementComplete);
}
//
private function addElementComplete(event:Event):void {
/*if(this._selectElementList) {
this._selectElementList.useElement();
}*/
this._mapEdit.removeBuildBrush();
if(this._selectZoomElementList != null) {
this._selectZoomElementList.dispose();
this._selectZoomElementList.dispatchEvent(new Event(Windows.DISPOSE));
this._selectZoomElementList =null;
}
}
private function delElementComplete(event:Event):void {
/*if(this._selectElementList) {
var w:Windows = new Windows();
w.alert('count111111'+this._selectElementList.count);
this._selectElementList.reElement();
w = new Windows();
w.alert('count22222222222'+this._selectElementList.count);
}*/
}
//加载地图成功
private function completeHandler(event:Event):void {
this._mapEdit.removeEventListener(Event.COMPLETE,completeHandler);
this.sources.addChild(this._mapEdit);
this.createRect();
}
//创建地图遮罩
private function createRect():void {
this._mapRect = new (mapDomain.getDefinition("com.kamon.study.Map.mapRect") as Class);
this._mapRect.x = 775;
this._mapRect.addEventListener(mapRect.MOVE_AREA,moveAreaHandler);
this.sources.addChild(this._mapRect);
this.createTool();
}
//moveAreaHandler
private function moveAreaHandler(event:Event):void {
var mRect:mapRect = mapRect(event.currentTarget);
var rect:Rectangle = this._mapEdit.scrollRect;
if( !rect ) {
rect = new Rectangle(mRect.rectX,mRect.rectY,mRect.rectW,mRect.rectH);
} else {
rect.x = mRect.rectX ;
rect.y = mRect.rectY ;
rect.width = mRect.rectW;
rect.height = mRect.rectH;
}
this._mapEdit.scrollRect = rect;
mRect = null;
rect = null;
}
//创建工具栏
private function createTool():void {
var c:Class = mapDomain.getDefinition("edit_menu_mc") as Class;
this._toolBar = new c();
this._toolBar.addEventListener(MouseEvent.CLICK,this.toolHandler);
this._toolBar.y = 650;
this.sources.addChild(this._toolBar);
this._toolBar.mouseEnabled = false;
//20100312
var point:Point = new Point(0,0);
var mapW:Number = this._mapEdit.mapWidth;
var mapH:Number = this._mapEdit.mapHeight;
//显示区域
this.mapProgram.setMapRect(this._mapRect,mapW,mapH,point);
//this._mapRect.initShowArea(this._mapEdit.mapWidth,this._mapEdit.mapHeight,0,0,1000,650);
this.dispatchEvent(new Event(libListLayer.LOAD_COMPLETE));
}
//工具栏事件
private function toolHandler(event:MouseEvent):void {
var name:String = event.target.name.toString();
switch(name){
case 'show_myshop_btn': //显示我的物品列表
//this.clearList(null);
this.createList();
break;
case 'save_map_btn'://保存数据
if(this._mapEdit){
var xml:XML = this._mapEdit.getMapXml();
var xmlwrite:xmlWriter = new xmlWriter(xml,this.getElmentCount(),this._uId,this._mId);
xmlwrite.addEventListener(Event.COMPLETE, saveXmlCompleteHandler);
savemapimage();
}
break;
case 'removebuild_btn': //删除建筑物
if(this._mapEdit){
this._mapEdit.removeBuildBrush();
this._mapEdit.removeBuild();
}
break;
case 'resetbuild_btn': //清除笔刷
if(this._mapEdit){
this._mapEdit.removeBuildBrush();
}
break;
case 'exit_btn': //退出
var w:Windows = new Windows();
w.alert('你将离开编辑模式,是否退出?');
w.addEventListener(Windows.SUBMIT,exitHandler);
break;
case 'showarea_btn' : //显示小地图
if(this._mapRect)
this._mapRect.dispatchEvent(new Event(mapRect.AREA_OPEN));
break;
default:
break;
}
}
//保存图片
private function savemapimage():void {
var matrix:Matrix = new Matrix();
matrix.scale(170/this._mapEdit.width, 110/this._mapEdit.height);
//matrix.tx = this._mapEdit.width/2+75;
//保存图片
var jpgSource:BitmapData = new BitmapData(170,110);
jpgSource.draw(this._mapEdit,matrix);
//this.mapProgram.host_ip.PutInfo(this._mapEdit);
var jpgEncoder:JPEGEncoder = new JPEGEncoder(300);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
var domain:String = 'localhost';
var LOCALCONN:LocalConnection = new LocalConnection();
domain = LOCALCONN.domain;
//设置请求链接和图片文件名称
var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("http://"+domain+"/jpg_encoder_download2.php?name="+ this._uId +"_"+ this._mId);
//save(\diy"+amfPHP.getUser+".jpg")
//this.mapProgram.host_ip.PutInfo("icon_" + this._uId +"_"+ this._mId +".jpg");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = jpgStream;
var loader:URLLoader = new URLLoader(jpgURLRequest);
//发送数据请求
loader.load(jpgURLRequest);
//添加数据发送结束的事件处理,服务器返回的数据会放入loader的data属性中
loader.addEventListener(Event.COMPLETE, JPGloaded_Handler);
this.dispatchEvent(new Event(libListLayer.LOADJPG_COMPLETE));
}
private function s(e:Event):void
{
trace("成功保存图片!");
}
//退出编辑
private function exitHandler(event:Event):void {
var w:Windows = Windows(event.currentTarget);
w.removeEventListener(Windows.SUBMIT,exitHandler);
this.dispose();
}
//保存数据
private function saveXmlCompleteHandler(event:Event):void {
event.currentTarget.removeEventListener(Event.COMPLETE,saveXmlCompleteHandler);
this.dispatchEvent(new Event(Event.COMPLETE));
}
//
function clickHandler(event:MouseEvent):void {
var name:String = event.target.name.toString();
name = name.split(',')[0];
if(this.elementObject[name] ==null || this._mapEdit == null) return;
this._mapEdit.buildElement(this.elementObject[name]);
}
//注销
public function dispose():void {
this.resetElementCount();
this.elementObject = null;
this.mapProgram.show();
this.mapProgram=null;
if(this._mapEdit) {
this._mapEdit.removeEventListener(Event.COMPLETE,completeHandler);
this._mapEdit.removeEventListener(mapEdit.ADD_TO_ELEMENT,addElementComplete);
this._mapEdit.removeEventListener(mapEdit.DEL_TO_ELEMENT,delElementComplete);
this._mapEdit.parent.removeChild(this._mapEdit);
}
if(this._shopmc && this._shopmc.parent) {
this._shopmc.parent.removeChild(this._shopmc);
}
if(this.sources) {
if(this._toolBar) {
this.sources.removeChild(this._toolBar);
}
this.sources.dispatchEvent(new Event(Windows.DISPOSE));
//this.sources.parent.removeChild(this.sources);
}
}
//根据链接名获取元素对象
public function getElement(id:String):* {
return this.mapProgram.getElement(id);
}
public function getElmentCount(r:Boolean=false):Object {
return this.mapProgram.getElmentCount(r);
}
//退出编辑时把所有 未改变的我的物品count变成 -1
public function resetElementCount():void {
var objct:Object = this.getElmentCount(true);
for each(var eObj:elementObj in objct) {
eObj.count = -1;
}
}
//获取地图数据
public function getMapXml():XML {
return this._mapEdit.mapXML.copy();
}
//获取配置值
public function getKeyValue(name:String):String {
return this.mapProgram.getKeyValue(name);
}
}
}