qzdoom/src/menu/joystickmenu.cpp

508 lines
13 KiB
C++
Raw Normal View History

2016-03-01 15:47:10 +00:00
/*
** joystickmenu.cpp
** The joystick configuration menus
**
**---------------------------------------------------------------------------
** Copyright 2010 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include <float.h>
#include "menu/menu.h"
#include "c_dispatch.h"
#include "w_wad.h"
#include "sc_man.h"
#include "v_font.h"
#include "g_level.h"
#include "d_player.h"
#include "v_video.h"
#include "gi.h"
#include "i_system.h"
#include "c_bind.h"
#include "v_palette.h"
#include "d_event.h"
#include "d_gui.h"
#include "i_music.h"
#include "m_joy.h"
#define NO_IMP
#include "optionmenuitems.h"
static TArray<IJoystickConfig *> Joysticks;
IJoystickConfig *SELECTED_JOYSTICK;
DEFINE_ACTION_FUNCTION(DMenu, GetCurrentJoystickConfig)
{
ACTION_RETURN_POINTER(SELECTED_JOYSTICK);
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, GetSensitivity)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
ACTION_RETURN_FLOAT(self->GetSensitivity());
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, SetSensitivity)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_FLOAT(sens);
self->SetSensitivity((float)sens);
return 0;
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, GetAxisScale)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
ACTION_RETURN_FLOAT(self->GetAxisScale(axis));
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, SetAxisScale)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
PARAM_FLOAT(sens);
self->SetAxisScale(axis, (float)sens);
return 0;
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, GetAxisDeadZone)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
ACTION_RETURN_FLOAT(self->GetAxisDeadZone(axis));
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, SetAxisDeadZone)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
PARAM_FLOAT(dz);
self->SetAxisDeadZone(axis, (float)dz);
return 0;
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, GetAxisMap)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
ACTION_RETURN_INT(self->GetAxisMap(axis));
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, SetAxisMap)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_INT(axis);
PARAM_INT(map);
self->SetAxisMap(axis, (EJoyAxis)map);
return 0;
}
DOptionMenuDescriptor *UpdateJoystickConfigMenu(IJoystickConfig *joy);
2016-03-01 15:47:10 +00:00
//=============================================================================
//
//
//
//=============================================================================
class DOptionMenuSliderJoySensitivity_ : public DOptionMenuSliderBase_
2016-03-01 15:47:10 +00:00
{
public:
DOptionMenuSliderJoySensitivity_(const char *label, double min, double max, double step, int showval)
: DOptionMenuSliderBase_(label, min, max, step, showval)
2016-03-01 15:47:10 +00:00
{
}
double GetSliderValue()
{
return SELECTED_JOYSTICK->GetSensitivity();
}
void SetSliderValue(double val)
{
SELECTED_JOYSTICK->SetSensitivity(float(val));
}
};
//=============================================================================
//
//
//
//=============================================================================
class DOptionMenuSliderJoyScale_ : public DOptionMenuSliderBase_
2016-03-01 15:47:10 +00:00
{
int mAxis;
int mNeg;
public:
DOptionMenuSliderJoyScale_(const char *label, int axis, double min, double max, double step, int showval)
: DOptionMenuSliderBase_(label, min, max, step, showval)
2016-03-01 15:47:10 +00:00
{
mAxis = axis;
mNeg = 1;
}
double GetSliderValue()
{
double d = SELECTED_JOYSTICK->GetAxisScale(mAxis);
mNeg = d < 0? -1:1;
return d;
}
void SetSliderValue(double val)
{
SELECTED_JOYSTICK->SetAxisScale(mAxis, float(val * mNeg));
}
};
//=============================================================================
//
//
//
//=============================================================================
class DOptionMenuSliderJoyDeadZone_ : public DOptionMenuSliderBase_
2016-03-01 15:47:10 +00:00
{
int mAxis;
int mNeg;
public:
DOptionMenuSliderJoyDeadZone_(const char *label, int axis, double min, double max, double step, int showval)
: DOptionMenuSliderBase_(label, min, max, step, showval)
2016-03-01 15:47:10 +00:00
{
mAxis = axis;
mNeg = 1;
}
double GetSliderValue()
{
double d = SELECTED_JOYSTICK->GetAxisDeadZone(mAxis);
mNeg = d < 0? -1:1;
return d;
}
void SetSliderValue(double val)
{
SELECTED_JOYSTICK->SetAxisDeadZone(mAxis, float(val * mNeg));
}
};
//=============================================================================
//
//
//
//=============================================================================
class DOptionMenuItemJoyMap_ : public DOptionMenuItemOptionBase_
2016-03-01 15:47:10 +00:00
{
int mAxis;
public:
DOptionMenuItemJoyMap_(const char *label, int axis, const char *values, int center)
: DOptionMenuItemOptionBase_(label, "none", values, NULL, center)
2016-03-01 15:47:10 +00:00
{
mAxis = axis;
}
int GetSelection()
{
double f = SELECTED_JOYSTICK->GetAxisMap(mAxis);
FOptionValues **opt = OptionValues.CheckKey(mValues);
if (opt != NULL && *opt != NULL)
{
// Map from joystick axis to menu selection.
for(unsigned i = 0; i < (*opt)->mValues.Size(); i++)
{
if (fabs(f - (*opt)->mValues[i].Value) < FLT_EPSILON)
{
return i;
}
}
}
return -1;
}
void SetSelection(int selection)
{
FOptionValues **opt = OptionValues.CheckKey(mValues);
// Map from menu selection to joystick axis.
if (opt == NULL || *opt == NULL || (unsigned)selection >= (*opt)->mValues.Size())
{
selection = JOYAXIS_None;
}
else
{
selection = (int)(*opt)->mValues[selection].Value;
}
SELECTED_JOYSTICK->SetAxisMap(mAxis, (EJoyAxis)selection);
}
};
//=============================================================================
//
//
//
//=============================================================================
class DOptionMenuItemInverter_ : public DOptionMenuItemOptionBase_
2016-03-01 15:47:10 +00:00
{
int mAxis;
public:
DOptionMenuItemInverter_(const char *label, int axis, int center)
: DOptionMenuItemOptionBase_(label, "none", "YesNo", NULL, center)
2016-03-01 15:47:10 +00:00
{
mAxis = axis;
}
int GetSelection()
{
float f = SELECTED_JOYSTICK->GetAxisScale(mAxis);
return f > 0? 0:1;
}
void SetSelection(int Selection)
{
float f = fabsf(SELECTED_JOYSTICK->GetAxisScale(mAxis));
if (Selection) f*=-1;
SELECTED_JOYSTICK->SetAxisScale(mAxis, f);
}
};
class DJoystickConfigMenu : public DOptionMenu
{
DECLARE_CLASS(DJoystickConfigMenu, DOptionMenu)
};
IMPLEMENT_CLASS(DJoystickConfigMenu, false, false)
2016-03-01 15:47:10 +00:00
//=============================================================================
//
// Executes a CCMD, action is a CCMD name
//
//=============================================================================
class DOptionMenuItemJoyConfigMenu_ : public DOptionMenuItemSubmenu_
2016-03-01 15:47:10 +00:00
{
DECLARE_CLASS(DOptionMenuItemJoyConfigMenu_, DOptionMenuItemSubmenu_)
2016-03-01 15:47:10 +00:00
IJoystickConfig *mJoy;
public:
DOptionMenuItemJoyConfigMenu_(const char *label = nullptr, IJoystickConfig *joy = nullptr)
: DOptionMenuItemSubmenu_(label, "JoystickConfigMenu")
2016-03-01 15:47:10 +00:00
{
mJoy = joy;
}
bool Activate()
{
UpdateJoystickConfigMenu(mJoy);
return DOptionMenuItemSubmenu_::Activate();
2016-03-01 15:47:10 +00:00
}
};
IMPLEMENT_CLASS(DOptionMenuItemJoyConfigMenu_, false, false)
2016-03-01 15:47:10 +00:00
/*=======================================
*
* Joystick Menu
*
*=======================================*/
DOptionMenuDescriptor *UpdateJoystickConfigMenu(IJoystickConfig *joy)
2016-03-01 15:47:10 +00:00
{
DMenuDescriptor **desc = MenuDescriptors.CheckKey(NAME_JoystickConfigMenu);
if (desc != NULL && (*desc)->IsKindOf(RUNTIME_CLASS(DOptionMenuDescriptor)))
2016-03-01 15:47:10 +00:00
{
DOptionMenuDescriptor *opt = (DOptionMenuDescriptor *)*desc;
DOptionMenuItem *it;
opt->mItems.Clear();
2016-03-01 15:47:10 +00:00
if (joy == NULL)
{
opt->mTitle = "Configure Controller";
it = new DOptionMenuItemStaticText_("Invalid controller specified for menu", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
}
else
{
opt->mTitle.Format("Configure %s", joy->GetName().GetChars());
SELECTED_JOYSTICK = joy;
it = new DOptionMenuSliderJoySensitivity_("Overall sensitivity", 0, 2, 0.1, 3);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuItemStaticText_(" ", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
if (joy->GetNumAxes() > 0)
{
it = new DOptionMenuItemStaticText_("Axis Configuration", true);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
for (int i = 0; i < joy->GetNumAxes(); ++i)
{
it = new DOptionMenuItemStaticText_(" ", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuItemJoyMap_(joy->GetAxisName(i), i, "JoyAxisMapNames", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuSliderJoyScale_("Overall sensitivity", i, 0, 4, 0.1, 3);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuItemInverter_("Invert", i, false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuSliderJoyDeadZone_("Dead Zone", i, 0, 0.9, 0.05, 3);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
}
}
else
{
it = new DOptionMenuItemStaticText_("No configurable axes", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
}
}
for (auto &p : opt->mItems)
{
GC::WriteBarrier(p);
}
2016-03-01 15:47:10 +00:00
opt->mScrollPos = 0;
opt->mSelectedItem = -1;
opt->mIndent = 0;
opt->mPosition = -25;
opt->CalcIndent();
return opt;
}
return NULL;
}
void UpdateJoystickMenu(IJoystickConfig *selected)
{
DMenuDescriptor **desc = MenuDescriptors.CheckKey(NAME_JoystickOptions);
if (desc != NULL && (*desc)->IsKindOf(RUNTIME_CLASS(DOptionMenuDescriptor)))
2016-03-01 15:47:10 +00:00
{
DOptionMenuDescriptor *opt = (DOptionMenuDescriptor *)*desc;
DOptionMenuItem *it;
2016-03-01 15:47:10 +00:00
opt->mItems.Clear();
int i;
int itemnum = -1;
I_GetJoysticks(Joysticks);
if ((unsigned)itemnum >= Joysticks.Size())
{
itemnum = Joysticks.Size() - 1;
}
if (selected != NULL)
{
for (i = 0; (unsigned)i < Joysticks.Size(); ++i)
{
if (Joysticks[i] == selected)
{
itemnum = i;
break;
}
}
}
// Todo: Block joystick for changing this one.
it = new DOptionMenuItemOption_("Enable controller support", "use_joystick", "YesNo", NULL, false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
#ifdef _WIN32
it = new DOptionMenuItemOption_("Enable DirectInput controllers", "joy_dinput", "YesNo", NULL, false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuItemOption_("Enable XInput controllers", "joy_xinput", "YesNo", NULL, false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuItemOption_("Enable raw PlayStation 2 adapters", "joy_ps2raw", "YesNo", NULL, false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
#endif
it = new DOptionMenuItemStaticText_(" ", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
if (Joysticks.Size() == 0)
{
it = new DOptionMenuItemStaticText_("No controllers detected", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
if (!use_joystick)
{
it = new DOptionMenuItemStaticText_("Controller support must be", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
it = new DOptionMenuItemStaticText_("enabled to detect any", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
}
}
else
{
it = new DOptionMenuItemStaticText_("Configure controllers:", false);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
for (int i = 0; i < (int)Joysticks.Size(); ++i)
{
it = new DOptionMenuItemJoyConfigMenu_(Joysticks[i]->GetName(), Joysticks[i]);
2016-03-01 15:47:10 +00:00
opt->mItems.Push(it);
if (i == itemnum) opt->mSelectedItem = opt->mItems.Size();
}
}
for (auto &p : opt->mItems)
{
GC::WriteBarrier(p);
}
2016-03-01 15:47:10 +00:00
if (opt->mSelectedItem >= (int)opt->mItems.Size())
{
opt->mSelectedItem = opt->mItems.Size() - 1;
}
opt->CalcIndent();
// If the joystick config menu is open, close it if the device it's
// open for is gone.
for (i = 0; (unsigned)i < Joysticks.Size(); ++i)
{
if (Joysticks[i] == SELECTED_JOYSTICK)
{
break;
}
}
if (i == (int)Joysticks.Size())
{
SELECTED_JOYSTICK = NULL;
if (DMenu::CurrentMenu != NULL && DMenu::CurrentMenu->IsKindOf(RUNTIME_CLASS(DJoystickConfigMenu)))
{
DMenu::CurrentMenu->Close();
}
}
}
}