mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-31 04:31:15 -06:00 
			
		
		
		
	ENABLE_3DCONNEXION_DEVICES -> Refactored Mouse3DController to simplify code
This commit is contained in:
		
							parent
							
								
									b15757a126
								
							
						
					
					
						commit
						f315681804
					
				
					 2 changed files with 27 additions and 89 deletions
				
			
		|  | @ -49,72 +49,6 @@ Mouse3DController::State::State() | |||
| { | ||||
| } | ||||
| 
 | ||||
| void Mouse3DController::State::set_translation(const Vec3d& translation) | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     m_translation = translation; | ||||
| } | ||||
| 
 | ||||
| void Mouse3DController::State::set_rotation(const Vec3f& rotation) | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     m_rotation = rotation; | ||||
| } | ||||
| 
 | ||||
| void Mouse3DController::State::set_button(unsigned int id) | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     m_buttons.push_back(id); | ||||
| } | ||||
| 
 | ||||
| void Mouse3DController::State::reset_buttons() | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return m_buttons.clear(); | ||||
| } | ||||
| 
 | ||||
| const Vec3d& Mouse3DController::State::get_translation() const | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return m_translation; | ||||
| } | ||||
| 
 | ||||
| const Vec3f& Mouse3DController::State::get_rotation() const | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return m_rotation; | ||||
| } | ||||
| 
 | ||||
| const std::vector<unsigned int>& Mouse3DController::State::get_buttons() const | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return m_buttons; | ||||
| } | ||||
| 
 | ||||
| bool Mouse3DController::State::has_translation() const | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return !m_translation.isApprox(Vec3d::Zero()); | ||||
| } | ||||
| 
 | ||||
| bool Mouse3DController::State::has_rotation() const | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return !m_rotation.isApprox(Vec3f::Zero()); | ||||
| } | ||||
| 
 | ||||
| bool Mouse3DController::State::has_translation_or_rotation() const | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return !m_translation.isApprox(Vec3d::Zero()) && !m_rotation.isApprox(Vec3f::Zero()); | ||||
| } | ||||
| 
 | ||||
| bool Mouse3DController::State::has_any_button() const | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return !m_buttons.empty(); | ||||
| } | ||||
| 
 | ||||
| bool Mouse3DController::State::apply(Camera& camera) | ||||
| { | ||||
|     if (!wxGetApp().IsActive()) | ||||
|  | @ -124,28 +58,25 @@ bool Mouse3DController::State::apply(Camera& camera) | |||
| 
 | ||||
|     if (has_translation()) | ||||
|     { | ||||
|         Vec3d translation = get_translation(); | ||||
|         camera.set_target(camera.get_target() + m_translation_scale * (translation(0) * camera.get_dir_right() + translation(1) * camera.get_dir_forward() + translation(2) * camera.get_dir_up())); | ||||
|         set_translation(Vec3d::Zero()); | ||||
|         camera.set_target(camera.get_target() + m_translation_scale * (m_translation(0) * camera.get_dir_right() + m_translation(1) * camera.get_dir_forward() + m_translation(2) * camera.get_dir_up())); | ||||
|         m_translation = Vec3d::Zero(); | ||||
|         ret = true; | ||||
|     } | ||||
| 
 | ||||
|     if (has_rotation()) | ||||
|     { | ||||
|         Vec3f rotation = get_rotation(); | ||||
|         float theta = m_rotation_scale * rotation(0); | ||||
|         float phi = m_rotation_scale * rotation(2); | ||||
|         float theta = m_rotation_scale * m_rotation(0); | ||||
|         float phi = m_rotation_scale * m_rotation(2); | ||||
|         float sign = camera.inverted_phi ? -1.0f : 1.0f; | ||||
|         camera.phi += sign * phi; | ||||
|         camera.set_theta(camera.get_theta() + theta, wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() != ptSLA); | ||||
|         set_rotation(Vec3f::Zero()); | ||||
|         m_rotation = Vec3f::Zero(); | ||||
|         ret = true; | ||||
|     } | ||||
| 
 | ||||
|     if (has_any_button()) | ||||
|     { | ||||
|         std::vector<unsigned int> buttons = get_buttons(); | ||||
|         for (unsigned int i : buttons) | ||||
|         for (unsigned int i : m_buttons) | ||||
|         { | ||||
|             switch (i) | ||||
|             { | ||||
|  | @ -204,6 +135,12 @@ void Mouse3DController::shutdown() | |||
|     m_initialized = false; | ||||
| } | ||||
| 
 | ||||
| bool Mouse3DController::apply(Camera& camera) | ||||
| { | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
|     return m_state.apply(camera); | ||||
| } | ||||
| 
 | ||||
| void Mouse3DController::render_settings_dialog(unsigned int canvas_width, unsigned int canvas_height) const | ||||
| { | ||||
|     if (!m_running || !m_settings_dialog) | ||||
|  | @ -383,6 +320,8 @@ void Mouse3DController::collect_input() | |||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     std::lock_guard<std::mutex> lock(m_mutex); | ||||
| 
 | ||||
|     if (res > 0) | ||||
|     { | ||||
|         switch (retrieved_data[0]) | ||||
|  |  | |||
|  | @ -24,8 +24,6 @@ class Mouse3DController | |||
|         static const float DefaultRotationScale; | ||||
| 
 | ||||
|     private: | ||||
|         mutable std::mutex m_mutex; | ||||
| 
 | ||||
|         Vec3d m_translation; | ||||
|         Vec3f m_rotation; | ||||
|         std::vector<unsigned int> m_buttons; | ||||
|  | @ -36,19 +34,19 @@ class Mouse3DController | |||
|     public: | ||||
|         State(); | ||||
| 
 | ||||
|         void set_translation(const Vec3d& translation); | ||||
|         void set_rotation(const Vec3f& rotation); | ||||
|         void set_button(unsigned int id); | ||||
|         void reset_buttons(); | ||||
|         void set_translation(const Vec3d& translation) { m_translation = translation; } | ||||
|         void set_rotation(const Vec3f& rotation) { m_rotation = rotation; } | ||||
|         void set_button(unsigned int id) { m_buttons.push_back(id); } | ||||
|         void reset_buttons() { return m_buttons.clear(); } | ||||
| 
 | ||||
|         const Vec3d& get_translation() const; | ||||
|         const Vec3f& get_rotation() const; | ||||
|         const std::vector<unsigned int>& get_buttons() const; | ||||
|         const Vec3d& get_translation() const { return m_translation; } | ||||
|         const Vec3f& get_rotation() const { return m_rotation; } | ||||
|         const std::vector<unsigned int>& get_buttons() const { return m_buttons; } | ||||
| 
 | ||||
|         bool has_translation() const; | ||||
|         bool has_rotation() const; | ||||
|         bool has_translation_or_rotation() const; | ||||
|         bool has_any_button() const; | ||||
|         bool has_translation() const { return !m_translation.isApprox(Vec3d::Zero()); } | ||||
|         bool has_rotation() const { return !m_rotation.isApprox(Vec3f::Zero()); } | ||||
|         bool has_translation_or_rotation() const { return !m_translation.isApprox(Vec3d::Zero()) && !m_rotation.isApprox(Vec3f::Zero()); } | ||||
|         bool has_any_button() const { return !m_buttons.empty(); } | ||||
| 
 | ||||
|         double get_translation_scale() const { return m_translation_scale; } | ||||
|         void set_translation_scale(double scale) { m_translation_scale = scale; } | ||||
|  | @ -62,6 +60,7 @@ class Mouse3DController | |||
| 
 | ||||
|     bool m_initialized; | ||||
|     mutable State m_state; | ||||
|     std::mutex m_mutex; | ||||
|     std::thread m_thread; | ||||
|     hid_device* m_device; | ||||
|     std::string m_device_str; | ||||
|  | @ -82,7 +81,7 @@ public: | |||
|     bool has_translation_or_rotation() const { return m_state.has_translation_or_rotation(); } | ||||
|     bool has_any_button() const { return m_state.has_any_button(); } | ||||
| 
 | ||||
|     bool apply(Camera& camera) { return m_state.apply(camera); } | ||||
|     bool apply(Camera& camera); | ||||
| 
 | ||||
|     bool is_settings_dialog_shown() const { return m_settings_dialog; } | ||||
|     void show_settings_dialog(bool show) { m_settings_dialog = show; } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Enrico Turri
						Enrico Turri