X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fg2d%2Fnodes%2FAnimatedSVGNode.java;h=1d0c9f795f81d8404caf4be42974c3bf26fa2230;hp=7e855293b0b11f0dce32850a9225839ddf93ee59;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/AnimatedSVGNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/AnimatedSVGNode.java index 7e855293b..1d0c9f795 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/AnimatedSVGNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/AnimatedSVGNode.java @@ -1,241 +1,241 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.scenegraph.g2d.nodes; - -import java.awt.Graphics2D; -import java.awt.geom.Rectangle2D; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; - -import org.simantics.scenegraph.utils.BufferedImage; -import org.simantics.scenegraph.utils.G2DUtils; -import org.simantics.scenegraph.utils.MipMapBufferedImage; -import org.simantics.scenegraph.utils.MipMapVRamBufferedImage; -import org.simantics.scenegraph.utils.VRamBufferedImage; - -import com.kitfox.svg.SVGCache; -import com.kitfox.svg.SVGElement; -import com.kitfox.svg.SVGException; -import com.kitfox.svg.SVGUniverse; -import com.kitfox.svg.xml.StyleAttribute; - -public class AnimatedSVGNode extends SVGNode { - /** - * - */ - private static final long serialVersionUID = 8698435757824280001L; - public Map valuezz = new HashMap(); - - protected String script = null; - - @PropertySetter("VariableFilter") - @SyncField("script") - public void setScript(String script) { - this.script = script; - } - - @SyncField("values") - public void setValue(String key, Object value) { - valuezz.put(key, value); - animate(); - } - - @PropertySetter("valuezz") - @SyncField("valuezz") - public void setValuezz(Map val) { - valuezz = val; - animate(); - } - - @ClientSide - public void animate() { - animate(script); - } - - @ClientSide - public void animate(final String script) { - if (script == null) - return; - - if (dataHash == null) { - dataHash = parseSVG(); - } - if (diagramCache == null) { - // Cannot execute script when SVGDiagram is not available - return; - } - -// AnimationExecutor.getInstance().animate(new IAnimation() { -// @Override -// public void run() { - try { - - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine engine = manager.getEngineByName("JavaScript"); - - for(String key : valuezz.keySet()) { - engine.put(key, valuezz.get(key)); - } - engine.put("time", System.currentTimeMillis()); - engine.put("svg", diagramCache); - engine.put("t", AnimatedSVGNode.this); - - //System.out.println("ANIMATE(" + engine.get("time") + ")"); - engine.eval(script); - - diagramCache.updateTime(0); - - buffer = null; - - } catch (ScriptException e) { - e.printStackTrace(); // Just report the error.. - } catch (Throwable t) { - t.printStackTrace(); - } - -// } -// }); - } - - protected Map eventAnimations = new HashMap(); - - @ClientSide - public void registerEventAnimation(String event, String animation) { - eventAnimations.put(event, animation); - } - - @ClientSide - public void animateEvent(String event) { - if(eventAnimations.containsKey(event)) { - animate(eventAnimations.get(event)); - } - } - - /** - * Use UUID hash - */ - @Override - protected String parseSVG() { - if (data == null) - return null; - try { - dataHash = UUID.randomUUID().toString(); - // NOTE: hard-coded to assume all SVG data is encoded in UTF-8 - InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8")); - SVGUniverse univ = SVGCache.getSVGUniverse(); - if (diagramCache != null) - univ.decRefCount(diagramCache.getXMLBase()); - diagramCache = univ.getDiagram(SVGCache.getSVGUniverse().loadSVG(is, dataHash), false); - documentCache = data; - try { - setBounds((Rectangle2D) diagramCache.getRoot().getBoundingBox().clone()); - } catch (SVGException e) { - setBounds((Rectangle2D) diagramCache.getViewRect().clone()); - } - univ.incRefCount(diagramCache.getXMLBase()); - } catch (IOException e) { - diagramCache = null; - } - - return dataHash; - } - - @SuppressWarnings("unused") - private void print(SVGElement e, int indent) { - for(int i=0;i valuezz = new HashMap(); + + protected String script = null; + + @PropertySetter("VariableFilter") + @SyncField("script") + public void setScript(String script) { + this.script = script; + } + + @SyncField("values") + public void setValue(String key, Object value) { + valuezz.put(key, value); + animate(); + } + + @PropertySetter("valuezz") + @SyncField("valuezz") + public void setValuezz(Map val) { + valuezz = val; + animate(); + } + + @ClientSide + public void animate() { + animate(script); + } + + @ClientSide + public void animate(final String script) { + if (script == null) + return; + + if (dataHash == null) { + dataHash = parseSVG(); + } + if (diagramCache == null) { + // Cannot execute script when SVGDiagram is not available + return; + } + +// AnimationExecutor.getInstance().animate(new IAnimation() { +// @Override +// public void run() { + try { + + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("JavaScript"); + + for(String key : valuezz.keySet()) { + engine.put(key, valuezz.get(key)); + } + engine.put("time", System.currentTimeMillis()); + engine.put("svg", diagramCache); + engine.put("t", AnimatedSVGNode.this); + + //System.out.println("ANIMATE(" + engine.get("time") + ")"); + engine.eval(script); + + diagramCache.updateTime(0); + + buffer = null; + + } catch (ScriptException e) { + e.printStackTrace(); // Just report the error.. + } catch (Throwable t) { + t.printStackTrace(); + } + +// } +// }); + } + + protected Map eventAnimations = new HashMap(); + + @ClientSide + public void registerEventAnimation(String event, String animation) { + eventAnimations.put(event, animation); + } + + @ClientSide + public void animateEvent(String event) { + if(eventAnimations.containsKey(event)) { + animate(eventAnimations.get(event)); + } + } + + /** + * Use UUID hash + */ + @Override + protected String parseSVG() { + if (data == null) + return null; + try { + dataHash = UUID.randomUUID().toString(); + // NOTE: hard-coded to assume all SVG data is encoded in UTF-8 + InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8")); + SVGUniverse univ = SVGCache.getSVGUniverse(); + if (diagramCache != null) + univ.decRefCount(diagramCache.getXMLBase()); + diagramCache = univ.getDiagram(SVGCache.getSVGUniverse().loadSVG(is, dataHash), false); + documentCache = data; + try { + setBounds((Rectangle2D) diagramCache.getRoot().getBoundingBox().clone()); + } catch (SVGException e) { + setBounds((Rectangle2D) diagramCache.getViewRect().clone()); + } + univ.incRefCount(diagramCache.getXMLBase()); + } catch (IOException e) { + diagramCache = null; + } + + return dataHash; + } + + @SuppressWarnings("unused") + private void print(SVGElement e, int indent) { + for(int i=0;i