mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Replaced the mouse button constants ("l", "r", "m", etc.) with button numbers. 1 is the primary button, 2 is the secondary button, 3 is middle-mouse, etc. Resolves issue #1019.
This commit is contained in:
@@ -483,7 +483,6 @@ source_group("modules\\love" FILES ${LOVE_SRC_MODULE_LOVE})
|
||||
set(LOVE_SRC_MODULE_MOUSE_ROOT
|
||||
src/modules/mouse/Cursor.cpp
|
||||
src/modules/mouse/Cursor.h
|
||||
src/modules/mouse/Mouse.cpp
|
||||
src/modules/mouse/Mouse.h
|
||||
src/modules/mouse/wrap_Cursor.cpp
|
||||
src/modules/mouse/wrap_Cursor.h
|
||||
|
||||
@@ -565,8 +565,6 @@
|
||||
FA0B7DE51A95902C000E1D17 /* Cursor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7C0E1A95902C000E1D17 /* Cursor.cpp */; };
|
||||
FA0B7DE61A95902C000E1D17 /* Cursor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7C0E1A95902C000E1D17 /* Cursor.cpp */; };
|
||||
FA0B7DE71A95902C000E1D17 /* Cursor.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B7C0F1A95902C000E1D17 /* Cursor.h */; };
|
||||
FA0B7DE81A95902C000E1D17 /* Mouse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7C101A95902C000E1D17 /* Mouse.cpp */; };
|
||||
FA0B7DE91A95902C000E1D17 /* Mouse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7C101A95902C000E1D17 /* Mouse.cpp */; };
|
||||
FA0B7DEA1A95902C000E1D17 /* Mouse.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B7C111A95902C000E1D17 /* Mouse.h */; };
|
||||
FA0B7DEB1A95902C000E1D17 /* Cursor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7C131A95902C000E1D17 /* Cursor.cpp */; };
|
||||
FA0B7DEC1A95902C000E1D17 /* Cursor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7C131A95902C000E1D17 /* Cursor.cpp */; };
|
||||
@@ -1301,7 +1299,6 @@
|
||||
FA0B7C0C1A95902C000E1D17 /* wrap_RandomGenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_RandomGenerator.h; sourceTree = "<group>"; };
|
||||
FA0B7C0E1A95902C000E1D17 /* Cursor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Cursor.cpp; sourceTree = "<group>"; };
|
||||
FA0B7C0F1A95902C000E1D17 /* Cursor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cursor.h; sourceTree = "<group>"; };
|
||||
FA0B7C101A95902C000E1D17 /* Mouse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Mouse.cpp; sourceTree = "<group>"; };
|
||||
FA0B7C111A95902C000E1D17 /* Mouse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Mouse.h; sourceTree = "<group>"; };
|
||||
FA0B7C131A95902C000E1D17 /* Cursor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Cursor.cpp; sourceTree = "<group>"; };
|
||||
FA0B7C141A95902C000E1D17 /* Cursor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cursor.h; sourceTree = "<group>"; };
|
||||
@@ -2403,7 +2400,6 @@
|
||||
children = (
|
||||
FA0B7C0E1A95902C000E1D17 /* Cursor.cpp */,
|
||||
FA0B7C0F1A95902C000E1D17 /* Cursor.h */,
|
||||
FA0B7C101A95902C000E1D17 /* Mouse.cpp */,
|
||||
FA0B7C111A95902C000E1D17 /* Mouse.h */,
|
||||
FA0B7C121A95902C000E1D17 /* sdl */,
|
||||
FA0B7C171A95902C000E1D17 /* wrap_Cursor.cpp */,
|
||||
@@ -3267,7 +3263,6 @@
|
||||
FA0B7D3D1A95902C000E1D17 /* Image.cpp in Sources */,
|
||||
FA0B7B351A958EA3000E1D17 /* wuff_convert.c in Sources */,
|
||||
FA0B7E431A95902C000E1D17 /* wrap_CircleShape.cpp in Sources */,
|
||||
FA0B7DE91A95902C000E1D17 /* Mouse.cpp in Sources */,
|
||||
FA0B7CE61A95902C000E1D17 /* wrap_Source.cpp in Sources */,
|
||||
FA0B7AA21A958EA3000E1D17 /* b2PulleyJoint.cpp in Sources */,
|
||||
FA0B7CCE1A95902C000E1D17 /* Audio.cpp in Sources */,
|
||||
@@ -3553,7 +3548,6 @@
|
||||
FA0B7D3C1A95902C000E1D17 /* Image.cpp in Sources */,
|
||||
FA0B7A8C1A958EA3000E1D17 /* b2DistanceJoint.cpp in Sources */,
|
||||
FA0B7E421A95902C000E1D17 /* wrap_CircleShape.cpp in Sources */,
|
||||
FA0B7DE81A95902C000E1D17 /* Mouse.cpp in Sources */,
|
||||
FA0B7CE51A95902C000E1D17 /* wrap_Source.cpp in Sources */,
|
||||
FA0B792C1A958E3B000E1D17 /* Memoizer.cpp in Sources */,
|
||||
FA0B7CCD1A95902C000E1D17 /* Audio.cpp in Sources */,
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "filesystem/DroppedFile.h"
|
||||
#include "filesystem/Filesystem.h"
|
||||
#include "keyboard/sdl/Keyboard.h"
|
||||
#include "mouse/Mouse.h"
|
||||
#include "joystick/JoystickModule.h"
|
||||
#include "joystick/sdl/Joystick.h"
|
||||
#include "touch/sdl/Touch.h"
|
||||
@@ -158,7 +157,6 @@ Message *Event::convert(const SDL_Event &e) const
|
||||
|
||||
love::keyboard::Keyboard::Key key = love::keyboard::Keyboard::KEY_UNKNOWN;
|
||||
love::keyboard::Keyboard::Scancode scancode = love::keyboard::Keyboard::SCANCODE_UNKNOWN;
|
||||
love::mouse::Mouse::Button button;
|
||||
|
||||
const char *txt;
|
||||
const char *txt2;
|
||||
@@ -245,14 +243,26 @@ Message *Event::convert(const SDL_Event &e) const
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (buttons.find(e.button.button, button) && mouse::Mouse::getConstant(button, txt))
|
||||
{
|
||||
// SDL uses button index 3 for the right mouse button, but we use
|
||||
// index 2.
|
||||
int button = e.button.button;
|
||||
switch (button)
|
||||
{
|
||||
case SDL_BUTTON_RIGHT:
|
||||
button = 2;
|
||||
break;
|
||||
case SDL_BUTTON_MIDDLE:
|
||||
button = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
double x = (double) e.button.x;
|
||||
double y = (double) e.button.y;
|
||||
windowToPixelCoords(&x, &y);
|
||||
vargs.push_back(new Variant(x));
|
||||
vargs.push_back(new Variant(y));
|
||||
vargs.push_back(new Variant(txt, strlen(txt)));
|
||||
vargs.push_back(new Variant((double) button));
|
||||
vargs.push_back(new Variant(e.button.which == SDL_TOUCH_MOUSEID));
|
||||
msg = new Message((e.type == SDL_MOUSEBUTTONDOWN) ?
|
||||
"mousepressed" : "mousereleased",
|
||||
@@ -798,20 +808,6 @@ std::map<SDL_Keycode, love::keyboard::Keyboard::Key> Event::createKeyMap()
|
||||
|
||||
std::map<SDL_Keycode, love::keyboard::Keyboard::Key> Event::keys = Event::createKeyMap();
|
||||
|
||||
EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM>::Entry Event::buttonEntries[] =
|
||||
{
|
||||
{love::mouse::Mouse::BUTTON_LEFT, SDL_BUTTON_LEFT},
|
||||
{love::mouse::Mouse::BUTTON_MIDDLE, SDL_BUTTON_MIDDLE},
|
||||
{love::mouse::Mouse::BUTTON_RIGHT, SDL_BUTTON_RIGHT},
|
||||
{love::mouse::Mouse::BUTTON_X1, SDL_BUTTON_X1},
|
||||
{love::mouse::Mouse::BUTTON_X2, SDL_BUTTON_X2+0},
|
||||
{love::mouse::Mouse::BUTTON_X3, SDL_BUTTON_X2+1},
|
||||
{love::mouse::Mouse::BUTTON_X4, SDL_BUTTON_X2+2},
|
||||
{love::mouse::Mouse::BUTTON_X5, SDL_BUTTON_X2+3},
|
||||
};
|
||||
|
||||
EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM> Event::buttons(Event::buttonEntries, sizeof(Event::buttonEntries));
|
||||
|
||||
} // sdl
|
||||
} // event
|
||||
} // love
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
// LOVE
|
||||
#include "event/Event.h"
|
||||
#include "common/runtime.h"
|
||||
#include "common/EnumMap.h"
|
||||
|
||||
// SDL
|
||||
#include <SDL_events.h>
|
||||
@@ -77,9 +76,6 @@ private:
|
||||
static std::map<SDL_Keycode, love::keyboard::Keyboard::Key> createKeyMap();
|
||||
static std::map<SDL_Keycode, love::keyboard::Keyboard::Key> keys;
|
||||
|
||||
static EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM>::Entry buttonEntries[];
|
||||
static EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM> buttons;
|
||||
|
||||
}; // Event
|
||||
|
||||
} // sdl
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 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", BUTTON_LEFT},
|
||||
{"m", BUTTON_MIDDLE},
|
||||
{"r", BUTTON_RIGHT},
|
||||
{"x1", BUTTON_X1},
|
||||
{"x2", BUTTON_X2},
|
||||
{"x3", BUTTON_X3},
|
||||
{"x4", BUTTON_X4},
|
||||
{"x5", BUTTON_X5},
|
||||
};
|
||||
|
||||
StringMap<Mouse::Button, Mouse::BUTTON_MAX_ENUM> Mouse::buttons(Mouse::buttonEntries, sizeof(Mouse::buttonEntries));
|
||||
|
||||
} // mouse
|
||||
} // love
|
||||
@@ -24,9 +24,11 @@
|
||||
// LOVE
|
||||
#include "Cursor.h"
|
||||
#include "common/Module.h"
|
||||
#include "common/StringMap.h"
|
||||
#include "image/ImageData.h"
|
||||
|
||||
// C++
|
||||
#include <vector>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace mouse
|
||||
@@ -36,21 +38,6 @@ class Mouse : public Module
|
||||
{
|
||||
public:
|
||||
|
||||
// FIXME: We should probably just use button numbers.
|
||||
enum Button
|
||||
{
|
||||
BUTTON_INVALID = 0,
|
||||
BUTTON_LEFT,
|
||||
BUTTON_MIDDLE,
|
||||
BUTTON_RIGHT,
|
||||
BUTTON_X1,
|
||||
BUTTON_X2,
|
||||
BUTTON_X3,
|
||||
BUTTON_X4,
|
||||
BUTTON_X5,
|
||||
BUTTON_MAX_ENUM
|
||||
};
|
||||
|
||||
virtual ~Mouse() {}
|
||||
|
||||
// Implements Module.
|
||||
@@ -73,21 +60,13 @@ public:
|
||||
virtual void setY(double y) = 0;
|
||||
virtual void setPosition(double x, double y) = 0;
|
||||
virtual void setVisible(bool visible) = 0;
|
||||
virtual bool isDown(Button *buttonlist) const = 0;
|
||||
virtual bool isDown(const std::vector<int> &buttons) const = 0;
|
||||
virtual bool isVisible() const = 0;
|
||||
virtual void setGrabbed(bool grab) = 0;
|
||||
virtual bool isGrabbed() const = 0;
|
||||
virtual bool setRelativeMode(bool relative) = 0;
|
||||
virtual bool getRelativeMode() const = 0;
|
||||
|
||||
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
|
||||
|
||||
@@ -176,12 +176,27 @@ void Mouse::setVisible(bool visible)
|
||||
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
|
||||
}
|
||||
|
||||
bool Mouse::isDown(Button *buttonlist) const
|
||||
bool Mouse::isDown(const std::vector<int> &buttons) const
|
||||
{
|
||||
Uint32 buttonstate = SDL_GetMouseState(nullptr, nullptr);
|
||||
|
||||
for (Button button = *buttonlist; button != BUTTON_MAX_ENUM; button = *(++buttonlist))
|
||||
for (int button : buttons)
|
||||
{
|
||||
if (button <= 0)
|
||||
continue;
|
||||
|
||||
// We use button index 2 to represent the right mouse button, but SDL
|
||||
// uses 2 to represent the middle mouse button.
|
||||
switch (button)
|
||||
{
|
||||
case 2:
|
||||
button = SDL_BUTTON_RIGHT;
|
||||
break;
|
||||
case 3:
|
||||
button = SDL_BUTTON_MIDDLE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (buttonstate & SDL_BUTTON(button))
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class Mouse : public love::mouse::Mouse
|
||||
public:
|
||||
|
||||
// Implements Module.
|
||||
const char *getName() const;
|
||||
const char *getName() const override;
|
||||
|
||||
Mouse();
|
||||
~Mouse();
|
||||
@@ -62,7 +62,7 @@ public:
|
||||
void setY(double y);
|
||||
void setPosition(double x, double y);
|
||||
void setVisible(bool visible);
|
||||
bool isDown(Button *buttonlist) const;
|
||||
bool isDown(const std::vector<int> &buttons) const;
|
||||
bool isVisible() const;
|
||||
void setGrabbed(bool grab);
|
||||
bool isGrabbed() const;
|
||||
|
||||
@@ -142,20 +142,14 @@ int w_setPosition(lua_State *L)
|
||||
|
||||
int w_isDown(lua_State *L)
|
||||
{
|
||||
Mouse::Button b;
|
||||
unsigned int num = lua_gettop(L);
|
||||
Mouse::Button *buttonlist = new Mouse::Button[num+1];
|
||||
unsigned int counter = 0;
|
||||
int num = lua_gettop(L);
|
||||
std::vector<int> buttons;
|
||||
buttons.reserve(num);
|
||||
|
||||
for (unsigned int i = 0; i < num; i++)
|
||||
{
|
||||
if (Mouse::getConstant(luaL_checkstring(L, i+1), b))
|
||||
buttonlist[counter++] = b;
|
||||
}
|
||||
buttonlist[counter] = Mouse::BUTTON_MAX_ENUM;
|
||||
for (int i = 0; i < num; i++)
|
||||
buttons.push_back(luaL_checkint(L, i + 1));
|
||||
|
||||
luax_pushboolean(L, instance()->isDown(buttonlist));
|
||||
delete[] buttonlist;
|
||||
luax_pushboolean(L, instance()->isDown(buttons));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user