pad object can be disabled and/or hidden
This commit is contained in:
parent
a91bc4b773
commit
333a7b73ac
87
src/Pad.hpp
87
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<GLuint> 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<bool> 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue