| 1 | package { |
|---|
| 2 | import flash.display.Sprite; |
|---|
| 3 | import hatesuta.*; |
|---|
| 4 | import flash.display.StageAlign; |
|---|
| 5 | import flash.display.StageScaleMode; |
|---|
| 6 | import com.rails2u.utils.ColorUtil; |
|---|
| 7 | import com.rails2u.utils.MathUtil; |
|---|
| 8 | import flash.events.TimerEvent; |
|---|
| 9 | import flash.utils.Timer; |
|---|
| 10 | import com.rails2u.swflayer.SWFLayer; |
|---|
| 11 | import com.rails2u.net.JSONPLoader; |
|---|
| 12 | import caurina.transitions.Tweener; |
|---|
| 13 | import flash.display.MovieClip; |
|---|
| 14 | import flash.net.URLRequest; |
|---|
| 15 | import flash.display.Loader; |
|---|
| 16 | import flash.events.Event; |
|---|
| 17 | import flash.display.DisplayObject; |
|---|
| 18 | import sketchbook.colors.ColorSB; |
|---|
| 19 | import flash.display.StageQuality; |
|---|
| 20 | import flash.net.URLLoader; |
|---|
| 21 | import flash.events.MouseEvent; |
|---|
| 22 | import com.rails2u.net.navigateToURL; |
|---|
| 23 | import flash.text.TextField; |
|---|
| 24 | import flash.external.ExternalInterface; |
|---|
| 25 | import flash.system.Security; |
|---|
| 26 | import flash.events.IOErrorEvent; |
|---|
| 27 | import flash.errors.IOError; |
|---|
| 28 | |
|---|
| 29 | [SWF(frameRate=60, background=0xFFFFFF)] |
|---|
| 30 | public class Hatesuta extends Sprite { |
|---|
| 31 | public var swf:SWFLayer; |
|---|
| 32 | public var fallingTimer:Timer; |
|---|
| 33 | public var fallingContainer:Sprite; |
|---|
| 34 | public var bs:BigStar; |
|---|
| 35 | |
|---|
| 36 | public function Hatesuta() { |
|---|
| 37 | stage.align = StageAlign.TOP_LEFT; |
|---|
| 38 | stage.scaleMode = StageScaleMode.NO_SCALE; |
|---|
| 39 | |
|---|
| 40 | Security.allowDomain('*'); |
|---|
| 41 | run(); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public function run():void { |
|---|
| 45 | swf = SWFLayer.getInstance(); |
|---|
| 46 | // swf.fitInBrowser(); |
|---|
| 47 | |
|---|
| 48 | bs = new BigStar(); |
|---|
| 49 | jsonpLoad(); |
|---|
| 50 | runFallingStars(); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | private function jsonpLoad():void { |
|---|
| 54 | var url:String = SWFLayer.execExternalInterface('return window.location.href;'); |
|---|
| 55 | var hatenaRegex:RegExp = new RegExp('^(https?://[^/]*?((\.g)|b|d)\.hatena\.ne\.jp/[^/]*)(.*)'); |
|---|
| 56 | if (url.match(hatenaRegex)) { |
|---|
| 57 | url = url.replace(hatenaRegex, "$1/").replace(/\/\/$/, '/'); |
|---|
| 58 | } |
|---|
| 59 | var loader:JSONPLoader = new JSONPLoader('http://s.hatena.ne.jp/blog.json/' + url); |
|---|
| 60 | loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:Event):void { |
|---|
| 61 | // log(e.target.url); |
|---|
| 62 | }); |
|---|
| 63 | loader.addEventListener(Event.COMPLETE, jsonpCompleteHandler); |
|---|
| 64 | loader.load(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | private function runFallingStars():void { |
|---|
| 68 | fallingContainer = new Sprite(); |
|---|
| 69 | fallingContainer.graphics.beginFill(0xDDDDDD, 0); |
|---|
| 70 | fallingContainer.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight); |
|---|
| 71 | fallingContainer.graphics.endFill(); |
|---|
| 72 | addChild(fallingContainer); |
|---|
| 73 | |
|---|
| 74 | var fs:FirstFallingStars = new FirstFallingStars(stage.stageWidth * 2/5,stage.stageHeight + 50, 5, 0xFFAC66, 15); |
|---|
| 75 | fs.createStars(); |
|---|
| 76 | fallingContainer.addChild(fs); |
|---|
| 77 | |
|---|
| 78 | fallingTimer = new Timer(100); |
|---|
| 79 | fallingTimer.addEventListener(TimerEvent.TIMER, fallingTimerHandler); |
|---|
| 80 | fallingTimer.start(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | private function fallingTimerHandler(e:TimerEvent):void { |
|---|
| 84 | var c:int = Math.min(e.target.currentCount, 20); |
|---|
| 85 | var fs:FallingStars; |
|---|
| 86 | var csb:ColorSB = new ColorSB(ColorUtil.random(128,128,128)); |
|---|
| 87 | |
|---|
| 88 | for (var i:uint = 0; i < 20 - Math.min(c, 10); i++) { |
|---|
| 89 | if (e.target.currentCount == 1) { |
|---|
| 90 | fs = new FirstFallingStars(stage.stageWidth * (1.5/5 + Math.random() / 5),stage.stageHeight + 50, |
|---|
| 91 | MathUtil.randomBetween(3 * c,5 * c), |
|---|
| 92 | ColorUtil.random(255,128,66), |
|---|
| 93 | 10); |
|---|
| 94 | fs.delay = true; |
|---|
| 95 | } else { |
|---|
| 96 | csb.hue += 5; |
|---|
| 97 | csb.saturation += 5; |
|---|
| 98 | csb.brightness += 5; |
|---|
| 99 | fs = new FallingStars(MathUtil.randomBetween(-stage.stageWidth,stage.stageWidth), |
|---|
| 100 | MathUtil.randomBetween(stage.stageHeight + 100 ,stage.stageHeight + 500), |
|---|
| 101 | MathUtil.randomBetween(3 * c,5 * c), |
|---|
| 102 | csb.value, |
|---|
| 103 | Math.max(8 - c, 3)); |
|---|
| 104 | } |
|---|
| 105 | fs.createStars(); |
|---|
| 106 | fallingContainer.addChild(fs); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | if (e.target.currentCount == 25) { |
|---|
| 110 | runBigStar(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (e.target.currentCount == 30) { |
|---|
| 114 | var origQuality:String = stage.quality; |
|---|
| 115 | stage.quality = StageQuality.LOW; |
|---|
| 116 | Tweener.addTween(fallingContainer, { |
|---|
| 117 | alpha:0, |
|---|
| 118 | transition: 'linear', |
|---|
| 119 | onCompleteParams: [origQuality], |
|---|
| 120 | onComplete: stopFalling, |
|---|
| 121 | time:0.7 |
|---|
| 122 | }); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | private function runBigStar():void { |
|---|
| 127 | addChild(bs); |
|---|
| 128 | bs.addEventListener(Event.COMPLETE, function(e:Event):void { |
|---|
| 129 | runFont(); |
|---|
| 130 | }); |
|---|
| 131 | bs.move(); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | private function runFont():void { |
|---|
| 135 | for each(var fName:String in ['ha', 'te', 'su', 'ta']) { |
|---|
| 136 | var font:Font = new Font(fName); |
|---|
| 137 | if (fName == 'ha') |
|---|
| 138 | font.addEventListener(Event.COMPLETE, fontCompleteHandler); |
|---|
| 139 | addChild(font); |
|---|
| 140 | font.move(); |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | private var runFinish:Boolean = false; |
|---|
| 145 | private function fontCompleteHandler(e:Event):void { |
|---|
| 146 | runFinish = true; |
|---|
| 147 | stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); |
|---|
| 148 | |
|---|
| 149 | if (jsonpData) starCountView(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | private function mouseDownHandler(e:MouseEvent):void { |
|---|
| 153 | swf.style.display = 'none'; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | private function stopFalling(origQuality:String):void { |
|---|
| 157 | stage.quality = origQuality; |
|---|
| 158 | fallingTimer.stop(); |
|---|
| 159 | removeChild(fallingContainer); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | private var jsonpData:Object; |
|---|
| 163 | private function jsonpCompleteHandler(e:Event):void { |
|---|
| 164 | this.jsonpData = e.target.data; |
|---|
| 165 | if (runFinish) starCountView(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | private function starCountView():void { |
|---|
| 169 | var cv:Sprite = new Sprite; |
|---|
| 170 | |
|---|
| 171 | var count:String = String(jsonpData['star_count']); |
|---|
| 172 | var sWidth:uint = count.length * 14 + 73; |
|---|
| 173 | cv.graphics.beginFill(0x555555, 0.8); |
|---|
| 174 | cv.graphics.drawRoundRectComplex(0,0, sWidth, 50, 10,10,10,10); |
|---|
| 175 | cv.graphics.endFill(); |
|---|
| 176 | addChild(cv); |
|---|
| 177 | cv.y = stage.stageHeight - 60; |
|---|
| 178 | cv.x = stage.stageWidth + sWidth + 10; |
|---|
| 179 | var st:MovieClip = new (BigStar.starSWF); |
|---|
| 180 | st.scaleX = 0.5; |
|---|
| 181 | st.scaleY = 0.5; |
|---|
| 182 | cv.addChild(st); |
|---|
| 183 | st.x = 8; |
|---|
| 184 | st.y = 7; |
|---|
| 185 | var t:TextField = new TextField; |
|---|
| 186 | t.width = 800; |
|---|
| 187 | t.htmlText = '<font size="30" face="arial" color="#FED924">' + count + '</font>'; |
|---|
| 188 | t.x = 43; |
|---|
| 189 | t.y = 7; |
|---|
| 190 | cv.addChild(t); |
|---|
| 191 | cv.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void { |
|---|
| 192 | e.stopPropagation(); |
|---|
| 193 | com.rails2u.net.navigateToURL(new URLRequest('http://s.hatena.ne.jp/'), '_blank'); |
|---|
| 194 | }); |
|---|
| 195 | Tweener.addTween(cv, { |
|---|
| 196 | x: stage.stageWidth - sWidth - 10, |
|---|
| 197 | time:1 |
|---|
| 198 | }); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | } |
|---|