mirror of
https://github.com/love2d/love.git
synced 2026-08-18 11:44:15 +02:00
Finally abstracted constants properly. Changed how the event module works; wasn't really abstract before. Removed libs we're not going to use: native, signal.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2009 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#include "Mouse.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace mouse
|
||||
{
|
||||
bool Mouse::getConstant(const char * in, Button & out)
|
||||
{
|
||||
return buttons.find(in, out);
|
||||
}
|
||||
|
||||
bool Mouse::getConstant(Button in, const char *& out)
|
||||
{
|
||||
return buttons.find(in, out);
|
||||
}
|
||||
|
||||
StringMap<Mouse::Button, Mouse::BUTTON_MAX_ENUM>::Entry Mouse::buttonEntries[] =
|
||||
{
|
||||
{"l", Mouse::BUTTON_LEFT},
|
||||
{"m", Mouse::BUTTON_MIDDLE},
|
||||
{"r", Mouse::BUTTON_RIGHT},
|
||||
{"wu", Mouse::BUTTON_WHEELUP},
|
||||
{"wd", Mouse::BUTTON_WHEELDOWN},
|
||||
{"x1", Mouse::BUTTON_X1},
|
||||
{"x2", Mouse::BUTTON_X2},
|
||||
};
|
||||
|
||||
StringMap<Mouse::Button, Mouse::BUTTON_MAX_ENUM> Mouse::buttons(Mouse::buttonEntries, sizeof(Mouse::buttonEntries));
|
||||
|
||||
} // mouse
|
||||
} // love
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
// LOVE
|
||||
#include <common/Module.h>
|
||||
#include <common/StringMap.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
@@ -32,17 +33,29 @@ namespace mouse
|
||||
{
|
||||
public:
|
||||
|
||||
enum MouseButton
|
||||
enum Button
|
||||
{
|
||||
MOUSE_LEFT = 1,
|
||||
MOUSE_MIDDLE,
|
||||
MOUSE_RIGHT,
|
||||
MOUSE_WHEELUP,
|
||||
MOUSE_WHEELDOWN
|
||||
BUTTON_INVALID,
|
||||
BUTTON_LEFT,
|
||||
BUTTON_MIDDLE,
|
||||
BUTTON_RIGHT,
|
||||
BUTTON_WHEELUP,
|
||||
BUTTON_WHEELDOWN,
|
||||
BUTTON_X1,
|
||||
BUTTON_X2,
|
||||
BUTTON_MAX_ENUM
|
||||
};
|
||||
|
||||
virtual ~Mouse(){};
|
||||
|
||||
static bool getConstant(const char * in, Button & out);
|
||||
static bool getConstant(Button in, const char *& out);
|
||||
|
||||
private:
|
||||
|
||||
static StringMap<Button, BUTTON_MAX_ENUM>::Entry buttonEntries[];
|
||||
static StringMap<Button, BUTTON_MAX_ENUM> buttons;
|
||||
|
||||
}; // Mouse
|
||||
|
||||
} // mouse
|
||||
|
||||
@@ -48,9 +48,9 @@ namespace sdl
|
||||
return y;
|
||||
}
|
||||
|
||||
void Mouse::getPosition(int * x, int * y) const
|
||||
void Mouse::getPosition(int & x, int & y) const
|
||||
{
|
||||
SDL_GetMouseState(x, y);
|
||||
SDL_GetMouseState(&x, &y);
|
||||
}
|
||||
|
||||
void Mouse::setPosition(int x, int y)
|
||||
@@ -63,9 +63,14 @@ namespace sdl
|
||||
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
|
||||
}
|
||||
|
||||
bool Mouse::isDown(int button) const
|
||||
bool Mouse::isDown(Button button) const
|
||||
{
|
||||
return (SDL_GetMouseState(0, 0) & SDL_BUTTON(button)) != 0;
|
||||
unsigned b = 0;
|
||||
|
||||
if(!buttons.find(button, b))
|
||||
return false;
|
||||
|
||||
return (SDL_GetMouseState(0, 0) & SDL_BUTTON(b)) != 0;
|
||||
}
|
||||
|
||||
bool Mouse::isVisible() const
|
||||
@@ -83,6 +88,19 @@ namespace sdl
|
||||
return (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON ? true : false);
|
||||
}
|
||||
|
||||
EnumMap<Mouse::Button, unsigned, Mouse::BUTTON_MAX_ENUM>::Entry Mouse::buttonEntries[] =
|
||||
{
|
||||
{ Mouse::BUTTON_LEFT, SDL_BUTTON_LEFT},
|
||||
{ Mouse::BUTTON_MIDDLE, SDL_BUTTON_MIDDLE},
|
||||
{ Mouse::BUTTON_RIGHT, SDL_BUTTON_RIGHT},
|
||||
{ Mouse::BUTTON_WHEELUP, SDL_BUTTON_WHEELUP},
|
||||
{ Mouse::BUTTON_WHEELDOWN, SDL_BUTTON_WHEELDOWN},
|
||||
{ Mouse::BUTTON_X1, SDL_BUTTON_X1},
|
||||
{ Mouse::BUTTON_X2, SDL_BUTTON_X2},
|
||||
};
|
||||
|
||||
EnumMap<Mouse::Button, unsigned, Mouse::BUTTON_MAX_ENUM> Mouse::buttons(Mouse::buttonEntries, sizeof(Mouse::buttonEntries));
|
||||
|
||||
} // sdl
|
||||
} // mouse
|
||||
} // love
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
// LOVE
|
||||
#include <mouse/Mouse.h>
|
||||
#include <common/EnumMap.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
@@ -39,14 +40,19 @@ namespace sdl
|
||||
|
||||
int getX() const;
|
||||
int getY() const;
|
||||
void getPosition(int * x, int * y) const;
|
||||
void getPosition(int & x, int & y) const;
|
||||
void setPosition(int x, int y);
|
||||
void setVisible(bool visible);
|
||||
bool isDown(int button) const;
|
||||
bool isDown(Button button) const;
|
||||
bool isVisible() const;
|
||||
void setGrab(bool grab);
|
||||
bool isGrabbed() const;
|
||||
|
||||
public:
|
||||
|
||||
static EnumMap<Button, unsigned, BUTTON_MAX_ENUM>::Entry buttonEntries[];
|
||||
static EnumMap<Button, unsigned, BUTTON_MAX_ENUM> buttons;
|
||||
|
||||
}; // Mouse
|
||||
|
||||
} // sdl
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace love
|
||||
namespace mouse
|
||||
{
|
||||
namespace sdl
|
||||
{
|
||||
{
|
||||
static Mouse * instance = 0;
|
||||
|
||||
int w_getX(lua_State * L)
|
||||
@@ -43,7 +43,7 @@ namespace sdl
|
||||
int w_getPosition(lua_State * L)
|
||||
{
|
||||
int x, y;
|
||||
instance->getPosition(&x, &y);
|
||||
instance->getPosition(x, y);
|
||||
lua_pushinteger(L, x);
|
||||
lua_pushinteger(L, y);
|
||||
return 2;
|
||||
@@ -59,8 +59,13 @@ namespace sdl
|
||||
|
||||
int w_isDown(lua_State * L)
|
||||
{
|
||||
int b = luaL_checkint(L, 1);
|
||||
luax_pushboolean(L, instance->isDown(b));
|
||||
Mouse::Button button;
|
||||
|
||||
if(!Mouse::getConstant(luaL_checkstring(L, 1), button))
|
||||
luax_pushboolean(L, false);
|
||||
else
|
||||
luax_pushboolean(L, instance->isDown(button));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -104,17 +109,6 @@ namespace sdl
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// List of constants.
|
||||
static const Constant constants[] = {
|
||||
{ "mouse_left", Mouse::MOUSE_LEFT },
|
||||
{ "mouse_middle", Mouse::MOUSE_MIDDLE },
|
||||
{ "mouse_right", Mouse::MOUSE_RIGHT },
|
||||
{ "mouse_wheelup", Mouse::MOUSE_WHEELUP },
|
||||
{ "mouse_wheeldown", Mouse::MOUSE_WHEELDOWN },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
int luaopen_love_mouse(lua_State * L)
|
||||
{
|
||||
if(instance == 0)
|
||||
@@ -135,7 +129,6 @@ namespace sdl
|
||||
w.flags = MODULE_T;
|
||||
w.functions = functions;
|
||||
w.types = 0;
|
||||
w.constants = constants;
|
||||
|
||||
return luax_register_module(L, w);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user