//-------------------------------------------------------------------------------------
void SpaceAvatarSelect::buttonHit(OgreBites::Button* button)
{
    if(button->getCaption().find("start") != Ogre::UTFString::npos)
    {
        if(g_selAvatarDBID == 0)
        {
            MessageBox( NULL, "SpaceAvatarSelect::no selected avatar!", "warning!", MB_OK);
            return;
        }

        kbe_lock();
        kbe_callEntityMethod(kbe_playerID(), "selectAvatarGame", KBEngine::StringConv::val2str(g_selAvatarDBID).c_str());
        kbe_unlock();

        OgreApplication::getSingleton().changeSpace(new SpaceWorld(mRoot, mWindow, mInputManager, mTrayMgr));
    }
    else if(button->getCaption() == "create avatar")
    {
        kbe_lock();
        kbe_callEntityMethod(kbe_playerID(), "reqCreateAvatar", "[1, \"kbengine\"]");
        kbe_unlock();
    }
    else
    {
        for(KBEngine::uint32 i=0; i<g_avatars.size(); i++)
        {
            if(button->getCaption() == g_avatars[i])
            {
                Ogre::String dbid = Ogre::StringUtil::split(button->getCaption(), "_")[1];
                g_selAvatarDBID = KBEngine::StringConv::str2value<KBEngine::DBID>(dbid.c_str());
                ((OgreBites::Button*)mTrayMgr->getWidget("start"))->setCaption(Ogre::String("start[") + dbid + Ogre::String("]"));
            }
        }
    }
}
//-------------------------------------------------------------------------------------
bool OgreApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
	if(g_space == NULL)
	{
		return BaseApplication::frameRenderingQueued(evt);
	}
	
	while(true)
	{
		g_spaceMutex.lock();

		if(events_.empty())
		{
			g_spaceMutex.unlock();
			break;
		}

		std::tr1::shared_ptr<const KBEngine::EventData> pEventData = events_.front();
		events_.pop();
		g_spaceMutex.unlock();

		KBEngine::EventID id = pEventData->id;
		
		if(id == CLIENT_EVENT_SERVER_CLOSED)
		{
			//OgreApplication::getSingleton().changeSpace(new SpaceAvatarSelect(mRoot, mWindow, mInputManager, mTrayMgr));
			//break;
		}

		// 如果需要在本线程访问脚本层则需要锁住引擎
		if(id == CLIENT_EVENT_SCRIPT)
		{
			kbe_lock();
		}

		g_space->kbengine_onEvent(pEventData.get());

		if(id == CLIENT_EVENT_SCRIPT)
		{
			kbe_unlock();
		}

		g_hasEvent = false;
	}

	if(!g_space->frameRenderingQueued(evt))
		return true;

    return BaseApplication::frameRenderingQueued(evt);
}