| 1 | package net.suztomo.FPazzle |
|---|
| 2 | { |
|---|
| 3 | import com.adobe.webapis.flickr.*; |
|---|
| 4 | |
|---|
| 5 | import flash.display.*; |
|---|
| 6 | import flash.events.Event; |
|---|
| 7 | import flash.events.EventDispatcher; |
|---|
| 8 | |
|---|
| 9 | import mx.containers.Panel; |
|---|
| 10 | import mx.controls.*; |
|---|
| 11 | import mx.core.UIComponent; |
|---|
| 12 | |
|---|
| 13 | import net.suztomo.FPazzle.event.*; |
|---|
| 14 | |
|---|
| 15 | public class FBoard extends UIComponent |
|---|
| 16 | { |
|---|
| 17 | public static var pazzleImage:BitmapData; |
|---|
| 18 | // [Embed(source='flower.jpg')] private var EmbedImage:Class; |
|---|
| 19 | |
|---|
| 20 | private var x_num:uint, y_num:uint; |
|---|
| 21 | |
|---|
| 22 | private static var answerDiff:Number = 2; |
|---|
| 23 | private var eventDispatcher:EventDispatcher; |
|---|
| 24 | private var numAnsweredPieces:int = 0;; |
|---|
| 25 | |
|---|
| 26 | public var p_width:Number, p_height:Number; |
|---|
| 27 | public var photo:Photo; |
|---|
| 28 | public var pieces:PazzleMap; |
|---|
| 29 | public var keyword:String; |
|---|
| 30 | public var infoPanel:Panel; |
|---|
| 31 | public var groups:Array; |
|---|
| 32 | |
|---|
| 33 | public static var BOARD_READY:String = "boardReady"; |
|---|
| 34 | public static var BOARD_COMPLETE:String = "boardComplete"; |
|---|
| 35 | public static var PIECE_SNAPPED:String = "pieceSnapped"; |
|---|
| 36 | |
|---|
| 37 | public function FBoard(_x_num:uint, _y_num:uint, _pazzleImage:BitmapData = null) |
|---|
| 38 | { |
|---|
| 39 | x_num = _x_num; |
|---|
| 40 | y_num = _y_num; |
|---|
| 41 | pazzleImage = _pazzleImage; |
|---|
| 42 | |
|---|
| 43 | eventDispatcher = new EventDispatcher(); |
|---|
| 44 | |
|---|
| 45 | groups = new Array(); |
|---|
| 46 | |
|---|
| 47 | p_width = Math.floor(pazzleImage.width / x_num); |
|---|
| 48 | p_height = Math.floor(pazzleImage.height / y_num); |
|---|
| 49 | |
|---|
| 50 | pieces = new PazzleMap(x_num, y_num); |
|---|
| 51 | super(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public function setPazzleImage(p:Photo) :void |
|---|
| 55 | { |
|---|
| 56 | photo = p; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public function putPiece(_x:Number = 0, _y:Number = 0, _x_index:uint = 0, _y_index:uint = 0):void |
|---|
| 60 | { |
|---|
| 61 | var piece:FPiece = new FPiece(pazzleImage, p_width, p_height, _x_index, _y_index); |
|---|
| 62 | if (pazzleImage == null) { |
|---|
| 63 | trace("pazzle Image is not setted"); |
|---|
| 64 | } else { |
|---|
| 65 | putPieceGroup(piece,_x, _y); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | pieces.setElement(_x_index, _y_index, piece); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | public function putPieceGroup(p:FPiece, _x:Number = 0, _y:Number = 0):void |
|---|
| 72 | { |
|---|
| 73 | var pg:FPieceGroup = new FPieceGroup(p); |
|---|
| 74 | pg.x = p.parent ? p.parent.x : _x; |
|---|
| 75 | pg.y = p.parent ? p.parent.y : _y; |
|---|
| 76 | addChild(pg); |
|---|
| 77 | groups.push(pg); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public function createPieces():void |
|---|
| 81 | { |
|---|
| 82 | var min_x:Number = p_width + 160, min_y:Number = 100; |
|---|
| 83 | var range_x:Number = 600; |
|---|
| 84 | var range_y:Number = 400; |
|---|
| 85 | for (var i:uint=0; i < x_num; i++) { |
|---|
| 86 | for (var j:uint=0; j < y_num; j++) { |
|---|
| 87 | putPiece(min_x + range_x*Math.random(), min_y + range_y*Math.random(), j, i); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public function checkAnswer():void |
|---|
| 93 | { |
|---|
| 94 | if (isAnswer()) { |
|---|
| 95 | deactivate(); |
|---|
| 96 | dispatchEvent(new FPazzleEvent(FBoard.BOARD_COMPLETE)); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public function isAnswer():Boolean |
|---|
| 101 | { |
|---|
| 102 | var tmp1:FPiece, tmp2:FPiece; |
|---|
| 103 | var g:FPieceGroup = pieces.getElement(0, 0).group; |
|---|
| 104 | for (var i:uint = 0; i<y_num; i++) { |
|---|
| 105 | for (var j:uint = 0; j<x_num; j++) { |
|---|
| 106 | tmp1 = pieces.getElement(j, i) as FPiece; |
|---|
| 107 | if (tmp1.rotation != 0) return false; |
|---|
| 108 | if (tmp1.group != g) return false; |
|---|
| 109 | |
|---|
| 110 | if (j != x_num-1) { |
|---|
| 111 | tmp2 = pieces.getElement(j+1, i) as FPiece; |
|---|
| 112 | if (Math.abs(tmp1.x + p_width - tmp2.x) > answerDiff |
|---|
| 113 | || Math.abs(tmp1.y - tmp2.y) > answerDiff){ |
|---|
| 114 | return false; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if (i != y_num-1) { |
|---|
| 119 | tmp2 = pieces.getElement(j, i+1) as FPiece; |
|---|
| 120 | if (Math.abs(tmp1.x - tmp2.x) > answerDiff |
|---|
| 121 | || Math.abs(tmp1.y + p_height - tmp2.y) > answerDiff){ |
|---|
| 122 | return false; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | return true; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | public function showPhotoInfo():void |
|---|
| 131 | { |
|---|
| 132 | var str:String = ""; |
|---|
| 133 | var p:Photo = photo; |
|---|
| 134 | if (p != null) { |
|---|
| 135 | if (p.title) |
|---|
| 136 | str += "Title: " + p.title + "\n"; |
|---|
| 137 | if (p.description) |
|---|
| 138 | str += p.description + "\n"; |
|---|
| 139 | if (p.tags && p.tags.length > 0) |
|---|
| 140 | str += "Tags: " + p.tags.toString() + "\n"; |
|---|
| 141 | if (p.dateTaken) |
|---|
| 142 | str += "Taken Date: " + p.dateTaken.fullYear + "/" + (p.dateTaken.month+1) + "/" + p.dateTaken.day + "\n\n"; |
|---|
| 143 | /* if (p.url) |
|---|
| 144 | str += "URL: " + p.url + "\n"; |
|---|
| 145 | if (p.id) |
|---|
| 146 | str += "ID: " + p.id + "\n";*/ |
|---|
| 147 | |
|---|
| 148 | if (p.ownerName) |
|---|
| 149 | str += "Owner: " + p.ownerName + ((p.ownerRealName != null) ? ("(" + p.ownerRealName + ")") : "") + "\n"; |
|---|
| 150 | if (p.ownerLocation) |
|---|
| 151 | str += "Location: " + p.ownerLocation + "\n"; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | Alert.show(str, "Completed"); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | public function deactivate():void |
|---|
| 158 | { |
|---|
| 159 | for (var i:uint=0; i<pieces.length; i++) { |
|---|
| 160 | pieces.getInline(i).deactivate(); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | for (i=0; i<groups.length; i++) { |
|---|
| 164 | (groups[i] as FPieceGroup).deactivate(); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | public function traceGroupId():void |
|---|
| 169 | { |
|---|
| 170 | trace("------traceGroupId()-----"); |
|---|
| 171 | for each(var g:FPieceGroup in groups){ |
|---|
| 172 | g.traceId(); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public function showAnswer():void |
|---|
| 177 | { |
|---|
| 178 | var g:FPieceGroup = groups[0]; |
|---|
| 179 | g.x = p_width*x_num * 0.4 + 300; |
|---|
| 180 | g.y = 150; |
|---|
| 181 | |
|---|
| 182 | for (var j:uint=0; j<groups.length;j++) { |
|---|
| 183 | (groups[j] as FPieceGroup).deactivate(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | for (var i:uint=0; i<pieces.length; i++) { |
|---|
| 187 | g.putNewPiece(pieces.getInline(i) as FPiece); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | slideNextPiece(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | public function slideNextPiece():void |
|---|
| 194 | { |
|---|
| 195 | if (numAnsweredPieces >= pieces.length) { |
|---|
| 196 | checkAnswer(); |
|---|
| 197 | return; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | var p:FPiece = pieces.getInline(numAnsweredPieces) as FPiece; |
|---|
| 201 | p.slideTo((numAnsweredPieces % x_num) * p_width, Math.floor(numAnsweredPieces / x_num) * p_height); |
|---|
| 202 | numAnsweredPieces += 1; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | // Event Listeners |
|---|
| 206 | public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void |
|---|
| 207 | { |
|---|
| 208 | eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | public override function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void |
|---|
| 212 | { |
|---|
| 213 | eventDispatcher.removeEventListener(type, listener, useCapture); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | public override function hasEventListener(type:String):Boolean |
|---|
| 217 | { |
|---|
| 218 | return eventDispatcher.hasEventListener(type); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | public override function willTrigger(type:String):Boolean |
|---|
| 222 | { |
|---|
| 223 | return eventDispatcher.willTrigger(type); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | public override function dispatchEvent(event:Event):Boolean |
|---|
| 227 | { |
|---|
| 228 | return eventDispatcher.dispatchEvent(event); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|