PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/canzam/mblocks/ControlPanel.as

https://code.google.com/p/music-blocks/
ActionScript | 1 lines | 1 code | 0 blank | 0 comment | 0 complexity | 0cf6b47facc71a6b10fa5027ab51a0b5 MD5 | raw file
  1. package canzam.mblocks { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.filters.BevelFilter; import flash.filters.DropShadowFilter; import flash.utils.Timer; import flash.media.Sound; import flash.media.SoundChannel; public class ControlPanel extends ControlPanel_Base { // Variables of ControlPanel_Base: // + sizeSlider:Slider // + sizeCombo:ComboBox // + pitchSlider:Slider // + pitchCombo:ComboBox // + removeBlocksButton:Button // + stopPlayingButton:Button // + helpButton:Button private var manager:BlockManager; private var dialogManager:DialogManager; private var block:Block; private var helper:HelpDialog; private var deleter:DeleteDialog; private var samples:SampleDialog; private var startHere:StartHere; private var fridayChannel:SoundChannel = null; public function ControlPanel(manager:BlockManager, dManager:DialogManager){ this.manager = manager; this.dialogManager = dManager; addEventListener(Event.ADDED_TO_STAGE, init); } private function init(evt:Event):void { removeEventListener(Event.ADDED_TO_STAGE, init); // Add bevel and drop shadow filters for 3D effect var bevel:BevelFilter = new BevelFilter(); bevel.distance = 3.0; bevel.strength = 0.7; var shadow:DropShadowFilter = new DropShadowFilter(); shadow.distance = 4.0; shadow.alpha = 0.5; this.filters = [bevel, shadow]; // Add number to size combo box for (var i:int = 1; i <= 10; i++){ sizeCombo.addItem({label: i}); } // Add note letters to letter combo box for (i = 0; i < NoteColor.COLORS.length; i++){ var letter:String = NoteColor.COLORS[i].letter; pitchCombo.addItem({label: letter}); } // Configure rotation combo for (i = -36; i <= 36; i++){ rotationCombo.addItem({label: i*5}); } rotationCombo.selectedIndex = 36; // Configure translation combos for (i = 0; i <= 20; i++){ translateXCombo.addItem({label: (i*10) - 100}); } translateXCombo.selectedIndex = 10; for (i = 0; i <= 20; i++){ translateYCombo.addItem({label: (i*10) - 100}); } translateYCombo.selectedIndex = 10; // Configure tabs createTabButton.selected = true; openCreateButtonClicked(null); createTabButton.addEventListener(MouseEvent.CLICK,openCreateButtonClicked); specialTabButton.addEventListener(MouseEvent.CLICK,openSpecialButtonClicked); editTabButton.addEventListener(MouseEvent.CLICK,openEditButtonClicked); // Add specialCombo items /* specialCombo.addItem({label:"None"}); specialCombo.addItem({label:"Flip"}); specialCombo.addItem({label:"Rotate 90"}); specialCombo.addItem({label:"Trans 50 l"}); specialCombo.addItem({label:"Trans 50 u"}); specialCombo.addItem({label:"Trans 50 r"}); specialCombo.addItem({label:"Trans 50 d"}); */ // Configure size slider sizeSlider.minimum = 0; sizeSlider.maximum = sizeCombo.length - 1; sizeSlider.snapInterval = 1; sizeSlider.liveDragging = true; // Configure pitch slider pitchSlider.minimum = 0; pitchSlider.maximum = pitchCombo.length - 1; pitchSlider.snapInterval = 1; pitchSlider.liveDragging = true; // Configure tempo slider // Configure rotation slider rotationSlider.minimum = -180; rotationSlider.maximum = 180; rotationSlider.snapInterval = 5; rotationSlider.liveDragging = true; // Configure translation sliders translateXSlider.minimum = -100; translateXSlider.maximum = 100; translateXSlider.snapInterval = 10; translateXSlider.liveDragging = true; translateYSlider.minimum = -100; translateYSlider.maximum = 100; translateYSlider.snapInterval = 10; translateYSlider.liveDragging = true; // Start at size 6 block, which is a really nice size sizeSlider.value = 5; sizeCombo.selectedIndex = 5; // Start at C3, or middle C. Also, it shows up well against // the wood background compared to C4 pitchSlider.value = 8; pitchCombo.selectedIndex = 8; // The contructor values are unimportant, as they will be modified anyway // by the values of the sliders block = new Block(50, 100, 30, NoteColor.COLORS[0], 0, 0, 0); block.x = 75; block.y = 105; // We take out the filters because the ControlPanel already has filters // applied to it, so the effect is needlessly multiplied if the block // has filters as well block.blockShape.filters = []; modifyBlock(); addChild(block); helper = new HelpDialog(); deleter = new DeleteDialog(); samples = new SampleDialog(); startHere = new StartHere(); stage.addEventListener(MouseEvent.MOUSE_DOWN, stageMouseDown); block.addEventListener(MouseEvent.MOUSE_DOWN, genesis); sizeSlider.addEventListener(Event.CHANGE, sizeSliderChanged); sizeCombo.addEventListener(Event.CHANGE, sizeComboChanged); pitchCombo.addEventListener(Event.CHANGE, pitchComboChanged); pitchSlider.addEventListener(Event.CHANGE, pitchSliderChanged); rotationSlider.addEventListener(Event.CHANGE, rotationSliderChanged); rotationCombo.addEventListener(Event.CHANGE, rotationComboChanged); translateXSlider.addEventListener(Event.CHANGE, translateXSliderChanged); translateXCombo.addEventListener(Event.CHANGE, translateXComboChanged); translateYSlider.addEventListener(Event.CHANGE, translateYSliderChanged); translateYCombo.addEventListener(Event.CHANGE, translateYComboChanged); removeBlocksButton.addEventListener(MouseEvent.CLICK, removeBlocksDialog); removeSpecialsButton.addEventListener(MouseEvent.CLICK, removeSpecials); deleter.xButton.addEventListener(MouseEvent.CLICK,noDelete); deleter.noButton.addEventListener(MouseEvent.CLICK,noDelete); deleter.yesButton.addEventListener(MouseEvent.CLICK,yesDelete); stopPlayingButton.addEventListener(MouseEvent.CLICK, stopPlaying); helpButton.addEventListener(MouseEvent.CLICK, showHelp); helper.xButton.addEventListener(MouseEvent.CLICK, hideHelp); startHere.xButton.addEventListener(MouseEvent.CLICK, removeStartHereDialog); sampleSongsButton.addEventListener(MouseEvent.CLICK, showSamples); samples.xButton.addEventListener(MouseEvent.CLICK, hideSamples); samples.maryHadALittleLambButton.addEventListener(MouseEvent.CLICK, showSong1); samples.twinkleTwinkleLittleStarButton.addEventListener(MouseEvent.CLICK, showSong2); samples.takeTheCubeRootButton.addEventListener(MouseEvent.CLICK, showSong3); //specialCombo.addEventListener(Event.CHANGE, specialComboChanged); } private function stageMouseDown(evt:MouseEvent){ openEditButtonClicked(null); } private function openCreateButtonClicked(evt:MouseEvent){ if(createTabButton.selected) // button was not already selected. Toggle makes this seem odd. { changeSpecialTab(false); //changeEditTab(false); changeCreateTab(true); } createTabButton.selected = true; } private function openSpecialButtonClicked(evt:MouseEvent){ if(specialTabButton.selected) // button was not already selected. Toggle makes this seem odd. { //changeEditTab(false); changeCreateTab(false); changeSpecialTab(true); } specialTabButton.selected = true; } public function openEditButtonClicked(evt:MouseEvent){ if(editTabButton.selected == true && manager.selected != null) // button was not already selected. Toggle makes this seem odd. { var b:Block = manager.selected; sizeSlider.value = (b.blockShape.sideLength / 10) - 1; sizeCombo.selectedIndex = sizeSlider.value; pitchCombo.selectedIndex = NoteColor.COLORS.indexOf(b.noteColor); pitchSlider.value = pitchCombo.selectedIndex; rotationSlider.value = b.willRotate; rotationCombo.selectedIndex = (rotationSlider.value / 5) + 36 translateXSlider.value = b.willTranslateX; translateXCombo.selectedIndex = 10 + (translateXSlider.value / 10); translateYSlider.value = b.willTranslateY; translateYCombo.selectedIndex = 10 + (translateYSlider.value / 10); modifyBlock(); } } private function changeSpecialTab(b:Boolean){ //specialText.visible = b; //specialCombo.visible = b; specialTabButton.selected = b; rotationLabel.visible = b; rotationCombo.visible = b; rotationSlider.visible = b; translateXLabel.visible = b; translateXSlider.visible = b; translateXCombo.visible = b; translateYLabel.visible = b; translateYSlider.visible = b; translateYCombo.visible = b; } private function changeEditTab(b:Boolean){ editTabButton.selected = b; } private function changeCreateTab(b:Boolean){ createTabButton.selected = b; colorPitchText.visible = b; pitchSlider.visible = b; pitchCombo.visible = b; sizeVolumeText.visible = b; sizeSlider.visible = b; sizeCombo.visible = b; removeSpecialsButton.visible = b; } private function specialComboChanged(evt:Event) { modifyBlock(); } private function removeSpecials(evt:MouseEvent):void{ rotationSlider.value = 0; rotationSliderChanged(null); translateXSlider.value = 0; translateXSliderChanged(null); translateYSlider.value = 0; translateYSliderChanged(null); } private function startHereDialog(x:Number, y:Number):void { if (dialogManager.dialogs.indexOf(startHere) != -1) { dialogManager.remove(startHere); } dialogManager.add(startHere); startHere.x = x; startHere.y = y; } private function removeStartHereDialog(evt:MouseEvent):void { dialogManager.remove(startHere); } private function removeBlocksDialog(evt:MouseEvent):void { if (dialogManager.dialogs.indexOf(deleter) == -1) { dialogManager.add(deleter); deleter.x = 200; deleter.y = 200; } } private function showSamples(evt:MouseEvent):void{ if (dialogManager.dialogs.indexOf(samples) == -1) { dialogManager.add(samples); samples.x = 175; samples.y = 50; } } private function hideSamples(evt:MouseEvent):void { dialogManager.remove(samples); } private function showSong1(evt:MouseEvent):void { startHereDialog(200,125); removeBlocks(null); createBlock(4,200,125,90); createBlock(3,275,125,90); createBlock(2,375,125,90); createBlock(3,450,125,90); createBlock(4,525,125,90); createBlock(4,600,125,180); createBlock(4,600,200,180); createBlock(0,600,275,180); createBlock(3,600,350,180); createBlock(3,600,425,270); createBlock(3,525,425,270); createBlock(0,450,425,270); createBlock(4,375,425,270); createBlock(6,300,425,270); createBlock(6,225,425,0); createBlock(0,225,350,0); createBlock(4,225,275,0); createBlock(3,225,200,90); createBlock(2,300,200,90); createBlock(3,375,200,90); createBlock(4,450,200,90); createBlock(4,525,200,180); createBlock(4,525,275,180); createBlock(4,525,350,270); createBlock(3,450,350,270); createBlock(3,375,350,270); createBlock(4,300,350,0); createBlock(3,300,275,90); createBlock(2,375,275,90); } private function showSong2(evt:MouseEvent):void { removeBlocks(null); startHereDialog(200,400); createBlockRotate90Right(4,200,400,90); createBlockFlip(3,275,400,90); createBlockFlip(2,350,400,90); createBlockFlip(0,425,400,0); createBlock(4,425,325,270); createBlock(3,350,325,270); createBlock(2,275,325,270); createBlockRotate90Left(0,200,325,0); createBlock(2,200,250,90); createBlock(2,275,250,90); createBlock(3,350,250,90); createBlock(3,425,250,180); } private function showSong3(evt:MouseEvent):void { if(fridayChannel == null) { var mySound:Friday = new Friday(); fridayChannel = mySound.play(); } } private function createBlock(nc:Number, x:Number , y:Number , rotation:Number):void{ var b; b = new Block(50, 100, 30, NoteColor.COLORS[nc], 0,0,0); manager.add(b); b.x = x; b.y = y; b.rotation = rotation; } private function createBlockRotate90Right(nc:Number, x:Number , y:Number , rotation:Number):void{ var b; b = new Block(50, 100, 30, NoteColor.COLORS[nc], 90,0,0); manager.add(b); b.x = x; b.y = y; b.rotation = rotation; } private function createBlockRotate90Left(nc:Number, x:Number , y:Number , rotation:Number):void{ var b; b = new Block(50, 100, 30, NoteColor.COLORS[nc], -90,0,0); manager.add(b); b.x = x; b.y = y; b.rotation = rotation; } private function createBlockFlip(nc:Number, x:Number , y:Number , rotation:Number):void{ var b; b = new Block(50, 100, 30, NoteColor.COLORS[nc], 180,0,0); manager.add(b); b.x = x; b.y = y; b.rotation = rotation; } private function yesDelete(evt:MouseEvent):void { dialogManager.remove(deleter); removeBlocks(evt); } private function noDelete(evt:MouseEvent):void { dialogManager.remove(deleter); } private function removeBlocks(evt:MouseEvent):void { manager.clear(); } private function stopPlaying(evt:MouseEvent):void { manager.stopPlaying(); if (fridayChannel != null) { fridayChannel.stop(); fridayChannel = null; } } private function showHelp(evt:MouseEvent):void { if (dialogManager.dialogs.indexOf(helper) == -1) { dialogManager.add(helper); // The Helper is centered on the screen when helper.x = 175 helper.x = 175; helper.y = -helper.height; addEventListener(Event.ENTER_FRAME, moveHelp); } } // Slide the help dialog into position private function moveHelp(evt:Event):void { // The helper is centered on the screen when helper.y = 7.5 helper.y = Math.min(7.5 , helper.y + 40); if (helper.y == 7.5) { removeEventListener(Event.ENTER_FRAME, moveHelp); } } private function hideHelp(evt:MouseEvent):void { dialogManager.remove(helper); } private function genesis(evt:MouseEvent):void { var b:Block = block.clone(); manager.add(b); manager.selected = b; // The target of this event is not the newly created block, // so it will be immediately deselected by the manager if // the event is allowed to bubble to the stage. evt.stopPropagation(); } private function modifyBlock():void { modifyBlockOverload(block); } private function modifyBlockOverload(b:Block):void { var size:Number = 10 * (sizeCombo.selectedIndex + 1); b.sideLength = size; b.effectArea.radius = size * 2; b.note.amplitude = (sizeCombo.selectedIndex + 1) / 200; b.noteColor = NoteColor.COLORS[pitchCombo.selectedIndex]; b.rotateHandle.radius = b.sideLength / 2 + 10; b.willRotate = rotationSlider.value; b.willTranslateX = translateXSlider.value; b.willTranslateY = translateYSlider.value; b.note.amplitude = b.sideLength / 200; //b.special = specialCombo.selectedLabel; if (editTabButton.selected && b != manager.selected) { modifyBlockOverload(manager.selected); //manager.selected.refreshBlock(); } } private function sizeSliderChanged(evt:Event):void { sizeCombo.selectedIndex = sizeSlider.value; modifyBlock(); } private function sizeComboChanged(evt:Event):void { sizeSlider.value = sizeCombo.selectedIndex; modifyBlock(); } private function pitchSliderChanged(evt:Event):void { pitchCombo.selectedIndex = pitchSlider.value; modifyBlock(); } private function pitchComboChanged(evt:Event):void { pitchSlider.value = pitchCombo.selectedIndex; modifyBlock(); } private function rotationComboChanged(evt:Event):void { rotationSlider.value = -180 + (5 * (rotationCombo.selectedIndex - 1)); modifyBlock(); } private function rotationSliderChanged(evt:Event):void { rotationCombo.selectedIndex = (rotationSlider.value / 5) + 36; modifyBlock(); } private function translateXComboChanged(evt:Event):void { translateXSlider.value = (10 * translateXCombo.selectedIndex) - 100; modifyBlock(); } private function translateXSliderChanged(evt:Event):void { translateXCombo.selectedIndex = 10 + (translateXSlider.value / 10); modifyBlock(); } private function translateYComboChanged(evt:Event):void { translateYSlider.value = (10 * translateYCombo.selectedIndex) - 100; modifyBlock(); } private function translateYSliderChanged(evt:Event):void { translateYCombo.selectedIndex = 10 + (translateYSlider.value / 10); modifyBlock(); } } }