#include "gameMainWindow.h" #include "gfx/camera_controller.h" #include "manualCameraController.h" #include "maths.h" #include "text.h" #include "toolbar.h" #include "ui/uiComponent.h" #include "window.h" #include #include #include #include #include #include #include // IWYU pragma: keep #include #include #include #include #include #include #include #include class UIShader; class GameMainToolbar : public Toolbar { public: GameMainToolbar() : Toolbar { {"ui/icon/network.png", [](const SDL_Event &) { // fprintf(stderr, "network click\n"); }}, } { } }; #include class GameMainSelector : public UIComponent { public: GameMainSelector(const Camera * c, glm::vec2 size) : UIComponent {{{}, size}}, camera {c} { } void render(const UIShader & shader, const Position & pos) const override { if (!clicked.empty()) { Text {clicked, {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, pos); } } bool handleInput(const SDL_Event & e, const Position &) override { switch (e.type) { case SDL_MOUSEBUTTONDOWN: if (e.button.button == SDL_BUTTON_LEFT) { const auto mouse = glm::vec2 {e.button.x, e.button.y} / position.size; glm::vec2 baryPos {}; float eh; const auto ray = camera->unProject(mouse); if (const auto selected = gameState->world.applyOne(&Selectable::intersectRay, ray, &baryPos, &eh); selected != gameState->world.end()) { const auto & ref = *selected.base()->get(); clicked = typeid(ref).name(); } else if (const auto pos = gameState->geoData->intersectRay(ray)) { clicked = streamed_string(*pos); } else { clicked.clear(); } return true; } } return false; } private: const Camera * camera; std::string clicked; }; GameMainWindow::GameMainWindow(size_t w, size_t h) : Window {w, h, "I Like Trains"}, camera {{-1250.0F, -1250.0F, 35.0F}, quarter_pi, rdiv(w, h), 0.1F, 10000.0F} { uiComponents.create(glm::vec2 {-1150, -1150}); uiComponents.create(&camera, glm::vec2 {w, h}); uiComponents.create(); shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, 0, -1})); shader.setUniform("lightColor", {.6, .6, .6}); shader.setUniform("ambientColor", {0.5, 0.5, 0.5}); } void GameMainWindow::tick(TickDuration) { uiComponents.apply(&CameraController::updateCamera, &camera); shader.setView(camera.GetViewProjection()); } void GameMainWindow::render() const { glEnable(GL_DEPTH_TEST); gameState->world.apply(&Renderable::render, shader); Window::render(); }