Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add container #4

Merged
merged 3 commits into from Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
185 changes: 185 additions & 0 deletions library/src/main/java/com/daasuu/library/Container.java
@@ -0,0 +1,185 @@
package com.daasuu.library;


import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;

import com.daasuu.library.drawer.BaseDrawer;

import java.util.ArrayList;
import java.util.List;

/**
* A Container is a nestable display list that allows you to work with compound display elements.
* For example you could group arm, leg, torso and head Bitmap instances together into a Person Container,
* and transform them as a group, while still being able to move the individual parts relative to each other.
* Children of containers have their transform and alpha properties concatenated with their parent Container.
*/
public class Container extends DisplayBase {

private static final long DEFAULT_FPS = -1;

private long mFps = DEFAULT_FPS;

/**
* constructor
*/
public Container() {
drawer(new ContainerDrawer()).tween().end();
}

/**
* Return Composer instance to setup this Container instance.
* This method is useful when you use only default class of animation.
*
* @return composer
*/
public DisplayComposer with() {
return new DisplayComposer();
}

@Override
public void draw(Canvas canvas) {
super.draw(canvas);

List<DisplayObject> copyDisplayObjectList = new ArrayList<DisplayObject>(mDisplayList);
for (DisplayObject DisplayObject : copyDisplayObjectList) {
if (DisplayObject == null) {
continue;
}
DisplayObject.draw(canvas, mAnimParameter);
}

}

@Override
void setUp(long fps) {
super.setUp(fps);
mFps = fps;
for (DisplayObject DisplayObject : mDisplayList) {
if (DisplayObject == null) {
continue;
}
DisplayObject.setUp(fps);
}
}

private List<DisplayObject> mDisplayList = new ArrayList<>();

/**
* Adds a child to the top of the display list.
*
* @param DisplayObject DisplayObject2
* @return this
*/
public Container addChild(@NonNull DisplayObject DisplayObject) {
if (mFps != DEFAULT_FPS) {
DisplayObject.setUp(mFps);
}
mDisplayList.add(DisplayObject);
return this;
}

/**
* Adds a child to the display list at the specified index, bumping children at equal or greater indexes up one, and setting its parent to this Container
*
* @param location index
* @param DisplayObject DisplayObject2
* @return this
*/
public Container addChildAt(int location, @NonNull DisplayObject DisplayObject) {
if (mFps != DEFAULT_FPS) {
DisplayObject.setUp(mFps);
}
mDisplayList.add(location, DisplayObject);
return this;
}

/**
* Removes the specified child from the display list.
*
* @param DisplayObject DisplayObject
* @return this
*/
public Container removeChild(@NonNull DisplayObject DisplayObject) {
mDisplayList.remove(DisplayObject);
return this;
}

/**
* Removes the child at the specified index from the display list.
*
* @param location index
* @return this
*/
public Container removeChildAt(int location) {
mDisplayList.remove(location);
return this;
}

/**
* Removes all children from the display list.
*
* @return this
*/
public Container removeAllChildren() {
mDisplayList.clear();
return this;
}

/**
* Swaps the specified children's depth in the display list. If either child is not a child of this Container, return false.
*
* @param child1 DisplayObject
* @param child2 DisplayObject
* @return if true, success to swapChildren
*/
public boolean swapChildren(@NonNull DisplayObject child1, @NonNull DisplayObject child2) {
int childIndex1 = mDisplayList.indexOf(child1);
int childIndex2 = mDisplayList.indexOf(child2);

if (childIndex1 == -1 || childIndex2 == -1) {
return false;
}

removeChildAt(childIndex1);
addChildAt(childIndex1, child2);
removeChildAt(childIndex2);
addChildAt(childIndex2, child1);
return true;
}

/**
* Getter DisplayList
*
* @return DisplayList
*/
public List<DisplayObject> getDisplayList() {
return mDisplayList;
}


private class ContainerDrawer extends BaseDrawer {

public ContainerDrawer() {
super(new Paint());
}

@Override
protected void draw(Canvas canvas, float x, float y) {
// Do nothing
}

@Override
public float getWidth() {
return 0;
}

@Override
public float getHeight() {
return 0;
}
}

}
109 changes: 109 additions & 0 deletions library/src/main/java/com/daasuu/library/DisplayBase.java
@@ -0,0 +1,109 @@
package com.daasuu.library;

import android.graphics.Canvas;
import android.support.annotation.NonNull;

import com.daasuu.library.animator.ParabolicAnimator;
import com.daasuu.library.animator.TweenAnimator;

/**
* Created by sudamasayuki on 16/01/26.
*/
public abstract class DisplayBase {

/**
* hold a parameter related to the drawing on the canvas.
*/
protected AnimParameter mAnimParameter;

protected Animator mAnimator;

protected Drawer mDrawer;

/**
* call from FPSTextureView or FPSSurfaceView when it is addChild.
*
* @param fps Set in FPSTextureView or FPSSurfaceView.
*/
void setUp(long fps) {
mAnimator.setUp(fps);
}

/**
* Return Composer instance to setup this DisplayObject instance.
* This method is useful when you use only default class of animation.
*
* @param drawer drawing object
* @return composer
*/
protected DisplayComposer drawer(@NonNull Drawer drawer) {
mDrawer = drawer;
return new DisplayComposer();
}

/**
* Set animation class.
* Use this method only when there is need to your own custom class of animation,
*
* @param animator Animator instance
*/
public DisplayBase animator(@NonNull Animator animator) {
this.mAnimator = animator;
mAnimParameter = mAnimator.getInitialAnimParameter();
return this;
}


/**
* Draws the display object into the specified context ignoring its visible, alpha, shadow, and transform.
*
* @param canvas This Canvas acquired by lookCanvas in FPSTextureView or FPSSurfaceView.
*/
void draw(@NonNull Canvas canvas) {
mAnimator.setBaseLine(canvas, mDrawer.getWidth(), mDrawer.getHeight());
mAnimator.updateAnimParam(mAnimParameter);
mDrawer.draw(canvas, mAnimParameter.x, mAnimParameter.y, mAnimParameter.alpha, mAnimParameter.scaleX, mAnimParameter.scaleY, mAnimParameter.rotation);
}

public AnimParameter getAnimParameter() {
return mAnimParameter;
}

/**
* Setter status of pause Motion Animator
*
* @param pause indicates whether to start the motion animation paused.
*/
public void pause(boolean pause) {
mAnimator.pause(pause);
}

/**
* Getter status of pause Motion Animator
*
* @return indicates whether to start the motion animation paused.
*/
public boolean isPause() {
return mAnimator.isPause();
}

/**
* Composer provide simple composing interface.
*/
public class DisplayComposer {
/**
* @return tween composer
*/
public TweenAnimator.Composer tween() {
return TweenAnimator.composer(DisplayBase.this);
}

/**
* @return parabolic composer
*/
public ParabolicAnimator.Composer parabolic() {
return ParabolicAnimator.composer(DisplayBase.this);
}
}

}