TGB/Level Builder/CustomResources

From TDN

This page is a Work In Progress.

Contents

Custom Resources

What are custom resources? The "Layer Floater" resource that was released a short while ago is a custom resource, and you too can create similiar functionalities. This page will hopefully show you how.

Purpose and Intent

This tutorial will show you how to create a custom Floater within the Level Builder, which can be Toggled On/Off using the View Menu or the Shortcut Key "Ctrl W" (configurable).

Plans to extend this tutorial to show you how to add additional controls to the Floater, as well as Bind them to actions within the Level Builder (add Objects, etc) are in the works.

Setting Up Base Files

If you are using Windows 7 or Vista, be aware that you might have to adjust the permissions on the TGB folder.

We'll start by first creating a "base" set of files, these are:

/pathToTGB/games/tools/resources/myCustomResource
/pathToTGB/games/tools/resources/myCustomResource/resourceDatabase.cs
/pathToTGB/games/tools/resources/myCustomResource/floater.gui
/pathToTGB/games/tools/resources/myCustomResource/floater.cs

Where "/pathtoTGB" is the location you installed TGB to.


For T2D 1.7.6 the paths are:

/pathToTGB/tgb/tools/resources/myCustomResource
/pathToTGB/tgb/tools/resources/myCustomResource/resourceDatabase.cs
/pathToTGB/tgb/tools/resources/myCustomResource/floater.gui
/pathToTGB/tgb/tools/resources/myCustomResource/floater.cs

Where "/pathtoTGB" is the location you installed TGB to.

You will also need to correct some lines in:

/pathToTGB/tgb/tools/editorClasses/scripts/resourceLoader.ed.cs

1) Change this line: (it would be best to comment out the lines than to delete them just in case you want to revert them later)

function ResourceObject::load( %resourcePath )

To:

function ResourceObject::load( %resourcePath, %resource )

2) And Change this line:

%resourceFile = %resourcePath @ "/resourceDatabase.cs";

To:

    %resourceFile = "~/resources/" @ %resource @ "/resourceDatabase.cs";

Resource Load/Unload

Inside your "resourceDatabase.cs" file, add the following:

// resourceDatabase.cs

// Create Resource Descriptor
$instantResource = new ScriptObject()
{
   Class = "myCustomResource";
   Name = "myCustomResource";
   User = "TOOLS";
   LoadFunction = "myCustomResource::LoadResource";
   UnloadFunction = "myCustomResource::UnloadResource";
};

// Load Resource Function - Hooks into game
function myCustomResource::LoadResource( %this )
{
   exec( "./floater.cs" );
   exec( "./floater.gui" );
   
   // Add to the menu.
   %this.menuItem = levelBuilderMenu.addMenuItem( "View", "Toggle My Custom Resource", "Canvas.toggleFloater(mcrFloaterGui);", "Ctrl W" );
}


// Unload Resource Function - Remove from game Sim.
function upkWindowSelectionTool::UnloadResource( %this )
{
   levelBuilderMenu.removeMenuItem( "View", %this.menuItem );
}

$instantResource.data = new SimGroup();

For T2D 1.7.6 use:

// resourceDatabase.cs

// Create Resource Descriptor
$instantResource = new ScriptObject()
{
   Class = "myCustomResource";
   Name = "myCustomResource";
   User = "TOOLS";
   LoadFunction = "myCustomResource::LoadResource";
   UnloadFunction = "myCustomResource::UnloadResource";
};
function openCustomGui()
{
echo("Toggle Custom Resource");
Canvas.toggleFloater(mcrFloaterGui);
}
function doNothing()
{
}
// Load Resource Function - Hooks into game
function myCustomResource::LoadResource( %this )
{
   exec( "./floater.cs" );
   exec( "./floater.gui" );
   echo("Press Control-W to activate Custom Resource...");

//Toggle window by pressing Ctrl W.
levelEditorMap.bindCmd(keyboard, "Ctrl W", "openCustomGui();", "doNothing();");
}

$instantResource.data = new SimGroup();

Developing the GUI

In your "floater.gui", add the following lines:

// floater.gui

//--- OBJECT WRITE BEGIN ---
new GuiWindowCtrl(mcrFloaterGui) {
   canSaveDynamicFields = "0";
   Profile = "EditorToolWindowProfile";
   HorizSizing = "relative";
   VertSizing = "relative";
   position = "0 0";
   Extent = "640 480";
   MinExtent = "8 2";
   canSave = "1";
   Visible = "1";
   hovertime = "1000";
   maxLength = "1024";
   resizeWidth = "1";
   resizeHeight = "1";
   canMove = "1";
   canClose = "1";
   canMinimize = "0";
   canMaximize = "0";
   minSize = "50 50";
   text = "My Custom Resource";
   closeCommand = "Canvas.toggleFloater(mcrFloaterGui);";
   
   new GuiTextCtrl(mcrFloaterText)
   {
   	canSaveDynamicFields = "0";
   	Profile = "GuiTextProfile";
   	HorizSizing = "relative";
   	VertSizing = "relative";
   	position = "5 10";
   	Extent = "635 20";
   	Visible = "1";
   	canSave = "0";
   	text = "Custom Text";
   };
   
   new GuiButtonCtrl() {
      canSaveDynamicFields = "0";
      Profile = "GuiButtonProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      position = "13 355";
      Extent = "163 98";
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      text = "Drop It!";
      groupNum = "-1";
      buttonType = "PushButton";
      command = "myCustomResource::CreateItem();";
   };
};
//--- OBJECT WRITE END ---

Adding Functionality

Now, in your floater.cs, add the following:

// floater.cs

function myCustomResource::CreateItem()
{
   %toolType = "t2dTrigger";
   %scenegraph = ToolManager.getLastWindow().getSceneGraph();

   %obj = new t2dSceneObject() { Class = "Foo"; SuperClass = "Bar"; size = "300 300"; };
   %scenegraph.addToScene(%obj);
}


Testing

Now, save all your files, and launch TGB (restart it, if it's already running).

Press "Ctrl W", press the "Drop It!" button and close then press "Ctrl W" again. You'll notice a new t2dSceneObject has been placed in the center of your level, with dimensions of "300 300" and a Class of "Foo" and a SuperClass of "Bar".

Pretty neat, eh?