| 1 |
#include "ArkanoidPad.hh" |
| 2 |
|
| 3 |
#include "Event.hh" |
| 4 |
#include "MSXEventDistributor.hh" |
| 5 |
#include "StateChange.hh" |
| 6 |
#include "StateChangeDistributor.hh" |
| 7 |
#include "serialize.hh" |
| 8 |
#include "serialize_meta.hh" |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace openmsx { |
| 22 |
|
| 23 |
static constexpr int POS_MIN = 55; |
| 24 |
static constexpr int POS_MAX = 325; |
| 25 |
static constexpr int POS_CENTER = 236; |
| 26 |
static constexpr int SCALE = 2; |
| 27 |
|
| 28 |
|
| 29 |
ArkanoidPad::ArkanoidPad(MSXEventDistributor& eventDistributor_, |
| 30 |
StateChangeDistributor& stateChangeDistributor_) |
| 31 |
: eventDistributor(eventDistributor_) |
| 32 |
, stateChangeDistributor(stateChangeDistributor_) |
| 33 |
, dialPos(POS_CENTER) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
ArkanoidPad::~ArkanoidPad() |
| 38 |
{ |
| 39 |
if (isPluggedIn()) { |
| 40 |
ArkanoidPad::unplugHelper(EmuTime::dummy()); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
zstring_view ArkanoidPad::getName() const |
| 47 |
{ |
| 48 |
return "arkanoidpad"; |
| 49 |
} |
| 50 |
|
| 51 |
zstring_view ArkanoidPad::getDescription() const |
| 52 |
{ |
| 53 |
return "Arkanoid pad"; |
| 54 |
} |
| 55 |
|
| 56 |
void ArkanoidPad::plugHelper(Connector& , EmuTime ) |
| 57 |
{ |
| 58 |
eventDistributor.registerEventListener(*this); |
| 59 |
stateChangeDistributor.registerListener(*this); |
| 60 |
} |
| 61 |
|
| 62 |
void ArkanoidPad::unplugHelper(EmuTime ) |
| 63 |
{ |
| 64 |
stateChangeDistributor.unregisterListener(*this); |
| 65 |
eventDistributor.unregisterEventListener(*this); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
uint8_t ArkanoidPad::read(EmuTime ) |
| 70 |
{ |
| 71 |
return buttonStatus | ((shiftReg & 0x100) >> 8); |
| 72 |
} |
| 73 |
|
| 74 |
void ArkanoidPad::write(uint8_t value, EmuTime ) |
| 75 |
{ |
| 76 |
uint8_t diff = lastValue ^ value; |
| 77 |
lastValue = value; |
| 78 |
|
| 79 |
if (diff & value & 0x4) { |
| 80 |
|
| 81 |
shiftReg = dialPos; |
| 82 |
} |
| 83 |
if (diff & value & 0x1) { |
| 84 |
|
| 85 |
shiftReg = (shiftReg << 1) | (shiftReg & 1); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
void ArkanoidPad::signalMSXEvent(const Event& event, |
| 91 |
EmuTime time) noexcept |
| 92 |
{ |
| 93 |
visit(overloaded{ |
| 94 |
[&](const MouseMotionEvent& e) { |
| 95 |
int newPos = std::min(POS_MAX, |
| 96 |
std::max(POS_MIN, |
| 97 |
dialPos + e.getX() / SCALE)); |
| 98 |
int delta = newPos - dialPos; |
| 99 |
if (delta != 0) { |
| 100 |
stateChangeDistributor.distributeNew<ArkanoidState>( |
| 101 |
time, delta, false, false); |
| 102 |
} |
| 103 |
}, |
| 104 |
[&](const MouseButtonDownEvent& ) { |
| 105 |
|
| 106 |
if (buttonStatus & 2) { |
| 107 |
stateChangeDistributor.distributeNew<ArkanoidState>( |
| 108 |
time, 0, true, false); |
| 109 |
} |
| 110 |
}, |
| 111 |
[&](const MouseButtonUpEvent& ) { |
| 112 |
|
| 113 |
if (!(buttonStatus & 2)) { |
| 114 |
stateChangeDistributor.distributeNew<ArkanoidState>( |
| 115 |
time, 0, false, true); |
| 116 |
} |
| 117 |
}, |
| 118 |
[](const EventBase&) { } |
| 119 |
}, event); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
void ArkanoidPad::signalStateChange(const StateChange& event) |
| 124 |
{ |
| 125 |
const auto* as = std::get_if<ArkanoidState>(&event); |
| 126 |
if (!as) return; |
| 127 |
|
| 128 |
dialPos += as->getDelta(); |
| 129 |
if (as->getPress()) buttonStatus &= ~2; |
| 130 |
if (as->getRelease()) buttonStatus |= 2; |
| 131 |
} |
| 132 |
|
| 133 |
void ArkanoidPad::stopReplay(EmuTime time) noexcept |
| 134 |
{ |
| 135 |
|
| 136 |
int delta = POS_CENTER - dialPos; |
| 137 |
bool release = (buttonStatus & 2) == 0; |
| 138 |
if ((delta != 0) || release) { |
| 139 |
stateChangeDistributor.distributeNew<ArkanoidState>( |
| 140 |
time, delta, false, release); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
template<typename Archive> |
| 150 |
void ArkanoidPad::serialize(Archive& ar, unsigned version) |
| 151 |
{ |
| 152 |
ar.serialize("shiftreg", shiftReg, |
| 153 |
"lastValue", lastValue); |
| 154 |
if (ar.versionAtLeast(version, 2)) { |
| 155 |
ar.serialize("dialpos", dialPos, |
| 156 |
"buttonStatus", buttonStatus); |
| 157 |
} |
| 158 |
if constexpr (Archive::IS_LOADER) { |
| 159 |
if (isPluggedIn()) { |
| 160 |
plugHelper(*getConnector(), EmuTime::dummy()); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
INSTANTIATE_SERIALIZE_METHODS(ArkanoidPad); |
| 165 |
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, ArkanoidPad, "ArkanoidPad"); |
| 166 |
|
| 167 |
} |