diff --git a/include/blah/input/binding.h b/include/blah/input/binding.h index f657506..2052b0e 100644 --- a/include/blah/input/binding.h +++ b/include/blah/input/binding.h @@ -6,60 +6,48 @@ namespace Blah { - // Represents a Controller Trigger or a single direction of a Controller Axis. - // Used in the Binding implementation. - struct BoundTrigger - { - // Controller Index we're bound to - int controller = 0; - - // The Axis we're bound to - Axis axis = Axis::None; - - // Minimum value of the axis - float threshold = 0.01f; - - // requires a positive value - // otherwise requires a negative value - bool positive = true; - - BoundTrigger() = default; - BoundTrigger(Axis axis); - BoundTrigger(int controller, Axis axis, float threshold, bool positive); - - bool is_down(float axis_value) const; - - // helper functions for frequent use cases - static BoundTrigger left_stick_left(int controller, float threshold); - static BoundTrigger left_stick_right(int controller, float threshold); - static BoundTrigger left_stick_up(int controller, float threshold); - static BoundTrigger left_stick_down(int controller, float threshold); - static BoundTrigger right_stick_left(int controller, float threshold); - static BoundTrigger right_stick_right(int controller, float threshold); - static BoundTrigger right_stick_up(int controller, float threshold); - static BoundTrigger right_stick_down(int controller, float threshold); - static BoundTrigger left_trigger(int controller, float threshold); - static BoundTrigger right_trigger(int controller, float threshold); - }; - - struct BoundButton - { - // Controller Index we're bound to - int controller = 0; - - // Button we're bound to - Button button = Button::None; - - BoundButton() = default; - BoundButton(Button button); - BoundButton(int controller, Button button); - }; - // Single input Binding class Binding { public: + // Represents a Controller Trigger or a single direction of a Controller Axis. + struct TriggerBind + { + // Controller Index we're bound to + int controller = 0; + + // The Axis we're bound to + Axis axis = Axis::None; + + // Minimum value of the axis + float threshold = 0.01f; + + // requires a positive value + // otherwise requires a negative value + bool positive = true; + + TriggerBind() = default; + TriggerBind(Axis axis); + TriggerBind(int controller, Axis axis, float threshold, bool positive); + + bool is_down(float axis_value) const; + }; + + // Represents a Controller Button. + struct ButtonBind + { + // Controller Index we're bound to + int controller = 0; + + // Button we're bound to + Button button = Button::None; + + ButtonBind() = default; + ButtonBind(Button button); + ButtonBind(int controller, Button button); + }; + // Input Buffer for press events float press_buffer = 0; @@ -68,16 +56,31 @@ namespace Blah // List of bound Keys StackVector keys; - + // List of bound Buttons - StackVector buttons; + StackVector buttons; // List of bound Triggers / Axis - StackVector triggers; + StackVector triggers; // List of bound Mouse buttons StackVector mouse; + Binding() = default; + + Binding(float press_buffer) + : press_buffer(press_buffer) + { + + } + + template + Binding(float press_buffer, const Args&... args) + : press_buffer(press_buffer) + { + add(args...); + } + // if the binding has been pressed bool pressed() const; @@ -106,27 +109,34 @@ namespace Blah void consume_release(); // adds a key to the binding - void add(Key key); + Binding& add(Key key); // adds a button to the binding - void add(BoundButton button); + Binding& add(ButtonBind button); // adds an trigger to the binding - void add(BoundTrigger trigger); + Binding& add(TriggerBind trigger); // adds a mouse button to the binding - void add(MouseButton mouse); + Binding& add(MouseButton mouse); // adds an input to the binding template - void add(T first, T2 second, const Args&... args) + Binding& add(T first, T2 second, const Args&... args) { add(first); add(second, args...); + return *this; } + // adds the left trigger to the binding + Binding& add_left_trigger(int controller, float threshold); + + // adds the right trigger to the binding + Binding& add_right_trigger(int controller, float threshold); + // assigns all the bindings to the specific controller - void set_controller(int index); + Binding& set_controller(int index); // removes all bindings void clear(); @@ -169,6 +179,14 @@ namespace Blah // How to handle overlaps (ex. Left and Right are both held) Overlap overlap = Overlap::Newer; + AxisBinding() = default; + + AxisBinding(const Binding& negative, const Binding& positive, Overlap overlap = Overlap::Newer) + : negative(negative) + , positive(positive) + , overlap(overlap) + {} + // Current Value from -1 to 1 float value() const; @@ -186,20 +204,21 @@ namespace Blah // Adds a negative & positive binding pair template - void add(NegativeT negative, PositiveT positive) + AxisBinding& add(NegativeT negative, PositiveT positive) { this->negative.add(negative); this->positive.add(positive); + return *this; } // Adds a Stick binding - void add_left_stick_x(int controller, float threshold); - void add_left_stick_y(int controller, float threshold); - void add_right_stick_x(int controller, float threshold); - void add_right_stick_y(int controller, float threshold); + AxisBinding& add_left_stick_x(int controller, float threshold); + AxisBinding& add_left_stick_y(int controller, float threshold); + AxisBinding& add_right_stick_x(int controller, float threshold); + AxisBinding& add_right_stick_y(int controller, float threshold); // assigns all the bindings to the specific controller - void set_controller(int index); + AxisBinding& set_controller(int index); // Clears all Bindings void clear(); @@ -209,15 +228,23 @@ namespace Blah { public: - // An optional threshold for circular thresholds - float round_threshold = 0.0f; - // X Axis Binding AxisBinding x; // Y Axis Binding AxisBinding y; + // An optional threshold for circular thresholds + float round_threshold = 0.0f; + + StickBinding() = default; + + StickBinding(const AxisBinding& x, const AxisBinding& y, float round_threshold = 0) + : x(x) + , y(y) + , round_threshold(round_threshold) + {} + // Current Value, -1 to 1 Vec2 value() const; @@ -235,25 +262,26 @@ namespace Blah // Adds directional bindings template - void add(LeftT left, RightT right, UpT up, DownT down) + StickBinding& add(LeftT left, RightT right, UpT up, DownT down) { x.negative.add(left); x.positive.add(right); y.negative.add(up); y.positive.add(down); + return *this; } // Adds the dpad binding - void add_dpad(int controller); + StickBinding& add_dpad(int controller); // Adds the left stick binding - void add_left_stick(int controller, float threshold); + StickBinding& add_left_stick(int controller, float threshold); // Adds the right stick binding - void add_right_stick(int controller, float threshold); + StickBinding& add_right_stick(int controller, float threshold); // assigns all the bindings to the specific controller - void set_controller(int index); + StickBinding& set_controller(int index); // Clears all the bindings void clear(); diff --git a/include/blah/input/binding_registry.h b/include/blah/input/binding_registry.h index 73c7660..d7ca72f 100644 --- a/include/blah/input/binding_registry.h +++ b/include/blah/input/binding_registry.h @@ -17,13 +17,13 @@ namespace Blah { public: // registers a new binding - static BindingRef register_binding(); + static BindingRef register_binding(const Binding& binding = Binding()); // registers a new axis binding - static AxisBindingRef register_axis(); + static AxisBindingRef register_axis(const AxisBinding& binding = AxisBinding()); // registers a new stick binding - static StickBindingRef register_stick(); + static StickBindingRef register_stick(const StickBinding& binding = StickBinding()); // updates all the bindings. This is called // automatically by the App loop. diff --git a/src/input/binding.cpp b/src/input/binding.cpp index dbb4385..595df6f 100644 --- a/src/input/binding.cpp +++ b/src/input/binding.cpp @@ -4,19 +4,19 @@ using namespace Blah; -BoundTrigger::BoundTrigger(Axis axis) +Binding::TriggerBind::TriggerBind(Axis axis) : axis(axis) { } -BoundTrigger::BoundTrigger(int controller, Axis axis, float threshold, bool positive) +Binding::TriggerBind::TriggerBind(int controller, Axis axis, float threshold, bool positive) : controller(controller), axis(axis), threshold(threshold), positive(positive) { } -bool BoundTrigger::is_down(float axis_value) const +bool Binding::TriggerBind::is_down(float axis_value) const { if ((axis_value > 0 && positive) || (axis_value < 0 && !positive)) { @@ -27,60 +27,10 @@ bool BoundTrigger::is_down(float axis_value) const return false; } -BoundTrigger BoundTrigger::left_stick_left(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::LeftX, threshold, false); -} - -BoundTrigger BoundTrigger::left_stick_right(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::LeftX, threshold, true); -} - -BoundTrigger BoundTrigger::left_stick_up(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::LeftY, threshold, false); -} - -BoundTrigger BoundTrigger::left_stick_down(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::LeftY, threshold, true); -} - -BoundTrigger BoundTrigger::right_stick_left(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::RightX, threshold, false); -} - -BoundTrigger BoundTrigger::right_stick_right(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::RightX, threshold, true); -} - -BoundTrigger BoundTrigger::right_stick_up(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::RightY, threshold, false); -} - -BoundTrigger BoundTrigger::right_stick_down(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::RightY, threshold, true); -} - -BoundTrigger BoundTrigger::left_trigger(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::LeftTrigger, threshold, true); -} - -BoundTrigger BoundTrigger::right_trigger(int controller, float threshold) -{ - return BoundTrigger(controller, Axis::RightTrigger, threshold, true); -} - -BoundButton::BoundButton(Button button) +Binding::ButtonBind::ButtonBind(Button button) : button(button) {} -BoundButton::BoundButton(int controller, Button button) +Binding::ButtonBind::ButtonBind(int controller, Button button) : controller(controller), button(button) {} bool Binding::pressed() const @@ -167,32 +117,50 @@ void Binding::consume_release() m_last_release_time = -1; } -void Binding::add(Key key) +Binding& Binding::add(Key key) { keys.push_back(key); + return *this; } -void Binding::add(BoundButton button) +Binding& Binding::add(ButtonBind button) { buttons.push_back(button); + return *this; } -void Binding::add(BoundTrigger trigger) +Binding& Binding::add(TriggerBind trigger) { triggers.push_back(trigger); + return *this; } -void Binding::add(MouseButton button) +Binding& Binding::add(MouseButton button) { mouse.push_back(button); + return *this; } -void Binding::set_controller(int index) +Binding& Binding::add_left_trigger(int controller, float threshold) +{ + triggers.push_back(TriggerBind(controller, Axis::LeftTrigger, threshold, true)); + return *this; +} + +Binding& Binding::add_right_trigger(int controller, float threshold) +{ + triggers.push_back(TriggerBind(controller, Axis::RightTrigger, threshold, true)); + return *this; +} + +Binding& Binding::set_controller(int index) { for (auto& it : buttons) it.controller = index; for (auto& it : triggers) it.controller = index; + + return *this; } void Binding::clear() @@ -325,7 +293,7 @@ float Binding::get_value() const highest = mapped_value; } } - + return highest; } @@ -333,7 +301,7 @@ float AxisBinding::value() const { float neg = negative.value(); float pos = positive.value(); - + // neither are down if (neg <= 0 && pos <= 0) return 0; @@ -365,7 +333,7 @@ float AxisBinding::value() const if (negative.timestamp() > positive.timestamp()) return -neg; else - return pos; + return pos; } int AxisBinding::sign() const @@ -391,34 +359,39 @@ void AxisBinding::consume_release() positive.consume_release(); } -void AxisBinding::add_left_stick_x(int controller, float threshold) +AxisBinding& AxisBinding::add_left_stick_x(int controller, float threshold) { - negative.add(BoundTrigger::left_stick_left(controller, threshold)); - positive.add(BoundTrigger::left_stick_right(controller, threshold)); + negative.add(Binding::TriggerBind(controller, Axis::LeftX, threshold, false)); + positive.add(Binding::TriggerBind(controller, Axis::LeftX, threshold, true)); + return *this; } -void AxisBinding::add_left_stick_y(int controller, float threshold) +AxisBinding& AxisBinding::add_left_stick_y(int controller, float threshold) { - negative.add(BoundTrigger::left_stick_up(controller, threshold)); - positive.add(BoundTrigger::left_stick_down(controller, threshold)); + negative.add(Binding::TriggerBind(controller, Axis::LeftY, threshold, false)); + positive.add(Binding::TriggerBind(controller, Axis::LeftY, threshold, true)); + return *this; } -void AxisBinding::add_right_stick_x(int controller, float threshold) +AxisBinding& AxisBinding::add_right_stick_x(int controller, float threshold) { - negative.add(BoundTrigger::right_stick_left(controller, threshold)); - positive.add(BoundTrigger::right_stick_right(controller, threshold)); + negative.add(Binding::TriggerBind(controller, Axis::RightX, threshold, false)); + positive.add(Binding::TriggerBind(controller, Axis::RightX, threshold, true)); + return *this; } -void AxisBinding::add_right_stick_y(int controller, float threshold) +AxisBinding& AxisBinding::add_right_stick_y(int controller, float threshold) { - negative.add(BoundTrigger::right_stick_up(controller, threshold)); - positive.add(BoundTrigger::right_stick_down(controller, threshold)); + negative.add(Binding::TriggerBind(controller, Axis::RightY, threshold, false)); + positive.add(Binding::TriggerBind(controller, Axis::RightY, threshold, true)); + return *this; } -void AxisBinding::set_controller(int index) +AxisBinding& AxisBinding::set_controller(int index) { negative.set_controller(index); positive.set_controller(index); + return *this; } void AxisBinding::clear() @@ -459,30 +432,34 @@ void StickBinding::consume_release() y.consume_release(); } -void StickBinding::add_dpad(int controller) +StickBinding& StickBinding::add_dpad(int controller) { - x.negative.add(BoundButton(controller, Button::Left)); - x.positive.add(BoundButton(controller, Button::Right)); - y.negative.add(BoundButton(controller, Button::Up)); - y.positive.add(BoundButton(controller, Button::Down)); + x.negative.add(Binding::ButtonBind(controller, Button::Left)); + x.positive.add(Binding::ButtonBind(controller, Button::Right)); + y.negative.add(Binding::ButtonBind(controller, Button::Up)); + y.positive.add(Binding::ButtonBind(controller, Button::Down)); + return *this; } -void StickBinding::add_left_stick(int controller, float threshold) +StickBinding& StickBinding::add_left_stick(int controller, float threshold) { x.add_left_stick_x(controller, threshold); y.add_left_stick_y(controller, threshold); + return *this; } -void StickBinding::add_right_stick(int controller, float threshold) +StickBinding& StickBinding::add_right_stick(int controller, float threshold) { x.add_right_stick_x(controller, threshold); y.add_right_stick_y(controller, threshold); + return *this; } -void StickBinding::set_controller(int index) +StickBinding& StickBinding::set_controller(int index) { x.set_controller(index); y.set_controller(index); + return *this; } void StickBinding::clear() diff --git a/src/input/binding_registry.cpp b/src/input/binding_registry.cpp index b4fbb7e..43ec290 100644 --- a/src/input/binding_registry.cpp +++ b/src/input/binding_registry.cpp @@ -6,25 +6,25 @@ Vector> BindingRegistry::bindings; Vector> BindingRegistry::axes; Vector> BindingRegistry::sticks; -BindingRef BindingRegistry::register_binding() +BindingRef BindingRegistry::register_binding(const Binding& binding) { - auto binding = std::make_shared(); - bindings.push_back(std::weak_ptr(binding)); - return binding; + auto result = std::make_shared(binding); + bindings.push_back(std::weak_ptr(result)); + return result; } -AxisBindingRef BindingRegistry::register_axis() +AxisBindingRef BindingRegistry::register_axis(const AxisBinding& binding) { - auto binding = std::make_shared(); - axes.push_back(std::weak_ptr(binding)); - return binding; + auto result = std::make_shared(binding); + axes.push_back(std::weak_ptr(result)); + return result; } -StickBindingRef BindingRegistry::register_stick() +StickBindingRef BindingRegistry::register_stick(const StickBinding& binding) { - auto binding = std::make_shared(); - sticks.push_back(std::weak_ptr(binding)); - return binding; + auto result = std::make_shared(binding); + sticks.push_back(std::weak_ptr(result)); + return result; } void BindingRegistry::update()