VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Mouse.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "MouseTypes.h"
16#include "JoystickTypes.h"
17#include "SubComponent.h"
18#include "Chrono.h"
19#include "Mouse1350.h"
20#include "Mouse1351.h"
21#include "NeosMouse.h"
22
23namespace vc64 {
24
25class ShakeDetector {
26
27 // Horizontal position
28 double x = 0.0;
29
30 // Moved distance
31 double dxsum = 0.0;
32
33 // Direction (1 or -1)
34 double dxsign = 1.0;
35
36 // Number of turns
37 isize dxturns = 0;
38
39 // Time stamps
40 u64 lastTurn = 0;
41 util::Time lastShake;
42
43public:
44
45 // Feed in new coordinates and checks for a shake
46 bool isShakingAbs(double x);
47 bool isShakingRel(double dx);
48};
49
50class Mouse final : public SubComponent {
51
52 Descriptions descriptions = {
53 {
54 .name = "Mouse1",
55 .description = "Mouse in Port 1"
56 },
57 {
58 .name = "Mouse2",
59 .description = "Mouse in Port 2"
60 }
61 };
62
63 ConfigOptions options = {
64
68 };
69
70 // Reference to the control port this device belongs to
71 ControlPort &port;
72
73 // Current configuration
74 MouseConfig config = { };
75
76
77 //
78 // Sub components
79 //
80
81 // Shake detector
82 class ShakeDetector shakeDetector;
83
84 // A Commdore 1350 (digital) mouse
85 Mouse1350 mouse1350 = Mouse1350(c64);
86
87 // A Commdore 1351 (analog) mouse
88 Mouse1351 mouse1351 = Mouse1351(c64);
89
90 // A Neos (analog) mouse
91 NeosMouse mouseNeos = NeosMouse(c64);
92
93 /* Target mouse position. In order to achieve a smooth mouse movement, a
94 * new mouse coordinate is not written directly into mouseX and mouseY.
95 * Instead, these variables are set. In execute(), mouseX and mouseY are
96 * shifted smoothly towards the target positions.
97 */
98 double targetX = 0.0;
99 double targetY = 0.0;
100
101 // Scaling factors applied to the raw mouse coordinates in setXY()
102 double scaleX = 1.0;
103 double scaleY = 1.0;
104
105
106 //
107 // Methods
108 //
109
110public:
111
112 Mouse(C64 &ref, ControlPort& pref);
113
114 Mouse& operator= (const Mouse& other) {
115
116 CLONE(targetX)
117 CLONE(targetY)
118 CLONE(scaleX)
119 CLONE(scaleY)
120
121 CLONE(mouse1350)
122 CLONE(mouse1351)
123 CLONE(mouseNeos)
124
125 return *this;
126 }
127
128
129 //
130 // Methods from Serializable
131 //
132
133public:
134
135 template <class T>
136 void serialize(T& worker)
137 {
138 worker
139
140 << mouse1350
141 << mouse1351
142 << mouseNeos;
143
144 if (isResetter(worker)) return;
145
146 worker
147
148 << config.model;
149
150 } SERIALIZERS(serialize)
151
152
153 //
154 // Methods from CoreComponent
155 //
156
157public:
158
159 const Descriptions &getDescriptions() const override { return descriptions; }
160
161private:
162
163 void _dump(Category category, std::ostream& os) const override;
164 void _reset(bool hard) override;
165
166
167 //
168 // Configuring
169 //
170
171public:
172
173 const MouseConfig &getConfig() const { return config; }
174 const ConfigOptions &getOptions() const override { return options; }
175 i64 getOption(Option opt) const override;
176 void checkOption(Option opt, i64 value) override;
177 void setOption(Option opt, i64 value) override;
178
179private:
180
181 void updateScalingFactors();
182
183
184 //
185 // Accessing
186 //
187
188public:
189
190 // Runs the shake detector
191 bool detectShakeXY(double x, double y);
192 bool detectShakeDxDy(double dx, double dy);
193
194 // Emulates a mouse movement
195 void setXY(double x, double y);
196 void setDxDy(double dx, double dy);
197
198 // Presses or releases a mouse button
199 void setLeftButton(bool value);
200 void setRightButton(bool value);
201
202 // Triggers a gamepad event
203 void trigger(GamePadAction event);
204
205 // Triggers a state change (Neos mouse only)
206 void risingStrobe();
207 void fallingStrobe();
208
209 // Updates the control port bits (must be called before reading)
210 void updateControlPort();
211
212 // Returns the control port bits as set by the mouse
213 u8 getControlPort() const;
214
215 // Updates the pot bits (must be called before reading)
216 void updatePotX();
217 void updatePotY();
218
219 // Reads the pot bits that show up in the SID registers
220 u8 readPotX() const;
221 u8 readPotY() const;
222
223 // Performs periodic actions for this device
224 void execute();
225};
226
227}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16
@ OPT_MOUSE_SHAKE_DETECT
Detect a shaking mouse.
Definition OptionTypes.h:157
@ OPT_MOUSE_VELOCITY
Mouse velocity.
Definition OptionTypes.h:158
@ OPT_MOUSE_MODEL
Mouse model.
Definition OptionTypes.h:156