home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH18 / Book Code / settingsscreen.cs next >
Encoding:
Text File  |  2006-09-25  |  15.4 KB  |  561 lines

  1. function SetupScreen::setPane(%this, %pane)
  2. {
  3.    OptAudioPane.setVisible(false);
  4.    OptGraphicsPane.setVisible(false);
  5.    OptNetworkPane.setVisible(false);
  6.    OptControlsPane.setVisible(false);
  7.    ("Opt" @ %pane @ "Pane").setVisible(true);
  8.    OptRemapList.fillList();
  9. }
  10.  
  11. function SetupScreen::onWake(%this)
  12. {
  13.    %this.setPane(Graphics);
  14.    %buffer = getDisplayDeviceList();
  15.    %count = getFieldCount( %buffer );
  16.    OptGraphicsDriverMenu.clear();
  17.    OptScreenshotMenu.init();
  18.    OptScreenshotMenu.setValue($pref::Video::screenShotFormat);
  19.    for(%i = 0; %i < %count; %i++)
  20.       OptGraphicsDriverMenu.add(getField(%buffer, %i), %i);
  21.    %selId = OptGraphicsDriverMenu.findText( $pref::Video::displayDevice );
  22.     if ( %selId == -1 )
  23.         %selId = 0; // shouldn't ever happen
  24.     OptGraphicsDriverMenu.setSelected( %selId );
  25.     OptGraphicsDriverMenu.onSelect( %selId, "" );
  26.  
  27.    // Audio
  28.    OptAudioUpdate();
  29.    OptAudioVolumeMaster.setValue( $pref::Audio::masterVolume);
  30.    OptAudioVolumeShell.setValue(  $pref::Audio::channelVolume[$GuiAudioType]);
  31.    OptAudioVolumeSim.setValue(    $pref::Audio::channelVolume[$SimAudioType]);
  32.    OptAudioDriverList.clear();
  33.    OptAudioDriverList.add("OpenAL", 1);
  34.    OptAudioDriverList.add("none", 2);
  35.    %selId = OptAudioDriverList.findText($pref::Audio::driver);
  36.     if ( %selId == -1 )
  37.         %selId = 0; // How did THAT happen?
  38.     OptAudioDriverList.setSelected( %selId );
  39.     OptAudioDriverList.onSelect( %selId, "" );
  40. }
  41.  
  42. function SetupScreen::onSleep(%this)
  43. {
  44.    // write out the control config into the fps/config.cs file
  45.    PlayerKeymap.save( "~/client/config.cs" );//KCF
  46.  
  47. }
  48.  
  49. function OptGraphicsDriverMenu::onSelect( %this, %id, %text )
  50. {
  51.     // Attempt to keep the same res and bpp settings:
  52.     if ( OptGraphicsResolutionMenu.size() > 0 )
  53.         %prevRes = OptGraphicsResolutionMenu.getText();
  54.     else
  55.         %prevRes = getWords( $pref::Video::resolution, 0, 1 );
  56.  
  57.     // Check if this device is full-screen only:
  58.     if ( isDeviceFullScreenOnly( %this.getText() ) )
  59.     {
  60.         OptGraphicsFullscreenToggle.setValue( true );
  61.         OptGraphicsFullscreenToggle.setActive( false );
  62.         OptGraphicsFullscreenToggle.onAction();
  63.     }
  64.     else
  65.         OptGraphicsFullscreenToggle.setActive( true );
  66.  
  67.     if ( OptGraphicsFullscreenToggle.getValue() )
  68.     {
  69.         if ( OptGraphicsBPPMenu.size() > 0 )
  70.             %prevBPP = OptGraphicsBPPMenu.getText();
  71.         else
  72.             %prevBPP = getWord( $pref::Video::resolution, 2 );
  73.     }
  74.  
  75.     // Fill the resolution and bit depth lists:
  76.     OptGraphicsResolutionMenu.init( %this.getText(), OptGraphicsFullscreenToggle.getValue() );
  77.     OptGraphicsBPPMenu.init( %this.getText() );
  78.  
  79.     // Try to select the previous settings:
  80.     %selId = OptGraphicsResolutionMenu.findText( %prevRes );
  81.     if ( %selId == -1 )
  82.         %selId = 0;
  83.     OptGraphicsResolutionMenu.setSelected( %selId );
  84.  
  85.     if ( OptGraphicsFullscreenToggle.getValue() )
  86.     {
  87.         %selId = OptGraphicsBPPMenu.findText( %prevBPP );
  88.         if ( %selId == -1 )
  89.             %selId = 0;
  90.         OptGraphicsBPPMenu.setSelected( %selId );
  91.         OptGraphicsBPPMenu.setText( OptGraphicsBPPMenu.getTextById( %selId ) );
  92.     }
  93.     else
  94.         OptGraphicsBPPMenu.setText( "Default" );
  95.  
  96. }
  97.  
  98. function OptGraphicsResolutionMenu::init( %this, %device, %fullScreen )
  99. {
  100.     %this.clear();
  101.     %resList = getResolutionList( %device );
  102.     %resCount = getFieldCount( %resList );
  103.     %deskRes = getDesktopResolution();
  104.  
  105.    %count = 0;
  106.     for ( %i = 0; %i < %resCount; %i++ )
  107.     {
  108.         %res = getWords( getField( %resList, %i ), 0, 1 );
  109.  
  110.         if ( !%fullScreen )
  111.         {
  112.             if ( firstWord( %res ) >= firstWord( %deskRes ) )
  113.                 continue;
  114.             if ( getWord( %res, 1 ) >= getWord( %deskRes, 1 ) )
  115.                 continue;
  116.         }
  117.  
  118.         // Only add to list if it isn't there already:
  119.         if ( %this.findText( %res ) == -1 )
  120.       {
  121.             %this.add( %res, %count );
  122.          %count++;
  123.       }
  124.     }
  125. }
  126.  
  127. function OptGraphicsFullscreenToggle::onAction(%this)
  128. {
  129.    Parent::onAction();
  130.    %prevRes = OptGraphicsResolutionMenu.getText();
  131.  
  132.    // Update the resolution menu with the new options
  133.    OptGraphicsResolutionMenu.init( OptGraphicsDriverMenu.getText(), %this.getValue() );
  134.  
  135.    // Set it back to the previous resolution if the new mode supports it.
  136.    %selId = OptGraphicsResolutionMenu.findText( %prevRes );
  137.    if ( %selId == -1 )
  138.        %selId = 0;
  139.      OptGraphicsResolutionMenu.setSelected( %selId );
  140. }
  141.  
  142.  
  143. function OptGraphicsBPPMenu::init( %this, %device )
  144. {
  145.     %this.clear();
  146.  
  147.     if ( %device $= "Voodoo2" )
  148.         %this.add( "16", 0 );
  149.     else
  150.     {
  151.         %resList = getResolutionList( %device );
  152.         %resCount = getFieldCount( %resList );
  153.       %count = 0;
  154.         for ( %i = 0; %i < %resCount; %i++ )
  155.         {
  156.             %bpp = getWord( getField( %resList, %i ), 2 );
  157.  
  158.             // Only add to list if it isn't there already:
  159.             if ( %this.findText( %bpp ) == -1 )
  160.          {
  161.                 %this.add( %bpp, %count );
  162.             %count++;
  163.          }
  164.         }
  165.     }
  166. }
  167.  
  168. function OptScreenshotMenu::init( %this )
  169. {
  170.    if( %this.findText("PNG") == -1 )
  171.       %this.add("PNG", 0);
  172.    if( %this.findText("JPEG") == - 1 )
  173.       %this.add("JPEG", 1);
  174. }
  175.  
  176. function SetupScreen::applyGraphics( %this )
  177. {
  178.     %newDriver = OptGraphicsDriverMenu.getText();
  179.     %newRes = OptGraphicsResolutionMenu.getText();
  180.     %newBpp = OptGraphicsBPPMenu.getText();
  181.     %newFullScreen = OptGraphicsFullscreenToggle.getValue();
  182.     $pref::Video::screenShotFormat = OptScreenshotMenu.getText();
  183.  
  184.     if ( %newDriver !$= $pref::Video::displayDevice )
  185.     {
  186.         setDisplayDevice( %newDriver, firstWord( %newRes ), getWord( %newRes, 1 ), %newBpp, %newFullScreen );
  187.         //SetupScreen::deviceDependent( %this );
  188.     }
  189.     else
  190.         setScreenMode( firstWord( %newRes ), getWord( %newRes, 1 ), %newBpp, %newFullScreen );
  191. }
  192.  
  193.  
  194.  
  195. $RemapCount = 0;
  196. $RemapName[$RemapCount] = "Forward";
  197. $RemapCmd[$RemapCount] = "moveforward";
  198. $RemapCount++;
  199. $RemapName[$RemapCount] = "Backward";
  200. $RemapCmd[$RemapCount] = "movebackward";
  201. $RemapCount++;
  202. $RemapName[$RemapCount] = "Strafe Left";
  203. $RemapCmd[$RemapCount] = "moveleft";
  204. $RemapCount++;
  205. $RemapName[$RemapCount] = "Strafe Right";
  206. $RemapCmd[$RemapCount] = "moveright";
  207. $RemapCount++;
  208. $RemapName[$RemapCount] = "Turn Left";
  209. $RemapCmd[$RemapCount] = "turnLeft";
  210. $RemapCount++;
  211. $RemapName[$RemapCount] = "Turn Right";
  212. $RemapCmd[$RemapCount] = "turnRight";
  213. $RemapCount++;
  214. $RemapName[$RemapCount] = "Look Up";
  215. $RemapCmd[$RemapCount] = "panUp";
  216. $RemapCount++;
  217. $RemapName[$RemapCount] = "Look Down";
  218. $RemapCmd[$RemapCount] = "panDown";
  219. $RemapCount++;
  220. $RemapName[$RemapCount] = "Jump";
  221. $RemapCmd[$RemapCount] = "jump";
  222. $RemapCount++;
  223. $RemapName[$RemapCount] = "Fire Weapon";
  224. $RemapCmd[$RemapCount] = "mouseFire";
  225. $RemapCount++;
  226. $RemapName[$RemapCount] = "Adjust Zoom";
  227. $RemapCmd[$RemapCount] = "setZoomFov";
  228. $RemapCount++;
  229. $RemapName[$RemapCount] = "Toggle Zoom";
  230. $RemapCmd[$RemapCount] = "toggleZoom";
  231. $RemapCount++;
  232. $RemapName[$RemapCount] = "Free Look";
  233. $RemapCmd[$RemapCount] = "toggleFreeLook";
  234. $RemapCount++;
  235. $RemapName[$RemapCount] = "Switch 1st/3rd";
  236. $RemapCmd[$RemapCount] = "toggleFirstPerson";
  237. $RemapCount++;
  238. $RemapName[$RemapCount] = "Toggle Message Hud";
  239. $RemapCmd[$RemapCount] = "toggleMessageHud";
  240. $RemapCount++;
  241. $RemapName[$RemapCount] = "Message Hud PageUp";
  242. $RemapCmd[$RemapCount] = "pageMessageHudUp";
  243. $RemapCount++;
  244. $RemapName[$RemapCount] = "Message Hud PageDown";
  245. $RemapCmd[$RemapCount] = "pageMessageHudDown";
  246. $RemapCount++;
  247. $RemapName[$RemapCount] = "Resize Message Hud";
  248. $RemapCmd[$RemapCount] = "resizeMessageHud";
  249. $RemapCount++;
  250. $RemapName[$RemapCount] = "Toggle Camera";
  251. $RemapCmd[$RemapCount] = "toggleCamera";
  252. $RemapCount++;
  253. $RemapName[$RemapCount] = "Drop Camera at Player";
  254. $RemapCmd[$RemapCount] = "dropCameraAtPlayer";
  255. $RemapCount++;
  256. $RemapName[$RemapCount] = "Drop Player at Camera";
  257. $RemapCmd[$RemapCount] = "dropPlayerAtCamera";
  258. $RemapCount++;
  259.  
  260.  
  261. function restoreDefaultMappings()
  262. {
  263.    PlayerKeymap.delete();//KCF
  264.    exec( "~/client/scripts/default.bind.cs" );
  265.    OptRemapList.fillList();
  266. }
  267.  
  268. function getMapDisplayName( %device, %action )
  269. {
  270.     if ( %device $= "keyboard" )
  271.         return( %action );
  272.     else if ( strstr( %device, "mouse" ) != -1 )
  273.     {
  274.         // Substitute "mouse" for "button" in the action string:
  275.         %pos = strstr( %action, "button" );
  276.         if ( %pos != -1 )
  277.         {
  278.             %mods = getSubStr( %action, 0, %pos );
  279.             %object = getSubStr( %action, %pos, 1000 );
  280.             %instance = getSubStr( %object, strlen( "button" ), 1000 );
  281.             return( %mods @ "mouse" @ ( %instance + 1 ) );
  282.         }
  283.         else
  284.             error( "Mouse input object other than button passed to getDisplayMapName!" );
  285.     }
  286.     else if ( strstr( %device, "joystick" ) != -1 )
  287.     {
  288.         // Substitute "joystick" for "button" in the action string:
  289.         %pos = strstr( %action, "button" );
  290.         if ( %pos != -1 )
  291.         {
  292.             %mods = getSubStr( %action, 0, %pos );
  293.             %object = getSubStr( %action, %pos, 1000 );
  294.             %instance = getSubStr( %object, strlen( "button" ), 1000 );
  295.             return( %mods @ "joystick" @ ( %instance + 1 ) );
  296.         }
  297.         else
  298.        {
  299.           %pos = strstr( %action, "pov" );
  300.          if ( %pos != -1 )
  301.          {
  302.             %wordCount = getWordCount( %action );
  303.             %mods = %wordCount > 1 ? getWords( %action, 0, %wordCount - 2 ) @ " " : "";
  304.             %object = getWord( %action, %wordCount - 1 );
  305.             switch$ ( %object )
  306.             {
  307.                case "upov":   %object = "POV1 up";
  308.                case "dpov":   %object = "POV1 down";
  309.                case "lpov":   %object = "POV1 left";
  310.                case "rpov":   %object = "POV1 right";
  311.                case "upov2":  %object = "POV2 up";
  312.                case "dpov2":  %object = "POV2 down";
  313.                case "lpov2":  %object = "POV2 left";
  314.                case "rpov2":  %object = "POV2 right";
  315.                default:       %object = "??";
  316.             }
  317.             return( %mods @ %object );
  318.          }
  319.          else
  320.             error( "Unsupported Joystick input object passed to getDisplayMapName!" );
  321.       }
  322.     }
  323.  
  324.     return( "??" );
  325. }
  326.  
  327. function buildFullMapString( %index )
  328. {
  329.    %name       = $RemapName[%index];
  330.    %cmd        = $RemapCmd[%index];
  331.  
  332.     %temp = PlayerKeymap.getBinding( %cmd );//KCF
  333.    %device = getField( %temp, 0 );
  334.    %object = getField( %temp, 1 );
  335.    if ( %device !$= "" && %object !$= "" )
  336.        %mapString = getMapDisplayName( %device, %object );
  337.    else
  338.       %mapString = "";
  339.  
  340.     return( %name TAB %mapString );
  341. }
  342.  
  343. function OptRemapList::fillList( %this )
  344. {
  345.     %this.clear();
  346.    for ( %i = 0; %i < $RemapCount; %i++ )
  347.       %this.addRow( %i, buildFullMapString( %i ) );
  348. }
  349.  
  350. //------------------------------------------------------------------------------
  351. function OptRemapList::doRemap( %this )
  352. {
  353.     %selId = %this.getSelectedId();
  354.    %name = $RemapName[%selId];
  355.  
  356.     OptRemapText.setValue( "REMAP \"" @ %name @ "\"" );
  357.     OptRemapInputCtrl.index = %selId;
  358.     Canvas.pushDialog( RemapDlg );
  359. }
  360.  
  361. //------------------------------------------------------------------------------
  362. function redoMapping( %device, %action, %cmd, %oldIndex, %newIndex )
  363. {
  364.     //%actionMap.bind( %device, %action, $RemapCmd[%newIndex] );
  365.     PlayerKeymap.bind( %device, %action, %cmd );//KCF
  366.     OptRemapList.setRowById( %oldIndex, buildFullMapString( %oldIndex ) );
  367.     OptRemapList.setRowById( %newIndex, buildFullMapString( %newIndex ) );
  368. }
  369.  
  370. //------------------------------------------------------------------------------
  371. function findRemapCmdIndex( %command )
  372. {
  373.     for ( %i = 0; %i < $RemapCount; %i++ )
  374.     {
  375.         if ( %command $= $RemapCmd[%i] )
  376.             return( %i );
  377.     }
  378.     return( -1 );
  379. }
  380.  
  381. function OptRemapInputCtrl::onInputEvent( %this, %device, %action )
  382. {
  383.     //error( "** onInputEvent called - device = " @ %device @ ", action = " @ %action @ " **" );
  384.     Canvas.popDialog( RemapDlg );
  385.  
  386.     // Test for the reserved keystrokes:
  387.     if ( %device $= "keyboard" )
  388.     {
  389.       // Cancel...
  390.       if ( %action $= "escape" )
  391.       {
  392.          // Do nothing...
  393.            return;
  394.       }
  395.     }
  396.  
  397.    %cmd  = $RemapCmd[%this.index];
  398.    %name = $RemapName[%this.index];
  399.  
  400.     // First check to see if the given action is already mapped:
  401.     %prevMap = PlayerKeymap.getCommand( %device, %action );//KCF
  402.    if ( %prevMap !$= %cmd )
  403.    {
  404.        if ( %prevMap $= "" )
  405.        {
  406.          PlayerKeymap.bind( %device, %action, %cmd );//KCF
  407.            OptRemapList.setRowById( %this.index, buildFullMapString( %this.index ) );
  408.        }
  409.        else
  410.        {
  411.          %mapName = getMapDisplayName( %device, %action );
  412.            %prevMapIndex = findRemapCmdIndex( %prevMap );
  413.            if ( %prevMapIndex == -1 )
  414.                MessageBoxOK( "REMAP FAILED", "\"" @ %mapName @ "\" is already bound to a non-remappable command!" );
  415.            else
  416.          {
  417.             %prevCmdName = $RemapName[%prevMapIndex];
  418.                MessageBoxYesNo( "WARNING",
  419.                    "\"" @ %mapName @ "\" is already bound to \""
  420.                        @ %prevCmdName @ "\"!\nDo you want to undo this mapping?",
  421.                    "redoMapping(" @ %device @ ", \"" @ %action @ "\", \"" @ %cmd @ "\", " @ %prevMapIndex @ ", " @ %this.index @ ");", "" );
  422.          }
  423.            return;
  424.        }
  425.    }
  426. }
  427.  
  428. // Audio
  429. function OptAudioUpdate()
  430. {
  431.    // set the driver text
  432.    %text =   "Vendor: " @ alGetString("AL_VENDOR") @
  433.            "\nVersion: " @ alGetString("AL_VERSION") @
  434.            "\nRenderer: " @ alGetString("AL_RENDERER") @
  435.            "\nExtensions: " @ alGetString("AL_EXTENSIONS");
  436.    OptAudioInfo.setText(%text);
  437.  
  438. }
  439.  
  440.  
  441. // Channel 0 is unused in-game, but is used here to test master volume.
  442.  
  443. new AudioDescription(AudioChannel0)
  444. {
  445.    volume   = 1.0;
  446.    isLooping= false;
  447.    is3D     = false;
  448.    type     = 0;
  449. };
  450.  
  451. new AudioDescription(AudioChannel1)
  452. {
  453.    volume   = 1.0;
  454.    isLooping= false;
  455.    is3D     = false;
  456.    type     = 1;
  457. };
  458.  
  459. new AudioDescription(AudioChannel2)
  460. {
  461.    volume   = 1.0;
  462.    isLooping= false;
  463.    is3D     = false;
  464.    type     = 2;
  465. };
  466.  
  467. new AudioDescription(AudioChannel3)
  468. {
  469.    volume   = 1.0;
  470.    isLooping= false;
  471.    is3D     = false;
  472.    type     = 3;
  473. };
  474.  
  475. new AudioDescription(AudioChannel4)
  476. {
  477.    volume   = 1.0;
  478.    isLooping= false;
  479.    is3D     = false;
  480.    type     = 4;
  481. };
  482.  
  483. new AudioDescription(AudioChannel5)
  484. {
  485.    volume   = 1.0;
  486.    isLooping= false;
  487.    is3D     = false;
  488.    type     = 5;
  489. };
  490.  
  491. new AudioDescription(AudioChannel6)
  492. {
  493.    volume   = 1.0;
  494.    isLooping= false;
  495.    is3D     = false;
  496.    type     = 6;
  497. };
  498.  
  499. new AudioDescription(AudioChannel7)
  500. {
  501.    volume   = 1.0;
  502.    isLooping= false;
  503.    is3D     = false;
  504.    type     = 7;
  505. };
  506.  
  507. new AudioDescription(AudioChannel8)
  508. {
  509.    volume   = 1.0;
  510.    isLooping= false;
  511.    is3D     = false;
  512.    type     = 8;
  513. };
  514.  
  515. $AudioTestHandle = 0;
  516.  
  517. function OptAudioUpdateMasterVolume(%volume)
  518. {
  519.    if (%volume == $pref::Audio::masterVolume)
  520.       return;
  521.    alxListenerf(AL_GAIN_LINEAR, %volume);
  522.    $pref::Audio::masterVolume = %volume;
  523.    if (!alxIsPlaying($AudioTestHandle))
  524.    {
  525.       $AudioTestHandle = alxCreateSource("AudioChannel0", expandFilename("~/data/sound/test.wav"));
  526.       alxPlay($AudioTestHandle);
  527.    }
  528. }
  529.  
  530.  
  531. function OptAudioUpdateChannelVolume(%channel, %volume)
  532. {
  533.    if (%channel < 1 || %channel > 8)
  534.       return;
  535.  
  536.    if (%volume == $pref::Audio::channelVolume[%channel])
  537.       return;
  538.  
  539.    alxSetChannelVolume(%channel, %volume);
  540.    $pref::Audio::channelVolume[%channel] = %volume;
  541.    if (!alxIsPlaying($AudioTestHandle))
  542.    {
  543.       $AudioTestHandle = alxCreateSource("AudioChannel"@%channel, expandFilename("~/data/sound/test.wav"));
  544.       alxPlay($AudioTestHandle);
  545.    }
  546. }
  547.  
  548.  
  549. function OptAudioDriverList::onSelect( %this, %id, %text )
  550. {
  551.    if (%text $= "")
  552.       return;
  553.  
  554.    if ($pref::Audio::driver $= %text)
  555.       return;
  556.  
  557.    $pref::Audio::driver = %text;
  558.    OpenALInit();
  559. }
  560.  
  561.