Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Plugins/BuildingViewer/Common/BgViewer/viewerCtrl/buildingmanager.h
T

483 lines
13 KiB
C++
Executable File

#pragma once
#include <dtEntity/entityid.h>
#include <osg/BoundingBox>
#include <string>
#include <vector>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Matrix>
#include <osg/Geode>
#include <dtEntity/message.h>
#include <QMutex>
#include <QMutexLocker>
#include <QPolygonF>
#include <set>
#include <map>
#include <functional>
#include "viewerinterface.h"
#include "cameraviewposition.h"
#include "logger.h"
namespace dtEntity
{
class EntityManager;
}
namespace VCtrl
{
class IconComponent;
class TextComponent;
class ViewWidget;
struct OBB
{
osg::Matrix mMatrix;
osg::Matrix mMatrixInverse;
osg::BoundingBox mBB;
QPolygonF mPoly;
dtEntity::StringId mBuilding = 0;
};
struct BuildingData
{
std::string mDescription;
std::string mDisplayName;
std::string mFeatureId;
std::string mFileName;
std::string mBuildingXMLPath;
double mX = 0;
double mY = 0;
double mZ = 0;
double mOrientation = 0;
int mBuildingGroup = 0;
bool properlySpawned = false;
bool roomBBoxedLoaded = false;
};
struct BuildingLevel
{
dtEntity::EntityId mEntityId;
int mFloorId;
float mHeightAboveGround;
osg::Matrix mLocalToLevel;
osg::BoundingBox mBounds;
BuildingLevel(dtEntity::EntityId eid, int floorId, float h, const osg::BoundingBox &bb)
: mEntityId(eid), mFloorId(floorId), mHeightAboveGround(h), mBounds(bb)
{
}
};
struct PlayerPosition
{
osg::Vec2d mPosition;
dtEntity::EntityId mWorldPlayerId = 0;
};
struct PlayerDisplay
{
dtEntity::EntityId mWorldPlayerId = 0;
dtEntity::EntityId mLocalPlayerId = 0;
IconComponent *mIcon = nullptr;
};
struct Device
{
dtEntity::EntityId mWorldPlayerId = 0;
std::string mPath;
osg::Vec3d mPosition;
};
struct DeviceDisplay
{
Device *mDevice = nullptr;
dtEntity::EntityId mWorldPlayerId = 0;
dtEntity::EntityId mLocalPlayerId = 0;
};
struct Name
{
dtEntity::EntityId mWorldPlayerId;
std::string mtext;
std::string mdistext;
std::string mbuilding;
osg::Vec3d mPosition;
};
struct NameDisplay
{
Name* mName = nullptr;
dtEntity::EntityId mWorldPlayerId = 0;
dtEntity::EntityId mLocalPlayerId = 0;
bool visible = false;
};
class Shot
{
public:
Shot(const int32_t id,
const osg::Vec3d& shooterPos,
const osg::Vec3d& targetPos,
int buildingIdSrc,
int rommNumberSrc,
int buildingIdDst,
int rommNumberDst,
int lineType,
const osg::Vec4& color,
unsigned int duration_s,
unsigned int durationMax_s,
double simTimeCreated) :
mId(id)
, mShooterPos(shooterPos)
, mTargetPos(targetPos)
, mBuildingIdSrc(buildingIdSrc)
, mRoomNumberSrc(rommNumberSrc)
, mBuildingIdDst(buildingIdDst)
, mRoomNumberDst(rommNumberDst)
, mLineType(lineType)
, mColor(color)
, mDuration_s(duration_s)
, m_DurationMax_s(durationMax_s)
, mSimTimeCreated(simTimeCreated) {
}
void setDisplayTime(unsigned int duration_s) {
if(duration_s > m_DurationMax_s)
m_DurationMax_s = duration_s;
mDuration_s = duration_s;
}
double simTimeCreated() const {
return mSimTimeCreated;
}
double displayTime() const {
return mSimTimeCreated + mDuration_s;
}
double lifeTime() const {
return mSimTimeCreated + (m_DurationMax_s*2);
}
const osg::Vec3d& getShooterPos() const { return mShooterPos; }
const osg::Vec3d& getTargetPos() const { return mTargetPos; }
int getBuildingIdSrc() const { return mBuildingIdSrc; }
double getSimeTimeCreated() const { return mSimTimeCreated; }
int getRoomNumberSrc() const { return mRoomNumberSrc; }
int getBuildingIdDst() const { return mBuildingIdDst; }
int getRoomNumberDst() const { return mRoomNumberDst; }
int getDuration() const { return mDuration_s; }
int getLineType() const { return mLineType; }
const osg::Vec4& getColor() const { return mColor; }
int32_t getId() const { return mId; }
private:
NGL::INGLLogger &m_logger;
int32_t mId;
osg::Vec3d mShooterPos;
osg::Vec3d mTargetPos;
int mBuildingIdSrc;
int mRoomNumberSrc;
int mBuildingIdDst;
int mRoomNumberDst;
int mLineType;
osg::Vec4 mColor;
unsigned int mDuration_s, m_DurationMax_s;
double mSimTimeCreated;
};
struct ShotDisplay
{
Shot *mShot = nullptr;
osg::ref_ptr<osg::Geode> mGeode;
};
struct Building;
class OrthoMotionModelComponent;
struct BuildingDisplay
{
unsigned int mContextId = 0;
dtEntity::EntityId mAttachPointId = 0;
std::vector<BuildingLevel> mLevels;
osg::BoundingBox mWorldBounds;
osg::BoundingBox mLocalBounds;
dtEntity::EntityId mCameraId = 0;
dtEntity::EntityId mMotionModelId = 0;
OrthoMotionModelComponent *pMotion = nullptr;
dtEntity::EntityId mLightId = 0;
PlayerDisplayMode mPlayerDisplayMode = PlayerDisplayMode::Icons;
ViewMode mViewMode = ViewMode::ISOMETRIC;
std::vector<PlayerDisplay> mPlayers;
std::vector<ShotDisplay> mShotDisplays;
std::vector<DeviceDisplay> mDeviceDisplays;
std::vector<NameDisplay> mNameDisplays;
bool mLockRotation = false;
bool mNameMode = false;
bool mLineMode = false;
BuildingDisplay() = default;
BuildingDisplay(unsigned int contextid, ViewMode vm)
: mContextId(contextid)
, mViewMode(vm)
{
}
};
struct Building
{
dtEntity::EntityId mWorldBuildingId = 0;
std::vector<BuildingDisplay> mDisplays;
osg::Matrix mWorldToLocal;
osg::Matrix mLocalToWorld;
std::string mNavMeshGrpName;
std::vector<Shot *> mShots;
std::vector<Device *> mDevices;
std::string mBuildingDisplayName;
std::vector<Name*> mNames;
std::string mFeatureId;
};
class BuildingManager
{
public:
explicit BuildingManager(dtEntity::EntityManager &em);
~BuildingManager();
void addBuilding(dtEntity::EntityId buildingid, const std::string &featureId);
void deleteAllBuildings();
BuildingDisplay *getBuildingDisplay(unsigned int contextid);
void applyViewModeToBuildingParts(const Building &bldg, BuildingDisplay &dspl) const;
bool displayBuilding(const std::string& featureId, unsigned int contextid, dtEntity::EntityId buildingid, const osg::Vec4 &bgcolor, ViewMode viewmode, bool exactPos);
bool displayBuilding(unsigned int contextid, dtEntity::EntityId buildingid, const osg::Vec4 &bgcolor, ViewMode viewmode, bool exactPos);
bool setCameraPosition(unsigned int contextid, const BuildingViewer::CameraViewPosition& pos);
bool getCameraPosition(unsigned int contextid, BuildingViewer::CameraViewPosition& pos);
bool setDefaultCameraPosition(unsigned int contextid);
void closeBuildingDisplay(unsigned int contextid);
void changeViewMode(unsigned int contextId, ViewMode viewmode, bool useOriginViewPos = true);
void changeLineMode(unsigned int contextId, bool line);
void changeNameMode(unsigned int contextId, bool name);
void applyMapRotation(const Building &bd, BuildingDisplay &dspl);
void lockViewRotationToMapRotation(unsigned int contextId, bool lock);
void setPlayerDisplayMode(unsigned int contextId, PlayerDisplayMode mode);
void setWallTransparency(unsigned int contextId, float value);
float getMapRotation() const;
void setMapRotation(float angle);
void openOverview(unsigned int contextid);
void addPlayerToBuildings(const std::string &navmeshgrpname, dtEntity::EntityId eid,
const osg::Vec4 &color, const std::string &symbol,
const osg::Vec3d &pos, bool visible);
void removePlayerFromBuilding(const std::string &navmeshgrpname, dtEntity::EntityId eid, const std::string &roomname);
void updatePlayerPosition(const std::string &navmeshgrpname, dtEntity::EntityId eid,
const osg::Vec3d &pos, int roomNumber);
osg::Vec3d remapPosition(const Building &bd, const BuildingDisplay &dspl, const osg::Vec3d &world, int roomNumber = -1);
void setPlayerColor(dtEntity::EntityId eid, const osg::Vec4 &color);
void setPlayerIcon(dtEntity::EntityId eid, const std::string &path);
void reloadPlayerIcons();
void updatePlayerPartyColor(dtEntity::EntityId eid, const VCtrl::Color& col);
void setShowPlayerSelection(dtEntity::EntityId eid, bool show);
void setShowDisplayNamesSelection(dtEntity::EntityId eid, bool show);
void setShowHidePlayer(dtEntity::EntityId eid, bool show);
void setShowHideDisplayNames(dtEntity::EntityId eid, unsigned int contextId, bool show);
void setRoomDestructionValue(dtEntity::EntityId eid, const std::string &roomname, float value);
void setBuildingDestructionValue(dtEntity::EntityId eid, float value);
void setBuildingDisplayName(const std::string& featureId, dtEntity::EntityId eid, const std::string& strDisplayName);
void setBuildingDisplayName(dtEntity::EntityId eid, const std::string& strDisplayName);
void setRoomDisplayName(const std::string& featureId, dtEntity::EntityId eid, const std::string& roomname, const std::string& strDisplayName);
void setRoomDisplayName(Building *building, const std::string &roomname, const std::string& strDisplayName);
void removeShoot(int32_t id);
void showShoot(int32_t id, dtEntity::EntityId shooter, dtEntity::EntityId target,
const dtEntity::Vec3d &shooterPos, const dtEntity::Vec3d &targetPos, int buildingIdSrc, int roomNumberSrc,
int buildingIdDst, int roomNumberDst, int lineType, float duration, double simTimeCreated);
dtEntity::EntityId getWorldPlayerIdFromLocalPlayer(dtEntity::EntityId local) const;
dtEntity::EntityId getWorldNameIdFromLocalName(dtEntity::EntityId local, std::string &buildingName, std::string &room) const;
void addDeviceToBuilding(dtEntity::EntityId iconmainid, dtEntity::EntityId buildingid,
const osg::Vec3d &pos, const std::string &iconpath);
void removeDeviceFromBuilding(dtEntity::EntityId iconmainid);
bool AddRoomOBBs(const BuildingData &);
const std::map<std::string, VCtrl::OBB> &GetRoomOBBs() const { return mRooms; }
bool getRoomBounds(const std::string &bldgname, const std::string &roomname, VCtrl::OBB &obb) const;
bool LoadBuildings(const std::string &path, std::vector<BuildingData> &);
size_t getNumBuildings() const;
void getBuildingIds(std::vector<dtEntity::EntityId> &toFill) const;
dtEntity::EntityId getNextBuilding(const osg::Vec3d &pos, float &distance, std::string &featureId) const;
Building getBuildingFromFeatureId(const std::string &bldname) const;
using RoomPlayers = std::vector<PlayerPosition>;
osg::Vec2d AddRoomUsages(const std::string& bldgname, const std::string& roomname, VCtrl::PlayerPosition& playPos);
const std::map<std::string, RoomPlayers> &GetRoomUsages() const { return mUsedRooms; }
void deleteRoomUsagePlayer(const std::string& bldgname, const std::string& roomname, dtEntity::EntityId playerId);
void SetExactPos(bool exactPos) { mExactPos = exactPos; }
bool GetExactPos() const { return mExactPos; }
void updateShotDuration(unsigned int newDuration_s);
using CallbackCamPosChanged = std::function<void(unsigned int, const BuildingViewer::CameraViewPosition &)>;
void setCallbackCamPosChanged(const CallbackCamPosChanged &cb);
private:
NGL::INGLLogger &m_logger;
void updateShots(const Building &bd, BuildingDisplay &dspl);
void updateDevices(const Building &bd, BuildingDisplay &dspl);
void createRoomNames(dtEntity::EntityId buildingid, Building& building);
void updateNames(const Building& bd, BuildingDisplay& dspl);
void repositionCamera(const Building &bd, const BuildingDisplay &dspl, bool useOriginViewPos = true);
void updatePlayerPositions(const Building &bd, BuildingDisplay &dspl);
void addPlayerToBuilding(const Building &bd, BuildingDisplay &dspl, dtEntity::EntityId eid,
const osg::Vec4 &color, const std::string &symbol, const osg::Vec3d &pos,
bool visible);
static osg::Vec2d shiftPosition(const VCtrl::OBB& obb, const osg::Vec2d& pos, double& shiftValue);
void cameraViewPositionChanged(unsigned int contextId, const BuildingViewer::CameraViewPosition &viewPos);
std::set<unsigned int> m_setInitialCameraPositionSet;
std::map<std::string, std::map<std::string, std::string>> m_mapBuildingRoomNames;
std::map<std::string, std::string> m_mapBuildingDisplayNames;
dtEntity::EntityManager *mEntityManager;
std::vector<Building *> mBuildings;
QMutex m_mutexShots;
bool mShotOverview;
bool mExactPos;
float mMapRotation;
dtEntity::MessageFunctor mTickFunctor;
void Tick(const dtEntity::Message &message);
std::map<std::string, VCtrl::OBB> mRooms;
std::map<std::string, RoomPlayers> mUsedRooms;
CallbackCamPosChanged mCbCamPosChanged;
bool getFloorOfRoom(const std::string &bdlgFeatureId, int roomNumber, int &floorOut) const;
std::map<std::string, std::map<int,int> > mRoomToFloorPerBldg;
};
}