Android Open Source - DolphinOES Renderer






From Project

Back to project page DolphinOES.

License

The source code is released under:

Apache License

If you think the Android project DolphinOES listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2015 See AUTHORS file./*  w ww .ja  va2  s  .  c om*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.sidereal.dolphinoes.behaviors.renderer;

import java.lang.ref.WeakReference;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.ObjectMap;
import com.sidereal.dolphinoes.architecture.DolphinOES;
import com.sidereal.dolphinoes.architecture.GameBehavior;
import com.sidereal.dolphinoes.architecture.GameObject;

/** Class for rendering objects of different types. It contains {@link Drawer}
 * instances that require a {@link DrawerBuilder} to build them.
 * <p>
 * All of the {@link Drawer}s are added using
 * {@link #addDrawer(String, DrawerBuilder)} to {@link #drawerList} and
 * {@link #drawerMap},the list being iterated over and drawn in order of the
 * indexes in the array. The order can be changed using
 * {@link #placeAtEnd(String)} and {@link #placeAtStart(String)}.
 * <p>
 * When we want to remove a {@link Drawer}, we call
 * {@link #removeDrawer(String)}, which will call
 * {@link Drawer#drawInternal(float)}.
 * 
 * @author Claudiu Bele */
public class Renderer extends GameBehavior
{

  // region fields

  /** Map of Drawers. Adding values in
   * {@link #addDrawer(String, DrawerBuilder)} and removing them in
   * {@link #removeDrawer(String)}. */
  private ObjectMap<String, Drawer> drawerMap;

  /** List of drawers. The order in which { Drawer} objects are found in the
   * list decides which one is rendered first, rendering first from index 0 to
   * n. */
  private Array<Drawer> drawerList;

  // endregion fields

  // region constructors

  /** Initialises the data types.
   * 
   * @param obj
   *            the object that the behavior is attached to. */
  public Renderer(GameObject obj)
  {

    super(obj);
    drawerList = new Array<Drawer>();
    drawerMap = new ObjectMap<String, Drawer>();
  }

  // endregion constructors

  // region methods

  /** Draws each {@link Drawer} object found in {@link #drawerList} in the
   * order that they are found in the list. Use {@link #placeAtStart(String)} or
   * {@link #placeAtEnd(String)} to adjust */
  @Override
  public final void update()
  {

    for (int i = 0; i < drawerList.size; i++)
    {
      Drawer drawer = drawerList.get(i);
      float delta = (drawer.getUseRealDeltaTime()) ? DolphinOES.time.getRealDeltaTime() : DolphinOES.time.getDeltaTime();
      drawer.drawInternal(delta);
    }
  }

  /** Adds a drawer to the renderer.
   * <p>
   * The parameters as well as the {@link Renderer}
   * instance itself are used for constructing {@link Drawer}
   * out of a {@link DrawerBuilder}.
   * <p>
   * Calls {@link #addDrawer(String, DrawerBuilder, boolean)} with the
   * boolean parameter set to false, which creates a weak reference to the builder signalling
   * that it can be removed by the GC
   * 
   * @param drawerName name of the drawer, will be used for retrieval of drawer using 
   * {@link #getDrawer(String)} and {@link #getDrawer(String, Class)}.
   * @param builder builder to crate a drawer with
   * @return the drawer resulted on calling {@link DrawerBuilder#buildInternal(Renderer, String)}
   */
  public final <T extends Drawer> T addDrawer(String drawerName,
      DrawerBuilder<T> builder)
  {
    return addDrawer(drawerName, builder, true);
  }
  
  /** Adds a drawer to the renderer.
   * <p>
   * The parameters as well as the {@link Renderer}
   * instance itself are used for constructing {@link Drawer}
   * out of a {@link DrawerBuilder}.
   * <p>
   * Is called from {@link #addDrawer(String, DrawerBuilder)} with the <code>destroyAfterCreation</code>
   * parameter set to true, but can also be called by the user on a renderer.
   * 
   * @param drawerName name of the drawer, will be used for retrieval of drawer using 
   * {@link #getDrawer(String)} and {@link #getDrawer(String, Class)}.
   * @param builder builder to crate a drawer with
   * @param destroyAfterCreation Whether to create a weak reference to the builder after
   * the drawer has been created
   * @return the drawer resulted on calling {@link DrawerBuilder#buildInternal(Renderer, String)}
   */
  public final <T extends Drawer> T addDrawer(String drawerName,
    DrawerBuilder<T> builder, boolean destroyAfterCreation)
  {
    T drawer = builder.buildInternal(this, drawerName);
    drawerList.add(drawer);
    drawerMap.put(drawerName, drawer);
    
    // create a weak reference to the builder
    if(destroyAfterCreation) new WeakReference<DrawerBuilder<T>>(builder);
    
    return drawer;
    
  }

  // region utility

  /** Switches the order of the elements in {@link #drawerList} which handles
   * rendering {@link Drawer} instances in the order that they are given, thus
   * placing an element at start will make it render first.
   * 
   * @param name
   *            The passed value in */
  public final void placeAtStart(String name)
  {

    drawerList.insert(0, drawerList.removeIndex(drawerList.indexOf(
        drawerMap.get(name), false)));
  }

  /** Switches the order of the elements in {@link #drawerList} which handles
   * rendering {@link Drawer} instances in the order that they are given, thus
   * placing an element at end will make it render last.
   * 
   * @param name
   *            the drawer key for {@link #drawerMap} passed in
   *            {@link #addDrawer(String, DrawerBuilder)}. */
  public final void placeAtEnd(String name)
  {

    drawerList.insert(drawerList.size - 1, drawerList
        .removeIndex(drawerList.indexOf(drawerMap.get(name), false)));

  }

  // endregion

  // region setters and getters

  /** Returns the drawer tied to the passed value/
   * 
   * @param name
   *            The key to the drawer used in
   *            {@link #addDrawer(String, DrawerBuilder)} for
   *            {@link #drawerMap}.
   * @return The value tied to the <code>name</code> parameter from
   *         {@link #drawerMap}. */
  public Drawer getDrawer(String name)
  {

    if (!drawerMap.containsKey(name))
      return null;
    return drawerMap.get(name);
  }

  /** Returns a drawer casted as the passed class.
   * <p>
   * Will return null if the value can't be found by key in {@link #drawerMap}
   * and a {@link GdxRuntimeException} if the object is casted as a different
   * {@link Drawer} class than it is.
   * 
   * @param name
   *            The key to the value we want to retrieve from
   *            {@link #drawerMap}
   * @param drawerClass
   *            The {@link Drawer} subclass we want to cast to and return
   * @return The casted value if found and passed class meets object's class,
   *         {@link GdxRuntimeException} if class to cast to doesn't meet
   *         object's class. */
  @SuppressWarnings("unchecked")
  public <T extends Drawer> T getDrawer(String name, Class<T> drawerClass)
  {

    if (!drawerMap.containsKey(name))
      return null;

    if (!drawerMap.get(name).getClass().equals(drawerClass))
      throw new GdxRuntimeException("You are trying to get the Drawer "
          + name + " as a " + drawerClass.getName()
          + " but the value is of class "
          + drawerMap.get(name).getClass().getName());
    return (T) drawerMap.get(name);

  }

  /** Removes a drawer from {@link #drawerList} and {@link #drawerMap}.
   * <p>
   * If a {@link Drawer} is found in {@link #drawerMap} with the parameter
   * passed as a key, {@link Drawer#dispose()} will also be called on the
   * found value.
   * 
   * @param name
   *            name of the drawer that is the key to the { Drawer} value in
   *            {@link #addDrawer(String, DrawerBuilder)} added in
   * @return */
  public boolean removeDrawer(String name)
  {

    if (!drawerMap.containsKey(name))
      return false;

    drawerMap.get(name).dispose();
    drawerList.removeIndex(drawerList.indexOf(drawerMap.get(name), false));
    drawerMap.remove(name);
    return true;
  }

  // endregion

  // endregion methods
}




Java Source Code List

com.sidereal.dolphinoes.architecture.AbstractEvent.java
com.sidereal.dolphinoes.architecture.DebugHandler.java
com.sidereal.dolphinoes.architecture.DolphinOES.java
com.sidereal.dolphinoes.architecture.GameBatch.java
com.sidereal.dolphinoes.architecture.GameBehavior.java
com.sidereal.dolphinoes.architecture.GameObject.java
com.sidereal.dolphinoes.architecture.GameScene.java
com.sidereal.dolphinoes.architecture.Module.java
com.sidereal.dolphinoes.architecture.core.Assets.java
com.sidereal.dolphinoes.architecture.core.Configurable.java
com.sidereal.dolphinoes.architecture.core.Debug.java
com.sidereal.dolphinoes.architecture.core.DolphinOESConfiguration.java
com.sidereal.dolphinoes.architecture.core.GameData.java
com.sidereal.dolphinoes.architecture.core.MouseMovedEvent.java
com.sidereal.dolphinoes.architecture.core.Time.java
com.sidereal.dolphinoes.architecture.core.assetload.AssetLoadHandler.java
com.sidereal.dolphinoes.architecture.core.assetload.AssetLoader.java
com.sidereal.dolphinoes.architecture.core.assetload.ClassFileHandleResolver.java
com.sidereal.dolphinoes.architecture.core.assetload.LoadingPercentage.java
com.sidereal.dolphinoes.architecture.core.input.ActionData.java
com.sidereal.dolphinoes.architecture.core.input.ActionEventWrapper.java
com.sidereal.dolphinoes.architecture.core.input.ActionEvent.java
com.sidereal.dolphinoes.architecture.core.input.Input.java
com.sidereal.dolphinoes.architecture.core.input.KeyTypedEvent.java
com.sidereal.dolphinoes.architecture.core.input.ScrollEvent.java
com.sidereal.dolphinoes.architecture.core.input.TouchData.java
com.sidereal.dolphinoes.architecture.core.input.TouchEventWrapper.java
com.sidereal.dolphinoes.architecture.core.input.TouchEvent.java
com.sidereal.dolphinoes.architecture.pos.Position.java
com.sidereal.dolphinoes.architecture.pos.Positions.java
com.sidereal.dolphinoes.backend.ConcreteGametester.java
com.sidereal.dolphinoes.backend.GameTester.java
com.sidereal.dolphinoes.backend.SceneGameTester.java
com.sidereal.dolphinoes.behaviors.audio.AudioListener.java
com.sidereal.dolphinoes.behaviors.audio.AudioPlayer.java
com.sidereal.dolphinoes.behaviors.events.EventTimer.java
com.sidereal.dolphinoes.behaviors.events.RecurringEvent.java
com.sidereal.dolphinoes.behaviors.input.Clickable.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleEmitter.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleHandler.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleSpriteLayout.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleSpriteObject.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingHandler.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingMap.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingNode.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingRoute.java
com.sidereal.dolphinoes.behaviors.renderer.DrawerBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.Drawer.java
com.sidereal.dolphinoes.behaviors.renderer.Renderer.java
com.sidereal.dolphinoes.behaviors.renderer.ninepatch.NinepatchBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.ninepatch.NinepatchDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.scml.SCMLBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.scml.SCMLDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.sprite.SpriteBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.sprite.SpriteDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequenceBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequenceDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequencePreference.java
com.sidereal.dolphinoes.behaviors.renderer.texture.TextureBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.texture.TextureDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.tilemap.TileMapBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.tilemap.TileMapDrawer.java
com.sidereal.dolphinoes.behaviors.triggers.Activatable.java
com.sidereal.dolphinoes.behaviors.triggers.Collider.java
com.sidereal.dolphinoes.behaviors.triggers.Hoverable.java
com.sidereal.dolphinoes.ui.MessageBubble.java
com.sidereal.dolphinoes.ui.TextBuilder.java
com.sidereal.dolphinoes.util.BooleanWrapper.java
com.sidereal.dolphinoes.util.DolphinOESException.java
com.sidereal.dolphinoes.util.FloatWrapper.java
com.sidereal.dolphinoes.util.IntWrapper.java
com.sidereal.dolphinoes.util.Utility.java