💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "tool-system.hpp"
  20. ToolSystem::ToolSystem(ComponentManager* componentManager):
  21. System(componentManager),
  22. tools(componentManager),
  23. picked(false)
  24. {
  25. tools.addGroupObserver(this);
  26. }
  27. ToolSystem::~ToolSystem()
  28. {}
  29. void ToolSystem::update(float t, float dt)
  30. {
  31. pick();
  32. auto members = tools.getMembers();
  33. for (const ToolGroup::Member* member: *members)
  34. {
  35. ModelComponent* model = std::get<0>(member->components);
  36. ToolComponent* tool = std::get<1>(member->components);
  37. TransformComponent* transform = std::get<2>(member->components);
  38. model->model.setActive(tool->active);
  39. if (picked)
  40. {
  41. transform->transform.translation = mouseWorldPosition;
  42. }
  43. }
  44. picked = false;
  45. }
  46. void ToolSystem::setPickingCamera(const Camera* camera)
  47. {
  48. pickingCamera = camera;
  49. }
  50. void ToolSystem::setPickingViewport(const Vector4& viewport)
  51. {
  52. pickingViewport = viewport;
  53. }
  54. void ToolSystem::pick()
  55. {
  56. Vector3 mouseNear = pickingCamera->unproject(Vector3(mouseScreenPosition, 0.0f), pickingViewport);
  57. Vector3 mouseFar = pickingCamera->unproject(Vector3(mouseScreenPosition, 1.0f), pickingViewport);
  58. Ray pickingRay;
  59. pickingRay.origin = mouseNear;
  60. pickingRay.direction = glm::normalize(mouseFar - mouseNear);
  61. Plane pickingPlane(Vector3(0.0f, 1.0f, 0.0f), Vector3(0.0f));
  62. auto pickingIntersection = pickingRay.intersects(pickingPlane);
  63. picked = std::get<0>(pickingIntersection);
  64. if (picked)
  65. {
  66. mouseWorldPosition = pickingRay.extrapolate(std::get<1>(pickingIntersection));
  67. }
  68. }
  69. void ToolSystem::memberRegistered(const ToolGroup::Member* member)
  70. {
  71. ToolComponent* tool = std::get<1>(member->components);
  72. tool->active = false;
  73. }
  74. void ToolSystem::memberUnregistered(const ToolGroup::Member* member)
  75. {}
  76. void ToolSystem::handleEvent(const MouseMovedEvent& event)
  77. {
  78. mouseScreenPosition = Vector2(event.x, pickingViewport[3] - event.y);
  79. }