added clickable buttons :D

This commit is contained in:
Jay Robson 2024-02-03 02:27:08 +11:00
parent 309025675a
commit d222599836
23 changed files with 138 additions and 89 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/unbaked/scene.blend (Stored with Git LFS)

Binary file not shown.

View File

@ -1,16 +1,23 @@
#include "focus.hpp"
#include "../camera.hpp"
#include "../../util/math.hpp"
#include "mouse.hpp"
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "focus.hpp"
#include "../../util/math.hpp"
#include "../window.hpp"
#include "../camera.hpp"
#include "../resize.hpp"
#include "mouse.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
using namespace sim::graphics;
static glm::vec<3, double> trigger_near;
static glm::vec<3, double> trigger_far;
static std::unique_ptr<focus::focus_t> state = nullptr;
static bool mouse_visible = false;
static bool mouse_locked = false;
@ -44,9 +51,28 @@ void focus::on_mouse_button(int button, int action, int mods)
state->on_mouse_button(button, action, mods);
}
else if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
else if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
triggered = true;
if(is_mouse_locked())
{
double mx, my;
mouse::get(mx, my);
glm::vec2 wsize = resize::get_size();
glm::vec4 viewport = glm::vec4(0, 0, wsize);
glm::vec2 mouse(mx, wsize.y - my);
trigger_near = glm::unProject(glm::vec3(mouse, -1), camera::get_matrix(), window::projection_matrix, viewport);
trigger_far = glm::unProject(glm::vec3(mouse, 1), camera::get_matrix(), window::projection_matrix, viewport);
}
else
{
trigger_near = camera::get_pos();
trigger_far = trigger_near + camera::get_normal();
}
}
}
@ -66,15 +92,30 @@ void focus::on_charcode(unsigned int c)
}
}
glm::vec<3, double> focus::get_trigger_near()
{
return trigger_near;
}
glm::vec<3, double> focus::get_trigger_far()
{
return trigger_far;
}
void focus::update()
{
triggered = false;
bool locked = is_mouse_locked();
bool c = is_mouse_locked();
if(locked != mouse_visible)
if(state && !state->cursor_is_visible())
{
if(locked)
c = false;
}
if(c != mouse_visible)
{
if(c)
{
mouse::show_cursor();
}
@ -84,7 +125,7 @@ void focus::update()
mouse::hide_cursor();
}
mouse_visible = locked;
mouse_visible = c;
}
if(state)

View File

@ -1,6 +1,8 @@
#pragma once
#include <glm/matrix.hpp>
#include <memory>
namespace sim::graphics::focus
@ -9,6 +11,7 @@ namespace sim::graphics::focus
struct focus_t
{
virtual ~focus_t() { }
virtual bool cursor_is_visible() { return true; }
virtual void on_keypress(int key, int sc, int action, int mods) { }
virtual void on_mouse_button(int button, int action, int mods) { }
virtual void on_cursor_pos(double x, double y) { }
@ -21,6 +24,8 @@ void clear_focus();
bool is_triggered();
bool is_mouse_locked();
void clear_mouse_locked();
glm::vec<3, double> get_trigger_near();
glm::vec<3, double> get_trigger_far();
void set(std::unique_ptr<focus_t> f);
void on_keypress(int key, int sc, int action, int mods);
void on_mouse_button(int button, int action, int mods);

View File

@ -38,6 +38,11 @@ void mouse::get(double& x, double& y)
y = ypos;
}
glm::vec2 mouse::get()
{
return {xpos, ypos};
}
void mouse::show_cursor()
{
double x, y;

View File

@ -1,10 +1,13 @@
#pragma once
#include <glm/vec2.hpp>
namespace sim::graphics::mouse
{
void init();
glm::vec2 get();
void get(double& x, double& y);
void show_cursor();
void hide_cursor();

View File

@ -98,7 +98,10 @@ bool ray_intersects_triangle(vec3 ray_origin,
bool mesh::check_focus(double len) const
{
return focus::is_triggered() && check_intersect(camera::get_pos(), camera::get_normal() * len);
auto near = focus::get_trigger_near();
auto far = focus::get_trigger_far();
return focus::is_triggered() && check_intersect(near, glm::normalize(far - near) * len);
}
bool mesh::check_focus() const

View File

@ -15,15 +15,33 @@
using namespace sim::graphics;
using namespace sim::graphics::monitor;
struct core_focus_t : public focus::focus_t
struct core_joystick : public focus::focus_t
{
virtual void on_cursor_pos(double x, double y)
{
sim::system::active.reactor->add_rod_speed(-y * 1e-6);
sim::system::active.reactor->add_rod_speed(y * 1e-6);
}
void set_all(bool state)
virtual void on_mouse_button(int button, int action, int mods)
{
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
{
focus::clear_focus();
}
}
virtual bool cursor_is_visible()
{
return false;
}
};
core::core()
{
}
static void set_all(bool state)
{
for(int i = 0; i < sim::system::active.reactor->rods.size(); i++)
{
sim::reactor::rod* r = sim::system::active.reactor->rods[i].get();
@ -33,65 +51,6 @@ struct core_focus_t : public focus::focus_t
r->selected = state;
}
}
}
void toggle_auto()
{
//TODO
}
virtual void on_charcode(unsigned int c)
{
sim::system& sys = sim::system::active;
switch(c)
{
case 'a': case 'A':
sys.reactor->move_cursor(-1);
break;
case 'd': case 'D':
sys.reactor->move_cursor(1);
break;
case 'w': case 'W':
sys.reactor->move_cursor(-sim::system::active.reactor->height);
break;
case 's': case 'S':
sys.reactor->move_cursor(sim::system::active.reactor->height);
break;
}
}
virtual void on_keypress(int key, int sc, int action, int mods)
{
if(action != GLFW_PRESS)
{
return;
}
switch(key)
{
case GLFW_KEY_Z:
sim::system::active.reactor->toggle_selected();
break;
case GLFW_KEY_C:
toggle_auto();
break;
case GLFW_KEY_X:
sim::system::active.reactor->reset_rod_speed();
break;
case GLFW_KEY_Q:
set_all(true);
break;
case GLFW_KEY_E:
set_all(false);
break;
}
}
};
core::core()
{
}
void core::init()
@ -111,21 +70,45 @@ void core::init()
mesh2.bind();
mesh2.set(rmesh2, GL_STATIC_DRAW);
mesh_click.load_model("../assets/model/", "reactor_core_input.stl");
mesh_scram.load_model("../assets/model/", "reactor_core_scram.stl");
m_buttons[0].load_model("../assets/model/", "reactor_core_button1.stl");
m_buttons[1].load_model("../assets/model/", "reactor_core_button2.stl");
m_buttons[2].load_model("../assets/model/", "reactor_core_button3.stl");
m_buttons[3].load_model("../assets/model/", "reactor_core_button4.stl");
m_buttons[4].load_model("../assets/model/", "reactor_core_button5.stl");
m_buttons[5].load_model("../assets/model/", "reactor_core_button6.stl");
m_buttons[6].load_model("../assets/model/", "reactor_core_button7.stl");
m_buttons[7].load_model("../assets/model/", "reactor_core_button8.stl");
m_buttons[8].load_model("../assets/model/", "reactor_core_button9.stl");
m_joystick.load_model("../assets/model/", "reactor_core_joystick.stl");
m_scram.load_model("../assets/model/", "reactor_core_scram.stl");
}
void core::update()
{
if(mesh_click.check_focus())
{
focus::set(std::make_unique<core_focus_t>());
}
sim::system& sys = sim::system::active;
if(mesh_scram.check_focus())
{
if(m_joystick.check_focus())
focus::set(std::make_unique<core_joystick>());
if(m_scram.check_focus())
sim::system::active.reactor->scram();
}
if(m_buttons[0].check_focus())
set_all(true);
if(m_buttons[1].check_focus())
sys.reactor->move_cursor(-sim::system::active.reactor->height);
if(m_buttons[2].check_focus())
set_all(false);
if(m_buttons[3].check_focus())
sys.reactor->move_cursor(-1);
if(m_buttons[4].check_focus())
sys.reactor->move_cursor(sim::system::active.reactor->height);
if(m_buttons[5].check_focus())
sys.reactor->move_cursor(1);
if(m_buttons[6].check_focus())
sim::system::active.reactor->toggle_selected();
if(m_buttons[7].check_focus())
sim::system::active.reactor->reset_rod_speed();
// if(m_buttons[8].check_focus())
//
}
void core::render()

View File

@ -9,9 +9,12 @@ namespace sim::graphics::monitor
class core
{
sim::graphics::mesh mesh_click, mesh_scram;
sim::graphics::glmesh mesh1, mesh2;
sim::graphics::mesh m_buttons[9];
sim::graphics::mesh m_joystick;
sim::graphics::mesh m_scram;
public:
core();

View File

@ -32,6 +32,8 @@ static glmesh MeshScene;
static monitor::vessel MonitorVessel;
static monitor::core MonitorCore;
glm::mat4 window::projection_matrix;
void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
if(severity != GL_DEBUG_SEVERITY_NOTIFICATION)
@ -132,6 +134,7 @@ void window::render()
glm::mat4 mat_projection = glm::perspective(glm::radians(90.0f), resize::get_aspect(), 0.01f, 20.f);
glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]);
glUniformMatrix4fv(shader::gl_camera, 1, false, &mat_camera[0][0]);
projection_matrix = mat_projection;
glClearColor(0, 0, 0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@ -2,12 +2,15 @@
#pragma once
#include <GLFW/glfw3.h>
#include <glm/matrix.hpp>
#include "../system.hpp"
namespace sim::graphics::window
{
extern glm::mat4 projection_matrix;
bool should_close();
void create();
void update(double dt);

View File

@ -50,7 +50,7 @@ void boron_rod::update(double secs)
void boron_rod::update_selected(double a)
{
inserted += a;
inserted -= a;
if(inserted > 1) inserted = 1;
if(inserted < 0) inserted = 0;

View File

@ -58,7 +58,7 @@ void reactor::add_rod_speed(double a)
void reactor::scram()
{
rod_speed = 0.2;
rod_speed = -0.2;
for(int i = 0; i < size; i++)
{