(→Script Files) |
Neverendingo (Talk | contribs) m (Text replace - "</code>" to "</syntaxhighlight>") |
||
| (4 intermediate revisions by 2 users not shown) | |||
| Line 5: | Line 5: | ||
== Writing An Animation == | == Writing An Animation == | ||
| − | The key components of a Javascript animation are the constructor function, the updateCurrentTime function and the registerAnimation call. | + | The key components of a Javascript animation are the '''constructor''' function, the '''updateCurrentTime''' function and the '''registerAnimation''' call. |
| − | The constructor function is called whenever a new instance of the animation is requested. The name of the function is not critical. | + | The constructor function is called whenever a new instance of the animation is requested. The name of the function is not critical, but it is used in the registerAnimation call. |
The updateCurrentTime function drives the actual animation: it is called whenever an animation frame is to be processed. The current time of the animation is passed in to it as the first parameter. | The updateCurrentTime function drives the actual animation: it is called whenever an animation frame is to be processed. The current time of the animation is passed in to it as the first parameter. | ||
| Line 15: | Line 15: | ||
A simple example of a zoom animation: | A simple example of a zoom animation: | ||
| − | < | + | <syntaxhighlight lang="javascript"> |
// constructor function for a zoom animation | // constructor function for a zoom animation | ||
function ZoomAnimation() { | function ZoomAnimation() { | ||
| Line 28: | Line 28: | ||
registerAnimation("ZoomAnimation", ZoomAnimation) | registerAnimation("ZoomAnimation", ZoomAnimation) | ||
| − | </ | + | </syntaxhighlight> |
== Script Files == | == Script Files == | ||
| Line 37: | Line 37: | ||
To make Plasma aware of the animations, create an [Animations] section in the theme's metadata.desktop file. This group must contain mappings between the script name and the animations the script contains. For example: | To make Plasma aware of the animations, create an [Animations] section in the theme's metadata.desktop file. This group must contain mappings between the script name and the animations the script contains. For example: | ||
| − | < | + | <syntaxhighlight lang="ini"> |
[Animations] | [Animations] | ||
stockanims.js=ZoomAnimation,PulseAnimation | stockanims.js=ZoomAnimation,PulseAnimation | ||
rotation.js=RotationAnimation | rotation.js=RotationAnimation | ||
| − | </ | + | </syntaxhighlight> |
The above example would associate the file in {{path|animations/stockanim.js}} with the ZoomAnimation and PulseAnimation animations and rotation.js with the RotationAnimation animation. Whenever these animations were requested by name, they would be loaded from the respective script and an Animation object would be returned. | The above example would associate the file in {{path|animations/stockanim.js}} with the ZoomAnimation and PulseAnimation animations and rotation.js with the RotationAnimation animation. Whenever these animations were requested by name, they would be loaded from the respective script and an Animation object would be returned. | ||
| Line 54: | Line 54: | ||
To make Plasma aware of the animations, create an [Animations] section in the Plasmoid's metadata.desktop file. This group must contain mappings between the script name and the animations the script contains. For example: | To make Plasma aware of the animations, create an [Animations] section in the Plasmoid's metadata.desktop file. This group must contain mappings between the script name and the animations the script contains. For example: | ||
| − | < | + | <syntaxhighlight lang="ini"> |
[Animations] | [Animations] | ||
stockanims.js=ZoomAnimation,PulseAnimation | stockanims.js=ZoomAnimation,PulseAnimation | ||
rotation.js=RotationAnimation | rotation.js=RotationAnimation | ||
| − | </ | + | </syntaxhighlight> |
| − | The above example would associate the file in {{path|contents/animations/stockanim.js}} with the ZoomAnimation and PulseAnimation animations and rotation.js with the RotationAnimation animation. Whenever these animations were requested by name, they would be loaded from the respective script and an Animation object would be returned. | + | The above example would associate the file in {{path|contents/animations/stockanim.js}} with the ZoomAnimation and PulseAnimation animations and rotation.js with the RotationAnimation animation. Whenever these animations were requested by name, they would be loaded from the respective script and an Animation object would be returned: |
| + | |||
| + | <syntaxhighlight lang="javascript"> | ||
| + | var button = new PushButton | ||
| + | button.text = i18n("Rotate me!") | ||
| + | var anim = animation("RotationAnimation") | ||
| + | anim.targetWidget = button | ||
| + | button.clicked.connet(anim.start) | ||
| + | </syntaxhighlight> | ||
=== In Plasma Applications === | === In Plasma Applications === | ||
Contents |
Plasma builds on top of the QtKinetic animation framework to provide a set of ready-made animations. Starting with KDE Development Platform v4.5, it is also possible to write new animations or mash up existing ones using Javascript. This tutorial describes how Javascript animations are defined, where they are stored and how you can access them from your code.
The key components of a Javascript animation are the constructor function, the updateCurrentTime function and the registerAnimation call.
The constructor function is called whenever a new instance of the animation is requested. The name of the function is not critical, but it is used in the registerAnimation call.
The updateCurrentTime function drives the actual animation: it is called whenever an animation frame is to be processed. The current time of the animation is passed in to it as the first parameter.
To let Plasma know that the animation exists, the registerAnimation function must be called from the script's global scope. registerAnimation takes two parameters: the name by which the animation will be identified and the constructor function.
A simple example of a zoom animation:
// constructor function for a zoom animation function ZoomAnimation() { // we could do some set up here // updateCurrentTime this.updateCurrentTime = function(currentTime) { var delta = currentTime/this.duration; this.target.scale = delta; } } registerAnimation("ZoomAnimation", ZoomAnimation)
A Plasma desktop theme can include one or more JS scripts in the animations/ subdirectory, each of which can contain one or more animations.
To make Plasma aware of the animations, create an [Animations] section in the theme's metadata.desktop file. This group must contain mappings between the script name and the animations the script contains. For example:
[Animations] stockanims.js=ZoomAnimation,PulseAnimation rotation.js=RotationAnimation
The above example would associate the file in animations/stockanim.js with the ZoomAnimation and PulseAnimation animations and rotation.js with the RotationAnimation animation. Whenever these animations were requested by name, they would be loaded from the respective script and an Animation object would be returned.
Theme inheritance is respected and supported as well, so animations defined in inherited themes as well as the default fallback theme will be available if they are not overridden by an animation of the same name in the theme.
Animations with the same name as a value in the Plasma::Animator::Animation enumeration will be automatically mapped as a replacement for the native C++ animation that is usually returned by Plasma::Animator::create().
A scripted Plasmoid can include one or more JS scripts in the contents/animations/ subdirectory of the package. Each script can contain one or more animations.
To make Plasma aware of the animations, create an [Animations] section in the Plasmoid's metadata.desktop file. This group must contain mappings between the script name and the animations the script contains. For example:
[Animations] stockanims.js=ZoomAnimation,PulseAnimation rotation.js=RotationAnimation
The above example would associate the file in contents/animations/stockanim.js with the ZoomAnimation and PulseAnimation animations and rotation.js with the RotationAnimation animation. Whenever these animations were requested by name, they would be loaded from the respective script and an Animation object would be returned:
var button = new PushButton button.text = i18n("Rotate me!") var anim = animation("RotationAnimation") anim.targetWidget = button button.clicked.connet(anim.start)
In the Plasma::Animator class there is a create(const QString &animationName, QObject *parent = 0) method which returns a QAbstractAnimation that can be used like any other. The animationName parameter is the name of the Javascript animation as defined at animation registration. If no such animation is available, a null pointer will be returned.
Javascript animations can be retrieved like any other animation: by name using the animation(String) method.
The following API is available to Javascript animations.
An Animation object can be created by calling the Animation animation(string type) function. The string corresponds to the type of animation, which are listed below.
By setting the widgetToAnimate property it can be assigned to animate any QGraphicsWidget desired.
All Animation objects have the following enumerations:
The reference point, relative to the target widget, for the animation.
With the exception of Pause and Property animations, all animations support the following read/write properties:
Animations may be put into groups for convenient sequential or parallel running. Sequential groups, where the animations run one after the other, are handled by the AnimationGroup class. Parallel aniations, where the animations run simultaneously, are handled the ParallelAnimationGroup class.
Animations are added to a group by calling add(Animation) on the group object. Groups may also be added to other groups.
Below is a list of all current animation types and their particular read/write properties:
Animates an object (must be a QObject internally, which includes all Plasma Widgets) by manipulating one of its properties. Property animations have the following read/write properties:
Used to set the progress shape of Animation objects, this class has the following read-only properties:
as well as the following read/write property:
and the following enumeration: