Setting ctrl fields via code.
by Gregory "Centove" McLean · in Torque Game Engine · 04/24/2004 (12:30 pm) · 2 replies
So I was looking at the following resource:
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4094
(Thats the one that adds a lil preview bitmap to the loadfile dialog)
And I thought HEY I know where this can be really useful.
In the gui editor when you're messing with a bitmap control, it has a field 'bitmap' with a text entry ctrl next to it. Which is nice and all and it works once you figure out what to type in there :) So I got to thinking, hey this would be nice with a 'browse' button so one could navigate to an image, click load and be done with it.
So I set out to do this.
Couple of things came up on the way (I did get it working but it just seems a bit complex for such a simple function)
1. Is there anyway in the guieditor to get the currently selected control? I didn't see one so I stuck a global (ick!) in there and update it on the onSelect methods.
2. Is there anyway to set the value of a field for a control without iterating over all the fields in that control?
The way I got it working now is I did the following:
In the guiInspector class, in the inspect member where its building all the text controls for the fields, if the type is TypeFilename, make it a button vs the text control and have that button's command set to getLoadFilename with the callback set to loadFile. This gets the loadfile dialog up and lets you select things. And calls a function with the filename. Which gets to question 1 above.
Now we need to get this filename into that control, this bit took me quite a bit to get working, but what I wound up doing is implementing a console method in the guiInspector that I call as such:
GuiEditorInspectFields.SetFilenameField($SelectedObject, %filename);
In that console method I have the following (There must be a better way)
Is that the only way to go about mucking around with the controls?
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4094
(Thats the one that adds a lil preview bitmap to the loadfile dialog)
And I thought HEY I know where this can be really useful.
In the gui editor when you're messing with a bitmap control, it has a field 'bitmap' with a text entry ctrl next to it. Which is nice and all and it works once you figure out what to type in there :) So I got to thinking, hey this would be nice with a 'browse' button so one could navigate to an image, click load and be done with it.
So I set out to do this.
Couple of things came up on the way (I did get it working but it just seems a bit complex for such a simple function)
1. Is there anyway in the guieditor to get the currently selected control? I didn't see one so I stuck a global (ick!) in there and update it on the onSelect methods.
2. Is there anyway to set the value of a field for a control without iterating over all the fields in that control?
The way I got it working now is I did the following:
In the guiInspector class, in the inspect member where its building all the text controls for the fields, if the type is TypeFilename, make it a button vs the text control and have that button's command set to getLoadFilename with the callback set to loadFile. This gets the loadfile dialog up and lets you select things. And calls a function with the filename. Which gets to question 1 above.
Now we need to get this filename into that control, this bit took me quite a bit to get working, but what I wound up doing is implementing a console method in the guiInspector that I call as such:
GuiEditorInspectFields.SetFilenameField($SelectedObject, %filename);
In that console method I have the following (There must be a better way)
ConsoleMethod( GuiInspector, setFilenameField, void, 4, 4, "(SimObject obj, string value)" "Set the filename field to the chosen value.")
{
SimObject *target = Sim::findObject(argv[2]);
if(!target)
{
Con::printf("%s(): invalid object: %s", argv[0], argv[2]);
return;
}
AbstractClassRep::FieldList fieldList = target->getFieldList();
AbstractClassRep::FieldList::iterator itr;
for(itr = fieldList.begin(); itr != fieldList.end(); itr++)
{
if(itr->type == AbstractClassRep::DepricatedFieldType ||
itr->type == AbstractClassRep::StartGroupFieldType ||
itr->type == AbstractClassRep::EndGroupFieldType)
continue;
for(U32 i = 0; i < itr->elementCount; i++)
{
if (itr->type == TypeFilename)
{
char textbuf[1024];
dSprintf(textbuf, sizeof(textbuf),
"InspectStatic%s%d_%d", itr->pFieldname, i, itr->type);
GuiControl *editCtrl = NULL;
SimObject *inspectObj = Sim::findObject(textbuf);
if (inspectObj)
editCtrl = dynamic_cast<GuiControl *>(inspectObj);
if (editCtrl)
editCtrl->setScriptValue(argv[3]);
}
}
}
}Is that the only way to go about mucking around with the controls?
#2
Actually I did this, the gui inspector puts up two colum's of controls.
TextCtrl(label) EditCtrl(depending on the field)
What I did was turn that TextCtrl into a button when on a filename field. (the ... button if you will)
That looks as such (in the inspect method)
Well thats what I thought, and I tried that, it didn't work :( (Was probably setting the wrong field)
editCtrl->setScriptValue(argv[3]);
initially was:
editCtrl->setField("text", argv[3]);
which didn't seem to do anything.
04/25/2004 (5:17 am)
Quote:I would change the inspector so that it adds a "..." button to the right of the text box - sometimes it's handy to be able to change stuff from the text box,
Actually I did this, the gui inspector puts up two colum's of controls.
TextCtrl(label) EditCtrl(depending on the field)
What I did was turn that TextCtrl into a button when on a filename field. (the ... button if you will)
That looks as such (in the inspect method)
.....
expandEscape(textbuf, dstr);
GuiControl *textCtrl = NULL;
char temp[1024];
// Put a button for the label so we can browse to a file
if (itr->type == TypeFilename)
{
textCtrl = new GuiButtonCtrl();
textCtrl->setField("profile", "GuiButtonProfile");
textCtrl->setField("command", "getLoadFilename(\"*.png\",loadFile);");
dSprintf(temp, sizeof(temp), "%s ...", itr->pFieldname);
}
else
{
textCtrl = new GuiTextCtrl();
textCtrl->setField("profile", "GuiTextProfile");
if(itr->elementCount > 1)
dSprintf(temp, sizeof(temp), "%s%d", itr->pFieldname, i);
else
dSprintf(temp, sizeof(temp), "%s", itr->pFieldname);
}
textCtrl->setField("text", temp);
textCtrl->registerObject();
....Quote:hmm... Gotta figure out how to do this... Any tips?
and I'd make the load button call a callback on the GuiInspector class, GuiInspector::onLoadRequest(%this, %object, %field, %currentValue).
Quote:
Or you could call setField, too. setScriptValue is probably not the right thing.
Well thats what I thought, and I tried that, it didn't work :( (Was probably setting the wrong field)
editCtrl->setScriptValue(argv[3]);
initially was:
editCtrl->setField("text", argv[3]);
which didn't seem to do anything.
Associate Kyle Carter
Or you could call setField, too. setScriptValue is probably not the right thing.