Change ToggleButton's state without triggering EventSelectStateChanged?
Posted: Thu Mar 29, 2018 07:33
...or "how to tell in a callback whether the state was changed through code or by a user?"
Your first though is: "if he's asking this question, he must be structuring something wrong"... - maybe? Please let me know!
As some background: I need to save user interactions so I can "play them back later", which works fine in 99% of my application. My general approach is that when the user performs any interactions I don't perform the action right away, I save them into a queue of actions which both gets serialized as well as getting performed and cleared out at the end of each frame. Eg: if the user clicks the [Beep] button I save a "DO_BEEP" action into a queue, then at the end of the frame I serialize the queue to disk as well as performing all the actions so the "beep" gets played then.
In the case I'm having problems with I have a ToggleButton - when the user clicks it I get a "state changed" callback, so I record the fact that it was set to a certain value (true/false). At the end of the frame I save this and also play back eg: "SET_TOGGLE:TRUE" - when I play this back the first thing I do is update the UI state of the toggle (in case it needs it) and then perform the action that's supposed to happen when it's toggled.
My problem is that when I update the UI state I call something like myToggleButton->setSelected(True); - this then calls the callback which creates another action and saves it to the queue! (so it tries to play back again next frame as well).
If I put enough "check the value's not the right value already" around things I can get it to work most of the time, but it's very reliant on making sure things are synced up in the first place, and restarting things or playing a replay tends to start with them out of sync.
My solution would be a lot more robust if I could update the button state in code without triggering the callback, or if I could tell from within the callback if it was triggered by the user or me calling it through code...
Any thoughts?
Your first though is: "if he's asking this question, he must be structuring something wrong"... - maybe? Please let me know!
As some background: I need to save user interactions so I can "play them back later", which works fine in 99% of my application. My general approach is that when the user performs any interactions I don't perform the action right away, I save them into a queue of actions which both gets serialized as well as getting performed and cleared out at the end of each frame. Eg: if the user clicks the [Beep] button I save a "DO_BEEP" action into a queue, then at the end of the frame I serialize the queue to disk as well as performing all the actions so the "beep" gets played then.
In the case I'm having problems with I have a ToggleButton - when the user clicks it I get a "state changed" callback, so I record the fact that it was set to a certain value (true/false). At the end of the frame I save this and also play back eg: "SET_TOGGLE:TRUE" - when I play this back the first thing I do is update the UI state of the toggle (in case it needs it) and then perform the action that's supposed to happen when it's toggled.
My problem is that when I update the UI state I call something like myToggleButton->setSelected(True); - this then calls the callback which creates another action and saves it to the queue! (so it tries to play back again next frame as well).
If I put enough "check the value's not the right value already" around things I can get it to work most of the time, but it's very reliant on making sure things are synced up in the first place, and restarting things or playing a replay tends to start with them out of sync.
My solution would be a lot more robust if I could update the button state in code without triggering the callback, or if I could tell from within the callback if it was triggered by the user or me calling it through code...
Any thoughts?