2017-11-20 04:08:23 +00:00
|
|
|
#pragma once
|
|
|
|
#include "stdafx.h"
|
2022-07-25 10:36:08 +00:00
|
|
|
#include <memory>
|
2017-11-20 04:08:23 +00:00
|
|
|
#include "BaseControlDevice.h"
|
|
|
|
#include "IKeyManager.h"
|
|
|
|
#include "KeyManager.h"
|
2018-07-14 02:19:26 +00:00
|
|
|
#include "Console.h"
|
|
|
|
#include "EmulationSettings.h"
|
2017-11-20 04:08:23 +00:00
|
|
|
|
|
|
|
class SnesMouse : public BaseControlDevice
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
uint32_t _stateBuffer = 0;
|
|
|
|
uint8_t _sensitivity = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool HasCoordinates() override { return true; }
|
|
|
|
|
|
|
|
enum Buttons { Left = 0, Right };
|
|
|
|
|
|
|
|
void StreamState(bool saving) override
|
|
|
|
{
|
|
|
|
BaseControlDevice::StreamState(saving);
|
|
|
|
Stream(_stateBuffer, _sensitivity);
|
|
|
|
}
|
|
|
|
|
|
|
|
string GetKeyNames() override
|
|
|
|
{
|
|
|
|
return "LR";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InternalSetStateFromInput() override
|
|
|
|
{
|
|
|
|
SetPressedState(Buttons::Left, KeyManager::IsMouseButtonPressed(MouseButton::LeftButton));
|
|
|
|
SetPressedState(Buttons::Right, KeyManager::IsMouseButtonPressed(MouseButton::RightButton));
|
2018-07-14 02:19:26 +00:00
|
|
|
SetMovement(KeyManager::GetMouseMovement(_console->GetSettings()->GetMouseSensitivity(MouseDevice::SnesMouse)));
|
2017-11-20 04:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2022-07-25 10:36:08 +00:00
|
|
|
SnesMouse(std::shared_ptr<Console> console, uint8_t port) : BaseControlDevice(console, port)
|
2017-11-20 04:08:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:17 +00:00
|
|
|
void WriteRAM(uint16_t addr, uint8_t value) override
|
2017-11-20 04:08:23 +00:00
|
|
|
{
|
|
|
|
StrobeProcessWrite(value);
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:17 +00:00
|
|
|
uint8_t ReadRAM(uint16_t addr) override
|
2017-11-20 04:08:23 +00:00
|
|
|
{
|
|
|
|
uint8_t output = 0;
|
|
|
|
if((addr == 0x4016 && (_port & 0x01) == 0) || (addr == 0x4017 && (_port & 0x01) == 1)) {
|
2019-12-24 01:47:10 +00:00
|
|
|
StrobeProcessRead();
|
|
|
|
|
2017-11-20 04:08:23 +00:00
|
|
|
if(_strobe) {
|
2019-01-05 14:53:14 +00:00
|
|
|
_sensitivity = (_sensitivity + 1) % 3;
|
2017-11-20 04:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output = (_stateBuffer & 0x80000000) >> 31;
|
|
|
|
if(_port >= 2) {
|
|
|
|
output <<= 1;
|
|
|
|
}
|
|
|
|
_stateBuffer <<= 1;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefreshStateBuffer() override
|
|
|
|
{
|
|
|
|
MouseMovement mov = GetMovement();
|
|
|
|
int32_t dx = mov.dx * (1 + _sensitivity);
|
|
|
|
int32_t dy = mov.dy * (1 + _sensitivity);
|
|
|
|
|
|
|
|
uint32_t upFlag = dy < 0 ? 0x80 : 0;
|
|
|
|
uint32_t leftFlag = dx < 0 ? 0x80 : 0;
|
|
|
|
|
|
|
|
dx = std::min(std::abs(dx), 127);
|
|
|
|
dy = std::min(std::abs(dy), 127);
|
|
|
|
|
|
|
|
uint8_t byte1 = 0;
|
|
|
|
uint8_t byte2 = 0x01 | ((_sensitivity & 0x03) << 4) | (IsPressed(SnesMouse::Buttons::Left) ? 0x40 : 0) | (IsPressed(SnesMouse::Buttons::Right) ? 0x80 : 0);
|
|
|
|
uint8_t byte3 = dy | upFlag;
|
|
|
|
uint8_t byte4 = dx | leftFlag;
|
|
|
|
|
|
|
|
_stateBuffer = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4;
|
|
|
|
}
|
2022-07-25 10:36:08 +00:00
|
|
|
};
|