Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct/Incorrect usage of delegates for achieving extensibility

I'm trying to give users of my GUI library unrestricted customization of in/out transition effects while still maintaining simplicity / preventing misuse (for when a Control enters or exits the view).

To do so, I added a delegate to the Control class, which would take a Control reference and a transition-completion percent, so that the user would be able to smoothly translate a control's position / opacity in any way he wanted, based on the given percent. All he'd have to do is subscribe a transition function before control entrance/exit.

However, I realized it would be impossible to transition / animate the controls using only the current completion percent, because you'd have to store and compare the control's initial position as well.
In order make this storage requirement apparent, should I force usage of a delegate-functor?

If so, how might I do that in a minimalistic / clean way?


Feel free to suggest another way to allow users to apply custom transition animations!

like image 229
Griffin Avatar asked May 20 '12 00:05

Griffin


1 Answers

If I understood you correctly, your Control invokes Animation(calculation) delegate (from time to time, probably on each frame) and passes transition Competition percent. The Animation delegate then calculates and returns/applies translation and position to the Control. Is this correct?

Assuming that above is correct there are several solutions:

  1. When animating only position and opacity:

    Beside competition percent, you must also send initial state of control's position and opacity when calling delegate. Initial state must be remembered on the transition start and sent into delegate in each call.

  2. When animating arbitrarily properties in general:

    Beside competition percent, you also provide State property (type of Object or even better Dictionary). This State property is fully controlled by delegate and it's animation logic.

    To your Control, State property would not have any semantics or meaning. Your Control only MUST retain value of State property between subsequent calls to delegate.

    Putting it all together, The Delegate fills the State with the initial values on the first call, uses these values on subsequent calls - does anything it wants. Delegate also applies calculated values to Control. Note that all properties that can be used in delegate must be public.

like image 180
Dusan Avatar answered Sep 24 '22 08:09

Dusan