Commit 0bbb25b2 authored by Viktor Lidholt's avatar Viktor Lidholt

Adds missing Flutter Sprites API docs and improved README (#4022)

parent 880f2f78
This diff is collapsed.
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// Signature for callbacks used by the [ActionCallFunction].
typedef void ActionCallback(); typedef void ActionCallback();
/// Actions are used to animate properties of nodes or any other type of /// Actions are used to animate properties of nodes or any other type of
...@@ -34,19 +39,21 @@ abstract class Action { ...@@ -34,19 +39,21 @@ abstract class Action {
_finished = false; _finished = false;
} }
/// The total time it will take to complete the action, in seconds.
double get duration => 0.0; double get duration => 0.0;
} }
/// Signature for callbacks for setting properties, used by [ActionTween].
typedef void SetterCallback(dynamic value); typedef void SetterCallback(dynamic value);
/// The abstract class for an action that changes properties over a time /// The abstract class for an action that changes properties over a time
/// interval, optionally using an easing curve. /// interval, optionally using an easing curve.
abstract class ActionInterval extends Action { abstract class ActionInterval extends Action {
/// Creates a new ActionInterval, typically you will want to pass in a
/// [duration] to specify how long time the action will take to complete.
ActionInterval([this._duration = 0.0, this.curve]); ActionInterval([this._duration = 0.0, this.curve]);
/// The duration, in seconds, of the action.
///
/// double myTime = myAction.duration;
@override @override
double get duration => _duration; double get duration => _duration;
double _duration; double _duration;
...@@ -84,9 +91,13 @@ abstract class ActionInterval extends Action { ...@@ -84,9 +91,13 @@ abstract class ActionInterval extends Action {
} }
} }
/// An action that repeats an action a fixed number of times. /// An action that repeats another action a fixed number of times.
class ActionRepeat extends ActionInterval { class ActionRepeat extends ActionInterval {
/// The number of times the [action] is repeated.
final int numRepeats; final int numRepeats;
/// The action that is repeated.
final ActionInterval action; final ActionInterval action;
int _lastFinishedRepeat = -1; int _lastFinishedRepeat = -1;
...@@ -119,6 +130,8 @@ class ActionRepeat extends ActionInterval { ...@@ -119,6 +130,8 @@ class ActionRepeat extends ActionInterval {
/// An action that repeats an action an indefinite number of times. /// An action that repeats an action an indefinite number of times.
class ActionRepeatForever extends Action { class ActionRepeatForever extends Action {
/// The action that is repeated indefinitely.
final ActionInterval action; final ActionInterval action;
double _elapsedInAction = 0.0; double _elapsedInAction = 0.0;
...@@ -325,6 +338,8 @@ abstract class ActionInstant extends Action { ...@@ -325,6 +338,8 @@ abstract class ActionInstant extends Action {
_finished = true; _finished = true;
} }
/// Called when the action is executed. If you are implementing your own
/// ActionInstant, override this method.
void fire(); void fire();
} }
...@@ -546,6 +561,8 @@ class ActionController { ...@@ -546,6 +561,8 @@ class ActionController {
} }
} }
/// Steps the action forward by the specified time, typically there is no need
/// to directly call this method.
void step(double dt) { void step(double dt) {
for (int i = _actions.length - 1; i >= 0; i--) { for (int i = _actions.length - 1; i >= 0; i--) {
Action action = _actions[i]; Action action = _actions[i];
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
Point _cardinalSplineAt(Point p0, Point p1, Point p2, Point p3, double tension, double t) { Point _cardinalSplineAt(Point p0, Point p1, Point p2, Point p3, double tension, double t) {
...@@ -17,6 +21,7 @@ Point _cardinalSplineAt(Point p0, Point p1, Point p2, Point p3, double tension, ...@@ -17,6 +21,7 @@ Point _cardinalSplineAt(Point p0, Point p1, Point p2, Point p3, double tension,
return new Point(x, y); return new Point(x, y);
} }
/// Signature for callbacks used by the [ActionSpline] to set a [Point] value.
typedef void PointSetterCallback(Point value); typedef void PointSetterCallback(Point value);
/// The spline action is used to animate a point along a spline definied by /// The spline action is used to animate a point along a spline definied by
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A sequence of colors representing a gradient or a color transition over /// A sequence of colors representing a gradient or a color transition over
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A constraint limits or otherwise controls a [Node]'s properties, such as /// A constraint limits or otherwise controls a [Node]'s properties, such as
...@@ -139,8 +143,13 @@ class ConstraintPositionToNode extends Constraint { ...@@ -139,8 +143,13 @@ class ConstraintPositionToNode extends Constraint {
/// same parent, but they need to be added to the same [SpriteBox]. /// same parent, but they need to be added to the same [SpriteBox].
ConstraintPositionToNode(this.targetNode, {this.dampening, this.offset: Offset.zero}); ConstraintPositionToNode(this.targetNode, {this.dampening, this.offset: Offset.zero});
/// Target node to follow.
final Node targetNode; final Node targetNode;
/// Offset to the target node.
final Offset offset; final Offset offset;
/// Dampening used when following the [targetNode], value between 0.0 and 1.0.
final double dampening; final double dampening;
@override @override
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// Used by [EffectLine] to determine how the width of the line is calculated.
enum EffectLineWidthMode { enum EffectLineWidthMode {
/// Linear interpolation between minWidth at the start and maxWidth at the
/// end of the line.
linear, linear,
/// Creates a barrel shaped line, with minWidth at the end points of the line
/// and maxWidth at the middle.
barrel, barrel,
} }
/// Used by [EffectLine] to determine how the texture of the line is animated.
enum EffectLineAnimationMode { enum EffectLineAnimationMode {
/// The texture of the line isn't animated.
none, none,
/// The texture of the line is scrolling.
scroll, scroll,
/// The texture of the line is set to a random position at every frame. This
/// mode is useful for creating flashing or electricity styled effects.
random, random,
} }
/// The EffectLine class is using the [TexturedLine] class to draw animated
/// lines. These can be used to draw things such as smoke trails, electricity
/// effects, or other animated types of lines.
class EffectLine extends Node { class EffectLine extends Node {
/// Creates a new EffectLine with the specified parameters. Only the
/// [texture] parameter is required, all other parameters are optional.
EffectLine({ EffectLine({
this.texture: null, this.texture: null,
this.transferMode: TransferMode.dstOver, this.transferMode: TransferMode.dstOver,
...@@ -49,22 +73,41 @@ class EffectLine extends Node { ...@@ -49,22 +73,41 @@ class EffectLine extends Node {
_painter.textureLoopLength = textureLoopLength; _painter.textureLoopLength = textureLoopLength;
} }
/// The texture used to draw the line.
final Texture texture; final Texture texture;
/// The transfer mode used to draw the line, default is
/// [TransferMode.dstOver].
final TransferMode transferMode; final TransferMode transferMode;
/// Mode used to calculate the width of the line.
final EffectLineWidthMode widthMode; final EffectLineWidthMode widthMode;
/// The width of the line at its thinnest point.
final double minWidth; final double minWidth;
/// The width of the line at its thickest point.
final double maxWidth; final double maxWidth;
/// The speed at which the line is growing, defined in points per second.
final double widthGrowthSpeed; final double widthGrowthSpeed;
/// The mode used to animate the texture of the line.
final EffectLineAnimationMode animationMode; final EffectLineAnimationMode animationMode;
/// The speed of which the texture of the line is scrolling. This property
/// is only used if the [animationMode] is set to
/// [EffectLineAnimationMode.scroll].
final double scrollSpeed; final double scrollSpeed;
ColorSequence _colorSequence;
/// Color gradient used to draw the line, from start to finish.
ColorSequence get colorSequence => _colorSequence; ColorSequence get colorSequence => _colorSequence;
List<Point> _points; ColorSequence _colorSequence;
/// List of points that make up the line. Typically, you will only want to
/// set this at the beginning. Then use [addPoint] to add additional points
/// to the line.
List<Point> get points => _points; List<Point> get points => _points;
set points(List<Point> points) { set points(List<Point> points) {
...@@ -75,15 +118,26 @@ class EffectLine extends Node { ...@@ -75,15 +118,26 @@ class EffectLine extends Node {
} }
} }
List<Point> _points;
List<double> _pointAges; List<double> _pointAges;
List<Color> _colors; List<Color> _colors;
List<double> _widths; List<double> _widths;
/// The time it takes for an added point to fade out. It's total life time is
/// [fadeDuration] + [fadeAfterDelay].
final double fadeDuration; final double fadeDuration;
/// The time it takes until an added point starts to fade out.
final double fadeAfterDelay; final double fadeAfterDelay;
/// The length, in points, that the texture is stretched to. If the
/// textureLoopLength is shorter than the line, the texture will be looped.
final double textureLoopLength; final double textureLoopLength;
/// True if the line should be simplified by removing points that are close
/// to other points. This makes drawing faster, but can result in a slight
/// jittering effect when points are added.
final bool simplify; final bool simplify;
TexturedLinePainter _painter; TexturedLinePainter _painter;
...@@ -168,6 +222,7 @@ class EffectLine extends Node { ...@@ -168,6 +222,7 @@ class EffectLine extends Node {
_painter.paint(canvas); _painter.paint(canvas);
} }
/// Adds a new point to the end of the line.
void addPoint(Point point) { void addPoint(Point point) {
// Skip duplicate points // Skip duplicate points
if (points.length > 0 && point.x == points[points.length - 1].x && point.y == points[points.length - 1].y) if (points.length > 0 && point.x == points[points.length - 1].x && point.y == points[points.length - 1].y)
......
...@@ -4,12 +4,18 @@ ...@@ -4,12 +4,18 @@
part of flutter_sprites; part of flutter_sprites;
/// The ImageMap is a helper class for loading and keeping references to
/// multiple images.
class ImageMap { class ImageMap {
/// Creates a new ImageMap where images will be loaded from the specified
/// [bundle].
ImageMap(AssetBundle bundle) : _bundle = bundle; ImageMap(AssetBundle bundle) : _bundle = bundle;
final AssetBundle _bundle; final AssetBundle _bundle;
final Map<String, ui.Image> _images = new Map<String, ui.Image>(); final Map<String, ui.Image> _images = new Map<String, ui.Image>();
/// Loads a list of images given their urls.
Future<List<ui.Image>> load(List<String> urls) { Future<List<ui.Image>> load(List<String> urls) {
return Future.wait(urls.map(_loadImage)); return Future.wait(urls.map(_loadImage));
} }
...@@ -20,6 +26,9 @@ class ImageMap { ...@@ -20,6 +26,9 @@ class ImageMap {
return image; return image;
} }
/// Returns a preloaded image, given its [url].
ui.Image getImage(String url) => _images[url]; ui.Image getImage(String url) => _images[url];
/// Returns a preloaded image, given its [url].
ui.Image operator [](String url) => _images[url]; ui.Image operator [](String url) => _images[url];
} }
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// Labels are used to display a string of text in a the node tree. To align /// Labels are used to display a string of text in a the node tree. To align
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A [Node] that provides an intermediate rendering surface in the sprite /// A [Node] that provides an intermediate rendering surface in the sprite
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A NineSliceSprite is similar to a [Sprite], but it it can strech its /// A NineSliceSprite is similar to a [Sprite], but it it can strech its
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// Converts degrees to radians.
double convertDegrees2Radians(double degrees) => degrees * math.PI/180.8; double convertDegrees2Radians(double degrees) => degrees * math.PI/180.8;
/// Converts radians to degrees.
double convertRadians2Degrees(double radians) => radians * 180.0/math.PI; double convertRadians2Degrees(double radians) => radians * 180.0/math.PI;
/// A base class for all objects that can be added to the sprite node tree and rendered to screen using [SpriteBox] and /// A base class for all objects that can be added to the sprite node tree and rendered to screen using [SpriteBox] and
...@@ -428,7 +434,8 @@ class Node { ...@@ -428,7 +434,8 @@ class Node {
return _transformMatrixBoxToNode; return _transformMatrixBoxToNode;
} }
Matrix4 inverseTransformMatrix() { /// The inverse transform matrix used by this node.
Matrix4 get inverseTransformMatrix {
if (_transformMatrixInverse == null) { if (_transformMatrixInverse == null) {
_transformMatrixInverse = new Matrix4.copy(transformMatrix); _transformMatrixInverse = new Matrix4.copy(transformMatrix);
_transformMatrixInverse.invert(); _transformMatrixInverse.invert();
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// An node that transforms its children using a 3D perspective projection. This /// An node that transforms its children using a 3D perspective projection. This
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// The super class of any [Node] that has a size. /// The super class of any [Node] that has a size.
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
class _Particle { class _Particle {
...@@ -41,6 +45,8 @@ class _ParticleAccelerations { ...@@ -41,6 +45,8 @@ class _ParticleAccelerations {
/// number of particles can never exceed the [maxParticles] limit. /// number of particles can never exceed the [maxParticles] limit.
class ParticleSystem extends Node { class ParticleSystem extends Node {
/// Creates a new particle system with the given properties. The only
/// required parameter is the texture, all other parameters are optional.
ParticleSystem(this.texture, ParticleSystem(this.texture,
{this.life: 1.5, {this.life: 1.5,
this.lifeVar: 1.0, this.lifeVar: 1.0,
...@@ -208,6 +214,8 @@ class ParticleSystem extends Node { ...@@ -208,6 +214,8 @@ class ParticleSystem extends Node {
double _emitCounter; double _emitCounter;
int _numEmittedParticles = 0; int _numEmittedParticles = 0;
/// The over all opacity of the particle system. This value is multiplied by
/// the opacity of the individual particles.
double opacity = 1.0; double opacity = 1.0;
static Paint _paint = new Paint() static Paint _paint = new Paint()
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// An audio asset loaded by the SoundEffectPlayer. /// An audio asset loaded by the SoundEffectPlayer.
class SoundEffect { class SoundEffect {
/// Creates a new sound effect with the given sound id. Normally,
/// SoundEffect objects are created through the [SoundEffectPlayer].
SoundEffect(this._soundId); SoundEffect(this._soundId);
int _soundId; int _soundId;
...@@ -9,6 +16,9 @@ class SoundEffect { ...@@ -9,6 +16,9 @@ class SoundEffect {
/// A sound being played by the SoundEffectPlayer. /// A sound being played by the SoundEffectPlayer.
class SoundEffectStream { class SoundEffectStream {
/// Creates a new SoundEffectStream. Typically SoundEffectStream objects are
/// created by the SoundEffectPlayer.
SoundEffectStream(SoundEffectPlayer player, int streamId, { SoundEffectStream(SoundEffectPlayer player, int streamId, {
double leftVolume, double leftVolume,
double rightVolume, double rightVolume,
...@@ -27,10 +37,12 @@ class SoundEffectStream { ...@@ -27,10 +37,12 @@ class SoundEffectStream {
SoundPoolProxy get _soundPool => _player._soundPool; SoundPoolProxy get _soundPool => _player._soundPool;
/// Stop the sound effect.
void stop() { void stop() {
_soundPool.ptr.stop(_streamId); _soundPool.ptr.stop(_streamId);
} }
/// True if the sound effect is paused.
bool get paused => _paused; bool get paused => _paused;
bool _paused; bool _paused;
set paused(bool value) { set paused(bool value) {
...@@ -42,6 +54,7 @@ class SoundEffectStream { ...@@ -42,6 +54,7 @@ class SoundEffectStream {
} }
} }
/// Left volume of the sound effect, valid values are 0.0 to 1.0.
double get leftVolume => _leftVolume; double get leftVolume => _leftVolume;
double _leftVolume; double _leftVolume;
set leftVolume(double value) { set leftVolume(double value) {
...@@ -49,6 +62,7 @@ class SoundEffectStream { ...@@ -49,6 +62,7 @@ class SoundEffectStream {
_soundPool.ptr.setVolume(_streamId, <double>[_leftVolume, _rightVolume]); _soundPool.ptr.setVolume(_streamId, <double>[_leftVolume, _rightVolume]);
} }
/// Right volume of the sound effect, valid values are 0.0 to 1.0.
double get rightVolume => _rightVolume; double get rightVolume => _rightVolume;
double _rightVolume; double _rightVolume;
set rightVolume(double value) { set rightVolume(double value) {
...@@ -56,6 +70,8 @@ class SoundEffectStream { ...@@ -56,6 +70,8 @@ class SoundEffectStream {
_soundPool.ptr.setVolume(_streamId, <double>[_leftVolume, _rightVolume]); _soundPool.ptr.setVolume(_streamId, <double>[_leftVolume, _rightVolume]);
} }
/// The pitch of the sound effect, a value of 1.0 plays back the sound effect
/// at normal speed. Cannot be negative.
double get pitch => _pitch; double get pitch => _pitch;
double _pitch; double _pitch;
set pitch(double value) { set pitch(double value) {
...@@ -64,7 +80,11 @@ class SoundEffectStream { ...@@ -64,7 +80,11 @@ class SoundEffectStream {
} }
} }
/// The SoundEffectPlayer loads and plays sound effects.
class SoundEffectPlayer { class SoundEffectPlayer {
/// Creates a new SoundEffectPlayer with a max number of simultaneous
/// streams specified.
SoundEffectPlayer(int maxStreams) { SoundEffectPlayer(int maxStreams) {
MediaServiceProxy mediaService = new MediaServiceProxy.unbound(); MediaServiceProxy mediaService = new MediaServiceProxy.unbound();
shell.connectToService("mojo:media_service", mediaService); shell.connectToService("mojo:media_service", mediaService);
...@@ -76,6 +96,7 @@ class SoundEffectPlayer { ...@@ -76,6 +96,7 @@ class SoundEffectPlayer {
bool _paused; bool _paused;
int _nextStreamId = 0; int _nextStreamId = 0;
/// Loads a sound effect.
Future<SoundEffect> load(MojoDataPipeConsumer data) async { Future<SoundEffect> load(MojoDataPipeConsumer data) async {
SoundPoolLoadResponseParams result = await _soundPool.ptr.load(data); SoundPoolLoadResponseParams result = await _soundPool.ptr.load(data);
if (result.success) if (result.success)
...@@ -84,6 +105,7 @@ class SoundEffectPlayer { ...@@ -84,6 +105,7 @@ class SoundEffectPlayer {
throw new Exception('Unable to load sound'); throw new Exception('Unable to load sound');
} }
/// Plays a sound effect.
Future<SoundEffectStream> play(SoundEffect sound, { Future<SoundEffectStream> play(SoundEffect sound, {
double leftVolume: 1.0, double leftVolume: 1.0,
double rightVolume: 1.0, double rightVolume: 1.0,
...@@ -106,6 +128,7 @@ class SoundEffectPlayer { ...@@ -106,6 +128,7 @@ class SoundEffectPlayer {
throw new Exception('Unable to play sound'); throw new Exception('Unable to play sound');
} }
/// Set to true to pause a sound effect.
bool get paused => _paused; bool get paused => _paused;
set paused(bool value) { set paused(bool value) {
...@@ -118,23 +141,44 @@ class SoundEffectPlayer { ...@@ -118,23 +141,44 @@ class SoundEffectPlayer {
} }
} }
/// Signature for callbacks used by [SoundTrack].
typedef void SoundTrackCallback(SoundTrack soundTrack); typedef void SoundTrackCallback(SoundTrack soundTrack);
/// Signature for callbacks used by [SoundTrack].
typedef void SoundTrackBufferingCallback(SoundTrack soundTrack, int index); typedef void SoundTrackBufferingCallback(SoundTrack soundTrack, int index);
/// A sound track is typically longer than a [SoundEffect]. Use sound tracks to
/// play back music or ambient sounds.
class SoundTrack { class SoundTrack {
MediaPlayerProxy _player; MediaPlayerProxy _player;
/// Called when the sound has finished playing.
SoundTrackCallback onSoundComplete; SoundTrackCallback onSoundComplete;
/// Called when a seek operation has finished.
SoundTrackCallback onSeekComplete; SoundTrackCallback onSeekComplete;
/// Called when buffering is being performed.
SoundTrackBufferingCallback onBufferingUpdate; SoundTrackBufferingCallback onBufferingUpdate;
/// If true, the sound track will automatically loop.
bool loop; bool loop;
/// The current playback time in seconds.
double time; double time;
/// The volume the sound track is currently played at, valid range is 0.0 to
/// 1.0.
double volume; double volume;
} }
SoundTrackPlayer _sharedSoundTrackPlayer; SoundTrackPlayer _sharedSoundTrackPlayer;
/// Loads and plays [SoundTrack]s.
class SoundTrackPlayer { class SoundTrackPlayer {
/// Creates a new [SoundTrackPlayer], typically you will want to use the
/// [sharedInstance] method to receive the player.
SoundTrackPlayer() { SoundTrackPlayer() {
_mediaService = new MediaServiceProxy.unbound(); _mediaService = new MediaServiceProxy.unbound();
shell.connectToService("mojo:media_service", _mediaService); shell.connectToService("mojo:media_service", _mediaService);
...@@ -144,10 +188,13 @@ class SoundTrackPlayer { ...@@ -144,10 +188,13 @@ class SoundTrackPlayer {
Set<SoundTrack> _soundTracks = new HashSet<SoundTrack>(); Set<SoundTrack> _soundTracks = new HashSet<SoundTrack>();
/// Retrives a singleton object of the SoundTrackPlayer, use this method
/// in favor for the constructor.
static SoundTrackPlayer sharedInstance() { static SoundTrackPlayer sharedInstance() {
return _sharedSoundTrackPlayer ??= new SoundTrackPlayer(); return _sharedSoundTrackPlayer ??= new SoundTrackPlayer();
} }
/// Loads a [SoundTrack].
Future<SoundTrack> load(Future<MojoDataPipeConsumer> pipe) async { Future<SoundTrack> load(Future<MojoDataPipeConsumer> pipe) async {
// Create media player // Create media player
SoundTrack soundTrack = new SoundTrack(); SoundTrack soundTrack = new SoundTrack();
...@@ -158,11 +205,13 @@ class SoundTrackPlayer { ...@@ -158,11 +205,13 @@ class SoundTrackPlayer {
return soundTrack; return soundTrack;
} }
/// Unloads a [SoundTrack] from memory.
void unload(SoundTrack soundTrack) { void unload(SoundTrack soundTrack) {
stop(soundTrack); stop(soundTrack);
_soundTracks.remove(soundTrack); _soundTracks.remove(soundTrack);
} }
/// Plays a [SoundTrack].
void play(SoundTrack soundTrack, { void play(SoundTrack soundTrack, {
bool loop: false, bool loop: false,
double volume: 1.0, double volume: 1.0,
...@@ -175,15 +224,19 @@ class SoundTrackPlayer { ...@@ -175,15 +224,19 @@ class SoundTrackPlayer {
_soundTracks.add(soundTrack); _soundTracks.add(soundTrack);
} }
/// Stops a [SoundTrack]. You may also want to call the [unload] method to
/// remove if from memory if you are not planning to play it again.
void stop(SoundTrack track) { void stop(SoundTrack track) {
track._player.ptr.pause(); track._player.ptr.pause();
} }
/// Pauses all [SoundTrack]s that are currently playing.
void pauseAll() { void pauseAll() {
for (SoundTrack soundTrack in _soundTracks) for (SoundTrack soundTrack in _soundTracks)
soundTrack._player.ptr.pause(); soundTrack._player.ptr.pause();
} }
/// Resumes all [SoundTrack]s that have been loaded by this player.
void resumeAll() { void resumeAll() {
for (SoundTrack soundTrack in _soundTracks) for (SoundTrack soundTrack in _soundTracks)
soundTrack._player.ptr.start(); soundTrack._player.ptr.start();
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A Sprite is a [Node] that renders a bitmap image to the screen. /// A Sprite is a [Node] that renders a bitmap image to the screen.
...@@ -84,6 +88,8 @@ class Sprite extends NodeWithSize with SpritePaint { ...@@ -84,6 +88,8 @@ class Sprite extends NodeWithSize with SpritePaint {
} }
} }
/// Defines properties, such as [opacity] and [transferMode] that are shared
/// between [Node]s that render textures to screen.
abstract class SpritePaint { abstract class SpritePaint {
double _opacity = 1.0; double _opacity = 1.0;
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// Options for setting up a [SpriteBox]. /// Options for setting up a [SpriteBox]'s coordinate system.
///
/// * [nativePoints]: use the same points as the parent [Widget].
/// * [letterbox]: use the size of the root node for the coordinate system, and constrain the aspect ratio and trim off
/// areas that end up outside the screen.
/// * [stretch]: use the size of the root node for the coordinate system, and scale it to fit the size of the box.
/// * [scaleToFit]: similar to the letterbox option, but instead of trimming areas the sprite system will be scaled
/// down to fit the box.
/// * [fixedWidth]: use the width of the root node to set the size of the coordinate system, and change
/// the height of the root node to fit the box.
/// * [fixedHeight]: use the height of the root node to set the size of the coordinate system, and change
/// the width of the root node to fit the box.
enum SpriteBoxTransformMode { enum SpriteBoxTransformMode {
/// Use the same points as the parent [Widget].
nativePoints, nativePoints,
/// Use the size of the root node for the coordinate system, and constrain the
/// aspect ratio and trim off areas that end up outside the screen.
letterbox, letterbox,
/// Use the size of the root node for the coordinate system, and scale it to
/// fit the size of the box.
stretch, stretch,
/// Similar to the letterbox option, but instead of trimming areas the sprite
/// system will be scaled down to fit the box.
scaleToFit, scaleToFit,
/// Use the width of the root node to set the size of the coordinate system,
/// and change the height of the root node to fit the box.
fixedWidth, fixedWidth,
/// Use the height of the root node to set the size of the coordinate system,
/// and change the width of the root node to fit the box.
fixedHeight, fixedHeight,
} }
/// A [RenderBox] that draws a sprite world represented by a [Node] tree.
class SpriteBox extends RenderBox { class SpriteBox extends RenderBox {
// Setup // Setup
...@@ -71,31 +81,6 @@ class SpriteBox extends RenderBox { ...@@ -71,31 +81,6 @@ class SpriteBox extends RenderBox {
// Member variables // Member variables
// Root node for drawing
NodeWithSize _rootNode;
set rootNode (NodeWithSize value) {
if (value == _rootNode) return;
// Ensure that the root node has a size
assert(_transformMode == SpriteBoxTransformMode.nativePoints
|| value.size.width > 0);
assert(_transformMode == SpriteBoxTransformMode.nativePoints
|| value.size.height > 0);
// Remove sprite box references
if (_rootNode != null)
_removeSpriteBoxReference(_rootNode);
// Update the value
_rootNode = value;
_actionControllers = null;
// Add new references
_addSpriteBoxReference(_rootNode);
markNeedsLayout();
}
// Tracking of frame rate and updates // Tracking of frame rate and updates
Duration _lastTimeStamp; Duration _lastTimeStamp;
double _frameRate = 0.0; double _frameRate = 0.0;
...@@ -126,14 +111,16 @@ class SpriteBox extends RenderBox { ...@@ -126,14 +111,16 @@ class SpriteBox extends RenderBox {
List<Node> _constrainedNodes; List<Node> _constrainedNodes;
Rect _visibleArea; /// A rectangle that represents the visible area of the sprite world's
/// coordinate system.
Rect get visibleArea { Rect get visibleArea {
if (_visibleArea == null) if (_visibleArea == null)
_calcTransformMatrix(); _calcTransformMatrix();
return _visibleArea; return _visibleArea;
} }
Rect _visibleArea;
bool _initialized = false; bool _initialized = false;
// Properties // Properties
...@@ -143,6 +130,30 @@ class SpriteBox extends RenderBox { ...@@ -143,6 +130,30 @@ class SpriteBox extends RenderBox {
/// var rootNode = mySpriteBox.rootNode; /// var rootNode = mySpriteBox.rootNode;
NodeWithSize get rootNode => _rootNode; NodeWithSize get rootNode => _rootNode;
NodeWithSize _rootNode;
set rootNode (NodeWithSize value) {
if (value == _rootNode) return;
// Ensure that the root node has a size
assert(_transformMode == SpriteBoxTransformMode.nativePoints
|| value.size.width > 0);
assert(_transformMode == SpriteBoxTransformMode.nativePoints
|| value.size.height > 0);
// Remove sprite box references
if (_rootNode != null)
_removeSpriteBoxReference(_rootNode);
// Update the value
_rootNode = value;
_actionControllers = null;
// Add new references
_addSpriteBoxReference(_rootNode);
markNeedsLayout();
}
@override @override
void performLayout() { void performLayout() {
size = constraints.biggest; size = constraints.biggest;
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A widget that uses a [SpriteBox] to render a sprite node tree to the screen. /// A widget that uses a [SpriteBox] to render a sprite node tree to the screen.
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A sprite sheet packs a number of smaller images into a single large image. /// A sprite sheet packs a number of smaller images into a single large image.
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A texture represents a rectangular area of an image and is typically used to draw a sprite to the screen. /// A texture represents a rectangular area of an image and is typically used to draw a sprite to the screen.
...@@ -65,6 +69,7 @@ class Texture { ...@@ -65,6 +69,7 @@ class Texture {
/// myTexture.pivot = new Point(0.5, 0.5); /// myTexture.pivot = new Point(0.5, 0.5);
Point pivot; Point pivot;
/// Creates a new Texture from a part of the current texture.
Texture textureFromRect(Rect rect, [String name = null]) { Texture textureFromRect(Rect rect, [String name = null]) {
assert(rect != null); assert(rect != null);
assert(!rotated); assert(!rotated);
...@@ -73,6 +78,8 @@ class Texture { ...@@ -73,6 +78,8 @@ class Texture {
return new Texture._fromSpriteFrame(image, name, rect.size, false, false, srcFrame, dstFrame, new Point(0.5, 0.5)); return new Texture._fromSpriteFrame(image, name, rect.size, false, false, srcFrame, dstFrame, new Point(0.5, 0.5));
} }
/// Draws the texture to a [Canvas] at a specified [position] and with the
/// specified [paint].
void drawTexture(Canvas canvas, Point position, Paint paint) { void drawTexture(Canvas canvas, Point position, Paint paint) {
// Get drawing position // Get drawing position
double x = position.x; double x = position.x;
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// A [Node] that draws a polyline from a list of points using the provided
/// [Texture]. The textured line draws static lines. If you want to create an
/// animated line, consider using the [EffectLine] instead.
class TexturedLine extends Node { class TexturedLine extends Node {
/// Creates a new TexturedLine.
TexturedLine(List<Point> points, List<Color> colors, List<double> widths, [Texture texture, List<double> textureStops]) { TexturedLine(List<Point> points, List<Color> colors, List<double> widths, [Texture texture, List<double> textureStops]) {
painter = new TexturedLinePainter(points, colors, widths, texture, textureStops); painter = new TexturedLinePainter(points, colors, widths, texture, textureStops);
} }
/// The painter used to draw the line.
TexturedLinePainter painter; TexturedLinePainter painter;
@override @override
...@@ -13,26 +23,34 @@ class TexturedLine extends Node { ...@@ -13,26 +23,34 @@ class TexturedLine extends Node {
} }
} }
/// Draws a polyline to a [Canvas] from a list of points using the provided [Texture].
class TexturedLinePainter { class TexturedLinePainter {
TexturedLinePainter(this._points, this.colors, this.widths, [Texture texture, this.textureStops]) { TexturedLinePainter(this._points, this.colors, this.widths, [Texture texture, this.textureStops]) {
this.texture = texture; this.texture = texture;
} }
List<Point> _points; /// The points that makes up the polyline.
List<Point> get points => _points; List<Point> get points => _points;
List<Point> _points;
set points(List<Point> points) { set points(List<Point> points) {
_points = points; _points = points;
_calculatedTextureStops = null; _calculatedTextureStops = null;
} }
/// The color of each point on the polyline. The color of the line will be
/// interpolated between the points.
List<Color> colors; List<Color> colors;
/// The width of the line at each point on the polyline.
List<double> widths; List<double> widths;
Texture _texture;
/// The texture this line will be drawn using.
Texture get texture => _texture; Texture get texture => _texture;
Texture _texture;
set texture(Texture texture) { set texture(Texture texture) {
_texture = texture; _texture = texture;
if (texture == null) { if (texture == null) {
...@@ -47,41 +65,51 @@ class TexturedLinePainter { ...@@ -47,41 +65,51 @@ class TexturedLinePainter {
} }
} }
/// Defines the position in the texture for each point on the polyline.
List<double> textureStops; List<double> textureStops;
List<double> _calculatedTextureStops; /// The [textureStops] used if no explicit texture stops has been provided.
List<double> get calculatedTextureStops { List<double> get calculatedTextureStops {
if (_calculatedTextureStops == null) if (_calculatedTextureStops == null)
_calculateTextureStops(); _calculateTextureStops();
return _calculatedTextureStops; return _calculatedTextureStops;
} }
List<double> _calculatedTextureStops;
double _length; double _length;
/// The length of the line.
double get length { double get length {
if (_calculatedTextureStops == null) if (_calculatedTextureStops == null)
_calculateTextureStops(); _calculateTextureStops();
return _length; return _length;
} }
/// The offset of the texture on the line.
double textureStopOffset = 0.0; double textureStopOffset = 0.0;
double _textureLoopLength; /// The length, in points, that the texture is stretched to. If the
/// textureLoopLength is shorter than the line, the texture will be looped.
double get textureLoopLength => textureLoopLength; double get textureLoopLength => textureLoopLength;
double _textureLoopLength;
set textureLoopLength(double textureLoopLength) { set textureLoopLength(double textureLoopLength) {
_textureLoopLength = textureLoopLength; _textureLoopLength = textureLoopLength;
_calculatedTextureStops = null; _calculatedTextureStops = null;
} }
/// If true, the textured line attempts to remove artifacts at sharp corners
/// on the polyline.
bool removeArtifacts = true; bool removeArtifacts = true;
/// The [TransferMode] used to draw the line to the [Canvas].
TransferMode transferMode = TransferMode.srcOver; TransferMode transferMode = TransferMode.srcOver;
Paint _cachedPaint = new Paint(); Paint _cachedPaint = new Paint();
/// Paints the line to the [canvas].
void paint(Canvas canvas) { void paint(Canvas canvas) {
// Check input values // Check input values
assert(_points != null); assert(_points != null);
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of flutter_sprites; part of flutter_sprites;
/// Provides a virtual joystick that can easily be added to your sprite scene.
class VirtualJoystick extends NodeWithSize { class VirtualJoystick extends NodeWithSize {
/// Creates a new virtual joystick.
VirtualJoystick() : super(new Size(160.0, 160.0)) { VirtualJoystick() : super(new Size(160.0, 160.0)) {
userInteractionEnabled = true; userInteractionEnabled = true;
handleMultiplePointers = false; handleMultiplePointers = false;
...@@ -17,11 +24,15 @@ class VirtualJoystick extends NodeWithSize { ...@@ -17,11 +24,15 @@ class VirtualJoystick extends NodeWithSize {
..style = PaintingStyle.stroke; ..style = PaintingStyle.stroke;
} }
Point _value = Point.origin; /// Reads the current value of the joystick. A point with from (-1.0, -1.0)
/// to (1.0, 1.0). If the joystick isn't moved it will return (0.0, 0.0).
Point get value => _value; Point get value => _value;
Point _value = Point.origin;
bool _isDown = false; /// True if the user is currently touching the joystick.
bool get isDown => _isDown; bool get isDown => _isDown;
bool _isDown = false;
Point _pointerDownAt; Point _pointerDownAt;
Point _center; Point _center;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment