Android Open Source - DolphinOES Clickable






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 2014 See AUTHORS file./*from  w  w w  . j  av a  2  s . 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.input;

import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectMap.Entry;
import com.sidereal.dolphinoes.architecture.DolphinOES;
import com.sidereal.dolphinoes.architecture.GameBehavior;
import com.sidereal.dolphinoes.architecture.GameObject;
import com.sidereal.dolphinoes.architecture.core.input.ActionData;
import com.sidereal.dolphinoes.architecture.core.input.ActionEvent;
import com.sidereal.dolphinoes.architecture.core.input.Input;
import com.sidereal.dolphinoes.architecture.core.input.TouchData;
import com.sidereal.dolphinoes.architecture.core.input.TouchEvent;
import com.sidereal.dolphinoes.architecture.core.input.Input.InputEventType;

//TODO Reimplement clickable using the new Input System
/** Behavior used for creating events that are to be triggered when the user
 * clicks in a certain area. The area is saved in {@link #area}, which gets
 * updated each frame based on the location of the AbstractObject. The event to
 * run is {@link #eventOnHold}
 * 
 * @see {@link Input}
 * @author Claudiu Bele */
public class Clickable extends GameBehavior
{
  // region external
  
  private class ActionEventEntry{
    
    String inputProcessor;
    ActionEvent event;
    
    public ActionEventEntry(String inputProcessor, ActionEvent event)
    {
      this.inputProcessor = inputProcessor;
      this.event = event;
    }
  }
  
  private class TouchEventEntry{
    
    String inputProcessor;
    TouchEvent event;
    
    public TouchEventEntry(String inputProcessor, TouchEvent event)
    {
      this.inputProcessor = inputProcessor;
      this.event = event;
    }
  }
  
  // endregion external
  
  // region fields

  /** The amount of time to wait so as to not trigger multiple events in a row */
  public float cooldown;

  /** The time remaining until we can record an event again */
  public float timeRemaining;

  /** Sprite used for debugging */
  public static Sprite debugSpriteSource;

  /** Events that were added from an instance of Clickable to the Input, to remove from Input in the dispose method */
  private List<ActionEventEntry> registeredActionEvents;
  
  /** Events that were added from an instance of Clickable to the Input, to remove from Input in the dispose method */
  private List<TouchEventEntry> registeredTouchEvents;
  
  /** The area which if clicked, triggers an event. Set in
   * {@link #setAreaSize(float, float)} or {@link #setArea(Rectangle)} */
  private Rectangle area;

  /** Debugging sprite */
  private Sprite debugSprite;

  private float offsetX, offsetY;

  // endregion fields

  // region constructors

  public Clickable(GameObject obj)
  {

    super(obj);

    if (DolphinOES.debug.isEnabled())
      this.debugSprite = new Sprite(debugSpriteSource);
    this.cooldown = 1f;
    this.timeRemaining = 0f;

    registeredActionEvents = new ArrayList<Clickable.ActionEventEntry>();
    registeredTouchEvents = new ArrayList<Clickable.TouchEventEntry>();
    
    GameBehavior.setDebugKeys(getClass(), new Integer[] { Keys.CONTROL_LEFT, Keys.Z });

    
  }

  @Override
  protected void initialiseClass()
  {

    if (!DolphinOES.debug.isEnabled())
      return;
    if (debugSpriteSource == null)
    {
      debugSpriteSource = new Sprite(
          DolphinOES.assets.get(
              DolphinOES.assets.frameworkAssetsFolder + "White.png",
              Texture.class));
      debugSpriteSource.setColor(new Color(218 / 255f, 165 / 255f,
          32 / 255f, 0.5f));
    }
  }

  // endregion constructors

  // region methods

  @Override
  public void updateDebug()
  {

    if (enabled )
    {
      debugSprite.setBounds(area.x, area.y, area.width, area.height);
      debugSprite.draw(object.gameBatch.spriteBatch);
    }
  }

  @Override
  public void update()
  {

    // adapt the area based on the position of the object
    area.x = object.pos.getX() + offsetX;
    area.y = object.pos.getY() + offsetY;

    timeRemaining -= DolphinOES.time.getDeltaTime();

  }

  public Rectangle getArea()
  {

    return area;
  }

  public void setAreaSize(float width, float height)
  {

    offsetX = -width / 2;
    offsetY = -height / 2;
    this.area = new Rectangle(object.pos.getX() - width / 2,
        object.pos.getY() - height / 2, width, height);
    
  }

  public void setAreaSize(float width, float height, float offsetX,
      float offsetY)
  {

    this.offsetX = offsetX;
    this.offsetY = offsetY;
    this.area = new Rectangle(object.pos.getX() + offsetX,
        object.pos.getY() + offsetY, width, height);
    
  }

  public void setArea(Rectangle area)
  {

    this.area = area;
  }
  
  /** Adds an action event to {@link DolphinOES#input}. To read more on what each parameter does, visit
   * {@link Input#addActionEvent(String, int, ActionEvent, InputEventType)}, as that is the method that will be accessed
   * from this one after some tweaking. For adding events related to individual fingers other than the first one, use
   * {@link #addTouchEvent(String, int, TouchEvent, InputEventType)}
   * 
   * @param inputProcessorName
   * @param action
   * @param event
   * @param eventType
   * @param inside Whether to run the action when the action happens inside or outside of clickable area
   */
  public void addActionEvent(String inputProcessorName, int action, final ActionEvent event, InputEventType eventType, final boolean inside)
  {
    
    // make event that encapsulates the passed event
    ActionEvent clickableEvent = new ActionEvent()
    {
      @Override
      public boolean run(ActionData inputData)
      {
        // event will not run if mouse position is not within bounds.
        if (area.contains(object.gameBatch.mousePosition) == inside)
          return event.run(inputData);
        else
          return false;
        
      }
    };
    
    DolphinOES.input.addActionEvent(inputProcessorName, action, clickableEvent, eventType);
    registeredActionEvents.add(new ActionEventEntry(inputProcessorName, clickableEvent));
    
  }
  
  public void addTouchEvent(String inputProcessorName, int action, final TouchEvent touchEvent, InputEventType eventType, final boolean inside)
  {
    // make event that encapsulates the passed event
    TouchEvent clickableEvent = new TouchEvent()
    {
      @Override
      public boolean run(TouchData inputData)
      {
        // translate touch input to a mouse position
        Vector3 translatedTouchPosition = new Vector3(inputData.getPosition(), 0);
        object.gameBatch.camera.unproject(translatedTouchPosition);
        
        
        if (area.contains(translatedTouchPosition.x, translatedTouchPosition.y) == inside)
          return touchEvent.run(inputData);
        else
          return false;
      }
    };
    
    DolphinOES.input.addTouchEvent(inputProcessorName, action, clickableEvent, eventType);
    registeredTouchEvents.add(new TouchEventEntry(inputProcessorName, clickableEvent));
  }

  @Override
  public void dispose()
  {

    for (int i = 0; i < registeredActionEvents.size(); i++)
    {
      DolphinOES.input.removeActionEvent(registeredActionEvents.get(i).inputProcessor, registeredActionEvents.get(i).event);
    }
    
    for (int i = 0; i < registeredTouchEvents.size(); i++)
    {
      DolphinOES.input.removeTouchEvent(registeredTouchEvents.get(i).inputProcessor, registeredTouchEvents.get(i).event);
    }
    
  }

  // 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