In the C family of languages I usually implement a state machine as a series of if else statements and enums, where if statements check in which state the machine is and the bodies execute state transitions, for example this:
if(current_left_state == GLFW_PRESS && !left_pressed)
{
left_pressed = true;
return MouseInputState::LEFT_DOWN;
}
if(current_left_state == GLFW_PRESS && left_pressed)
{
left_pressed = true;
return MouseInputState::LEFT_DRAG;
}
if(current_left_state == GLFW_RELEASE && left_pressed)
{
left_pressed = false;
return MouseInputState::LEFT_UP;
}
if(current_right_state == GLFW_PRESS && !right_pressed)
{
right_pressed = true;
return MouseInputState::RIGHT_DOWN;
}
Rust has a lot of idiomatic sugar which is nice, I was wondering if there is a way to use rust's syntactic sugar to make cleaner state machines.
Like, there has got to be a better way than this:
MouseState::NoAction =>
{
if *button == glfw::MouseButtonLeft && *action == glfw::Action::Press
{
return MouseState::LeftDown;
}
if *button == glfw::MouseButtonRight && *action == glfw::Action::Press
{
return MouseState::RightDown;
}
return MouseState::NoAction;
}
MouseState::LeftDown =>
{
if *button == glfw::MouseButtonLeft && *action == glfw::Action::Release
{
return MouseState::LeftUp;
}
return MouseState::LeftDrag;
}
MouseState::LeftDrag =>
{
if *button == glfw::MouseButtonLeft && *action == glfw::Action::Release
{
return MouseState::LeftUp;
}
return MouseState::LeftDrag;
}
MouseState::LeftUp =>
{
if *button == glfw::MouseButtonLeft && *action == glfw::Action::Press
{
return MouseState::LeftUp;
}
return MouseState::NoAction;
}