My favorites
▼
|
Sign in
daleyjem
Code Repository for Jeremy Daley
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
com
/
daleyjem
/
as3
/
ExternalImage.as
‹r38
r60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.daleyjem.as3
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.ContextMenuEvent;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.net.FileReference;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.EventDispatcher;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.system.LoaderContext;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
[Event(name = "init", type = "flash.events.Event")]
[Event(name = "complete", type = "flash.events.Event")]
[Event(name = "progress", type = "flash.events.ProgressEvent")]
[Event(name="ioError", type="flash.events.IOErrorEvent")]
public class ExternalImage extends Sprite
{
public var image:Bitmap;
private var imageLoader:Loader = new Loader();
private var hasSmoothing:Boolean = false;
private var percLoaded:Number = 0;
private var imgPath:String;
private var bLoaded:Number = 0;
private var bTotal:Number = 0;
private var clickContextMenu:ContextMenu;
private var fileReference:FileReference;
private var hasBorder:Boolean = false;
private var borderThickness:Number;
private var borderColor:Number;
private var hasDrawnBorder:Boolean = false;
private var isInit:Boolean = false;
public var url:String = "";
public var props:Object = new Object();
/**
* Loads an image into a Sprite from the url specified and applies smoothing.
* Provides right-click menu options for emulating a browser's native image behaviors.
*
* @param imagePath <String> Uri of the image.
* @param addSmoothing <Boolean> Apply smoothing to the image.
* @param addRightClickMenu <Boolean> Adds a menu with "Save As" and "View Image" options to emulate browser image behaviors.
*/
public function ExternalImage(imagePath:String, addSmoothing:Boolean = false, addRightClickMenu:Boolean = false, loaderContext:LoaderContext = null):void
{
url = imagePath;
clickContextMenu = new ContextMenu();
imgPath = imagePath;
hasSmoothing = addSmoothing;
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, doLoadFail);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
if (loaderContext == null) loaderContext = new LoaderContext();
imageLoader.load(new URLRequest(imagePath), loaderContext);
if (addRightClickMenu)
{
var viewImageItem:ContextMenuItem = new ContextMenuItem("View Image");
var saveImageAsItem:ContextMenuItem = new ContextMenuItem("Save Image As...");
viewImageItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, viewImage);
saveImageAsItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, saveImageAs);
clickContextMenu.hideBuiltInItems();
clickContextMenu.customItems.push(viewImageItem);
clickContextMenu.customItems.push(saveImageAsItem);
contextMenu = clickContextMenu;
}
}
/**
* Gets or sets whether the image has smoothing on it.
*/
public function get smoothing():Boolean
{
return hasSmoothing;
}
public function set smoothing(_smoothing:Boolean):void
{
hasSmoothing = _smoothing;
image.smoothing = _smoothing;
}
/**
* Read only. Returns how many bytes of the image has loaded.
*/
public function get bytesLoaded():Number
{
return bLoaded;
}
/**
* Read only. Returns the total number of bytes for the image.
*/
public function get bytesTotal():Number
{
return bTotal;
}
/**
* Adds or removes smoothing to the image.
*/
public function toggleSmoothing():void
{
smoothing = smoothing ? false : true;
}
/**
* Adds a border around the image
* @param thickness <Number> Pixel thickness of image border.
* @param color <Number> Hex color of border color.
*/
public function addBorder(thickness:Number, color:Number):void
{
borderThickness = thickness;
borderColor = color;
hasBorder = true;
if (image != null && hasDrawnBorder == false) drawBorder();
}
private function viewImage(e:ContextMenuEvent):void
{
navigateToURL(new URLRequest(imgPath), "_blank");
}
private function saveImageAs(e:ContextMenuEvent):void
{
fileReference = new FileReference();
fileReference.download(new URLRequest(getQualifiedPath(imgPath)));
}
private function getQualifiedPath(origPath:String):String
{
if (origPath.indexOf("http://") > -1 || origPath.indexOf("https://") > -1) return origPath;
if (origPath.indexOf("../") > -1) origPath = origPath.replace("../", "");
var qualifiedPath:String = "";
var protocol:String = ExternalInterface.call("window.location.protocol.toString");
if (protocol == "http:" || protocol == "https:")
{
var domain:String = ExternalInterface.call("window.location.host.toString");
var path:String = ExternalInterface.call("window.location.pathname.toString");
if (path.indexOf("/") > -1) path = path.replace(path.split("/")[1], "");
protocol += "//";
origPath = formatRelativePath(qualifiedPath, origPath);
qualifiedPath += protocol + domain + path + origPath;
}
return qualifiedPath;
}
private function formatRelativePath(shortPath:String, relativePath:String):String
{
var backDirSplit:Array = relativePath.split("../");
var backDirCount:Number = backDirSplit.length - 1;
var endAppend:String = backDirSplit[backDirCount];
var shortSplit:Array = shortPath.split("/");
shortSplit.splice(shortSplit.length - 1, 1);
for (var i:Number = 0; i < backDirCount; i++)
{
shortSplit.splice(shortSplit.length - 1, 1);
}
return shortSplit.join("/") + endAppend;
}
private function dispose():void
{
imageLoader.removeEventListener(Event.COMPLETE, onComplete);
imageLoader.removeEventListener(IOErrorEvent.IO_ERROR, doLoadFail);
imageLoader.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}
private function onProgress(e:ProgressEvent):void
{
bTotal = e.bytesTotal;
bLoaded = e.bytesLoaded;
dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS));
if (!isInit)
{
isInit = true;
dispatchEvent(new Event(Event.INIT));
}
}
private function onComplete(e:Event):void
{
dispose();
var imageBMData:BitmapData = (imageLoader.content as Bitmap).bitmapData;
image = addChild(new Bitmap(imageBMData, "auto", true)) as Bitmap;
image.smoothing = hasSmoothing;
if (hasBorder && hasDrawnBorder == false) drawBorder();
dispatchEvent(new Event(Event.COMPLETE));
}
private function doLoadFail(e:IOErrorEvent):void
{
dispose();
dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
}
private function drawBorder():void
{
var border:Sprite = new Sprite();
border.graphics.lineStyle(borderThickness, borderColor);
border.graphics.beginFill(0, 0);
border.graphics.drawRect(0, 0, image.width, image.height);
border.graphics.endFill();
addChild(border);
hasDrawnBorder = true;
}
}
}
Show details
Hide details
Change log
r39
by daleyjem on Jan 14, 2011
Diff
[No log message]
Go to:
...om/daleyjem/as3/ExternalImage.as
.../com/daleyjem/as3/ExternalSWF.as
/trunk/com/daleyjem/as3/Tracer.as
...com/daleyjem/as3/system/Utils.as
...com/daleyjem/as3/utils/Arrays.as
...leyjem/as3/utils/DisplayTools.as
...k/com/daleyjem/as3/utils/HTML.as
...om/daleyjem/as3/utils/String2.as
Project members,
sign in
to write a code review
Older revisions
r38
by daleyjem on Jan 7, 2011
Diff
[No log message]
r3
by daleyjem on May 26, 2010
Diff
initial import
All revisions of this file
File info
Size: 7489 bytes, 224 lines
View raw file
Powered by
Google Project Hosting