Android Open Source - DolphinOES Ninepatch Drawer






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.//  ww  w  . j av a  2s . c o m
 * 
 * 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.ninepatch;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.sidereal.dolphinoes.architecture.DolphinOES;
import com.sidereal.dolphinoes.architecture.core.Assets;
import com.sidereal.dolphinoes.behaviors.renderer.Drawer;
import com.sidereal.dolphinoes.behaviors.renderer.Renderer;

/** {@link Drawer} extension that can draw ninepatch images. Images do not have to
 * be processed ( as in have meta data), the size will be set manually in the
 * parameters
 * 
 * @author Claudiu Bele */
public class NinepatchDrawer extends Drawer
{
  // region fields

  NinePatch image;
  private Vector2 positionOffset;
  private Vector2 size;
  private Vector2 scale;
  private int paddingLeft;
  private int paddingRight;
  private int paddingTop;
  private int paddingBottom;
  private Rectangle boundingRectangle;
  private Color tintColor;

  // endregion fields

  // region constructors

  public NinepatchDrawer(Renderer renderer, String name, String filepath,
      int left, int right, int top, int bottom)
  {

    super(renderer, name, false);

    paddingLeft = left;
    paddingRight = right;
    paddingTop = top;
    paddingBottom = bottom;
    setTexture(filepath);

    if (!DolphinOES.assets.contains(filepath))
    {
      DolphinOES.assets.load(filepath, Texture.class);
      DolphinOES.assets.finishLoading();
    }

    size = new Vector2(image.getTexture().getWidth(), image.getTexture()
        .getHeight());
    positionOffset = new Vector2();
    scale = new Vector2(1,1);
    boundingRectangle = new Rectangle();
    
  }

  // endregion constructors

  // region methods

  @Override
  protected void dispose()
  {

  }

  @Override
  protected void draw(float delta)
  {

    image.draw(renderer.object.gameBatch.spriteBatch,
        renderer.object.pos.getX() + positionOffset.x,
        renderer.object.pos.getY() + positionOffset.y, size.x,
        size.y);
    
  }

  @Override
  protected boolean isOutOfBounds()
  {

    boundingRectangle.set(renderer.object.pos.getX()
        + positionOffset.x, renderer.object.pos.getY()
        + positionOffset.y, size.x, size.y);

    return !renderer.object.gameBatch.renderingArea
        .overlaps(boundingRectangle);
  }

  /** Sets the texture to use for the ninepatch. The {@link #paddingLeft},
   * {@link #paddingRight}, {@link #sizeTop} and {@link #paddingBottom} are
   * used when assigning {@link #image} using the following NinePatch
   * constructor: {@link NinePatch#NinePatch(Texture, int, int, int, int)}
   * <p>
   * If the texture was not loaded, the texture will be loaded by the
   * {@link Assets} ,being forced to finish loading the texture
   * before assigning it to {@link #image}
   * 
   * @param filepath
   *            filepath to the image */
  public void setTexture(String filepath)
  {
    if (filepath == null)
      throw new NullPointerException(
          "Passed null String parameter to NinePatchDrawer.setTexture(String)");
    
    // asset is not loaded
    if (!DolphinOES.assets.contains(filepath))
    {
      DolphinOES.assets.load(filepath, Texture.class);
      DolphinOES.assets.finishLoading();
      image = new NinePatch(DolphinOES.assets.get(
          filepath, Texture.class), paddingLeft, paddingRight,
          paddingTop, paddingBottom);
    }
    // asset is loaded
    else
    {
      Texture targetTexture = DolphinOES.assets.get(
          filepath, Texture.class);
      if (image == null || !targetTexture.equals(image.getTexture()))
      {
        image = new NinePatch(targetTexture, paddingLeft, paddingRight,
            paddingTop, paddingBottom);
      }
    }

  }

  /** Sets the size the ninepatch to draw. If not assigned, will be set to
   * {@link Texture#getWidth()} and {@link Texture#getHeight()}.
   * <p>
   * Can be used at runtime, and will be used when building the object, after
   * using {@link NinepatchBuilder#setSize(float, float)} in the
   * {@link NinepatchBuilder}.
   * 
   * @param width
   *            new width of the texture
   * @param height
   *            new height of the texture */
  public void setSize(float width, float height)
  {

    if (size == null)
      size = new Vector2(width, height);
    else
      size.set(width, height);
  }

  /** Offset at which to draw the texture, relative to
   * {@link #MISSING()}.
   * <p>
   * Can be used at runtime, and will be used when building the object, after
   * using {@link NinepatchBuilder#setOffsetPosition(float, float)} in the
   * {@link NinepatchBuilder}.
   * 
   * @param offsetX
   *            number of units to push the object on the X axis, can be
   *            negative.
   * @param offsetY
   *            number of units to push the object on the Y axis, can be
   *            negative. */
  public void setOffsetPosition(float offsetX, float offsetY)
  {

    if (positionOffset == null)
      positionOffset = new Vector2(offsetX, offsetY);
    else
      positionOffset.set(offsetX, offsetY);
  }

  /** Sets the color of the ninepatch, using {@link NinePatch#setColor(Color)}.
   * <p>
   * Can be used at runtime, and will be used when building the object, after
   * using {@link NinepatchBuilder#setColor(Color)} in the
   * {@link NinepatchBuilder}.
   * 
   * @param color */
  public void setColor(Color color)
  {

    tintColor = color;
    image.setColor(tintColor);
  }

  /** Sets the scale of the nine patch. This method internally handles using the
   * {@link NinePatch#scale(float, float)} method with parameters that match the
   * target scale, regardless of previous scale sets. The scaling is in accordance to
   * the default scale values, which are 1,1.
   * <p>
   * Example: To scale the width of an element to be 3 times the default value but you
   * want the height to be different, use <code>setScale(3,1);</code>
   * 
   * @param scaleX
   * @param scaleY
   */
  public void setScale(float scaleX, float scaleY)
  {
    // the real scaling to be used in the system
    scale.x = scaleX / scale.x;
    scale.y = scaleY / scale.y;
    
    // apply the scaling;
    image.scale(scale.x, scale.y);
    
  }
  
  // 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