refactored Keyboard and Mouse Events

This commit is contained in:
Anatoly Baksheev 2013-12-08 21:00:00 +04:00
parent cad2f98007
commit 2705068290
7 changed files with 99 additions and 174 deletions

View File

@ -506,40 +506,28 @@ This class represents a keyboard event. ::
class CV_EXPORTS KeyboardEvent
{
public:
static const unsigned int Alt = 1;
static const unsigned int Ctrl = 2;
static const unsigned int Shift = 4;
enum { ALT = 1, CTRL = 2, SHIFT = 4 };
enum Action { KEY_UP = 0, KEY_DOWN = 1 };
//! Create a keyboard event
//! - Note that action is true if key is pressed, false if released
KeyboardEvent (bool action, const std::string& key_sym, unsigned char key, bool alt, bool ctrl, bool shift);
KeyboardEvent(Action action, const String& symbol, unsigned char code, int modifiers);
bool isAltPressed () const;
bool isCtrlPressed () const;
bool isShiftPressed () const;
unsigned char getKeyCode () const;
const String& getKeySym () const;
bool keyDown () const;
bool keyUp () const;
protected:
/* hidden */
Action action;
String symbol;
unsigned char code;
int modifiers;
};
viz::KeyboardEvent::KeyboardEvent
---------------------------------
Constructs a KeyboardEvent.
.. ocv:function:: KeyboardEvent (bool action, const std::string& key_sym, unsigned char key, bool alt, bool ctrl, bool shift)
.. ocv:function:: KeyboardEvent (Action action, const String& symbol, unsigned char code, Modifiers modifiers)
:param action: If true, key is pressed. If false, key is released.
:param key_sym: Name of the key.
:param key: Code of the key.
:param alt: If true, ``alt`` is pressed.
:param ctrl: If true, ``ctrl`` is pressed.
:param shift: If true, ``shift`` is pressed.
:param action: Signals if key is pressed or released.
:param symbol: Name of the key.
:param code: Code of the key.
:param modifiers: Signals if ``alt``, ``ctrl`` or ``shift`` are pressed or their combination.
viz::MouseEvent
---------------
@ -553,26 +541,24 @@ This class represents a mouse event. ::
enum Type { MouseMove = 1, MouseButtonPress, MouseButtonRelease, MouseScrollDown, MouseScrollUp, MouseDblClick } ;
enum MouseButton { NoButton = 0, LeftButton, MiddleButton, RightButton, VScroll } ;
MouseEvent (const Type& type, const MouseButton& button, const Point& p, bool alt, bool ctrl, bool shift);
MouseEvent(const Type& type, const MouseButton& button, const Point& pointer, int modifiers);
Type type;
MouseButton button;
Point pointer;
unsigned int key_state;
int modifiers;
};
viz::MouseEvent::MouseEvent
---------------------------
Constructs a MouseEvent.
.. ocv:function:: MouseEvent (const Type& type, const MouseButton& button, const Point& p, bool alt, bool ctrl, bool shift)
.. ocv:function:: MouseEvent (const Type& type, const MouseButton& button, const Point& p, Modifiers modifiers)
:param type: Type of the event. This can be **MouseMove**, **MouseButtonPress**, **MouseButtonRelease**, **MouseScrollDown**, **MouseScrollUp**, **MouseDblClick**.
:param button: Mouse button. This can be **NoButton**, **LeftButton**, **MiddleButton**, **RightButton**, **VScroll**.
:param p: Position of the event.
:param alt: If true, ``alt`` is pressed.
:param ctrl: If true, ``ctrl`` is pressed.
:param shift: If true, ``shift`` is pressed.
:param modifiers: Signals if ``alt``, ``ctrl`` or ``shift`` are pressed or their combination.
viz::Camera
-----------

View File

@ -41,9 +41,6 @@
// * Ozan Tonkal, ozantonkal@gmail.com
// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com
//
// OpenCV Viz module is complete rewrite of
// PCL visualization module (www.pointclouds.org)
//
//M*/
#ifndef __OPENCV_VIZ_TYPES_HPP__
@ -121,56 +118,13 @@ namespace cv
struct loadMeshImpl;
};
class CV_EXPORTS KeyboardEvent
{
public:
static const unsigned int Alt = 1;
static const unsigned int Ctrl = 2;
static const unsigned int Shift = 4;
//! Create a keyboard event
//! - Note that action is true if key is pressed, false if released
KeyboardEvent(bool action, const String& key_sym, unsigned char key, bool alt, bool ctrl, bool shift);
bool isAltPressed() const;
bool isCtrlPressed() const;
bool isShiftPressed() const;
unsigned char getKeyCode() const;
const String& getKeySym() const;
bool keyDown() const;
bool keyUp() const;
protected:
bool action_;
unsigned int modifiers_;
unsigned char key_code_;
String key_sym_;
};
class CV_EXPORTS MouseEvent
{
public:
enum Type { MouseMove = 1, MouseButtonPress, MouseButtonRelease, MouseScrollDown, MouseScrollUp, MouseDblClick } ;
enum MouseButton { NoButton = 0, LeftButton, MiddleButton, RightButton, VScroll } ;
MouseEvent(const Type& type, const MouseButton& button, const Point& p, bool alt, bool ctrl, bool shift);
Type type;
MouseButton button;
Point pointer;
unsigned int key_state;
};
class CV_EXPORTS Camera
{
public:
Camera(float f_x, float f_y, float c_x, float c_y, const Size &window_size);
Camera(const Vec2f &fov, const Size &window_size);
Camera(const cv::Matx33f &K, const Size &window_size);
Camera(const cv::Matx44f &proj, const Size &window_size);
Camera(float fx, float fy, float cx, float cy, const Size &window_size);
explicit Camera(const Vec2f &fov, const Size &window_size);
explicit Camera(const cv::Matx33f &K, const Size &window_size);
explicit Camera(const cv::Matx44f &proj, const Size &window_size);
inline const Vec2d & getClip() const { return clip_; }
inline void setClip(const Vec2d &clip) { clip_ = clip; }
@ -189,7 +143,7 @@ namespace cv
static Camera KinectCamera(const Size &window_size);
private:
void init(float f_x, float f_y, float c_x, float c_y, const Size &window_size);
void init(float fx, float fy, float cx, float cy, const Size &window_size);
Vec2d clip_;
Vec2f fov_;
@ -197,6 +151,34 @@ namespace cv
Vec2f principal_point_;
Vec2f focal_;
};
class CV_EXPORTS KeyboardEvent
{
public:
enum { NONE = 0, ALT = 1, CTRL = 2, SHIFT = 4 };
enum Action { KEY_UP = 0, KEY_DOWN = 1 };
KeyboardEvent(Action action, const String& symbol, unsigned char code, int modifiers);
Action action;
String symbol;
unsigned char code;
int modifiers;
};
class CV_EXPORTS MouseEvent
{
public:
enum Type { MouseMove = 1, MouseButtonPress, MouseButtonRelease, MouseScrollDown, MouseScrollUp, MouseDblClick } ;
enum MouseButton { NoButton = 0, LeftButton, MiddleButton, RightButton, VScroll } ;
MouseEvent(const Type& type, const MouseButton& button, const Point& pointer, int modifiers);
Type type;
MouseButton button;
Point pointer;
int modifiers;
};
} /* namespace viz */
} /* namespace cv */

View File

@ -185,7 +185,7 @@ cv::viz::WCloud::WCloud(InputArray _cloud, InputArray _colors)
mapper->SetInputData(polydata);
#endif
Vec3d minmax(scalars->GetRange());
Vec2d minmax(scalars->GetRange());
mapper->SetScalarRange(minmax.val);
mapper->SetScalarModeToUsePointData();

View File

@ -51,7 +51,6 @@
//////////////////////////////////////////////////////////////////////////////////////////////
void cv::viz::InteractorStyle::Initialize()
{
modifier_ = cv::viz::InteractorStyle::KB_MOD_ALT;
// Set windows size (width, height) to unknown (-1)
win_size_ = Vec2i(-1, -1);
win_pos_ = Vec2i(0, 0);
@ -121,13 +120,7 @@ void cv::viz::InteractorStyle::OnChar()
else if (key.find("XF86ZoomOut") != String::npos)
zoomOut();
int keymod = false;
switch (modifier_)
{
case KB_MOD_ALT: keymod = Interactor->GetAltKey(); break;
case KB_MOD_CTRL: keymod = Interactor->GetControlKey(); break;
case KB_MOD_SHIFT: keymod = Interactor->GetShiftKey(); break;
}
int keymod = Interactor->GetAltKey();
switch (Interactor->GetKeyCode())
{
@ -184,6 +177,21 @@ bool cv::viz::InteractorStyle::getAltKey() { return Interactor->GetAltKey() != 0
bool cv::viz::InteractorStyle::getShiftKey() { return Interactor->GetShiftKey()!= 0; }
bool cv::viz::InteractorStyle::getControlKey() { return Interactor->GetControlKey()!= 0; }
int cv::viz::InteractorStyle::getModifiers()
{
int modifiers = KeyboardEvent::NONE;
if (Interactor->GetAltKey())
modifiers |= KeyboardEvent::ALT;
if (Interactor->GetControlKey())
modifiers |= KeyboardEvent::CTRL;
if (Interactor->GetShiftKey())
modifiers |= KeyboardEvent::SHIFT;
return modifiers;
}
//////////////////////////////////////////////////////////////////////////////////////////////
void
cv::viz::InteractorStyle::OnKeyDown()
@ -204,19 +212,7 @@ cv::viz::InteractorStyle::OnKeyDown()
if (win_size_[0] == -1 || win_size_[1] == -1)
win_size_ = Vec2i(Interactor->GetRenderWindow()->GetSize());
// Get the status of special keys (Cltr+Alt+Shift)
bool shift = getShiftKey();
bool ctrl = getControlKey();
bool alt = getAltKey();
bool keymod = false;
switch (modifier_)
{
case KB_MOD_ALT: keymod = alt; break;
case KB_MOD_CTRL: keymod = ctrl; break;
case KB_MOD_SHIFT: keymod = shift; break;
}
bool alt = getAltKey();
std::string key(Interactor->GetKeySym());
if (key.find("XF86ZoomIn") != std::string::npos)
@ -344,7 +340,7 @@ cv::viz::InteractorStyle::OnKeyDown()
// Switch between maximize and original window size
case 'f': case 'F':
{
if (keymod)
if (alt)
{
Vec2i screen_size(Interactor->GetRenderWindow()->GetScreenSize());
Vec2i win_size(Interactor->GetRenderWindow()->GetSize());
@ -386,7 +382,7 @@ cv::viz::InteractorStyle::OnKeyDown()
// 's'/'S' w/out ALT
case 's': case 'S':
{
if (keymod)
if (alt)
{
int stereo_render = Interactor->GetRenderWindow()->GetStereoRender();
if (!stereo_render)
@ -423,7 +419,7 @@ cv::viz::InteractorStyle::OnKeyDown()
// Overwrite the camera reset
case 'r': case 'R':
{
if (!keymod)
if (!alt)
{
Superclass::OnKeyDown();
break;
@ -491,7 +487,8 @@ cv::viz::InteractorStyle::OnKeyDown()
}
}
KeyboardEvent event(true, Interactor->GetKeySym(), Interactor->GetKeyCode(), getAltKey(), getControlKey(), getShiftKey());
KeyboardEvent event(KeyboardEvent::KEY_DOWN, Interactor->GetKeySym(), Interactor->GetKeyCode(), getModifiers());
// Check if there is a keyboard callback registered
if (keyboardCallback_)
keyboardCallback_(event, keyboard_callback_cookie_);
@ -503,7 +500,7 @@ cv::viz::InteractorStyle::OnKeyDown()
//////////////////////////////////////////////////////////////////////////////////////////////
void cv::viz::InteractorStyle::OnKeyUp()
{
KeyboardEvent event(false, Interactor->GetKeySym(), Interactor->GetKeyCode(), getAltKey(), getControlKey(), getShiftKey());
KeyboardEvent event(KeyboardEvent::KEY_UP, Interactor->GetKeySym(), Interactor->GetKeyCode(), getModifiers());
// Check if there is a keyboard callback registered
if (keyboardCallback_)
keyboardCallback_(event, keyboard_callback_cookie_);
@ -515,7 +512,7 @@ void cv::viz::InteractorStyle::OnKeyUp()
void cv::viz::InteractorStyle::OnMouseMove()
{
Vec2i p(Interactor->GetEventPosition());
MouseEvent event(MouseEvent::MouseMove, MouseEvent::NoButton, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(MouseEvent::MouseMove, MouseEvent::NoButton, p, getModifiers());
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
Superclass::OnMouseMove();
@ -526,7 +523,7 @@ void cv::viz::InteractorStyle::OnLeftButtonDown()
{
Vec2i p(Interactor->GetEventPosition());
MouseEvent::Type type = (Interactor->GetRepeatCount() == 0) ? MouseEvent::MouseButtonPress : MouseEvent::MouseDblClick;
MouseEvent event(type, MouseEvent::LeftButton, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(type, MouseEvent::LeftButton, p, getModifiers());
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
Superclass::OnLeftButtonDown();
@ -536,7 +533,7 @@ void cv::viz::InteractorStyle::OnLeftButtonDown()
void cv::viz::InteractorStyle::OnLeftButtonUp()
{
Vec2i p(Interactor->GetEventPosition());
MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::LeftButton, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::LeftButton, p, getModifiers());
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
Superclass::OnLeftButtonUp();
@ -548,7 +545,7 @@ void cv::viz::InteractorStyle::OnMiddleButtonDown()
Vec2i p(Interactor->GetEventPosition());
MouseEvent::Type type = (Interactor->GetRepeatCount() == 0) ? MouseEvent::MouseButtonPress : MouseEvent::MouseDblClick;
MouseEvent event(type, MouseEvent::MiddleButton, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(type, MouseEvent::MiddleButton, p, getModifiers());
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
Superclass::OnMiddleButtonDown();
@ -558,7 +555,7 @@ void cv::viz::InteractorStyle::OnMiddleButtonDown()
void cv::viz::InteractorStyle::OnMiddleButtonUp()
{
Vec2i p(Interactor->GetEventPosition());
MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::MiddleButton, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::MiddleButton, p, getModifiers());
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
Superclass::OnMiddleButtonUp();
@ -570,7 +567,7 @@ void cv::viz::InteractorStyle::OnRightButtonDown()
Vec2i p(Interactor->GetEventPosition());
MouseEvent::Type type = (Interactor->GetRepeatCount() == 0) ? MouseEvent::MouseButtonPress : MouseEvent::MouseDblClick;
MouseEvent event(type, MouseEvent::RightButton, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(type, MouseEvent::RightButton, p, getModifiers());
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
Superclass::OnRightButtonDown();
@ -580,7 +577,7 @@ void cv::viz::InteractorStyle::OnRightButtonDown()
void cv::viz::InteractorStyle::OnRightButtonUp()
{
Vec2i p(Interactor->GetEventPosition());
MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::RightButton, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(MouseEvent::MouseButtonRelease, MouseEvent::RightButton, p, getModifiers());
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
Superclass::OnRightButtonUp();
@ -590,7 +587,7 @@ void cv::viz::InteractorStyle::OnRightButtonUp()
void cv::viz::InteractorStyle::OnMouseWheelForward()
{
Vec2i p(Interactor->GetEventPosition());
MouseEvent event(MouseEvent::MouseScrollUp, MouseEvent::VScroll, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(MouseEvent::MouseScrollUp, MouseEvent::VScroll, p, getModifiers());
// If a mouse callback registered, call it!
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);
@ -622,7 +619,7 @@ void cv::viz::InteractorStyle::OnMouseWheelForward()
void cv::viz::InteractorStyle::OnMouseWheelBackward()
{
Vec2i p(Interactor->GetEventPosition());
MouseEvent event(MouseEvent::MouseScrollDown, MouseEvent::VScroll, p, getAltKey(), getControlKey(), getShiftKey());
MouseEvent event(MouseEvent::MouseScrollDown, MouseEvent::VScroll, p, getModifiers());
// If a mouse callback registered, call it!
if (mouseCallback_)
mouseCallback_(event, mouse_callback_cookie_);

View File

@ -56,9 +56,6 @@ namespace cv
class InteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
enum KeyboardModifier { KB_MOD_ALT, KB_MOD_CTRL, KB_MOD_SHIFT };
static InteractorStyle *New();
virtual ~InteractorStyle() {}
@ -74,9 +71,6 @@ namespace cv
void registerKeyboardCallback(void (*callback)(const KeyboardEvent&, void*), void * cookie = 0);
void saveScreenshot(const String &file);
/** \brief Change the default keyboard modified from ALT to a different special key.*/
inline void setKeyboardModifier(const KeyboardModifier &modifier) { modifier_ = modifier; }
private:
/** \brief Set to true after initialization is complete. */
bool init_;
@ -121,8 +115,6 @@ namespace cv
/** \brief True if we're using red-blue colors for anaglyphic stereo, false if magenta-green. */
bool stereo_anaglyph_mask_default_;
KeyboardModifier modifier_;
void (*keyboardCallback_)(const KeyboardEvent&, void*);
void *keyboard_callback_cookie_;
@ -132,6 +124,7 @@ namespace cv
bool getAltKey();
bool getControlKey();
bool getShiftKey();
int getModifiers();
};
}
}

View File

@ -49,44 +49,13 @@
#include "precomp.hpp"
////////////////////////////////////////////////////////////////////
/// cv::viz::KeyboardEvent
/// Events
cv::viz::KeyboardEvent::KeyboardEvent(bool _action, const String& _key_sym, unsigned char key, bool alt, bool ctrl, bool shift)
: action_(_action), modifiers_(0), key_code_(key), key_sym_(_key_sym)
{
if (alt)
modifiers_ = Alt;
cv::viz::KeyboardEvent::KeyboardEvent(Action _action, const String& _symbol, unsigned char _code, int _modifiers)
: action(_action), symbol(_symbol), code(_code), modifiers(_modifiers) {}
if (ctrl)
modifiers_ |= Ctrl;
if (shift)
modifiers_ |= Shift;
}
bool cv::viz::KeyboardEvent::isAltPressed() const { return (modifiers_ & Alt) != 0; }
bool cv::viz::KeyboardEvent::isCtrlPressed() const { return (modifiers_ & Ctrl) != 0; }
bool cv::viz::KeyboardEvent::isShiftPressed() const { return (modifiers_ & Shift) != 0; }
unsigned char cv::viz::KeyboardEvent::getKeyCode() const { return key_code_; }
const cv::String& cv::viz::KeyboardEvent::getKeySym() const { return key_sym_; }
bool cv::viz::KeyboardEvent::keyDown() const { return action_; }
bool cv::viz::KeyboardEvent::keyUp() const { return !action_; }
////////////////////////////////////////////////////////////////////
/// cv::viz::MouseEvent
cv::viz::MouseEvent::MouseEvent(const Type& _type, const MouseButton& _button, const Point& _p, bool alt, bool ctrl, bool shift)
: type(_type), button(_button), pointer(_p), key_state(0)
{
if (alt)
key_state = KeyboardEvent::Alt;
if (ctrl)
key_state |= KeyboardEvent::Ctrl;
if (shift)
key_state |= KeyboardEvent::Shift;
}
cv::viz::MouseEvent::MouseEvent(const Type& _type, const MouseButton& _button, const Point& _pointer, int _modifiers)
: type(_type), button(_button), pointer(_pointer), modifiers(_modifiers) {}
////////////////////////////////////////////////////////////////////
/// cv::viz::Mesh3d
@ -167,9 +136,9 @@ cv::viz::Mesh3d cv::viz::Mesh3d::loadMesh(const String& file)
////////////////////////////////////////////////////////////////////
/// Camera implementation
cv::viz::Camera::Camera(float f_x, float f_y, float c_x, float c_y, const Size &window_size)
cv::viz::Camera::Camera(float fx, float fy, float cx, float cy, const Size &window_size)
{
init(f_x, f_y, c_x, c_y, window_size);
init(fx, fy, cx, cy, window_size);
}
cv::viz::Camera::Camera(const Vec2f &fov, const Size &window_size)
@ -220,19 +189,19 @@ cv::viz::Camera::Camera(const Matx44f &proj, const Size &window_size)
window_size_ = window_size;
}
void cv::viz::Camera::init(float f_x, float f_y, float c_x, float c_y, const Size &window_size)
void cv::viz::Camera::init(float fx, float fy, float cx, float cy, const Size &window_size)
{
CV_Assert(window_size.width > 0 && window_size.height > 0);
setClip(Vec2d(0.01, 1000.01));// Default clipping
fov_[0] = (atan2(c_x,f_x) + atan2(window_size.width-c_x,f_x));
fov_[1] = (atan2(c_y,f_y) + atan2(window_size.height-c_y,f_y));
fov_[0] = (atan2(cx,fx) + atan2(window_size.width-cx,fx));
fov_[1] = (atan2(cy,fy) + atan2(window_size.height-cy,fy));
principal_point_[0] = c_x;
principal_point_[1] = c_y;
principal_point_[0] = cx;
principal_point_[1] = cy;
focal_[0] = f_x;
focal_[1] = f_y;
focal_[0] = fx;
focal_[1] = fy;
window_size_ = window_size;
}

View File

@ -247,9 +247,7 @@ void cv::viz::Viz3d::VizImpl::setDesiredUpdateRate(double rate)
/////////////////////////////////////////////////////////////////////////////////////////////
double cv::viz::Viz3d::VizImpl::getDesiredUpdateRate()
{
if (interactor_)
return interactor_->GetDesiredUpdateRate();
return 0.0;
return interactor_ ? interactor_->GetDesiredUpdateRate() : 0.0;
}
/////////////////////////////////////////////////////////////////////////////////////////////