Change to Preset View in Code

From SIMboxWiki
Jump to navigation Jump to search

It is possible to change the camera view to a certain preset view in code by using the SelectWindow event. You need to supply the following minimal pieces of information:

1. Which cockpit you wish to view – in case you have multiple aircrafts in the scenario
2. Which window you wish to replace
3. What type of camera you wish to create (this is a base object name – CockpitView is the default)
4. The preset view name

Refer to the code sample below.

// get the current active scene. this will indicate which actual window to replace.
// You may use any other method to reference a scene by its entity ID.
int globalAttActiveSceneEntityId = 0;
GET_GLOBAL_ATT_BY_UNIT(_T("GLOBAL_ATT_ACTIVE_SCENE_ENTITY_ID"), globalAttActiveSceneEntityId, UNIT_TYPE_DEFAULT);

// get the cockpit view base object name - as this is configurable,
// you do not want to use a hard-coded value here
STRING globalPropCockpitViewName;
GET_GLOBAL_ATT(_T("GLOBAL_PROP_COCKPIT_VIEW_NAME"), globalPropCockpitViewName);

// get the selected (controlled) entity id. this will
// indicate which entity cockpit you want to view.
// You may use any other method to reference a cockpit by its parent entity ID.
int selectedEntityId = SimApi::Entities::getSelectedEntityId();

// select the required preset view name – avoid hard coded value here,
// make it configurable by a property or a global property
STRING cameraReferenceName = L"MyPresetNameHere"

// publish the event
SimApi::EventParams* eventParams = SimApi::EventsFactory::getEventInstance(_T("SelectWindow"));
eventParams->getParam(_T("CameraReferenceName"))->setValue( cameraReferenceName );
eventParams->getParam(_T("EntityID1"))->setValue( selectedEntityId );
eventParams->getParam(_T("SceneEntityId"))->setValue( globalAttActiveSceneEntityId );
eventParams->getParam(_T("SceneName"))->setValue( globalPropCockpitViewName );
SimApi::EventHandler::publishEvent(eventParams);