From 333a7b73ac7a20aa39de2241effcd3549273c9b6 Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 8 Nov 2023 20:00:48 -0500 Subject: [PATCH] pad object can be disabled and/or hidden --- src/Pad.hpp | 87 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 24 deletions(-) diff --git a/src/Pad.hpp b/src/Pad.hpp index 20e9a13..54c2b0d 100644 --- a/src/Pad.hpp +++ b/src/Pad.hpp @@ -51,6 +51,7 @@ namespace sb sb::Plane _plane; Box box; int texture_index = 0; + bool _enabled = true, _visible = true; public: @@ -224,45 +225,56 @@ namespace sb */ void draw(GLuint transformation_uniform, glm::mat4 view, glm::mat4 projection, std::optional texture_flag_uniform = std::nullopt) { - if (!_plane.textures().empty()) + if (_visible) { - if (texture_flag_uniform.has_value()) + if (!_plane.textures().empty()) { - glUniform1i(texture_flag_uniform.value(), true); - } + if (texture_flag_uniform.has_value()) + { + glUniform1i(texture_flag_uniform.value(), true); + } - /* Determine texture index by checking the state of the pad and the amount of available textures. If there is more than 1 texture, - * the texture will correspond with the state. */ - if (connection && _plane.textures().size() > 1) - { - texture_index = 1; + /* Determine texture index by checking the state of the pad and the amount of available textures. If there is more than 1 texture, + * the texture will correspond with the state. */ + if (connection && _plane.textures().size() > 1) + { + texture_index = 1; + } + else + { + texture_index = 0; + } + _plane.texture(texture_index).bind(); } - else + else if (texture_flag_uniform.has_value()) { - texture_index = 0; + glUniform1i(texture_flag_uniform.value(), false); } - _plane.texture(texture_index).bind(); + glm::mat4 mvp = projection * view * _plane.transformation(); + glUniformMatrix4fv(transformation_uniform, 1, GL_FALSE, &mvp[0][0]); + _plane.enable(); + glDrawArrays(GL_TRIANGLES, 0, _plane.attributes("position")->count()); + _plane.disable(); } - else if (texture_flag_uniform.has_value()) - { - glUniform1i(texture_flag_uniform.value(), false); - } - glm::mat4 mvp = projection * view * _plane.transformation(); - glUniformMatrix4fv(transformation_uniform, 1, GL_FALSE, &mvp[0][0]); - _plane.enable(); - glDrawArrays(GL_TRIANGLES, 0, _plane.attributes("position")->count()); - _plane.disable(); } /*! * Run the reaction function. * - * @param args arguments to pass to the reaction function - * @return result of the reaction function if it returns a value, or void otherwise + * @param args Arguments to pass to the reaction function + * @exception std::runtime_error if the pad is currently disabled + * @return Result of the reaction function if it returns a value, or void otherwise */ ReturnType press(Arguments... args) { - return connection.flip(args...); + if (!_enabled) + { + throw std::runtime_error("The pad cannot be pressed because it is currently disabled. Please check Pad::enabled before calling Pad::press."); + } + else + { + return connection.flip(args...); + } } /*! @@ -290,5 +302,32 @@ namespace sb { return _plane.size(); } + + /*! + * By default, a pad object is enabled to accept input presses. If the pad is disabled, however, the press function will not be able to be + * used and will throw an exception. This function can be used both to check the state and to set the state. + * + * @param state Set to false to disable, true to enable, or omit to check the current state + * @return True if button is set to enabled + */ + bool enabled(std::optional state = std::nullopt) + { + if (state.has_value()) + { + _enabled = state.value(); + } + return _enabled; + } + + /*! + * Use this function to prevent the draw function from running, which will prevent the pad object from being rendered. Note that this does + * not disable input. To do that, use Pad::enabled(bool) + * + * @param state Set to false to prevent the pad from being drawn, set to true to re-enable drawing + */ + void visible(bool state = true) + { + _visible = state; + } }; }