Rename Input to input_system and move to prism namespace
This commit is contained in:
parent
bae6d05184
commit
653d5bb6ca
5 changed files with 103 additions and 124 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -5,82 +5,84 @@
|
|||
|
||||
#include "platform.hpp"
|
||||
|
||||
enum class Axis {
|
||||
MouseX,
|
||||
MouseY,
|
||||
namespace prism {
|
||||
enum class axis {
|
||||
MouseX,
|
||||
MouseY,
|
||||
|
||||
ScrollX,
|
||||
ScrollY,
|
||||
ScrollX,
|
||||
ScrollY,
|
||||
|
||||
LeftStickX,
|
||||
LeftStickY,
|
||||
LeftStickX,
|
||||
LeftStickY,
|
||||
|
||||
RightStickX,
|
||||
RightStickY
|
||||
};
|
||||
RightStickX,
|
||||
RightStickY
|
||||
};
|
||||
|
||||
struct InputBindingData {
|
||||
std::string name;
|
||||
std::map<InputButton, float> buttons;
|
||||
struct input_binding {
|
||||
std::string name;
|
||||
std::map<InputButton, float> buttons;
|
||||
|
||||
std::vector<Axis> axises;
|
||||
std::vector<axis> axises;
|
||||
|
||||
float value = 0.0f;
|
||||
float value = 0.0f;
|
||||
|
||||
bool repeat = false;
|
||||
InputButton last_button = InputButton::Invalid;
|
||||
};
|
||||
bool repeat = false;
|
||||
InputButton last_button = InputButton::Invalid;
|
||||
};
|
||||
|
||||
/** Input system designed for handling events in a cross platform manner and across different input types.
|
||||
*/
|
||||
class Input {
|
||||
public:
|
||||
/// Updates input bindings and their values.
|
||||
void update();
|
||||
|
||||
/** Add a new binding.
|
||||
@param name The binding name.
|
||||
/** Input system designed for handling events in a cross platform manner and across different input types.
|
||||
*/
|
||||
void add_binding(const std::string& name);
|
||||
class input_system {
|
||||
public:
|
||||
/// Updates input bindings and their values.
|
||||
void update();
|
||||
|
||||
/** Ties a button-type input to the binding.
|
||||
@param name The binding name.
|
||||
@param button The button to associate with.
|
||||
@param value The resulting input value when the button is pressed. Default is 1.0.
|
||||
*/
|
||||
void add_binding_button(const std::string& name, InputButton button, float value = 1.0f);
|
||||
/** Add a new binding.
|
||||
@param name The binding name.
|
||||
*/
|
||||
void add_binding(const std::string &name);
|
||||
|
||||
/** Ties an axis-type input to the button.
|
||||
@param name The binding name.
|
||||
@param button The axis to associate with.
|
||||
*/
|
||||
void add_binding_axis(const std::string& name, Axis axis);
|
||||
/** Ties a button-type input to the binding.
|
||||
@param name The binding name.
|
||||
@param button The button to associate with.
|
||||
@param value The resulting input value when the button is pressed. Default is 1.0.
|
||||
*/
|
||||
void add_binding_button(const std::string &name, InputButton button, float value = 1.0f);
|
||||
|
||||
/** Gets the input value of a binding.
|
||||
@param name The binding name.
|
||||
@return The input value associated with that binding, or 0.0 if none is found.
|
||||
@note If this binding is tied to a button-type input, assume that 0.0 means not pressed.
|
||||
@note If this binding is tied to an axis-type input, assume that 0.0 means the stick is centered.
|
||||
*/
|
||||
float get_value(const std::string& name);
|
||||
/** Ties an axis-type input to the button.
|
||||
@param name The binding name.
|
||||
@param button The axis to associate with.
|
||||
*/
|
||||
void add_binding_axis(const std::string &name, axis axis);
|
||||
|
||||
/** Gets whether or not the binding is pressed.
|
||||
@param name The binding name.
|
||||
@param repeating If true, return a correct value regardless if it was identical for multiple frames. If false, any continued input past the first frame of being pressed is ignored and the input is considered released.
|
||||
@return If the binding is found, return true if the binding value is 1.0. If no binding is found or if the binding has continued pressing and repeating is turned off, returns false.
|
||||
*/
|
||||
bool is_pressed(const std::string& name, bool repeating = false);
|
||||
/** Gets the input value of a binding.
|
||||
@param name The binding name.
|
||||
@return The input value associated with that binding, or 0.0 if none is found.
|
||||
@note If this binding is tied to a button-type input, assume that 0.0 means not pressed.
|
||||
@note If this binding is tied to an axis-type input, assume that 0.0 means the stick is centered.
|
||||
*/
|
||||
float get_value(const std::string &name);
|
||||
|
||||
/** Queries if the binding is repeating it's input.
|
||||
@param name The binding name.
|
||||
@return If the binding is found, returns true if the binding's values have been identical for multiple frames. Returns false if the binding is not found.
|
||||
*/
|
||||
bool is_repeating(const std::string& name);
|
||||
/** Gets whether or not the binding is pressed.
|
||||
@param name The binding name.
|
||||
@param repeating If true, return a correct value regardless if it was identical for multiple frames. If false, any continued input past the first frame of being pressed is ignored and the input is considered released.
|
||||
@return If the binding is found, return true if the binding value is 1.0. If no binding is found or if the binding has continued pressing and repeating is turned off, returns false.
|
||||
*/
|
||||
bool is_pressed(const std::string &name, bool repeating = false);
|
||||
|
||||
/// Returns all of the bindings registered with this Input system.
|
||||
std::vector<InputBindingData> get_bindings() const;
|
||||
/** Queries if the binding is repeating it's input.
|
||||
@param name The binding name.
|
||||
@return If the binding is found, returns true if the binding's values have been identical for multiple frames. Returns false if the binding is not found.
|
||||
*/
|
||||
bool is_repeating(const std::string &name);
|
||||
|
||||
private:
|
||||
std::vector<InputBindingData> _input_bindings;
|
||||
std::tuple<int, int> _last_cursor_position;
|
||||
};
|
||||
/// Returns all of the bindings registered with this Input system.
|
||||
[[nodiscard]] std::vector<input_binding> get_bindings() const;
|
||||
|
||||
private:
|
||||
std::vector<input_binding> _input_bindings;
|
||||
std::tuple<int, int> _last_cursor_position;
|
||||
};
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Reference in a new issue