Skip to content

Commit

Permalink
Refactor|libgui|Client: Mouse events
Browse files Browse the repository at this point in the history
A MouseEventSource now produces actual MouseEvent instances that
can then be passed on to whoever handles events. Added absolute mouse
position event generation to Canvas.
  • Loading branch information
skyjake committed May 31, 2013
1 parent c2eb94f commit 82f4af7
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 86 deletions.
44 changes: 23 additions & 21 deletions doomsday/client/src/ui/clientwindow.cpp
Expand Up @@ -50,8 +50,7 @@ DENG2_PIMPL(ClientWindow),
DENG2_OBSERVES(KeyEventSource, KeyEvent),
DENG2_OBSERVES(MouseEventSource, MouseStateChange),
#ifndef WIN32
DENG2_OBSERVES(MouseEventSource, MouseAxisEvent),
DENG2_OBSERVES(MouseEventSource, MouseButtonEvent),
DENG2_OBSERVES(MouseEventSource, MouseEvent),
#endif
DENG2_OBSERVES(Canvas, FocusChange)
{
Expand Down Expand Up @@ -145,8 +144,7 @@ DENG2_OBSERVES(Canvas, FocusChange)
self.canvas().audienceForMouseStateChange += this;

#ifndef WIN32 // On Windows, DirectInput bypasses the mouse input from Canvas.
self.canvas().audienceForMouseAxisEvent += this;
self.canvas().audienceForMouseButtonEvent += this;
self.canvas().audienceForMouseEvent += this;
#endif
}

Expand Down Expand Up @@ -214,33 +212,37 @@ DENG2_OBSERVES(Canvas, FocusChange)
}

#ifndef WIN32
void mouseAxisEvent(MouseEventSource::Axis axis, Vector2i const &value)
void mouseEvent(MouseEvent const &event)
{
switch(axis)
switch(event.type())
{
case MouseEventSource::Motion:
Mouse_Qt_SubmitMotion(IMA_POINTER, value.x, value.y);
case Event::MouseButton:
Mouse_Qt_SubmitButton(
event.button() == MouseEvent::Left? IMB_LEFT :
event.button() == MouseEvent::Middle? IMB_MIDDLE :
event.button() == MouseEvent::Right? IMB_RIGHT :
event.button() == MouseEvent::XButton1? IMB_EXTRA1 :
event.button() == MouseEvent::XButton2? IMB_EXTRA2 : IMB_MAXBUTTONS,
event.state() == MouseEvent::Pressed);
break;

case MouseEventSource::Wheel:
Mouse_Qt_SubmitMotion(IMA_WHEEL, value.x, value.y);
case Event::MousePosition:
// Pass onto the window system.
ClientApp::windowSystem().processEvent(event);
break;

case Event::MouseMotion:
Mouse_Qt_SubmitMotion(IMA_POINTER, event.pos().x, event.pos().y);
break;

case Event::MouseWheel:
Mouse_Qt_SubmitMotion(IMA_WHEEL, event.pos().x, event.pos().y);
break;

default:
break;
}
}

void mouseButtonEvent(MouseEventSource::Button button, MouseEventSource::ButtonState state)
{
Mouse_Qt_SubmitButton(
button == MouseEventSource::Left? IMB_LEFT :
button == MouseEventSource::Middle? IMB_MIDDLE :
button == MouseEventSource::Right? IMB_RIGHT :
button == MouseEventSource::XButton1? IMB_EXTRA1 :
button == MouseEventSource::XButton2? IMB_EXTRA2 : IMB_MAXBUTTONS,
state == MouseEventSource::Pressed);
}
#endif // !WIN32

void canvasFocusChanged(Canvas &canvas, bool hasFocus)
Expand Down
11 changes: 8 additions & 3 deletions doomsday/libdeng2/include/de/core/event.h
Expand Up @@ -33,9 +33,14 @@ class DENG2_PUBLIC Event
public:
/// Event type codes.
enum {
KeyPress = 1,
KeyRelease = 2,
KeyRepeat = 3
KeyPress = 1,
KeyRelease = 2,
KeyRepeat = 3,

MouseButton = 4,
MouseMotion = 5,
MousePosition = 6,
MouseWheel = 7
};

public:
Expand Down
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/MouseEvent
@@ -0,0 +1 @@
#include "gui/mouseevent.h"
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/gui/canvas.h
Expand Up @@ -157,6 +157,7 @@ class LIBGUI_PUBLIC Canvas : public QGLWidget, public KeyEventSource, public Mou
void keyReleaseEvent(QKeyEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);
void mouseMoveEvent(QMouseEvent *ev);
void wheelEvent(QWheelEvent *ev);
void showEvent(QShowEvent *ev);

Expand Down
2 changes: 1 addition & 1 deletion doomsday/libgui/include/de/gui/keyevent.h
@@ -1,4 +1,4 @@
/** @file keyevent.h Input event from a keyboard.
/** @file keyevent.h Input event from a keyboard.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
Expand Down
76 changes: 76 additions & 0 deletions doomsday/libgui/include/de/gui/mouseevent.h
@@ -0,0 +1,76 @@
/** @file mouseevent.h Input event from a mouse.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBGUI_MOUSEEVENT_H
#define LIBGUI_MOUSEEVENT_H

#include <de/Event>
#include <de/Vector>

#include "libgui.h"

namespace de {

/**
* Input event from a mouse. @ingroup ui
*/
class LIBGUI_PUBLIC MouseEvent : public Event
{
public:
enum MotionType
{
Absolute = 0,
Relative = 1,
Wheel = 2
};

enum Button
{
Unknown = -1,
Left = 0,
Middle = 1,
Right = 2,
XButton1 = 3,
XButton2 = 4
};

enum ButtonState
{
Released, ///< Released button.
Pressed ///< Pressed button.
};

public:
MouseEvent();
MouseEvent(MotionType motion, Vector2i const &pos);
MouseEvent(Button button, ButtonState state);

MotionType motion() const;
Vector2i const &pos() const { return _axisValue; }
Button button() const { return _button; }
ButtonState state() const { return _state; }

private:
Vector2i _axisValue;
Button _button;
ButtonState _state;
};

} // namespace de

#endif // LIBGUI_MOUSEEVENT_H
28 changes: 3 additions & 25 deletions doomsday/libgui/include/de/gui/mouseeventsource.h
Expand Up @@ -20,6 +20,7 @@
#define LIBGUI_MOUSEEVENTSOURCE_H

#include "libgui.h"
#include "mouseevent.h"
#include <de/Vector>
#include <de/Observers>

Expand All @@ -37,32 +38,9 @@ class LIBGUI_PUBLIC MouseEventSource
Trapped
};

enum Axis
{
Motion, ///< Relative motion of the pointer.
Position, ///< Absolute position of the pointer.
Wheel ///< Wheel motion.
};

enum Button
{
Unknown = -1,
Left = 0,
Middle = 1,
Right = 2,
XButton1 = 3,
XButton2 = 4
};

enum ButtonState
{
Released, ///< Released button.
Pressed ///< Pressed button.
};

DENG2_DEFINE_AUDIENCE(MouseStateChange, void mouseStateChanged(State))
DENG2_DEFINE_AUDIENCE(MouseAxisEvent, void mouseAxisEvent (Axis, Vector2i const &))
DENG2_DEFINE_AUDIENCE(MouseButtonEvent, void mouseButtonEvent (Button, ButtonState))

DENG2_DEFINE_AUDIENCE(MouseEvent, void mouseEvent(MouseEvent const &))

public:
virtual ~MouseEventSource() {}
Expand Down
3 changes: 3 additions & 0 deletions doomsday/libgui/libgui.pro
Expand Up @@ -68,6 +68,7 @@ HEADERS += \
include/de/ImageBank \
include/de/KeyEvent \
include/de/KeyEventSource \
include/de/MouseEvent \
include/de/MouseEventSource \
include/de/PersistentCanvasWindow \
include/de/RowAtlasAllocator \
Expand Down Expand Up @@ -99,6 +100,7 @@ HEADERS += \
include/de/gui/keyevent.h \
include/de/gui/keyeventsource.h \
include/de/gui/libgui.h \
include/de/gui/mouseevent.h \
include/de/gui/mouseeventsource.h \
include/de/gui/opengl.h \
include/de/gui/persistentcanvaswindow.h \
Expand Down Expand Up @@ -129,6 +131,7 @@ SOURCES += \
src/image.cpp \
src/imagebank.cpp \
src/keyevent.cpp \
src/mouseevent.cpp \
src/persistentcanvaswindow.cpp \
src/rowatlasallocator.cpp

Expand Down

0 comments on commit 82f4af7

Please sign in to comment.