1 50 51 package org.openlaszlo.iv.flash.api.button; 52 53 import java.io.PrintStream ; 54 import java.awt.geom.*; 55 56 import org.openlaszlo.iv.flash.util.*; 57 import org.openlaszlo.iv.flash.parser.*; 58 import org.openlaszlo.iv.flash.api.*; 59 import org.openlaszlo.iv.flash.api.image.*; 60 import org.openlaszlo.iv.flash.api.action.*; 61 import org.openlaszlo.iv.flash.context.Context; 62 63 66 public class Button extends FlashDef { 67 68 protected IVVector buttonRecords = new IVVector(); 69 protected IVVector conditions = new IVVector(4); 70 protected ButtonCXForm buttonCXForm; 71 protected ButtonSound buttonSound; 72 protected boolean isProcessed; 73 74 public Button() {} 75 76 public int getTag() { 77 return Tag.DEFINEBUTTON; 78 } 79 80 public void setButtonCXForm( ButtonCXForm bcxf ) { 81 this.buttonCXForm = bcxf; 82 } 83 84 public ButtonCXForm getButtonCXForm() { 85 return this.buttonCXForm; 86 } 87 88 public void setButtonSound( ButtonSound bsnd ) { 89 this.buttonSound = bsnd; 90 } 91 92 public ButtonSound getButtonSound() { 93 return this.buttonSound; 94 } 95 96 public void addButtonRecord( ButtonRecord br ) { 97 buttonRecords.addElement(br); 98 } 99 100 public IVVector getButtonRecords() { 101 return buttonRecords; 102 } 103 104 public void setButtonRecords( IVVector buttonRecords ) { 105 this.buttonRecords = buttonRecords; 106 } 107 108 public void addActionCondition( ActionCondition ac ) { 109 conditions.addElement(ac); 110 } 111 112 public IVVector getActionConditions() { 113 return conditions; 114 } 115 116 public void setActionConditions( IVVector conditions ) { 117 this.conditions = conditions; 118 } 119 120 public static Button parse( Parser p ) { 121 Button o = new Button(); 122 o.setID( p.getUWord() ); 124 o.parseButtonRecords(p,false); 125 o.addActionCondition( new ActionCondition(0,new Program(p.getBuf(), p.getPos(), p.getTagEndPos())) ); 126 return o; 127 } 128 129 protected void parseButtonRecords( Parser p, boolean withAlpha ) { 130 for(;;) { 131 int flags = p.getUByte(); 132 if( flags == 0 ) break; 133 ButtonRecord br = new ButtonRecord(); 134 br.setStates( flags ); 135 br.setDef( p.getDef( p.getUWord() ) ); 136 br.setLayer( p.getUWord() ); 137 br.setMatrix( p.getMatrix() ); 138 br.setCXForm( CXForm.parse(p, withAlpha) ); 139 addButtonRecord(br); 140 } 141 } 142 143 protected void writeButtonRecords( FlashOutput fob ) { 144 for( int i=0; i<buttonRecords.size(); i++ ) { 145 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 146 t.write(fob); 147 } 148 fob.writeByte(0); 149 } 150 151 public void collectDeps( DepsCollector dc ) { 152 for( int i=0; i<buttonRecords.size(); i++ ) { 154 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 155 dc.addDep( t.getDef() ); 157 } 158 if( buttonSound != null ) buttonSound.collectDeps(dc); 159 } 160 161 public void collectFonts( FontsCollector fc ) { 162 for( int i=0; i<buttonRecords.size(); i++ ) { 163 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 164 t.getDef().collectFonts(fc); 165 } 166 } 167 168 173 public Rectangle2D getBounds() { 174 Rectangle2D rect = null; 175 Rectangle2D bounds = GeomHelper.newRectangle(); 176 for( int i=0; i<buttonRecords.size(); i++ ) { 177 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 178 GeomHelper.calcBounds(t.getMatrix(), t.getDef().getBounds(), bounds); 181 rect = GeomHelper.add( rect, bounds ); 182 } 183 return rect; 184 } 185 186 public void write( FlashOutput fob ) { 187 int pos = fob.getPos(); 188 fob.skip(6); 189 fob.writeDefID( this ); 190 writeButtonRecords(fob); 191 ((ActionCondition)conditions.elementAt(0)).getProgram().write(fob); 192 fob.writeLongTagAt(getTag(), fob.getPos()-pos-6, pos); 193 writeExternals( fob ); 194 } 195 196 protected void writeExternals( FlashOutput fob ) { 197 if( buttonCXForm != null ) { 198 buttonCXForm.setButton( this ); 199 buttonCXForm.write(fob); 200 } 201 if( buttonSound != null ) { 202 buttonSound.setButton( this ); 203 buttonSound.write(fob); 204 } 205 } 206 207 public void printContent( PrintStream out, String indent ) { 208 out.println( indent+"Button("+Tag.tagNames[getTag()]+"): id="+getID() ); 209 for( int i=0; i<buttonRecords.size(); i++ ) { 210 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 211 t.printContent(out, indent); 212 } 213 for( int i=0; i<conditions.size(); i++ ) { 214 ActionCondition ac = (ActionCondition) conditions.elementAt(i); 215 ac.printContent(out, indent); 216 } 217 } 218 219 protected boolean _isConstant() { 220 for( int i=0; i<buttonRecords.size(); i++ ) { 221 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 222 if( !t.getDef().isConstant() ) return false; 223 } 224 for( int i=0; i<conditions.size(); i++ ) { 225 ActionCondition ac = (ActionCondition) conditions.elementAt(i); 226 if( !ac.getProgram().isConstant() ) return false; 227 } 228 return true; 229 } 230 231 public void process( FlashFile file, Context context ) throws IVException { 232 for( int i=0; i<buttonRecords.size(); i++ ) { 233 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 234 FlashDef def = t.getDef(); 235 if( !isProcessed() ) { 236 file.processObject(def, context); 237 } 238 } 239 } 240 241 public boolean isProcessed() { 242 return isProcessed; 243 } 244 245 public void setProcessed() { 246 this.isProcessed = true; 247 } 248 249 public void apply( Context context ) { 250 super.apply( context ); 251 for( int i=0; i<buttonRecords.size(); i++ ) { 252 ButtonRecord t = (ButtonRecord) buttonRecords.elementAt(i); 253 t.getDef().apply(context); 254 } 255 for( int i=0; i<conditions.size(); i++ ) { 256 ActionCondition ac = (ActionCondition) conditions.elementAt(i); 257 ac.getProgram().apply(context); 258 } 259 } 260 261 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 262 super.copyInto( item, copier ); 263 ((Button)item).buttonRecords = buttonRecords.getCopy(copier); 264 ((Button)item).conditions = conditions.getCopy(copier); 265 ((Button)item).buttonCXForm = buttonCXForm != null? (ButtonCXForm)buttonCXForm.getCopy(copier): null; 266 ((Button)item).buttonSound = buttonSound != null? (ButtonSound)buttonSound.getCopy(copier): null; 267 ((Button)item).isProcessed = isProcessed; 268 return item; 269 } 270 271 public FlashItem getCopy( ScriptCopier copier ) { 272 return copyInto( new Button(), copier ); 273 } 274 275 } 276 277 | Popular Tags |