2019-09-27 12:52:19 +00:00
|
|
|
#ifndef slic3r_Mouse3DController_hpp_
|
|
|
|
#define slic3r_Mouse3DController_hpp_
|
|
|
|
|
|
|
|
#if ENABLE_3DCONNEXION_DEVICES
|
2019-09-27 13:26:13 +00:00
|
|
|
#include "libslic3r/Point.hpp"
|
2019-09-27 12:52:19 +00:00
|
|
|
|
2019-09-30 13:20:23 +00:00
|
|
|
#include "hidapi.h"
|
2019-09-27 13:26:13 +00:00
|
|
|
|
2019-10-08 06:44:50 +00:00
|
|
|
#include <queue>
|
2019-09-27 12:52:19 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
namespace GUI {
|
|
|
|
|
2019-10-03 09:38:31 +00:00
|
|
|
struct Camera;
|
2019-09-27 12:52:19 +00:00
|
|
|
|
|
|
|
class Mouse3DController
|
|
|
|
{
|
|
|
|
class State
|
|
|
|
{
|
2019-10-02 13:55:26 +00:00
|
|
|
public:
|
2019-09-27 12:52:19 +00:00
|
|
|
static const double DefaultTranslationScale;
|
|
|
|
static const float DefaultRotationScale;
|
|
|
|
|
2019-10-02 13:55:26 +00:00
|
|
|
private:
|
2019-10-08 06:44:50 +00:00
|
|
|
std::queue<Vec3d> m_translation;
|
|
|
|
std::queue<Vec3f> m_rotation;
|
|
|
|
std::queue<unsigned int> m_buttons;
|
2019-09-27 12:52:19 +00:00
|
|
|
|
|
|
|
double m_translation_scale;
|
|
|
|
float m_rotation_scale;
|
|
|
|
|
2019-10-08 06:44:50 +00:00
|
|
|
// When the 3Dconnexion driver is running the system gets, by default, mouse wheel events when rotations around the X axis are detected.
|
|
|
|
// We want to filter these out because we are getting the data directly from the device, bypassing the driver, and those mouse wheel events interfere
|
|
|
|
// by triggering unwanted zoom in/out of the scene
|
|
|
|
// The following variable is used to count the potential mouse wheel events triggered and is updated by:
|
|
|
|
// Mouse3DController::collect_input() through the call to the append_rotation() method
|
|
|
|
// GLCanvas3D::on_mouse_wheel() through the call to the process_mouse_wheel() method
|
|
|
|
// GLCanvas3D::on_idle() through the call to the apply() method
|
|
|
|
unsigned int m_mouse_wheel_counter;
|
|
|
|
|
2019-09-27 12:52:19 +00:00
|
|
|
public:
|
|
|
|
State();
|
|
|
|
|
2019-10-08 06:44:50 +00:00
|
|
|
void append_translation(const Vec3d& translation) { m_translation.push(translation); }
|
|
|
|
void append_rotation(const Vec3f& rotation)
|
|
|
|
{
|
|
|
|
m_rotation.push(rotation);
|
|
|
|
if (rotation(0) != 0.0f)
|
|
|
|
++m_mouse_wheel_counter;
|
|
|
|
}
|
|
|
|
void append_button(unsigned int id) { m_buttons.push(id); }
|
|
|
|
|
|
|
|
bool has_translation() const { return !m_translation.empty(); }
|
|
|
|
bool has_rotation() const { return !m_rotation.empty(); }
|
2019-10-03 10:16:59 +00:00
|
|
|
bool has_any_button() const { return !m_buttons.empty(); }
|
2019-09-27 12:52:19 +00:00
|
|
|
|
2019-10-08 06:44:50 +00:00
|
|
|
bool process_mouse_wheel();
|
|
|
|
|
2019-09-30 13:58:45 +00:00
|
|
|
double get_translation_scale() const { return m_translation_scale; }
|
|
|
|
void set_translation_scale(double scale) { m_translation_scale = scale; }
|
|
|
|
|
|
|
|
float get_rotation_scale() const { return m_rotation_scale; }
|
|
|
|
void set_rotation_scale(float scale) { m_rotation_scale = scale; }
|
|
|
|
|
2019-09-27 12:52:19 +00:00
|
|
|
// return true if any change to the camera took place
|
2019-10-03 09:38:31 +00:00
|
|
|
bool apply(Camera& camera);
|
2019-09-27 12:52:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool m_initialized;
|
2019-10-02 13:55:26 +00:00
|
|
|
mutable State m_state;
|
2019-10-03 10:16:59 +00:00
|
|
|
std::mutex m_mutex;
|
2019-09-27 12:52:19 +00:00
|
|
|
std::thread m_thread;
|
|
|
|
hid_device* m_device;
|
2019-10-03 08:26:28 +00:00
|
|
|
std::string m_device_str;
|
2019-09-27 12:52:19 +00:00
|
|
|
bool m_running;
|
2019-10-02 13:55:26 +00:00
|
|
|
bool m_settings_dialog;
|
2019-09-27 12:52:19 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Mouse3DController();
|
|
|
|
|
|
|
|
void init();
|
|
|
|
void shutdown();
|
|
|
|
|
|
|
|
bool is_device_connected() const { return m_device != nullptr; }
|
|
|
|
bool is_running() const { return m_running; }
|
|
|
|
|
2019-10-08 06:44:50 +00:00
|
|
|
bool process_mouse_wheel() { std::lock_guard<std::mutex> lock(m_mutex); return m_state.process_mouse_wheel(); }
|
2019-09-27 12:52:19 +00:00
|
|
|
|
2019-10-03 10:16:59 +00:00
|
|
|
bool apply(Camera& camera);
|
2019-09-27 12:52:19 +00:00
|
|
|
|
2019-10-02 13:55:26 +00:00
|
|
|
bool is_settings_dialog_shown() const { return m_settings_dialog; }
|
2019-10-04 08:59:27 +00:00
|
|
|
void show_settings_dialog(bool show) { m_settings_dialog = show && is_running(); }
|
2019-10-03 09:38:31 +00:00
|
|
|
void render_settings_dialog(unsigned int canvas_width, unsigned int canvas_height) const;
|
2019-10-02 13:55:26 +00:00
|
|
|
|
2019-09-27 12:52:19 +00:00
|
|
|
private:
|
2019-10-04 08:59:27 +00:00
|
|
|
bool connect_device();
|
2019-09-27 12:52:19 +00:00
|
|
|
void disconnect_device();
|
|
|
|
void start();
|
|
|
|
void stop() { m_running = false; }
|
|
|
|
|
2019-10-04 08:59:27 +00:00
|
|
|
// secondary thread methods
|
2019-09-27 12:52:19 +00:00
|
|
|
void run();
|
|
|
|
void collect_input();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace GUI
|
|
|
|
} // namespace Slic3r
|
|
|
|
|
|
|
|
#endif // ENABLE_3DCONNEXION_DEVICES
|
|
|
|
|
|
|
|
#endif // slic3r_Mouse3DController_hpp_
|
|
|
|
|