Gui Editor Improvement - use the system clipboard for cut - copy - paste
by Orion Elenzil · 03/20/2009 (12:18 pm) · 1 comments
Gui Editor Improvement - use the system clipboard for cut - copy - paste
This resource was implemented on a TGE 1.3.5 codebase,
but will very likely work for all current flavours of TGE, TGEA, TGB, and T3D when it comes out,
assuming they use a similar method for copy and paste.
I very, very rarely save GUIs out from the gui editor.
In fact, never. I use the editor to mock-up the layout,
and use a text editor to directly edit the source code.
I find i can be much more accurate that way,
and often our controls have a degree of dynamic layout
which the Gui Editor simply can't capture.
So, with that usage pattern it would sometimes be nice
to be able to copy the source code for a given selection into the system clipboard
so it can easily be pasted into a text editor, and ditto the reverse direction.
This resource is somewhat hacky, but it does that.
Here's the basic method:
Stock torque implements copy in the Gui Editor by making a temporary SimSet named "guiClipboard", temporarily adding all the currently selected controls to it, and then writing it out to disk in a special file named clipboard.gui.
Paste is implemented by exec()ing that file, which causes a SimSet "guiClipboard" to be created. All the child controls of that SimSet are then added to the current selected GuiControl.
Sort of a hack, but it works.
This resource changes this process just a bit:
For copy, the special file is still written out, but the controls are no longer first embedded in a SimSet. After the file has been written, it's closed and reopened, and read back in as a simple string which is then copied into the system clipboard.
For paste, the special file is re-written using the contents of the system clipboard, and the text needed to instantiate the "guiClipboard" SimSet is automatically inserted. Then the file is exec()d and the previous mechanism plays out.
guiEditCtrl.cc
GuiEditCtrl::loadSelection() - insert this code at the beginning of the routine:
GuiEditCtrl::saveSelection() - delete these lines:
change this line:
replace these lines:
This resource was implemented on a TGE 1.3.5 codebase,
but will very likely work for all current flavours of TGE, TGEA, TGB, and T3D when it comes out,
assuming they use a similar method for copy and paste.
I very, very rarely save GUIs out from the gui editor.
In fact, never. I use the editor to mock-up the layout,
and use a text editor to directly edit the source code.
I find i can be much more accurate that way,
and often our controls have a degree of dynamic layout
which the Gui Editor simply can't capture.
So, with that usage pattern it would sometimes be nice
to be able to copy the source code for a given selection into the system clipboard
so it can easily be pasted into a text editor, and ditto the reverse direction.
This resource is somewhat hacky, but it does that.
Here's the basic method:
Stock torque implements copy in the Gui Editor by making a temporary SimSet named "guiClipboard", temporarily adding all the currently selected controls to it, and then writing it out to disk in a special file named clipboard.gui.
Paste is implemented by exec()ing that file, which causes a SimSet "guiClipboard" to be created. All the child controls of that SimSet are then added to the current selected GuiControl.
Sort of a hack, but it works.
This resource changes this process just a bit:
For copy, the special file is still written out, but the controls are no longer first embedded in a SimSet. After the file has been written, it's closed and reopened, and read back in as a simple string which is then copied into the system clipboard.
For paste, the special file is re-written using the contents of the system clipboard, and the text needed to instantiate the "guiClipboard" SimSet is automatically inserted. Then the file is exec()d and the previous mechanism plays out.
guiEditCtrl.cc
GuiEditCtrl::loadSelection() - insert this code at the beginning of the routine:
// first open the file and write the system clipboard into it.
FileStream stream;
if (!stream.open(filename, FileStream::Write))
{
Con::errorf("%s() - could not open file "%s".", __FUNCTION__, filename);
}
else
{
const char* buf = Platform::getClipboard();
U32 bufSize = dStrlen(buf);
const char line1[] = "new SimSet(guiClipboard) {n";
const char line2[] = "};n";
stream.write(sizeof(line1) - 1, line1);
stream.write(bufSize , buf );
stream.write(sizeof(line2) - 1, line2);
stream.close();
}GuiEditCtrl::saveSelection() - delete these lines:
SimSet *clipboardSet = new SimSet; clipboardSet->registerObject(); Sim::getRootGroup()->addObject(clipboardSet, "guiClipboard");
change this line:
clipboardSet->addObject(*i);to this:
(*i)->write(stream, 0);
replace these lines:
clipboardSet->write(stream, 0); clipboardSet->deleteObject();with these:
stream.close();
// now re-open the file and copy to the system clipboard
if (!stream.open(filename, FileStream::Read))
{
Con::errorf("%s() - could not open file "%s".", __FUNCTION__, filename);
}
else
{
U32 bufSize = stream.getStreamSize();
char* buf = new char[bufSize + 1];
stream.read(bufSize, buf);
buf[bufSize] = '�';
Platform::setClipboard(buf);
delete [] buf;
stream.close();
}About the author

Associate Konrad Kiss
Bitgap Games