Game Development Community

dev|Pro Game Development Curriculum

GuiEditor improvement: Save Current Control

by Orion Elenzil · 08/17/2006 (12:08 pm) · 2 comments

This ain't for everyone.
The stock GuiEditor in TGE 1.3 allows you to save the current GUI to a file.
However, for one reason or another, you may want to save only a portion of the GUI to a file.
This resource lets you do that.

Usage:
1. Right-click the GuiControl you specifically want to save. It should be outlined in green. This is AKA the "Current Add Set".

2. Select File | Save Current Control..

3. Browse to target file and save. - Be warned that the initial directory in the save-file dialog may be inconvenient.

Motivation:
This basically allows you to construct GUIs more modularly.
For example if you have several complex components in a GUI, you can save each component in a seperate file, and build the gui by exec'ing them and adding them in the master gui. for example:

new GameTSCtrl(PlayGui) {
   // yadda yadda
}

//--- OBJECT WRITE END ---


exec("./myPanelGui.gui");
PlayGui.add(myPanel);

There is more discussion here.


okay!


now the code!
it's pretty minimal.

guiEditCtrl.h
additions in bold.
void pushToBack();

   [b]GuiControl* getCurrentAddSet() { return mCurrentAddSet; }[/b]

guiEditCtrl.cc
additions in bold.
ConsoleMethod( GuiEditCtrl, setCurrentAddSet, void, 3, 3, "(GuiControl ctrl)")
{
   GuiControl *addSet;
   
   if (!Sim::findObject(argv[2], addSet))
   {
      Con::printf("%s(): Invalid control: %s", argv[0], argv[2]);
      return;
   }
   object->setCurrentAddSet(addSet);
}

[b]ConsoleMethod( GuiEditCtrl, getCurrentAddSet, S32, 2, 2, "()")
{
   if (object->getCurrentAddSet())
      return object->getCurrentAddSet()->getId();
   else
      return -1;
}[/b]

GuiEditorGui.gui
additions in bold.
//----------------------------------------
function GuiEditorSaveGui()
{
   %obj = GuiEditorContent.getObject(0);
   if(%obj == -1 || %obj.getName() $= "")
      return;
   %name = %obj.getName() @ ".gui";
   getSaveFilename("*.gui", "GuiEditorSaveGuiCallback", %name);
}

function GuiEditorSaveGuiCallback(%name)
{
   %obj = GuiEditorContent.getObject(0);
   
   // make sure it is saved...
   if(!%obj.save(%name))
   {
      MessageBoxOK("GuiEditor Save Failure", "Failed to save '" @ %name @ "'. The file may be read-only.");
   }
}   

[b]//----------------------------------------
function GuiEditorSaveControl()
{
   %obj = GuiEditor.getCurrentAddSet();
   if(%obj == -1 || %obj.getName() $= "")
      return;
   %name = %obj.getName() @ ".gui";
   getSaveFilename("*.gui", "GuiEditorSaveCtrlCallback", %name);
}

function GuiEditorSaveCtrlCallback(%name)
{
   %obj = GuiEditor.getCurrentAddSet();
   
   // make sure it is saved...
   if(!%obj.save(%name))
   {
      MessageBoxOK("GuiEditor Save Failure", "Failed to save '" @ %name @ "'. The file may be read-only.");
   }
}[/b]

//----------------------------------------


and still in GuiEditorGui.gui,
but this time there's changes in addition to additions. in bold.
GuiEditorMenuBar.clearMenus();
   GuiEditorMenuBar.addMenu("File", 0);
   GuiEditorMenuBar.addMenuItem("File", "New GUI...", 1);
   GuiEditorMenuBar.scriptCommand["File", 1] = "GuiEditorStartCreate();";
   GuiEditorMenuBar.addMenuItem("File", "Save GUI...", 2);
   GuiEditorMenuBar.scriptCommand["File", 2] = "GuiEditorSaveGui();";[b]
   GuiEditorMenuBar.addMenuItem("File", "Save Current Control...", 3);
   GuiEditorMenuBar.scriptCommand["File", 3] = "GuiEditorSaveControl();";
   GuiEditorMenuBar.addMenuItem("File", "-", 0);
   GuiEditorMenuBar.addMenuItem("File", "GUI Editor Help...", 4, "F1");
   GuiEditorMenuBar.scriptCommand["File", 4] = "getHelp(\"3. Gui Editor\");";
   GuiEditorMenuBar.addMenuItem("File", "Toggle GUI Editor...", 5, "F10");
   GuiEditorMenuBar.scriptCommand["File", 5] = "GuiEdit(0);";[/b]


that's it.
let me know any problems etc.

#1
11/10/2006 (11:48 am)
This is EXACTLY what I needed for our GUI workflow. Thanks for the resource.
#2
11/10/2006 (1:01 pm)
Sweet! Glad you found it.