Skip to content
Permalink
Browse files

fix #5075

do not start camera transitions in Lua-invoked CameraHandler::SetState
(*gets can already implement smooth transitioning themselves, a callout
would count as "nice to have") so it can be safely called in DrawScreen
  • Loading branch information
rtri
rtri committed Feb 9, 2016
1 parent ffede31 commit c033f79b0d6a834883dfe800adc1f199b8ac8cad
Showing with 42 additions and 51 deletions.
  1. +32 −39 rts/Game/CameraHandler.cpp
  2. +10 −12 rts/Lua/LuaUnsyncedCtrl.cpp
@@ -235,10 +235,8 @@ void CCameraHandler::CameraTransition(float nsecs)

void CCameraHandler::SetCameraMode(unsigned int mode)
{

if ((mode >= camControllers.size()) || (mode == static_cast<unsigned int>(currCamCtrlNum))) {
if ((mode >= camControllers.size()) || (mode == static_cast<unsigned int>(currCamCtrlNum)))
return;
}

CameraTransition(1.0f);

@@ -255,36 +253,34 @@ void CCameraHandler::SetCameraMode(unsigned int mode)

void CCameraHandler::SetCameraMode(const std::string& modeName)
{

const int modeNum = GetModeIndex(modeName);

// do nothing if the name is not matched
if (modeNum >= 0) {
SetCameraMode(modeNum);
}
// do nothing if the name is not matched
}


int CCameraHandler::GetModeIndex(const std::string& name) const
{
const auto it = nameMap.find(name);

std::map<std::string, unsigned int>::const_iterator it = nameMap.find(name);
if (it != nameMap.end()) {
if (it != nameMap.end())
return it->second;
}

return -1;
}


void CCameraHandler::PushMode()
{

controllerStack.push(GetCurrentControllerNum());
}


void CCameraHandler::PopMode()
{

if (!controllerStack.empty()) {
SetCameraMode(controllerStack.top());
controllerStack.pop();
@@ -311,23 +307,22 @@ void CCameraHandler::ToggleState()

void CCameraHandler::ToggleOverviewCamera()
{

CameraTransition(1.0f);

if (controllerStack.empty()) {
PushMode();
SetCameraMode(CAMERA_MODE_OVERVIEW);
}
else {
} else {
PopMode();
}
}


void CCameraHandler::SaveView(const std::string& name)
{

if (name.empty())
return;

ViewData vd;
vd["mode"] = currCamCtrlNum;
currCamCtrl->GetState(vd);
@@ -338,15 +333,13 @@ void CCameraHandler::SaveView(const std::string& name)

bool CCameraHandler::LoadView(const std::string& name)
{

if (name.empty()) {
if (name.empty())
return false;
}

std::map<std::string, ViewData>::const_iterator it = views.find(name);
if (it == views.end()) {
const auto it = views.find(name);
if (it == views.end())
return false;
}

const ViewData& saved = it->second;

ViewData current;
@@ -355,21 +348,21 @@ bool CCameraHandler::LoadView(const std::string& name)
if (saved == current) { // load a view twice to return to old settings
if (name != "__old_view") { // safety: should not happen, but who knows?
return LoadView("__old_view");
} else {
return false;
}
} else {
if (name != "__old_view") {
SaveView("__old_view");
}
return LoadViewData(saved);

return false;
}

if (name != "__old_view") {
SaveView("__old_view");
}

return LoadViewData(saved);
}


void CCameraHandler::GetState(CCameraController::StateMap& sm) const
{

sm.clear();
sm["mode"] = (float)currCamCtrlNum;

@@ -379,21 +372,23 @@ void CCameraHandler::GetState(CCameraController::StateMap& sm) const

bool CCameraHandler::SetState(const CCameraController::StateMap& sm)
{
CCameraController::StateMap::const_iterator it = sm.find("mode");
const auto it = sm.find("mode");

if (it != sm.end()) {
const unsigned int camMode = (unsigned int)it->second;
if (camMode >= camControllers.size()) {
const unsigned int oldMode = currCamCtrlNum;

if (camMode >= camControllers.size())
return false;
}

if (camMode != currCamCtrlNum) {
CameraTransition(1.0f);
const int oldMode = currCamCtrlNum;
currCamCtrlNum = camMode;
currCamCtrl = camControllers[camMode];
currCamCtrl->SwitchTo(oldMode);
}
}
bool result = currCamCtrl->SetState(sm);

const bool result = currCamCtrl->SetState(sm);
currCamCtrl->Update();
return result;
}
@@ -407,7 +402,6 @@ const std::string CCameraHandler::GetCurrentControllerName() const

void CCameraHandler::PushAction(const Action& action)
{

const std::string cmd = action.command;

if (cmd == "viewfps") {
@@ -463,12 +457,10 @@ void CCameraHandler::PushAction(const Action& action)

bool CCameraHandler::LoadViewData(const ViewData& vd)
{

if (vd.empty()) {
if (vd.empty())
return false;
}

ViewData::const_iterator it = vd.find("mode");
const auto it = vd.find("mode");
if (it != vd.end()) {
const unsigned int camMode = (unsigned int)it->second;
if (camMode >= camControllers.size()) {
@@ -484,6 +476,7 @@ bool CCameraHandler::LoadViewData(const ViewData& vd)
currCamCtrl->SwitchTo(oldMode, showMode);
}
}

return currCamCtrl->SetState(vd);
}

@@ -778,9 +778,8 @@ int LuaUnsyncedCtrl::DrawUnitCommands(lua_State* L)

int LuaUnsyncedCtrl::SetCameraTarget(lua_State* L)
{
if (mouse == NULL) {
if (mouse == nullptr)
return 0;
}

const float3 pos(luaL_checkfloat(L, 1),
luaL_checkfloat(L, 2),
@@ -797,15 +796,15 @@ int LuaUnsyncedCtrl::SetCameraTarget(lua_State* L)

int LuaUnsyncedCtrl::SetCameraState(lua_State* L)
{
if (mouse == NULL) {
// ??
if (mouse == nullptr)
return 0;
}

if (!lua_istable(L, 1)) {
if (!lua_istable(L, 1))
luaL_error(L, "Incorrect arguments to SetCameraState(table, camTime)");
}

const float camTime = luaL_checkfloat(L, 2);
// TODO: add separate callout for smooth mode transitions?
// const float camTime = luaL_checkfloat(L, 2);

CCameraController::StateMap camState;

@@ -822,14 +821,13 @@ int LuaUnsyncedCtrl::SetCameraState(lua_State* L)
}
}

camHandler->CameraTransition(camTime);
lua_pushboolean(L, camHandler->SetState(camState));

if (!CLuaHandle::GetHandleSynced(L)) {
lua_pushboolean(L, camHandler->SetState(camState));
return 1;
} else {
return 0;
}

camHandler->SetState(camState);
return 0;
}


0 comments on commit c033f79

Please sign in to comment.
You can’t perform that action at this time.