1 50 51 package org.openlaszlo.iv.flash.commands; 52 53 import java.io.*; 54 import java.awt.geom.*; 55 import org.openlaszlo.iv.flash.parser.*; 56 import org.openlaszlo.iv.flash.api.*; 57 import org.openlaszlo.iv.flash.api.image.*; 58 import org.openlaszlo.iv.flash.api.shape.*; 59 import org.openlaszlo.iv.flash.util.*; 60 import org.openlaszlo.iv.flash.cache.*; 61 import org.openlaszlo.iv.flash.url.*; 62 63 import org.openlaszlo.iv.flash.context.Context; 64 65 78 public class InsertMediaCommand extends GenericCommand { 79 80 private static final int ALWAYS_SCALE = 0; 81 private static final int NEVER_SCALE = 1; 82 private static final int IFNEEDED_SCALE = 2; 83 84 public InsertMediaCommand() {} 85 86 public void doCommand( FlashFile file, Context context, Script parent, int frameNum ) throws IVException { 87 String filename = getParameter( context, "filename", "" ); 88 boolean cache = getBoolParameter( context, "cache", false ); 89 boolean expand = getBoolParameter( context, "expand", false ); 90 int scaletox = getIntParameter( context, "scalex", -1 ) * 20; 91 int scaletoy = getIntParameter( context, "scaley", -1 ) * 20; 92 String halign = getParameter( context, "halign", "left" ); 93 String valign = getParameter( context, "valign", "top" ); 94 boolean mask = getBoolParameter( context, "mask", false ); 95 boolean scaleproport = getBoolParameter(context, "scaleproport", false); 96 String instancename = getParameter( context, "instancename" ); 99 100 String mypscale = getParameter(context, "scale", "always"); 101 102 boolean neverScale = mypscale.equalsIgnoreCase("never") || mypscale.equalsIgnoreCase("false"); 103 boolean ifNeededScale = mypscale.equalsIgnoreCase("if needed"); 104 boolean alwaysScale = !neverScale && !ifNeededScale; 105 106 Instance inst = getInstance(); 107 108 IVUrl url = IVUrl.newUrl(filename, file); 109 110 Object media = null; 111 112 try { 113 media = file.addExternalMedia(url, cache); 114 } catch( IOException e ) { 115 throw new IVException(Resource.ERRCMDFILEREAD, new Object [] {url.getName(), getCommandName()}, e); 116 } 117 118 Script script; 119 double width, height; 120 121 if( media instanceof FlashFile ) { 122 FlashFile flashFile = (FlashFile) media; 124 synchronized(flashFile) { 125 script = flashFile.getMainScript(); 126 script.resetMain(); 127 if( flashFile.isTemplate() ) { 128 script.removeFileDepGlobalCommands(); 129 script = script.copyScript(); 130 } 131 } 132 if( flashFile.isTemplate() ) file.processScript(script, context); 133 134 Rectangle2D r = flashFile.getFrameSize(); width = r.getWidth(); height = r.getHeight(); 138 if( expand ) { 140 int myTotal = script.getFrameCount(); 142 parent.getFrameAt(frameNum+myTotal-1); 143 } 144 145 } else { 146 Bitmap bitmap = (Bitmap) media; 148 Instance myInst = bitmap.newInstance(); 149 width = bitmap.getWidth()*20; height = bitmap.getHeight()*20; 152 script = inst.copyScript(); 153 Frame myFrame = script.newFrame(); 154 myFrame.addInstance( myInst, 1 ); 155 } 156 157 inst.setScript( script ); 158 159 if( mask ) { 161 addMask(parent, frameNum, inst, GeomHelper.newRectangle(-1024, -1024, 2048, 2048)); 162 } 163 164 Rectangle2D winBounds = GeomHelper.getTransformedSize(inst.matrix, 165 GeomHelper.newRectangle(-1024, -1024, 2048, 2048)); 166 int winWidth = (int) winBounds.getWidth(); 167 int winHeight = (int) winBounds.getHeight(); 168 169 boolean deScale = true; 170 171 double scaleX, scaleY, translateX, translateY; 172 173 scaleX = scaleY = 1.0; 174 if( scaletox >= 0 || scaletoy >= 0 ) { 175 176 if( scaletox >= 0 ) { 177 if( !ifNeededScale || width > scaletox ) { 178 scaleX = scaletox / width; 179 width = scaletox; 180 } 181 } 182 183 if( scaletoy >= 0 ) { 184 if( !ifNeededScale || height > scaletoy ) { 185 scaleY = scaletoy / height; 186 height = scaletoy; 187 } 188 } 189 190 if( scaleproport ) { 191 if( scaletox < 0 ) { 192 scaleX = scaleY; 193 width = scaleX*width; 194 } 195 if( scaletoy < 0 ) { 196 scaleY = scaleX; 197 height = scaleY*height; 198 } 199 } 200 } else if( alwaysScale ) { 201 if( scaleproport ) { 202 scaleX = winWidth/width; 203 scaleY = winHeight/height; 204 if( scaleX < scaleY ) { 205 scaleY = scaleX; 206 } else { 207 scaleX = scaleY; 208 } 209 width *= scaleX; 210 height *= scaleY; 211 } else { 212 scaleX = 2048.0/width; 213 scaleY = 2048.0/height; 214 deScale = false; 215 } 216 } else if( ifNeededScale ) { 217 if( width > winWidth || height > winHeight ) { 218 scaleX = winWidth/width; 219 scaleY = winHeight/height; 220 if( scaleproport ) { 221 if( scaleX < scaleY ) { 222 scaleY = scaleX; 223 } else { 224 scaleX = scaleY; 225 } 226 } 227 width *= scaleX; 228 height *= scaleY; 229 } 230 } 231 232 if( deScale ) { 233 234 if( halign.equalsIgnoreCase( "left" ) ) { 236 translateX = -winWidth/2; 237 } else if( halign.equalsIgnoreCase( "right" ) ) { 238 translateX = -width+winWidth/2; 239 } else { translateX = -width/2; 241 } 242 243 if( valign.equalsIgnoreCase( "top" ) ) { 245 translateY = -winHeight/2; 246 } else if( valign.equalsIgnoreCase( "bottom" ) ) { 247 translateY = -height+winHeight/2; 248 } else { translateY = -height/2; 250 } 251 252 GeomHelper.deScaleMatrix( inst.matrix ); 253 } else { 254 translateX = translateY = -1024.0; 255 } 256 257 inst.matrix.concatenate( new AffineTransform(scaleX,0,0,scaleY,translateX,translateY) ); 258 259 if( instancename != null ) { 260 inst.name = instancename; 261 } 262 } 263 264 } 265 | Popular Tags |