summaryrefslogtreecommitdiffhomepage
path: root/nGenEx/MouseController.cpp
diff options
context:
space:
mode:
authorFWoltermann@gmail.com <FWoltermann@gmail.com@076cb2c4-205e-83fd-5cf3-1be9aa105544>2011-12-08 14:53:40 +0000
committerFWoltermann@gmail.com <FWoltermann@gmail.com@076cb2c4-205e-83fd-5cf3-1be9aa105544>2011-12-08 14:53:40 +0000
commite33e19d0587146859d48a134ec9fd94e7b7ba5cd (patch)
tree69d048c8801858d2756ab3a487090a7a1b74bf14 /nGenEx/MouseController.cpp
downloadstarshatter-e33e19d0587146859d48a134ec9fd94e7b7ba5cd.zip
starshatter-e33e19d0587146859d48a134ec9fd94e7b7ba5cd.tar.gz
starshatter-e33e19d0587146859d48a134ec9fd94e7b7ba5cd.tar.bz2
Initial upload
Diffstat (limited to 'nGenEx/MouseController.cpp')
-rw-r--r--nGenEx/MouseController.cpp247
1 files changed, 247 insertions, 0 deletions
diff --git a/nGenEx/MouseController.cpp b/nGenEx/MouseController.cpp
new file mode 100644
index 0000000..ecdf5ad
--- /dev/null
+++ b/nGenEx/MouseController.cpp
@@ -0,0 +1,247 @@
+/* Project nGenEx
+ Destroyer Studios LLC
+ Copyright © 1997-2004. All Rights Reserved.
+
+ SUBSYSTEM: nGenEx.lib
+ FILE: MouseController.cpp
+ AUTHOR: John DiCamillo
+
+
+ OVERVIEW
+ ========
+ MouseController Input class
+*/
+
+#include "MemDebug.h"
+#include "MouseController.h"
+#include "Keyboard.h"
+#include "Mouse.h"
+#include "Game.h"
+#include "Video.h"
+
+// +--------------------------------------------------------------------+
+
+static MouseController* instance = 0;
+static DWORD rbutton_latch = 0;
+static DWORD mbutton_latch = 0;
+static DWORD active_latch = 0;
+
+// +--------------------------------------------------------------------+
+
+MouseController::MouseController()
+ : p(0), r(0), w(0), dx(0), dy(0), t(0)
+{
+ instance = this;
+ select = 0;
+ sensitivity = 10;
+ swapped = 0;
+ active = false;
+
+ active_key = 20; // caps lock
+
+ rbutton_latch = 0;
+
+ for (int i = 0; i < MotionController::MaxActions; i++)
+ action[i] = 0;
+}
+
+MouseController::~MouseController()
+{
+ instance = 0;
+}
+
+MouseController*
+MouseController::GetInstance()
+{
+ return instance;
+}
+
+// +--------------------------------------------------------------------+
+
+void
+MouseController::MapKeys(KeyMapEntry* mapping, int nkeys)
+{
+ for (int i = 0; i < nkeys; i++) {
+ KeyMapEntry k = mapping[i];
+
+ if (k.act >= KEY_MAP_FIRST && k.act <= KEY_MAP_LAST) {
+
+ if (k.act == KEY_MOUSE_SENSE)
+ sensitivity = k.key;
+
+ else if (k.act == KEY_MOUSE_SWAP)
+ swapped = k.key;
+
+ else if (k.act == KEY_MOUSE_INVERT)
+ inverted = k.key;
+
+ else if (k.act == KEY_MOUSE_SELECT)
+ select = k.key;
+
+ else if (k.act == KEY_MOUSE_ACTIVE)
+ active_key = k.key;
+ }
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+static inline double sqr(double a) { return a*a; }
+
+void
+MouseController::Acquire()
+{
+ p = r = w = 0;
+ action[0] = 0;
+ action[1] = 0;
+ action[2] = 0;
+ action[3] = 0;
+
+ if (active_key && Keyboard::KeyDown(active_key)) {
+ active_latch = 1;
+ }
+ else {
+ if (active_latch) {
+ active_latch = 0;
+ active = !active;
+ }
+ }
+
+ if (!select || !active)
+ return;
+
+ action[0] = Mouse::LButton();
+
+ int roll_enable = 0;
+
+ if (Mouse::RButton()) {
+ roll_enable = 1;
+
+ if (!rbutton_latch)
+ rbutton_latch = Game::RealTime();
+ }
+ else {
+ if (rbutton_latch) {
+ rbutton_latch = Game::RealTime() - rbutton_latch;
+ if (rbutton_latch < 250)
+ action[1] = 1;
+ }
+
+ rbutton_latch = 0;
+ }
+
+ if (Mouse::MButton()) {
+ if (!mbutton_latch)
+ mbutton_latch = Game::RealTime();
+ }
+ else {
+ if (mbutton_latch) {
+ action[3] = 1;
+ }
+
+ mbutton_latch = 0;
+ }
+
+ double step = 0;
+ int cx = Video::GetInstance()->Width()/2;
+ int cy = Video::GetInstance()->Height()/2;
+
+ dx += Mouse::X() - cx;
+ dy += Mouse::Y() - cy;
+
+ step = fabs(dx)/cx;
+
+ if (roll_enable || select == 1)
+ step *= 3 * sensitivity;
+ else
+ step *= step * sensitivity/4;
+
+ if (roll_enable) {
+ if (dx > 0)
+ r = -step;
+ else if (dx < 0)
+ r = step;
+ }
+ else {
+ if (dx > 0)
+ w = step;
+ else if (dx < 0)
+ w = -step;
+ }
+
+ step = fabs(dy)/cy;
+
+ if (select == 1)
+ step *= 2 * sensitivity;
+ else
+ step *= step * sensitivity/4;
+
+ if (inverted) {
+ step *= -1;
+ }
+
+ if (dy > 0)
+ p = step;
+ else if (dy < 0)
+ p = -step;
+
+ if (select == 1) {
+ ::SetCursorPos(cx, cy);
+
+ double drain = cx * 4 * Game::FrameTime();
+
+ if (dx > drain) {
+ dx -= drain;
+ }
+ else if (dx < -drain) {
+ dx += drain;
+ }
+ else {
+ dx = 0;
+ }
+
+ if (dy > drain) {
+ dy -= drain;
+ }
+ else if (dy < -drain) {
+ dy += drain;
+ }
+ else {
+ dy = 0;
+ }
+ }
+ else {
+ dx = 0;
+ dy = 0;
+ }
+
+ if (Mouse::Wheel() > 0) {
+ if (t < 0.25)
+ t += 0.025;
+ else
+ t += 0.1;
+
+ if (t > 1) t = 1;
+ }
+
+ else if (Mouse::Wheel() < 0) {
+ if (t < 0.25)
+ t -= 0.025;
+ else
+ t -= 0.1;
+
+ if (t < 0) t = 0;
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+int
+MouseController::ActionMap(int n)
+{
+ if (n >= KEY_ACTION_0 && n <= KEY_ACTION_3)
+ return action[n - KEY_ACTION_0];
+
+ return 0;
+}
+