+void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
+ if (e.keysym.sym == SDLK_p) {
+ paused = !paused;
+ } else if (e.keysym.sym == SDLK_F1) {
+ rp.Toggle();
+ }
+}
+
+void MasterState::OnMouseDown(const SDL_MouseButtonEvent &e) {
+ if (e.button == SDL_BUTTON_RIGHT) {
+ SDL_SetRelativeMouseMode(SDL_TRUE);
+ cam_dragging = true;
+ }
+}
+
+void MasterState::OnMouseUp(const SDL_MouseButtonEvent &e) {
+ if (e.button == SDL_BUTTON_LEFT) {
+ glm::dmat4 inverse(glm::inverse(cam.Projection() * cam.View()));
+ math::Ray ray(inverse * App().GetViewport().ShootPixel(e.x, e.y));
+
+ creature::Creature *closest_creature = nullptr;
+ double closest_dist = std::numeric_limits<double>::infinity();
+ for (creature::Creature *c : sim.LiveCreatures()) {
+ glm::dvec3 normal(0.0);
+ double dist = 0.0;
+ if (Intersect(ray, c->CollisionBounds(), glm::dmat4(cam.Model(c->GetSituation().GetPlanet())) * c->CollisionTransform(), normal, dist)
+ && dist < closest_dist) {
+ closest_creature = c;
+ closest_dist = dist;
+ }
+ }
+
+ world::Body *closest_body = nullptr;
+ for (world::Body *b : sim.Bodies()) {
+ glm::dvec3 normal(0.0);
+ double dist = 0.0;
+ if (Intersect(ray, glm::dmat4(cam.Model(*b)) * b->CollisionBounds(), normal, dist) && dist < closest_dist) {
+ closest_creature = nullptr;
+ closest_body = b;
+ closest_dist = dist;
+ }
+ }
+
+ if (closest_creature) {
+ cp.Show(*closest_creature);
+ bp.Hide();
+ } else if (closest_body) {
+ bp.Show(*closest_body);
+ cp.Hide();
+ } else {
+ cp.Hide();
+ bp.Hide();
+ }
+ } else if (e.button == SDL_BUTTON_RIGHT) {
+ SDL_SetRelativeMouseMode(SDL_FALSE);
+ cam_dragging = false;
+ }
+}
+
+void MasterState::OnMouseMotion(const SDL_MouseMotionEvent &e) {
+ constexpr double pitch_scale = PI * 0.001;
+ constexpr double yaw_scale = PI * 0.002;
+ if (cam_dragging) {
+ cam_orient.x = glm::clamp(cam_orient.x + double(e.yrel) * pitch_scale, 0.0, PI * 0.499);
+ cam_orient.y = std::fmod(cam_orient.y + double(e.xrel) * yaw_scale, PI * 2.0);
+ }
+}
+
+void MasterState::OnMouseWheel(const SDL_MouseWheelEvent &e) {
+ constexpr double roll_scale = PI * 0.0625;
+ constexpr double zoom_scale = -1.0;
+ constexpr double zoom_base = 1.125;
+ cam_orient.z = glm::clamp(cam_orient.z + double(e.x) * roll_scale, PI * -0.5, PI * 0.5);
+ if (cp.Shown()) {
+ cam_tgt_dist = std::max(cp.GetCreature().Size() * 2.0, cam_tgt_dist * std::pow(zoom_base, double(e.y) * zoom_scale));
+ } else {
+ cam_tgt_dist = std::max(1.0, cam_tgt_dist * std::pow(zoom_base, double(e.y) * zoom_scale));
+ }
+}
+