Verified Commit b3f4919b authored by RobLoach's avatar RobLoach
Browse files

Initial work on move to mouse libretro API

parent ca95e116
......@@ -7,10 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## 0.14.3 - Unreleased
### Changed
- Updated the input description names
- Switch Keyboard input from SDL API to libretro API
- Switch Keyboard/Mouse input from SDL API to libretro API
### Added
- `love.keyboard.isScancodeDown()`
- `love.mouse` `x1` and `x2` buttons
## 0.14.2 - 2018-02-17
### Changed
......
......@@ -90,19 +90,11 @@ bool ChaiLove::update() {
event.quit();
return !event.quitstatus;
break;
case SDL_MOUSEMOTION:
mouse.moveEvent(sdlEvent.motion);
break;
case SDL_MOUSEBUTTONDOWN:
mouse.buttonEvent(sdlEvent.button);
break;
case SDL_MOUSEBUTTONUP:
mouse.buttonEvent(sdlEvent.button);
break;
}
}
// Update some of the sub-systems.
mouse.update();
joystick.update();
keyboard.update();
......
#include "mouse.h"
#include <SDL.h>
#include <libretro.h>
#include <string>
#include "libretro.h"
#include "Types/Graphics/Point.h"
#include "../ChaiLove.h"
#include <string>
using love::Types::Graphics::Point;
using ::ChaiLove;
......@@ -12,31 +12,9 @@ using ::ChaiLove;
namespace love {
bool mouse::load() {
setVisible(false);
return true;
}
bool mouse::isVisible() {
return SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE;
}
mouse& mouse::setVisible(bool visible) {
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
return *this;
}
mouse& mouse::setX(int x) {
m_x = x;
SDL_WarpMouse(m_x, m_y);
return *this;
}
mouse& mouse::setY(int y) {
m_y = y;
SDL_WarpMouse(m_x, m_y);
return *this;
}
int mouse::getX() {
return m_x;
}
......@@ -46,7 +24,7 @@ int mouse::getY() {
}
bool mouse::isDown(int button) {
return buttonState[button] == true;
return buttonState[button] != 0;
}
bool mouse::isDown(const std::string& button) {
return isDown(getButtonKey(button));
......@@ -67,6 +45,10 @@ int mouse::getButtonKey(const std::string& button) {
return RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP;
} else if (button == "horizwheeldown") {
return RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN;
} else if (button == "x1") {
return RETRO_DEVICE_ID_MOUSE_BUTTON_4;
} else if (button == "x2") {
return RETRO_DEVICE_ID_MOUSE_BUTTON_5;
}
return -1;
}
......@@ -87,23 +69,51 @@ std::string mouse::getButtonName(int button) {
return "horizwheelup";
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
return "horizwheeldown";
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
return "x1";
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
return "x2";
}
return "unknown";
}
void mouse::moveEvent(SDL_MouseMotionEvent event) {
m_x = event.x;
m_y = event.y;
ChaiLove::getInstance()->script->mousemoved(m_x, m_y);
}
void mouse::update() {
int16_t state, dx, dy;
void mouse::buttonEvent(SDL_MouseButtonEvent event) {
if (event.state == SDL_PRESSED) {
ChaiLove::getInstance()->script->mousepressed(m_x, m_y, event.button);
} else if (event.state == SDL_RELEASED) {
ChaiLove::getInstance()->script->mousereleased(m_x, m_y, event.button);
// Update the x/y coordinates.
dx = ChaiLove::input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
dy = ChaiLove::input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
if (dx != 0 || dy != 0) {
m_x = m_x + dx;
m_y = m_y + dy;
mousemoved(m_x, m_y, dx, dy);
}
buttonState[event.button] = event.state;
// Update all buttons.
for (int i = RETRO_DEVICE_ID_MOUSE_LEFT; i <= RETRO_DEVICE_ID_MOUSE_BUTTON_5; i++) {
state = ChaiLove::input_state_cb(0, RETRO_DEVICE_MOUSE, 0, i);
if (state != buttonState[i]) {
buttonState[i] = state;
if (buttonState[i] == 0) {
mousereleased(m_x, m_y, i);
} else {
mousepressed(m_x, m_y, i);
}
}
}
}
void mouse::mousemoved(int x, int y, int dx, int dy) {
ChaiLove::getInstance()->script->mousemoved(x, y, dx, dy);
}
void mouse::mousepressed(int x, int y, int button) {
ChaiLove::getInstance()->script->mousepressed(x, y, button);
}
void mouse::mousereleased(int x, int y, int button) {
ChaiLove::getInstance()->script->mousereleased(x, y, button);
}
Point mouse::getPosition() {
......
#ifndef SRC_LOVE_MOUSE_H_
#define SRC_LOVE_MOUSE_H_
#include <SDL.h>
#include <string>
#include "Types/Graphics/Point.h"
#include "libretro.h"
namespace love {
......@@ -13,43 +13,6 @@ namespace love {
class mouse {
public:
bool load();
/**
* @brief Sets the current visibility of the cursor.
*
* @param visible True to set the cursor to visible, false to hide the cursor.
*
* @see love.mouse.getVisible
*/
mouse& setVisible(bool visible);
/**
* @brief Checks if the cursor is visible.
*
* @return True if the cursor to visible, false if the cursor is hidden.
*
* @see love.mouse.setVisible
*/
bool isVisible();
/**
* @brief Sets the current X position of the mouse.
*
* @param x The new position of the mouse along the x-axis.
*
* @return The mouse module.
*/
mouse& setX(int x);
/**
* @brief Sets the current Y position of the mouse.
*
* @param y The new position of the mouse along the y-axis.
*
* @return The mouse module.
*
* @see love.mouse.getY
*/
mouse& setY(int y);
/**
* @brief Returns the current x-position of the mouse.
......@@ -92,14 +55,17 @@ class mouse {
*/
bool isDown(const std::string& button);
void update();
int getButtonKey(const std::string& button);
std::string getButtonName(int button);
int m_x, m_y;
int buttonState[20];
int16_t m_x, m_y;
int16_t buttonState[RETRO_DEVICE_ID_MOUSE_BUTTON_5];
void moveEvent(SDL_MouseMotionEvent event);
void buttonEvent(SDL_MouseButtonEvent event);
void mousemoved(int x, int y, int dx, int dy);
void mousepressed(int x, int y, int button);
void mousereleased(int x, int y, int button);
};
} // namespace love
......
......@@ -242,10 +242,6 @@ script::script(const std::string& file) {
chai.add(fun(&system::getVersionString), "getVersionString");
// Mouse
chai.add(fun(&mouse::setVisible), "setVisible");
chai.add(fun(&mouse::isVisible), "isVisible");
chai.add(fun(&mouse::setX), "setX");
chai.add(fun(&mouse::setY), "setY");
chai.add(fun(&mouse::getX), "getX");
chai.add(fun(&mouse::getY), "getY");
chai.add(fun(&mouse::getPosition), "getPosition");
......@@ -377,7 +373,7 @@ script::script(const std::string& file) {
hasmousereleased = false;
}
try {
chaimousemoved = chai.eval<std::function<void(int, int)> >("mousemoved");
chaimousemoved = chai.eval<std::function<void(int, int, int, int)> >("mousemoved");
}
catch (const std::exception& e) {
std::cout << "[ChaiLove] [script] mousemoved() " << e.what() << std::endl;
......@@ -542,11 +538,11 @@ void script::mousereleased(int x, int y, int button) {
#endif
}
void script::mousemoved(int x, int y) {
void script::mousemoved(int x, int y, int dx, int dy) {
#ifdef __HAVE_CHAISCRIPT__
if (hasmousemoved) {
try {
chaimousemoved(x, y);
chaimousemoved(x, y, dx, dy);
}
catch (const std::exception& e) {
std::cout << "[ChaiLove] [script] Failed to call mousemoved(): " << e.what() << std::endl;
......
......@@ -195,8 +195,10 @@ class script {
*
* @param x The mouse position on the x-axis.
* @param y The mouse position on the y-axis.
* @param dx The amount moved along the x-axis since the last time love.mousemoved was called.
* @param dy The amount moved along the y-axis since the last time love.mousemoved was called.
*/
void mousemoved(int x, int y);
void mousemoved(int x, int y, int dx, int dy);
/**
* @brief Called when a key on the keyboard has been pressed.
......@@ -243,7 +245,7 @@ class script {
std::function<void(int, const std::string&)> chaijoystickreleased;
std::function<void(int, int, int)> chaimousepressed;
std::function<void(int, int, int)> chaimousereleased;
std::function<void(int, int)> chaimousemoved;
std::function<void(int, int, int, int)> chaimousemoved;
std::function<void(const std::string&, int)> chaikeypressed;
std::function<void(const std::string&, int)> chaikeyreleased;
bool hasUpdate = true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment