What is the proper way of handling synchronization between GUI animations' execution order and the user's dynamic input?
More specifically, I would like to know how to enforce some logical conditioning on the execution's order, while remembering, that the user's input cannot be foreseen.
For instance, a greatly simplified example:
- 9 rectangles are shown on a screen.
- A click on a rectangle fades it out.
- The user clicks whichever rectangle he wants in whatever order he likes and as fast as he feels is right.
- The condition here breaks into two simple ones:
- The rectangles must not fade out simultaneously.
- The rectangles' fade out effect occurs in the same order as their clicks.
I would assume a simple FIFO methodology here where threads would register themselves in some ordered queue and be allowed to enter the critical code only on their turn would be sufficient.
A slightly more complicated scenario:
- 8 rectangles are shown on a screen - the 9th is an invisible rectangle.
- A click on a rectangle that is a neighbor of the invisible one, activates a translate animation that swipes between the two.
- The user clicks whichever rectangle he wants in whatever order he likes and as fast as he feels is right.
- A "shuffle" button is shown on the screen - shuffles the rectangles in a legal manner, i.e., by randomly moving the rectangles many times.
- The conditions:
- The rectangles must not get swiped simultaneously when clicked directly, their animations must be played in the order they were clicked.
- The rectangles must get swiped simultaneously when the "shuffle" button is clicked. Meaning, the user must only see each rectangle move to its final position.
- Some constraints:
- Each rectangle has a
Coordinate
field that itself containsxCoordinate
&yCoordinate
. - Checking if the clicked rectangle is a neighbor of the invisible one will be done by comparing the rectangles'
Coordinate
s and not their positioning on the screen, i.e.,translateX
,translateY
. - If they are neighbors: the rectangles swipe places.
- Else: the rectangle shows a nudge animation (as in invalid move).
- Only after the animation is finished, rectangles'
Coordinates
can be swiped. - Each process (containing the needed steps after a click) apart from the animation itself, should be handled in a different thread.
- Each rectangle has a
A question: If the Coordinates
can be swiped only after the animation is done, meaning each click activates a thread that holds all other threads(clicks that came after) until it is done so it can swipe the coordinates so the next thread's neighbor check is based on correct data, how would it be possible to allow the "shuffle option" without creating an entirely different execution path, i.e., by reusing most of the already written code and leveraging the Java & JavaFX's strengths?
What if we add a constraint that only every second click should be executed?
My point being, these problems can appear in various ways. There must be someone who thought of providing some kind of framework or an abstract solution to this kind of problems. Or is it each case for itself?