Files
EgtExecutor/PictureObj.cpp
T
Dario Sassi 9be9b80b8e EgtExecutor :
- al caricamento immagine per texture se non trovata si prova anche nel direttorio dell'eventuale macchina corrente.
2025-03-31 17:26:09 +02:00

381 lines
13 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2017-2017
//----------------------------------------------------------------------------
// File : PictureObj.cpp Data : 26.12.17 Versione : 1.8l3
// Contenuto : Oggetto custom Picture.
// Viene attaccato ad una Region rettangolare.
//
//
// Modifiche : 26.12.17 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "PictureObj.h"
#include "GseContext.h"
#include "EXE.h"
#include "/EgtDev/Include/EXeExecutor.h"
#include "/EgtDev/Include/EXeConst.h"
#include "/EgtDev/Include/EGrScene.h"
#include "/EgtDev/Include/EGkGdbConst.h"
#include "/EgtDev/Include/EGkUserObjFactory.h"
#include "/EgtDev/Include/EGkStringUtils3d.h"
#include "/EgtDev/Include/EGnStringKeyVal.h"
#include "/EgtDev/Include/EGnFileUtils.h"
// per far dimenticare macro di WinUser.h
#undef GetClassName
using namespace std ;
//----------------------------------------------------------------------------
static const int NKEY = 4 ;
static const string KEY_NAME = "N" ;
static const string KEY_PATH = "P" ;
static const string KEY_DIMX = "DX" ;
static const string KEY_DIMY = "DY" ;
//----------------------------------------------------------------------------
USEROBJ_REGISTER( "EXePicture", PictureObj) ;
//----------------------------------------------------------------------------
const string&
PictureObj::GetClassName( void) const
{
return USEROBJ_GETNAME( PictureObj) ;
}
//----------------------------------------------------------------------------
PictureObj*
PictureObj::Clone( void) const
{
// alloco oggetto
PictureObj* pImage = new( nothrow) PictureObj ;
// eseguo copia dei dati
if ( pImage != nullptr) {
try {
pImage->m_nOwnerId = GDB_ID_NULL ;
pImage->m_pGeomDB = nullptr ;
pImage->m_sName = m_sName ;
pImage->m_sPath = m_sPath ;
pImage->m_dDimX = m_dDimX ;
pImage->m_dDimY = m_dDimY ;
}
catch( ...) {
delete pImage ;
return nullptr ;
}
}
// ritorno l'oggetto
return pImage ;
}
//----------------------------------------------------------------------------
bool
PictureObj::Dump( string& sOut, bool bMM, const char* szNewLine) const
{
sOut += GetClassName() + szNewLine ;
sOut += "Id=" + ToString( m_nOwnerId) + szNewLine ;
sOut += "Name=" + m_sName + szNewLine ;
sOut += "Path=" + m_sPath + szNewLine ;
sOut += "Dim=" + ToString( m_dDimX) + "," + ToString( m_dDimY) + szNewLine ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::Save( int nBaseId, STRVECTOR& vString) const
{
// recupero nome del progetto corrente
string sFilePath ;
ExeGetCurrFilePath( sFilePath) ;
string sFileDir = GetDirectory( sFilePath) ;
ReplaceString( sFileDir, "/", "\\") ;
ToUpper( sFileDir) ;
string sFileTitle = GetFileTitleEgt( sFilePath) ;
// sostituzione degli eventuali riferimenti al direttorio e al nome del progetto nella path
string sPath = m_sPath ;
string sDir = GetDirectory( sPath) ;
ReplaceString( sDir, "/", "\\") ;
ToUpper( sDir) ;
string sName = GetFileName( sPath) ;
ReplaceString( sDir, sFileDir, SUB_PROJECT_DIR) ;
ReplaceString( sName, sFileTitle, SUB_PROJECT_TITLE) ;
sPath = sDir + "\\" + sName ;
try {
vString.insert( vString.begin(), NKEY, "") ;
if ( ! SetVal( KEY_NAME, m_sName, vString[0]))
return false ;
if ( ! SetVal( KEY_PATH, const_cast<const string&>( sPath), vString[1]))
return false ;
if ( ! SetVal( KEY_DIMX, m_dDimX, vString[2]))
return false ;
if ( ! SetVal( KEY_DIMY, m_dDimY, vString[3]))
return false ;
}
catch( ...) {
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::Load( const STRVECTOR& vString, int nBaseGdbId)
{
if ( int( vString.size()) < NKEY)
return false ;
if ( ! GetVal( vString[0], KEY_NAME, m_sName))
return false ;
if ( ! GetVal( vString[1], KEY_PATH, m_sPath))
return false ;
if ( ! GetVal( vString[2], KEY_DIMX, m_dDimX))
return false ;
if ( ! GetVal( vString[3], KEY_DIMY, m_dDimY))
return false ;
// se caricata scena, carico la texture
if ( GetCurrScene() != nullptr) {
// verifico l'esistenza della texture
if ( ExeExistsTexture( m_sName))
return true ;
// recupero nome e direttorio del progetto corrente
string sFilePath ;
ExeGetCurrFilePath( sFilePath) ;
string sFileDir = GetDirectory( sFilePath) ;
string sFileTitle = GetFileTitleEgt( sFilePath) ;
// sostituzione dell'eventuale riferimento al direttorio del progetto nella path
ReplaceString( m_sPath, SUB_PROJECT_DIR, sFileDir) ;
// sostituzione dell'eventuale riferimento al nome del progetto nella path
ReplaceString( m_sPath, SUB_PROJECT_TITLE, sFileTitle) ;
// carico la texture dalla path indicata
if ( ExistsFile( m_sPath)) {
if ( ! ExeLoadTexture( m_sName, m_sPath, 0, m_dDimX, m_dDimY, TXR_CLAMP)) {
string sOut = "Error loading image " + m_sPath ;
LOG_ERROR( GetLogger(), sOut.c_str())
}
return true ;
}
// provo dalla path del progetto corrente
string sOtherPath = sFileDir + "\\" + GetFileName( m_sPath) ;
if ( ExistsFile( sOtherPath)) {
if ( ! ExeLoadTexture( m_sName, sOtherPath, 0, m_dDimX, m_dDimY, TXR_CLAMP)) {
string sOut = "Error loading image " + sOtherPath ;
LOG_ERROR( GetLogger(), sOut.c_str())
}
return true ;
}
// provo dalla path della macchina corrente
string sMachineDir ;
if ( ExeGetCurrMachineDir( sMachineDir)) {
string sOtherPath = sMachineDir + "\\" + GetFileName( m_sPath) ;
if ( ExistsFile( sOtherPath)) {
if ( ! ExeLoadTexture( m_sName, sOtherPath, 0, m_dDimX, m_dDimY, TXR_CLAMP)) {
string sOut = "Error loading image " + sOtherPath ;
LOG_ERROR( GetLogger(), sOut.c_str())
}
return true ;
}
}
// altrimenti errore
{
string sOut = "Error missing image " + sOtherPath ;
LOG_ERROR( GetLogger(), sOut.c_str())
}
return true ;
}
// altrimenti ne verifico solo l'esistenza
else {
// recupero nome e direttorio del progetto corrente
string sFilePath ;
ExeGetCurrFilePath( sFilePath) ;
string sFileDir = GetDirectory( sFilePath) ;
string sFileTitle = GetFileTitleEgt( sFilePath) ;
// sostituzione dell'eventuale riferimento al direttorio del progetto nella path
ReplaceString( m_sPath, SUB_PROJECT_DIR, sFileDir) ;
// sostituzione dell'eventuale riferimento al nome del progetto nella path
ReplaceString( m_sPath, SUB_PROJECT_TITLE, sFileTitle) ;
// carico la texture dalla path indicata
if ( ExistsFile( m_sPath))
return true ;
// provo dalla path del progetto corrente
string sOtherPath = sFileDir + "\\" + GetFileName( m_sPath) ;
if ( ! ExistsFile( sOtherPath)) {
string sOut = "Error missing image " + sOtherPath ;
LOG_ERROR( GetLogger(), sOut.c_str())
}
return true ;
}
}
//----------------------------------------------------------------------------
bool
PictureObj::SetOwner( int nId, IGeomDB* pGDB)
{
m_nOwnerId = nId ;
m_pGeomDB = pGDB ;
return ( m_nOwnerId != GDB_ID_NULL && m_pGeomDB != nullptr) ;
}
//----------------------------------------------------------------------------
int
PictureObj::GetOwner( void) const
{
return m_nOwnerId ;
}
//----------------------------------------------------------------------------
IGeomDB*
PictureObj::GetGeomDB( void) const
{
return m_pGeomDB ;
}
//----------------------------------------------------------------------------
bool
PictureObj::Translate( const Vector3d& vtMove)
{
if ( m_pGeomDB == nullptr || m_nOwnerId == GDB_ID_NULL)
return false ;
// traslo il riferimento della texture
Frame3d frTxt ;
m_pGeomDB->GetTextureFrame( m_nOwnerId, frTxt) ;
frTxt.Translate( vtMove) ;
m_pGeomDB->SetTextureFrame( m_nOwnerId, frTxt) ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::Rotate( const Point3d& ptAx, const Vector3d& vtAx, double dCosAng, double dSinAng)
{
if ( m_pGeomDB == nullptr || m_nOwnerId == GDB_ID_NULL)
return false ;
// ruoto il riferimento della texture
Frame3d frTxt ;
m_pGeomDB->GetTextureFrame( m_nOwnerId, frTxt) ;
frTxt.Rotate( ptAx, vtAx, dCosAng, dSinAng) ;
m_pGeomDB->SetTextureFrame( m_nOwnerId, frTxt) ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::Scale( const Frame3d& frRef, double dCoeffX, double dCoeffY, double dCoeffZ)
{
IEGrScene* pScene = GetCurrScene() ;
if ( m_pGeomDB == nullptr || m_nOwnerId == GDB_ID_NULL || pScene == nullptr)
return false ;
// recupero il riferimento della texture
Frame3d frTxt ;
m_pGeomDB->GetTextureFrame( m_nOwnerId, frTxt) ;
// determino i vettori dimensioni della texture
//double dDimX, dDimY ;
//pScene->GetTextureDimensions( m_sName, dDimX, dDimY) ;
Vector3d vtDimX = frTxt.VersX() * m_dDimX ;
Vector3d vtDimY = frTxt.VersY() * m_dDimY ;
// pseudoscalo il riferimento della texture
frTxt.PseudoScale( frRef, dCoeffX, dCoeffY, dCoeffZ) ;
m_pGeomDB->SetTextureFrame( m_nOwnerId, frTxt) ;
// scalo le dimensioni della texture
vtDimX.Scale( frRef, dCoeffX, dCoeffY, dCoeffZ) ;
vtDimY.Scale( frRef, dCoeffX, dCoeffY, dCoeffZ) ;
m_dDimX = vtDimX.Len() ;
m_dDimY = vtDimY.Len() ;
pScene->ChangeTextureDimensions( m_sName, m_dDimX, m_dDimY) ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::ToGlob( const Frame3d& frRef)
{
if ( m_pGeomDB == nullptr || m_nOwnerId == GDB_ID_NULL)
return false ;
// porto in globale il riferimento della texture
Frame3d frTxt ;
m_pGeomDB->GetTextureFrame( m_nOwnerId, frTxt) ;
frTxt.ToGlob( frRef) ;
m_pGeomDB->SetTextureFrame( m_nOwnerId, frTxt) ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::ToLoc( const Frame3d& frRef)
{
if ( m_pGeomDB == nullptr || m_nOwnerId == GDB_ID_NULL)
return false ;
// porto in locale il riferimento della texture
Frame3d frTxt ;
m_pGeomDB->GetTextureFrame( m_nOwnerId, frTxt) ;
frTxt.ToLoc( frRef) ;
m_pGeomDB->SetTextureFrame( m_nOwnerId, frTxt) ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::LocToLoc( const Frame3d& frOri, const Frame3d& frDest)
{
if ( m_pGeomDB == nullptr || m_nOwnerId == GDB_ID_NULL)
return false ;
// porto da un locale all'altro il riferimento della texture
Frame3d frTxt ;
m_pGeomDB->GetTextureFrame( m_nOwnerId, frTxt) ;
frTxt.LocToLoc( frOri, frDest) ;
m_pGeomDB->SetTextureFrame( m_nOwnerId, frTxt) ;
return true ;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
PictureObj::PictureObj( void)
: m_nOwnerId( GDB_ID_NULL), m_pGeomDB( nullptr), m_dDimX( 0), m_dDimY( 0)
{
}
//----------------------------------------------------------------------------
bool
PictureObj::Set( const std::string& sName, const std::string& sPath, double dDimX, double dDimY)
{
m_sName = sName ;
m_sPath = sPath ;
m_dDimX = dDimX ;
m_dDimY = dDimY ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::GetName( string& sName) const
{
sName = m_sName ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::GetPath( string& sPath) const
{
sPath = m_sPath ;
return true ;
}
//----------------------------------------------------------------------------
bool
PictureObj::ChangePath( const string& sPath)
{
m_sPath = sPath ;
return ( ! m_sPath.empty()) ;
}
//----------------------------------------------------------------------------
bool
PictureObj::GetDimensions( double& dDimX, double& dDimY) const
{
dDimX = m_dDimX ;
dDimY = m_dDimY ;
return true ;
}