Angel 3.2
A 2D Game Prototyping Engine
MouseInput.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Input/MouseInput.h"
32 
33 #include "../Infrastructure/Common.h"
34 
35 std::set<MouseListener*> _mouseSubscribers;
36 Vec2i _mouseLocation;
37 
39 {
40  RegisterMouseSubscriber(this);
41 }
42 
44 {
45  DeregisterMouseSubscriber(this);
46 }
47 
48 
49 void MouseListener::MouseDownEvent(Vec2i /*screenCoordinates*/, MouseButtonInput /*button*/) {}
50 void MouseListener::MouseUpEvent(Vec2i /*screenCoordinates*/, MouseButtonInput /*button*/) {}
51 void MouseListener::MouseMotionEvent(Vec2i /*screenCoordinates*/) {}
52 void MouseListener::MouseWheelEvent(const Vector2& /*scrollOffset*/) {}
53 
54 void RegisterMouseSubscriber(MouseListener* actor)
55 {
56  _mouseSubscribers.insert(actor);
57 }
58 
59 void DeregisterMouseSubscriber(MouseListener *actor)
60 {
61  std::set<MouseListener*>::iterator it = _mouseSubscribers.find(actor);
62  if (it != _mouseSubscribers.end())
63  {
64  _mouseSubscribers.erase(it);
65  }
66 }
67 
68 void MouseMotion(GLFWwindow* window, double x, double y)
69 {
70  _mouseLocation.X = (int)x;
71  _mouseLocation.Y = (int)y;
72  std::set<MouseListener*>::iterator it = _mouseSubscribers.begin();
73  while (it != _mouseSubscribers.end())
74  {
75  (*it)->MouseMotionEvent(Vec2i(x, y));
76  it++;
77  }
78 }
79 
80 void MouseButton(GLFWwindow* window, int button, int state, int mods)
81 {
82  MouseButtonInput mouseButton;
83  if (button == GLFW_MOUSE_BUTTON_RIGHT)
84  {
85  mouseButton = MOUSE_RIGHT;
86  }
87  else if (button == GLFW_MOUSE_BUTTON_MIDDLE)
88  {
89  mouseButton = MOUSE_MIDDLE;
90  }
91  else
92  {
93  mouseButton = MOUSE_LEFT;
94  }
95 
96  std::set<MouseListener*>::iterator it = _mouseSubscribers.begin();
97  while (it != _mouseSubscribers.end())
98  {
99  if (state == GLFW_PRESS)
100  {
101  (*it)->MouseDownEvent(_mouseLocation, mouseButton);
102  }
103  else
104  {
105  (*it)->MouseUpEvent(_mouseLocation, mouseButton);
106  }
107  it++;
108  }
109 }
110 
111 void MouseWheel(GLFWwindow* window, double x, double y)
112 {
113  std::set<MouseListener*>::iterator it = _mouseSubscribers.begin();
114  Vector2 offset(x, y);
115  while (it != _mouseSubscribers.end())
116  {
117  (*it)->MouseWheelEvent(offset);
118  it++;
119  }
120 }