Archived
1
Fork 0

Rename Input to input_system and move to prism namespace

This commit is contained in:
redstrate 2021-04-20 11:53:38 -04:00
parent bae6d05184
commit 653d5bb6ca
5 changed files with 103 additions and 124 deletions

View file

@ -16,7 +16,6 @@ namespace ui {
}
class Scene;
class Input;
class Renderer;
class RenderTarget;
class Physics;
@ -25,6 +24,7 @@ struct Timer;
namespace prism {
class app;
class imgui_backend;
class input_system;
struct AnimationTarget {
float current_time = 0.0f;
@ -114,7 +114,7 @@ namespace prism {
/** Get the input system.
@return Instance of the input system. Will not be null.
*/
Input* get_input();
input_system* get_input();
/** Get the renderer for a window.
@param index Index of the window. Default is 0.
@ -371,7 +371,7 @@ namespace prism {
app* app = nullptr;
GFX* gfx = nullptr;
std::unique_ptr<Input> input;
std::unique_ptr<input_system> input;
std::unique_ptr<Physics> physics;
std::unique_ptr<Renderer> renderer;

View file

@ -5,7 +5,8 @@
#include "platform.hpp"
enum class Axis {
namespace prism {
enum class axis {
MouseX,
MouseY,
@ -19,11 +20,11 @@ enum class Axis {
RightStickY
};
struct InputBindingData {
struct input_binding {
std::string name;
std::map<InputButton, float> buttons;
std::vector<Axis> axises;
std::vector<axis> axises;
float value = 0.0f;
@ -33,7 +34,7 @@ struct InputBindingData {
/** Input system designed for handling events in a cross platform manner and across different input types.
*/
class Input {
class input_system {
public:
/// Updates input bindings and their values.
void update();
@ -54,7 +55,7 @@ public:
@param name The binding name.
@param button The axis to associate with.
*/
void add_binding_axis(const std::string& name, Axis axis);
void add_binding_axis(const std::string &name, axis axis);
/** Gets the input value of a binding.
@param name The binding name.
@ -78,9 +79,10 @@ public:
bool is_repeating(const std::string &name);
/// Returns all of the bindings registered with this Input system.
std::vector<InputBindingData> get_bindings() const;
[[nodiscard]] std::vector<input_binding> get_bindings() const;
private:
std::vector<InputBindingData> _input_bindings;
std::vector<input_binding> _input_bindings;
std::tuple<int, int> _last_cursor_position;
};
}

View file

@ -42,7 +42,7 @@ engine::engine(const int argc, char* argv[]) {
for(int i = 0; i < argc; i++)
command_line_arguments.emplace_back(argv[i]);
input = std::make_unique<Input>();
input = std::make_unique<input_system>();
physics = std::make_unique<Physics>();
imgui = std::make_unique<prism::imgui_backend>();
assetm = std::make_unique<AssetManager>();
@ -108,7 +108,7 @@ GFX* engine::get_gfx() {
return gfx;
}
Input* engine::get_input() {
prism::input_system* engine::get_input() {
return input.get();
}

View file

@ -5,6 +5,8 @@
#include "engine.hpp"
#include "log.hpp"
using prism::input_system;
bool is_in_range(int value, int cond, int range) {
int low = cond - range;
int high = cond + range;
@ -12,7 +14,7 @@ bool is_in_range(int value, int cond, int range) {
return value >= low && value <= high;
}
void Input::update() {
void input_system::update() {
const auto& [x, y] = platform::get_cursor_position();
auto& [oldX, oldY] = _last_cursor_position;
@ -72,28 +74,28 @@ void Input::update() {
float nextValue = 0.0f;
switch (axis) {
case Axis::MouseX:
case axis::MouseX:
nextValue = xDelta;
break;
case Axis::MouseY:
case axis::MouseY:
nextValue = yDelta;
break;
case Axis::ScrollX:
case axis::ScrollX:
nextValue = sx;
break;
case Axis::ScrollY:
case axis::ScrollY:
nextValue = sy;
break;
case Axis::LeftStickX:
case axis::LeftStickX:
nextValue = lx;
break;
case Axis::LeftStickY:
case axis::LeftStickY:
nextValue = ly;
break;
case Axis::RightStickX:
case axis::RightStickX:
nextValue = rx;
break;
case Axis::RightStickY:
case axis::RightStickY:
nextValue = ry;
break;
}
@ -104,21 +106,21 @@ void Input::update() {
}
}
void Input::add_binding(const std::string& name) {
InputBindingData data;
void input_system::add_binding(const std::string& name) {
input_binding data;
data.name = name;
_input_bindings.push_back(data);
}
void Input::add_binding_button(const std::string& name, InputButton key, float value) {
void input_system::add_binding_button(const std::string& name, InputButton key, float value) {
for (auto& binding : _input_bindings) {
if (binding.name == name)
binding.buttons[key] = value;
}
}
void Input::add_binding_axis(const std::string& name, Axis axis) {
void input_system::add_binding_axis(const std::string& name, axis axis) {
for (auto& binding : _input_bindings) {
if (binding.name == name) {
binding.axises.push_back(axis);
@ -126,7 +128,7 @@ void Input::add_binding_axis(const std::string& name, Axis axis) {
}
}
float Input::get_value(const std::string& name) {
float input_system::get_value(const std::string& name) {
for (auto& binding : _input_bindings) {
if (binding.name == name) {
return binding.value;
@ -136,11 +138,11 @@ float Input::get_value(const std::string& name) {
return 0.0f;
}
bool Input::is_pressed(const std::string &name, bool repeating) {
bool input_system::is_pressed(const std::string &name, bool repeating) {
return get_value(name) == 1.0 && (repeating ? true : !is_repeating(name));
}
bool Input::is_repeating(const std::string& name) {
bool input_system::is_repeating(const std::string& name) {
for (auto& binding : _input_bindings) {
if (binding.name == name) {
return binding.repeat;
@ -150,6 +152,6 @@ bool Input::is_repeating(const std::string& name) {
return false;
}
std::vector<InputBindingData> Input::get_bindings() const {
std::vector<prism::input_binding> input_system::get_bindings() const {
return _input_bindings;
}

View file

@ -65,7 +65,7 @@ CommonEditor::CommonEditor(std::string id) : id(id) {
load_options();
Input* input = engine->get_input();
auto input = engine->get_input();
input->add_binding("movementX");
input->add_binding("movementY");
@ -78,8 +78,8 @@ CommonEditor::CommonEditor(std::string id) : id(id) {
input->add_binding("lookX");
input->add_binding("lookY");
input->add_binding_axis("lookX", Axis::MouseX);
input->add_binding_axis("lookY", Axis::MouseY);
input->add_binding_axis("lookX", prism::axis::MouseX);
input->add_binding_axis("lookY", prism::axis::MouseY);
input->add_binding("cameraLook");
input->add_binding_button("cameraLook", InputButton::MouseRight);
@ -96,31 +96,6 @@ static float yaw = 0.0f;
static float pitch = 0.0f;
static bool willCaptureMouse = false;
struct Ray {
Vector3 origin, direction;
float t;
};
// from https://nelari.us/post/gizmos/
float closest_distance_between_lines(Ray& l1, Ray& l2) {
const Vector3 dp = l2.origin - l1.origin;
const float v12 = dot(l1.direction, l1.direction);
const float v22 = dot(l2.direction, l2.direction);
const float v1v2 = dot(l1.direction, l2.direction);
const float det = v1v2 * v1v2 - v12 * v22;
const float inv_det = 10.f / det;
const float dpv1 = dot(dp, l1.direction);
const float dpv2 = dot(dp, l2.direction);
l1.t = inv_det * (v22 * dpv1 - v1v2 * dpv2);
l2.t = inv_det * (v1v2 * dpv1 - v12 * dpv2);
return length(dp + l2.direction * l2.t - l1.direction * l1.t);
}
static float previous_intersect = 0.0;
void CommonEditor::update(float deltaTime) {
@ -197,14 +172,14 @@ void CommonEditor::update(float deltaTime) {
Vector4 ray_end = view_proj_inverse * Vector4(n.x, n.y, 1.0f, 1.0f);
ray_end *= 1.0f / ray_end.w;
Ray camera_ray;
ray camera_ray;
camera_ray.origin = ray_start.xyz;
camera_ray.direction = normalize(ray_end.xyz - ray_start.xyz);
camera_ray.t = std::numeric_limits<float>::max();
auto& transform = engine->get_scene()->get<Transform>(selected_object);
Ray transform_ray;
ray transform_ray;
transform_ray.origin = last_object_position;
transform_ray.t = std::numeric_limits<float>::max();