| 1 | /** |
|---|
| 2 | * Image model class of the players MCV pattern. |
|---|
| 3 | * |
|---|
| 4 | * @author Jeroen Wijering |
|---|
| 5 | * @version 1.5 |
|---|
| 6 | **/ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import com.jeroenwijering.players.*; |
|---|
| 10 | import com.jeroenwijering.utils.ImageLoader; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | class com.jeroenwijering.players.ImageModel extends AbstractModel { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** array with extensions used by this model **/ |
|---|
| 17 | private var mediatypes:Array = new Array("jpg","gif","png","swf"); |
|---|
| 18 | /** ImageLoader instance **/ |
|---|
| 19 | private var imageLoader:ImageLoader; |
|---|
| 20 | /** Clip to load the image into **/ |
|---|
| 21 | private var imageClip:MovieClip; |
|---|
| 22 | /** interval ID of image duration function **/ |
|---|
| 23 | private var positionInterval:Number; |
|---|
| 24 | /** current state **/ |
|---|
| 25 | private var currentState:Number; |
|---|
| 26 | /** boolean to check for current SWF **/ |
|---|
| 27 | private var isSWF:Boolean; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | /** Constructor **/ |
|---|
| 31 | function ImageModel(vws:Array,ctr:AbstractController,cfg:Object, |
|---|
| 32 | fed:Object,imc:MovieClip,scl:Boolean) { |
|---|
| 33 | super(vws,ctr,cfg,fed); |
|---|
| 34 | imageClip = imc; |
|---|
| 35 | var ref = this; |
|---|
| 36 | if(arguments[5] == true) { |
|---|
| 37 | imageLoader = new ImageLoader(imageClip,config["overstretch"], |
|---|
| 38 | config["width"],config["height"]); |
|---|
| 39 | } else { |
|---|
| 40 | imageLoader = new ImageLoader(imageClip); |
|---|
| 41 | } |
|---|
| 42 | imageLoader.onLoadFinished = function() { |
|---|
| 43 | ref.currentState = 2; |
|---|
| 44 | ref.sendUpdate("state",2); |
|---|
| 45 | ref.sendUpdate("load",100); |
|---|
| 46 | }; |
|---|
| 47 | imageLoader.onLoadProgress = function(tgt,btl,btt) { |
|---|
| 48 | ref.sendUpdate("load",Math.round(btl/btt*100)); |
|---|
| 49 | }; |
|---|
| 50 | imageLoader.onMetaData = function() { |
|---|
| 51 | ref.sendUpdate("size",this.sourceWidth,this.sourceHeight); |
|---|
| 52 | if(this.sourceLength > ref.feeder.feed[ref.currentItem]["duration"]) { |
|---|
| 53 | ref.feeder.feed[ref.currentItem]["duration"] = this.sourceLength; |
|---|
| 54 | } |
|---|
| 55 | }; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /** Start display interval for a specific image **/ |
|---|
| 60 | private function setStart(pos:Number) { |
|---|
| 61 | if(pos < 1 ) { |
|---|
| 62 | pos = 0; |
|---|
| 63 | } else if (pos > feeder.feed[currentItem]["duration"] - 1) { |
|---|
| 64 | pos = feeder.feed[currentItem]["duration"] - 1; |
|---|
| 65 | } |
|---|
| 66 | clearInterval(positionInterval); |
|---|
| 67 | if(feeder.feed[currentItem]["file"] != currentURL) { |
|---|
| 68 | imageClip._visible = true; |
|---|
| 69 | currentURL = feeder.feed[currentItem]["file"]; |
|---|
| 70 | if(feeder.feed[currentItem]["file"].indexOf(".swf") == -1) { |
|---|
| 71 | isSWF = false; |
|---|
| 72 | } else { |
|---|
| 73 | isSWF = true; |
|---|
| 74 | } |
|---|
| 75 | imageLoader.loadImage(feeder.feed[currentItem]["file"]); |
|---|
| 76 | currentState = 1; |
|---|
| 77 | sendUpdate("state",1); |
|---|
| 78 | sendUpdate("load",0); |
|---|
| 79 | } else { |
|---|
| 80 | currentState = 2; |
|---|
| 81 | sendUpdate("state",2); |
|---|
| 82 | } |
|---|
| 83 | if (pos != undefined) { |
|---|
| 84 | currentPosition = pos; |
|---|
| 85 | isSWF == true ? imageClip.mc.gotoAndPlay(pos*20): null; |
|---|
| 86 | if(pos == 0) {sendUpdate("time",0,feeder.feed[currentItem]["duration"]); } |
|---|
| 87 | } else { |
|---|
| 88 | isSWF == true ? imageClip.mc.play(): null; |
|---|
| 89 | } |
|---|
| 90 | positionInterval = setInterval(this,"updatePosition",100); |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | /** Read and broadcast the current position of the song **/ |
|---|
| 95 | private function updatePosition() { |
|---|
| 96 | if(currentState == 2) { |
|---|
| 97 | currentPosition += 0.1; |
|---|
| 98 | if(currentPosition >= feeder.feed[currentItem]["duration"]) { |
|---|
| 99 | currentState = 3; |
|---|
| 100 | sendUpdate("state",3); |
|---|
| 101 | sendCompleteEvent(); |
|---|
| 102 | } else { |
|---|
| 103 | sendUpdate("time",currentPosition,feeder.feed[currentItem]["duration"]-currentPosition); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /** stop the image display interval **/ |
|---|
| 110 | private function setPause(pos:Number) { |
|---|
| 111 | if(pos < 1 ) { |
|---|
| 112 | pos = 0; |
|---|
| 113 | } else if (pos > feeder.feed[currentItem]["duration"] - 1) { |
|---|
| 114 | pos = feeder.feed[currentItem]["duration"] - 1; |
|---|
| 115 | } |
|---|
| 116 | clearInterval(positionInterval); |
|---|
| 117 | currentState = 0; |
|---|
| 118 | sendUpdate("state",0); |
|---|
| 119 | if(pos != undefined) { |
|---|
| 120 | currentPosition = pos; |
|---|
| 121 | sendUpdate("time",currentPosition,feeder.feed[currentItem]["duration"]-currentPosition); |
|---|
| 122 | isSWF == true ? imageClip.mc.gotoAndStop(pos*20+1): null; |
|---|
| 123 | } else { |
|---|
| 124 | isSWF == true ? imageClip.mc.stop(): null; |
|---|
| 125 | } |
|---|
| 126 | }; |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /** stop display of the item altogether **/ |
|---|
| 130 | private function setStop() { |
|---|
| 131 | delete currentURL; |
|---|
| 132 | clearInterval(positionInterval); |
|---|
| 133 | currentPosition = 0; |
|---|
| 134 | isSWF == true ? imageClip.mc.gotoAndStop(1): null; |
|---|
| 135 | if (imageClip.bg == undefined) { |
|---|
| 136 | imageClip.mc.removeMovieClip(); |
|---|
| 137 | imageClip.smc.removeMovieClip(); |
|---|
| 138 | imageClip._visible = false; |
|---|
| 139 | } |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | /** set duration to rotatetime for images **/ |
|---|
| 144 | private function setItem(idx:Number) { |
|---|
| 145 | super.setItem(idx); |
|---|
| 146 | if(feeder.feed[currentItem]["duration"] == 0 && isActive == true) { |
|---|
| 147 | feeder.feed[currentItem]["duration"] = config['rotatetime']; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | } |
|---|