home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH10 / creator / editor / EditorGui.cs < prev    next >
Encoding:
Text File  |  2005-11-23  |  89.0 KB  |  2,907 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. function EditorGui::getPrefs()
  7. {
  8.    // same defaults as WorldEditor ctor
  9.    EWorldEditor.axisGizmoActive = getPrefSetting($pref::WorldEditor::axisGizmoActive, true);
  10.    EWorldEditor.axisGizmoMaxScreenLen = getPrefSetting($pref::WorldEditor::axisGizmoMaxScreenLen, 200);
  11.    EWorldEditor.boundingBoxCollision = getPrefSetting($pref::WorldEditor::boundingBoxCollision, true);
  12.    EWorldEditor.defaultHandle = getPrefSetting($pref::WorldEditor::defaultHandle, "gui/Editor_DefaultHandle.png");
  13.    EWorldEditor.dragRectColor = getPrefSetting($pref::WorldEditor::dragRectColor, "255 255 0");
  14.    EWorldEditor.dropType = getPrefSetting($pref::WorldEditor::dropType, "screenCenter");
  15.    EWorldEditor.faceSelectColor = getPrefSetting($pref::WorldEditor::faceSelectColor, "0 0 100 100");
  16.    EWorldEditor.gridColor = getPrefSetting($pref::WorldEditor::gridColor, "255 255 255 20");
  17.    EWorldEditor.gridSize = getPrefSetting($pref::WorldEditor::gridSize, "10 10 10");
  18.    EWorldEditor.lockedHandle = getPrefSetting($pref::WorldEditor::lockedHandle, "gui/Editor_LockedHandle.png");
  19.    EWorldEditor.maxScaleFactor = getPrefSetting($pref::WorldEditor::maxScaleFactor, 4000);
  20.    EWorldEditor.minScaleFactor = getPrefSetting($pref::WorldEditor::minScaleFactor, 0.1);
  21.    EWorldEditor.mouseMoveScale = getPrefSetting($pref::WorldEditor::mouseMoveScale, 0.2);
  22.    EWorldEditor.mouseRotateScale = getPrefSetting($pref::WorldEditor::mouseRotateScale, 0.01);
  23.    EWorldEditor.mouseScaleScale = getPrefSetting($pref::WorldEditor::mouseScaleScale, 0.01);
  24.    EWorldEditor.objectsUseBoxCenter = getPrefSetting($pref::WorldEditor::objectsUseBoxCenter, true);
  25.    EWorldEditor.objectTextColor = getPrefSetting($pref::WorldEditor::objectTextColor, "255 255 255");
  26.    EWorldEditor.objMouseOverColor = getPrefSetting($pref::WorldEditor::objMouseOverColor, "0 255 0");
  27.    EWorldEditor.objMouseOverSelectColor = getPrefSetting($pref::WorldEditor::objMouseOverSelectColor, "0 0 255");
  28.    EWorldEditor.objSelectColor = getPrefSetting($pref::WorldEditor::objSelectColor, "255 0 0");
  29.    EWorldEditor.planarMovement = getPrefSetting($pref::WorldEditor::planarMovement, true);
  30.    EWorldEditor.planeDim = getPrefSetting($pref::WorldEditor::planeDim, 500);
  31.    EWorldEditor.popupBackgroundColor = getPrefSetting($pref::WorldEditor::popupBackgroundColor, "100 100 100");
  32.    EWorldEditor.popupTextColor = getPrefSetting($pref::WorldEditor::popupTextColor, "255 255 0");
  33.    EWorldEditor.projectDistance = getPrefSetting($pref::WorldEditor::projectDistance, 2000);
  34.    EWorldEditor.renderObjHandle = getPrefSetting($pref::WorldEditor::renderObjHandle, true);
  35.    EWorldEditor.renderObjText = getPrefSetting($pref::WorldEditor::renderObjText, true);
  36.    EWorldEditor.renderPlane = getPrefSetting($pref::WorldEditor::renderPlane, true);
  37.    EWorldEditor.renderPlaneHashes = getPrefSetting($pref::WorldEditor::renderPlaneHashes, true);
  38.    EWorldEditor.renderPopupBackground = getPrefSetting($pref::WorldEditor::renderPopupBackground, true);
  39.    EWorldEditor.renderSelectionBox = getPrefSetting($pref::WorldEditor::renderSelectionBox, true);
  40.    EWorldEditor.rotationSnap = getPrefSetting($pref::WorldEditor::rotationSnap, "15");
  41.    EWorldEditor.selectHandle = getPrefSetting($pref::WorldEditor::selectHandle, "gui/Editor_SelectHandle.png");
  42.    EWorldEditor.selectionBoxColor = getPrefSetting($pref::WorldEditor::selectionBoxColor, "255 255 0");
  43.    EWorldEditor.showMousePopupInfo = getPrefSetting($pref::WorldEditor::showMousePopupInfo, true);
  44.    EWorldEditor.snapRotations = getPrefSetting($pref::WorldEditor::snapRotations, false);
  45.    EWorldEditor.snapToGrid = getPrefSetting($pref::WorldEditor::snapToGrid, false);
  46.    EWorldEditor.undoLimit = getPrefSetting($pref::WorldEditor::undoLimit, 40);
  47.  
  48.    ETerrainEditor.softSelecting = 1;
  49.    ETerrainEditor.currentAction = "raiseHeight";
  50.    ETerrainEditor.currentMode = "select";
  51. }
  52.  
  53. function EditorGui::setPrefs()
  54. {
  55.    $pref::WorldEditor::axisGizmoActive = EWorldEditor.axisGizmoActive;
  56.    $pref::WorldEditor::axisGizmoMaxScreenLen = EWorldEditor.axisGizmoMaxScreenLen;
  57.    $pref::WorldEditor::boundingBoxCollision = EWorldEditor.boundingBoxCollision;
  58.    $pref::WorldEditor::defaultHandle = EWorldEditor.defaultHandle;
  59.    $pref::WorldEditor::dragRectColor = EWorldEditor.dragRectColor;
  60.    $Pref::WorldEditor::dropType = EWorldEditor.dropType;
  61.    $pref::WorldEditor::gridColor = EWorldEditor.GridColor;
  62.    $pref::WorldEditor::gridSize = EWorldEditor.GridSize;
  63.    $pref::WorldEditor::lockedHandle = EWorldEditor.lockedHandle;
  64.    $pref::WorldEditor::maxScaleFactor = EWorldEditor.maxScaleFactor;
  65.    $pref::WorldEditor::minScaleFactor = EWorldEditor.minScaleFactor;
  66.    $pref::WorldEditor::mouseMoveScale = EWorldEditor.mouseMoveScale;
  67.    $pref::WorldEditor::mouseRotateScale = EWorldEditor.mouseRotateScale;
  68.    $pref::WorldEditor::mouseScaleScale = EWorldEditor.mouseScaleScale;
  69.    $pref::WorldEditor::objectsUseBoxCenter = EWorldEditor.objectsUseBoxCenter;
  70.    $pref::WorldEditor::objectTextColor = EWorldEditor.ObjectTextColor;
  71.    $pref::WorldEditor::objMouseOverColor = EWorldEditor.objMouseOverColor;
  72.    $pref::WorldEditor::objMouseOverSelectColor = EWorldEditor.objMouseOverSelectColor;
  73.    $pref::WorldEditor::objSelectColor = EWorldEditor.objSelectColor;
  74.    $pref::WorldEditor::planarMovement = EWorldEditor.planarMovement;
  75.    $pref::WorldEditor::planeDim = EWorldEditor.planeDim;
  76.    $pref::WorldEditor::popupBackgroundColor = EWorldEditor.PopupBackgroundColor;
  77.    $pref::WorldEditor::popupTextColor = EWorldEditor.PopupTextColor;
  78.    $pref::WorldEditor::projectDistance = EWorldEditor.projectDistance;
  79.    $pref::WorldEditor::raceSelectColor = EWorldEditor.faceSelectColor;
  80.    $pref::WorldEditor::renderObjHandle = EWorldEditor.renderObjHandle;
  81.    $pref::WorldEditor::renderObjText = EWorldEditor.renderObjText;
  82.    $pref::WorldEditor::renderPlane = EWorldEditor.renderPlane;
  83.    $pref::WorldEditor::renderPlaneHashes = EWorldEditor.renderPlaneHashes;
  84.    $pref::WorldEditor::renderPopupBackground = EWorldEditor.renderPopupBackground;
  85.    $pref::WorldEditor::renderSelectionBox = EWorldEditor.renderSelectionBox;
  86.    $pref::WorldEditor::rotationSnap = EWorldEditor.rotationSnap;
  87.    $pref::WorldEditor::selectHandle = EWorldEditor.selectHandle;
  88.    $pref::WorldEditor::selectionBoxColor = EWorldEditor.selectionBoxColor;
  89.    $pref::WorldEditor::showMousePopupInfo = EWorldEditor.showMousePopupInfo;
  90.    $pref::WorldEditor::snapRotations = EWorldEditor.snapRotations;
  91.    $pref::WorldEditor::snapToGrid = EWorldEditor.snapToGrid;
  92.    $pref::WorldEditor::undoLimit = EWorldEditor.undoLimit;
  93.  
  94. }
  95.  
  96. function EditorGui::onSleep(%this)
  97. {
  98.    %this.setPrefs();
  99. }
  100.  
  101. function EditorGui::init(%this)
  102. {
  103.    %this.getPrefs();
  104.  
  105.    if(!isObject("terraformer"))
  106.       new Terraformer("terraformer");
  107.       
  108.    $SelectedOperation = -1;
  109.    $NextOperationId   = 1;
  110.    $HeightfieldDirtyRow = -1;
  111.  
  112.    EditorMenuBar.clearMenus();
  113.    EditorMenuBar.addMenu("File", 0);
  114.    EditorMenuBar.addMenuItem("File", "New Mission...", 1);
  115.    EditorMenuBar.addMenuItem("File", "Open Mission...", 2, "Ctrl O");
  116.    EditorMenuBar.addMenuItem("File", "Save Mission...", 3, "Ctrl S");
  117.    EditorMenuBar.addMenuItem("File", "Save Mission As...", 4);
  118.    EditorMenuBar.addMenuItem("File", "-", 0);
  119.    EditorMenuBar.addMenuItem("File", "Import Terraform Data...", 6);
  120.    EditorMenuBar.addMenuItem("File", "Import Texture Data...", 5);
  121.    EditorMenuBar.addMenuItem("File", "-", 0);
  122.    EditorMenuBar.addMenuItem("File", "Export Terraform Bitmap...", 5);
  123.    EditorMenuBar.addMenuItem("File", "-", 0);
  124.    EditorMenuBar.addMenuItem("File", "Toggle Map Editor...", 7, "F11");
  125.    EditorMenuBar.addMenuItem("File", "Quit", 8);
  126.  
  127.    EditorMenuBar.addMenu("Edit", 1);
  128.    EditorMenuBar.addMenuItem("Edit", "Undo", 1, "Ctrl Z");
  129.    EditorMenuBar.setMenuItemBitmap("Edit", "Undo", 1);
  130.    EditorMenuBar.addMenuItem("Edit", "Redo", 2, "Ctrl R");
  131.    EditorMenuBar.setMenuItemBitmap("Edit", "Redo", 2);
  132.    EditorMenuBar.addMenuItem("Edit", "-", 0);
  133.    EditorMenuBar.addMenuItem("Edit", "Cut", 3, "Ctrl X");
  134.    EditorMenuBar.setMenuItemBitmap("Edit", "Cut", 3);
  135.    EditorMenuBar.addMenuItem("Edit", "Copy", 4, "Ctrl C");
  136.    EditorMenuBar.setMenuItemBitmap("Edit", "Copy", 4);
  137.    EditorMenuBar.addMenuItem("Edit", "Paste", 5, "Ctrl V");
  138.    EditorMenuBar.setMenuItemBitmap("Edit", "Paste", 5);
  139.    EditorMenuBar.addMenuItem("Edit", "-", 0);
  140.    EditorMenuBar.addMenuItem("Edit", "Select All", 6, "Ctrl A");
  141.    EditorMenuBar.addMenuItem("Edit", "Select None", 7, "Ctrl N");
  142.    EditorMenuBar.addMenuItem("Edit", "-", 0);
  143.    EditorMenuBar.addMenuItem("Edit", "Relight Scene", 14, "Alt L");
  144.    EditorMenuBar.addMenuItem("Edit", "-", 0);
  145.    EditorMenuBar.addMenuItem("Edit", "World Editor Settings...", 12);
  146.    EditorMenuBar.addMenuItem("Edit", "Terrain Editor Settings...", 13);
  147.  
  148.    EditorMenuBar.addMenu("Camera", 7);
  149.    EditorMenuBar.addMenuItem("Camera", "Drop Camera at Player", 1, "Alt Q");
  150.    EditorMenuBar.addMenuItem("Camera", "Drop Player at Camera", 2, "Alt W");
  151.    EditorMenuBar.addMenuItem("Camera", "Toggle Camera", 10, "Alt C");
  152.    EditorMenuBar.addMenuItem("Camera", "-", 0);
  153.    EditorMenuBar.addMenuItem("Camera", "Slowest", 3, "Shift 1", 1);
  154.    EditorMenuBar.addMenuItem("Camera", "Very Slow", 4, "Shift 2", 1);
  155.    EditorMenuBar.addMenuItem("Camera", "Slow", 5, "Shift 3", 1);
  156.    EditorMenuBar.addMenuItem("Camera", "Medium Pace", 6, "Shift 4", 1);
  157.    EditorMenuBar.addMenuItem("Camera", "Fast", 7, "Shift 5", 1);
  158.    EditorMenuBar.addMenuItem("Camera", "Very Fast", 8, "Shift 6", 1);
  159.    EditorMenuBar.addMenuItem("Camera", "Fastest", 9, "Shift 7", 1);
  160.  
  161.    EditorMenuBar.addMenu("World", 6);
  162.    EditorMenuBar.addMenuItem("World", "Lock Selection", 10, "Ctrl L");
  163.    EditorMenuBar.addMenuItem("World", "Unlock Selection", 11, "Ctrl Shift L");
  164.    EditorMenuBar.addMenuItem("World", "-", 0);
  165.    EditorMenuBar.addMenuItem("World", "Hide Selection", 12, "Ctrl H");
  166.    EditorMenuBar.addMenuItem("World", "Show Selection", 13, "Ctrl Shift H");
  167.    EditorMenuBar.addMenuItem("World", "-", 0);
  168.    EditorMenuBar.addMenuItem("World", "Delete Selection", 17, "Delete");
  169.    EditorMenuBar.addMenuItem("World", "Camera To Selection", 14);
  170.    EditorMenuBar.addMenuItem("World", "Reset Transforms", 15);
  171.    EditorMenuBar.addMenuItem("World", "Drop Selection", 16, "Ctrl D");
  172.    EditorMenuBar.addMenuItem("World", "Add Selection to Instant Group", 17);
  173.    EditorMenuBar.addMenuItem("World", "-", 0);
  174.    EditorMenuBar.addMenuItem("World", "Drop at Origin", 0, "", 1);
  175.    EditorMenuBar.addMenuItem("World", "Drop at Camera", 1, "", 1);
  176.    EditorMenuBar.addMenuItem("World", "Drop at Camera w/Rot", 2, "", 1);
  177.    EditorMenuBar.addMenuItem("World", "Drop below Camera", 3, "", 1);
  178.    EditorMenuBar.addMenuItem("World", "Drop at Screen Center", 4, "", 1);
  179.    EditorMenuBar.addMenuItem("World", "Drop at Centroid", 5, "", 1);
  180.    EditorMenuBar.addMenuItem("World", "Drop to Ground", 6, "", 1);
  181.  
  182.    EditorMenuBar.addMenu("Action", 3);
  183.    EditorMenuBar.addMenuItem("Action", "Select", 1, "", 1);
  184.    EditorMenuBar.addMenuItem("Action", "Adjust Selection", 2, "", 1);
  185.    EditorMenuBar.addMenuItem("Action", "-", 0);
  186.    EditorMenuBar.addMenuItem("Action", "Add Dirt", 6, "", 1);
  187.    EditorMenuBar.addMenuItem("Action", "Excavate", 6, "", 1);
  188.    EditorMenuBar.addMenuItem("Action", "Adjust Height", 6, "", 1);
  189.    EditorMenuBar.addMenuItem("Action", "Flatten", 4, "", 1);
  190.    EditorMenuBar.addMenuItem("Action", "Smooth", 5, "", 1);
  191.    EditorMenuBar.addMenuItem("Action", "Set Height", 7, "", 1);
  192.    EditorMenuBar.addMenuItem("Action", "-", 0);
  193.    EditorMenuBar.addMenuItem("Action", "Set Empty", 8, "", 1);
  194.    EditorMenuBar.addMenuItem("Action", "Clear Empty", 8, "", 1);
  195.    EditorMenuBar.addMenuItem("Action", "-", 0);
  196.    EditorMenuBar.addMenuItem("Action", "Paint Material", 9, "", 1);
  197.  
  198.    EditorMenuBar.addMenu("Brush", 4);
  199.    EditorMenuBar.addMenuItem("Brush", "Box Brush", 91, "", 1);
  200.    EditorMenuBar.addMenuItem("Brush", "Circle Brush", 92, "", 1);
  201.    EditorMenuBar.addMenuItem("Brush", "-", 0);
  202.    EditorMenuBar.addMenuItem("Brush", "Soft Brush", 93, "", 2);
  203.    EditorMenuBar.addMenuItem("Brush", "Hard Brush", 94, "", 2);
  204.    EditorMenuBar.addMenuItem("Brush", "-", 0);
  205.    EditorMenuBar.addMenuItem("Brush", "Size 1 x 1", 1, "Alt 1", 3);
  206.    EditorMenuBar.addMenuItem("Brush", "Size 3 x 3", 3, "Alt 2", 3);
  207.    EditorMenuBar.addMenuItem("Brush", "Size 5 x 5", 5, "Alt 3", 3);
  208.    EditorMenuBar.addMenuItem("Brush", "Size 9 x 9", 9, "Alt 4", 3);
  209.    EditorMenuBar.addMenuItem("Brush", "Size 15 x 15", 15, "Alt 5", 3);
  210.    EditorMenuBar.addMenuItem("Brush", "Size 25 x 25", 25, "Alt 6", 3);
  211.  
  212.    EditorMenuBar.addMenu("Window", 2);
  213.    EditorMenuBar.addMenuItem("Window", "World Editor", 2, "F2", 1);
  214.    EditorMenuBar.addMenuItem("Window", "World Editor Inspector", 3, "F3", 1);
  215.    EditorMenuBar.addMenuItem("Window", "World Editor Creator", 4, "F4", 1);
  216.    EditorMenuBar.addMenuItem("Window", "Mission Area Editor", 5, "F5", 1);
  217.    EditorMenuBar.addMenuItem("Window", "-", 0);
  218.    EditorMenuBar.addMenuItem("Window", "Terrain Editor", 6, "F6", 1);
  219.    EditorMenuBar.addMenuItem("Window", "Terrain Terraform Editor", 7, "F7", 1);
  220.    EditorMenuBar.addMenuItem("Window", "Terrain Texture Editor", 8, "F8", 1);
  221.    EditorMenuBar.addMenuItem("Window", "Terrain Texture Painter", 9, "", 1);
  222.  
  223.    EditorMenuBar.onActionMenuItemSelect(0, "Adjust Height");
  224.    EditorMenuBar.onBrushMenuItemSelect(0, "Circle Brush");
  225.    EditorMenuBar.onBrushMenuItemSelect(0, "Soft Brush");
  226.    EditorMenuBar.onBrushMenuItemSelect(9, "Size 9 x 9");
  227.    EditorMenuBar.onCameraMenuItemSelect(6, "Medium Pace");
  228.    EditorMenuBar.onWorldMenuItemSelect(0, "Drop at Screen Center");
  229.  
  230.    EWorldEditor.init();
  231.    ETerrainEditor.attachTerrain();
  232.    TerraformerInit();
  233.    TextureInit();
  234.  
  235.    //
  236.    //Creator.init();
  237.    EditorTree.init();
  238.    ObjectBuilderGui.init();
  239.  
  240.    EWorldEditor.isDirty = false;
  241.    ETerrainEditor.isDirty = false;
  242.    ETerrainEditor.isMissionDirty = false;
  243.    EditorGui.saveAs = false;
  244. }
  245.  
  246. function EditorQuitMission()
  247. {  
  248.    if(ETerrainEditor.isMissionDirty || ETerrainEditor.isDirty || EWorldEditor.isDirty)
  249.    {
  250.       MessageBoxYesNo("Mission Modified", "Would you like to save your changes before quiting?", "EditorDoQuitMission(true);", "EditorDoQuitMission(false);");
  251.    }
  252.    else
  253.       EditorDoQuitMission(false);
  254. }
  255.  
  256. function EditorDoQuitMission(%saveFirst)
  257. {
  258.    if(%saveFirst)
  259.    {
  260.       EditorSaveMissionMenu();
  261.       Editor.close("MainMenuGui");
  262.       disconnect();
  263.    }
  264.    else
  265.    {
  266.       Editor.close("MainMenuGui");
  267.       disconnect();
  268.    }
  269. }
  270.  
  271. function EditorNewMission()
  272. {
  273.    if(ETerrainEditor.isMissionDirty || ETerrainEditor.isDirty || EWorldEditor.isDirty)
  274.    {
  275.       MessageBoxYesNo("Mission Modified", "Would you like to save changes to the current mission \"" @
  276.          $Server::MissionFile @ "\" before creating a new mission?", "EditorDoNewMission(true);", "EditorDoNewMission(false);");
  277.    }
  278.    else
  279.       EditorDoNewMission(false);
  280. }
  281.  
  282. function EditorSaveMissionMenu()
  283. {
  284.    //if (MissionInfo.type !$= "DemoScene") {
  285.    if ($BlockSave!=1) {
  286.       if(EditorGui.saveAs)
  287.          EditorSaveMissionAs();
  288.       else
  289.          EditorSaveMission();
  290.    }  else {
  291.       echo("To enable mission saving, make \$BlockSave=1.");
  292.       MessageBoxOK("Save Function Disabled","The Save Mission function has been disabled for this demo walkthrough.\n\n  If you are sure you want to make changes to this mission and save them, go to the console (tilde \"~\" key) and type \"$BlockSave=0;\" first.");
  293.    }
  294. }
  295.  
  296. function EditorSaveMission()
  297. {
  298.    // just save the mission without renaming it
  299.  
  300.    // first check for dirty and read-only files:
  301.    if((EWorldEditor.isDirty || ETerrainEditor.isMissionDirty) && !isWriteableFileName($Server::MissionFile))
  302.    {
  303.       MessageBoxOK("Error", "Mission file \""@ $Server::MissionFile @ "\" is read-only.");
  304.       return false;
  305.    }
  306.    if(ETerrainEditor.isDirty && !isWriteableFileName(Terrain.terrainFile))
  307.    {
  308.       MessageBoxOK("Error", "Terrain file \""@ Terrain.terrainFile @ "\" is read-only.");
  309.       return false;
  310.    }
  311.   
  312.    // now write the terrain and mission files out:
  313.  
  314.    if(EWorldEditor.isDirty || ETerrainEditor.isMissionDirty)
  315.       MissionGroup.save($Server::MissionFile);
  316.    if(ETerrainEditor.isDirty)
  317.       Terrain.save(Terrain.terrainFile);
  318.    EWorldEditor.isDirty = false;
  319.    ETerrainEditor.isDirty = false;
  320.    ETerrainEditor.isMissionDirty = false;
  321.    EditorGui.saveAs = false;
  322.  
  323.    return true;
  324. }
  325.  
  326. function EditorDoSaveAs(%missionName)
  327. {
  328.    ETerrainEditor.isDirty = true;
  329.    EWorldEditor.isDirty = true;
  330.    %saveMissionFile = $Server::MissionFile;
  331.    %saveTerrName = Terrain.terrainFile;
  332.  
  333.    $Server::MissionFile = %missionName;
  334.    Terrain.terrainFile = filePath(%missionName) @ "/" @ fileBase(%missionName) @ ".ter";
  335.  
  336.    if(!EditorSaveMission())
  337.    {
  338.       $Server::MissionFile = %saveMissionFile;
  339.       Terrain.terrainFile = %saveTerrName;
  340.    }
  341. }
  342.  
  343. function EditorSaveMissionAs()
  344. {
  345.    if ($BlockSave!=1) {
  346.       getSaveFilename("*.mis", "EditorDoSaveAs", $Server::MissionFile);
  347.    }  else {
  348.       echo("To enable mission saving, make \$BlockSave=0.");
  349.       MessageBoxOK("Save Function Disabled","The Save Mission function has been disabled for this demo walkthrough.\n\n  If you are sure you want to make changes to this mission and save them, go to the console (tilde \"~\" key) and type \"$BlockSave=0;\" first.");
  350.    }
  351. }
  352.  
  353. function EditorDoLoadMission(%file)
  354. {
  355.    // close the current editor, it will get cleaned up by MissionCleanup
  356.    Editor.close();
  357.  
  358.    loadMission( %file, true ) ;
  359.  
  360.    // recreate and open the editor
  361.    Editor::create();
  362.    MissionCleanup.add(Editor);
  363.    EditorGui.loadingMission = true;
  364.    Editor.open();
  365. }
  366.  
  367. function EditorSaveBeforeLoad()
  368. {
  369.    if(EditorSaveMission())
  370.       getLoadFilename("*.mis", "EditorDoLoadMission");
  371. }
  372.  
  373. function EditorDoNewMission(%saveFirst)
  374. {
  375.    if(%saveFirst)
  376.       EditorSaveMission();
  377.  
  378.    EditorDoLoadMission(expandFilename("~/data/newMission.mis"));
  379.    EditorGui.saveAs = true;
  380.    EWorldEditor.isDirty = true;
  381.    ETerrainEditor.isDirty = true;
  382. }
  383.  
  384. function EditorOpenMission()
  385. {
  386.    if(ETerrainEditor.isMissionDirty || ETerrainEditor.isDirty || EWorldEditor.isDirty)
  387.    {
  388.       MessageBoxYesNo("Mission Modified", "Would you like to save changes to the current mission \"" @
  389.          $Server::MissionFile @ "\" before opening a new mission?", "EditorSaveBeforeLoad();", "getLoadFilename(\"*.mis\", \"EditorDoLoadMission\");");
  390.    }
  391.    else
  392.       getLoadFilename("*.mis", "EditorDoLoadMission");
  393. }
  394.  
  395. function EditorMenuBar::onMenuSelect(%this, %menuId, %menu)
  396. {
  397.    if(%menu $= "File")
  398.    {
  399.       %editingHeightfield = ETerrainEditor.isVisible() && EHeightField.isVisible();
  400.       EditorMenuBar.setMenuItemEnable("File", "Export Terraform Bitmap...", %editingHeightfield);
  401.       EditorMenuBar.setMenuItemEnable("File", "Save Mission...", ETerrainEditor.isDirty || ETerrainEditor.isMissionDirty || EWorldEditor.isDirty);
  402.    }
  403.    else if(%menu $= "Edit")
  404.    {
  405.       // enable/disable undo, redo, cut, copy, paste depending on editor settings
  406.  
  407.       if(EWorldEditor.isVisible())
  408.       {
  409.          // do actions based on world editor...
  410.          EditorMenuBar.setMenuItemEnable("Edit", "Select All", true);
  411.          EditorMenuBar.setMenuItemEnable("Edit", "Paste", EWorldEditor.canPasteSelection());
  412.          %canCutCopy = EWorldEditor.getSelectionSize() > 0;
  413.  
  414.          EditorMenuBar.setMenuItemEnable("Edit", "Cut", %canCutCopy);
  415.          EditorMenuBar.setMenuItemEnable("Edit", "Copy", %canCutCopy);
  416.  
  417.       }
  418.       else if(ETerrainEditor.isVisible())
  419.       {
  420.          EditorMenuBar.setMenuItemEnable("Edit", "Cut", false);
  421.          EditorMenuBar.setMenuItemEnable("Edit", "Copy", false);
  422.          EditorMenuBar.setMenuItemEnable("Edit", "Paste", false);
  423.          EditorMenuBar.setMenuItemEnable("Edit", "Select All", false);
  424.       }
  425.    }
  426.    else if(%menu $= "World")
  427.    {
  428.       %selSize = EWorldEditor.getSelectionSize();
  429.       %lockCount = EWorldEditor.getSelectionLockCount();
  430.       %hideCount = EWorldEditor.getSelectionHiddenCount();
  431.  
  432.       EditorMenuBar.setMenuItemEnable("World", "Lock Selection", %lockCount < %selSize);
  433.       EditorMenuBar.setMenuItemEnable("World", "Unlock Selection", %lockCount > 0);
  434.       EditorMenuBar.setMenuItemEnable("World", "Hide Selection", %hideCount < %selSize);
  435.       EditorMenuBar.setMenuItemEnable("World", "Show Selection", %hideCount > 0);
  436.  
  437.       EditorMenuBar.setMenuItemEnable("World", "Add Selection to Instant Group", %selSize > 0);
  438.       EditorMenuBar.setMenuItemEnable("World", "Camera To Selection", %selSize > 0);
  439.       EditorMenuBar.setMenuItemEnable("World", "Reset Transforms", %selSize > 0 && %lockCount == 0);
  440.       EditorMenuBar.setMenuItemEnable("World", "Drop Selection", %selSize > 0 && %lockCount == 0);
  441.       EditorMenuBar.setMenuItemEnable("World", "Delete Selection", %selSize > 0 && %lockCount == 0);
  442.    }
  443. }
  444.  
  445. function EditorMenuBar::onMenuItemSelect(%this, %menuId, %menu, %itemId, %item)
  446. {
  447.    switch$(%menu)
  448.    {
  449.       case "File":
  450.          %this.onFileMenuItemSelect(%itemId, %item);
  451.       case "Edit":
  452.          %this.onEditMenuItemSelect(%itemId, %item);
  453.       case "World":
  454.          %this.onWorldMenuItemSelect(%itemId, %item);
  455.       case "Window":
  456.          %this.onWindowMenuItemSelect(%itemId, %item);
  457.       case "Action":
  458.          %this.onActionMenuItemSelect(%itemId, %item);
  459.       case "Brush":
  460.          %this.onBrushMenuItemSelect(%itemId, %item);
  461.       case "Camera":
  462.          %this.onCameraMenuItemSelect(%itemId, %item);
  463.    }
  464. }
  465.  
  466. function EditorMenuBar::onFileMenuItemSelect(%this, %itemId, %item)
  467. {
  468.    switch$(%item)
  469.    {
  470.       case "New Mission...":
  471.          EditorNewMission();
  472.       case "Open Mission...":
  473.          EditorOpenMission();
  474.       case "Save Mission...":
  475.          EditorSaveMissionMenu();
  476.       case "Save Mission As...":
  477.          EditorSaveMissionAs();
  478.       case "Import Texture Data...":
  479.          Texture::import();
  480.       case "Import Terraform Data...":
  481.          Heightfield::import();
  482.       case "Export Terraform Bitmap...":
  483.          Heightfield::saveBitmap("");
  484.       case "Toggle Map Editor...":
  485.          Editor.close("PlayGui");
  486.       case "Quit":
  487.          EditorQuitMission();
  488.    }
  489. }
  490.  
  491. function EditorMenuBar::onCameraMenuItemSelect(%this, %itemId, %item)
  492. {
  493.    switch$(%item)
  494.    {
  495.       case "Drop Camera at Player":
  496.          commandToServer('dropCameraAtPlayer');
  497.       case "Drop Player at Camera":
  498.          commandToServer('DropPlayerAtCamera');
  499.       case "Toggle Camera":
  500.          commandToServer('ToggleCamera');
  501.       default:
  502.          // all the rest are camera speeds:
  503.          // item ids go from 3 (slowest) to 9 (fastest)
  504.          %this.setMenuItemChecked("Camera", %itemId, true);
  505.          // camera movement speed goes from 5 to 200:
  506.          $Camera::movementSpeed = ((%itemId - 3) / 6.0) * 195 + 5;
  507.    }
  508. }
  509.  
  510. function EditorMenuBar::onActionMenuItemSelect(%this, %itemId, %item)
  511. {
  512.    EditorMenuBar.setMenuItemChecked("Action", %item, true);
  513.    switch$(%item)
  514.    {
  515.       case "Select":
  516.          ETerrainEditor.currentMode = "select";
  517.          ETerrainEditor.selectionHidden = false;
  518.          ETerrainEditor.renderVertexSelection = true;
  519.          ETerrainEditor.setAction("select");
  520.       case "Adjust Selection":
  521.          ETerrainEditor.currentMode = "adjust";
  522.          ETerrainEditor.selectionHidden = false;
  523.          ETerrainEditor.setAction("adjustHeight");
  524.          ETerrainEditor.currentAction = brushAdjustHeight;
  525.          ETerrainEditor.renderVertexSelection = true;
  526.       default:
  527.          ETerrainEditor.currentMode = "paint";
  528.          ETerrainEditor.selectionHidden = true;
  529.          ETerrainEditor.setAction(ETerrainEditor.currentAction);
  530.          switch$(%item)
  531.          {
  532.             case "Add Dirt":
  533.                ETerrainEditor.currentAction = raiseHeight;
  534.                ETerrainEditor.renderVertexSelection = true;
  535.             case "Paint Material":
  536.                ETerrainEditor.currentAction = paintMaterial;
  537.                ETerrainEditor.renderVertexSelection = true;
  538.             case "Excavate":
  539.                ETerrainEditor.currentAction = lowerHeight;
  540.                ETerrainEditor.renderVertexSelection = true;
  541.             case "Set Height":
  542.                ETerrainEditor.currentAction = setHeight;
  543.                ETerrainEditor.renderVertexSelection = true;
  544.             case "Adjust Height":
  545.                ETerrainEditor.currentAction = brushAdjustHeight;
  546.                ETerrainEditor.renderVertexSelection = true;
  547.             case "Flatten":
  548.                ETerrainEditor.currentAction = flattenHeight;
  549.                ETerrainEditor.renderVertexSelection = true;
  550.             case "Smooth":
  551.                ETerrainEditor.currentAction = smoothHeight;
  552.                ETerrainEditor.renderVertexSelection = true;
  553.             case "Set Empty":
  554.                ETerrainEditor.currentAction = setEmpty;
  555.                ETerrainEditor.renderVertexSelection = false;
  556.             case "Clear Empty":
  557.                ETerrainEditor.currentAction = clearEmpty;
  558.                ETerrainEditor.renderVertexSelection = false;
  559.          }
  560.          if(ETerrainEditor.currentMode $= "select")
  561.             ETerrainEditor.processAction(ETerrainEditor.currentAction);
  562.          else if(ETerrainEditor.currentMode $= "paint")
  563.             ETerrainEditor.setAction(ETerrainEditor.currentAction);
  564.    }
  565. }
  566.  
  567. function EditorMenuBar::onBrushMenuItemSelect(%this, %itemId, %item)
  568. {
  569.    EditorMenuBar.setMenuItemChecked("Brush", %item, true);
  570.    switch$(%item)
  571.    {
  572.       case "Box Brush":
  573.          ETerrainEditor.setBrushType(box);
  574.       case "Circle Brush":
  575.          ETerrainEditor.setBrushType(ellipse);
  576.       case "Soft Brush":
  577.          ETerrainEditor.enableSoftBrushes = true;
  578.       case "Hard Brush":
  579.          ETerrainEditor.enableSoftBrushes = false;
  580.       default:
  581.          // the rest are brush sizes:
  582.          ETerrainEditor.brushSize = %itemId;
  583.          
  584.          ETerrainEditor.setBrushSize(%itemId, %itemId);
  585.    }
  586. }
  587.  
  588. function EditorMenuBar::onWorldMenuItemSelect(%this, %itemId, %item)
  589. {
  590.    // edit commands for world editor...
  591.    switch$(%item)
  592.    {
  593.       case "Lock Selection":
  594.          EWorldEditor.lockSelection(true);
  595.       case "Unlock Selection":
  596.          EWorldEditor.lockSelection(false);
  597.       case "Hide Selection":
  598.          EWorldEditor.hideSelection(true);
  599.       case "Show Selection":
  600.          EWorldEditor.hideSelection(false);
  601.       case "Camera To Selection":
  602.          EWorldEditor.dropCameraToSelection();
  603.       case "Reset Transforms":
  604.          EWorldEditor.resetTransforms();
  605.       case "Drop Selection":
  606.          EWorldEditor.dropSelection();
  607.       case "Delete Selection":
  608.          EWorldEditor.deleteSelection();
  609.       case "Add Selection to Instant Group":
  610.          EWorldEditor.addSelectionToAddGroup();
  611.       default:
  612.          EditorMenuBar.setMenuItemChecked("World", %item, true);
  613.          switch$(%item)
  614.          {
  615.             case "Drop at Origin":
  616.                EWorldEditor.dropType = "atOrigin";
  617.             case "Drop at Camera":
  618.                EWorldEditor.dropType = "atCamera";
  619.             case "Drop at Camera w/Rot":
  620.                EWorldEditor.dropType = "atCameraRot";
  621.             case "Drop below Camera":
  622.                EWorldEditor.dropType = "belowCamera";
  623.             case "Drop at Screen Center":
  624.                EWorldEditor.dropType = "screenCenter";
  625.             case "Drop to Ground":
  626.                EWorldEditor.dropType = "toGround";
  627.             case "Drop at Centroid":
  628.                EWorldEditor.dropType = "atCentroid";
  629.          }
  630.    }
  631. }
  632.  
  633. function EditorMenuBar::onEditMenuItemSelect(%this, %itemId, %item)
  634. {
  635.    if(%item $= "World Editor Settings...")
  636.       Canvas.pushDialog(WorldEditorSettingsDlg);
  637.    else if(%item $= "Terrain Editor Settings...")
  638.       Canvas.pushDialog(TerrainEditorValuesSettingsGui, 99);
  639.    else if(%item $= "Relight Scene")
  640.       lightScene("", forceAlways);
  641.    else if(EWorldEditor.isVisible())
  642.    {
  643.       // edit commands for world editor...
  644.       switch$(%item)
  645.       {
  646.          case "Undo":
  647.             EWorldEditor.undo();
  648.          case "Redo":
  649.             EWorldEditor.redo();
  650.          case "Copy":
  651.             EWorldEditor.copySelection();
  652.          case "Cut":
  653.             EWorldEditor.copySelection();
  654.             EWorldEditor.deleteSelection();
  655.          case "Paste":
  656.             EWorldEditor.pasteSelection();
  657.          case "Select All":
  658.          case "Select None":
  659.       }
  660.    }
  661.    else if(ETerrainEditor.isVisible())
  662.    {
  663.       // do some terrain stuffin'
  664.       switch$(%item)
  665.       {
  666.          case "Undo":
  667.             ETerrainEditor.undo();
  668.          case "Redo":
  669.             ETerrainEditor.redo();
  670.          case "Select None":
  671.             ETerrainEditor.clearSelection();
  672.       }
  673.    }
  674. }
  675.  
  676. function EditorMenuBar::onWindowMenuItemSelect(%this, %itemId, %item)
  677. {
  678.    EditorGui.setEditor(%item);
  679. }
  680.  
  681. function Creator::onWake(%this)
  682. {
  683.    Creator.init();
  684. }
  685.  
  686. function EditorGui::setWorldEditorVisible(%this)
  687. {
  688.    EWorldEditor.setVisible(true);
  689.    ETerrainEditor.setVisible(false);
  690.    EditorMenuBar.setMenuVisible("World", true);
  691.    EditorMenuBar.setMenuVisible("Action", false);
  692.    EditorMenuBar.setMenuVisible("Brush", false);
  693.    EWorldEditor.makeFirstResponder(true);
  694.    EditorTree.open(MissionGroup,true);
  695. }
  696.  
  697. function EditorGui::setTerrainEditorVisible(%this)
  698. {
  699.    EWorldEditor.setVisible(false);
  700.    ETerrainEditor.setVisible(true);
  701.    ETerrainEditor.attachTerrain();
  702.    EHeightField.setVisible(false);
  703.    ETexture.setVisible(false);
  704.    EditorMenuBar.setMenuVisible("World", false);
  705.    EditorMenuBar.setMenuVisible("Action", true);
  706.    EditorMenuBar.setMenuVisible("Brush", true);
  707.    ETerrainEditor.makeFirstResponder(true);
  708.    EPainter.setVisible(false);
  709. }
  710.  
  711. function EditorGui::setEditor(%this, %editor)
  712. {
  713.    EditorMenuBar.setMenuItemBitmap("Window", %this.currentEditor, -1);
  714.    EditorMenuBar.setMenuItemBitmap("Window", %editor, 0);
  715.    %this.currentEditor = %editor;
  716.  
  717.    switch$(%editor)
  718.    {
  719.       case "World Editor":
  720.          EWFrame.setVisible(false);
  721.          EWMissionArea.setVisible(false);
  722.          %this.setWorldEditorVisible();
  723.       case "World Editor Inspector":
  724.          EWFrame.setVisible(true);
  725.          EWMissionArea.setVisible(false);
  726.          EWCreatorPane.setVisible(false);
  727.          EWInspectorPane.setVisible(true);
  728.          %this.setWorldEditorVisible();
  729.       case "World Editor Creator":
  730.          EWFrame.setVisible(true);
  731.          EWMissionArea.setVisible(false);
  732.          EWCreatorPane.setVisible(true);
  733.          EWInspectorPane.setVisible(false);
  734.          %this.setWorldEditorVisible();
  735.       case "Mission Area Editor":
  736.          EWFrame.setVisible(false);
  737.          EWMissionArea.setVisible(true);
  738.          %this.setWorldEditorVisible();
  739.       case "Terrain Editor":
  740.          %this.setTerrainEditorVisible();
  741.       case "Terrain Terraform Editor":
  742.          %this.setTerrainEditorVisible();
  743.          EHeightField.setVisible(true);
  744.       case "Terrain Texture Editor":
  745.          %this.setTerrainEditorVisible();
  746.          ETexture.setVisible(true);
  747.       case "Terrain Texture Painter":
  748.          %this.setTerrainEditorVisible();
  749.          EPainter.setVisible(true);
  750.          EPainter.setup();
  751.  
  752.    }
  753. }
  754.  
  755. function EditorGui::getHelpPage(%this)
  756. {
  757.    switch$(%this.currentEditor)
  758.    {
  759.       case "World Editor" or "World Editor Inspector" or "World Editor Creator":
  760.          return "5. World Editor";
  761.       case "Mission Area Editor":
  762.          return "6. Mission Area Editor";
  763.       case "Terrain Editor":
  764.          return "7. Terrain Editor";
  765.       case "Terrain Terraform Editor":
  766.          return "8. Terrain Terraform Editor";
  767.       case "Terrain Texture Editor":
  768.          return "9. Terrain Texture Editor";
  769.       case "Terrain Texture Painter":
  770.          return "10. Terrain Texture Painter";
  771.    }
  772. }
  773.  
  774.  
  775. function ETerrainEditor::setPaintMaterial(%this, %matIndex)
  776. {
  777.    ETerrainEditor.paintMaterial = EPainter.mat[%matIndex];
  778. }
  779.  
  780. function ETerrainEditor::changeMaterial(%this, %matIndex)
  781. {
  782.    EPainter.matIndex = %matIndex;
  783.    getLoadFilename("*.png\t*.jpg", EPainterChangeMat);
  784. }
  785.  
  786. function EPainterChangeMat(%file)
  787. {
  788.    // make sure the material isn't already in the terrain.
  789.    %file = filePath(%file) @ "/" @ fileBase(%file);
  790.    for(%i = 0; %i < 6; %i++)
  791.       if(EPainter.mat[%i] $= %file)
  792.          return;
  793.  
  794.    EPainter.mat[EPainter.matIndex] = %file;
  795.    %mats = "";
  796.    for(%i = 0; %i < 6; %i++)
  797.       %mats = %mats @ EPainter.mat[%i] @ "\n";
  798.    ETerrainEditor.setTerrainMaterials(%mats);
  799.    EPainter.setup();
  800.    ("ETerrainMaterialPaint" @ EPainter.matIndex).performClick();
  801. }
  802.  
  803. function EPainter::setup(%this)
  804. {
  805.    EditorMenuBar.onActionMenuItemSelect(0, "Paint Material");
  806.    %mats = ETerrainEditor.getTerrainMaterials();
  807.    %valid = true;
  808.    for(%i = 0; %i < 6; %i++)
  809.    {
  810.       %mat = getRecord(%mats, %i);
  811.       %this.mat[%i] = %mat;
  812.       ("ETerrainMaterialText" @ %i).setText(fileBase(%mat));
  813.       ("ETerrainMaterialBitmap" @ %i).setBitmap(%mat);
  814.       ("ETerrainMaterialChange" @ %i).setActive(true);
  815.       ("ETerrainMaterialPaint" @ %i).setActive(%mat !$= "");
  816.       if(%mat $= "")
  817.       {
  818.          ("ETerrainMaterialChange" @ %i).setText("Add...");
  819.          if(%valid)
  820.             %valid = false;
  821.          else
  822.             ("ETerrainMaterialChange" @ %i).setActive(false);
  823.       }
  824.       else
  825.          ("ETerrainMaterialChange" @ %i).setText("Change...");
  826.    }
  827.    ETerrainMaterialPaint0.performClick();
  828. }
  829.  
  830. function EditorGui::onWake(%this)
  831. {
  832.    MoveMap.push();
  833.    EditorMap.push();
  834.    %this.setEditor(%this.currentEditor);
  835.    
  836.    if (DemoEditorAlert.helpTag<2) Canvas.pushDialog(DemoEditorAlert);
  837.  
  838. }
  839.  
  840. function EditorGui::onSleep(%this)
  841. {
  842.    EditorMap.pop();
  843.    MoveMap.pop();
  844.    $Server::CurrentScene.open();
  845. }
  846.  
  847. function AreaEditor::onUpdate(%this, %area)
  848. {
  849.    AreaEditingText.setValue( "X: " @ getWord(%area,0) @ " Y: " @ getWord(%area,1) @ " W: " @ getWord(%area,2) @ " H: " @ getWord(%area,3));
  850. }
  851.  
  852. function AreaEditor::onWorldOffset(%this, %offset)
  853. {
  854. }
  855.  
  856. function WorldEditor::onDelete(%this)
  857. {
  858.    EditorTree.deleteSelection();
  859. }
  860.  
  861. function WorldEditor::onSelect(%this,%obj)
  862. {
  863.    EditorTree.addSelection(%obj); 
  864. }
  865.  
  866. function WorldEditor::onUnSelect(%this,%obj)
  867.    EditorTree.removeSelection(%obj);
  868. }
  869.  
  870. function WorldEditor::onClearSelected(%this)
  871. {
  872.    EditorTree.clearSelection();
  873. }
  874.  
  875. function WorldEditor::onClearSelection(%this)
  876. {
  877.    EditorTree.clearSelection();
  878. }
  879.  
  880. function EditorTree::onClearSelected(%this)
  881. {
  882.    WorldEditor.clearSelection();
  883. }
  884.  
  885. function EditorTree::init(%this)
  886. {
  887.    //%this.open(MissionGroup);
  888.  
  889.    // context menu
  890.    new GuiControl(ETContextPopupDlg)
  891.    {
  892.     profile = "GuiModelessDialogProfile";
  893.        horizSizing = "width";
  894.        vertSizing = "height";
  895.        position = "0 0";
  896.        extent = "640 480";
  897.        minExtent = "8 8";
  898.        visible = "1";
  899.        setFirstResponder = "0";
  900.        modal = "1";
  901.       
  902.       new GuiPopUpMenuCtrl(ETContextPopup)
  903.       {
  904.          profile = "GuiScrollProfile";
  905.          position = "0 0";
  906.          extent = "0 0";
  907.          minExtent = "0 0";
  908.          maxPopupHeight = "200";
  909.          command = "canvas.popDialog(ETContextPopupDlg);";
  910.       };
  911.    };
  912.    ETContextPopup.setVisible(false);
  913. }
  914.  
  915. function EditorTree::onInspect(%this, %obj)
  916. {
  917.    Inspector.inspect(%obj);
  918.    InspectorNameEdit.setValue(%obj.getName());
  919. }
  920. function EditorTree::onAddSelection(%this, %obj)
  921. {
  922.    if($AIEdit)   
  923.       aiEdit.selectObject(%obj);
  924.    else
  925.       EWorldEditor.selectObject(%obj);
  926. }
  927. function EditorTree::onRemoveSelection(%this, %obj)
  928. {
  929.    if($AIEdit)   
  930.       aiEdit.selectObject(%obj);
  931.    else
  932.       EWorldEditor.unselectObject(%obj);
  933. }
  934. function EditorTree::onSelect(%this, %obj)
  935. {
  936.    if($AIEdit)   
  937.       aiEdit.selectObject(%obj);
  938.    else {
  939.       EWorldEditor.selectObject(%obj);      
  940.    }
  941.  
  942. }
  943.  
  944. function EditorTree::onUnselect(%this, %obj)
  945. {
  946.    if($AIEdit)
  947.       aiEdit.unselectObject(%obj);
  948.    else
  949.       EWorldEditor.unselectObject(%obj);
  950. }
  951.  
  952. function ETContextPopup::onSelect(%this, %index, %value)
  953. {
  954.    switch(%index)
  955.    {
  956.       case 0:
  957.          EditorTree.contextObj.delete();
  958.    }
  959. }
  960.  
  961. //------------------------------------------------------------------------------
  962. // Functions
  963. //------------------------------------------------------------------------------
  964.  
  965. function WorldEditor::init(%this)
  966. {
  967.    // add objclasses which we do not want to collide with
  968.    %this.ignoreObjClass(TerrainBlock, Sky, AIObjective);
  969.  
  970.    // editing modes
  971.    %this.numEditModes = 3;
  972.    %this.editMode[0]    = "move";
  973.    %this.editMode[1]    = "rotate";
  974.    %this.editMode[2]    = "scale";
  975.  
  976.    // context menu
  977.    new GuiControl(WEContextPopupDlg)
  978.    {
  979.       profile = "GuiModelessDialogProfile";
  980.        horizSizing = "width";
  981.        vertSizing = "height";
  982.        position = "0 0";
  983.        extent = "640 480";
  984.        minExtent = "8 8";
  985.        visible = "1";
  986.        setFirstResponder = "0";
  987.        modal = "1";
  988.       
  989.       new GuiPopUpMenuCtrl(WEContextPopup)
  990.       {
  991.          profile = "GuiScrollProfile";
  992.          position = "0 0";
  993.          extent = "0 0";
  994.          minExtent = "0 0";
  995.          maxPopupHeight = "200";
  996.          command = "canvas.popDialog(WEContextPopupDlg);";
  997.       };
  998.    };
  999.    WEContextPopup.setVisible(false);
  1000. }
  1001.  
  1002. //------------------------------------------------------------------------------
  1003.  
  1004. function WorldEditor::onDblClick(%this, %obj)
  1005. {
  1006.    // Commented out because making someone double click to do this is stupid
  1007.    // and has the possibility of moving hte object
  1008.    
  1009.    //Inspector.inspect(%obj);
  1010.    //InspectorNameEdit.setValue(%obj.getName());
  1011. }
  1012.  
  1013. function WorldEditor::onClick( %this, %obj )
  1014. {
  1015.    Inspector.inspect( %obj );
  1016.    InspectorNameEdit.setValue( %obj.getName() );
  1017. }
  1018.  
  1019. //------------------------------------------------------------------------------
  1020.  
  1021. function WorldEditor::export(%this)
  1022. {
  1023.    getSaveFilename("~/editor/*.mac", %this @ ".doExport", "selection.mac");
  1024. }
  1025.  
  1026. function WorldEditor::doExport(%this, %file)
  1027. {
  1028.    missionGroup.save("~/editor/" @ %file, true);
  1029. }
  1030.  
  1031. function WorldEditor::import(%this)
  1032. {
  1033.    getLoadFilename("~/editor/*.mac", %this @ ".doImport");
  1034. }
  1035.  
  1036. function WorldEditor::doImport(%this, %file)
  1037. {
  1038.    exec("~/editor/" @ %file);
  1039. }
  1040.  
  1041. function WorldEditor::onGuiUpdate(%this, %text)
  1042. {
  1043. }
  1044.  
  1045. function WorldEditor::getSelectionLockCount(%this)
  1046. {
  1047.    %ret = 0;
  1048.    for(%i = 0; %i < %this.getSelectionSize(); %i++)
  1049.    {
  1050.       %obj = %this.getSelectedObject(%i);
  1051.       if(%obj.locked $= "true")
  1052.          %ret++;
  1053.    }
  1054.    return %ret;
  1055. }
  1056.  
  1057. function WorldEditor::getSelectionHiddenCount(%this)
  1058. {
  1059.    %ret = 0;
  1060.    for(%i = 0; %i < %this.getSelectionSize(); %i++)
  1061.    {
  1062.       %obj = %this.getSelectedObject(%i);
  1063.       if(%obj.hidden $= "true")
  1064.          %ret++;
  1065.    }
  1066.    return %ret;
  1067. }
  1068.  
  1069. function WorldEditor::dropCameraToSelection(%this)
  1070. {
  1071.    if(%this.getSelectionSize() == 0)
  1072.       return;
  1073.  
  1074.    %pos = %this.getSelectionCentroid();
  1075.    %cam = LocalClientConnection.camera.getTransform();
  1076.  
  1077.    // set the pnt
  1078.    %cam = setWord(%cam, 0, getWord(%pos, 0));
  1079.    %cam = setWord(%cam, 1, getWord(%pos, 1));
  1080.    %cam = setWord(%cam, 2, getWord(%pos, 2));
  1081.  
  1082.    LocalClientConnection.camera.setTransform(%cam);
  1083. }
  1084.  
  1085. // * pastes the selection at the same place (used to move obj from a group to another)
  1086. function WorldEditor::moveSelectionInPlace(%this)
  1087. {
  1088.    %saveDropType = %this.dropType;
  1089.    %this.dropType = "atCentroid";
  1090.    %this.copySelection();
  1091.    %this.deleteSelection();
  1092.    %this.pasteSelection();
  1093.    %this.dropType = %saveDropType;
  1094. }
  1095.  
  1096. function WorldEditor::addSelectionToAddGroup(%this)
  1097. {
  1098.    for(%i = 0; %i < %this.getSelectionSize(); %i++) 
  1099.    {
  1100.       %obj = %this.getSelectedObject(%i);
  1101.       $InstantGroup.add(%obj);
  1102.    }
  1103. }   
  1104.  
  1105. // resets the scale and rotation on the selection set
  1106. function WorldEditor::resetTransforms(%this)
  1107. {
  1108.    %this.addUndoState();
  1109.  
  1110.    for(%i = 0; %i < %this.getSelectionSize(); %i++)
  1111.    {
  1112.       %obj = %this.getSelectedObject(%i);
  1113.       %transform = %obj.getTransform();
  1114.  
  1115.       %transform = setWord(%transform, 3, "0");
  1116.       %transform = setWord(%transform, 4, "0");
  1117.       %transform = setWord(%transform, 5, "1");
  1118.       %transform = setWord(%transform, 6, "0");
  1119.          
  1120.       //
  1121.       %obj.setTransform(%transform);
  1122.       %obj.setScale("1 1 1");
  1123.    }
  1124. }
  1125.  
  1126.  
  1127. function WorldEditorToolbarDlg::init(%this)
  1128. {
  1129.    WorldEditorInspectorCheckBox.setValue(WorldEditorToolFrameSet.isMember("EditorToolInspectorGui"));
  1130.    WorldEditorMissionAreaCheckBox.setValue(WorldEditorToolFrameSet.isMember("EditorToolMissionAreaGui"));
  1131.    WorldEditorTreeCheckBox.setValue(WorldEditorToolFrameSet.isMember("EditorToolTreeViewGui"));
  1132.    WorldEditorCreatorCheckBox.setValue(WorldEditorToolFrameSet.isMember("EditorToolCreatorGui"));
  1133. }
  1134.  
  1135. function Creator::init( %this ) 
  1136. {
  1137.    //%this.clear();
  1138.    $InstantGroup = "MissionGroup";
  1139.  
  1140.    // ---------- INTERIORS    
  1141.    %base = %this.insertItem( 0, "Interiors" );
  1142.  
  1143.    // walk all the interiors and add them to the correct group
  1144.    %interiorId = "";
  1145.    %file = findFirstFile( "*.dif" );
  1146.    
  1147.    while( %file !$= "" ) 
  1148.    {
  1149.       // Determine which group to put the file in
  1150.       // and build the group hierarchy as we go
  1151.       %split    = strreplace(%file, "/", " ");
  1152.       %dirCount = getWordCount(%split)-1;
  1153.       %parentId = %base;
  1154.       
  1155.       for(%i=0; %i<%dirCount; %i++)
  1156.       {
  1157.          %parent = getWords(%split, 0, %i);
  1158.          
  1159.          // if the group doesn't exist create it
  1160.          if ( !%interiorId[%parent] )
  1161.             %interiorId[%parent] = %this.insertItem( %parentId, getWord(%split, %i));
  1162.          %parentId = %interiorId[%parent];
  1163.       }
  1164.       
  1165.       // Add the file to the group
  1166.       %create = "createInterior(" @ "\"" @ %file @ "\"" @ ");";
  1167.       %this.insertItem( %parentId, fileBase( %file ), %create,"Interior" );
  1168.    
  1169.       %file = findNextFile( "*.dif" );
  1170.    }
  1171.  
  1172.  
  1173.    // ---------- SHAPES - add in all the shapes now...
  1174.    %base = %this.insertItem(0, "Shapes");
  1175.    %dataGroup = "DataBlockGroup";
  1176.    
  1177.    for(%i = 0; %i < %dataGroup.getCount(); %i++)
  1178.    {
  1179.       %obj = %dataGroup.getObject(%i);
  1180.       echo ("Obj: " @ %obj.getName() @ " - " @ %obj.category );
  1181.       if(%obj.category !$= "" || %obj.category != 0)
  1182.       {
  1183.          %id = %this.findItemByName(%obj.category);
  1184.             if (%id  == 0) 
  1185.             { 
  1186.             %grp = %this.insertItem(%base,%obj.category);
  1187.             %this.insertItem(%grp, %obj.getName(), %obj.getClassName() @ "::create(" @ %obj.getName() @ ");","Item");
  1188.          }
  1189.          else
  1190.          {
  1191.             %this.insertItem(%id, %obj.getName(), %obj.getClassName() @ "::create(" @ %obj.getName() @ ");","Item");
  1192.          }
  1193.       }
  1194.    }
  1195.  
  1196.  
  1197.    // ---------- Static Shapes    
  1198.    %base = %this.insertItem( 0, "Static Shapes" );
  1199.  
  1200.    // walk all the statics and add them to the correct group
  1201.    %staticId = "";
  1202.    %file = findFirstFile( "*.dts" );
  1203.    while( %file !$= "" ) 
  1204.    {
  1205.       // Determine which group to put the file in
  1206.       // and build the group heirarchy as we go
  1207.       %split    = strreplace(%file, "/", " ");
  1208.       %dirCount = getWordCount(%split)-1;
  1209.       %parentId = %base;
  1210.       
  1211.       for(%i=0; %i<%dirCount; %i++)
  1212.       {
  1213.          %parent = getWords(%split, 0, %i);
  1214.          // if the group doesn't exist create it
  1215.          if ( !%staticId[%parent] )
  1216.             %staticId[%parent] = %this.insertItem( %parentId, getWord(%split, %i));
  1217.          %parentId = %staticId[%parent];
  1218.       }
  1219.       // Add the file to the group
  1220.       %create = "TSStatic::create(\"" @ %file @ "\");";
  1221.       %this.insertItem( %parentId, fileBase( %file ), %create,"TSStatic" );
  1222.    
  1223.       %file = findNextFile( "*.dts" );
  1224.    }
  1225.  
  1226.  
  1227.    // *** OBJECTS - do the objects now...
  1228.    %objGroup[0] = "Environment";
  1229.    %objGroup[1] = "Mission";
  1230.    %objGroup[2] = "System";
  1231.    //%objGroup[3] = "AI";
  1232.  
  1233.    %Environment_Item[0]  = "Sky";
  1234.    %Environment_Item[1]  = "Sun";
  1235.    %Environment_Item[2]  = "Lightning";
  1236.    %Environment_Item[3]  = "Water";
  1237.    %Environment_Item[4]  = "Terrain";
  1238.    %Environment_Item[5]  = "AudioEmitter";
  1239.    %Environment_Item[6]  = "Precipitation";
  1240.    %Environment_Item[7]  = "ParticleEmitter";
  1241.    %Environment_Item[8]  = "fxSunLight";
  1242.    %Environment_Item[9]  = "fxShapeReplicator";
  1243.    %Environment_Item[10] = "fxFoliageReplicator";
  1244.    %Environment_Item[11] = "fxLight";
  1245.    
  1246.    %Mission_Item[0] = "MissionArea";
  1247.    %Mission_Item[1] = "Path";
  1248.    %Mission_Item[2] = "PathMarker";
  1249.    %Mission_Item[3] = "Trigger";
  1250.    %Mission_Item[4] = "PhysicalZone";
  1251.    %Mission_Item[5] = "Camera";
  1252.    //%Mission_Item[5] = "GameType";
  1253.    //%Mission_Item[6] = "Forcefield";
  1254.  
  1255.    %System_Item[0] = "SimGroup";
  1256.  
  1257.    //%AI_Item[0] = "Objective";
  1258.    //%AI_Item[1] = "NavigationGraph";
  1259.  
  1260.    // objects group
  1261.    %base = %this.insertItem(0, "Mission Objects");
  1262.  
  1263.    // create 'em
  1264.    for(%i = 0; %objGroup[%i] !$= ""; %i++)
  1265.    {
  1266.       %grp = %this.insertItem(%base, %objGroup[%i]);
  1267.  
  1268.       %groupTag = "%" @ %objGroup[%i] @ "_Item";
  1269.  
  1270.       %done = false;
  1271.       for(%j = 0; !%done; %j++)
  1272.       {
  1273.          eval("%itemTag = " @ %groupTag @ %j @ ";");
  1274.          
  1275.          if(%itemTag $= "")
  1276.             %done = true;
  1277.          else 
  1278.          {
  1279.             //echo("itemTag:" @ %itemTag @ " j:" @ %j);
  1280.             %this.insertItem(%grp, %itemTag, "ObjectBuilderGui.build" @ %itemTag @ "();",%itemTag);
  1281.          }
  1282.       }
  1283.    }
  1284. }
  1285.  
  1286. function createInterior(%name)
  1287. {
  1288.    %obj = new InteriorInstance()
  1289.    {
  1290.       position = "0 0 0";
  1291.       rotation = "0 0 0";
  1292.       interiorFile = %name;
  1293.    };
  1294.    
  1295.    return(%obj);
  1296. }
  1297.  
  1298. //function Creator::onAction(%this)
  1299. //{
  1300. //   %this.currentSel = -1;
  1301. //   %this.currentRoot = -1;
  1302. //   %this.currentObj = -1;
  1303.  
  1304.   // %sel = %this.getSelected();
  1305.   // if(%sel == -1 || %this.isGroup(%sel) || !$missionRunning)
  1306.   //    return;
  1307.       
  1308.    // the value is the callback function..
  1309.   // if(%this.getValue(%sel) $= "")
  1310.   //    return;
  1311.  
  1312. //   %this.currentSel = %sel;
  1313. //   %this.currentRoot = %this.getRootGroup(%sel);
  1314.  
  1315.   // %this.create(%sel);
  1316. //}
  1317.  
  1318. function WorldEditor::onAddSelected(%this,%obj)
  1319. {
  1320.    EditorTree.addSelection(%obj);
  1321. }
  1322.  
  1323. function Creator::onSelect(%this)
  1324. {
  1325.    Creator.clearSelection();
  1326. }
  1327.  
  1328. function Creator::onInspect(%this,%obj)
  1329. {
  1330.    if(!$missionRunning)
  1331.       return;
  1332.       
  1333.    %objId = eval(%this.getItemValue(%obj));
  1334.       
  1335.    // drop it from the editor - only SceneObjects can be selected...   
  1336.    Creator.removeSelection(%obj);
  1337.    
  1338.    EditorTree.clearSelection();
  1339.    EWorldEditor.clearSelection();
  1340.    EWorldEditor.selectObject(%objId);
  1341.    EWorldEditor.dropSelection();
  1342. }
  1343.  
  1344. function Creator::create(%this, %sel)
  1345. {
  1346.    // create the obj and add to the instant group
  1347.    %obj = eval(%this.getItemValue(%sel));
  1348.  
  1349.    if(%obj == -1)
  1350.       return;
  1351.  
  1352. //   %this.currentObj = %obj;
  1353.  
  1354.    $InstantGroup.add(%obj);
  1355.       
  1356.    // drop it from the editor - only SceneObjects can be selected...
  1357.    EWorldEditor.clearSelection();
  1358.    EWorldEditor.selectObject(%obj);
  1359.    EWorldEditor.dropSelection();
  1360. }
  1361.  
  1362.  
  1363. function TSStatic::create(%shapeName)
  1364. {
  1365.    %obj = new TSStatic() 
  1366.    {
  1367.       shapeName = %shapeName;
  1368.    };
  1369.    return(%obj);
  1370. }
  1371.  
  1372. function TSStatic::damage(%this)
  1373. {
  1374.    // prevent console error spam
  1375. }
  1376.  
  1377.  
  1378. //function Creator::getRootGroup(%sel)
  1379. //{
  1380. //   if(%sel == -1 || %sel == 0)
  1381. //      return(-1);
  1382. //
  1383. //   %parent = %this.getParent(%sel);
  1384. //   while(%parent != 0 || %parent != -1)
  1385. //   {
  1386. //      %sel = %parent;
  1387. //      %parent = %this.getParent(%sel);
  1388. //   }
  1389. //
  1390. //   return(%sel);
  1391. //}
  1392. //   
  1393. //function Creator::getLastItem(%rootGroup)
  1394. //{
  1395. //   %traverse = %rootGroup + 1;
  1396. //   while(%this.getRootGroup(%traverse) == %rootGroup)
  1397. //      %traverse++;
  1398. //   return(%traverse - 1);
  1399. //}
  1400. //
  1401. //function Creator::createNext(%this)
  1402. //{
  1403. //   if(%this.currentSel == -1 || %this.currentRoot == -1 || %this.currentObj == -1)
  1404. //      return;
  1405. //
  1406. //   %sel = %this.currentSel;
  1407. //   %this.currentSel++;
  1408. //
  1409. //   while(%this.currentSel != %sel)
  1410. //   {
  1411. //      if(%this.getRootGroup(%this.currentSel) != %this.currentRoot)
  1412. //         %this.currentSel = %this.currentRoot + 1;
  1413. //
  1414. //      if(%this.isGroup(%this.currentSel))
  1415. //         %this.currentSel++;
  1416. //      else
  1417. //         %sel = %this.currentSel;
  1418. //   }
  1419. //
  1420. //   //
  1421. //   %this.currentObj.delete();
  1422. //   %this.create(%sel);
  1423. //}
  1424. //
  1425. //function Creator::createPrevious(%this)
  1426. //{
  1427. //   if(%this.currentSel == -1 || %this.currentGroup == -1 || %this.currentObj == -1)
  1428. //      return;
  1429. //
  1430. //   %sel = %this.currentSel;
  1431. //   %this.currentSel--;
  1432. //
  1433. //   while(%this.currentSel != %sel)
  1434. //   {
  1435. //      if(%this.getRootGroup(%this.currentSel) != %this.currentRoot)
  1436. //         %this.currentSel = getLastItem(%this.currentRoot);
  1437. //
  1438. //      if(%this.isGroup(%this.currentSel))
  1439. //         %this.currentSel--;
  1440. //      else
  1441. //         %sel = %this.currentSel;
  1442. //   }
  1443. //
  1444. //   //
  1445. //   %this.currentObj.delete();
  1446. //   %this.create(%sel);
  1447. //}
  1448.  
  1449.  
  1450. function TerraformerGui::init(%this)
  1451. {
  1452.    TerraformerHeightfieldGui.init();
  1453.    TerraformerTextureGui.init();
  1454. }
  1455.  
  1456. function TerraformerGui::onWake(%this)
  1457. {
  1458.    // Only the canvas level gui's get wakes, so udpate manually.
  1459.    TerraformerTextureGui.update();
  1460. }
  1461.  
  1462. function TerraformerGui::onSleep(%this)
  1463. {
  1464.    %this.setPrefs();
  1465. }
  1466.  
  1467. $nextTextureId = 1;
  1468. $nextTextureRegister = 1000;
  1469. $selectedMaterial = -1;
  1470. $selectedTextureOperation = -1;
  1471. $TerraformerTextureDir = "common/editor/textureScripts";
  1472.  
  1473. //--------------------------------------
  1474.  
  1475. function TextureInit()
  1476. {
  1477.    // Assumes the terrain object is called terrain
  1478.    
  1479.    Texture_operation_menu.clear();
  1480.    Texture_operation_menu.setText("Placement Operations");
  1481.    Texture_operation_menu.add("Place by Fractal", 1);
  1482.    Texture_operation_menu.add("Place by Height", 2);
  1483.    Texture_operation_menu.add("Place by Slope", 3);
  1484.    Texture_operation_menu.add("Place by Water Level", 4);
  1485.  
  1486.    $HeightfieldSrcRegister = Heightfield_operation.rowCount()-1;
  1487.  
  1488.    // sync up the preview windows
  1489.    TexturePreview.setValue(HeightfieldPreview.getValue());
  1490.    %script = terrain.getTextureScript();
  1491.    if(%script !$= "")
  1492.       Texture::loadFromScript(%script);
  1493.  
  1494.    if (Texture_material.rowCount() == 0)
  1495.    {
  1496.       Texture_operation.clear();
  1497.       $nextTextureRegister = 1000;
  1498.    }
  1499.    else
  1500.    {
  1501.       // it's difficult to tell if the heightfield was modified so 
  1502.       // just in case flag all dependent operations as dirty.
  1503.       %rowCount = Texture_material.rowCount();
  1504.       for (%row = 0; %row < %rowCount; %row++)
  1505.       {
  1506.          %data = Texture_material.getRowText(%row);
  1507.          %entry= getRecord(%data,0);
  1508.          %reg  = getField(%entry,1);
  1509.          $dirtyTexture[ %reg ] = true;
  1510.  
  1511.          %opCount = getRecordCount(%data);
  1512.          for (%op = 2; %op < %opCount; %op++)
  1513.          {
  1514.             %entry= getRecord(%data,%op);
  1515.             %label= getField(%entry,0);
  1516.             if (%label !$= "Place by Fractal" && %label !$= "Fractal Distortion")
  1517.             {
  1518.                %reg  = getField(%entry,2);
  1519.                $dirtyTexture[ %reg ] = true;
  1520.             }
  1521.          }
  1522.       }
  1523.       Texture::previewMaterial();   
  1524.    }
  1525. }
  1526.  
  1527. function TerraformerTextureGui::refresh(%this)
  1528. {
  1529. }   
  1530.  
  1531.  
  1532. //--------------------------------------
  1533. function Texture_material_menu::onSelect(%this, %id, %text)
  1534. {
  1535.    %this.setText("Materials");
  1536.  
  1537.    // FORMAT
  1538.    //   material name
  1539.    //   register
  1540.    //     operation
  1541.    //       name
  1542.    //       tab name
  1543.    //       register
  1544.    //       distortion register
  1545.    //       {field,value}, ...
  1546.    //     operation
  1547.    //       ...
  1548.    Texture::saveMaterial();
  1549.    Texture::hideTab();   
  1550.    %id = Texture::addMaterial(%text @ "\t" @ $nextTextureRegister++);
  1551.    
  1552.    if (%id != -1)
  1553.    {
  1554.       Texture_material.setSelectedById(%id);
  1555.       Texture::addOperation("Fractal Distortion\ttab_DistortMask\t" @ $nextTextureRegister++ @ "\t0\tdmask_interval\t20\tdmask_rough\t0\tdmask_seed\t" @ terraFormer.generateSeed() @ "\tdmask_filter\t0.00000 0.00000 0.13750 0.487500 0.86250 1.00000 1.00000");
  1556.    }
  1557. }   
  1558.  
  1559.  
  1560. function Texture::addMaterialTexture()
  1561. {
  1562.    %root = filePath(terrain.terrainFile);
  1563.    getLoadFilename("*.png\t*.jpg", addLoadedMaterial);
  1564. }
  1565.  
  1566. function addLoadedMaterial(%file)
  1567. {
  1568.    Texture::saveMaterial();
  1569.    Texture::hideTab();   
  1570.    %text = filePath(%file) @ "/" @ fileBase(%file);
  1571.    %id = Texture::addMaterial(%text @ "\t" @ $nextTextureRegister++);
  1572.    if (%id != -1)
  1573.    {
  1574.       Texture_material.setSelectedById(%id);
  1575.       Texture::addOperation("Fractal Distortion\ttab_DistortMask\t" @ $nextTextureRegister++ @ "\t0\tdmask_interval\t20\tdmask_rough\t0\tdmask_seed\t" @ terraFormer.generateSeed() @ "\tdmask_filter\t0.00000 0.00000 0.13750 0.487500 0.86250 1.00000 1.00000");
  1576.    }
  1577.    Texture::save();
  1578. }
  1579.  
  1580. //--------------------------------------
  1581. function Texture_material::onSelect(%this, %id, %text) 
  1582. {
  1583.    Texture::saveMaterial();
  1584.    if (%id != $selectedMaterial)
  1585.    {
  1586.       $selectedTextureOperation = -1;
  1587.       Texture_operation.clear();
  1588.       
  1589.       Texture::hideTab();
  1590.       Texture::restoreMaterial(%id);
  1591.    }
  1592.  
  1593.    %matName = getField(%text, 0);
  1594.    ETerrainEditor.paintMaterial = %matName;
  1595.  
  1596.    Texture::previewMaterial(%id);
  1597.    $selectedMaterial = %id;
  1598.    $selectedTextureOperation = -1;
  1599.    Texture_operation.clearSelection();
  1600. }   
  1601.  
  1602.  
  1603. //--------------------------------------
  1604. function Texture_operation_menu::onSelect(%this, %id, %text)
  1605. {
  1606.    %this.setText("Placement Operations");
  1607.    %id = -1;
  1608.  
  1609.    if ($selectedMaterial == -1)
  1610.       return;
  1611.  
  1612.    %dreg = getField(Texture_operation.getRowText(0),2);
  1613.                      
  1614.    switch$ (%text)
  1615.    {
  1616.       case "Place by Fractal":
  1617.          %id = Texture::addOperation("Place by Fractal\ttab_FractalMask\t" @ $nextTextureRegister++ @ "\t" @ %dreg @ "\tfbmmask_interval\t16\tfbmmask_rough\t0.000\tfbmmask_seed\t" @ terraFormer.generateSeed() @ "\tfbmmask_filter\t0.000000 0.166667 0.333333 0.500000 0.666667 0.833333 1.000000\tfBmDistort\ttrue");
  1618.  
  1619.       case "Place by Height":
  1620.          %id = Texture::addOperation("Place by Height\ttab_HeightMask\t" @ $nextTextureRegister++ @ "\t" @ %dreg @ "\ttextureHeightFilter\t0 0.2 0.4 0.6 0.8 1.0\theightDistort\ttrue");
  1621.  
  1622.       case "Place by Slope":
  1623.          %id = Texture::addOperation("Place by Slope\ttab_SlopeMask\t" @ $nextTextureRegister++ @ "\t" @ %dreg @ "\ttextureSlopeFilter\t0 0.2 0.4 0.6 0.8 1.0\tslopeDistort\ttrue");
  1624.  
  1625.       case "Place by Water Level":
  1626.          %id = Texture::addOperation("Place by Water Level\ttab_WaterMask\t" @ $nextTextureRegister++ @ "\t" @ %dreg @ "\twaterDistort\ttrue");
  1627.    }
  1628.  
  1629.    // select it
  1630.    Texture::hideTab();
  1631.    if (%id != -1)
  1632.       Texture_operation.setSelectedById(%id);
  1633. }   
  1634.  
  1635.  
  1636. //--------------------------------------
  1637. function Texture_operation::onSelect(%this, %id, %text)
  1638. {
  1639.    Texture::saveOperation();   
  1640.    if (%id !$= $selectedTextureOperation)   
  1641.    {
  1642.       Texture::hideTab();   
  1643.       Texture::restoreOperation(%id);
  1644.       Texture::showTab(%id);   
  1645.    }
  1646.  
  1647.    Texture::previewOperation(%id);
  1648.    $selectedTextureOperation = %id;
  1649. }   
  1650.  
  1651.  
  1652. //--------------------------------------
  1653. function Texture::deleteMaterial(%id)
  1654. {
  1655.    if (%id $= "")
  1656.       %id = $selectedMaterial;
  1657.    if (%id == -1)   
  1658.       return;
  1659.    
  1660.    %row = Texture_material.getRowNumById(%id);
  1661.    
  1662.    Texture_material.removeRow(%row);
  1663.  
  1664.    // find the next row to select
  1665.    %rowCount = Texture_material.rowCount()-1;
  1666.    if (%row > %rowCount)
  1667.       %row = %rowCount;
  1668.  
  1669.    if (%id == $selectedMaterial)
  1670.       $selectedMaterial = -1;
  1671.  
  1672.    Texture_operation.clear();
  1673.    %id = Texture_material.getRowId(%row);
  1674.    Texture_material.setSelectedById(%id);
  1675.    Texture::save();
  1676. }   
  1677.  
  1678.  
  1679. //--------------------------------------
  1680. function Texture::deleteOperation(%id)
  1681. {
  1682.    if (%id $= "")
  1683.       %id = $selectedTextureOperation;
  1684.    if (%id == -1)   
  1685.       return;
  1686.    
  1687.    %row = Texture_operation.getRowNumById(%id);
  1688.    
  1689.    // don't delete the first entry
  1690.    if (%row == 0)
  1691.       return;
  1692.    
  1693.    Texture_operation.removeRow(%row);
  1694.  
  1695.    // find the next row to select
  1696.    %rowCount = Texture_operation.rowCount()-1;
  1697.    if (%row > %rowCount)
  1698.       %row = %rowCount;
  1699.  
  1700.    if (%id == $selectedTextureOperation)
  1701.       $selectedTextureOperation = -1;
  1702.  
  1703.    %id = Texture_operation.getRowId(%row);
  1704.    Texture_operation.setSelectedById(%id);
  1705.    Texture::save();
  1706. }   
  1707.  
  1708.  
  1709. //--------------------------------------
  1710. function Texture::applyMaterials()
  1711. {
  1712.    Texture::saveMaterial();
  1713.    %count = Texture_material.rowCount();
  1714.    if (%count > 0)   
  1715.    {
  1716.       %data = getRecord(Texture_material.getRowText(0),0);
  1717.       %mat_list = getField( %data, 0);
  1718.       %reg_list = getField( %data, 1);
  1719.       Texture::evalMaterial(Texture_material.getRowId(0));
  1720.       
  1721.       for (%i=1; %i<%count; %i++)
  1722.       {
  1723.          Texture::evalMaterial(Texture_material.getRowId(%i));
  1724.          %data = getRecord(Texture_material.getRowText(%i),0);
  1725.          %mat_list = %mat_list @ " " @ getField( %data, 0);
  1726.          %reg_list = %reg_list @ " " @ getField( %data, 1);
  1727.       }
  1728.       terraformer.setMaterials(%reg_list, %mat_list);
  1729.    }
  1730. }   
  1731.  
  1732.  
  1733. //--------------------------------------
  1734. function Texture::previewMaterial(%id)
  1735. {
  1736.    if (%id $= "")
  1737.       %id = $selectedMaterial;
  1738.    if (%id == -1)
  1739.       return;
  1740.  
  1741.    %data = Texture_material.getRowTextById(%id);
  1742.    %row  = Texture_material.getRowNumById(%id);
  1743.    %reg  = getField(getRecord(%data,0),1);
  1744.  
  1745.    Texture::evalMaterial(%id);
  1746.  
  1747.    terraformer.preview(TexturePreview, %reg);
  1748. }   
  1749.  
  1750.  
  1751. //--------------------------------------
  1752. function Texture::evalMaterial(%id)
  1753. {
  1754.    if (%id $= "")
  1755.       %id = $selectedMaterial;
  1756.    if (%id == -1)   
  1757.       return;
  1758.  
  1759.    %data = Texture_material.getRowTextbyId(%id);
  1760.    %reg  = getField(getRecord(%data,0), 1);
  1761.  
  1762.    // make sure all operation on this material are up to date
  1763.    // and accumulate register data for each
  1764.    %opCount = getRecordCount(%data);
  1765.    if (%opCount >= 2)    // record0=material record1=fractal
  1766.    {
  1767.       %entry = getRecord(%data, 1);
  1768.       Texture::evalOperationData(%entry, 1);
  1769.       for (%op=2; %op<%opCount; %op++)
  1770.       {
  1771.          %entry = getRecord(%data, %op);
  1772.          %reg_list = %reg_list @ getField(%entry, 2) @ " ";
  1773.          Texture::evalOperationData(%entry, %op);
  1774.       }
  1775.       // merge the masks in to the dst reg
  1776.       terraformer.mergeMasks(%reg_list, %reg);
  1777.    }
  1778.    Texture::save();
  1779. }   
  1780.  
  1781.  
  1782. //--------------------------------------
  1783. function Texture::evalOperation(%id)
  1784. {
  1785.    if (%id $= "")
  1786.       %id = $selectedTextureOperation;
  1787.    if (%id == -1)   
  1788.       return;
  1789.    
  1790.    %data   = Texture_operation.getRowTextById(%id);
  1791.    %row    = Texture_operation.getRowNumById(%id);
  1792.    
  1793.    if (%row != 0)    
  1794.       Texture::evalOperation( Texture_operation.getRowId(0) );
  1795.  
  1796.    Texture::evalOperationData(%data, %row);
  1797.    Texture::save();
  1798. }   
  1799.  
  1800.  
  1801. //--------------------------------------
  1802. function Texture::evalOperationData(%data, %row)
  1803. {
  1804.    %label  = getField(%data, 0);
  1805.    %reg    = getField(%data, 2);
  1806.    %dreg   = getField(%data, 3);
  1807.    %id     = Texture_material.getRowId(%row);
  1808.  
  1809.    if ( $dirtyTexture[%reg] == false )
  1810.    {
  1811.       return;
  1812.    }
  1813.  
  1814.    switch$ (%label)
  1815.    {
  1816.       case "Fractal Distortion":
  1817.          terraformer.maskFBm( %reg, getField(%data,5), getField(%data,7), getField(%data,9), getField(%data,11), false, 0 );
  1818.  
  1819.       case "Place by Fractal":
  1820.          terraformer.maskFBm( %reg, getField(%data,5), getField(%data,7), getField(%data,9), getField(%data,11), getField(%data,13), %dreg );
  1821.  
  1822.       case "Place by Height":
  1823.          terraformer.maskHeight( $HeightfieldSrcRegister, %reg, getField(%data,5), getField(%data,7), %dreg );
  1824.  
  1825.       case "Place by Slope":
  1826.          terraformer.maskSlope( $HeightfieldSrcRegister, %reg, getField(%data,5), getField(%data,7), %dreg );
  1827.  
  1828.       case "Place by Water Level":
  1829.          terraformer.maskWater( $HeightfieldSrcRegister, %reg, getField(%data,5), %dreg );
  1830.    }
  1831.  
  1832.  
  1833.    $dirtyTexture[%reg] = false;
  1834. }   
  1835.  
  1836.  
  1837.  
  1838. //--------------------------------------
  1839. function Texture::previewOperation(%id)
  1840. {
  1841.    if (%id $= "")
  1842.       %id = $selectedTextureOperation;
  1843.    if (%id == -1)
  1844.       return;
  1845.  
  1846.    %row  = Texture_operation.getRowNumById(%id);
  1847.    %data = Texture_operation.getRowText(%row);
  1848.    %reg  = getField(%data,2);
  1849.  
  1850.    Texture::evalOperation(%id);
  1851.    terraformer.preview(TexturePreview, %reg);
  1852. }   
  1853.  
  1854.  
  1855.  
  1856. //--------------------------------------
  1857. function Texture::restoreMaterial(%id)
  1858. {
  1859.    if (%id == -1)
  1860.       return;
  1861.  
  1862.    %data = Texture_material.getRowTextById(%id);
  1863.    
  1864.    Texture_operation.clear();
  1865.    %recordCount = getRecordCount(%data);
  1866.    for (%record=1; %record<%recordCount; %record++)
  1867.    {
  1868.       %entry = getRecord(%data, %record);
  1869.       Texture_operation.addRow($nextTextureId++, %entry);
  1870.    }
  1871. }   
  1872.  
  1873.  
  1874. //--------------------------------------
  1875. function Texture::saveMaterial()
  1876. {
  1877.    %id = $selectedMaterial;
  1878.    if (%id == -1)
  1879.       return;
  1880.  
  1881.    Texture::SaveOperation();
  1882.    %data = Texture_Material.getRowTextById(%id);
  1883.    %newData = getRecord(%data,0);
  1884.  
  1885.    %rowCount = Texture_Operation.rowCount();
  1886.    for (%row=0; %row<%rowCount; %row++)
  1887.       %newdata = %newdata @ "\n" @ Texture_Operation.getRowText(%row);
  1888.  
  1889.    Texture_Material.setRowById(%id, %newdata);
  1890.    Texture::save();
  1891. }   
  1892.  
  1893.  
  1894. //--------------------------------------
  1895. function Texture::restoreOperation(%id)
  1896. {
  1897.    if (%id == -1)
  1898.       return;
  1899.  
  1900.    %data = Texture_operation.getRowTextById(%id);
  1901.  
  1902.    %fieldCount = getFieldCount(%data);
  1903.    for (%field=4; %field<%fieldCount; %field += 2)
  1904.    {
  1905.       %obj = getField(%data, %field);
  1906.       %obj.setValue( getField(%data, %field+1) );
  1907.    }
  1908.    Texture::save();
  1909. }   
  1910.  
  1911.  
  1912. //--------------------------------------
  1913. function Texture::saveOperation()
  1914. {
  1915.    %id = $selectedTextureOperation;
  1916.    if (%id == -1)
  1917.       return;
  1918.  
  1919.    %data = Texture_operation.getRowTextById(%id);
  1920.    %newData = getField(%data,0) @ "\t" @ getField(%data,1) @ "\t" @ getField(%data,2) @ "\t" @ getField(%data,3); 
  1921.  
  1922.    // go through each object and update its value
  1923.    %fieldCount = getFieldCount(%data);
  1924.    for (%field=4; %field<%fieldCount; %field += 2)
  1925.    {
  1926.       %obj = getField(%data, %field);
  1927.       %newdata = %newdata @ "\t" @ %obj @ "\t" @ %obj.getValue();
  1928.    }
  1929.  
  1930.    %dirty = (%data !$= %newdata);
  1931.    %reg   = getField(%data, 2);
  1932.    $dirtyTexture[%reg] = %dirty;
  1933.       
  1934.    Texture_operation.setRowById(%id, %newdata);
  1935.  
  1936.    // mark the material register as dirty too
  1937.    if (%dirty == true)
  1938.    {
  1939.       %data = Texture_Material.getRowTextById($selectedMaterial);
  1940.       %reg  = getField(getRecord(%data,0), 1);
  1941.       $dirtyTexture[ %reg ] = true;
  1942.    }
  1943.  
  1944.    // if row is zero the fractal mask was modified
  1945.    // mark everything else in the list as dirty
  1946.     %row = Texture_material.getRowNumById(%id);
  1947.     if (%row == 0)
  1948.     {
  1949.        %rowCount = Texture_operation.rowCount();
  1950.        for (%r=1; %r<%rowCount; %r++)
  1951.        {
  1952.           %data = Texture_operation.getRowText(%r);
  1953.           $dirtyTexture[ getField(%data,2) ] = true;
  1954.        }
  1955.    }
  1956.    Texture::save();
  1957. }   
  1958.  
  1959.  
  1960. //--------------------------------------
  1961. function Texture::addMaterial(%entry)
  1962. {
  1963.    %id = $nextTextureId++;
  1964.    Texture_material.addRow(%id, %entry);
  1965.    
  1966.    %reg = getField(%entry, 1);
  1967.    $dirtyTexture[%reg] = true;
  1968.  
  1969.    Texture::save();
  1970.    return %id;
  1971. }   
  1972.  
  1973. //--------------------------------------
  1974. function Texture::addOperation(%entry)
  1975. {
  1976.    // Assumes: operation is being added to selected material
  1977.  
  1978.    %id = $nextTextureId++;
  1979.    Texture_operation.addRow(%id, %entry);
  1980.  
  1981.    %reg = getField(%entry, 2);
  1982.    $dirtyTexture[%reg] = true;
  1983.  
  1984.    Texture::save();
  1985.    return %id;
  1986. }   
  1987.  
  1988.  
  1989. //--------------------------------------
  1990. function Texture::save()
  1991. {
  1992.    %script = "";
  1993.  
  1994.    // loop through each operation and save it to disk
  1995.    %rowCount = Texture_material.rowCount();
  1996.    for(%row = 0; %row < %rowCount; %row++)
  1997.    {
  1998.       if(%row != 0)
  1999.          %script = %script @ "\n";
  2000.       %data = expandEscape(Texture_material.getRowText(%row));
  2001.       %script = %script @ %data;
  2002.    }
  2003.    terrain.setTextureScript(%script);
  2004.    ETerrainEditor.isDirty = true;
  2005. }
  2006.  
  2007. //--------------------------------------
  2008. function Texture::import()
  2009. {
  2010.    getLoadFilename("*.ter", "Texture::doLoadTexture");
  2011. }   
  2012.  
  2013. function Texture::loadFromScript(%script)
  2014. {
  2015.    Texture_material.clear();
  2016.    Texture_operation.clear();
  2017.    $selectedMaterial = -1;
  2018.    $selectedTextureOperation = -1;
  2019.  
  2020.    %i = 0;
  2021.    for(%rec = getRecord(%script, %i); %rec !$= ""; %rec = getRecord(%script, %i++))
  2022.       Texture::addMaterial(collapseEscape(%rec));
  2023.    // initialize dirty register array
  2024.    // patch up register usage
  2025.    // ...and deterime what the next register should be.
  2026.    $nextTextureRegister = 1000;
  2027.    %rowCount = Texture_material.rowCount();
  2028.    for (%row = 0; %row < %rowCount; %row++)
  2029.    {
  2030.       $dirtyTexture[ $nextTextureRegister ] = true;
  2031.       %data    = Texture_material.getRowText(%row);
  2032.       %rec     = getRecord(%data, 0);
  2033.       %rec     = setField(%rec, 1, $nextTextureRegister);
  2034.       %data    = setRecord(%data, 0, %rec);
  2035.       $nextTextureRegister++;
  2036.       
  2037.       %opCount = getRecordCount(%data);
  2038.       for (%op = 1; %op < %opCount; %op++)
  2039.       {
  2040.          if (%op == 1)
  2041.             %frac_reg = $nextTextureRegister;
  2042.          $dirtyTexture[ $nextTextureRegister ] = true;
  2043.          %rec  = getRecord(%data,%op);
  2044.          %rec  = setField(%rec, 2, $nextTextureRegister);
  2045.          %rec  = setField(%rec, 3, %frac_reg);
  2046.          %data = setRecord(%data, %op, %rec);
  2047.          $nextTextureRegister++;
  2048.       }
  2049.       %id = Texture_material.getRowId(%row);
  2050.       Texture_material.setRowById(%id, %data);
  2051.    }
  2052.  
  2053.    $selectedMaterial = -1;
  2054.    Texture_material.setSelectedById(Texture_material.getRowId(0));
  2055. }
  2056.  
  2057. //--------------------------------------
  2058. function Texture::doLoadTexture(%name)
  2059. {
  2060.    // ok, we're getting a terrain file...
  2061.    %newTerr = new TerrainBlock() // unnamed - since we'll be deleting it shortly:
  2062.    {
  2063.       position = "0 0 0";
  2064.       terrainFile = %name;
  2065.       squareSize = 8;
  2066.       visibleDistance = 100;
  2067.    };
  2068.    if(isObject(%newTerr))
  2069.    {
  2070.       %script = %newTerr.getTextureScript();
  2071.       if(%script !$= "")
  2072.          Texture::loadFromScript(%script);
  2073.       %newTerr.delete();
  2074.    }   
  2075. }   
  2076.  
  2077.  
  2078.  
  2079. //--------------------------------------
  2080. function Texture::hideTab()
  2081. {
  2082.    tab_DistortMask.setVisible(false);   
  2083.    tab_FractalMask.setVisible(false);   
  2084.    tab_HeightMask.setVisible(false);   
  2085.    tab_SlopeMask.setVisible(false);   
  2086.    tab_waterMask.setVisible(false);   
  2087. }   
  2088.  
  2089.  
  2090. //--------------------------------------
  2091. function Texture::showTab(%id)
  2092. {
  2093.    Texture::hideTab();
  2094.    %data = Texture_operation.getRowTextById(%id);
  2095.    %tab  = getField(%data,1);
  2096.    %tab.setVisible(true);
  2097. }   
  2098.  
  2099.  
  2100.  
  2101. $TerraformerHeightfieldDir = "c/editor/heightScripts";
  2102.  
  2103. function tab_Blend::reset(%this)
  2104. {
  2105.    blend_option.clear();
  2106.    blend_option.add("Add", 0);
  2107.    blend_option.add("Subtract", 1);
  2108.    blend_option.add("Max", 2);
  2109.    blend_option.add("Min", 3);
  2110.    blend_option.add("Multiply", 4);
  2111. }
  2112.  
  2113. function tab_fBm::reset(%this)
  2114. {
  2115.    fBm_detail.clear();
  2116.    fBm_detail.add("Very Low", 0);
  2117.    fBm_detail.add("Low", 1);
  2118.    fBm_detail.add("Normal", 2);
  2119.    fBm_detail.add("High", 3);
  2120.    fBm_detail.add("Very High", 4);
  2121. }
  2122.  
  2123. function tab_RMF::reset(%this)
  2124. {
  2125.    rmf_detail.clear();
  2126.    rmf_detail.add("Very Low", 0);
  2127.    rmf_detail.add("Low", 1);
  2128.    rmf_detail.add("Normal", 2);
  2129.    rmf_detail.add("High", 3);
  2130.    rmf_detail.add("Very High", 4);
  2131. }
  2132.  
  2133. function tab_terrainFile::reset(%this)
  2134. {
  2135.    // update tab controls..
  2136.    terrainFile_textList.clear();
  2137.  
  2138.    %filespec = $TerraformerHeightfieldDir @ "/*.ter";
  2139.  
  2140.    for(%file = findFirstFile(%filespec); %file !$= ""; %file = findNextFile(%filespec))
  2141.    {
  2142.       terrainFile_textList.addRow(%i++, fileBase(%file) @ fileExt(%file));
  2143.    }
  2144. }
  2145.  
  2146. function tab_canyon::reset()
  2147. {
  2148. }
  2149.  
  2150. function tab_smooth::reset()
  2151. {
  2152. }
  2153.  
  2154. function tab_smoothWater::reset()
  2155. {
  2156. }
  2157.  
  2158. function tab_smoothRidge::reset()
  2159. {
  2160. }
  2161.  
  2162. function tab_filter::reset()
  2163. {
  2164. }
  2165.  
  2166. function tab_turbulence::reset()
  2167. {
  2168. }
  2169.  
  2170. function tab_thermal::reset()
  2171. {
  2172. }
  2173.  
  2174. function tab_hydraulic::reset()
  2175. {
  2176. }
  2177.  
  2178. function tab_general::reset()
  2179. {
  2180. }
  2181.  
  2182. function tab_bitmap::reset()
  2183. {
  2184. }
  2185.  
  2186. function tab_sinus::reset()
  2187. {
  2188. }
  2189.  
  2190.  
  2191. //--------------------------------------
  2192.  
  2193. function Heightfield::resetTabs()
  2194. {
  2195.    tab_terrainFile.reset();
  2196.    tab_fbm.reset();
  2197.    tab_rmf.reset();
  2198.    tab_canyon.reset();
  2199.    tab_smooth.reset();
  2200.    tab_smoothWater.reset();
  2201.    tab_smoothRidge.reset();
  2202.    tab_filter.reset();
  2203.    tab_turbulence.reset();
  2204.    tab_thermal.reset();
  2205.    tab_hydraulic.reset();
  2206.    tab_general.reset();
  2207.    tab_bitmap.reset();
  2208.    tab_blend.reset();
  2209.    tab_sinus.reset();
  2210. }
  2211.  
  2212. //--------------------------------------
  2213. function TerraformerInit()
  2214. {
  2215.    Heightfield_options.clear();   
  2216.    Heightfield_options.setText("Operation");   
  2217.    Heightfield_options.add("fBm Fractal",0);
  2218.    Heightfield_options.add("Rigid MultiFractal",1);
  2219.    Heightfield_options.add("Canyon Fractal",2);
  2220.    Heightfield_options.add("Sinus",3);
  2221.    Heightfield_options.add("Bitmap",4);
  2222.    Heightfield_options.add("Turbulence",5);
  2223.    Heightfield_options.add("Smoothing",6);
  2224.    Heightfield_options.add("Smooth Water",7);
  2225.    Heightfield_options.add("Smooth Ridges/Valleys", 8);
  2226.    Heightfield_options.add("Filter",9);
  2227.    Heightfield_options.add("Thermal Erosion",10);
  2228.    Heightfield_options.add("Hydraulic Erosion",11);
  2229.    Heightfield_options.add("Blend",12);
  2230.    Heightfield_options.add("Terrain File",13);
  2231.  
  2232.    Heightfield::resetTabs();
  2233.  
  2234.    %script = Terrain.getHeightfieldScript();
  2235.    if(%script !$= "")
  2236.       Heightfield::loadFromScript(%script,true);
  2237.  
  2238.    if (Heightfield_operation.rowCount() == 0)
  2239.    {
  2240.       Heightfield_operation.clear();
  2241.       %id1 = Heightfield::add("General\tTab_general\tgeneral_min_height\t50\tgeneral_scale\t300\tgeneral_water\t0.000\tgeneral_centerx\t0\tgeneral_centery\t0");
  2242.       Heightfield_operation.setSelectedById(%id1);
  2243.    }
  2244.  
  2245.    Heightfield::resetTabs();
  2246.    Heightfield::preview();
  2247. }   
  2248.  
  2249. //--------------------------------------
  2250. function Heightfield_options::onSelect(%this, %_id, %text)
  2251. {
  2252.    Heightfield_options.setText("Operation");
  2253.    %id = -1;
  2254.  
  2255.    %rowCount = Heightfield_operation.rowCount();
  2256.  
  2257.    // FORMAT
  2258.    //  item name
  2259.    //  tab name
  2260.    //    control name 
  2261.    //    control value
  2262.    switch$(%text)
  2263.    {
  2264.       case "Terrain File":
  2265.          %id = HeightField::add("Terrain File\ttab_terrainFile\tterrainFile_terrFileText\tterrains/terr1.ter\tterrainFile_textList\tterr1.ter");
  2266.  
  2267.       case "fBm Fractal":
  2268.          %id = Heightfield::add("fBm Fractal\ttab_fBm\tfbm_interval\t9\tfbm_rough\t0.000\tfBm_detail\tNormal\tfBm_seed\t" @ terraformer.generateSeed());
  2269.  
  2270.       case "Rigid MultiFractal":
  2271.          %id = Heightfield::add("Rigid MultiFractal\ttab_RMF\trmf_interval\t4\trmf_rough\t0.000\trmf_detail\tNormal\trmf_seed\t" @ terraformer.generateSeed());
  2272.  
  2273.       case "Canyon Fractal":
  2274.          %id = Heightfield::add("Canyon Fractal\ttab_Canyon\tcanyon_freq\t5\tcanyon_factor\t0.500\tcanyon_seed\t" @ terraformer.generateSeed());
  2275.    
  2276.       case "Sinus":
  2277.          %id = Heightfield::add("Sinus\ttab_Sinus\tsinus_filter\t1 0.83333 0.6666 0.5 0.33333 0.16666 0\tsinus_seed\t" @ terraformer.generateSeed());
  2278.    
  2279.       case "Bitmap":
  2280.          %id = Heightfield::add("Bitmap\ttab_Bitmap\tbitmap_name\t");
  2281.          Heightfield::setBitmap();
  2282.    }
  2283.  
  2284.  
  2285.    if (Heightfield_operation.rowCount() >= 1)
  2286.    {
  2287.       switch$(%text)
  2288.       {
  2289.          case "Smoothing":
  2290.             %id = Heightfield::add("Smoothing\ttab_Smooth\tsmooth_factor\t0.500\tsmooth_iter\t0");
  2291.    
  2292.          case "Smooth Water":
  2293.             %id = Heightfield::add("Smooth Water\ttab_SmoothWater\twatersmooth_factor\t0.500\twatersmooth_iter\t0");
  2294.  
  2295.          case "Smooth Ridges/Valleys":
  2296.             %id = Heightfield::add("Smooth Ridges/Valleys\ttab_SmoothRidge\tridgesmooth_factor\t0.8500\tridgesmooth_iter\t1");
  2297.    
  2298.          case "Filter":
  2299.             %id = Heightfield::add("Filter\ttab_Filter\tfilter\t0 0.16666667 0.3333333 0.5 0.6666667 0.8333333 1");
  2300.    
  2301.          case "Turbulence":
  2302.             %id = Heightfield::add("Turbulence\ttab_Turbulence\tturbulence_factor\t0.250\tturbulence_radius\t10");
  2303.    
  2304.          case "Thermal Erosion":
  2305.             %id = Heightfield::add("Thermal Erosion\ttab_Thermal\tthermal_slope\t30\tthermal_cons\t80.0\tthermal_iter\t0");
  2306.    
  2307.          case "Hydraulic Erosion":
  2308.             %id = Heightfield::add("Hydraulic Erosion\ttab_Hydraulic\thydraulic_iter\t0\thydraulic_filter\t0 0.16666667 0.3333333 0.5 0.6666667 0.8333333 1");
  2309.       }
  2310.    }
  2311.  
  2312.    if (Heightfield_operation.rowCount() >= 2)
  2313.    {
  2314.       if("Blend" $= %text)
  2315.          %id = Heightfield::add("Blend\ttab_Blend\tblend_factor\t0.500\tblend_srcB\t" @ %rowCount-2 @"\tblend_option\tadd");
  2316.    }
  2317.  
  2318.    
  2319.    // select it
  2320.    if (%id != -1)
  2321.       Heightfield_operation.setSelectedById(%id);
  2322. }   
  2323.  
  2324.  
  2325. //--------------------------------------
  2326. function Heightfield::eval(%id)
  2327. {
  2328.    if (%id == -1)   
  2329.       return;
  2330.    
  2331.    %data  = restWords(Heightfield_operation.getRowTextById(%id));
  2332.    %label = getField(%data,0);
  2333.    %row   = Heightfield_operation.getRowNumById(%id);
  2334.     
  2335.    echo("Heightfield::eval:" @ %row @ "  " @ %label );
  2336.  
  2337.    switch$(%label)
  2338.    {
  2339.       case "General":
  2340.          if (Terrain.squareSize>0) %size = Terrain.squareSize;
  2341.          else %size = 8;
  2342.          terraformer.setTerrainInfo( 256, %size, getField(%data,3), getField(%data,5), getField(%data,7) );
  2343.          terraformer.setShift( getField(%data,9), getField(%data,11) );
  2344.          terraformer.terrainData(%row);
  2345.  
  2346.       case "Terrain File":
  2347.          terraformer.terrainFile(%row, getField(%data,3));
  2348.  
  2349.       case "fBm Fractal":
  2350.          terraformer.fBm( %row, getField(%data,3), getField(%data,5), getField(%data,7), getField(%data,9) );
  2351.  
  2352.       case "Sinus":
  2353.          terraformer.sinus( %row, getField(%data,3), getField(%data,5) );
  2354.     
  2355.       case "Rigid MultiFractal":
  2356.          terraformer.rigidMultiFractal( %row, getField(%data,3), getField(%data,5), getField(%data,7), getField(%data,9) );
  2357.     
  2358.       case "Canyon Fractal":
  2359.          terraformer.canyon( %row, getField(%data,3), getField(%data,5), getField(%data,7) );
  2360.    
  2361.       case "Smoothing":
  2362.          terraformer.smooth( %row-1, %row, getField(%data,3), getField(%data,5) );
  2363.        
  2364.       case "Smooth Water":
  2365.          terraformer.smoothWater( %row-1, %row, getField(%data,3), getField(%data,5) );
  2366.        
  2367.       case "Smooth Ridges/Valleys":
  2368.          terraformer.smoothRidges( %row-1, %row, getField(%data,3), getField(%data,5) );
  2369.  
  2370.       case "Filter":
  2371.          terraformer.filter( %row-1, %row, getField(%data,3) );
  2372.        
  2373.       case "Turbulence":
  2374.          terraformer.turbulence( %row-1, %row, getField(%data,3), getField(%data,5) );
  2375.        
  2376.       case "Thermal Erosion":
  2377.          terraformer.erodeThermal( %row-1, %row, getField(%data,3), getField(%data,5),getField(%data,7) );
  2378.        
  2379.       case "Hydraulic Erosion":
  2380.          terraformer.erodeHydraulic( %row-1, %row, getField(%data,3), getField(%data,5) );
  2381.        
  2382.       case "Bitmap":
  2383.          terraformer.loadGreyscale(%row, getField(%data,3));
  2384.  
  2385.       case "Blend":
  2386.          %rowCount = Heightfield_operation.rowCount();
  2387.          if(%rowCount > 2) 
  2388.          {
  2389.             %a = Heightfield_operation.getRowNumById(%id)-1;
  2390.             %b = getField(%data, 5);
  2391.             echo("Blend: " @ %data);
  2392.             echo("Blend: " @ getField(%data,3) @ "  " @ getField(%data,7));
  2393.             if(%a < %rowCount || %a > 0 || %b < %rowCount || %b > 0 ) 
  2394.                terraformer.blend(%a, %b, %row, getField(%data,3), getField(%data,7) );
  2395.             else
  2396.                echo("Heightfield Editor: Blend parameters out of range.");
  2397.          }
  2398.    }
  2399.  
  2400. }   
  2401.  
  2402. //--------------------------------------
  2403. function Heightfield::add(%entry)
  2404. {
  2405.    Heightfield::saveTab();
  2406.    Heightfield::hideTab();
  2407.  
  2408.    %id = $NextOperationId++;
  2409.    if ($selectedOperation != -1)
  2410.    {
  2411.       %row = Heightfield_operation.getRowNumById($selectedOperation) + 1;
  2412.       %entry = %row @ " " @ %entry;
  2413.       Heightfield_operation.addRow(%id, %entry, %row); // insert
  2414.  
  2415.       // adjust row numbers
  2416.       for(%i = %row+1; %i < Heightfield_operation.rowCount(); %i++)
  2417.       {
  2418.          %id = Heightfield_operation.getRowId(%i);
  2419.          %text = Heightfield_operation.getRowTextById(%id);
  2420.          %text = setWord(%text, 0, %i);
  2421.          Heightfield_operation.setRowById(%id, %text);
  2422.       }
  2423.    }
  2424.    else
  2425.    {
  2426.       %entry = Heightfield_operation.rowCount() @ " " @ %entry;
  2427.       Heightfield_operation.addRow(%id, %entry);   // add to end
  2428.    }
  2429.  
  2430.    %row = Heightfield_operation.getRowNumById(%id);
  2431.    if (%row <= $HeightfieldDirtyRow)
  2432.       $HeightfieldDirtyRow = %row;
  2433.    Heightfield::save();
  2434.    return %id;
  2435. }   
  2436.  
  2437.  
  2438. //--------------------------------------
  2439. function Heightfield::onDelete(%id)
  2440. {
  2441.    if (%id $= "")
  2442.       %id = $selectedOperation;
  2443.  
  2444.    %row = Heightfield_operation.getRowNumById(%id);
  2445.    
  2446.    // don't delete the first entry
  2447.    if (%row == 0)
  2448.       return;
  2449.  
  2450.    Heightfield_operation.removeRow(%row);
  2451.    
  2452.    // adjust row numbers
  2453.    for(%i = %row; %i < Heightfield_operation.rowCount(); %i++)
  2454.    {
  2455.       %id2 = Heightfield_operation.getRowId(%i);
  2456.       %text = Heightfield_operation.getRowTextById(%id2);
  2457.       %text = setWord(%text, 0, %i);
  2458.       Heightfield_operation.setRowById(%id2, %text);
  2459.    }
  2460.  
  2461.    // adjust the Dirty Row position
  2462.    if ($HeightfieldDirtyRow >= %row)
  2463.       $HeightfieldDirtyRow = %row;   
  2464.    
  2465.    // find the next row to select
  2466.    %rowCount = Heightfield_operation.rowCount()-1;
  2467.    if (%row > %rowCount)
  2468.       %row = %rowCount;
  2469.  
  2470.    if (%id == $selectedOperation)
  2471.       $selectedOperation = -1;
  2472.  
  2473.    %id = Heightfield_operation.getRowId(%row);
  2474.    Heightfield_operation.setSelectedById(%id);
  2475.    Heightfield::save();
  2476. }   
  2477.  
  2478.  
  2479. //--------------------------------------
  2480. function Heightfield_operation::onSelect(%this, %id, %text)
  2481. {
  2482.    Heightfield::saveTab();
  2483.    Heightfield::hideTab();
  2484.  
  2485.    $selectedOperation = %id; 
  2486.    Heightfield::restoreTab($selectedOperation);
  2487.    Heightfield::showTab($selectedOperation);
  2488.    Heightfield::preview($selectedOperation);
  2489. }   
  2490.  
  2491.  
  2492. //--------------------------------------
  2493. function Heightfield::restoreTab(%id)
  2494. {
  2495.    if (%id == -1)   
  2496.       return;
  2497.  
  2498.    Heightfield::hideTab();
  2499.    
  2500.    %data = restWords(Heightfield_operation.getRowTextById(%id));
  2501.  
  2502.    %fieldCount = getFieldCount(%data);
  2503.    for (%field=2; %field<%fieldCount; %field += 2)
  2504.    {
  2505.       %obj = getField(%data, %field);
  2506.       %obj.setValue( getField(%data, %field+1) );
  2507.    }
  2508.    Heightfield::save();
  2509. }  
  2510.  
  2511.  
  2512. //--------------------------------------
  2513. function Heightfield::saveTab()
  2514. {
  2515.    if ($selectedOperation == -1)   
  2516.       return;
  2517.    
  2518.    %data = Heightfield_operation.getRowTextById($selectedOperation);
  2519.  
  2520.    %rowNum = getWord(%data, 0);
  2521.    %data = restWords(%data);   
  2522.    %newdata = getField(%data,0) @ "\t" @ getField(%data,1);
  2523.  
  2524.    %fieldCount = getFieldCount(%data);
  2525.    for (%field=2; %field < %fieldCount; %field += 2)
  2526.    {
  2527.       %obj = getField(%data, %field);
  2528.       %newdata = %newdata @ "\t" @ %obj @ "\t" @ %obj.getValue();
  2529.    }
  2530.    // keep track of the top-most dirty operation
  2531.    // so we know who to evaluate later
  2532.    if (%data !$= %newdata)
  2533.    {
  2534.       %row = Heightfield_operation.getRowNumById($selectedOperation);
  2535.       if (%row <= $HeightfieldDirtyRow && %row > 0)
  2536.          $HeightfieldDirtyRow = %row;
  2537.    }
  2538.  
  2539.    Heightfield_operation.setRowById($selectedOperation, %rowNum @ " " @ %newdata);
  2540.    Heightfield::save();
  2541. }  
  2542.  
  2543.  
  2544. //--------------------------------------
  2545. function Heightfield::preview(%id)
  2546. {
  2547.    %rowCount = Heightfield_operation.rowCount();
  2548.    if (%id $= "")
  2549.       %id = Heightfield_operation.getRowId(%rowCount-1);
  2550.  
  2551.    %row = Heightfield_operation.getRowNumById(%id);
  2552.  
  2553.    Heightfield::refresh(%row);
  2554.    terraformer.previewScaled(HeightfieldPreview, %row);
  2555. }   
  2556.  
  2557.  
  2558. //--------------------------------------
  2559. function Heightfield::refresh(%last)
  2560. {
  2561.    if (%last $= "")   
  2562.       %last = Heightfield_operation.rowCount()-1;
  2563.  
  2564.    // always update the general info
  2565.    Heightfield::eval(Heightfield_operation.getRowId(0));
  2566.  
  2567.    for( 0; $HeightfieldDirtyRow<=%last; $HeightfieldDirtyRow++)
  2568.    {
  2569.       %id = Heightfield_operation.getRowId($HeightfieldDirtyRow);
  2570.       Heightfield::eval(%id);
  2571.    }
  2572.    Heightfield::save();
  2573. }   
  2574.  
  2575.  
  2576. //--------------------------------------
  2577. function Heightfield::apply(%id)
  2578. {
  2579.    %rowCount = Heightfield_operation.rowCount();
  2580.    if (%rowCount < 1)
  2581.       return;
  2582.    if (%id $= "")
  2583.       %id = Heightfield_operation.getRowId(%rowCount-1);
  2584.  
  2585.    %row = Heightfield_operation.getRowNumById(%id);
  2586.  
  2587.    HeightfieldPreview.setRoot();
  2588.    Heightfield::refresh(%row);
  2589.    terraformer.setTerrain(%row);
  2590.  
  2591.    terraformer.setCameraPosition(0,0,0); 
  2592.    ETerrainEditor.isDirty = true;
  2593. }   
  2594.  
  2595. //--------------------------------------
  2596. $TerraformerSaveRegister = 0;
  2597. function Heightfield::saveBitmap(%name)
  2598. {
  2599.    if(%name $= "")
  2600.       getSaveFilename("*.png", "Heightfield::doSaveBitmap",
  2601.          $TerraformerHeightfieldDir @ "/" @ fileBase($Client::MissionFile) @ ".png");
  2602.    else
  2603.       Heightfield::doSaveBitmap(%name);
  2604. }
  2605.  
  2606. function Heightfield::doSaveBitmap(%name)
  2607. {        
  2608.    terraformer.saveGreyscale($TerraformerSaveRegister, %name);
  2609. }
  2610.  
  2611. //--------------------------------------
  2612.  
  2613. function Heightfield::save()
  2614. {
  2615.    %script = "";
  2616.    %rowCount = Heightfield_operation.rowCount();
  2617.    for(%row = 0; %row < %rowCount; %row++)
  2618.    {
  2619.       if(%row != 0)
  2620.          %script = %script @ "\n";
  2621.       %data = restWords(Heightfield_operation.getRowText(%row));
  2622.       %script = %script @ expandEscape(%data);
  2623.    }
  2624.    terrain.setHeightfieldScript(%script);
  2625.    ETerrainEditor.isDirty = true;
  2626. }
  2627.  
  2628. //--------------------------------------
  2629. function Heightfield::import()
  2630. {
  2631.    getLoadFilename("*.ter", "Heightfield::doLoadHeightfield");
  2632. }   
  2633.  
  2634.  
  2635. //--------------------------------------
  2636. function Heightfield::loadFromScript(%script,%leaveCamera)
  2637. {
  2638.    echo(%script);
  2639.  
  2640.    Heightfield_operation.clear();
  2641.    $selectedOperation = -1;
  2642.    $HeightfieldDirtyRow = -1;
  2643.  
  2644.    // zero out all shifting
  2645.    HeightfieldPreview.reset();
  2646.  
  2647.    for(%rec = getRecord(%script, %i); %rec !$= ""; %rec = getRecord(%script, %i++))
  2648.       Heightfield::add(collapseEscape(%rec));
  2649.  
  2650.    if (Heightfield_operation.rowCount() == 0)
  2651.    {
  2652.       // if there was a problem executing the script restore
  2653.       // the operations list to a known state
  2654.       Heightfield_operation.clear();
  2655.       Heightfield::add("General\tTab_general\tgeneral_min_height\t50\tgeneral_scale\t300\tgeneral_water\t0.000\tgeneral_centerx\t0\tgeneral_centery\t0");
  2656.    }
  2657.    %data = restWords(Heightfield_operation.getRowText(0));
  2658.    %x = getField(%data,7); 
  2659.    %y = getField(%data,9); 
  2660.    HeightfieldPreview.setOrigin(%x, %y);
  2661.    Heightfield_operation.setSelectedById(Heightfield_operation.getRowId(0));
  2662.  
  2663.    // Move the control object to the specified position
  2664.    if (!%leaveCamera)
  2665.       terraformer.setCameraPosition(%x,%y); 
  2666. }   
  2667.  
  2668. //--------------------------------------
  2669. function strip(%stripStr, %strToStrip)
  2670. {
  2671.    %len = strlen(%stripStr);
  2672.    if(strcmp(getSubStr(%strToStrip, 0, %len), %stripStr) == 0)
  2673.       return getSubStr(%strToStrip, %len, 100000);
  2674.    return %strToStrip;
  2675. }
  2676.  
  2677. function Heightfield::doLoadHeightfield(%name)
  2678. {
  2679.    // ok, we're getting a terrain file...
  2680.  
  2681.    %newTerr = new TerrainBlock() // unnamed - since we'll be deleting it shortly:
  2682.    {
  2683.       position = "0 0 -1000";
  2684.       terrainFile = strip("terrains/", %name);
  2685.       squareSize = 8;
  2686.       visibleDistance = 100;
  2687.    };
  2688.    if(isObject(%newTerr))
  2689.    {
  2690.       %script = %newTerr.getHeightfieldScript();
  2691.       if(%script !$= "")
  2692.          Heightfield::loadFromScript(%script);
  2693.       %newTerr.delete();
  2694.    }   
  2695. }   
  2696.  
  2697.  
  2698. //--------------------------------------
  2699. function Heightfield::setBitmap()
  2700. {
  2701.    getLoadFilename("*.png", "Heightfield::doSetBitmap");
  2702. }   
  2703.  
  2704. //--------------------------------------
  2705. function Heightfield::doSetBitmap(%name)
  2706. {
  2707.    bitmap_name.setValue(%name);
  2708.    Heightfield::saveTab();
  2709.    Heightfield::preview($selectedOperation);
  2710. }   
  2711.  
  2712.  
  2713. //--------------------------------------
  2714. function Heightfield::hideTab()
  2715. {
  2716.    tab_terrainFile.setVisible(false);
  2717.    tab_fbm.setvisible(false);
  2718.    tab_rmf.setvisible(false);
  2719.    tab_canyon.setvisible(false);
  2720.    tab_smooth.setvisible(false);
  2721.    tab_smoothWater.setvisible(false);
  2722.    tab_smoothRidge.setvisible(false);
  2723.    tab_filter.setvisible(false);
  2724.    tab_turbulence.setvisible(false);
  2725.    tab_thermal.setvisible(false);
  2726.    tab_hydraulic.setvisible(false);
  2727.    tab_general.setvisible(false);
  2728.    tab_bitmap.setvisible(false);
  2729.    tab_blend.setvisible(false);
  2730.    tab_sinus.setvisible(false);
  2731. }
  2732.  
  2733.  
  2734. //--------------------------------------
  2735. function Heightfield::showTab(%id)
  2736. {
  2737.    Heightfield::hideTab();
  2738.    %data = restWords(Heightfield_operation.getRowTextById(%id));
  2739.    %tab  = getField(%data,1);
  2740.    echo("Tab data: " @ %data @ " tab: " @ %tab);
  2741.    %tab.setVisible(true);
  2742. }
  2743.  
  2744.  
  2745. //--------------------------------------
  2746. function Heightfield::center()
  2747. {
  2748.    %camera = terraformer.getCameraPosition(); 
  2749.    %x = getWord(%camera, 0);
  2750.    %y = getWord(%camera, 1);
  2751.  
  2752.    HeightfieldPreview.setOrigin(%x, %y);
  2753.  
  2754.    %origin = HeightfieldPreview.getOrigin();
  2755.    %x = getWord(%origin, 0);
  2756.    %y = getWord(%origin, 1);
  2757.  
  2758.    %root = HeightfieldPreview.getRoot();
  2759.    %x += getWord(%root, 0);
  2760.    %y += getWord(%root, 1);
  2761.  
  2762.    general_centerx.setValue(%x);
  2763.    general_centery.setValue(%y);
  2764.    Heightfield::saveTab();
  2765. }   
  2766.  
  2767. function ExportHeightfield::onAction()
  2768. {
  2769.    error("Time to export the heightfield...");
  2770.    if (Heightfield_operation.getSelectedId() != -1) {
  2771.       $TerraformerSaveRegister = getWord(Heightfield_operation.getValue(), 0);
  2772.       Heightfield::saveBitmap("");
  2773.    }
  2774. }
  2775.  
  2776. //------------------------------------------------------------------------------
  2777. // Functions
  2778. //------------------------------------------------------------------------------
  2779. function onNeedRelight()
  2780. {
  2781.    if( RelightMessage.visible == false )
  2782.       RelightMessage.visible = true;
  2783. }
  2784.  
  2785. function TerrainEditor::onGuiUpdate(%this, %text)
  2786. {
  2787.    %mouseBrushInfo = " (Mouse Brush) #: " @ getWord(%text, 0) @ "  avg: " @ getWord(%text, 1);
  2788.    %selectionInfo = " (Selection) #: " @ getWord(%text, 2) @ "  avg: " @ getWord(%text, 3);
  2789.    
  2790.    TEMouseBrushInfo.setValue(%mouseBrushInfo);
  2791.    TEMouseBrushInfo1.setValue(%mouseBrushInfo);
  2792.    TESelectionInfo.setValue(%selectionInfo);
  2793.    TESelectionInfo1.setValue(%selectionInfo);
  2794. }
  2795.  
  2796. function TerrainEditor::offsetBrush(%this, %x, %y)
  2797. {
  2798.    %curPos = %this.getBrushPos();
  2799.    %this.setBrushPos(getWord(%curPos, 0) + %x, getWord(%curPos, 1) + %y);
  2800. }
  2801.  
  2802. function TerrainEditor::swapInLoneMaterial(%this, %name)
  2803. {
  2804.    // swapped?
  2805.    if(%this.baseMaterialsSwapped $= "true")
  2806.    {
  2807.       %this.baseMaterialsSwapped = "false";
  2808.       tEditor.popBaseMaterialInfo();
  2809.    }
  2810.    else
  2811.    {
  2812.       %this.baseMaterialsSwapped = "true";
  2813.       %this.pushBaseMaterialInfo();
  2814.       %this.setLoneBaseMaterial(%name);
  2815.    }
  2816.       
  2817.    //
  2818.    flushTextureCache();
  2819. }
  2820.  
  2821. //------------------------------------------------------------------------------
  2822. // Functions
  2823. //------------------------------------------------------------------------------
  2824.  
  2825. //------------------------------------------------------------------------------
  2826.  
  2827.  
  2828. function TELoadTerrainButton::onAction(%this)
  2829. {
  2830.    getLoadFilename("terrains/*.ter", %this @ ".gotFileName");
  2831. }
  2832.  
  2833. function TELoadTerrainButton::gotFileName(%this, %name)
  2834. {
  2835.    //
  2836.    %pos = "0 0 0";
  2837.    %squareSize = "8";
  2838.    %visibleDistance = "1200";
  2839.  
  2840.    // delete current
  2841.    if(isObject(terrain))
  2842.    {
  2843.       %pos = terrain.position;
  2844.       %squareSize = terrain.squareSize;
  2845.       %visibleDistance = terrain.visibleDistance;
  2846.  
  2847.       terrain.delete();
  2848.    }
  2849.  
  2850.    // create new
  2851.    new TerrainBlock(terrain)
  2852.    {
  2853.       position = %pos;
  2854.       terrainFile = %name;
  2855.       squareSize = %squareSize;
  2856.       visibleDistance = %visibleDistance;
  2857.    };
  2858.  
  2859.    ETerrainEditor.attachTerrain();
  2860. }
  2861.  
  2862. function TerrainEditorSettingsGui::onWake(%this)
  2863. {
  2864.    TESoftSelectFilter.setValue(ETerrainEditor.softSelectFilter);
  2865. }
  2866.  
  2867. function TerrainEditorSettingsGui::onSleep(%this)
  2868. {
  2869.    ETerrainEditor.softSelectFilter = TESoftSelectFilter.getValue();
  2870. }
  2871.  
  2872. function TESettingsApplyButton::onAction(%this)
  2873. {
  2874.    ETerrainEditor.softSelectFilter = TESoftSelectFilter.getValue();
  2875.    ETerrainEditor.resetSelWeights(true);
  2876.    ETerrainEditor.processAction("softSelect");
  2877. }
  2878.  
  2879. function getPrefSetting(%pref, %default)
  2880. {
  2881.    // 
  2882.    if(%pref $= "")
  2883.       return(%default);
  2884.    else
  2885.       return(%pref);
  2886. }
  2887.  
  2888. //------------------------------------------------------------------------------
  2889.  
  2890. function Editor::open(%this)
  2891. {
  2892.    // prevent the mission editor from opening while the GuiEditor is open.
  2893.    if(Canvas.getContent() == GuiEditorGui.getId())
  2894.       return;
  2895.  
  2896.    Canvas.setContent(EditorGui);
  2897. }
  2898.  
  2899. function Editor::close(%this, %gui)
  2900. {
  2901.    Canvas.setContent(%gui);
  2902.    MessageHud.close();
  2903. }
  2904.  
  2905. //------------------------------------------------------------------------------
  2906.