| 1 |
#include "Trackball.hh" |
| 2 |
|
| 3 |
#include "Event.hh" |
| 4 |
#include "MSXEventDistributor.hh" |
| 5 |
#include "StateChange.hh" |
| 6 |
#include "StateChangeDistributor.hh" |
| 7 |
|
| 8 |
#include "serialize.hh" |
| 9 |
#include "serialize_meta.hh" |
| 10 |
|
| 11 |
#include "narrow.hh" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace openmsx { |
| 25 |
|
| 26 |
Trackball::Trackball(MSXEventDistributor& eventDistributor_, |
| 27 |
StateChangeDistributor& stateChangeDistributor_) |
| 28 |
: eventDistributor(eventDistributor_) |
| 29 |
, stateChangeDistributor(stateChangeDistributor_) |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
Trackball::~Trackball() |
| 34 |
{ |
| 35 |
if (isPluggedIn()) { |
| 36 |
Trackball::unplugHelper(EmuTime::dummy()); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
zstring_view Trackball::getName() const |
| 43 |
{ |
| 44 |
return "trackball"; |
| 45 |
} |
| 46 |
|
| 47 |
zstring_view Trackball::getDescription() const |
| 48 |
{ |
| 49 |
return "MSX Trackball"; |
| 50 |
} |
| 51 |
|
| 52 |
void Trackball::plugHelper(Connector& , EmuTime time) |
| 53 |
{ |
| 54 |
eventDistributor.registerEventListener(*this); |
| 55 |
stateChangeDistributor.registerListener(*this); |
| 56 |
lastSync = time; |
| 57 |
targetDeltaX = 0; |
| 58 |
targetDeltaY = 0; |
| 59 |
currentDeltaX = 0; |
| 60 |
currentDeltaY = 0; |
| 61 |
} |
| 62 |
|
| 63 |
void Trackball::unplugHelper(EmuTime ) |
| 64 |
{ |
| 65 |
stateChangeDistributor.unregisterListener(*this); |
| 66 |
eventDistributor.unregisterEventListener(*this); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
uint8_t Trackball::read(EmuTime time) |
| 71 |
{ |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
syncCurrentWithTarget(time); |
| 92 |
auto delta = (lastValue & 4) ? currentDeltaY : currentDeltaX; |
| 93 |
return (status & ~0x0F) | ((delta + 8) & 0x0F); |
| 94 |
} |
| 95 |
|
| 96 |
void Trackball::write(uint8_t value, EmuTime time) |
| 97 |
{ |
| 98 |
syncCurrentWithTarget(time); |
| 99 |
uint8_t diff = lastValue ^ value; |
| 100 |
lastValue = value; |
| 101 |
if (diff & 0x4) { |
| 102 |
|
| 103 |
if (value & 4) { |
| 104 |
targetDeltaX = narrow<int8_t>(std::clamp(targetDeltaX - currentDeltaX, -8, 7)); |
| 105 |
currentDeltaX = 0; |
| 106 |
} else { |
| 107 |
targetDeltaY = narrow<int8_t>(std::clamp(targetDeltaY - currentDeltaY, -8, 7)); |
| 108 |
currentDeltaY = 0; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void Trackball::syncCurrentWithTarget(EmuTime time) |
| 114 |
{ |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (!smooth) { |
| 149 |
|
| 150 |
currentDeltaX = targetDeltaX; |
| 151 |
currentDeltaY = targetDeltaY; |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
static constexpr auto INTERVAL = EmuDuration::msec(1); |
| 156 |
|
| 157 |
auto maxSteps = narrow<int>((time - lastSync) / INTERVAL); |
| 158 |
lastSync += INTERVAL * maxSteps; |
| 159 |
|
| 160 |
if (targetDeltaX >= currentDeltaX) { |
| 161 |
currentDeltaX = narrow<int8_t>(std::min<int>(currentDeltaX + maxSteps, targetDeltaX)); |
| 162 |
} else { |
| 163 |
currentDeltaX = narrow<int8_t>(std::max<int>(currentDeltaX - maxSteps, targetDeltaX)); |
| 164 |
} |
| 165 |
if (targetDeltaY >= currentDeltaY) { |
| 166 |
currentDeltaY = narrow<int8_t>(std::min<int>(currentDeltaY + maxSteps, targetDeltaY)); |
| 167 |
} else { |
| 168 |
currentDeltaY = narrow<int8_t>(std::max<int>(currentDeltaY - maxSteps, targetDeltaY)); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
void Trackball::signalMSXEvent(const Event& event, |
| 174 |
EmuTime time) noexcept |
| 175 |
{ |
| 176 |
visit(overloaded{ |
| 177 |
[&](const MouseMotionEvent& e) { |
| 178 |
constexpr int SCALE = 2; |
| 179 |
int dx = e.getX() / SCALE; |
| 180 |
int dy = e.getY() / SCALE; |
| 181 |
if ((dx != 0) || (dy != 0)) { |
| 182 |
createTrackballStateChange(time, dx, dy, 0, 0); |
| 183 |
} |
| 184 |
}, |
| 185 |
[&](const MouseButtonDownEvent& e) { |
| 186 |
switch (e.getButton()) { |
| 187 |
case SDL_BUTTON_LEFT: |
| 188 |
createTrackballStateChange(time, 0, 0, JOY_BUTTONA, 0); |
| 189 |
break; |
| 190 |
case SDL_BUTTON_RIGHT: |
| 191 |
createTrackballStateChange(time, 0, 0, JOY_BUTTONB, 0); |
| 192 |
break; |
| 193 |
default: |
| 194 |
|
| 195 |
break; |
| 196 |
} |
| 197 |
}, |
| 198 |
[&](const MouseButtonUpEvent& e) { |
| 199 |
switch (e.getButton()) { |
| 200 |
case SDL_BUTTON_LEFT: |
| 201 |
createTrackballStateChange(time, 0, 0, 0, JOY_BUTTONA); |
| 202 |
break; |
| 203 |
case SDL_BUTTON_RIGHT: |
| 204 |
createTrackballStateChange(time, 0, 0, 0, JOY_BUTTONB); |
| 205 |
break; |
| 206 |
default: |
| 207 |
|
| 208 |
break; |
| 209 |
} |
| 210 |
}, |
| 211 |
[](const EventBase&) { } |
| 212 |
}, event); |
| 213 |
} |
| 214 |
|
| 215 |
void Trackball::createTrackballStateChange( |
| 216 |
EmuTime time, int deltaX, int deltaY, uint8_t press, uint8_t release) |
| 217 |
{ |
| 218 |
stateChangeDistributor.distributeNew<TrackballState>( |
| 219 |
time, deltaX, deltaY, press, release); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
void Trackball::signalStateChange(const StateChange& event) |
| 224 |
{ |
| 225 |
const auto* ts = std::get_if<TrackballState>(&event); |
| 226 |
if (!ts) return; |
| 227 |
|
| 228 |
targetDeltaX = narrow<int8_t>(std::clamp(targetDeltaX + ts->getDeltaX(), -8, 7)); |
| 229 |
targetDeltaY = narrow<int8_t>(std::clamp(targetDeltaY + ts->getDeltaY(), -8, 7)); |
| 230 |
status = (status & ~ts->getPress()) | ts->getRelease(); |
| 231 |
} |
| 232 |
|
| 233 |
void Trackball::stopReplay(EmuTime time) noexcept |
| 234 |
{ |
| 235 |
syncCurrentWithTarget(time); |
| 236 |
|
| 237 |
uint8_t release = (JOY_BUTTONA | JOY_BUTTONB) & ~status; |
| 238 |
if ((currentDeltaX != 0) || (currentDeltaY != 0) || (release != 0)) { |
| 239 |
stateChangeDistributor.distributeNew<TrackballState>( |
| 240 |
time, -currentDeltaX, -currentDeltaY, uint8_t(0), release); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
template<typename Archive> |
| 247 |
void Trackball::serialize(Archive& ar, unsigned version) |
| 248 |
{ |
| 249 |
if (ar.versionAtLeast(version, 2)) { |
| 250 |
ar.serialize("lastSync", lastSync, |
| 251 |
"targetDeltaX", targetDeltaX, |
| 252 |
"targetDeltaY", targetDeltaY, |
| 253 |
"currentDeltaX", currentDeltaX, |
| 254 |
"currentDeltaY", currentDeltaY); |
| 255 |
} else { |
| 256 |
ar.serialize("deltaX", targetDeltaX, |
| 257 |
"deltaY", targetDeltaY); |
| 258 |
currentDeltaX = targetDeltaX; |
| 259 |
currentDeltaY = targetDeltaY; |
| 260 |
smooth = false; |
| 261 |
} |
| 262 |
ar.serialize("lastValue", lastValue, |
| 263 |
"status", status); |
| 264 |
|
| 265 |
if constexpr (Archive::IS_LOADER) { |
| 266 |
if (isPluggedIn()) { |
| 267 |
plugHelper(*getConnector(), EmuTime::dummy()); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
INSTANTIATE_SERIALIZE_METHODS(Trackball); |
| 272 |
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, Trackball, "Trackball"); |
| 273 |
|
| 274 |
} |