| 1 | /** |
|---|
| 2 | * Abstract model class of the players MCV pattern, extended by all models. |
|---|
| 3 | * |
|---|
| 4 | * @author Jeroen Wijering |
|---|
| 5 | * @version 1.4 |
|---|
| 6 | **/ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import com.jeroenwijering.players.*; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class com.jeroenwijering.players.AbstractModel { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | /** a list of all registered views **/ |
|---|
| 16 | private var registeredViews:Array; |
|---|
| 17 | /** a reference to the controller **/ |
|---|
| 18 | private var controller:AbstractController; |
|---|
| 19 | /** reference to the config array **/ |
|---|
| 20 | private var config:Object; |
|---|
| 21 | /** reference to the feed array **/ |
|---|
| 22 | private var feeder:Object; |
|---|
| 23 | /** item that's currently playing **/ |
|---|
| 24 | private var currentItem:Number; |
|---|
| 25 | /** url of the item that's currently used by this model **/ |
|---|
| 26 | private var currentURL:String; |
|---|
| 27 | /** array with extensions used by a model **/ |
|---|
| 28 | private var mediatypes:Array; |
|---|
| 29 | /** boolean to check if a model is currently active **/ |
|---|
| 30 | private var isActive:Boolean; |
|---|
| 31 | /** current playhead position **/ |
|---|
| 32 | private var currentPosition:Number; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /** Constructor. **/ |
|---|
| 36 | function AbstractModel(vws:Array,ctr:AbstractController, |
|---|
| 37 | cfg:Object,fed:Object) { |
|---|
| 38 | registeredViews = vws; |
|---|
| 39 | controller = ctr; |
|---|
| 40 | config = cfg; |
|---|
| 41 | feeder = fed; |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** Receive changes from the PlayerController. **/ |
|---|
| 46 | public function getChange(typ:String,prm:Number):Void { |
|---|
| 47 | trace("model: "+typ+": "+prm); |
|---|
| 48 | switch(typ) { |
|---|
| 49 | case "item": |
|---|
| 50 | setItem(prm); |
|---|
| 51 | break; |
|---|
| 52 | case "start": |
|---|
| 53 | if(isActive == true) { setStart(prm); } |
|---|
| 54 | break; |
|---|
| 55 | case "pause": |
|---|
| 56 | if(isActive == true) { setPause(prm); } |
|---|
| 57 | break; |
|---|
| 58 | case "stop": |
|---|
| 59 | if(isActive == true) { setStop(); } |
|---|
| 60 | break; |
|---|
| 61 | case "volume": |
|---|
| 62 | setVolume(prm); |
|---|
| 63 | break; |
|---|
| 64 | default: |
|---|
| 65 | trace("Model: incompatible change received"); |
|---|
| 66 | break; |
|---|
| 67 | } |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | /** Set new item and check if the model should be the active one. **/ |
|---|
| 72 | private function setItem(idx:Number) { |
|---|
| 73 | currentItem = idx; |
|---|
| 74 | var fnd:Boolean = false; |
|---|
| 75 | for (var i=0; i<mediatypes.length; i++) { |
|---|
| 76 | if(feeder.feed[idx]["type"] == mediatypes[i]) { |
|---|
| 77 | fnd = true; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | if(feeder.feed[idx]["start"] > 0) { |
|---|
| 81 | currentPosition = feeder.feed[idx]["start"]; |
|---|
| 82 | } |
|---|
| 83 | if(fnd == true) { |
|---|
| 84 | isActive = true; |
|---|
| 85 | sendUpdate("item",idx); |
|---|
| 86 | } else { |
|---|
| 87 | isActive = false; |
|---|
| 88 | } |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** Start function. **/ |
|---|
| 93 | private function setStart(prm:Number) {}; |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | /** Pause function. **/ |
|---|
| 97 | private function setPause(prm:Number) {}; |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | /** Stop function. **/ |
|---|
| 101 | private function setStop() {}; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | /** Set volume and pass through if active. **/ |
|---|
| 105 | private function setVolume(vol:Number) { |
|---|
| 106 | if(isActive == true) { sendUpdate("volume",vol); } |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** Send updates to the views. **/ |
|---|
| 111 | private function sendUpdate(typ:String,prm:Number,pr2:Number) { |
|---|
| 112 | for(var i=0; i<registeredViews.length; i++) { |
|---|
| 113 | registeredViews[i].getUpdate(typ,prm,pr2); |
|---|
| 114 | } |
|---|
| 115 | if(typ == 'size') { |
|---|
| 116 | controller.getEvent(typ,prm,pr2); |
|---|
| 117 | } |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | /** Send a "complete" event directly to the controller. **/ |
|---|
| 122 | private function sendCompleteEvent() { |
|---|
| 123 | controller.getEvent("complete"); |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | } |
|---|