Tenori-CAL
Morioka.as->lab(0x06)で発表したものを
wonderflに移植しました。
元ネタ: http://morioka-as.org/lab/gon_0x06/
これはなに?
- ドラムマシンっぽいカレンダーです。
- 音がでます。
- MP3を差し替えると好きな音であそべます。
最適化とかあんまししてない
テキトーにいじってみてください。
/**
* Copyright gonoverdrive ( http://wonderfl.net/user/gonoverdrive )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wBhR
*/
/***********************************
Morioka.as->lab(0x06)で発表したものを
wonderflに移植しました。
元ネタ: http://morioka-as.org/lab/gon_0x06/
これはなに?
- ドラムマシンっぽいカレンダーです。
- 音がでます。
- MP3を差し替えると好きな音であそべます。
最適化とかあんまししてない
テキトーにいじってみてください。
************************************/
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.*;
import flash.net.*;
import flash.system.Security;
import flash.geom.Point;
/* import Snd;
import Transport;
import Pad;
*/
[SWF(width = "640", height = "480", backgroundColor = "0x000000", frameRate = "60")]
public class Main extends MovieClip {
private var now:Date = new Date();
private const DEF_BPM:uint = 180; //←テンポ
private const MP3URL:String = 'http://morioka-as.org/lab/gon_0x06/sound/kit.mp3';
private const LAST_DATE:uint = new Date(now.fullYear, now.month+1, 0).date;
private const START_DAY:uint = new Date(now.fullYear, now.month, 1).day;
private const BARS:uint = 7;
private const STAGE:Point = new Point(640, 480);
private var bg:Shape;
private var panel:Sprite;
private var a_panel:Array;
private var bpm:uint;
private var ticks:uint;
private var timer:Timer;
private var isRun:Boolean = false;
private var bar:uint = 0;
private var prev_bar:uint = BARS - 1;
private var a_sound:Array;
private var transport:Transport;
//Constructor
public function Main() {
Security.loadPolicyFile('http://morioka-as.org/lab/gon_0x06/crossdomain.xml');
changeBPM();
setupBG();
generateCalender();
setupTransport();
setupSound();
}
//
private function setupBG():void {
bg = new Shape();
bg.graphics.beginFill(0xEDEDED);
bg.graphics.drawRect(0, 0, STAGE.x, STAGE.y);
bg.graphics.endFill();
addChild(bg);
}
//テンポ変更
private function changeBPM(_n:uint = DEF_BPM):void {
bpm = _n;
ticks = Math.ceil(30 / bpm * 1000);
timer = new Timer(ticks);
}
//操作パネル?生成
private function generateCalender():void {
panel = new Sprite();
var m:uint = LAST_DATE + START_DAY + BARS - (LAST_DATE + START_DAY) % BARS;
a_panel = new Array();
var j:uint = 0;
for(var i:uint = 0; i<m; i++) {
a_panel[i] = new Pad();
if(i >= START_DAY && j < LAST_DATE) {
a_panel[i].text = j + 1;
j++;
if(j === now.getDate()) {
a_panel[i].select();
} else {
a_panel[i].buttonMode = true;
a_panel[i].addEventListener(MouseEvent.CLICK, onClickHandler);
a_panel[i].addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
a_panel[i].addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
}
}
a_panel[i].x = Math.floor(i%7) * (a_panel[i].width + 2);
a_panel[i].y = Math.floor(i/7) * (a_panel[i].height + 2);
panel.addChild(a_panel[i]);
}
addChild(panel);
panel.x = stage.width / 2 - panel.width / 2;
panel.y = 50;
}
//操作ボタン
private function setupTransport():void {
transport = new Transport();
transport.name = "transport";
transport.buttonMode = true;
transport.x = stage.width / 2 - transport.width / 2;
transport.y = panel.height + panel.y + 20;
addChild(transport);
transport.addEventListener(MouseEvent.CLICK, onClickHandler);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
}
//MP3
private function setupSound():void {
a_sound = new Array(a_panel.length / BARS);
for(var i:uint = 0; i < a_sound.length; i++) {
a_sound[i] = new Snd(i, ticks, MP3URL);
}
//trace(a_sound.length);
}
/*--------------
イベントハンドラ
----------------*/
private function onClickHandler(e:MouseEvent):void {
if(e.currentTarget.name === 'transport') {
isRun = !isRun;
if(isRun) {
transport.text = transport.PAUSE;
timer.start();
} else {
transport.text = transport.PLAY;
timer.stop();
}
} else {
e.currentTarget.isSelect = !e.currentTarget.isSelect;
if(e.currentTarget.isSelect) {
e.currentTarget.setColor('green');
} else {
e.currentTarget.setColor();
}
}
}
private function onRollOverHandler(e:MouseEvent):void {
e.currentTarget.focus();
}
private function onRollOutHandler(e:MouseEvent):void {
e.currentTarget.unfocus();
}
//アニメーション
private function timerHandler(e:Event):void {
for(var i:uint = 0; i < uint(a_panel.length / BARS); i++) {
var j:uint = i * BARS + bar;
//trace(j);
a_panel[j].active();
if(a_panel[j].isSelect) {
a_sound[i].start();
}
var k:uint = i * BARS +prev_bar;
a_panel[k].negative();
}
prev_bar = bar;
if(bar < BARS - 1) {
bar++;
} else {
bar = 0;
}
}
}
}
import flash.display.*;
import flash.media.*;
import flash.net.*;
import flash.text.*;
import flash.events.*;
import flash.utils.Timer;
import flash.geom.ColorTransform;
import flash.geom.Point;
//MP3
class Snd extends Sound {
private var snd:Sound;
private var sch:SoundChannel;
private var startPos:uint;
private var dur:Timer;
private var uri:URLRequest;
public function Snd(_s:uint, _t:uint, _u:String) {
snd = new Sound();
sch = new SoundChannel();
uri = new URLRequest(_u);
startPos = _s * 500;
snd.load(uri);
dur = new Timer(_t - 30, 1);
dur.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerHandler);
}
public function start():void {
sch = snd.play(startPos, 0);
dur.start();
trace("Play:" + startPos);
}
private function onTimerHandler(e:Event):void {
sch.stop();
}
}
//親クラス
class ButtonTPL extends Sprite {
public var isSelect:Boolean;
private var area:Point = new Point();
private var t:String;
private var bg:ButtonRect;
private var tf:ButtonLabel;
//Constructor
public function ButtonTPL(_w:Number, _h:Number, _t:String = '') {
area.x = _w;
area.y = _h;
t = _t;
bg = new ButtonRect(area.x, area.y);
tf = new ButtonLabel(t, area.x, area.y);
addChild(bg);
addChild(tf);
}
//Public Method
public function focus():void {
this.alpha = 0.5;
}
public function unfocus():void {
this.alpha = 1;
}
public function setColor(n:String = "black"):void {
switch(n) {
case "red":
this.transform.colorTransform = new ColorTransform(
1, 1, 1, 1, 0xCC, 0, 0, 0
);
break;
case "green":
this.transform.colorTransform = new ColorTransform(
1, 1, 1, 1, 0, 0x99, 0, 0
);
break;
default:
this.transform.colorTransform = new ColorTransform(
1, 1, 1, 1, 0, 0, 0, 0
);
}
}
//Getter&Setter
public function get text():String {
return tf.text;
}
public function set text(_str:String):void {
tf.text = _str;
}
}
//ボタン背景
class ButtonRect extends Shape {
private var tf:ButtonLabel;
public function ButtonRect(_w:Number, _h:Number, _c:uint = 0x000000, _r:Number = 3) {
this.graphics.beginFill(_c);
this.graphics.drawRoundRect(0,0,_w,_h,_r);
this.graphics.endFill();
}
}
//ボタンラベル
class ButtonLabel extends TextField {
private var fmt:TextFormat;
public function ButtonLabel(_str:String = "", _w:Number = 0, _h:Number = 0) {
//文字
fmt = new TextFormat();
fmt.font = '_sans';
fmt.bold = true;
fmt.size = 32;
fmt.color = 0XFFFFFF;
this.defaultTextFormat = fmt;
this.text = _str;
this.width = _w;
this.height = _h;
this.selectable = false;
this.autoSize = TextFieldAutoSize.CENTER;
}
}
//Pad
class Pad extends ButtonTPL {
public function Pad() {
isSelect = false;
super(48, 48);
}
public function active():void {
if(!isSelect) {
this.transform.colorTransform = new ColorTransform(
1, 1, 1, 1, 0x66, 0x66, 0x66, 0
);
}
}
public function negative():void {
if(!isSelect) {
this.transform.colorTransform = new ColorTransform(
1, 1, 1, 1, 0, 0, 0, 0
);
}
}
public function select():void {
this.isSelect = true;
this.setColor('red');
}
}
//Play or Pause
class Transport extends ButtonTPL {
public const PLAY:String = 'play';
public const PAUSE:String = 'pause';
//Constructor
public function Transport() {
super(180, 40, PLAY);
}
}