16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-21 18:19:19 +02:00

Merge branch 'tmj-dev' into dev

This commit is contained in:
milek7
2017-12-25 18:27:10 +01:00
65 changed files with 5288 additions and 4558 deletions

143
Gauge.cpp
View File

@@ -19,26 +19,23 @@ http://mozilla.org/MPL/2.0/.
#include "Model3d.h"
#include "Timer.h"
#include "Logs.h"
#include "World.h"
#include "Train.h"
#include "renderer.h"
void TGauge::Init(TSubModel *NewSubModel, TGaugeType eNewType, double fNewScale, double fNewOffset, double fNewFriction, double fNewValue)
{ // ustawienie parametrów animacji submodelu
if (NewSubModel)
{ // warunek na wszelki wypadek, gdyby się submodel nie
// podłączył
if (NewSubModel) {
// warunek na wszelki wypadek, gdyby się submodel nie podłączył
fFriction = fNewFriction;
fValue = fNewValue;
fOffset = fNewOffset;
fScale = fNewScale;
SubModel = NewSubModel;
eType = eNewType;
if (eType == gt_Digital)
{
if (eType == gt_Digital) {
TSubModel *sm = SubModel->ChildGet();
do
{ // pętla po submodelach potomnych i obracanie ich o kąt zależy od
// cyfry w (fValue)
do {
// pętla po submodelach potomnych i obracanie ich o kąt zależy od cyfry w (fValue)
if (sm->pName.size())
{ // musi mieć niepustą nazwę
if (sm->pName[0] >= '0')
@@ -50,14 +47,17 @@ void TGauge::Init(TSubModel *NewSubModel, TGaugeType eNewType, double fNewScale,
}
else // a banan może być z optymalizacją?
NewSubModel->WillBeAnimated(); // wyłączenie ignowania jedynkowego transformu
float4x4 mat;
SubModel->ParentMatrix(&mat);
model_pos = *mat.TranslationGet();
// pass submodel location to defined sounds
auto const offset { model_offset() };
m_soundfxincrease.offset( offset );
m_soundfxdecrease.offset( offset );
for( auto &soundfxrecord : m_soundfxvalues ) {
soundfxrecord.second.offset( offset );
}
}
};
bool TGauge::Load(cParser &Parser, TModel3d *md1, TModel3d *md2, double mul) {
bool TGauge::Load( cParser &Parser, TDynamicObject const *Owner, TModel3d *md1, TModel3d *md2, double mul ) {
std::string submodelname, gaugetypename;
double scale, offset, friction;
@@ -76,24 +76,30 @@ bool TGauge::Load(cParser &Parser, TModel3d *md1, TModel3d *md2, double mul) {
else {
// new, block type config
// TODO: rework the base part into yaml-compatible flow style mapping
cParser mappingparser( Parser.getToken<std::string>( false, "}" ) );
submodelname = mappingparser.getToken<std::string>( false );
gaugetypename = mappingparser.getToken<std::string>( true );
mappingparser.getTokens( 3, false );
mappingparser
submodelname = Parser.getToken<std::string>( false );
gaugetypename = Parser.getToken<std::string>( true );
Parser.getTokens( 3, false );
Parser
>> scale
>> offset
>> friction;
// new, variable length section
while( true == Load_mapping( mappingparser ) ) {
while( true == Load_mapping( Parser ) ) {
; // all work done by while()
}
}
// bind defined sounds with the button owner
m_soundfxincrease.owner( Owner );
m_soundfxdecrease.owner( Owner );
for( auto &soundfxrecord : m_soundfxvalues ) {
soundfxrecord.second.owner( Owner );
}
scale *= mul;
TSubModel *submodel = md1->GetFromName( submodelname );
TSubModel *submodel = md1->GetFromName( submodelname );
if( scale == 0.0 ) {
ErrorLog( "Scale of 0.0 defined for sub-model \"" + submodelname + "\" in 3d model \"" + md1->NameGet() + "\". Forcing scale of 1.0 to prevent division by 0" );
ErrorLog( "Bad model: scale of 0.0 defined for sub-model \"" + submodelname + "\" in 3d model \"" + md1->NameGet() + "\". Forcing scale of 1.0 to prevent division by 0" );
scale = 1.0;
}
if (submodel) // jeśli nie znaleziony
@@ -101,7 +107,7 @@ bool TGauge::Load(cParser &Parser, TModel3d *md1, TModel3d *md2, double mul) {
else if (md2) // a jest podany drugi model (np. zewnętrzny)
submodel = md2->GetFromName(submodelname); // to może tam będzie, co za różnica gdzie
if( submodel == nullptr ) {
ErrorLog( "Failed to locate sub-model \"" + submodelname + "\" in 3d model \"" + md1->NameGet() + "\"" );
ErrorLog( "Bad model: failed to locate sub-model \"" + submodelname + "\" in 3d model \"" + md1->NameGet() + "\"" );
}
std::map<std::string, TGaugeType> gaugetypes {
@@ -122,26 +128,15 @@ bool TGauge::Load(cParser &Parser, TModel3d *md1, TModel3d *md2, double mul) {
bool
TGauge::Load_mapping( cParser &Input ) {
if( false == Input.getTokens( 2, true, ", \n\r\t" ) ) {
return false;
}
std::string key, value;
Input
>> key
>> value;
// token can be a key or block end
std::string const key { Input.getToken<std::string>( true, "\n\r\t ,;" ) };
if( ( true == key.empty() ) || ( key == "}" ) ) { return false; }
// if not block end then the key is followed by assigned value or sub-block
if( key == "soundinc:" ) {
m_soundfxincrease = (
value != "none" ?
sound_man->create_sound(value) :
nullptr );
m_soundfxincrease.deserialize( Input, sound_type::single );
}
else if( key == "sounddec:" ) {
m_soundfxdecrease = (
value != "none" ?
sound_man->create_sound(value) :
nullptr );
m_soundfxdecrease.deserialize( Input, sound_type::single );
}
else if( key.compare( 0, std::min<std::size_t>( key.size(), 5 ), "sound" ) == 0 ) {
// sounds assigned to specific gauge values, defined by key soundFoo: where Foo = value
@@ -150,9 +145,7 @@ TGauge::Load_mapping( cParser &Input ) {
if( indexstart != std::string::npos ) {
m_soundfxvalues.emplace(
std::stoi( key.substr( indexstart, indexend - indexstart ) ),
( value != "none" ?
sound_man->create_sound(value) :
nullptr ) );
sound_source( sound_placement::internal, EU07_SOUND_CABCONTROLSCUTOFFRANGE ).deserialize( Input, sound_type::single ) );
}
}
return true; // return value marks a key: value pair was extracted, nothing about whether it's recognized
@@ -182,9 +175,21 @@ void TGauge::DecValue(double fNewDesired)
fDesiredValue = 0;
};
void
TGauge::UpdateValue( double fNewDesired ) {
return UpdateValue( fNewDesired, nullptr );
}
void
TGauge::UpdateValue( double fNewDesired, sound_source &Fallbacksound ) {
return UpdateValue( fNewDesired, &Fallbacksound );
}
// ustawienie wartości docelowej. plays provided fallback sound, if no sound was defined in the control itself
void
TGauge::UpdateValue( double fNewDesired, sound* Fallbacksound ) {
TGauge::UpdateValue( double fNewDesired, sound_source *Fallbacksound ) {
auto const desiredtimes100 = static_cast<int>( std::round( 100.0 * fNewDesired ) );
if( static_cast<int>( std::round( 100.0 * ( fDesiredValue - fOffset ) / fScale ) ) == desiredtimes100 ) {
@@ -197,21 +202,25 @@ TGauge::UpdateValue( double fNewDesired, sound* Fallbacksound ) {
// filter out values other than full integers
auto const lookup = m_soundfxvalues.find( desiredtimes100 / 100 );
if( lookup != m_soundfxvalues.end() ) {
play( lookup->second );
lookup->second.play();
return;
}
}
// ...and if there isn't any, fall back on the basic set...
auto const currentvalue = GetValue();
if( ( currentvalue < fNewDesired ) && ( m_soundfxincrease != nullptr ) ) {
play( m_soundfxincrease );
if( ( currentvalue < fNewDesired )
&& ( false == m_soundfxincrease.empty() ) ) {
// shift up
m_soundfxincrease.play();
}
else if( ( currentvalue > fNewDesired ) && ( m_soundfxdecrease != nullptr ) ) {
play( m_soundfxdecrease );
else if( ( currentvalue > fNewDesired )
&& ( false == m_soundfxdecrease.empty() ) ) {
// shift down
m_soundfxdecrease.play();
}
else if( Fallbacksound != nullptr ) {
// ...and if that fails too, try the provided fallback sound from legacy system
play( Fallbacksound );
Fallbacksound->play();
}
};
@@ -288,8 +297,6 @@ void TGauge::Update() {
}
};
void TGauge::Render(){};
void TGauge::AssignFloat(float *fValue)
{
cDataType = 'f';
@@ -321,28 +328,12 @@ void TGauge::UpdateValue()
}
};
// todo: ugly approach to getting train translation
// returns offset of submodel associated with the button from the model centre
glm::vec3
TGauge::model_offset() const {
extern TWorld World;
void TGauge::play( sound *Sound )
{
if (!Sound)
return;
Sound->stop();
if (SubModel && World.train())
{
if (glm::length(model_pos) > 1.0f)
{
auto pos = glm::vec3(glm::vec4(model_pos, 1.0f) * glm::inverse((glm::mat4)World.train()->Dynamic()->mMatrix));
pos += (glm::vec3)World.train()->Dynamic()->GetPosition();
Sound->set_mode(sound::anchored).dist(3.0f).position(pos);
}
}
Sound->play();
return;
return (
SubModel != nullptr ?
SubModel->offset( 1.f ) :
glm::vec3() );
}