forked from: アナログ入力実験(GAINER)
表現を変更してみた
* forked by nomura masayuki
GAINERのアナログ入力[0,1]とデジタル入力[0]を使うサンプル
* AIN[0]=中心位置(横方向)
* AIN[1]=中心位置(縦方向)
* DIN[0)=座標( AIN[0], AIN[1] )を中心にパーティクルを運動させる
* ついでにDOUT[0]もオンにする
* created by nomura masayuki
/**
* Copyright king_nomura ( http://wonderfl.net/user/king_nomura )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/a1nh
*/
// forked from king_nomura's アナログ入力実験(GAINER)
/* 表現を変更してみた
* forked by nomura masayuki
*/
/*
* GAINERのアナログ入力[0,1]とデジタル入力[0]を使うサンプル
* AIN[0]=中心位置(横方向)
* AIN[1]=中心位置(縦方向)
* DIN[0)=座標( AIN[0], AIN[1] )を中心にパーティクルを運動させる
* ついでにDOUT[0]もオンにする
* created by nomura masayuki
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
[SWF(width="300", height="300", backgroundColor="0xFFFFFF", frameRate="30")]
public class Sample4 extends Sprite
{
private var mc:CircleMC;
public function Sample4()
{
mc = new CircleMC();
mc.addEventListener(CircleMC.INIT_COMPLETE, circleMCInitCompleteHandler);
addChild(mc);
}
private function circleMCInitCompleteHandler(event:Event):void
{
addChild(mc.ui);
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import funnel.Convolution;
import funnel.Gainer;
import funnel.gui.GainerGUI;
import funnel.PinEvent;
import funnel.Scaler;
class CircleMC extends MovieClip
{
public static const INIT_COMPLETE:String = "CircleMC.INIT_COMPLETE";
private static const _PARTICLES_COUNT:uint = 3000;
private static const _SPEED_MAX:uint = 10;
private static const _SIZE_MAX:uint = 300;
private static const _SIZE_MIN:uint = 10;
private var _gainerUI:GainerGUI;
private var _gainerIO:Gainer;
private var _particleArray:Array /* of Particle */ = [];
private var centerX:Number;
private var centerY:Number;
private var bitmap:Bitmap;
private var dio1:Boolean = false;
public function CircleMC()
{
if (stage) init() else addEventListener(Event.ADDED_TO_STAGE, init);
}
public function get ui():DisplayObject
{
return _gainerUI;
}
private function init(event:Event = null):void
{
if (event) removeEventListener(Event.ADDED_TO_STAGE, init);
initGainer();
initGraphics(stage.stageWidth / 2, stage.stageHeight / 2 + 50, _gainerIO.analogInput(0).value, _gainerIO.analogInput(1).value);
dispatchEvent(new Event(CircleMC.INIT_COMPLETE));
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
drawGraphics();
}
private function initGainer():void
{
_gainerIO = new Gainer();
_gainerUI = new GainerGUI();
_gainerIO.gui = _gainerUI;
var smoother:Convolution = new Convolution(Convolution.MOVING_AVERAGE);
var scalerSpeed:Scaler = new Scaler(
_gainerIO.analogInput(0).minimum, _gainerIO.analogInput(0).maximum,
_SPEED_MAX, 0, Scaler.LINEAR, true);
var scalerSize:Scaler = new Scaler(
_gainerIO.analogInput(1).minimum, _gainerIO.analogInput(1).maximum,
_SIZE_MAX, _SIZE_MIN, Scaler.LINEAR, true);
_gainerIO.analogInput(0).filters = [smoother, scalerSpeed];
_gainerIO.analogInput(1).filters = [smoother, scalerSize];
_gainerIO.analogInput(0).addEventListener(PinEvent.CHANGE, pinChangeHandler);
_gainerIO.analogInput(1).addEventListener(PinEvent.CHANGE, pinChangeHandler);
_gainerIO.digitalInput(0).addEventListener(PinEvent.FALLING_EDGE, dout1ReleaseHandler);
_gainerIO.digitalInput(0).addEventListener(PinEvent.RISING_EDGE, dout1DownHandler);
}
private function pinChangeHandler(event:PinEvent):void
{
//if (!dio1) return;
if (event.target == _gainerIO.analogInput(0))
{
var _speed:Number = _gainerIO.analogInput(0).value;
for each (var particle1:Particle in _particleArray)
{
particle1.speed = _speed;
}
}
if (event.target == _gainerIO.analogInput(1))
{
var _size:Number = _gainerIO.analogInput(1).value;
for each (var particle2:Particle in _particleArray)
{
particle2.size = _size;
}
}
drawGraphics();
}
private function initGraphics(_x:Number, _y:Number, _speed:Number, _size:Number):void
{
centerX = _x;
centerY = _y;
for (var i:uint = 0; i < _PARTICLES_COUNT; i++)
{
var particle:Particle = new Particle(centerX, centerY, _speed, _size);
_particleArray.push(particle);
}
bitmap = new Bitmap();
addChild(bitmap);
drawGraphics();
}
private function dout1ReleaseHandler(event:PinEvent):void
{
dio1 = false;
_gainerIO.digitalOutput(0).value = 0;
}
private function dout1DownHandler(event:PinEvent):void
{
dio1 = true;
_gainerIO.digitalOutput(0).value = 1;
//pinChangeHandler(event);
}
private function drawGraphics():void
{
var bitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFFFF);
var rect:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
var color:uint = (dio1) ? 0x00660000 : 0x00666666;
for each (var particle:Particle in _particleArray)
{
if (rect.contains(particle.x, particle.y))
bitmapData.setPixel(particle.x, particle.y, color);
particle.move(dio1);
}
bitmap.bitmapData = bitmapData;
}
}
class Particle
{
private var _size:Number;
private var _center:Point;
private var _x:Number;
private var _y:Number;
private var _dx:Number;
private var _dy:Number;
private var _speedA:Number;
private var _speedB:Number;
public function Particle(__x:Number, __y:Number, __speed:Number, __size:Number)
{
_size = __size;
_center = new Point(__x, __y);
_speedA = Math.random()* 2;
_speedB = __speed;
var rad:Number = Math.random() * Math.PI * 2;
_dx = Math.cos(rad) * (_speedA + _speedB);
_dy = Math.sin(rad) * (_speedA + _speedB);
var distance:Number = Math.random() * _size;
_x = __x + _dx * distance;
_y = __y + _dy * distance;
}
public function move(mode:Boolean):void
{
_x -= _dx;
_y -= _dy;
var distance:Number = Point.distance(_center, point);
if (!mode) return;
if (distance > _size) resetCenter();
if (distance < 5)
{
var rad:Number = Math.random() * Math.PI * 2;
_dx = Math.cos(rad) * (_speedA + _speedB);
_dy = Math.sin(rad) * (_speedA + _speedB);
}
}
public function get x():Number
{
return _x;
}
public function get y():Number
{
return _y;
}
public function get point():Point
{
return new Point(_x, _y);
}
public function set speed(value:Number):void
{
_speedB = value;
}
public function set size(value:Number):void
{
_size = value;
}
private function resetCenter():void
{
var newPoint:Point = new Point(_x - _center.x, _y - _center.y);
newPoint.normalize(1);
_dx = newPoint.x * (_speedA + _speedB);
_dy = newPoint.y * (_speedA + _speedB);
}
public function set dy(val:Number):void
{
_dy = val;
}
}