Proper gamepad support for Garry's Mod!

This is a little addon i’m working on. I’ve started developing it to play with two players on the same computer. (One with keyboard+mouse, other with gamepad)

Right now it has several functions:

Concmds that can be bound to a gamepad:

  • for looking around and moving the mouse in spawnmenu etc (+up +down +left2 +right2)
  • for clicking (These act as +attack and +attack2 when not in spawnmenu, +click and +click2)
  • A proper duck toggle command, that doesnt bork in multiplayer (+duck_toggle)
  • Two convars to control sensitivity for looking and mouse moving (x_sens and y_sens)

To-do list:

  • Prop rotation doesn’t work properly yet :frowning:
  • Make viewing and mouse moving analog

Please note that this is a WIP, and it might contain bugs. It also contains a PS3 Controller config, type ‘exec config_gamepad’ in console.
The default config is mapped like this:
DPAD: numpad 8 4 6 and 2
R2 + L2: left and right mouse button
R1 + L1: next/prev weapon
select: spawn menu
start: noclip
left joystick: moving
left joystick press: undo
right joystick: view/mouse movement
right joystick press: crouch toggle
triangle: use
square: run
circle: reload
cross: jump

To make it work with other gamepads right now you will have to use the bind console command. Later versions will feature a one time setup dialog, where you just press the controller button and then map it to a action with a handy GUI!

tl;dr
This fixes +lookup and +lookdown (+up and +down now), and allows you to bind a different key than mouse buttons for left and right clicking in GUI’s

DOWNLOAD

Seems like a neat addon, however looking at the code I’d recommend that you optimize it a bit, you should declare all those variables as local instead of global, same goes for the functions.

I don’t see the source for gmcl_mouseclick.dll?

What the hell are you doing?

[lua]
looking_up == 0 & looking_down == 0 & looking_left == 0 & looking_right ==0
[/lua]

Why would you use binary operations for that?

It’s on facepunch: http://www.facepunch.com/threads/793355
Download link there is broken, here’s a mirror: http://www.mauritsdebruin.com/data/garrysmod/lua/modules/gmcl_mouseclick.zip

And the source if you’re feeling lazy:



#include <windows.h>
#include "GMLuaModule.h"
#include <string>

#define MOUSE_LEFT 107
#define MOUSE_RIGHT 108
#define MOUSE_MIDDLE 109

// This is a macro to make life easier.
// When the dll is loaded it will call the function (Init)
GMOD_MODULE( Init, Shutdown );

LUA_FUNCTION(Click)
{
	int button = g_Lua->GetInteger(1);

	switch (button)
	{
		case MOUSE_LEFT:
			mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		case MOUSE_RIGHT:
			mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		case MOUSE_MIDDLE:
			mouse_event(MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		default:
			g_Lua->Push(false);
	}

	return 0;
}

LUA_FUNCTION(Press)
{
	int button = g_Lua->GetInteger(1);

	switch (button)
	{
		case MOUSE_LEFT:
			mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		case MOUSE_RIGHT:
			mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		case MOUSE_MIDDLE:
			mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		default:
			g_Lua->Push(false);
	}

	return 0;
}

LUA_FUNCTION(Release)
{
	int button = g_Lua->GetInteger(1);

	switch (button)
	{
		case MOUSE_LEFT:
			mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		case MOUSE_RIGHT:
			mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		case MOUSE_MIDDLE:
			mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
			g_Lua->Push(true);
			break;

		default:
			g_Lua->Push(false);
	}

	return 0;
}

// When the DLL is initialized it will call this function to set itself up.
// It should be used to set up your library so it's all ready to go.
int Init( void )
{
	// Msg is defined in GMLuaModule.h
	// It's actually calling the Lua function, Msg
	Msg("gmcl_mouseclick loaded
");

	// Lets make a global table filled with functions (a module)
	g_Lua->NewGlobalTable("mouse");
	ILuaObject* table = g_Lua->GetGlobal("mouse");
		table->SetMember("Click", Click);	
		table->SetMember("Press", Press);
		table->SetMember("Release", Release);

	// We're not deleting the table here - we're deleting the reference to it.
	// If you don't do this you're leaking memory.
	SAFE_UNREF( table );

	return 0;
}

// When the DLL is shutdown it calls this function.
// g_Lua should be assumed to have been released here
int Shutdown( void )
{

	return 0;
}


[editline]16th December 2011[/editline]

I’m pretty new to Lua, could you explain to me what’s wrong with that?

Since you seem know cpp, these operators do the same.

(It wasn’t written by him)

& is bitwise AND. What you want is a boolean operator, ‘and’ or the C-style ‘&&’ that garry added to Lua.

Oh whoops, that actually seems pretty obvious.

[editline]16th December 2011[/editline]

Also, i can ‘read’ some CPP, i just confuse programming languages now and then :wink:

[editline]16th December 2011[/editline]

Thanks for the support too, made all variables/functions locals, and replaced the &'s with and in those if’s.

Still trying to figure out what will be the best way to get analog view support, i’ll probably use the already existing joystick module…