diff --git a/tests/unit/test_gauge_duplicates.cpp b/tests/unit/test_gauge_duplicates.cpp new file mode 100644 index 00000000..7bdbe909 --- /dev/null +++ b/tests/unit/test_gauge_duplicates.cpp @@ -0,0 +1,53 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/gaugeduplicates.h" + +// cab controls with more than one submodel (eu07/maszyna#527): a definition of a control in the .mmd marked +// "duplicate: yes" adds a copy of the control already defined in the cab, instead of replacing it. TGauge itself +// (3d models, sounds) is not part of the core library, the rule which decides it is + +namespace { + +// stand-ins for the 3d models of the cab +struct model {}; + +} // namespace + +TEST_CASE("gauge duplicates: the values of the duplicate key which mark a duplicate") { + CHECK(gauge_duplicates::is_duplicate("yes")); + CHECK(gauge_duplicates::is_duplicate("true")); + CHECK(gauge_duplicates::is_duplicate("1")); + CHECK_FALSE(gauge_duplicates::is_duplicate("no")); + CHECK_FALSE(gauge_duplicates::is_duplicate("false")); + CHECK_FALSE(gauge_duplicates::is_duplicate("0")); + CHECK_FALSE(gauge_duplicates::is_duplicate("")); +} + +TEST_CASE("gauge duplicates: a duplicate adds a copy to the control loaded from either model of the cab") { + model cab; + model lowpoly; + CHECK(gauge_duplicates::adds_copy(true, &cab, &cab, &lowpoly)); + CHECK(gauge_duplicates::adds_copy(true, &lowpoly, &cab, &lowpoly)); + // a cab without a low poly interior + CHECK(gauge_duplicates::adds_copy(true, &cab, &cab, nullptr)); +} + +TEST_CASE("gauge duplicates: a definition not marked as a duplicate replaces the control, as before") { + model cab; + model lowpoly; + CHECK_FALSE(gauge_duplicates::adds_copy(false, &cab, &cab, &lowpoly)); + CHECK_FALSE(gauge_duplicates::adds_copy(false, nullptr, &cab, &lowpoly)); +} + +TEST_CASE("gauge duplicates: with nothing loaded in this cab a duplicate is the control itself") { + model cab; + model lowpoly; + model previouscab; + // the control isn't defined before it + CHECK_FALSE(gauge_duplicates::adds_copy(true, nullptr, &cab, &lowpoly)); + CHECK_FALSE(gauge_duplicates::adds_copy(true, nullptr, &cab, nullptr)); + // the control is left over from the cab loaded before + CHECK_FALSE(gauge_duplicates::adds_copy(true, &previouscab, &cab, &lowpoly)); + CHECK_FALSE(gauge_duplicates::adds_copy(true, &previouscab, &cab, nullptr)); +} diff --git a/vehicle/Gauge.cpp b/vehicle/Gauge.cpp index af8e9a06..2190370e 100644 --- a/vehicle/Gauge.cpp +++ b/vehicle/Gauge.cpp @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/Gauge.h" #include "vehicle/gaugepositions.h" +#include "vehicle/gaugeduplicates.h" #include "utilities/parser.h" #include "model/Model3d.h" #include "vehicle/DynObj.h" @@ -109,6 +110,14 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu endvalue = -1; bool interpolatescale { false }; + // the control as defined so far in this cab, which a definition marked as its duplicate adds a copy to + // rather than replacing it (eu07/maszyna#527) + std::optional original; + if( gauge_duplicates::adds_copy( true, m_sourcemodel, Owner->mdKabina, Owner->mdLowPolyInt ) ) { + original = *this; + } + scratch_data scratchpad; + Parser.getTokens(); if( Parser.peek() != "{" ) { // old fixed size config @@ -148,7 +157,6 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu } // new, variable length section { - scratch_data scratchpad; while( true == Load_mapping( Parser, scratchpad ) ) { // all work done by while() } @@ -190,17 +198,20 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu endscale *= static_cast(mul); } TSubModel *submodel { nullptr }; + m_sourcemodel = nullptr; std::array sources { Owner->mdKabina, Owner->mdLowPolyInt }; for( auto const *source : sources ) { if( source != nullptr && ( submodel = source->GetFromName( submodelname ) ) != nullptr ) { // got what we wanted, bail out + m_sourcemodel = source; break; } // there's a possibility the default submodel was named with _off suffix if( source != nullptr && ( submodel = source->GetFromName( submodelname + "_off" ) ) != nullptr ) { // got what we wanted, bail out + m_sourcemodel = source; break; } } @@ -230,6 +241,14 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu Init( submodel, submodelon, type, scale, offset, friction, 0, endvalue, endscale, interpolatescale ); + if( scratchpad.duplicate && original ) { + // a duplicate submodel of the control already defined: the control stays as it was, with one more copy + // animated along with it. the duplicate's own sounds, if any, are left out so the control isn't heard twice + auto duplicate { MirrorCopy() }; + *this = std::move( *original ); + m_mirrors.emplace_back( std::move( duplicate ) ); + } + // return md2 != nullptr; // true, gdy podany model zewnętrzny, a w kabinie nie było }; @@ -240,7 +259,12 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) { auto const key { Input.getToken( 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 == "type:" ) { + if( key == "duplicate:" ) { + // the definition is another submodel of the control defined before it, rather than its replacement + auto const duplicate { Input.getToken( true, "\n\r\t ,;" ) }; + Scratchpad.duplicate = gauge_duplicates::is_duplicate( duplicate ); + } + else if( key == "type:" ) { using enum TGaugeType; auto const gaugetype { Input.getToken( true, "\n\r\t ,;" ) }; m_type = gaugetype == "push" ? push : @@ -376,6 +400,22 @@ void TGauge::Update( bool const Power ) { else { m_soundfxoff.play(); } } m_state = state; + UpdateVisibility(); + // update submodel animations + UpdateAnimation( SubModel ); + UpdateAnimation( SubModelOn ); + // the copies follow the control's value and state, each with its own animation + for( auto &mirror : m_mirrors ) { + mirror.m_value = m_value; + mirror.m_targetvalue = m_targetvalue; + mirror.m_state = m_state; + mirror.UpdateVisibility(); + mirror.UpdateAnimation( mirror.SubModel ); + mirror.UpdateAnimation( mirror.SubModelOn ); + } +}; + +void TGauge::UpdateVisibility() const { // toggle submodel visibility only if the active state submodel is present, // keep the default model always visible otherwise if( SubModelOn != nullptr ) { @@ -384,10 +424,26 @@ void TGauge::Update( bool const Power ) { SubModel->iVisible = !m_state; } } - // update submodel animations - UpdateAnimation( SubModel ); - UpdateAnimation( SubModelOn ); -}; +} + +TGauge TGauge::MirrorCopy() const { + + TGauge mirror; + mirror.SubModel = SubModel; + mirror.SubModelOn = SubModelOn; + mirror.m_animation = m_animation; + mirror.m_friction = m_friction; + mirror.m_targetvalue = m_targetvalue; + mirror.m_value = m_value; + mirror.m_offset = m_offset; + mirror.m_scale = m_scale; + mirror.m_endscale = m_endscale; + mirror.m_endvalue = m_endvalue; + mirror.m_interpolatescale = m_interpolatescale; + mirror.m_state = m_state; + mirror.m_sourcemodel = m_sourcemodel; + return mirror; +} void TGauge::AssignFloat(float *fValue) { diff --git a/vehicle/Gauge.h b/vehicle/Gauge.h index fdef9c58..d83668c4 100644 --- a/vehicle/Gauge.h +++ b/vehicle/Gauge.h @@ -55,6 +55,9 @@ public: void AssignBool(bool *bValue); void UpdateValue(); void AssignState( bool const *State ); + // copies of the control animated along with it, from its definitions marked duplicate: yes + std::vector const &mirrors() const { + return m_mirrors; } // returns offset of submodel associated with the button from the model centre glm::vec3 model_offset() const; TGaugeType type() const; @@ -74,6 +77,7 @@ private: // types struct scratch_data { std::optional< std::array > soundproofing; + bool duplicate { false }; // the definition is a duplicate submodel of the control defined before }; // methods // imports member data pair from the config file @@ -83,6 +87,12 @@ private: GetScaledValue() const; void UpdateAnimation( TSubModel *Submodel ) const; + // shows the default or the active state submodel as the state says + void + UpdateVisibility() const; + // the copy of this control's submodels and animation, without its sounds and bindings + TGauge + MirrorCopy() const; // members TGaugeAnimation m_animation { TGaugeAnimation::gt_Unknown }; // typ ruchu @@ -95,7 +105,7 @@ private: float m_endscale { -1.f }; // scale applied to the value at the end of accepted value range float m_endvalue { -1.f }; // end value of accepted value range bool m_interpolatescale { false }; - char m_datatype; // typ zmiennej parametru: f-float, d-double, i-int + char m_datatype { '\0' }; // typ zmiennej parametru: f-float, d-double, i-int, b-bool; none until one is assigned union { // wskaźnik na parametr pokazywany przez animację float *fData; @@ -112,6 +122,8 @@ private: sound_source m_soundfxincrease { m_soundtemplate }; // sound associated with increasing control's value sound_source m_soundfxdecrease { m_soundtemplate }; // sound associated with decreasing control's value std::map m_soundfxvalues; // sounds associated with specific values + TModel3d const *m_sourcemodel { nullptr }; // the 3d model the submodel was found in by Load() + std::vector m_mirrors; // duplicate submodels of the control (duplicate: yes), animated along with it }; diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 5dd745fe..63ec0cda 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -60,6 +60,11 @@ void control_mapper::insert(TGauge const &Gauge, std::string const &Label) { m_controlnames.try_emplace(Gauge.SubModelOn, Label); } + // the duplicate submodels of a control (duplicate: yes) work the control as well (eu07/maszyna#527) + for (auto const &mirror : Gauge.mirrors()) + { + insert(mirror, Label); + } m_names.emplace(Label); } @@ -11451,6 +11456,7 @@ void TTrain::clear_cab_controls() ggLeftEndLightButton.Clear(); ggRightEndLightButton.Clear(); ggLightsButton.Clear(); + ggCompressorListButton.Clear(); // hunter-230112 ggRearLeftLightButton.Clear(); ggRearRightLightButton.Clear(); diff --git a/vehicle/gaugeduplicates.h b/vehicle/gaugeduplicates.h new file mode 100644 index 00000000..3a20c930 --- /dev/null +++ b/vehicle/gaugeduplicates.h @@ -0,0 +1,30 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include + +// cab controls with more than one submodel (eu07/maszyna#527): a definition of a control in the .mmd marked +// "duplicate: yes" adds a copy of the control already defined in the cab, instead of replacing it +namespace gauge_duplicates { + +// whether the value of the duplicate: key marks the definition as a duplicate +inline bool is_duplicate(std::string_view const Value) { + return Value == "yes" || Value == "true" || Value == "1"; +} + +// whether a definition adds a copy to the control rather than replacing it: it is marked as a duplicate, and the +// control is already loaded from one of the 3d models of the cab being loaded (Cab, Lowpoly). a control left over +// from another cab, or not loaded at all (Loadedfrom null), has nothing in this cab to add the copy to +template bool adds_copy(bool const Duplicate, Model_ const *Loadedfrom, Model_ const *Cab, Model_ const *Lowpoly) { + return Duplicate && Loadedfrom != nullptr && (Loadedfrom == Cab || Loadedfrom == Lowpoly); +} + +} // namespace gauge_duplicates