Rotate camera in follow plane view only if space key is pressed

This commits merges the two orbit plane functions together in CTraffic.
The separation was necessary since XSwiftBus needed to be started manually
and until started, the pointer to CTraffic was invalid. Since XSwiftBus
is started automatically, it can be merged into one function again.
This commit also adds the feature to rotate the camera only if the space key
is pressed. This is equal to how FSX/P3D do it. Using the right mouse was
not possible, since X-Plane SDK does not offer that option.

ref T360
This commit is contained in:
Roland Winklmeier
2018-10-16 16:29:32 +02:00
committed by Klaus Basan
parent 46def05670
commit 9bb75a6f2e
4 changed files with 93 additions and 96 deletions

View File

@@ -51,7 +51,10 @@ namespace XSwiftBus
m_service->setDisappearMessageWindow(!checked);
});
m_planeViewSubMenu = m_menu.subMenu("Follow Plane View");
planeViewOwnAircraftMenuItem = m_planeViewSubMenu.item("Own Aircraft", [this] { switchToOwnAircraftView(); });
planeViewOwnAircraftMenuItem = m_planeViewSubMenu.item("Own Aircraft", [this]
{
m_traffic->setFollowedAircraft(m_traffic->ownAircraftString());
});
/*m_dbusThread = std::thread([this]()
{
@@ -141,18 +144,6 @@ namespace XSwiftBus
INFO_LOG("XSwiftBus started.");
}
void CPlugin::switchToOwnAircraftView()
{
/* This is the hotkey callback. First we simulate a joystick press and
* release to put us in 'free view 1'. This guarantees that no panels
* are showing and we are an external view. */
XPLMCommandButtonPress(xplm_joy_v_fr1);
XPLMCommandButtonRelease(xplm_joy_v_fr1);
/* Now we control the camera until the view changes. */
XPLMControlCamera(xplm_ControlCameraUntilViewChanges, orbitOwnAircraftFunc, this);
}
void CPlugin::onAircraftModelChanged()
{
if (m_service)
@@ -190,48 +181,4 @@ namespace XSwiftBus
if (plugin->m_traffic) { plugin->m_traffic->process(); }
return -1;
}
int CPlugin::orbitOwnAircraftFunc(XPLMCameraPosition_t *cameraPosition, int isLosingControl, void *refcon)
{
auto *plugin = static_cast<CPlugin *>(refcon);
if (isLosingControl == 1) { return 0; }
if (cameraPosition)
{
int w, h, x, y;
// First get the screen size and mouse location. We will use this to decide
// what part of the orbit we are in. The mouse will move us up-down and around.
// fixme: In a future update, change the orbit only while right mouse button is pressed.
XPLMGetScreenSize(&w, &h);
XPLMGetMouseLocation(&x, &y);
double heading = 360.0 * static_cast<double>(x) / static_cast<double>(w);
double pitch = 20.0 * ((static_cast<double>(y) / static_cast<double>(h)) * 2.0 - 1.0);
// Now calculate where the camera should be positioned to be 200
// meters from the plane and pointing at the plane at the pitch and
// heading we wanted above.
static const double PI = std::acos(-1);
double dx = -50.0 * sin(heading * PI / 180.0);
double dz = 50.0 * cos(heading * PI / 180.0);
double dy = -50.0 * tan(pitch * PI / 180.0);
double lx, ly, lz;
lx = plugin->m_ownAircraftPositionX.get();
ly = plugin->m_ownAircraftPositionY.get();
lz = plugin->m_ownAircraftPositionZ.get();
// Fill out the camera position info.
cameraPosition->x = static_cast<float>(lx + dx);
cameraPosition->y = static_cast<float>(ly + dy);
cameraPosition->z = static_cast<float>(lz + dz);
cameraPosition->pitch = static_cast<float>(pitch);
cameraPosition->heading = static_cast<float>(heading);
cameraPosition->roll = 0;
}
// Return 1 to indicate we want to keep controlling the camera.
return 1;
}
}