Wheel
Wheelクラス
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/e9HI
*/
////////////////////////////////////////////////////////////////////////////////
// Wheel
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var wheel:Wheel;
private var label:Label;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
wheel = new Wheel();
addChild(wheel);
wheel.x = 182;
wheel.y = 152;
wheel.init({label: "wheel"});
wheel.addEventListener(CompoEvent.CHANGE, change, false, 0, true);
//
label = new Label(100, 20, 20, Label.CENTER);
addChild(label);
label.textColor = 0xCC0000;
label.text = "0";
label.x = 182;
label.y = 272;
}
private function change(evt:CompoEvent):void {
label.text = String(evt.value);
}
}
}
//////////////////////////////////////////////////
// Wheelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.geom.ColorTransform;
import flash.events.Event;
import flash.events.MouseEvent;
class Wheel extends Sprite {
private var base:Sprite;
private var thumb:Sprite;
private var point:Shape;
private var hit:Shape;
private var title:TextField;
private var txt:TextField;
private var label:String = "";
private static var fontType:String = "_ゴシック";
private static var __width:uint = 90;
private static var _height:uint = 85;
private static var tHeight:uint = 20;
private static var bColor:uint = 0xFFFFFF;
private static var cColor:uint = 0x999999;
private static var sColor:uint = 0x000000;
private static var tColor:uint = 0x666666;
private static var pColor:uint = 0x666666;
private static var offColor:uint = 0xCCCCCC;
private static var cColorTrans:ColorTransform;
private static var pColorTrans:ColorTransform;
private static var offColorTrans:ColorTransform;
private var zero:Number = 0;
private var angle:Number = 0;
private var shade:DropShadowFilter;
private var initValue:Number;
private var value:Number;
private var _enabled:Boolean = true;
public function Wheel() {
}
public function init(option:Object):void {
if (option.label != undefined) label = option.label;
if (option.zero != undefined) zero = option.zero;
if (option.angle != undefined) angle = option.angle;
value = initValue = angle;
draw();
}
private function draw():void {
shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 2, 3, false, false);
cColorTrans = new ColorTransform();
cColorTrans.color = cColor;
pColorTrans = new ColorTransform();
pColorTrans.color = pColor;
offColorTrans = new ColorTransform();
offColorTrans.color = offColor;
base = new Sprite();
thumb = new Sprite();
point = new Shape();
hit = new Shape();
title = new TextField();
txt = new TextField();
addChild(base);
base.addChild(thumb);
thumb.addChild(point);
thumb.addChild(hit);
addChild(title);
addChild(txt);
addChild(thumb);
base.x = thumb.x = 50;
base.y = thumb.y = 55;
createDonut(base, 30, 10);
base.filters = [shade];
point.x = 20;
createCircle(point, 5, bColor, 1);
hit.x = 20;
createCircle(hit, 10, bColor, 0);
title.height = tHeight-1;
title.type = TextFieldType.DYNAMIC;
title.selectable = false;
//title.embedFonts = true;
//title.antiAliasType = AntiAliasType.ADVANCED;
title.textColor = tColor;
title.autoSize = TextFieldAutoSize.LEFT;
var tfl:TextFormat = new TextFormat();
tfl.font = fontType;
tfl.size = 12;
tfl.align = TextFormatAlign.LEFT;
title.defaultTextFormat = tfl;
title.text = label;
txt.x = 50;
txt.width = 40;
txt.height = tHeight-1;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tfr:TextFormat = new TextFormat();
tfr.font = fontType;
tfr.size = 12;
tfr.align = TextFormatAlign.RIGHT;
txt.defaultTextFormat = tfr;
reset();
_up();
enabled = true;
thumb.mouseChildren = false;
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
_up();
}
private function press(evt:MouseEvent):void {
_down();
thumb.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseOutside, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, change, false, 0, true);
stage.addEventListener(Event.MOUSE_LEAVE, leave, false, 0, true);
}
private function release(evt:MouseEvent):void {
_up();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
}
private function releaseOutside(evt:MouseEvent):void {
_up();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
}
private function leave(evt:Event):void {
_up();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
}
private function _up():void {
point.transform.colorTransform = cColorTrans;
}
private function _over():void {
point.transform.colorTransform = pColorTrans;
}
private function _down():void {
point.transform.colorTransform = pColorTrans;
}
private function _off():void {
point.transform.colorTransform = offColorTrans;
txt.textColor = offColor;
}
private function change(evt:MouseEvent):void {
_down();
thumb.rotation = Math.round(Math.atan2(base.mouseY, base.mouseX)/Math.PI*180);
evt.updateAfterEvent();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.CHANGE, value);
dispatchEvent(e);
}
private function checkValue():void {
value = (thumb.rotation + 360 + 90 - zero)%360;
txt.text = String(value);
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
_enabled = param;
if (!_enabled) _off();
thumb.buttonMode = _enabled;
thumb.mouseEnabled = _enabled;
thumb.useHandCursor = _enabled;
if (_enabled) {
thumb.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
} else {
thumb.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
thumb.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
thumb.removeEventListener(MouseEvent.MOUSE_DOWN, press);
}
}
public function reset():void {
value = initValue;
thumb.rotation = value - 90 + zero;
txt.text = String(value);
}
private function createCircle(target:Shape, r:uint, color:uint, alpha:Number):void {
target.graphics.beginFill(color, alpha);
target.graphics.drawCircle(0, 0, r);
target.graphics.endFill();
}
private function createDonut(target:Sprite, outer:uint, inner:uint):void {
target.graphics.beginFill(bColor);
target.graphics.drawCircle(0, 0, outer);
target.graphics.drawCircle(0, 0, inner);
target.graphics.endFill();
}
}
//////////////////////////////////////////////////
// CompoEventクラス
//////////////////////////////////////////////////
import flash.events.Event;
class CompoEvent extends Event {
public static const SELECT:String = "select";
public static const CHANGE:String = "change";
public var value:*;
public function CompoEvent(type:String, value:*) {
super(type);
this.value = value;
}
public override function clone():Event {
return new CompoEvent(type, value);
}
}
//////////////////////////////////////////////////
// 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;
}
}