📄 guicontrol.cc
字号:
if(!ctrl->isAwake())
ctrl->awaken();
}
AssertFatal(!mAwake, "GuiControl::awaken: should not be awake here");
if(!mAwake)
{
if(!onWake())
{
Con::errorf(ConsoleLogEntry::General, "GuiControl::awaken: failed onWake for obj: %s", getName());
AssertFatal(0, "GuiControl::awaken: failed onWake");
deleteObject();
}
}
}
void GuiControl::sleep()
{
AssertFatal(mAwake, "GuiControl::sleep: control is not awake");
if(!mAwake)
return;
iterator i;
for(i = begin(); i != end(); i++)
{
GuiControl *ctrl = static_cast<GuiControl *>(*i);
AssertFatal(ctrl->isAwake(), "GuiControl::sleep: child control is already asleep");
if(ctrl->isAwake())
ctrl->sleep();
}
AssertFatal(mAwake, "GuiControl::sleep: should not be asleep here");
if(mAwake)
onSleep();
}
void GuiControl::preRender()
{
AssertFatal(mAwake, "GuiControl::preRender: control is not awake");
if(!mAwake)
return;
iterator i;
for(i = begin(); i != end(); i++)
{
GuiControl *ctrl = static_cast<GuiControl *>(*i);
ctrl->preRender();
}
onPreRender();
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
bool GuiControl::onWake()
{
AssertFatal( !mAwake, "GuiControl::onWake: control is already awake" );
if( mAwake )
return false;
// [tom, 4/18/2005] Cause mLangTable to be refreshed in case it was changed
mLangTable = NULL;
//make sure we have a profile
if( !mProfile )
{
// First lets try to get a profile based on the classname of this object
const char *cName = getClassName();
// Ensure this is a valid name...
if( cName && cName[0] )
{
S32 pos = 0;
for( pos = 0; pos <= dStrlen( cName ); pos++ )
if( !dStrncmp( cName + pos, "Ctrl", 4 ) )
break;
if( pos != 0 ) {
char buff[255];
dStrncpy( buff, cName, pos );
buff[pos] = '\0';
dStrcat( buff, "Profile\0" );
SimObject *obj = Sim::findObject( buff );
if( obj )
mProfile = dynamic_cast<GuiControlProfile*>( obj );
}
}
// Ok lets check to see if that worked
if( !mProfile ) {
SimObject *obj = Sim::findObject( "GuiDefaultProfile" );
if( obj )
mProfile = dynamic_cast<GuiControlProfile*>(obj);
}
AssertFatal( mProfile, avar( "GuiControl: %s created with no profile.", getName() ) );
}
//set the flag
mAwake = true;
//set the layer
GuiCanvas *root = getRoot();
AssertFatal(root, "Unable to get the root Canvas.");
GuiControl *parent = getParent();
if (parent && parent != root)
mLayer = parent->mLayer;
//make sure the first responder exists
if (! mFirstResponder)
mFirstResponder = findFirstTabable();
//see if we should force this control to be the first responder
if (mProfile->mTabable && mProfile->mCanKeyFocus)
setFirstResponder();
//increment the profile
mProfile->incRefCount();
#ifdef TGE_RPG
if(m_bAutoSize) // if( !GuiControl::smDesignTime )
autoSizeToContent();
#endif
// Only invoke script callbacks if we have a namespace in which to do so
// This will suppress warnings
if( getNamespace() )
Con::executef(this, 1, "onWake");
return true;
}
void GuiControl::onSleep()
{
AssertFatal(mAwake, "GuiControl::onSleep: control is not awake");
if(!mAwake)
return;
//decrement the profile referrence
if( mProfile != NULL )
mProfile->decRefCount();
clearFirstResponder();
mouseUnlock();
// Only invoke script callbacks if we have a namespace in which to do so
// This will suppress warnings
if( getNamespace() )
Con::executef(this, 1, "onSleep");
// Set Flag
mAwake = false;
}
void GuiControl::setControlProfile(GuiControlProfile *prof)
{
AssertFatal(prof, "GuiControl::setControlProfile: invalid profile");
if(prof == mProfile)
return;
if(mAwake)
mProfile->decRefCount();
mProfile = prof;
if(mAwake)
mProfile->incRefCount();
}
void GuiControl::onPreRender()
{
// do nothing.
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
#ifdef TGE_RPG
ConsoleMethod( GuiControl, autoSizeToContent, void, 2, 2, "()")
{
object->autoSizeToContent();
}
#endif
ConsoleMethod( GuiControl, setValue, void, 3, 3, "(string value)")
{
object->setScriptValue(argv[2]);
}
ConsoleMethod( GuiControl, getValue, const char*, 2, 2, "")
{
return object->getScriptValue();
}
ConsoleMethod( GuiControl, setActive, void, 3, 3, "(bool active)")
{
object->setActive(dAtob(argv[2]));
}
ConsoleMethod( GuiControl, isActive, bool, 2, 2, "")
{
return object->isActive();
}
ConsoleMethod( GuiControl, setVisible, void, 3, 3, "(bool visible)")
{
object->setVisible(dAtob(argv[2]));
}
ConsoleMethod( GuiControl, makeFirstResponder, void, 3, 3, "(bool isFirst)")
{
object->makeFirstResponder(dAtob(argv[2]));
}
ConsoleMethod( GuiControl, isVisible, bool, 2, 2, "")
{
return object->isVisible();
}
ConsoleMethod( GuiControl, isAwake, bool, 2, 2, "")
{
return object->isAwake();
}
ConsoleMethod( GuiControl, setProfile, void, 3, 3, "(GuiControlProfile p)")
{
GuiControlProfile * profile;
if(Sim::findObject(argv[2], profile))
object->setControlProfile(profile);
}
ConsoleMethod( GuiControl, resize, void, 6, 6, "(int x, int y, int w, int h)")
{
Point2I newPos(dAtoi(argv[2]), dAtoi(argv[3]));
Point2I newExt(dAtoi(argv[4]), dAtoi(argv[5]));
object->resize(newPos, newExt);
}
#ifdef TGE_RPG
ConsoleMethod( GuiControl, getRight, S32, 2, 2, "()")
{
return object->getRight();
}
ConsoleMethod( GuiControl, getBottom, S32, 2, 2, "()")
{
return object->getBottom();
}
ConsoleMethod( GuiControl, setLeft, void, 3, 3, "(int x)")
{
object->setLeft(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setTop, void, 3, 3, "(int y)")
{
object->setTop(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setRight, void, 3, 3, "(int rx)")
{
object->setRight(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setRightBy, void, 3, 3, "(int rx)")
{
object->setRightBy(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setBottom, void, 3, 3, "(int bx)")
{
object->setBottom(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setBottomBy, void, 3, 3, "(int bx)")
{
object->setBottomBy(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setWidth, void, 3, 3, "(int w)")
{
object->setWidth(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setHeight, void, 3, 3, "(int h)")
{
object->setHeight(dAtoi(argv[2]));
}
ConsoleMethod( GuiControl, setPosition, void, 4, 4, "(int x,int y)")
{
Point2I newPos(dAtoi(argv[2]), dAtoi(argv[3]));
object->setPosition(newPos);
}
ConsoleMethod( GuiControl, setExtent, void, 4, 4, "(int w,int h)")
{
Point2I newExt(dAtoi(argv[2]), dAtoi(argv[3]));
object->setPosition(newExt);
}
#endif
ConsoleMethod( GuiControl, getPosition, const char*, 2, 2, "")
{
char *retBuffer = Con::getReturnBuffer(64);
const Point2I &pos = object->getPosition();
dSprintf(retBuffer, 64, "%d %d", pos.x, pos.y);
return retBuffer;
}
ConsoleMethod( GuiControl, getExtent, const char*, 2, 2, "Get the width and height of the control.")
{
char *retBuffer = Con::getReturnBuffer(64);
const Point2I &ext = object->getExtent();
dSprintf(retBuffer, 64, "%d %d", ext.x, ext.y);
return retBuffer;
}
ConsoleMethod( GuiControl, getMinExtent, const char*, 2, 2, "Get the minimum allowed size of the control.")
{
char *retBuffer = Con::getReturnBuffer(64);
const Point2I &minExt = object->getMinExtent();
dSprintf(retBuffer, 64, "%d %d", minExt.x, minExt.y);
return retBuffer;
}
void GuiControl::onRemove()
{
// Only invoke script callbacks if they can be received
if( getNamespace() )
Con::executef(this, 1, "onRemove");
clearFirstResponder();
Parent::onRemove();
// If we are a child, notify our parent that we've been removed
GuiControl *parent = getParent();
if( parent )
parent->onChildRemoved( this );
}
void GuiControl::onChildRemoved( GuiControl *child )
{
// Base does nothing with this
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
const char *GuiControl::getScriptValue()
{
return NULL;
}
void GuiControl::setScriptValue(const char *value)
{
value;
}
void GuiControl::setConsoleVariable(const char *variable)
{
if (variable)
{
mConsoleVariable = StringTable->insert(variable);
}
else
{
mConsoleVariable = StringTable->insert("");
}
}
void GuiControl::setConsoleCommand(const char *newCmd)
{
if (newCmd)
mConsoleCommand = StringTable->insert(newCmd);
else
mConsoleCommand = StringTable->insert("");
}
const char * GuiControl::getConsoleCommand()
{
return mConsoleCommand;
}
void GuiControl::setSizing(S32 horz, S32 vert)
{
mHorizSizing = horz;
mVertSizing = vert;
}
void GuiControl::setVariable(const char *value)
{
if (mConsoleVariable[0])
Con::setVariable(mConsoleVariable, value);
}
void GuiControl::setIntVariable(S32 value)
{
if (mConsoleVariable[0])
Con::setIntVariable(mConsoleVariable, value);
}
void GuiControl::setFloatVariable(F32 value)
{
if (mConsoleVariable[0])
Con::setFloatVariable(mConsoleVariable, value);
}
const char * GuiControl::getVariable()
{
if (mConsoleVariable[0])
return Con::getVariable(mConsoleVariable);
else return NULL;
}
S32 GuiControl::getIntVariable()
{
if (mConsoleVariable[0])
return Con::getIntVariable(mConsoleVariable);
else return 0;
}
F32 GuiControl::getFloatVariable()
{
if (mConsoleVariable[0])
return Con::getFloatVariable(mConsoleVariable);
else return 0.0f;
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
bool GuiControl::cursorInControl()
{
GuiCanvas *root = getRoot();
if (! root) return false;
Point2I pt = root->getCursorPos();
Point2I offset = localToGlobalCoord(Point2I(0, 0));
if (pt.x >= offset.x && pt.y >= offset.y &&
pt.x < offset.x + mBounds.extent.x && pt.y < offset.y + mBounds.extent.y)
{
return true;
}
else
{
return false;
}
}
bool GuiControl::pointInControl(const Point2I& parentCoordPoint)
{
S32 xt = parentCoordPoint.x - mBounds.point.x;
S32 yt = parentCoordPoint.y - mBounds.point.y;
return xt >= 0 && yt >= 0 && xt < mBounds.extent.x && yt < mBounds.extent.y;
}
GuiControl* GuiControl::findHitControl(const Point2I &pt, S32 initialLayer)
{
iterator i = end(); // find in z order (last to first)
while (i != begin())
{
i--;
GuiControl *ctrl = static_cast<GuiControl *>(*i);
if (initialLayer >= 0 && ctrl->mLayer > initialLayer)
{
continue;
}
else if (ctrl->mVisible && ctrl->pointInControl(pt))
{
Point2I ptemp = pt - ctrl->mBounds.point;
#ifdef TGE_RPG
GuiControl *hitCtrl;
if(initialLayer == -2)
hitCtrl = ctrl;
else
hitCtrl = ctrl->findHitControl(ptemp);
#else
GuiControl *hitCtrl = ctrl->findHitControl(ptemp);
#endif
if(hitCtrl->mProfile->mModal)
return hitCtrl;
}
}
return this;
}
#ifdef TGE_RPG
GuiControl* GuiControl::findAnyHitControl(const Point2I &pt, S32 initialLayer)
{
iterator i = end(); // find in z order (last to first)
while (i != begin())
{
i--;
GuiControl *ctrl = static_cast<GuiControl *>(*i);
if (initialLayer >= 0 && ctrl->mLayer > initialLayer)
{
continue;
}
else if (ctrl->mVisible && ctrl->pointInControl(pt))
{
Point2I ptemp = pt - ctrl->mBounds.point;
GuiControl *hitCtrl;
if(initialLayer == -2)
hitCtrl = ctrl;
else
hitCtrl = ctrl->findHitControl(ptemp);
//if(hitCtrl->mProfile->mModal)
return hitCtrl;
}
}
return this;
}
#endif
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
bool GuiControl::isMouseLocked()
{
GuiCanvas *root = getRoot();
return root ? root->getMouseLockedControl() == this : false;
}
void GuiControl::mouseLock(GuiControl *lockingControl)
{
GuiCanvas *root = getRoot();
if (root)
root->mouseLock(lockingControl);
}
void GuiControl::mouseLock()
{
GuiCanvas *root = getRoot();
if (root)
root->mouseLock(this);
}
void GuiControl::mouseUnlock()
{
GuiCanvas *root = getRoot();
if (root)
root->mouseUnlock(this);
}
bool GuiControl::onInputEvent(const InputEvent &event)
{
// Do nothing by default...
return( false );
}
void GuiControl::onMouseUp(const GuiEvent &event)
{
}
void GuiControl::onMouseDown(const GuiEvent &event)
{
}
void GuiControl::onMouseMove(const GuiEvent &event)
{
//if this control is a dead end, make sure the event stops here
if ( !mVisible || !mAwake )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -