Skip to content

Commit 22bd51f

Browse files
committed
Split downloadThumbnail() into overridable parts
1 parent ff1dc31 commit 22bd51f

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/util/Server.as

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
package util {
2929
import by.blooddy.crypto.serialization.JSON;
3030

31+
import flash.display.BitmapData;
3132
import flash.display.Loader;
3233
import flash.events.ErrorEvent;
3334
import flash.events.Event;
3435
import flash.events.HTTPStatusEvent;
3536
import flash.events.IOErrorEvent;
3637
import flash.events.SecurityErrorEvent;
38+
import flash.geom.Matrix;
3739
import flash.net.SharedObject;
3840
import flash.net.URLLoader;
3941
import flash.net.URLLoaderDataFormat;
@@ -94,7 +96,7 @@ public class Server implements IServer {
9496
// Re-trying here should help project save failures but we'll need to add more code to re-try loading projects
9597
if (event is SecurityErrorEvent) {
9698
var urlPathStart:int = url.indexOf('/', 10);
97-
var policyFileURL:String = url.substr(0, urlPathStart) + '/crossdomain.xml?cb='+Math.random();
99+
var policyFileURL:String = url.substr(0, urlPathStart) + '/crossdomain.xml?cb=' + Math.random();
98100
Security.loadPolicyFile(policyFileURL);
99101
Scratch.app.log('Reloading policy file from : ' + policyFileURL);
100102
}
@@ -142,6 +144,7 @@ public class Server implements IServer {
142144
}
143145

144146
var httpStatus:int = 0;
147+
145148
function errorHandler(event:ErrorEvent):void {
146149
removeListeners();
147150
onCallServerError(url, data, event);
@@ -231,7 +234,7 @@ public class Server implements IServer {
231234
return serverGet(url, whenDone);
232235
}
233236

234-
public function getThumbnail(idAndExt:String, w:int, h:int, whenDone:Function):URLLoader {
237+
protected function downloadThumbnail(url:String, w:int, h:int, whenDone:Function):URLLoader {
235238
function decodeImage(data:ByteArray):void {
236239
if (!data || data.length == 0) return; // no data
237240
var decoder:Loader = new Loader();
@@ -244,23 +247,40 @@ public class Server implements IServer {
244247
Scratch.app.logException(e);
245248
}
246249
else {
247-
Scratch.app.logMessage('Server caught exception decoding image: ' + idAndExt);
250+
Scratch.app.logMessage('Server caught exception decoding image: ' + url);
248251
}
249252
}
250253
}
251254

252255
function imageError(e:IOErrorEvent):void {
253-
Scratch.app.log('ServerOnline failed to decode image: ' + idAndExt);
256+
Scratch.app.log('ServerOnline failed to decode image: ' + url);
254257
}
255258

256259
function imageDecoded(e:Event):void {
257-
whenDone(e.target.content.bitmapData);
260+
whenDone(makeThumbnail(e.target.content.bitmapData));
258261
}
259262

260-
var url:String = getCdnStaticSiteURL() + 'medialibrarythumbnails/' + idAndExt;
261263
return serverGet(url, decodeImage);
262264
}
263265

266+
private static function makeThumbnail(bm:BitmapData):BitmapData {
267+
const tnWidth:int = 120;
268+
const tnHeight:int = 90;
269+
var result:BitmapData = new BitmapData(tnWidth, tnHeight, true, 0);
270+
if ((bm.width == 0) || (bm.height == 0)) return result;
271+
var scale:Number = Math.min(tnWidth / bm.width, tnHeight / bm.height);
272+
var m:Matrix = new Matrix();
273+
m.scale(scale, scale);
274+
m.translate((tnWidth - (scale * bm.width)) / 2, (tnHeight - (scale * bm.height)) / 2);
275+
result.draw(bm, m);
276+
return result;
277+
}
278+
279+
public function getThumbnail(idAndExt:String, w:int, h:int, whenDone:Function):URLLoader {
280+
var url:String = getCdnStaticSiteURL() + 'medialibrarythumbnails/' + idAndExt;
281+
return downloadThumbnail(url, w, h, whenDone);
282+
}
283+
264284
// -----------------------------
265285
// Translation Support
266286
//------------------------------

0 commit comments

Comments
 (0)