From 18320e63bf5befe789bf8faccc126aea232e32b0 Mon Sep 17 00:00:00 2001 From: Nickolay Grebenshikov Date: Mon, 27 Oct 2014 13:40:11 +0700 Subject: [PATCH] Fix ngrebenshikov/openfl-snapsvg#66 "Make StablexUI working" --- openfl/Assets.hx | 35 +++++++--------------- openfl/display/Bitmap.hx | 24 +++++++-------- openfl/display/BitmapData.hx | 27 +++++------------ openfl/display/DisplayObject.hx | 14 ++++----- openfl/display/DisplayObjectContainer.hx | 1 - openfl/display/Graphics.hx | 13 +++++--- openfl/display/Stage.hx | 4 +-- openfl/events/EventDispatcher.hx | 8 ++--- openfl/text/TextField.hx | 24 +++++++++------ openfl/text/TextFormat.hx | 4 +++ templates/html5/template/index.html | 38 ++++++++++++++++-------- 11 files changed, 97 insertions(+), 95 deletions(-) diff --git a/openfl/Assets.hx b/openfl/Assets.hx index 9fa1daa..dfd7e1e 100644 --- a/openfl/Assets.hx +++ b/openfl/Assets.hx @@ -1516,7 +1516,7 @@ class Assets { macro public static function embedBitmap ():Array { - #if (html5 && !openfl_html5_dom) + #if (html5 && !openfl_snapsvg) var fields = embedData (":bitmap", true); #else var fields = embedData (":bitmap"); @@ -1527,14 +1527,13 @@ class Assets { var constructor = macro { #if html5 - #if openfl_html5_dom + #if openfl_snapsvg super (width, height, transparent, fillRGBA); var currentType = Type.getClass (this); if (preload != null) { - ___textureBuffer.width = Std.int (preload.width); ___textureBuffer.height = Std.int (preload.height); rect = new openfl.geom.Rectangle (0, 0, preload.width, preload.height); @@ -1544,15 +1543,10 @@ class Assets { } else { var byteArray = openfl.utils.ByteArray.fromBytes (haxe.Resource.getBytes(resourceName)); - if (onload != null && !Std.is (onload, Bool)) { - __loadFromBytes(byteArray, null, onload); - } else { - __loadFromBytes(byteArray); - } } @@ -1562,27 +1556,18 @@ class Assets { super (0, 0, transparent, fillRGBA); if (preload != null) { - - __sourceImage = preload; - width = __sourceImage.width; - height = __sourceImage.height; - + __sourceCanvas = preload.__sourceCanvas; + width = preload.__sourceCanvas.width; + height = preload.__sourceCanvas.height; } else { __loadFromBase64 (haxe.Resource.getString(resourceName), resourceType, function (b) { - - if (preload == null) { - - preload = b.__sourceImage; - - } - if (onload != null) { - onload (b); - } - + if (preload == null) { + preload = b; + } }); } @@ -1603,7 +1588,7 @@ class Assets { #if html5 args.push ({ name: "onload", opt: true, type: macro :Dynamic, value: null }); - #if openfl_html5_dom + #if openfl_snapsvg fields.push ({ kind: FVar(macro :openfl.display.BitmapData, null), name: "preload", doc: null, meta: [], access: [ APublic, AStatic ], pos: Context.currentPos() }); #else fields.push ({ kind: FVar(macro :js.html.Image, null), name: "preload", doc: null, meta: [], access: [ APublic, AStatic ], pos: Context.currentPos() }); @@ -1696,7 +1681,7 @@ class Assets { super(); - #if openfl_html5_dom + #if openfl_snapsvg nmeFromBytes (haxe.Resource.getBytes (resourceName)); #else __fromBytes (haxe.Resource.getBytes (resourceName)); diff --git a/openfl/display/Bitmap.hx b/openfl/display/Bitmap.hx index 4bfe08b..fe2fbfc 100644 --- a/openfl/display/Bitmap.hx +++ b/openfl/display/Bitmap.hx @@ -143,16 +143,13 @@ class Bitmap extends DisplayObject { override public function __render (inMask:SnapElement = null, clipRect:Rectangle = null):Void { - if (!__combinedVisible) return; - if (bitmapData == null) return; - + if (!__combinedVisible || bitmapData == null) return; + if (_matrixInvalid || _matrixChainInvalid) { - __validateMatrix (); - } - var imageDataLease = bitmapData.__getLease (); + var imageDataLease = bitmapData.__getLease (); if (imageDataLease != null && (__currentLease == null || imageDataLease.seed != __currentLease.seed || imageDataLease.time != __currentLease.time)) { var srcCanvas: CanvasElement = bitmapData.handle (); var child = snap.select('*'); @@ -170,21 +167,24 @@ class Bitmap extends DisplayObject { handleGraphicsUpdated (null); } //TODO: uncomment - if (inMask != null) { +// if (inMask != null) { // // __applyFilters (__graphics.__surface); // var m = getBitmapSurfaceTransform (__graphics); // Lib.__drawToSurface (__graphics.__surface, inMask, m, (parent != null ? parent.__combinedAlpha : 1) * alpha, clipRect, smoothing); // - } else { - if (__testFlag (DisplayObject.TRANSFORM_INVALID)) { +// } else { + if (__testFlag(DisplayObject.TRANSFORM_INVALID)) { var m = getSurfaceTransform (); __setTransform (m); - __clearFlag (DisplayObject.TRANSFORM_INVALID); + __clearFlag(DisplayObject.TRANSFORM_INVALID); } var el: Element = cast(snap.node); - el.setAttribute('opacity', Std.string(alpha)); - } + var alphaStr = Std.string(alpha); + if (el.getAttribute('opacity') != alphaStr) { + el.setAttribute('opacity', alphaStr); + } +// } //updateClipRect(); } diff --git a/openfl/display/BitmapData.hx b/openfl/display/BitmapData.hx index 01229f5..819f240 100644 --- a/openfl/display/BitmapData.hx +++ b/openfl/display/BitmapData.hx @@ -1362,9 +1362,7 @@ class BitmapData implements IBitmapDrawable { public inline function __getLease ():ImageDataLease { - return __lease; - } @@ -1374,7 +1372,6 @@ class BitmapData implements IBitmapDrawable { var canvas = ___textureBuffer; var drawImage = function (_) { - canvas.width = img.width; canvas.height = img.height; @@ -1382,11 +1379,12 @@ class BitmapData implements IBitmapDrawable { ctx.drawImage (img, 0, 0); rect = new Rectangle (0, 0, canvas.width, canvas.height); - + __buildLease (); + + __sourceImage = cast(img); + if (onload != null) { - onload (this); - } } @@ -1530,15 +1528,10 @@ class BitmapData implements IBitmapDrawable { if (image.complete) { } } - - - + // Event Handlers - - - - + private function __onLoad (data:LoadData, e) { var canvas:CanvasElement = cast data.texture; @@ -1551,6 +1544,8 @@ class BitmapData implements IBitmapDrawable { var ctx:CanvasRenderingContext2D = canvas.getContext ("2d"); ctx.drawImage (data.image, 0, 0, width, height); + + __sourceImage = cast(data.image); data.bitmapData.width = width; data.bitmapData.height = height; @@ -1569,14 +1564,8 @@ class BitmapData implements IBitmapDrawable { } - - - // Getters & Setters - - - private inline function get_height ():Int { if ( ___textureBuffer != null ) { diff --git a/openfl/display/DisplayObject.hx b/openfl/display/DisplayObject.hx index a180c05..4cfcaf2 100644 --- a/openfl/display/DisplayObject.hx +++ b/openfl/display/DisplayObject.hx @@ -384,9 +384,7 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { private inline function __clearFlag (mask:Int):Void { - ___renderFlags &= ~mask; - } @@ -626,21 +624,23 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { var fullAlpha:Float = (parent != null ? parent.__combinedAlpha : 1) * alpha; - if (inMask != null) { +// if (inMask != null) { //TODO: uncomment // var m = getSurfaceTransform (gfx); // Lib.__drawToSurface (gfx.__surface, inMask, m, fullAlpha, clipRect); - } else { +// } else { if (__testFlag (TRANSFORM_INVALID)) { - var m = getSurfaceTransform (); __setTransform (m); __clearFlag (TRANSFORM_INVALID); } var el: Element = cast(snap.node); - el.setAttribute('opacity', Std.string(alpha)); - } + var alphaStr = Std.string(alpha); + if (el.getAttribute('opacity') != alphaStr) { + el.setAttribute('opacity', alphaStr); + } +// } if (null != clipRect) { updateClipRect(clipRect); diff --git a/openfl/display/DisplayObjectContainer.hx b/openfl/display/DisplayObjectContainer.hx index 5826cbc..22d9bb2 100644 --- a/openfl/display/DisplayObjectContainer.hx +++ b/openfl/display/DisplayObjectContainer.hx @@ -538,7 +538,6 @@ class DisplayObjectContainer extends InteractiveObject { if (child.__visible) { if (clipRect != null) { if (child._matrixInvalid || child._matrixChainInvalid) { - //child.invalidateGraphics (); child.__validateMatrix (); } } diff --git a/openfl/display/Graphics.hx b/openfl/display/Graphics.hx index 73f59db..fc68df2 100644 --- a/openfl/display/Graphics.hx +++ b/openfl/display/Graphics.hx @@ -1092,11 +1092,12 @@ class Graphics { private function __addStrokeAttribute(element: SnapElement, lineJob: LineJob):Void { + var htmlElement: Element = cast(element.node); if(lineJob != null){ - if(lineJob.grad == null){ - element.attr({stroke: createCanvasColor(lineJob.colour, lineJob.alpha)}); + if (lineJob.grad == null) { + htmlElement.setAttribute('stroke', createCanvasColor(lineJob.colour, lineJob.alpha)); } else { - element.attr({stroke: createCanvasGradient(lineJob.grad)}); + element.attr({ stroke: createCanvasGradient(lineJob.grad)}); } element.attr({ 'stroke-width': lineJob.thickness, @@ -1122,7 +1123,7 @@ class Graphics { } }); } else { - element.attr({ stroke: "none" }); + htmlElement.setAttribute('stroke', 'none'); } } @@ -1240,6 +1241,10 @@ class Graphics { __snap.append(circle); case SnapDrawable.RECT(x, y, width, height, rx, ry): + if (x < 0) x = 0; + if (y < 0) y = 0; + if (width < 0) width = 0; + if (height < 0) height = 0; var rect: SnapElement = Lib.snap.rect(x, y, width, height, rx, ry); __addStrokeAttribute(rect, d.lineJobs.length == 1 ? d.lineJobs[0] : null); diff --git a/openfl/display/Stage.hx b/openfl/display/Stage.hx index 6096b03..5e8243e 100644 --- a/openfl/display/Stage.hx +++ b/openfl/display/Stage.hx @@ -491,10 +491,10 @@ class Stage extends DisplayObjectContainer { } __uIEventsQueueIndex = 0; - this.dispatchEvent(new Event(Event.ENTER_FRAME)); + __broadcast(new Event(Event.ENTER_FRAME)); if (__invalid) { - this.dispatchEvent(new Event(Event.RENDER)); + __broadcast(new Event(Event.RENDER)); } this.__renderAll (); this.dispatchEvent(new Event(Event.STAGE_RENDERED)); diff --git a/openfl/events/EventDispatcher.hx b/openfl/events/EventDispatcher.hx index 772dce0..5c83b1c 100644 --- a/openfl/events/EventDispatcher.hx +++ b/openfl/events/EventDispatcher.hx @@ -30,10 +30,10 @@ class EventDispatcher implements IEventDispatcher { public function addEventListener (type:String, inListener:Dynamic -> Void, useCapture:Bool = false, inPriority:Int = 0, useWeakReference:Bool = false):Void { - var stage = Lib.__getStage(); - if ((type == Event.RENDER || type == Event.ENTER_FRAME || type == Event.STAGE_RENDERED) && null != stage && this != stage) { - return stage.addEventListener(type, inListener, useCapture, inPriority, useWeakReference); - } +// var stage = Lib.__getStage(); +// if ((type == Event.RENDER || type == Event.ENTER_FRAME || type == Event.STAGE_RENDERED) && null != stage && this != stage) { +// return stage.addEventListener(type, inListener, useCapture, inPriority, useWeakReference); +// } var capture:Bool = (useCapture == null ? false : useCapture); var priority:Int = (inPriority==null ? 0 : inPriority); diff --git a/openfl/text/TextField.hx b/openfl/text/TextField.hx index 8f98c36..1000452 100644 --- a/openfl/text/TextField.hx +++ b/openfl/text/TextField.hx @@ -285,9 +285,11 @@ class TextField extends InteractiveObject { char_idx = wrapParagraph(paragraph, wrap, char_idx, mSelStart, mSelEnd); } - mTextSnap.selectAll("tspan").forEach(function (s) { - s.remove(); - }, this); + var textNode = mTextSnap.node; + var spans = [for (i in 0...textNode.childNodes.length) textNode.childNodes.item(i)]; + for (s in spans) { + textNode.removeChild(s); + } var svgBuf: StringBuf = new StringBuf(); var firstParagraph = true; @@ -675,7 +677,7 @@ class TextField extends InteractiveObject { lineWidth += s.rect.width; } } - if (paragraph.align == TextFormatAlign.CENTER) { + if (paragraph.align == TextFormatAlign.CENTER && null != startSpan) { startSpan.startX = Math.floor((mUserWidth - lineWidth)/2); } @@ -1324,11 +1326,11 @@ class TextField extends InteractiveObject { } - private function get_maxScrollH ():Int { return 0; } - private function get_maxScrollV ():Int { return 0; } + private function get_maxScrollH ():Int { return scrollH; } + private function get_maxScrollV ():Int { return scrollV; } private function get_multiline ():Bool { return multiline; } private function set_multiline (value:Bool):Bool { return multiline = value; } - private function get_numLines ():Int { return 0; } + private function get_numLines ():Int { return if (null != mText) 1 else 0; } private function get_scrollH ():Int { return scrollH; } private function set_scrollH (value:Int):Int { return scrollH = value; } private function get_scrollV ():Int { return scrollV; } @@ -1349,16 +1351,20 @@ class TextField extends InteractiveObject { public function set_text (inText:String):String { - mText = Std.string(inText); + if (mText == inText) return inText; + + mText = inText; if (!multiline) { mText = StringTools.replace(mText, '\n', ''); } + //mHTMLText = inText; mHTMLMode = false; RebuildText (); __invalidateBounds (); dispatchEvent(new Event(Event.CHANGE)); - return mText; + + return mText; } diff --git a/openfl/text/TextFormat.hx b/openfl/text/TextFormat.hx index 49f8029..0ddbb79 100644 --- a/openfl/text/TextFormat.hx +++ b/openfl/text/TextFormat.hx @@ -43,6 +43,10 @@ class TextFormat { rightMargin = in_rightMargin; indent = in_indent; leading = in_leading; + + if (null == leading) { + leading = 0.0; + } } diff --git a/templates/html5/template/index.html b/templates/html5/template/index.html index 61e2000..9efdd22 100644 --- a/templates/html5/template/index.html +++ b/templates/html5/template/index.html @@ -22,17 +22,7 @@ @@ -42,7 +32,31 @@ ::if (LIB_OPENFL)::
- + + + + +