mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
feat: drag universal cab controls to any position with the mouse (#62)
A universal control (window, door, sun blind) whose definition in the .mmd
has the new optional key drag: can be dragged with the left mouse button to
any position between its ends, and stays where it is let go:
universal1: { okno_p mov -0.71 0 0.3 drag: left soundinc: ... }
The value is the direction the mouse moves to open the control: up, down,
left or right (yes = up, no = off). The drag works like the sliders of the
controllers: the cursor is hidden, its move over 0.6 of the window height
takes the control from one end to the other. A click without dragging
toggles the control as before, and so do the keys: to the end farther from
where it is. Push controls aren't dragged. Without the key every universal
works as it did; none of the cab definitions has it yet.
New commands genericset0..29 set universal N to the position in param1
(0-1); the mouse sends them, and they can be bound to a gamepad axis.
The rules (the key's values, the move of the cursor along the direction,
the ends, click or drag, the keys) are in vehicle/universaldrag.h, with
unit tests.
This commit is contained in:
@@ -416,6 +416,36 @@ commanddescription_sequence Commands_descriptions = {
|
||||
{"linebreakeropenc", command_target::vehicle, command_mode::oneoff},
|
||||
{"linebreakerclosec", command_target::vehicle, command_mode::oneoff},
|
||||
{"repaircoupler", command_target::simulation, command_mode::oneoff},
|
||||
{"genericset0", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset1", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset2", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset3", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset4", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset5", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset6", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset7", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset8", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset9", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset10", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset11", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset12", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset13", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset14", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset15", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset16", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset17", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset18", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset19", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset20", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset21", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset22", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset23", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset24", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset25", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset26", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset27", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset28", command_target::vehicle, command_mode::oneoff},
|
||||
{"genericset29", command_target::vehicle, command_mode::oneoff},
|
||||
};
|
||||
|
||||
// Maps of command and coresponding strings
|
||||
@@ -811,6 +841,36 @@ std::unordered_map<std::string, user_command> const commandMap = {
|
||||
{"linebreakeropenc", user_command::linebreakeropenc},
|
||||
{"linebreakerclosec", user_command::linebreakerclosec},
|
||||
{"repaircoupler", user_command::repaircoupler},
|
||||
{"genericset0", user_command::genericset0},
|
||||
{"genericset1", user_command::genericset1},
|
||||
{"genericset2", user_command::genericset2},
|
||||
{"genericset3", user_command::genericset3},
|
||||
{"genericset4", user_command::genericset4},
|
||||
{"genericset5", user_command::genericset5},
|
||||
{"genericset6", user_command::genericset6},
|
||||
{"genericset7", user_command::genericset7},
|
||||
{"genericset8", user_command::genericset8},
|
||||
{"genericset9", user_command::genericset9},
|
||||
{"genericset10", user_command::genericset10},
|
||||
{"genericset11", user_command::genericset11},
|
||||
{"genericset12", user_command::genericset12},
|
||||
{"genericset13", user_command::genericset13},
|
||||
{"genericset14", user_command::genericset14},
|
||||
{"genericset15", user_command::genericset15},
|
||||
{"genericset16", user_command::genericset16},
|
||||
{"genericset17", user_command::genericset17},
|
||||
{"genericset18", user_command::genericset18},
|
||||
{"genericset19", user_command::genericset19},
|
||||
{"genericset20", user_command::genericset20},
|
||||
{"genericset21", user_command::genericset21},
|
||||
{"genericset22", user_command::genericset22},
|
||||
{"genericset23", user_command::genericset23},
|
||||
{"genericset24", user_command::genericset24},
|
||||
{"genericset25", user_command::genericset25},
|
||||
{"genericset26", user_command::genericset26},
|
||||
{"genericset27", user_command::genericset27},
|
||||
{"genericset28", user_command::genericset28},
|
||||
{"genericset29", user_command::genericset29},
|
||||
{"none", user_command::none}};
|
||||
|
||||
} // namespace simulation
|
||||
|
||||
@@ -414,6 +414,37 @@ enum class user_command
|
||||
linebreakeropenc,
|
||||
linebreakerclosec,
|
||||
repaircoupler, // the broken coupler of the vehicle (payload) at the end (param1)
|
||||
// sets universal control N to the position given in param1, 0-1 (MaSzyna-EU07/maszyna#62)
|
||||
genericset0,
|
||||
genericset1,
|
||||
genericset2,
|
||||
genericset3,
|
||||
genericset4,
|
||||
genericset5,
|
||||
genericset6,
|
||||
genericset7,
|
||||
genericset8,
|
||||
genericset9,
|
||||
genericset10,
|
||||
genericset11,
|
||||
genericset12,
|
||||
genericset13,
|
||||
genericset14,
|
||||
genericset15,
|
||||
genericset16,
|
||||
genericset17,
|
||||
genericset18,
|
||||
genericset19,
|
||||
genericset20,
|
||||
genericset21,
|
||||
genericset22,
|
||||
genericset23,
|
||||
genericset24,
|
||||
genericset25,
|
||||
genericset26,
|
||||
genericset27,
|
||||
genericset28,
|
||||
genericset29,
|
||||
none = -1
|
||||
};
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ void
|
||||
mouse_slider::bind( user_command const &Command ) {
|
||||
|
||||
m_command = Command;
|
||||
m_universal.reset();
|
||||
m_universaldrag.reset();
|
||||
|
||||
auto const *train { simulation::Train };
|
||||
TMoverParameters const *vehicle { nullptr };
|
||||
@@ -124,18 +126,47 @@ mouse_slider::bind( user_command const &Command ) {
|
||||
m_analogue ? controledge - m_value * controlsize : controledge - m_value * stepsize - 0.5 * stepsize );
|
||||
}
|
||||
|
||||
void
|
||||
mouse_slider::bind_universal( int const Index, universal_drag::direction const Drag, float const Position ) {
|
||||
|
||||
m_command = static_cast<user_command>( static_cast<int>( user_command::genericset0 ) + Index );
|
||||
m_universal = Index;
|
||||
m_universaldrag.emplace( Drag, Position );
|
||||
m_value = m_universaldrag->position();
|
||||
m_valuerange = 1.0;
|
||||
m_analogue = true;
|
||||
m_invertrange = false;
|
||||
// the cursor is hidden for the drag and put back where it was when the drag ends; the drag follows its moves
|
||||
// from wherever it is, so it isn't placed anywhere
|
||||
m_cursorposition = glm::dvec2( Global.cursor_pos );
|
||||
Application.set_cursor( GLFW_CURSOR_DISABLED );
|
||||
}
|
||||
|
||||
void
|
||||
mouse_slider::release() {
|
||||
|
||||
m_command = user_command::none;
|
||||
m_universal.reset();
|
||||
m_universaldrag.reset();
|
||||
Application.set_cursor_pos( m_cursorposition.x, m_cursorposition.y );
|
||||
Application.set_cursor( GLFW_CURSOR_NORMAL );
|
||||
}
|
||||
|
||||
void
|
||||
mouse_slider::on_move( double const /*Mousex*/, double const Mousey ) {
|
||||
bool
|
||||
mouse_slider::on_move( double const Mousex, double const Mousey ) {
|
||||
|
||||
auto const controlsize { Global.window_size.y * EU07_CONTROLLER_MOUSESLIDERSIZE };
|
||||
|
||||
if( m_universaldrag ) {
|
||||
// a universal control, dragged the same way as the controllers: the move of the cursor over the size of the
|
||||
// slider takes it from one end to the other
|
||||
if( false == m_universaldrag->move( Mousex, Mousey, controlsize ) ) {
|
||||
return false;
|
||||
}
|
||||
m_value = m_universaldrag->position();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto const controledge { Global.window_size.y * 0.5 + controlsize * 0.5 };
|
||||
auto const stepsize { controlsize / m_valuerange };
|
||||
|
||||
@@ -143,6 +174,7 @@ mouse_slider::on_move( double const /*Mousex*/, double const Mousey ) {
|
||||
m_value = m_analogue ? (controledge - mousey) / controlsize : std::floor((controledge - mousey) / stepsize);
|
||||
if( m_invertrange ) {
|
||||
m_value = ( m_analogue ? 1.0 : m_valuerange ) - m_value; }
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -246,8 +278,8 @@ drivermouse_input::move( double Mousex, double Mousey ) {
|
||||
}
|
||||
else {
|
||||
// control picking mode
|
||||
if( m_slider.command() != user_command::none ) {
|
||||
m_slider.on_move( Mousex, Mousey );
|
||||
if( m_slider.command() != user_command::none
|
||||
&& true == m_slider.on_move( Mousex, Mousey ) ) {
|
||||
m_relay.post(
|
||||
m_slider.command(),
|
||||
m_slider.value(),
|
||||
@@ -341,7 +373,14 @@ drivermouse_input::button( int const Button, int const Action ) {
|
||||
else {
|
||||
m_pickwaiting = false;
|
||||
if (Button == GLFW_MOUSE_BUTTON_LEFT && m_slider.command() != user_command::none) {
|
||||
m_relay.post( m_slider.command(), 0, 0, Action, 0 );
|
||||
if( auto const universal { m_slider.universal() };
|
||||
universal && m_slider.is_click() ) {
|
||||
// a universal control clicked rather than dragged goes to its end, as a click on any universal does
|
||||
toggle_universal( *universal );
|
||||
}
|
||||
else {
|
||||
m_relay.post( m_slider.command(), 0, 0, Action, 0 );
|
||||
}
|
||||
m_slider.release();
|
||||
}
|
||||
// if it's the right mouse button that got released and we had no command active, we were potentially in view panning mode; stop it
|
||||
@@ -392,6 +431,25 @@ drivermouse_input::button( int const Button, int const Action ) {
|
||||
default: { break; }
|
||||
}
|
||||
}
|
||||
// a universal control with drag: in its definition is dragged to any position between its ends,
|
||||
// and a click without dragging toggles it as before (MaSzyna-EU07/maszyna#62)
|
||||
if( Button == GLFW_MOUSE_BUTTON_LEFT ) {
|
||||
auto const universal { universal_index( mousecommand ) };
|
||||
auto const drag {
|
||||
universal ?
|
||||
simulation::Train->universal_drag_direction( *universal ) :
|
||||
universal_drag::direction::none };
|
||||
if( drag != universal_drag::direction::none ) {
|
||||
mousecommand = none;
|
||||
if( false == pickwaiting ) {
|
||||
// the button was let go before the control was found under the cursor: a click
|
||||
toggle_universal( *universal );
|
||||
return;
|
||||
}
|
||||
m_slider.bind_universal( *universal, drag, simulation::Train->universal_position( *universal ) );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch( mousecommand ) {
|
||||
case mastercontrollerincrease:
|
||||
@@ -1162,4 +1220,23 @@ drivermouse_input::adjust_command( user_command Command ) const {
|
||||
return Command;
|
||||
}
|
||||
|
||||
std::optional<int>
|
||||
drivermouse_input::universal_index( user_command const Command ) {
|
||||
|
||||
auto const index { static_cast<int>( Command ) - static_cast<int>( user_command::generictoggle0 ) };
|
||||
if( index < 0
|
||||
|| index > static_cast<int>( user_command::generictoggle29 ) - static_cast<int>( user_command::generictoggle0 ) ) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
void
|
||||
drivermouse_input::toggle_universal( int const Index ) const {
|
||||
|
||||
auto const toggle { static_cast<user_command>( static_cast<int>( user_command::generictoggle0 ) + Index ) };
|
||||
m_relay.post( toggle, 0, 0, GLFW_PRESS, 0 );
|
||||
m_relay.post( toggle, 0, 0, GLFW_RELEASE, 0 );
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -9,8 +9,10 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include "input/command.h"
|
||||
#include "vehicle/universaldrag.h"
|
||||
|
||||
// virtual slider; value determined by position of the mouse
|
||||
class mouse_slider {
|
||||
@@ -21,9 +23,14 @@ public:
|
||||
// methods
|
||||
void
|
||||
bind( user_command const &Command );
|
||||
// binds the slider to universal control Index, which the mouse drags in the Drag direction from its Position
|
||||
// to any position between its ends (MaSzyna-EU07/maszyna#62)
|
||||
void
|
||||
bind_universal( int const Index, universal_drag::direction const Drag, float const Position );
|
||||
void
|
||||
release();
|
||||
void
|
||||
// returns whether the value changed
|
||||
bool
|
||||
on_move( double const Mousex, double const Mousey );
|
||||
inline
|
||||
user_command
|
||||
@@ -32,10 +39,19 @@ public:
|
||||
double
|
||||
value() const {
|
||||
return m_value; }
|
||||
// the universal control the slider drags, if any, and whether letting it go now is a click rather than a drag
|
||||
std::optional<int>
|
||||
universal() const {
|
||||
return m_universal; }
|
||||
bool
|
||||
is_click() const {
|
||||
return m_universaldrag && m_universaldrag->is_click(); }
|
||||
|
||||
private:
|
||||
// members
|
||||
user_command m_command { user_command::none };
|
||||
std::optional<int> m_universal; // the universal control dragged, if any
|
||||
std::optional<universal_drag::drag> m_universaldrag;
|
||||
double m_value { 0.0 };
|
||||
double m_valuerange { 0.0 };
|
||||
bool m_analogue { false };
|
||||
@@ -92,6 +108,12 @@ private:
|
||||
// potentially replaces supplied command with a more relevant one
|
||||
user_command
|
||||
adjust_command( user_command Command ) const;
|
||||
// the universal control a command toggles, if it toggles one
|
||||
static std::optional<int>
|
||||
universal_index( user_command const Command );
|
||||
// toggles universal control Index, as a click on it does
|
||||
void
|
||||
toggle_universal( int const Index ) const;
|
||||
|
||||
// members
|
||||
command_relay m_relay;
|
||||
|
||||
143
tests/unit/test_universal_drag.cpp
Normal file
143
tests/unit/test_universal_drag.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "vehicle/universaldrag.h"
|
||||
|
||||
// smooth regulation of the universal cab controls (MaSzyna-EU07/maszyna#62): a universal with "drag: <direction>"
|
||||
// in its definition is dragged with the mouse to any position between its ends and stays there when let go; a click
|
||||
// and the keys move it to its ends. the mouse and TGauge are not part of the core library, the rules they follow are
|
||||
|
||||
using universal_drag::direction;
|
||||
|
||||
TEST_CASE("universal drag: the values of the drag key") {
|
||||
CHECK(universal_drag::parse("up") == direction::up);
|
||||
CHECK(universal_drag::parse("down") == direction::down);
|
||||
CHECK(universal_drag::parse("left") == direction::left);
|
||||
CHECK(universal_drag::parse("right") == direction::right);
|
||||
// yes drags it up, as the sliders of the controllers
|
||||
CHECK(universal_drag::parse("yes") == direction::up);
|
||||
CHECK(universal_drag::parse("true") == direction::up);
|
||||
CHECK(universal_drag::parse("1") == direction::up);
|
||||
CHECK(universal_drag::parse("no") == direction::none);
|
||||
CHECK(universal_drag::parse("false") == direction::none);
|
||||
CHECK(universal_drag::parse("0") == direction::none);
|
||||
CHECK(universal_drag::parse("none") == direction::none);
|
||||
// a value it doesn't know is reported, and the control can't be dragged
|
||||
CHECK_FALSE(universal_drag::parse("sideways").has_value());
|
||||
CHECK_FALSE(universal_drag::parse("").has_value());
|
||||
CHECK_FALSE(universal_drag::parse("Up").has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: a push universal can't be dragged, it springs back when let go") {
|
||||
CHECK(universal_drag::effective(direction::up, true) == direction::none);
|
||||
CHECK(universal_drag::effective(direction::left, true) == direction::none);
|
||||
CHECK(universal_drag::effective(direction::up, false) == direction::up);
|
||||
CHECK(universal_drag::effective(direction::none, false) == direction::none);
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: the move of the cursor along the direction") {
|
||||
auto const size{600.0};
|
||||
// the cursor's y grows downwards
|
||||
CHECK(universal_drag::along(direction::up, 0.0, -60.0, size) == doctest::Approx(0.1));
|
||||
CHECK(universal_drag::along(direction::up, 0.0, 60.0, size) == doctest::Approx(-0.1));
|
||||
CHECK(universal_drag::along(direction::down, 0.0, 60.0, size) == doctest::Approx(0.1));
|
||||
CHECK(universal_drag::along(direction::left, -150.0, 0.0, size) == doctest::Approx(0.25));
|
||||
CHECK(universal_drag::along(direction::right, -150.0, 0.0, size) == doctest::Approx(-0.25));
|
||||
// the move across the direction doesn't count
|
||||
CHECK(universal_drag::along(direction::up, 300.0, 0.0, size) == 0.0);
|
||||
CHECK(universal_drag::along(direction::left, 0.0, 300.0, size) == 0.0);
|
||||
// nothing to drag
|
||||
CHECK(universal_drag::along(direction::none, 100.0, 100.0, size) == 0.0);
|
||||
CHECK(universal_drag::along(direction::up, 0.0, -60.0, 0.0) == 0.0);
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: the position is kept between the ends") {
|
||||
CHECK(universal_drag::clamp(-0.5) == 0.f);
|
||||
CHECK(universal_drag::clamp(1.5) == 1.f);
|
||||
CHECK(universal_drag::clamp(0.25) == doctest::Approx(0.25f));
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: the keys and a click move the control to the end farther from its position") {
|
||||
CHECK(universal_drag::toggled(0.f) == 1.f);
|
||||
CHECK(universal_drag::toggled(1.f) == 0.f);
|
||||
// a control left in between
|
||||
CHECK(universal_drag::toggled(0.3f) == 1.f);
|
||||
CHECK(universal_drag::toggled(0.7f) == 0.f);
|
||||
// half way, as the two-state universals always did
|
||||
CHECK(universal_drag::toggled(0.5f) == 0.f);
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: a drag takes the control to a position between its ends, where it stays") {
|
||||
auto const size{600.0};
|
||||
universal_drag::drag drag{direction::up, 0.f};
|
||||
// the first position of the cursor is where the drag starts from, even if the hidden cursor was moved
|
||||
CHECK_FALSE(drag.move(400.0, 500.0, size));
|
||||
CHECK(drag.position() == 0.f);
|
||||
CHECK(drag.is_click());
|
||||
// up by a quarter of the size
|
||||
CHECK(drag.move(400.0, 350.0, size));
|
||||
CHECK(drag.position() == doctest::Approx(0.25f));
|
||||
// sideways it stays
|
||||
CHECK_FALSE(drag.move(700.0, 350.0, size));
|
||||
CHECK(drag.position() == doctest::Approx(0.25f));
|
||||
// back down a little
|
||||
CHECK(drag.move(700.0, 380.0, size));
|
||||
CHECK(drag.position() == doctest::Approx(0.2f));
|
||||
CHECK_FALSE(drag.is_click());
|
||||
// let go: the position is what the drag left, the control keeps it
|
||||
CHECK(drag.start() == 0.f);
|
||||
CHECK(drag.position() == doctest::Approx(0.2f));
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: the control stops at its ends, and moving back takes it back at once") {
|
||||
auto const size{100.0};
|
||||
universal_drag::drag drag{direction::right, 0.9f};
|
||||
drag.move(0.0, 0.0, size);
|
||||
CHECK(drag.move(50.0, 0.0, size));
|
||||
CHECK(drag.position() == 1.f);
|
||||
// further on it stays at the end
|
||||
CHECK_FALSE(drag.move(200.0, 0.0, size));
|
||||
CHECK(drag.position() == 1.f);
|
||||
// back: from the end, not from where the cursor went past it
|
||||
CHECK(drag.move(190.0, 0.0, size));
|
||||
CHECK(drag.position() == doctest::Approx(0.9f));
|
||||
// and the other end
|
||||
CHECK(drag.move(0.0, 0.0, size));
|
||||
CHECK(drag.position() == 0.f);
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: a press let go with the cursor barely moved is a click") {
|
||||
auto const size{600.0};
|
||||
universal_drag::drag drag{direction::down, 0.4f};
|
||||
drag.move(10.0, 10.0, size);
|
||||
// a jitter of the hand, under the travel of a click
|
||||
drag.move(11.0, 13.0, size);
|
||||
drag.move(10.0, 11.0, size);
|
||||
CHECK(drag.is_click());
|
||||
// the click moves it to the end farther from it, as the key does
|
||||
CHECK(universal_drag::toggled(drag.position()) == 1.f);
|
||||
// a drag there and back is a drag, though it ends where it started
|
||||
universal_drag::drag back{direction::down, 0.4f};
|
||||
back.move(0.0, 0.0, size);
|
||||
back.move(0.0, 60.0, size);
|
||||
back.move(0.0, 0.0, size);
|
||||
CHECK_FALSE(back.is_click());
|
||||
CHECK(back.position() == doctest::Approx(0.4f));
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: a drag starts from the position the control is set to") {
|
||||
universal_drag::drag drag{direction::up, 0.6f};
|
||||
CHECK(drag.start() == doctest::Approx(0.6f));
|
||||
CHECK(drag.position() == doctest::Approx(0.6f));
|
||||
// a position out of the range is brought into it
|
||||
CHECK(universal_drag::drag{direction::up, 3.f}.position() == 1.f);
|
||||
CHECK(universal_drag::drag{direction::up, -1.f}.position() == 0.f);
|
||||
}
|
||||
|
||||
TEST_CASE("universal drag: a control which can't be dragged doesn't move") {
|
||||
universal_drag::drag drag{direction::none, 0.f};
|
||||
drag.move(0.0, 0.0, 600.0);
|
||||
CHECK_FALSE(drag.move(300.0, -300.0, 600.0));
|
||||
CHECK(drag.position() == 0.f);
|
||||
CHECK(drag.is_click());
|
||||
}
|
||||
@@ -121,8 +121,9 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
|
||||
Parser.getTokens();
|
||||
if( Parser.peek() != "{" ) {
|
||||
// old fixed size config
|
||||
// it has no scaletable:, drop the one of an earlier definition of the same control
|
||||
// it has no scaletable: nor drag:, drop the ones of an earlier definition of the same control
|
||||
m_scaletable.clear();
|
||||
m_drag = universal_drag::direction::none;
|
||||
Parser >> submodelname;
|
||||
gaugetypename = Parser.getToken<std::string>( true );
|
||||
Parser.getTokens( 3, false );
|
||||
@@ -168,6 +169,7 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
|
||||
}
|
||||
gauge_scale::sort( scratchpad.scaletable );
|
||||
m_scaletable = std::move( scratchpad.scaletable );
|
||||
m_drag = scratchpad.drag;
|
||||
// post-deserialization cleanup
|
||||
// set provided custom soundproofing to assigned sounds (for sounds without their own custom soundproofing)
|
||||
if( scratchpad.soundproofing ) {
|
||||
@@ -308,6 +310,16 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) {
|
||||
>> soundproofing[ 5 ];
|
||||
Scratchpad.soundproofing = soundproofing;
|
||||
}
|
||||
else if( key == "drag:" ) {
|
||||
// the mouse drags the control to any position between its ends, moving in the given direction to open it
|
||||
// (MaSzyna-EU07/maszyna#62)
|
||||
auto const value { Input.getToken<std::string>( true, "\n\r\t ,;" ) };
|
||||
auto const drag { universal_drag::parse( value ) };
|
||||
if( false == drag.has_value() ) {
|
||||
ErrorLog( "Bad model: unknown drag direction \"" + value + "\" of a cab control, it takes up, down, left, right, yes or no", logtype::model );
|
||||
}
|
||||
Scratchpad.drag = drag.value_or( universal_drag::direction::none );
|
||||
}
|
||||
else if( key == "scaletable:" ) {
|
||||
// non-uniform scale in format [ value1 scaled1, value2 scaled2, ... ]
|
||||
return Load_scaletable( Input, Scratchpad );
|
||||
|
||||
@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "utilities/Classes.h"
|
||||
#include "audio/sound.h"
|
||||
#include "vehicle/gaugescale.h"
|
||||
#include "vehicle/universaldrag.h"
|
||||
|
||||
enum class TGaugeAnimation {
|
||||
// typ ruchu
|
||||
@@ -86,6 +87,10 @@ public:
|
||||
// true if the gauge was given a non-uniform scale (scaletable:)
|
||||
bool has_scaletable() const {
|
||||
return false == m_scaletable.empty(); }
|
||||
// the direction the mouse drags the control in to any position between its ends (drag:, MaSzyna-EU07/maszyna#62),
|
||||
// none for a control set only to its ends. a push control springs back, so it isn't dragged
|
||||
universal_drag::direction drag() const {
|
||||
return universal_drag::effective( m_drag, is_push() ); }
|
||||
// members
|
||||
TSubModel *SubModel { nullptr }; // McZapkie-310302: zeby mozna bylo sprawdzac czy zainicjowany poprawnie
|
||||
TSubModel *SubModelOn { nullptr }; // optional submodel visible when the state input is set
|
||||
@@ -96,6 +101,7 @@ private:
|
||||
std::optional< std::array<float, 6> > soundproofing;
|
||||
bool duplicate { false }; // the definition is a duplicate submodel of the control defined before
|
||||
std::vector<gauge_scale::point> scaletable; // as read, in the units of the config
|
||||
universal_drag::direction drag { universal_drag::direction::none };
|
||||
};
|
||||
// methods
|
||||
// imports member data pair from the config file
|
||||
@@ -127,6 +133,7 @@ private:
|
||||
float m_endvalue { -1.f }; // end value of accepted value range
|
||||
bool m_interpolatescale { false };
|
||||
std::vector<gauge_scale::point> m_scaletable; // optional non-uniform scale, sorted by value; replaces scale and offset
|
||||
universal_drag::direction m_drag { universal_drag::direction::none }; // the mouse drags the control smoothly, drag:
|
||||
|
||||
char m_datatype { '\0' }; // typ zmiennej parametru: f-float, d-double, i-int, b-bool; none until one is assigned
|
||||
union {
|
||||
|
||||
@@ -537,6 +537,36 @@ TTrain::commandhandler_map const TTrain::m_commandhandlers = {
|
||||
{user_command::generictoggle27, &TTrain::OnCommand_generictoggle},
|
||||
{user_command::generictoggle28, &TTrain::OnCommand_generictoggle},
|
||||
{user_command::generictoggle29, &TTrain::OnCommand_generictoggle},
|
||||
{user_command::genericset0, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset1, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset2, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset3, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset4, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset5, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset6, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset7, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset8, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset9, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset10, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset11, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset12, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset13, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset14, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset15, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset16, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset17, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset18, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset19, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset20, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset21, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset22, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset23, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset24, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset25, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset26, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset27, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset28, &TTrain::OnCommand_genericset},
|
||||
{user_command::genericset29, &TTrain::OnCommand_genericset},
|
||||
{user_command::vehiclemoveforwards, &TTrain::OnCommand_vehiclemoveforwards},
|
||||
{user_command::vehiclemovebackwards, &TTrain::OnCommand_vehiclemovebackwards},
|
||||
{user_command::vehicleboost, &TTrain::OnCommand_vehicleboost},
|
||||
@@ -6994,19 +7024,9 @@ void TTrain::OnCommand_generictoggle(TTrain *Train, command_data const &Command)
|
||||
}
|
||||
else
|
||||
{
|
||||
// two-state switch
|
||||
if (item.GetDesiredValue() < 0.5)
|
||||
{
|
||||
// turn on
|
||||
// visual feedback
|
||||
item.UpdateValue(1.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
// turn off
|
||||
// visual feedback
|
||||
item.UpdateValue(0.0);
|
||||
}
|
||||
// two-state switch, or one dragged in between its ends with the mouse: to the end farther from it
|
||||
// visual feedback
|
||||
item.UpdateValue(universal_drag::toggled(item.GetDesiredValue()));
|
||||
}
|
||||
}
|
||||
else if (Command.action == GLFW_RELEASE && item.type() == TGaugeType::push)
|
||||
@@ -7018,6 +7038,46 @@ void TTrain::OnCommand_generictoggle(TTrain *Train, command_data const &Command)
|
||||
}
|
||||
}
|
||||
|
||||
// sets a universal control to the position given in param1, from 0 to 1 (MaSzyna-EU07/maszyna#62): the mouse drags
|
||||
// the ones with drag: in their definition, and it can be bound to an axis of a gamepad. it stays where it is set
|
||||
void TTrain::OnCommand_genericset(TTrain *Train, command_data const &Command)
|
||||
{
|
||||
|
||||
if (Command.action == GLFW_RELEASE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
auto const itemindex = static_cast<int>(Command.command) - static_cast<int>(user_command::genericset0);
|
||||
auto &item = Train->ggUniversals[itemindex];
|
||||
if (item.is_push())
|
||||
{
|
||||
// an impulse switch springs back, it has no position to be left in
|
||||
return;
|
||||
}
|
||||
// visual feedback
|
||||
item.UpdateValue(universal_drag::clamp(Command.param1));
|
||||
}
|
||||
|
||||
universal_drag::direction TTrain::universal_drag_direction(int const Index) const
|
||||
{
|
||||
|
||||
if (Index < 0 || Index >= static_cast<int>(ggUniversals.size()) || ggUniversals[Index].SubModel == nullptr)
|
||||
{
|
||||
return universal_drag::direction::none;
|
||||
}
|
||||
return ggUniversals[Index].drag();
|
||||
}
|
||||
|
||||
float TTrain::universal_position(int const Index) const
|
||||
{
|
||||
|
||||
if (Index < 0 || Index >= static_cast<int>(ggUniversals.size()))
|
||||
{
|
||||
return 0.f;
|
||||
}
|
||||
return ggUniversals[Index].GetDesiredValue();
|
||||
}
|
||||
|
||||
void TTrain::OnCommand_springbraketoggle(TTrain *Train, command_data const &Command)
|
||||
{
|
||||
|
||||
|
||||
@@ -172,6 +172,11 @@ class TTrain {
|
||||
inline
|
||||
std::string GetLabel( TSubModel const *Control ) const {
|
||||
return m_controlmapper.find( Control ); }
|
||||
// the direction the mouse drags universal control Index in to any position between its ends, none if the control
|
||||
// is only set to its ends (MaSzyna-EU07/maszyna#62)
|
||||
universal_drag::direction universal_drag_direction( int const Index ) const;
|
||||
// the position universal control Index is set to, from 0 to 1
|
||||
float universal_position( int const Index ) const;
|
||||
void UpdateCab();
|
||||
bool Update( double const Deltatime );
|
||||
void UpdateDirectionRelays();
|
||||
@@ -518,6 +523,7 @@ class TTrain {
|
||||
static void OnCommand_cabchangeforward( TTrain *Train, command_data const &Command );
|
||||
static void OnCommand_cabchangebackward( TTrain *Train, command_data const &Command );
|
||||
static void OnCommand_generictoggle( TTrain *Train, command_data const &Command );
|
||||
static void OnCommand_genericset( TTrain *Train, command_data const &Command );
|
||||
static void OnCommand_vehiclemoveforwards( TTrain *Train, command_data const &Command );
|
||||
static void OnCommand_vehiclemovebackwards( TTrain *Train, command_data const &Command );
|
||||
static void OnCommand_vehicleboost( TTrain *Train, command_data const &Command );
|
||||
|
||||
138
vehicle/universaldrag.h
Normal file
138
vehicle/universaldrag.h
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
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 <algorithm>
|
||||
#include <cmath>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
|
||||
// smooth regulation of the universal cab controls (MaSzyna-EU07/maszyna#62): a universal whose definition in the
|
||||
// .mmd has "drag: <direction>" can be dragged with the mouse to any position between its ends (0 and 1), and stays
|
||||
// where it is let go, like a window opened a crack or a sun blind pulled half way down. a click without dragging and
|
||||
// the keys still move it to its ends, as they do any universal. universals without the key work as they always did
|
||||
namespace universal_drag {
|
||||
|
||||
// the way the mouse moves to take the control towards its end 1 (to open it); none: the control can't be dragged
|
||||
enum class direction { none, up, down, left, right };
|
||||
|
||||
// the direction the value of the drag: key gives, nothing for a value it doesn't know
|
||||
inline std::optional<direction> parse(std::string_view const Value) {
|
||||
if (Value == "yes" || Value == "true" || Value == "1" || Value == "up") {
|
||||
return direction::up;
|
||||
}
|
||||
if (Value == "down") {
|
||||
return direction::down;
|
||||
}
|
||||
if (Value == "left") {
|
||||
return direction::left;
|
||||
}
|
||||
if (Value == "right") {
|
||||
return direction::right;
|
||||
}
|
||||
if (Value == "no" || Value == "false" || Value == "0" || Value == "none") {
|
||||
return direction::none;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// a push (impulse) universal springs back when it is let go, so it can't be left anywhere in between
|
||||
inline direction effective(direction const Drag, bool const Push) {
|
||||
return Push ? direction::none : Drag;
|
||||
}
|
||||
|
||||
// the position of the control, kept between its ends
|
||||
inline float clamp(double const Position) {
|
||||
return static_cast<float>(std::clamp(Position, 0.0, 1.0));
|
||||
}
|
||||
|
||||
// how far the cursor moving by Dx, Dy pixels (y grows downwards, as the cursor's does) takes the control along the
|
||||
// Drag direction, as a fraction of its full travel; Size is the move in pixels which takes it from one end to the other
|
||||
inline double along(direction const Drag, double const Dx, double const Dy, double const Size) {
|
||||
if (Size <= 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
switch (Drag) {
|
||||
case direction::up:
|
||||
return -Dy / Size;
|
||||
case direction::down:
|
||||
return Dy / Size;
|
||||
case direction::left:
|
||||
return -Dx / Size;
|
||||
case direction::right:
|
||||
return Dx / Size;
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
// the position the keys and a click move the control to: the end farther from the position it is set to, as for
|
||||
// the two-state universals: a control open less than half way opens fully, one open half way or more closes
|
||||
inline float toggled(float const Desired) {
|
||||
return Desired < 0.5f ? 1.f : 0.f;
|
||||
}
|
||||
|
||||
// the travel of the cursor along the direction of the drag, as a fraction of the full travel of the control, under
|
||||
// which letting the button go counts as a click rather than a drag
|
||||
inline constexpr double click_travel{0.02};
|
||||
|
||||
// one drag of a control, from the press of the mouse button to its release
|
||||
class drag {
|
||||
|
||||
public:
|
||||
drag(direction const Drag, float const Position) : m_direction{Drag}, m_position{clamp(Position)}, m_start{m_position} {}
|
||||
|
||||
// the cursor moved to X, Y; Size is the move in pixels which takes the control from one end to the other.
|
||||
// the first position of the cursor is only where the drag starts from: when the cursor gets hidden for the drag
|
||||
// it can be put elsewhere. returns whether the position of the control changed
|
||||
bool move(double const X, double const Y, double const Size) {
|
||||
if (false == m_started) {
|
||||
m_started = true;
|
||||
m_x = X;
|
||||
m_y = Y;
|
||||
return false;
|
||||
}
|
||||
auto const step{along(m_direction, X - m_x, Y - m_y, Size)};
|
||||
m_x = X;
|
||||
m_y = Y;
|
||||
m_travel += std::abs(step);
|
||||
// the position follows each move from where it is, so after the control reached its end, moving the cursor
|
||||
// back takes it back at once rather than after the overshoot is undone
|
||||
auto const position{clamp(m_position + step)};
|
||||
if (position == m_position) {
|
||||
return false;
|
||||
}
|
||||
m_position = position;
|
||||
return true;
|
||||
}
|
||||
// the position the control is dragged to
|
||||
float position() const {
|
||||
return m_position;
|
||||
}
|
||||
// the position the control was in when the drag started
|
||||
float start() const {
|
||||
return m_start;
|
||||
}
|
||||
// whether letting the button go now is a click rather than a drag: the cursor barely moved along the direction
|
||||
bool is_click() const {
|
||||
return m_travel < click_travel;
|
||||
}
|
||||
|
||||
private:
|
||||
direction m_direction;
|
||||
float m_position;
|
||||
float m_start;
|
||||
bool m_started{false};
|
||||
double m_x{0.0};
|
||||
double m_y{0.0};
|
||||
double m_travel{0.0}; // the moves of the cursor along the direction, either way, as fractions of the full travel
|
||||
};
|
||||
|
||||
} // namespace universal_drag
|
||||
Reference in New Issue
Block a user