From 75d8ec1e9f474ce6c2bfc0b8ebe574ca44f9f3d8 Mon Sep 17 00:00:00 2001
From: german77 <juangerman-13@hotmail.com>
Date: Sun, 25 Jul 2021 20:52:19 -0500
Subject: UI: Relocate tas menu and add brief description

---
 src/input_common/tas/tas_input.cpp | 88 ++++++++++++++++++++++++--------------
 src/input_common/tas/tas_input.h   | 48 +++++++++++----------
 2 files changed, 82 insertions(+), 54 deletions(-)

(limited to 'src/input_common/tas')

diff --git a/src/input_common/tas/tas_input.cpp b/src/input_common/tas/tas_input.cpp
index 877d35088..1598092b6 100644
--- a/src/input_common/tas/tas_input.cpp
+++ b/src/input_common/tas/tas_input.cpp
@@ -40,12 +40,15 @@ constexpr std::array<std::pair<std::string_view, TasButton>, 20> text_to_tas_but
 
 Tas::Tas() {
     if (!Settings::values.tas_enable) {
+        needs_reset = true;
         return;
     }
     LoadTasFiles();
 }
 
-Tas::~Tas() = default;
+Tas::~Tas() {
+    Stop();
+};
 
 void Tas::LoadTasFiles() {
     script_length = 0;
@@ -184,6 +187,9 @@ std::string Tas::ButtonsToString(u32 button) const {
 
 void Tas::UpdateThread() {
     if (!Settings::values.tas_enable) {
+        if (is_running) {
+            Stop();
+        }
         return;
     }
 
@@ -196,34 +202,35 @@ void Tas::UpdateThread() {
         LoadTasFiles();
         LOG_DEBUG(Input, "tas_reset done");
     }
-    if (is_running) {
-        if (current_command < script_length) {
-            LOG_DEBUG(Input, "Playing TAS {}/{}", current_command, script_length);
-            size_t frame = current_command++;
-            for (size_t i = 0; i < commands.size(); i++) {
-                if (frame < commands[i].size()) {
-                    TASCommand command = commands[i][frame];
-                    tas_data[i].buttons = command.buttons;
-                    auto [l_axis_x, l_axis_y] = command.l_axis;
-                    tas_data[i].axis[0] = l_axis_x;
-                    tas_data[i].axis[1] = l_axis_y;
-                    auto [r_axis_x, r_axis_y] = command.r_axis;
-                    tas_data[i].axis[2] = r_axis_x;
-                    tas_data[i].axis[3] = r_axis_y;
-                } else {
-                    tas_data[i] = {};
-                }
-            }
-        } else {
-            is_running = Settings::values.tas_loop.GetValue();
-            current_command = 0;
-            tas_data.fill({});
-            if (!is_running) {
-                SwapToStoredController();
+
+    if (!is_running) {
+        tas_data.fill({});
+        return;
+    }
+    if (current_command < script_length) {
+        LOG_DEBUG(Input, "Playing TAS {}/{}", current_command, script_length);
+        size_t frame = current_command++;
+        for (size_t i = 0; i < commands.size(); i++) {
+            if (frame < commands[i].size()) {
+                TASCommand command = commands[i][frame];
+                tas_data[i].buttons = command.buttons;
+                auto [l_axis_x, l_axis_y] = command.l_axis;
+                tas_data[i].axis[0] = l_axis_x;
+                tas_data[i].axis[1] = l_axis_y;
+                auto [r_axis_x, r_axis_y] = command.r_axis;
+                tas_data[i].axis[2] = r_axis_x;
+                tas_data[i].axis[3] = r_axis_y;
+            } else {
+                tas_data[i] = {};
             }
         }
     } else {
+        is_running = Settings::values.tas_loop.GetValue();
+        current_command = 0;
         tas_data.fill({});
+        if (!is_running) {
+            SwapToStoredController();
+        }
     }
     LOG_DEBUG(Input, "TAS inputs: {}", DebugInputs(tas_data));
 }
@@ -237,8 +244,8 @@ TasAnalog Tas::ReadCommandAxis(const std::string& line) const {
         seglist.push_back(segment);
     }
 
-    const float x = std::stof(seglist.at(0)) / 32767.f;
-    const float y = std::stof(seglist.at(1)) / 32767.f;
+    const float x = std::stof(seglist.at(0)) / 32767.0f;
+    const float y = std::stof(seglist.at(1)) / 32767.0f;
 
     return {x, y};
 }
@@ -293,14 +300,22 @@ std::string Tas::WriteCommandButtons(u32 data) const {
 }
 
 void Tas::StartStop() {
-    is_running = !is_running;
+    if (!Settings::values.tas_enable) {
+        return;
+    }
     if (is_running) {
-        SwapToTasController();
+        Stop();
     } else {
-        SwapToStoredController();
+        is_running = true;
+        SwapToTasController();
     }
 }
 
+void Tas::Stop() {
+    is_running = false;
+    SwapToStoredController();
+}
+
 void Tas::SwapToTasController() {
     if (!Settings::values.tas_swap_controllers) {
         return;
@@ -315,7 +330,8 @@ void Tas::SwapToTasController() {
             continue;
         }
 
-        auto tas_param = Common::ParamPackage{{"pad", static_cast<u8>(index)}};
+        Common::ParamPackage tas_param;
+        tas_param.Set("pad", static_cast<u8>(index));
         auto button_mapping = GetButtonMappingForDevice(tas_param);
         auto analog_mapping = GetAnalogMappingForDevice(tas_param);
         auto& buttons = player.buttons;
@@ -328,25 +344,33 @@ void Tas::SwapToTasController() {
             analogs[i] = analog_mapping[static_cast<Settings::NativeAnalog::Values>(i)].Serialize();
         }
     }
+    is_old_input_saved = true;
     Settings::values.is_device_reload_pending.store(true);
 }
 
 void Tas::SwapToStoredController() {
-    if (!Settings::values.tas_swap_controllers) {
+    if (!is_old_input_saved) {
         return;
     }
     auto& players = Settings::values.players.GetValue();
     for (std::size_t index = 0; index < players.size(); index++) {
         players[index] = player_mappings[index];
     }
+    is_old_input_saved = false;
     Settings::values.is_device_reload_pending.store(true);
 }
 
 void Tas::Reset() {
+    if (!Settings::values.tas_enable) {
+        return;
+    }
     needs_reset = true;
 }
 
 bool Tas::Record() {
+    if (!Settings::values.tas_enable) {
+        return true;
+    }
     is_recording = !is_recording;
     return is_recording;
 }
diff --git a/src/input_common/tas/tas_input.h b/src/input_common/tas/tas_input.h
index 52d000db4..3e2db8f00 100644
--- a/src/input_common/tas/tas_input.h
+++ b/src/input_common/tas/tas_input.h
@@ -13,8 +13,8 @@
 
 /*
 To play back TAS scripts on Yuzu, select the folder with scripts in the configuration menu below
-Emulation -> Configure TAS. The file itself has normal text format and has to be called
-script0-1.txt for controller 1, script0-2.txt for controller 2 and so forth (with max. 8 players).
+Tools -> Configure TAS. The file itself has normal text format and has to be called script0-1.txt
+for controller 1, script0-2.txt for controller 2 and so forth (with max. 8 players).
 
 A script file has the same format as TAS-nx uses, so final files will look like this:
 
@@ -56,26 +56,26 @@ enum class TasState {
 };
 
 enum class TasButton : u32 {
-    BUTTON_A = 0x000001,
-    BUTTON_B = 0x000002,
-    BUTTON_X = 0x000004,
-    BUTTON_Y = 0x000008,
-    STICK_L = 0x000010,
-    STICK_R = 0x000020,
-    TRIGGER_L = 0x000040,
-    TRIGGER_R = 0x000080,
-    TRIGGER_ZL = 0x000100,
-    TRIGGER_ZR = 0x000200,
-    BUTTON_PLUS = 0x000400,
-    BUTTON_MINUS = 0x000800,
-    BUTTON_LEFT = 0x001000,
-    BUTTON_UP = 0x002000,
-    BUTTON_RIGHT = 0x004000,
-    BUTTON_DOWN = 0x008000,
-    BUTTON_SL = 0x010000,
-    BUTTON_SR = 0x020000,
-    BUTTON_HOME = 0x040000,
-    BUTTON_CAPTURE = 0x080000,
+    BUTTON_A = 1U << 0,
+    BUTTON_B = 1U << 1,
+    BUTTON_X = 1U << 2,
+    BUTTON_Y = 1U << 3,
+    STICK_L = 1U << 4,
+    STICK_R = 1U << 5,
+    TRIGGER_L = 1U << 6,
+    TRIGGER_R = 1U << 7,
+    TRIGGER_ZL = 1U << 8,
+    TRIGGER_ZR = 1U << 9,
+    BUTTON_PLUS = 1U << 10,
+    BUTTON_MINUS = 1U << 11,
+    BUTTON_LEFT = 1U << 12,
+    BUTTON_UP = 1U << 13,
+    BUTTON_RIGHT = 1U << 14,
+    BUTTON_DOWN = 1U << 15,
+    BUTTON_SL = 1U << 16,
+    BUTTON_SR = 1U << 17,
+    BUTTON_HOME = 1U << 18,
+    BUTTON_CAPTURE = 1U << 19,
 };
 
 enum class TasAxes : u8 {
@@ -105,6 +105,9 @@ public:
     //  Sets the flag to start or stop the TAS command excecution and swaps controllers profiles
     void StartStop();
 
+    //  Stop the TAS and reverts any controller profile
+    void Stop();
+
     // Sets the flag to reload the file and start from the begining in the next update
     void Reset();
 
@@ -219,6 +222,7 @@ private:
 
     size_t script_length{0};
     std::array<TasData, PLAYER_NUMBER> tas_data;
+    bool is_old_input_saved{false};
     bool is_recording{false};
     bool is_running{false};
     bool needs_reset{false};
-- 
cgit v1.2.3