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.//fromwww.java2s.com
*
* 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.triggers;
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.sidereal.dolphinoes.architecture.AbstractEvent;
import com.sidereal.dolphinoes.architecture.DolphinOES;
import com.sidereal.dolphinoes.architecture.GameBehavior;
import com.sidereal.dolphinoes.architecture.GameObject;
/** Behavior that handles hovering over an area. Can handle being inside as well
* as outside of the designated area.
*
* @author Claudiu Bele */publicclass Hoverable extends GameBehavior
{
// region fields
private Rectangle area;
private AbstractEvent insideEvent;
private AbstractEvent outsideEvent;
privatefloat offsetX, offsetY;
private Sprite debugSprite;
privatestatic Sprite debugSpriteSource;
// endregion fields
// region constructors
public Hoverable(GameObject obj)
{
super(obj);
if (DolphinOES.debug.isEnabled())
this.debugSprite = new Sprite(debugSpriteSource);
GameBehavior.setDebugKeys(getClass(), newInteger[] { Keys.SHIFT_LEFT,
Keys.X });
}
@Override
protectedvoid initialiseClass()
{
if (!DolphinOES.debug.isEnabled())
return;
debugSpriteSource = new Sprite(
DolphinOES.assets.get(
DolphinOES.assets.frameworkAssetsFolder + "White.png",
Texture.class));
debugSpriteSource.setColor(new Color(0.7f, 0, 1, 0.5f));
}
// endregion constructors
// region methods
@Override
publicvoid updateDebug()
{
debugSprite.setBounds(area.x, area.y, area.width, area.height);
debugSprite.draw(object.gameBatch.spriteBatch);
}
@Override
publicvoid update()
{
// adapt the area based on the position of the object
// so the position of the object is in the middle of the area
area.x = object.pos.getX() + offsetX;
area.y = object.pos.getY() + offsetY;
// mouse click is inside the area of the object
if (area.contains(object.gameBatch.mousePosition))
{
if (insideEvent != null)
insideEvent.run();
} elseif (outsideEvent != null)
outsideEvent.run();
}
public Rectangle getArea()
{
return area;
}
publicvoid 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);
}
publicvoid 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);
}
publicvoid setArea(Rectangle area)
{
this.area = area;
}
public AbstractEvent getEvent()
{
return insideEvent;
}
publicvoid setEventOnInside(AbstractEvent event)
{
this.insideEvent = event;
}
publicvoid setEventOnOutside(AbstractEvent event)
{
this.outsideEvent = event;
}
// endregion methods
}