[minimalComps]LogSlider
対数スケールのスライダー。
この例だとmaximumが10^4、minimumが10^0なので、ハンドルを真ん中に持っていくとvalueは10^2、すなわち100になります。
maximumやminimumの値が10^nの時は正常に動作するはずですが、他の場合は合ってるか分かりません。
/**
* Copyright Nicolas ( http://wonderfl.net/user/Nicolas )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3F2a
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import com.bit101.components.*;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
var hLabel:Label = new Label(this, 0, 0);
hLabel.text = "1";
var hSlider:HLogSlider = new HLogSlider(this, 0, 20, function(e:Event):void{
hLabel.text = String(hSlider.value);
});
hSlider.minimum = 1;
hSlider.maximum = 10000;
var vLabel:Label = new Label(this, 200, 100);
vLabel.text = "1";
var vSlider:VLogSlider = new VLogSlider(this, 200, 0, function(e:Event):void{
vLabel.text = String(vSlider.value);
});
vSlider.minimum = 1;
vSlider.maximum = 10000;
}
}
}
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.DisplayObjectContainer;
import com.bit101.components.*;
class LogSlider extends Slider{
public function LogSlider(
orientation:String = Slider.HORIZONTAL,
parent:DisplayObjectContainer = null,
xpos:Number = 0,
ypos:Number = 0,
defaultHandler:Function = null
)
{
super(orientation, parent, xpos, ypos);
if(defaultHandler != null)
{
addEventListener(Event.CHANGE, defaultHandler);
}
}
override protected function positionHandle():void
{
if(_min <= 0)
{
minimum = 1;
}
var logMin:Number = Math.log(_min);
var logMax:Number = Math.log(_max);
var logValue:Number = Math.log(_value);
var range:Number;
if(_orientation == HORIZONTAL)
{
range = _width - _height;
_handle.x = (logValue - logMin) / (logMax - logMin) * range;
}
else
{
range = _height - _width;
_handle.y = _height - _width - (logValue - logMin) / (logMax - logMin) * range;
}
}
override protected function onSlide(event:MouseEvent):void
{
var oldValue:Number = _value;
if(_min <= 0)
{
minimum = 1;
}
var logMin:Number = Math.log(_min);
var logMax:Number = Math.log(_max);
var logValue:Number;
if(_orientation == HORIZONTAL)
{
logValue = _handle.x / (width - _height) * (logMax - logMin) + logMin;
_value = Math.exp(logValue);
}
else
{
logValue = (_height - _width - _handle.y) / (height - _width) * (logMax - logMin) + logMin;
_value = Math.exp(logValue);
}
if(_value != oldValue)
{
dispatchEvent(new Event(Event.CHANGE));
}
}
override protected function onBackClick(event:MouseEvent):void
{
if(_min <= 0)
{
minimum = 1;
}
var logMin:Number = Math.log(_min) * Math.LOG10E;
var logMax:Number = Math.log(_max) * Math.LOG10E;
var logValue:Number;
if(_orientation == HORIZONTAL)
{
_handle.x = mouseX - _height / 2;
_handle.x = Math.max(_handle.x, 0);
_handle.x = Math.min(_handle.x, _width - _height);
logValue = _handle.x / (width - _height) * (logMax - logMin) + logMin;
_value = Math.exp(logValue);
}
else
{
_handle.y = mouseY - _width / 2;
_handle.y = Math.max(_handle.y, 0);
_handle.y = Math.min(_handle.y, _height - _width);
logValue = (_height - _width - _handle.y) / (height - _width) * (logMax - logMin) + logMin;
_value = Math.exp(logValue);
}
dispatchEvent(new Event(Event.CHANGE));
}
}
class HLogSlider extends LogSlider
{
public function HLogSlider(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function = null)
{
super(Slider.HORIZONTAL, parent, xpos, ypos, defaultHandler);
}
}
class VLogSlider extends LogSlider
{
public function VLogSlider(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function = null)
{
super(Slider.VERTICAL, parent, xpos, ypos, defaultHandler);
}
}