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 { public static const MIN_SAMPLE_SIZE:int = 8192; private const MAX_DX_FILL:int = 100; private var _set:Boolean; private var _wave:Bitmap; public var waveData:Array; public var waveDataOffset:int; public var playHead:int; private var _prevMouseX:int, _prevMouseY:Number; 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.playHead = 0; this.waveData = new Array(MIN_SAMPLE_SIZE * 20); this.waveDataOffset = 0; this._prevMouseX = - MAX_DX_FILL; for(var x:int = 0; x < this.waveData.length; x++) { this.waveData[x] = 0; } this.plotWavePoints(); this.addEventListener(MouseEvent.CLICK, this._setWavePoint); this.addEventListener(MouseEvent.MOUSE_MOVE, this._setWavePoints); this.addEventListener(MouseEvent.MOUSE_DOWN, this._startSetting); } public function normalizeWave() { var maxAmp:Number = 0; for(var x:int = 0; x < this.waveData.length; x++) { if(maxAmp < Math.abs(this.waveData[x])) { maxAmp = Math.abs(this.waveData[x]); } } for(x = 0; x < this.waveData.length; x++) { this.waveData[x] /= maxAmp; } } public function plotWavePoints() { for(var x:int = 0; x < this.width; x++) { this._plotWavePoint(x, this.waveData[x + this.waveDataOffset]); } } private function _plotWavePoint(x:int, y:Number) { y = this.height / 2 - (y * this.height / 2); this._drawWavePoint(x, y); } private function _drawWavePoint(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.waveDataOffset] = (this.height / 2 - y) / (this.height / 2); } private function _setWavePoint(event:MouseEvent) { this._drawWavePoint(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._drawWavePoint(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._drawWavePoint(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.plotWavePoints(); } } }
demo
document.as
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 scrollBy(dx:int) { this._scroller.x = this._scrollX(this._scroller.x + dx); dispatchEvent(new Event(SCROLL)); } 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; import flash.text.StyleSheet; import flash.events.MouseEvent; import flash.text.TextFormat; public class Button extends Sprite { private var _label:TextField; private var _textFormat:TextFormat; private var _enabled:Boolean; public function Button(label:String = "", width:Number = 100, height:Number = 30) { //A gray box. this.graphics.beginFill(0x999999, 1); this.graphics.drawRect(0, 0, width, height); this.graphics.endFill(); //Centers the text label on the button. this._textFormat = new TextFormat(); this._textFormat.size = 20; this._textFormat.font = "Arial"; this._label = new TextField(); this._label.selectable = false; this._label.mouseEnabled = false; this.buttonMode = true; this.label = label; this.addChild(this._label); this._enabled = true; this.addEventListener(MouseEvent.MOUSE_OVER, this._mouseOver); this.addEventListener(MouseEvent.MOUSE_OUT, this._mouseOut); this.addEventListener(MouseEvent.MOUSE_DOWN, this._mouseDown); this.addEventListener(MouseEvent.MOUSE_UP, this._mouseUp); } private function _mouseOver(event:MouseEvent) { this._label.textColor = 0xffffff; } private function _mouseOut(event:MouseEvent) { this._label.textColor = 0x000000; } private function _mouseDown(event:MouseEvent) { this._label.textColor = 0x999999; } private function _mouseUp(event:MouseEvent) { this._label.textColor = 0xffffff; } public function get enabled():Boolean { return(this._enabled); } //Disables or enables the button. public function set enabled(state:Boolean):void { this._enabled = state; if(state) { this._label.alpha = 1; this.alpha = 1; this.mouseEnabled = true; this.buttonMode = true; } else { this._label.alpha = 0.0; this.alpha = 0.7; this.mouseEnabled = false; this.buttonMode = false; } } public function get label():String { return(this._label.text); } public function set label(label:String):void { this._label.text = label; this._label.setTextFormat(this._textFormat); this._label.width = this._label.textWidth + this._label.getLineMetrics(0).ascent; this._label.height = this._label.textHeight + this._label.getLineMetrics(0).descent; 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; } } }