Clock (1)
////////////////////////////////////////////////////////////////////////////////
// Clock (1)
//
// [AS3.0] 時計っぽいの (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1457
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/buHy
*/
////////////////////////////////////////////////////////////////////////////////
// Clock (1)
//
// [AS3.0] 時計っぽいの (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1457
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var clock:Clock;
public function Main() {
//Wonderfl.capture_delay(1);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
init();
}
private function init():void {
var icons:Array = new Array();
icons.push({icon: Icon, id: 0});
icons.push({icon: Icon, id: 1});
icons.push({icon: Icon, id: 2});
icons.push({icon: Icon, id: 3});
icons.push({icon: Icon, id: 4});
icons.push({icon: Icon, id: 5});
icons.push({icon: Icon, id: 6});
icons.push({icon: Icon, id: 7});
icons.push({icon: Icon, id: 8});
icons.push({icon: Icon, id: 9});
clock = new Clock(icons);
addChild(clock);
clock.x = 232;
clock.y = 232;
var date:Date = new Date();
clock.start(date.time, date.timezoneOffset);
}
}
}
//////////////////////////////////////////////////
// Clockクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.filters.DropShadowFilter;
class Clock extends Sprite {
private var icons:Array;
private var container:Sprite;
private var hour:ClockItem;
private var minute:ClockItem;
private var second:ClockItem;
private var time:Time;
private static var baseColor:uint = 0x000000;
private var overlay:Shape;
private static var offset:Number = 0.4;
public function Clock(list:Array) {
icons = list;
init();
}
private function init():void {
container = new Sprite();
addChild(container);
var label:Label = new Label(100, 60, 54, Label.CENTER);
container.addChild(label);
label.x = -50;
label.y = -38;
label.text = ": :";
hour = new ClockItem(24, icons, offset);
container.addChild(hour);
hour.x = -100;
minute = new ClockItem(60, icons, offset);
container.addChild(minute);
minute.x = 0;
second = new ClockItem(60, icons, offset);
container.addChild(second);
second.x = 100;
draw();
//
time = new Time();
time.addEventListener(TimeEvent.INIT, initialize, false, 0, true);
time.addEventListener(TimeEvent.UPDATE, update, false, 0, true);
}
public function start(t:Number, o:Number = 0):void {
time.initialize(t, o);
}
private function initialize(evt:TimeEvent):void {
hour.initialize(evt.value.hour);
minute.initialize(evt.value.minute);
second.initialize(evt.value.second);
time.start(offset);
}
private function update(evt:TimeEvent):void {
hour.update(evt.value.hour);
minute.update(evt.value.minute);
second.update(evt.value.second);
}
private function draw():void {
var back:Shape = new Shape();
addChild(back);
var w:uint = 300;
var h:uint = 60;
back.graphics.beginFill(baseColor);
back.graphics.drawRect(-w/2, -h/2, w, h);
back.graphics.endFill();
container.mask = back;
//
overlay = new Shape();
addChild(overlay);
overlay.graphics.beginFill(baseColor);
overlay.graphics.drawRect(-w/2, -h/2, w, h);
overlay.graphics.endFill();
overlay.filters = [new DropShadowFilter(1, 90, 0x000000, 0.4, 8, 8, 2, 3, true, true, false)];
}
}
//////////////////////////////////////////////////
// ClockItemクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.events.TweenEvent;
import org.libspark.betweenas3.easing.*;
class ClockItem extends Sprite {
private var time:Array;
private var total:uint;
private static var _width:uint = 40;
private var icons:Array;
private var items:Array;
private var contents:Array;
private var hold:Array;
private var offset:Number = 0.5;
public function ClockItem(t:uint, list:Array, o:Number = 0.5) {
total = t;
icons = list;
offset = o;
init();
}
private function init():void {
items = new Array();
var Icon:Class;
items[0] = new Array();
for (var n:uint = 0; n < uint(total/10) + 1; n++) {
Icon = icons[n].icon;
items[0].push(new Icon(icons[n].id));
}
items[1] = new Array();
for (var t:uint = 0; t < 10; t++) {
Icon = icons[t].icon;
items[1].push(new Icon(icons[t].id));
}
time = new Array();
}
public function initialize(t:uint):void {
setup(t);
for (var n:uint = 0; n < 2; n++) {
addItem(n);
}
}
public function update(t:uint):void {
hold = time.concat();
setup(t);
for (var n:uint = 0; n < 2; n++) {
if (time[n] != hold[n]) {
slideItem(n);
}
}
}
private function addItem(id:uint):void {
var item:Sprite = items[id][time[id]];
addChild(item);
item.x = -_width/2 + _width*id;
item.y = 0;
}
private function slideItem(id:uint):void {
var prev:Sprite = items[id][hold[id]];
var next:Sprite = items[id][time[id]];
addChild(next);
next.x = -_width/2 + _width*id;
next.y = -50;
var itween:ITween = BetweenAS3.parallel(
BetweenAS3.to(prev, {y: 50}, offset, Quad.easeOut),
BetweenAS3.to(next, {y: 0}, offset, Quad.easeOut)
);
itween.addEventListener(TweenEvent.COMPLETE, complete, false, 0, true);
itween.play();
}
private function complete(evt:TweenEvent):void {
evt.target.removeEventListener(TweenEvent.COMPLETE, complete);
}
private function removeItem(id:uint):void {
var item:Sprite = items[id][hold[id]];
if (contains(item)) removeChild(item);
}
private function setup(t:uint):void {
if (t < 10) {
time[0] = 0;
time[1] = t;
} else {
time[0] = uint(t/10);
time[1] = t%10;
}
}
}
//////////////////////////////////////////////////
// Timeクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.utils.getTimer;
class Time extends EventDispatcher {
private var time:Number = 0;
private var _time:Number = 0;
private var timezone:int = 0;
private var offset:Number = 0.5;
private var value:Object = {hour: 0, minute: 0, second: 0, milisecond: 0};
private static var interval:uint = 25;
private var updated:Boolean = false;
public function Time() {
}
public function initialize(t:Number, tz:Number = 0):void {
time = t;
timezone = tz;
_time = time;
setup();
init();
}
private function init():void {
dispatchEvent(new TimeEvent(TimeEvent.INIT, value));
}
public function start(o:Number):void {
offset = o;
var timer:Timer = new Timer(interval);
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true);
timer.start();
}
private function tick(evt:TimerEvent):void {
var count:Number = time + getTimer();
if (!updated && count - _time > 1000 - offset*1000) {
_time = uint(count/1000)*1000;
update();
updated = true;
}
if (count - _time > 1000) {
updated = false;
}
}
private function update():void {
setup();
dispatchEvent(new TimeEvent(TimeEvent.UPDATE, value));
}
private function setup():void {
var count:Number = _time/1000;
var second:uint = (count*2 | 0) - (count | 0);
var minute:uint = int(second/60) - timezone;
var hour:uint = int(minute/60);
value.second = second%60;
value.minute = minute%60;
value.hour = hour%24;
}
}
//////////////////////////////////////////////////
// TimeEventクラス
//////////////////////////////////////////////////
import flash.events.Event;
class TimeEvent extends Event {
public static const INIT:String = "init";
public static const UPDATE:String = "update";
public var value:*;
public function TimeEvent(type:String, value:* = null) {
super(type);
this.value = value;
}
override public function clone():Event {
return new TimeEvent(type, value);
}
}
//////////////////////////////////////////////////
// Iconクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Icon extends Sprite {
private var id:uint;
private var txt:TextField;
private static var fontType:String = "Arial";
public function Icon(n:uint) {
id = n;
init();
}
private function init():void {
txt = new TextField();
addChild(txt);
txt.x = -30;
txt.y = -30;
txt.width = 60;
txt.height = 60;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 54;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
txt.text = String(id);
}
}
//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private static var fontType:String = "_ゴシック";
private var _width:uint = 20;
private var _height:uint = 20;
private var size:uint = 12;
public static const LEFT:String = TextFormatAlign.LEFT;
public static const CENTER:String = TextFormatAlign.CENTER;
public static const RIGHT:String = TextFormatAlign.RIGHT;
public function Label(w:uint, h:uint, s:uint = 12, align:String = LEFT) {
_width = w;
_height = h;
size = s;
draw(align);
}
private function draw(align:String):void {
txt = new TextField();
addChild(txt);
txt.width = _width;
txt.height = _height;
txt.autoSize = align;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = size;
tf.align = align;
txt.defaultTextFormat = tf;
textColor = 0x000000;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}