//===========================================================================//
// File:    l4padbindings.h                                                  //
// Project: MUNGA           Brick: PadRIO binding profiles                   //
// Contents: The rebindable input profile (vRIO bindings format)             //
//---------------------------------------------------------------------------//
// Copyright (C) 1994-1995, Virtual World Entertainment, Inc.                //
//                      PROPRIETARY AND CONFIDENTIAL                         //
//===========================================================================//

#pragma once

#include "..\munga\style.h"

//########################################################################
// The bindings file (vRIO's format, shared grammar):
//
//   key <name>      button <addr> [toggle]
//   key <name>      axis <axis> deflect <n> | rate <n>
//   pad <button>    button <addr> [toggle]
//   padaxis <src>   axis <axis> [invert] [deadzone <d>] [rate <n>]
//
// <addr> is a RIO input address: lamp buttons 0x00-0x47, internal
// keypad 0x50-0x5F, external keypad 0x60-0x6F. Loaded from
// bindings.txt beside the exe; written there (self-documenting, with
// the full default profile) on first run. Bad lines are logged and
// skipped, good lines always win.
//########################################################################

enum PadBindRioAxis
{
	BindAxisThrottle = 0,
	BindAxisLeftPedal,
	BindAxisRightPedal,
	BindAxisJoystickY,
	BindAxisJoystickX,
	BindAxisCount
};

enum PadBindKeyMode
{
	BindKeyDeflect = 0,	// hold at value while down, spring back
	BindKeyRate			// walk by value/second while down, position holds
};

enum PadBindPadAxis
{
	BindPadLeftStickX = 0,
	BindPadLeftStickY,
	BindPadRightStickX,
	BindPadRightStickY,
	BindPadLeftTrigger,
	BindPadRightTrigger,
	BindPadAxisCount
};

struct PadKeyButtonBinding
{
	int		virtualKey;
	int		address;		// 0x00-0x47 button, 0x50-0x6F keypad
	Logical	toggle;
	// runtime edge/latch state
	Logical	wasDown;
	Logical	latched;
};

struct PadKeyAxisBinding
{
	int		virtualKey;
	int		axis;			// PadBindRioAxis
	int		mode;			// PadBindKeyMode
	Scalar	value;
};

struct PadPadButtonBinding
{
	unsigned short	padMask;	// XINPUT_GAMEPAD_*
	int				address;
	Logical			toggle;
	Logical			wasDown;
	Logical			latched;
};

struct PadPadAxisBinding
{
	int		source;			// PadBindPadAxis
	int		axis;			// PadBindRioAxis
	Logical	invert;
	Scalar	deadzone;		// normalized 0..1
	Scalar	rate;			// 0 = direct position, >0 = speed integrate
};

struct PadBindingProfile
{
	enum
	{
		maxKeyButtons	= 128,
		maxKeyAxes		= 32,
		maxPadButtons	= 32,
		maxPadAxes		= 16
	};

	PadKeyButtonBinding		keyButtons[maxKeyButtons];
	int						keyButtonCount;
	PadKeyAxisBinding		keyAxes[maxKeyAxes];
	int						keyAxisCount;
	PadPadButtonBinding		padButtons[maxPadButtons];
	int						padButtonCount;
	PadPadAxisBinding		padAxes[maxPadAxes];
	int						padAxisCount;
};

// Load bindings.txt from the working directory into the profile,
// writing the default profile file first when absent. Errors go to
// the debug log with line numbers.
void
	PadBindings_Load(PadBindingProfile *profile);
