From a6dc3c47a9806a47421b1c04bf27b7cdd89d92f3 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Fri, 11 Jul 2014 07:18:21 -0400 Subject: [PATCH] ControllerEmu: Change the GetState interface to remove range/base Do the scaling in the code that interprets the results. This also removes the templatization of things and changes the interface to always take a double. This does add a bit more code to the users of GetState, especially when having to deal with focus management, but this will be cleaned up very soon, as focus and focus-related options will be centralized inside the input platforms themselves, rather than spread out across all the input plugins. --- Source/Core/Core/HW/GCPadEmu.cpp | 15 +++- .../Core/HW/WiimoteEmu/Attachment/Classic.cpp | 54 ++++++++---- .../Core/HW/WiimoteEmu/Attachment/Drums.cpp | 16 +++- .../Core/HW/WiimoteEmu/Attachment/Guitar.cpp | 22 +++-- .../Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp | 6 +- .../HW/WiimoteEmu/Attachment/Turntable.cpp | 88 ++++++++++++++----- Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp | 25 ++++-- .../Core/DolphinWX/InputConfigDiagBitmaps.cpp | 23 ++--- Source/Core/InputCommon/ControllerEmu.h | 39 ++++---- 9 files changed, 193 insertions(+), 95 deletions(-) diff --git a/Source/Core/Core/HW/GCPadEmu.cpp b/Source/Core/Core/HW/GCPadEmu.cpp index c79e6c057a..b95969c0ea 100644 --- a/Source/Core/Core/HW/GCPadEmu.cpp +++ b/Source/Core/Core/HW/GCPadEmu.cpp @@ -92,6 +92,8 @@ void GCPad::GetInput(GCPadStatus* const pad) // if window has focus or background input enabled if (Host_RendererHasFocus() || m_options[0].settings[0]->value) { + double x, y, triggers[2]; + // buttons m_buttons->GetState(&pad->button, button_bitmasks); @@ -103,11 +105,18 @@ void GCPad::GetInput(GCPadStatus* const pad) m_dpad->GetState(&pad->button, dpad_bitmasks); // sticks - m_main_stick->GetState(&pad->stickX, &pad->stickY, 0x80, 127); - m_c_stick->GetState(&pad->substickX, &pad->substickY, 0x80, 127); + m_main_stick->GetState(&x, &y); + pad->stickX = 0x7F + (x * 0x80); + pad->stickY = 0x7F + (y * 0x80); + + m_c_stick->GetState(&x, &y); + pad->substickX = 0x7F + (x * 0x80); + pad->substickY = 0x7F + (y * 0x80); // triggers - m_triggers->GetState(&pad->button, trigger_bitmasks, &pad->triggerLeft, 0xFF); + m_triggers->GetState(&pad->button, trigger_bitmasks, triggers); + pad->triggerLeft = triggers[0] * 0xFF; + pad->triggerRight = triggers[1] * 0xFF; } else { diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp index 915d051c94..1a2d43c098 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Classic.cpp @@ -89,32 +89,56 @@ void Classic::GetState(u8* const data, const bool focus) // left stick { - u8 x, y; - m_left_stick->GetState(&x, &y, 0x20, focus ? 0x1F /*0x15*/ : 0); + double x, y; + if (focus) + { + m_left_stick->GetState(&x, &y); + } + else + { + x = 0.0; + y = 0.0; + } - ccdata->lx = x; - ccdata->ly = y; + ccdata->lx = (x * 0x1F) + 0x20; + ccdata->ly = (y * 0x1F) + 0x20; } // right stick { - u8 x, y; - m_right_stick->GetState(&x, &y, 0x10, focus ? 0x0F /*0x0C*/ : 0); + double x, y; + u8 x_, y_; + if (focus) + { + m_right_stick->GetState(&x, &y); + } + else + { + x = 0.0; + y = 0.0; + } - ccdata->rx1 = x; - ccdata->rx2 = x >> 1; - ccdata->rx3 = x >> 3; - ccdata->ry = y; + x_ = (x * 0x1F) + 0x20; + y_ = (y * 0x1F) + 0x20; + + ccdata->rx1 = x_; + ccdata->rx2 = x_ >> 1; + ccdata->rx3 = x_ >> 3; + ccdata->ry = y_; } //triggers { - u8 trigs[2]; - m_triggers->GetState(&ccdata->bt, classic_trigger_bitmasks, trigs, focus ? 0x1F : 0); + double trigs[2] = { 0, 0 }; + u8 lt, rt; + m_triggers->GetState(&ccdata->bt, classic_trigger_bitmasks, trigs); - ccdata->lt1 = trigs[0]; - ccdata->lt2 = trigs[0] >> 3; - ccdata->rt = trigs[1]; + lt = trigs[0] * 0x1F; + rt = trigs[1] * 0x1F; + + ccdata->lt1 = lt; + ccdata->lt2 = lt >> 3; + ccdata->rt = rt; } if (focus) diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp index 35a1fe8e87..749287ac51 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Drums.cpp @@ -60,11 +60,19 @@ void Drums::GetState(u8* const data, const bool focus) // stick { - u8 x, y; - m_stick->GetState(&x, &y, 0x20, focus ? 0x1F /*0x15*/ : 0); + double x, y; + if (focus) + { + m_stick->GetState(&x, &y); + } + else + { + x = 0.0; + y = 0.0; + } - ddata->sx = x; - ddata->sy = y; + ddata->sx = (x * 0x1F) + 0x20; + ddata->sx = (y * 0x1F) + 0x20; } // TODO: softness maybe diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp index 51c3224e31..8017bd80db 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp @@ -73,20 +73,28 @@ void Guitar::GetState(u8* const data, const bool focus) // stick { - u8 x, y; - m_stick->GetState(&x, &y, 0x20, focus ? 0x1F /*0x15*/ : 0); + double x, y; + if (focus) + { + m_stick->GetState(&x, &y); + } + else + { + x = 0; + y = 0; + } - gdata->sx = x; - gdata->sy = y; + gdata->sx = (x * 0x1F) + 0x20; + gdata->sy = (y * 0x1F) + 0x20; } // TODO: touch bar, probably not gdata->tb = 0x0F; // not touched // whammy bar - u8 whammy; - m_whammy->GetState(&whammy, 0x1F); - gdata->whammy = whammy; + double whammy; + m_whammy->GetState(&whammy); + gdata->whammy = whammy * 0x1F; if (focus) { diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp index e1b1c82dc6..a0b25b4b5f 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Nunchuk.cpp @@ -66,8 +66,8 @@ void Nunchuk::GetState(u8* const data, const bool focus) ncdata->bt = 0; // stick - ControlState state[2]; - m_stick->GetState(&state[0], &state[1], 0, 1); + double state[2]; + m_stick->GetState(&state[0], &state[1]); nu_cal &cal = *(nu_cal*)®.calibration; nu_js cal_js[2]; @@ -75,7 +75,7 @@ void Nunchuk::GetState(u8* const data, const bool focus) cal_js[1] = cal.jy; for (int i = 0; i < 2; i++) { - ControlState &s = state[i]; + double &s = state[i]; nu_js c = cal_js[i]; if (s < 0) s = s * abs(c.min - c.center) + c.center; diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp index 650d5b6720..537dc33361 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp @@ -62,49 +62,93 @@ void Turntable::GetState(u8* const data, const bool focus) // stick { - u8 x, y; - m_stick->GetState(&x, &y, 0x20, focus ? 0x1F /*0x15*/ : 0); + double x, y; + if (focus) + { + m_stick->GetState(&x, &y); + } + else + { + x = 0.0; + y = 0.0; + } - ttdata->sx = x; - ttdata->sy = y; + ttdata->sx = (x * 0x1F) + 0x20; + ttdata->sy = (y * 0x1F) + 0x20; } // left table { - s8 tt = 0; - m_left_table->GetState(&tt, focus ? 0x1F : 0); + double tt; + s8 tt_; + if (focus) + { + m_left_table->GetState(&tt); + } + else + { + tt = 0.0; + } - ttdata->ltable1 = tt; - ttdata->ltable2 = tt >> 5; + tt_ = tt * 0x1F; + + ttdata->ltable1 = tt_; + ttdata->ltable2 = tt_ >> 5; } // right table { - s8 tt = 0; - m_right_table->GetState(&tt, focus ? 0x1F : 0); + double tt; + s8 tt_; + if (focus) + { + m_right_table->GetState(&tt); + } + else + { + tt = 0.0; + } - ttdata->rtable1 = tt; - ttdata->rtable2 = tt >> 1; - ttdata->rtable3 = tt >> 3; - ttdata->rtable4 = tt >> 5; + tt_ = tt * 0x1F; + + ttdata->rtable1 = tt_; + ttdata->rtable2 = tt_ >> 1; + ttdata->rtable3 = tt_ >> 3; + ttdata->rtable4 = tt_ >> 5; } // effect dial { - u8 dial = 0; - m_effect_dial->GetState(&dial, focus ? 0xF : 0); + double dial; + u8 dial_; + if (focus) + { + m_effect_dial->GetState(&dial); + } + else + { + dial = 0; + } - ttdata->dial1 = dial; - ttdata->dial2 = dial >> 3; + dial_ = dial * 0x0F; + + ttdata->dial1 = dial_; + ttdata->dial2 = dial_ >> 3; } // crossfade slider { - s8 cfs = 0; - m_crossfade->GetState(&cfs, focus ? 7 : 0); - cfs += 8; + double cfs; + if (focus) + { + m_crossfade->GetState(&cfs); + } + else + { + cfs = 0; + } - ttdata->slider = cfs; + ttdata->slider = (cfs * 0x07) + 0x08; } if (focus) diff --git a/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp b/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp index e502ba077b..bc34afac90 100644 --- a/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.cpp @@ -120,9 +120,20 @@ void EmulateTilt(AccelData* const accel , ControllerEmu::Tilt* const tilt_group , const bool focus, const bool sideways, const bool upright) { - float roll, pitch; + double roll, pitch; // 180 degrees - tilt_group->GetState(&roll, &pitch, 0, focus ? PI : 0); + if (focus) + { + tilt_group->GetState(&roll, &pitch); + } + else + { + roll = 0.0; + pitch = 0.0; + } + + roll *= PI; + pitch *= PI; unsigned int ud = 0, lr = 0, fb = 0; @@ -144,7 +155,7 @@ void EmulateTilt(AccelData* const accel if (!sideways && upright) sgn[ud] *= -1; - (&accel->x)[ud] = (sin((PI / 2) - std::max(fabsf(roll), fabsf(pitch))))*sgn[ud]; + (&accel->x)[ud] = (sin((PI / 2) - std::max(fabs(roll), fabs(pitch))))*sgn[ud]; (&accel->x)[lr] = -sin(roll)*sgn[lr]; (&accel->x)[fb] = sin(pitch)*sgn[fb]; } @@ -155,8 +166,8 @@ void EmulateSwing(AccelData* const accel , ControllerEmu::Force* const swing_group , const bool sideways, const bool upright) { - float swing[3]; - swing_group->GetState(swing, 0, SWING_INTENSITY); + double swing[3]; + swing_group->GetState(swing); s8 g_dir[3] = {-1, -1, -1}; u8 axis_map[3]; @@ -174,7 +185,7 @@ void EmulateSwing(AccelData* const accel g_dir[axis_map[0]] *= -1; for (unsigned int i=0; i<3; ++i) - (&accel->x)[axis_map[i]] += swing[i] * g_dir[i]; + (&accel->x)[axis_map[i]] += swing[i] * g_dir[i] * SWING_INTENSITY; } const u16 button_bitmasks[] = @@ -436,7 +447,7 @@ void Wiimote::GetIRData(u8* const data, bool use_accel) if (has_focus) { - float xx = 10000, yy = 0, zz = 0; + double xx = 10000, yy = 0, zz = 0; double nsin,ncos; if (use_accel) diff --git a/Source/Core/DolphinWX/InputConfigDiagBitmaps.cpp b/Source/Core/DolphinWX/InputConfigDiagBitmaps.cpp index 924aa88f0a..72faac4150 100644 --- a/Source/Core/DolphinWX/InputConfigDiagBitmaps.cpp +++ b/Source/Core/DolphinWX/InputConfigDiagBitmaps.cpp @@ -60,15 +60,15 @@ static void DrawControlGroupBox(wxDC &dc, ControlGroupBox *g) { // this is starting to be a mess combining all these in one case - float x = 0, y = 0, z = 0; + double x = 0, y = 0, z = 0; switch (g->control_group->type) { case GROUP_TYPE_STICK : - ((ControllerEmu::AnalogStick*)g->control_group)->GetState(&x, &y, 0, 1); + ((ControllerEmu::AnalogStick*)g->control_group)->GetState(&x, &y); break; case GROUP_TYPE_TILT : - ((ControllerEmu::Tilt*)g->control_group)->GetState(&x, &y, 0, 1); + ((ControllerEmu::Tilt*)g->control_group)->GetState(&x, &y); break; case GROUP_TYPE_CURSOR : ((ControllerEmu::Cursor*)g->control_group)->GetState(&x, &y, &z); @@ -177,12 +177,12 @@ static void DrawControlGroupBox(wxDC &dc, ControlGroupBox *g) break; case GROUP_TYPE_FORCE : { - float raw_dot[3]; - float adj_dot[3]; + double raw_dot[3]; + double adj_dot[3]; const float deadzone = g->control_group->settings[0]->value; // adjusted - ((ControllerEmu::Force*)g->control_group)->GetState(adj_dot, 0, 1); + ((ControllerEmu::Force*)g->control_group)->GetState(adj_dot); // raw for (unsigned int i=0; i<3; ++i) @@ -287,8 +287,8 @@ static void DrawControlGroupBox(wxDC &dc, ControlGroupBox *g) dc.SetPen(*wxGREY_PEN); ControlState deadzone = g->control_group->settings[0]->value; - unsigned int* const trigs = new unsigned int[trigger_count]; - ((ControllerEmu::Triggers*)g->control_group)->GetState(trigs, 64); + double* const trigs = new double[trigger_count]; + ((ControllerEmu::Triggers*)g->control_group)->GetState(trigs); for (unsigned int n = 0; n < trigger_count; ++n) { @@ -305,7 +305,7 @@ static void DrawControlGroupBox(wxDC &dc, ControlGroupBox *g) // deadzone affected dc.SetBrush(*wxRED_BRUSH); - dc.DrawRectangle(0, n*12, trigs[n], 14); + dc.DrawRectangle(0, n*12, trigs[n]*64, 14); // text dc.DrawText(StrToWxStr(g->control_group->controls[n]->name), 3, n*12 + 1); @@ -363,12 +363,13 @@ static void DrawControlGroupBox(wxDC &dc, ControlGroupBox *g) dc.SetBrush(*wxGREY_BRUSH); dc.DrawRectangle(31 + state * 30, 0, 2, 14); - ((ControllerEmu::Slider*)g->control_group)->GetState(&state, 1); + double adj_state; + ((ControllerEmu::Slider*)g->control_group)->GetState(&adj_state); if (state) { dc.SetPen(*wxRED_PEN); dc.SetBrush(*wxRED_BRUSH); - dc.DrawRectangle(31 + state * 30, 0, 2, 14); + dc.DrawRectangle(31 + adj_state * 30, 0, 2, 14); } // deadzone box diff --git a/Source/Core/InputCommon/ControllerEmu.h b/Source/Core/InputCommon/ControllerEmu.h index 4f883c9cb8..9f8bc50985 100644 --- a/Source/Core/InputCommon/ControllerEmu.h +++ b/Source/Core/InputCommon/ControllerEmu.h @@ -121,8 +121,7 @@ public: public: AnalogStick(const char* const _name); - template - void GetState(C* const x, C* const y, const unsigned int base, const unsigned int range) + void GetState(double* const x, double* const y) { ControlState yy = controls[0]->control_ref->State() - controls[1]->control_ref->State(); ControlState xx = controls[3]->control_ref->State() - controls[2]->control_ref->State(); @@ -152,8 +151,8 @@ public: yy = std::max(-1.0f, std::min(1.0f, ang_sin * dist)); xx = std::max(-1.0f, std::min(1.0f, ang_cos * dist)); - *y = C(yy * range + base); - *x = C(xx * range + base); + *y = yy; + *x = xx; } }; @@ -181,20 +180,19 @@ public: public: MixedTriggers(const std::string& _name); - template - void GetState(C* const digital, const C* bitmasks, S* analog, const unsigned int range) + void GetState(u16 *const digital, const u16* bitmasks, double* analog) { const unsigned int trig_count = ((unsigned int) (controls.size() / 2)); for (unsigned int i=0; icontrol_ref->State() > settings[0]->value) //threshold { - *analog = range; + *analog = 1.0; *digital |= *bitmasks; } else { - *analog = S(controls[i+trig_count]->control_ref->State() * range); + *analog = controls[i+trig_count]->control_ref->State(); } } } @@ -205,13 +203,12 @@ public: public: Triggers(const std::string& _name); - template - void GetState(S* analog, const unsigned int range) + void GetState(double* analog) { const unsigned int trig_count = ((unsigned int) (controls.size())); const ControlState deadzone = settings[0]->value; for (unsigned int i=0; icontrol_ref->State() - deadzone, 0.0f) / (1 - deadzone) * range); + *analog = std::max(controls[i]->control_ref->State() - deadzone, 0.0f) / (1 - deadzone); } }; @@ -220,14 +217,13 @@ public: public: Slider(const std::string& _name); - template - void GetState(S* const slider, const unsigned int range, const unsigned int base = 0) + void GetState(double* const slider) { const float deadzone = settings[0]->value; const float state = controls[1]->control_ref->State() - controls[0]->control_ref->State(); if (fabsf(state) > deadzone) - *slider = (S)((state - (deadzone * sign(state))) / (1 - deadzone) * range + base); + *slider = (state - (deadzone * sign(state))) / (1 - deadzone); else *slider = 0; } @@ -238,8 +234,7 @@ public: public: Force(const std::string& _name); - template - void GetState(C* axis, const u8 base, const R range) + void GetState(double* axis) { const float deadzone = settings[0]->value; for (unsigned int i=0; i<6; i+=2) @@ -250,7 +245,7 @@ public: tmpf = ((state - (deadzone * sign(state))) / (1 - deadzone)); float &ax = m_swing[i >> 1]; - *axis++ = (C)((tmpf - ax) * range + base); + *axis++ = (tmpf - ax); ax = tmpf; } } @@ -264,8 +259,7 @@ public: public: Tilt(const std::string& _name); - template - void GetState(C* const x, C* const y, const unsigned int base, const R range, const bool step = true) + void GetState(double* const x, double* const y, const bool step = true) { // this is all a mess @@ -324,8 +318,8 @@ public: m_tilt[1] = std::max(m_tilt[1] - 0.1f, yy); } - *y = C(m_tilt[1] * angle * range + base); - *x = C(m_tilt[0] * angle * range + base); + *y = m_tilt[1] * angle; + *x = m_tilt[0] * angle; } private: @@ -337,8 +331,7 @@ public: public: Cursor(const std::string& _name); - template - void GetState(C* const x, C* const y, C* const z, const bool adjusted = false) + void GetState(double* const x, double* const y, double* const z, const bool adjusted = false) { const float zz = controls[4]->control_ref->State() - controls[5]->control_ref->State();