<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:v="view.*"
    xmlns:cps="components.ps.*"
    xmlns:ckb="components.kb.*"
    layout="absolute"
    initialize="initAppHandler(event);"
    applicationComplete="appCompleteHandler(event);"
    width="584" viewSourceURL="srcview/index.html" height="254">

    <mx:Script>
        <![CDATA[
          // platform
          import mx.collections.ArrayCollection;
          import flash.events.KeyboardEvent;
          import flash.events.MouseEvent;
          import mx.controls.ComboBox;
          import com.adobe.viewsource.ViewSource;
          
          // app
          import model.Tuning;
          import components.ps.PitchSliderEvent;
          import model.Scale;
          import utils.Assert;
          import model.Note;
          import components.kb.KBCanvas;
          
          // see Scale.as
          [Bindable] 
          public var primeScales:ArrayCollection = new ArrayCollection([ 
            {label:"Diatonic",            data:"FaDoSoReLaMiTi"}, 
            {label:"Melodic",            data:"TeDoSoReLaMiFi"}, 
            {label:"Neapolitan",        data:"MeFaSoReLaTiDi"},
            {label:"Double Harmonic",    data:"MeTeSoReLaFiDi"},
            {label:"Harmonic Minor",    data:"TeFaSoReLaMiDi"},  // FaDoReLaMiTiSi
            {label:"Harmonic Major",    data:"MeDoSoReLaTiFi"}   // LeFaDoSoReMiTi
            ]);

          public function initAppHandler(event:Event):void {
              // default to the diatonic scale
              Scale.globalScale.name  = primeScales[0].name;
              Scale.globalScale.notes = primeScales[0].data;
          }
          
          public function appCompleteHandler(event:Event):void {
            kbCanvas.setFocus();
            ViewSource.addMenuItem(this, "srcview/index.html");
          }
          
          public function globalMouseHandler(e:MouseEvent):void {
              if (e.target == kbCanvas) {                  
                trace("globalMouseHandler(): " + e.type);
                // kbCanvas.setFocus();
                stage.focus = kbCanvas;
              }
          }
          
          public function frequencyChangedHandler(e:PitchSliderEvent):void {
              Tuning.globalTuning.refFreq = e.newFrequency;
          }
         
          
          public function scaleChangedHandler(event:Event):void {
              var ct:ComboBox = (event.currentTarget as ComboBox);
              CONFIG::DEBUG {Assert.assertTrue("scaleChangedHandler(): ct != null", ct != null);}
              CONFIG::DEBUG {Assert.assertTrue("scaleChangedHandler(): ct == cbScales", ct == cbScales);}
              CONFIG::DEBUG {Assert.assertTrue("scaleChangedHandler(): ct.selectedItem != null", ct.selectedItem != null);}
              
              var d:Object = ct.selectedItem;
              
              trace("\n\td.label: \"" + d.label + "\n\td.data: \"" + d.data);
              
              Scale.globalScale.name  = d.label;
              Scale.globalScale.notes = d.data;

                   
              // invalidate the kbButtons
            for (var i:int; i < kbCanvas.numChildren; i++) {
                // init childByKeyCode
                var c:KBButton = (kbCanvas.getChildAt(i) as KBButton);
                CONFIG::DEBUG {Assert.assertTrue("creationComplete(): c != null", c != null );}
                
                var n:Note = c.note;
                if ( Note.isValidNote(n) ) {
                    c.invalidateDisplayList();
                }
            }
          }
        ]]>
    </mx:Script>
        

    <mx:Panel id="panel" title="Pitch of Re0" 
        height="250" width="100" 
        layout="horizontal"
        paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
        
        <cps:PitchSliderBox id="psBox" horizontalAlign="center" frequencyChanged="frequencyChangedHandler(event)"/>
    </mx:Panel>
    
    <ckb:KBComboBox x="108" y="177" dataProvider="{primeScales}" id="cbScales" selectedIndex="0" enabled="true" change="scaleChangedHandler(event);"></ckb:KBComboBox>
    
    <ckb:KBCanvas id="kbCanvas" x="108" y="0" width="476" height="169">
        <ckb:KBButton mouseEnabled="false" x="5" y="5" width="30" height="30" label="`" keyCode="192" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="37" y="5" width="30" height="30" label="1" keyCode="49" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="69" y="5" width="30" height="30" label="2" keyCode="50" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="101" y="5" width="30" height="30" label="3" keyCode="51" alphas="5" betas="-8" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="133" y="5" width="30" height="30" label="4" keyCode="52" alphas="4" betas="-6" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="165" y="5" width="30" height="30" label="5" keyCode="53" alphas="3" betas="-4" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="197" y="5" width="30" height="30" label="6" keyCode="54" alphas="2" betas="-2" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="229" y="5" width="30" height="30" label="7" keyCode="55" alphas="1" betas="0" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="261" y="5" width="30" height="30" label="8" keyCode="56" alphas="0" betas="2" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="293" y="5" width="30" height="30" label="9" keyCode="57" alphas="-1" betas="4" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="325" y="5" width="30" height="30" label="0" keyCode="48" alphas="-2" betas="6" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="357" y="5" width="30" height="30" label="-" keyCode="189" alphas="-3" betas="8" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="389" y="5" width="30" height="30" label="=" keyCode="187" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="421" y="5" width="48" height="30" label="del" keyCode="46" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="5" y="37" width="48" height="30" label="tab" keyCode="9" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="5" y="69" width="63" height="30" label="caplock" keyCode="20" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="5" y="101" width="79" height="30" label="shift" keyCode="16" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="5" y="133" width="45" height="30" label="ctl" keyCode="17" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="52" y="133" width="45" height="30" label="opt" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="99" y="133" width="45" height="30" label="cmd" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="146" y="133" width="183" height="30" label="spacebar" keyCode="32" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="406" y="101" width="64" height="30" label="shift" keyCode="16" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="422" y="69" width="48" height="30" label="return" keyCode="13" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="55" y="37" width="30" height="30" label="Q" keyCode="81" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="87" y="37" width="30" height="30" label="W" keyCode="87" alphas="5" betas="-9" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="119" y="37" width="30" height="30" label="E" keyCode="69" alphas="4" betas="-7" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="151" y="37" width="30" height="30" label="R" keyCode="82" alphas="3" betas="-5" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="183" y="37" width="30" height="30" label="T" keyCode="84" alphas="2" betas="-3" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="215" y="37" width="30" height="30" label="Y" keyCode="89" alphas="1" betas="-1" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="247" y="37" width="30" height="30" label="U" keyCode="85" alphas="0" betas="1" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="279" y="37" width="30" height="30" label="I" keyCode="73" alphas="-1" betas="3" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="311" y="37" width="30" height="30" label="O" keyCode="79" alphas="-2" betas="5" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="343" y="37" width="30" height="30" label="P" keyCode="80" alphas="-3" betas="7" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="375" y="37" width="30" height="30" label="[" keyCode="219" alphas="-4" betas="9" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="407" y="37" width="30" height="30" label="]" keyCode="221" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="439" y="37" width="30" height="30" label="\" keyCode="220" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="70" y="69" width="30" height="30" label="A" keyCode="65" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="102" y="69" width="30" height="30" label="S" keyCode="83" alphas="4" betas="-8" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="134" y="69" width="30" height="30" label="D" keyCode="68" alphas="3" betas="-6" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="166" y="69" width="30" height="30" label="F" keyCode="70" alphas="2" betas="-4" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="198" y="69" width="30" height="30" label="G" keyCode="71" alphas="1" betas="-2" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="230" y="69" width="30" height="30" label="H" keyCode="72" alphas="0" betas="0" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="262" y="69" width="30" height="30" label="J" keyCode="74" alphas="-1" betas="2" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="294" y="69" width="30" height="30" label="K" keyCode="75" alphas="-2" betas="4" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="326" y="69" width="30" height="30" label="L" keyCode="76" alphas="-3" betas="6" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="358" y="69" width="30" height="30" label=";" keyCode="186" alphas="-4" betas="8" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="390" y="69" width="30" height="30" label="'" keyCode="222" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="86" y="101" width="30" height="30" label="Z" keyCode="90" alphas="4" betas="-9" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="118" y="101" width="30" height="30" label="X" keyCode="88" alphas="3" betas="-7" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="150" y="101" width="30" height="30" label="C" keyCode="67" alphas="2" betas="-5" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="182" y="101" width="30" height="30" label="V" keyCode="86" alphas="1" betas="-3" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="214" y="101" width="30" height="30" label="B" keyCode="66" alphas="0" betas="-1" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="246" y="101" width="30" height="30" label="N" keyCode="78" alphas="-1" betas="1" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="278" y="101" width="30" height="30" label="M" keyCode="77" alphas="-2" betas="3" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="310" y="101" width="30" height="30" label="," keyCode="188" alphas="-3" betas="5" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="342" y="101" width="30" height="30" label="." keyCode="190" alphas="-4" betas="7" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="374" y="101" width="30" height="30" label="/" keyCode="191" alphas="-5" betas="9" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="331" y="133" width="45" height="30" label="cmd" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="378" y="133" width="45" height="30" label="opt" fontSize="8"/>
        <ckb:KBButton mouseEnabled="false" x="425" y="133" width="45" height="30" label="ctl" keyCode="17" fontSize="8"/>
    </ckb:KBCanvas>
</mx:Application>