Skip to content
Snippets Groups Projects
Select Git revision
  • 49fd261e3169abf11b8adb76cc7304910e6e9ce9
  • master default protected
2 results

coincellhell.c

Blame
  • laser_menu.cpp 4.56 KiB
    #include "laser_menu.hpp"
    
    #include "menu_helper.hpp"
    #include "ray_pass.hpp"
    #include "selector.hpp"
    #include "text.hpp"
    
    #include "phx/rendering/components/material_handle.hpp"
    #include "phx/rendering/components/transform.hpp"
    #include "phx/rendering/rendering_system.hpp"
    #include "phx/resources/loaders/scene_loader.hpp"
    #include "phx/resources/resource_utils.hpp"
    #include "phx/resources/types/material.hpp"
    #include "phx/resources/types/model.hpp"
    #include "phx/suppress_warnings.hpp"
    
    SUPPRESS_WARNINGS_BEGIN
    #include "glm/glm.hpp"
    #include "glm/gtx/rotate_vector.hpp"
    #include "glm/gtx/string_cast.hpp"
    SUPPRESS_WARNINGS_END
    
    LaserMenu::LaserMenu(phx::Scene* scene, phx::Engine* engine) {
      entity_ = phx::SceneLoader::InsertModelIntoScene(
          "models/opticalBench/menu/Tablet.obj", scene);
    
      auto rayPass = engine->GetSystem<phx::RenderingSystem>()
                         ->GetFrameGraph()
                         ->GetRenderPasses<phx::RayPass>()
                         .at(0);
    
      auto image = phx::ResourceUtils::LoadResourceFromFile<phx::Image>(
          "models/opticalBench/menu/Screen_Laser.png");
      auto transform = entity_->GetFirstComponent<phx::Transform>();
      for (auto i = 0u; i < transform->GetChildCount(); i++) {
        auto handle = transform->GetChild(i)
                          ->GetEntity()
                          ->GetFirstComponent<phx::MaterialHandle>();
        if (handle->GetMaterial()->GetName().compare("Screen") == 0) {
          handle->SetMaterial(MenuHelper::cloneMaterial(handle->GetMaterial(),
                                                        "LaserMenuScreenMaterial"));
          handle->GetMaterial()->SetAmbientImage(image);
          break;
        }
      }
    
      MenuHelper::createButtonPairVertical(up_p_, down_p_, text_p_, -0.1f, -0.05f,
                                           scene, engine, entity_);
      up_p_->SetOnPress([rayPass, this](phx::Transform*, glm::mat4) {
        rayPass->NextLaserPattern();
        text_p_->SetText(rayPass->GetCurrentPatternName());
      });
      down_p_->SetOnPress([rayPass, this](phx::Transform*, glm::mat4) {
        rayPass->PreviousLaserPattern();
        text_p_->SetText(rayPass->GetCurrentPatternName());
      });
      text_p_->SetText(rayPass->GetCurrentPatternName());
    
      // TIR
      switch_tir_ = new MenuSwitch(engine, scene, rayPass->GetRayTIR());
      switch_tir_->SetOnSwitch([this, rayPass](bool a) {
        rayPass->setRayTIR(a);
        text_tir_->SetText((a ? "on" : "off"));
      });
      switch_tir_->GetTransform()->SetParent(
          entity_->GetFirstComponent<phx::Transform>(), false);
      switch_tir_->GetTransform()->SetLocalTranslation(
          glm::vec3(0.0f, -0.075f, 0.1f));
    
      auto text_entity = scene->CreateEntity();
      text_entity->AddComponent<phx::Transform>()
          ->Translate(glm::vec3(-0.001f, -0.05f, 0.1f))
          .SetLocalRotationEuler(glm::vec3(180, -90, 0))
          .SetParent(entity_->GetFirstComponent<phx::Transform>());
      text_tir_ = text_entity->AddComponent<phx::Text>("", 0.006f);
      text_tir_->SetText(((rayPass->GetRayTIR()) ? "on" : "off"));
      text_tir_->SetGradient(glm::vec4(0), glm::vec4(0));
    
      // target write
    
      switch_tw_ = new MenuSwitch(engine, scene, rayPass->getTargetBufferWrite());
      switch_tw_->SetOnSwitch([this, rayPass](bool a) {
        rayPass->setTargetBufferWrite(a);
        text_tw_->SetText((a ? "on" : "off"));
      });
      switch_tw_->GetTransform()->SetParent(
          entity_->GetFirstComponent<phx::Transform>(), false);
      switch_tw_->GetTransform()->SetLocalTranslation(
          glm::vec3(0.0f, -0.025f, 0.0f));
    
      auto text_entity2 = scene->CreateEntity();
      text_entity2->AddComponent<phx::Transform>()
          ->Translate(glm::vec3(-0.001f, -0.05f, 0.0f))
          .SetLocalRotationEuler(glm::vec3(180, -90, 0))
          .SetParent(entity_->GetFirstComponent<phx::Transform>());
      text_tw_ = text_entity2->AddComponent<phx::Text>("", 0.006f);
      text_tw_->SetText(((rayPass->getTargetBufferWrite()) ? "on" : "off"));
      text_tw_->SetGradient(glm::vec4(0), glm::vec4(0));
    
      // clear laser target
      clear_target_ = new MenuButton(engine, scene, MenuButton::RECTANGULAR);
      clear_target_->GetTransform()->SetParent(
          entity_->GetFirstComponent<phx::Transform>(), false);
      clear_target_->GetTransform()->SetLocalRotationEuler(glm::vec3(0, -90, 90));
      clear_target_->GetTransform()->SetLocalScale(glm::vec3(0.06f, 0.01f, 0.017f));
      clear_target_->GetTransform()->SetLocalTranslation(glm::vec3(0.0f,-0.075f,0.0f));
      clear_target_->SetHoldable(false);
      clear_target_->SetOnPress(
          [this, rayPass](phx::Transform*, glm::mat4) { rayPass->clearTargetBuffers(); });
    }
    
    LaserMenu::~LaserMenu() {
      delete switch_tir_;
      delete up_p_;
      delete down_p_;
      delete clear_target_;
      delete switch_tw_;
      delete text_tw_;
    }