Files
qt5-legatus-legacy/Lts/Plugins/BuildingViewer/Common/BgViewer/viewerCtrl/viewerinterfaceimpl.h
T
2026-08-13 18:33:10 +02:00

525 lines
19 KiB
C++
Executable File

#pragma once
#include "viewerinterface.h"
#include "viewwidgetcontainer.h"
#include <dtEntity/mapcomponent.h>
#include <map>
#include <string>
#include <algorithm>
#include <QPointer>
#include <QWidget>
#include <dtEntity/mapcomponent.h>
#include <dtEntity/core.h>
#include <dtEntity/commandmessages.h>
#include <dtEntity/systemmessages.h>
#include <dtEntity/windowinterface.h>
#include <dtEntityOSG/cameracomponent.h>
#include <dtEntityOSG/layercomponent.h>
#include <dtEntityOSG/positionattitudetransformcomponent.h>
#include <dtEntityOSG/osgsysteminterface.h>
#include <dtEntityOSG/staticmeshcomponent.h>
#include <osg/io_utils>
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osg/ComputeBoundsVisitor>
#include <osgViewer/CompositeViewer>
#include "buildingmanager.h"
#include "exccommands.h"
#include "viewwidgetstate.h"
#include "logger.h"
namespace dtEntity
{
class EntityManager;
class MapSystem;
class Message;
}
namespace VCtrl
{
class PlayerSystem;
class BuildingManager;
class ViewWidget;
class LogHandler;
class CtrlMainWindow;
class CtrlImpl : public QObject, public ViewerInterface
{
Q_OBJECT
public:
CtrlImpl(dtEntity::EntityManager &em, QWidget *parentWidget);
~CtrlImpl();
void SetViewerCallback(ViewerCallback *) override;
void RemoveViewerCallback() override;
/**
* Is used to set location of project specific terrain and building data.
* Example: "/home/martin/ProjectAssetsBremen"
*/
void SetProjectDataPaths(const std::vector<std::string> &paths) override;
QWidget* getMainWindow() override;
quintptr requestMainWindowHandle() override;
void openViews(const std::vector<std::string> &bldIds, bool exactPos) override;
bool isViewOpen(const std::string &strBldId) const override;
void closeViews(const std::vector<std::string> &vecFeatureIDs) override;
void closeAllViews();
void UseExactPositioning(bool exactPos) override;
/**
* Open a new display showing the building identified by buildingid.
* @param buildingid ID of the building. Use GetBuildingIds() to get all building ids
* @param windowtitle Title to display on window top
* @param bgcolor Window background color
* @param viewmode The perspective mode to show building in
* @param screennum Number of screen to open display on
* @param fullscreen Ignore width and position and display as fullscreen instead
* @param x X-Position of window
* @param y Y-Position of window
* @param width Width of display in pixels
* @param height Height of display in pixels
* @param contextId receives context ID of the new view. Use it to close the view programmatically.
* @param exactPos true if positioningsystem gets exact position of player
* @return true if success
*/
bool OpenView(const std::string &buildingid, const std::string &windowtitle, const Color &col,
ViewMode viewmode, int screennum,
bool fullscreen, int x, int y, int w, int h, unsigned int &contextid, bool exactPos = true) override;
/**
* Close display previously opened with OpenView
* @param contextid context id of view, as returned from OpenView()
*/
void CloseView(unsigned int contextid) override;
/**
* Change view mode of an existing view.
* @param contextId context id of view, as returned from OpenView()
* @param viewmode Viewmode enum value
*/
void ChangeViewMode(unsigned int contextId, ViewMode viewmode) override;
/**
* Set if view should rotate in accord with map rotation
* @param contextId context id of view, as returned from OpenView()
* @param lock true if view should rotate, false if it should remain static
*/
void LockViewRotationToMapRotation(unsigned int contextId, bool lock) override;
/**
* Set if view should display movable players as dots or as icons
* @param contextId context id of view, as returned from OpenView()
* @param mode Display mode enumerator
*/
void SetPlayerDisplayMode(unsigned int contextId, PlayerDisplayMode mode) override;
/**
* Set wall transparency for a building window.
* @param contextId context id of view, as returned from OpenView()
* @param transparency 0 equals opaque, 1 equals invisible
*/
void SetWallTransparency(unsigned int contextId, float transparency) override;
/**
* All views with "LockViewRotationToMapRotation" enabled will rotate their camera
* to have a camera angle looking towards north, rotated by input angle.
* @returnRotation angle in degrees from north CCW
*/
float GetMapRotation() const override;
/**
* All views with "LockViewRotationToMapRotation" enabled will rotate their camera
* to have a camera angle looking towards north, rotated by input angle.
* @param angle Rotation angle in degrees from north CCW
*/
void SetMapRotation(float angle) override;
/**
* Open 3d view of whole terrain - for debugging purposes only!
*/
void OpenOverview() override;
/**
* add buildings to scene and recalculate navigation meshes
* @param path Load map from this path. example: "Projections/data.xml"
* @param loadAllAvailableBuildings If true, the path variable (=XML file with building configuration) will be ignored)
*/
bool LoadBuildingPositionMap(const std::string &path, bool loadAllAvailableBuildings) override;
void LoadAllBuildings();
/**
* Create new player at (0,0,0). Player is initially invisible!
* @param id a unique string that can later be used to reference the player
* @param iconpath system file path to player icon. Can be relative to ProjectAssets folder root.
* @param color Initial color of the player
*/
void CreatePlayer(const std::string &id, const std::string &iconpath, const Color &color) override;
/**
* Delete player with given id
* @param seconds Delete player n seconds from now
*/
void DeletePlayer(const std::string &id, float seconds = 0) override;
/**
* This color is added to the player texture color.
*/
void SetPlayerColor(const std::string &id, const Color &color) override;
/**
* Set icon path for existing player
* @param path system file path to player icon. Can be relative to ProjectAssets folder root.
* If path is to current icon path then icon is reloaded from file.
*/
void SetPlayerIcon(const std::string &id, const std::string &path) override;
void reloadPlayerIcons() override;
void UpdatePlayerPartyColors(const std::map<std::string, VCtrl::Color>& mapPlayerColors) override;
/**
* Return number of soldier instances
*/
size_t GetNumPlayers() const override;
/**
* Fill vector with ids of all existing players
*/
void GetPlayerIds(std::vector<std::string> &toFill) override;
/**
* Hide or unhide player with given id
* @param show true to show, false to hide
* @param when simulation time when show or hide should be executed
*/
void SetPlayerShown(const std::string &id, bool show, double when = -1) override;
/**
* Return number of building instances
*/
size_t GetNumBuildings() const override;
/**
* Fill vector with ids of all existing buildings
*/
void GetBuildingIds(std::vector<std::string> &toFill) const override;
/**
* Add or remove visible bounding box around entity
* @param id unique id of entity
* @param show set true to show box, false to make invisible
* @param duration If non-negative, remove box after duration seconds
*/
void SetShowSelectionBox(const std::string &id,
bool show,
float duration = -1) override;
/**
* Add a line from start to end
* Line is displayed by an array of sprites, placed
* in equal intervals.
* Sprites move toward target with given speed in m/s
* Line is deleted after duration seconds.
* If duration is 0 then line is never deleted
*/
void Shoot(const int32_t id,
const std::string &shooter,
const std::string &target,
const Vec3d &shooterPos,
const Vec3d &targetPos,
int buildingIdSrc, int roomNumberSrc, int buildingIdDst, int roomNumberDst,
int lineType,
double when) override;
void Shoot(const int32_t id,
const std::string &shooter,
const std::string &targetBuildingId,
const std::string &targetRoomId,
const Vec3d &shooterPos, int lineType,
double when) override;
void removeShoot(const int32_t id) override;
/**
* Directly move player to target position, do not use pathfinding. Cancels current path following activity.
* @param id Id of player
* @param when Simulation time when teleport should happen
* @param target position to teleport to
* @param bldgname name of building containing position, empty string to ignore
* @param roomname name of room containing position, empty string to ignore
*/
void Teleport(const std::string &id,
double when,
const Vec3d &target,
const std::string &bldgname,
int roomNumber,
const std::string &roomname,
ZValue zval) override;
/**
* Get names of all rooms in building
* @param id Id of building player
* @param names will be filled with room names
*/
void GetRoomNames(const std::string &id, std::vector<std::string> &names) override;
/**
* Sets destruction value for room of a building.
* @param id Id of building player
* @param roomnumber number of room to set (ex: "2001")
* @param value destruction value. 0: not destroyed, 1: fully destroyed
* @return true if room was found
*/
bool SetRoomDestructionValue(const std::string &id,
const std::string &roomname,
float value) override;
/**
* Sets destruction value for all rooms of a building.
* @param id Id of building player
* @param value destruction value. 0: not destroyed, 1: fully destroyed
* @return true if room was found
*/
bool SetBuildingDestructionValue(const std::string &id, float value) override;
/**
* Get names of all cameras, loudspeakers and other devices in building
* @param buildingid Id of building player
* @param names will be filled with device names
*/
void GetDeviceNames(const std::string &buildingId, std::vector<std::string> &names) override;
/**
* Show an icon at center position of device node
* @param id unique id for entity that can be used to hide it at a later point
* @param buildingid id of building containing the device
* @param devicename name of device node to show icon at
* @param iconpath absolute path to icon to show at device position
* @return true if device was found in building
*/
bool ShowDeviceIcon(const std::string &id, const std::string &buildingid,
const std::string &devicename, const std::string &iconpath) override;
/**
* Show an icon at center position of device node
* @param id unique id for entity that can be used to hide it at a later point
* @param buildingid id of building containing the device
* @param position Position of camera relative to building
* @param iconpath absolute path to icon to show at device position
* @return true if device was found in building
*/
bool ShowDeviceIcon(const std::string &id, const std::string &buildingid,
const Vec3d &position, const std::string &iconpath) override;
void ChangeLineMode(unsigned int contextId, bool line) override;
void ChangeNameMode(unsigned int contextId, bool name) override;
bool SetDisplayName(const std::string& buildingid, const std::string& roomname, const std::string& strDisplayName) override;
void ShowDisplayNames(const std::string& buildingid, bool show) override;
void SetShowDisplayNamesSelectionBox(const std::string& buildingid, const std::string& id, bool show, float duration = -1) override;
void setHitLineDuration(uint32_t durationSec) override;
void setMountingPointAsset(const MountingPointDescription &asset) override;
std::vector<BuildingState> getBuildingState() const override;
/**
* Hide device icon created with ShowDevice
* @param id Id used in call to ShowDevice
* @return true if device entity was found and successfully removed
*/
bool HideDeviceIcon(const std::string &id) override;
/**
* Get time scale. Scale 1 is normal, scale 2 is double normal speed
*/
float GetTimeScale() const override;
/**
* Scale time. Scale 1 is normal, scale 2 is double normal speed
*/
void SetTimeScale(float scale) override;
/**
* Set current simulation time in seconds
*/
void SetSimulationTime(double t) override;
/**
* Get current simulation time in seconds
*/
double GetSimulationTime() const override;
/**
* Pause simulation time and all events
*/
void SetPaused(bool) override;
/**
* Is simulation currently paused?
*/
bool GetPaused() const override;
/**
* Advance simulation until given simulation time is reached
*/
void FastForwardSimulationTo(double t) override;
/**
* Remove all queued commands for soldiers
*/
void DeleteQueuedCommands() override;
/**
* Switch on or off non realtime mode.
* Non realtime mode is used for recording videos at faster than real time speed.
* Frames are rendered and compressed as fast as possible, simulation time
* does not correspond to real time.
*/
void SetNonRealtimeMode(bool on) override;
/**
* Is non-realtime mode on or off?
*/
bool GetNonRealtimeMode() const override;
/**
* Start recording an mpeg video of the view.
* @param contextid context id of the view to record.
* @param path Record to this file. Extension should be .mpg
* @param w width of recorded video file (input is rescaled)
* @param h height of recorded video file (input is rescaled)
* @param framerate frames per second to record. Reasonable value: 15 - 30
* @param quality Compression quality in percent.
*/
bool StartVideoRecording(unsigned int contextid, const std::string &path,
int w, int h, int framerate, int quality) override;
bool StartVideoRecording(const std::string &buildingId, const std::string &path,
int w, int h, int framerate, int quality) override;
/**
* Stop recording
* @param contextid context id of the view to record.
*/
bool StopVideoRecording(unsigned int contextid) override;
bool StopVideoRecording(const std::string &buildingId) override;
void setTransparency(double percent) override;
void setViewMode(ViewMode m) override;
void setDisplayMode(PlayerDisplayMode m) override;
int GetFrameRate() const { return mFrameRate; }
void OnShowMsgBox(const std::string &strText);
bool allViewWindowsExposed() const
{
for (auto *vw : m_openedViewWidgets.getViewWidgets())
{
if (!vw->isOpenGlWindowExposed())
return false;
}
return !m_openedViewWidgets.getViewWidgets().empty();
}
private:
bool open(const std::string& strFeatureID, bool exactPos);
bool open(const std::vector<std::string> &vecFeatureIDs, bool exactPos);
bool openAll(bool exactPos);
void onEXCOpen(const QString& strIDs, bool exactPos);
void onEXCClose(const QString& strIDs);
void convertAssetsToPngFiles();
void OnTransformSend(const dtEntity::Message &m);
void OnMapLoaded(const dtEntity::Message &m);
void OnMapUnloaded(const dtEntity::Message &m);
void OnNavMeshGroupChanged(const dtEntity::Message &m);
void OnPlayerPicked(const dtEntity::Message &m);
void OnHideSelection(const dtEntity::Message &m);
//void OnRemoveShoot(const dtEntity::Message &m);
void OnShoot(const dtEntity::Message &m);
void OnShowHidePlayer(const dtEntity::Message &m);
void OnDisplayNameClicked(const dtEntity::Message& m);
bool SpawnBuilding(const BuildingData &b, bool useStaticMeshesFolder = true);
void LoadBuilding(BuildingData& bd, bool useStaticMeshesFolder);
void deleteAllBuildings();
bool getBuildingString(const std::string &strFeatureID, std::string *strBuildingNameID);
bool OpenView(unsigned int contextId);
bool CreateView(dtEntity::EntityId bid, const std::string &windowtitle, const Color &col, ViewMode viewmode,
int screennum, bool fullscreen, int x, int y, int w, int h,
unsigned int &contextid, bool exactPos);
QPointer<CtrlMainWindow> m_pMainWindow;
dtEntity::EntityManager *mEntityManager;
ViewerCallback *mViewerCallback;
LogHandler* mLogHandler = nullptr;
dtEntity::MapSystem *mMapSystem;
PlayerSystem *mPlayerSystem;
BuildingManager *mBuildingManager;
float mTimeScale;
double mSimTime;
bool mPaused;
bool mNonRealtimeMode;
int mFrameRate; // only for non realtime mode
std::map<unsigned int, ViewWidget *> mViewWidgets; // contextIDs to ViewWidgets
ViewWidget *GetViewWidget(unsigned int contextid);
void storeWidget(ViewWidget *pWidget, const std::string &strBuildingNameID, unsigned int contextid, VCtrl::ViewMode vmode, VCtrl::PlayerDisplayMode displayMode);
const std::size_t m_maxParallelViewWidgets = 8;
// Global setting for all ViewWidgets
unsigned int m_hitlineDuration_s = 10;
double m_desiredTransparencyValue = 0.75;
bool m_showHitLines = true;
ViewWidget *CreateMainWindowView(dtEntity::EntityId bid, const std::string &featureId, unsigned int &contextid, bool exactPos);
std::unordered_map<std::string, BuildingData> m_loadedBuildings; // Feature_IDs
ViewWidgetContainer m_openedViewWidgets;
bool m_isInFullscreenMode = false;
// Global Settings valid for all newly opened widgets
ViewMode m_desiredViewMode = ViewMode::ORTHOGRAPHIC;
VCtrl::PlayerDisplayMode m_desiredPlayerDisplayMode = VCtrl::PlayerDisplayMode::Icons;
void CloseView(ViewWidget *pWidget);
void onExcRestoreViewState(const std::string &strFeatureID);
void restoreViewWidgetStates(const std::vector<BuildingViewer::ViewWidgetState> &vecStates);
void restoreViewWidgetStateDeferred(ViewWidget *pViewWidget);
std::unordered_map<std::string, BuildingViewer::ViewWidgetState> m_deferredViewStates;
QJsonValue getViewState() const override;
void setViewState(const QJsonValue &val) override;
void highlightView(const std::string &buildingId) override;
void setBuildingDisplayName(const std::string &buildingId, const std::string &displayName) override;
//NGL::INGLLogger &m_logger;
private slots:
void onViewWidget_MaximizeRequest(ViewWidget *pWidget);
void onViewWidget_MinimizeRequest(ViewWidget *pWidget);
void onViewWidget_CloseRequest(ViewWidget *pWidget);
void onViewWidget_FocusRequest(ViewWidget *pWidget);
void onViewWidget_Zoom(ViewWidget *pWidget, bool zoomIn);
};
}