The right / wrong use of delegates to achieve extensibility

I am trying to provide users with my graphics library with unlimited customization of transition / exit effects, while preserving simplicity / preventing misuse (when the control enters or exits the view).

To do this, I added a delegate to the Control class, which will take the control link and the percentage of completion of the transition, so that the user can smoothly translate the control position / opacity in any way that he would like, based on this percentage. All he needs to do is subscribe to the transition function before entering / exiting control.

However, I realized that it would not be possible to translate / animate the controls using only the current completion percentage, because you would need to save and compare the starting position of the control. To make this storage requirement obvious, should I use a delegate functor?

If so, how can I do this in a minimalist / clean way?


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

+7
source share
3 answers

If you understand correctly, your Control calls the delegate of the animation (calculation) (from time to time, possibly on each frame) and passes the percentage of transition. The animation delegate then calculates and returns / applies the translation and position to the control. Is it correct?

Assuming this is true, there are several solutions:

  • When animating only position and opacity:

    In addition to the percentage of competition, you should also send the initial state of the management position and opacity when calling the delegate. The initial state should be remembered at the beginning of the transition and sent to the delegate in each call.

  • When animating arbitrary properties in general:

    In addition to the percentage of competition, you also provide state ownership (object type or even the best dictionary). This State property is fully controlled by the delegate and its animation logic.

    The State property has no semantics or values ​​in its control. Your control MUST only keep the value of the State property between subsequent calls for delegation.

    Combining everything together, the delegate fills the state with initial values ​​on the first call, uses these values ​​on subsequent calls - does everything he wants. The delegate also applies the calculated values ​​to the control. Please note that all properties that can be used in a delegate must be publicly available.

+1
source

IMO, you do not need to provide the control user with the initial position of the control, since he can position it relative to the initial position: negative numbers for the left and top, and positive numbers for the right and bottom.

The following code is a quick jump function:

Point FastTranDiagonial(float Percentage){ Thread.Sleep(10); int pixelsDist = (1 - Percentage)* 300;//300 is the maximum distance return new Point(-pixelsDist ,pixelsDist); } 

When invoking the delegate, you must add Point to the original position of the control. You should notice that the delegate contains Thread.Sleep (X), it must control the user as he may want to execute a fast or slow transaction.

You can also consider adding sequential transitions, such as jQuery, so that one transition begins after another termination.

luck

+1
source

I think you need to pass at least the following parameters to the delegate: -

  • the control itself
  • a container containing the control (for example, a panel)
  • completion percentage

Having passed the control itself, the user will have all of his initial state information (for example, position). In addition, if the user needs to set some property of the control, he will definitely need a link to the control itself.

The user may need a container if he needs his size / location information, or if he needs to do something special for this control.

0
source

All Articles