Scripts.FranciscoCharrua.com
Index
News
Downloads
SCREEN.AS
package classes { import flash.display.Bitmap; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Rectangle; import flash.display.BitmapData; public class Screen extends Sprite { private const MAX_DX_FILL:int = 100; private var _set:Boolean; private var _wave:Bitmap; public var waveData:Array; private var _prevMouseX:int, _prevMouseY:Number, _x:int; public function Screen(width:int, height:Number) { this._wave = new Bitmap(); this._wave.bitmapData = new BitmapData(width - 1, height, false, 0x000000); this.addChild(this._wave); this._x = 0; this.waveData = new Array(width); /******************************************************************* Each point in the sound wave is drawn using a one pixel wide sprite. ********************************************************************/ for(var x:int = 0; x < this.waveData.length; x++) { this._prevMouseX = - MAX_DX_FILL; this.setWavePoint(x, this.height / 2 + 1); this.waveData[x] = 0; } this.addEventListener(MouseEvent.CLICK, this._setWavePoint); this.addEventListener(MouseEvent.MOUSE_MOVE, this._setWavePoints); this.addEventListener(MouseEvent.MOUSE_DOWN, this._startSetting); } private function getWavePoint(y:Number):Number { return((this.height / 2 - y) / (this.height / 2)); } private function setWavePoint(x:int, y:Number) { if(x < 0 || x > this.waveData.length - 1) { return; } if(y < 0) { y = 0; } if(y > this.height - 1) { y = this.height - 1; } this._wave.bitmapData.fillRect(new Rectangle(x, 0, 1, this.height), 0x000000); if(y < this.height / 2) { this._wave.bitmapData.fillRect(new Rectangle(x, y, 1, this.height / 2 - y), 0xffffff); } else { this._wave.bitmapData.fillRect(new Rectangle(x, this.height / 2, 1, y - this.height / 2), 0xffffff); } //Makes the sound data available as soon as it's drawn. this.waveData[x] = this.getWavePoint(y); } private function _setWavePoint(event:MouseEvent) { this.setWavePoint(mouseX - 1, mouseY); } private function _setWavePoints(event:MouseEvent) { if(this._set) { //Draws on the graph when the mouse is moved with the left button down. this.setWavePoint(mouseX - 1, mouseY); if(Math.abs(this._prevMouseX - mouseX) < MAX_DX_FILL) { var dx:int = mouseX - this._prevMouseX > 0 ? 1 : -1; var dy:Number = (mouseY - this._prevMouseY) / Math.abs(mouseX - this._prevMouseX); var y:Number = mouseY; //Fills in the gaps. for(var x:int = this._prevMouseX; Math.abs(x - mouseX) > 0; x += dx) { this.setWavePoint(x - 1, y); y += dy; } } this._prevMouseX = mouseX; this._prevMouseY = mouseY; } } private function _startSetting(event:MouseEvent) { this._set = true; } private function _stopSetting(event:MouseEvent) { this._set = false; this._prevMouseX = - MAX_DX_FILL; } public function stopSetting(event:MouseEvent) { this._stopSetting(event); } public function reset() { for(var x:int = 0; x < this.waveData.length; x++) { this.waveData[x] = 0; this.setWavePoint(x, this.height / 2 + 1); } } } }
demo
document.as
Screen.as
old demo
old Screen.as
SCROLLBAR.AS
package classes { import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.Event; import classes.Button; public class ScrollBar extends Sprite { public static const SCROLL:String = "SCROLL"; private var _scroller:Button; private var _dragging:Boolean; private var _mouseX:int; public function ScrollBar(width:Number, height:Number) { this.graphics.beginFill(0x000000, 0.5); this.graphics.drawRect(0, 0, width, height); this._scroller = new Button("", 50, height); this._scroller.mouseEnabled = false; this._dragging = false; this.addChild(this._scroller); this.addEventListener(MouseEvent.CLICK, this._scroll); this.addEventListener(MouseEvent.MOUSE_MOVE, this._drag); this.addEventListener(MouseEvent.MOUSE_OUT, this._slip); this.addEventListener(MouseEvent.MOUSE_DOWN, this._grab); } private function _scroll(event:MouseEvent) { //For when the user clicks somewhere on the scroll bar. var x:int = this._scrollX(mouseX); if(!this._dragging){ this._scroller.x = x; dispatchEvent(new Event(SCROLL)); } this._dragging = false; } private function _drag(event:MouseEvent) { //For when the user is dragging the mouse around. var x:int = this._scrollX(mouseX - this._mouseX); if(this._dragging){ this._scroller.x = x; dispatchEvent(new Event(SCROLL)); } } //I had to disable the mouse on the button in order to make the SCROLL event consistent. private function _grab(event:MouseEvent) { if(mouseX > this._scroller.x && mouseX < this._scroller.x + this._scroller.width) { this._dragging = true; this._mouseX = mouseX - this._scroller.x; } } private function _slip(event:MouseEvent) { this._dragging = false; } private function _scrollX(x:int):int { if(x > this.width - this._scroller.width) { x = this.width - this._scroller.width; } if(x < 0) { x = 0; } //Keeps the button inside the bounds of the scroll bar. return(x); } public function get scrollPercent():Number { return(this._scroller.x / (this.width - this._scroller.width)); } } }
BUTTON.AS
package classes { import flash.display.Sprite; import flash.text.TextField; public class Button extends Sprite { private var _label:TextField; public function Button(label:String = "", width:Number = 100, height:Number = 30) { //A gray box. this.graphics.beginFill(0x999999, 0.75); this.graphics.drawRect(0, 0, width, height); this.graphics.endFill(); //Centers the text label on the button. if(label != "") { this._label = new TextField(); this._label.text = label; this._label.background = false; this._label.x = (this.width - this._label.textWidth) / 2; this._label.y = (this.height - this._label.textHeight - this._label.thickness - this._label.getLineMetrics(0).descent) / 2; this._label.selectable = false; this.addChild(this._label); } } } }