com.sidereal.dolphinoes.behaviors.input.Clickable.java Source code

Java tutorial

Introduction

Here is the source code for com.sidereal.dolphinoes.behaviors.input.Clickable.java

Source

/*******************************************************************************
 * Copyright 2014 See AUTHORS file.
 * 
 * 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
}