18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 02:56:01 +01:00

feat: cab controls can animate duplicate submodels (eu07/maszyna#527)

A control defined again in the same cab section of the .mmd with the
block key "duplicate: yes" no longer replaces the control: the first
definition stays the control (sounds, type, command feedback), and the
duplicate adds a copy with its own submodel, animation type and scale /
offset, following the control's value and active state. The duplicate's
sound keys are ignored so the control is not heard twice, and clicking
any copy works the control. This lets e.g. the ASF cab move its second
master controller and reverser with the first ones.

Without the key a repeated definition still replaces the earlier one,
as some of the game's .mmd files repeat controls with other submodels.
A duplicate with no definition of the control loaded from the current
cab's models before it is just the control.

clear_cab_controls() now also clears ggCompressorListButton, which kept
the previous cab's submodel.

The rule is in vehicle/gaugeduplicates.h, covered by
tests/unit/test_gauge_duplicates.cpp.

(eu07/maszyna#527)
This commit is contained in:
jerrrrycho
2026-11-07 23:19:53 +01:00
parent 68fdf1efdf
commit d259251373
5 changed files with 163 additions and 6 deletions

View File

@@ -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<model>(true, &cab, &cab, &lowpoly));
CHECK(gauge_duplicates::adds_copy<model>(true, &lowpoly, &cab, &lowpoly));
// a cab without a low poly interior
CHECK(gauge_duplicates::adds_copy<model>(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<model>(false, &cab, &cab, &lowpoly));
CHECK_FALSE(gauge_duplicates::adds_copy<model>(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<model>(true, nullptr, &cab, &lowpoly));
CHECK_FALSE(gauge_duplicates::adds_copy<model>(true, nullptr, &cab, nullptr));
// the control is left over from the cab loaded before
CHECK_FALSE(gauge_duplicates::adds_copy<model>(true, &previouscab, &cab, &lowpoly));
CHECK_FALSE(gauge_duplicates::adds_copy<model>(true, &previouscab, &cab, nullptr));
}

View File

@@ -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<TGauge> original;
if( gauge_duplicates::adds_copy<TModel3d>( 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<float>(mul);
}
TSubModel *submodel { nullptr };
m_sourcemodel = nullptr;
std::array<TModel3d *, 2> 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<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 == "type:" ) {
if( key == "duplicate:" ) {
// the definition is another submodel of the control defined before it, rather than its replacement
auto const duplicate { Input.getToken<std::string>( true, "\n\r\t ,;" ) };
Scratchpad.duplicate = gauge_duplicates::is_duplicate( duplicate );
}
else if( key == "type:" ) {
using enum TGaugeType;
auto const gaugetype { Input.getToken<std::string>( 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)
{

View File

@@ -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<TGauge> 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<float, 6> > 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
@@ -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<int, sound_source> m_soundfxvalues; // sounds associated with specific values
TModel3d const *m_sourcemodel { nullptr }; // the 3d model the submodel was found in by Load()
std::vector<TGauge> m_mirrors; // duplicate submodels of the control (duplicate: yes), animated along with it
};

View File

@@ -59,6 +59,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);
}
@@ -11266,6 +11271,7 @@ void TTrain::clear_cab_controls()
ggLeftEndLightButton.Clear();
ggRightEndLightButton.Clear();
ggLightsButton.Clear();
ggCompressorListButton.Clear();
// hunter-230112
ggRearLeftLightButton.Clear();
ggRearRightLightButton.Clear();

30
vehicle/gaugeduplicates.h Normal file
View File

@@ -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 <string_view>
// 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 <typename Model_> 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