package game.habits;
import engine.geometry.Point;
import engine.geometry.Rectangle;
import engine.hierarchy.DefaultHabit;
import engine.interfaces.Clock;
import engine.interfaces.Keyboard;
import engine.interfaces.Mouse;
import engine.interfaces.Mouse.Button;
import engine.utility.Geometry;
public class ClickHabit extends DefaultHabit {
public interface Listener {
void onClick(ClickHabit button);
}
private Listener listener;
private Rectangle bounds;
public ClickHabit(final Listener listener, final Rectangle bounds) {
this.listener = listener;
this.bounds = bounds;
}
@Override
public void onBeforeMove(final Keyboard keyboard, final Mouse mouse, final Clock clock) {
Point mousePos = new Point(mouse.getX(), mouse.getY());
if (mouse.wasReleased(Button.MB_LEFT) && Geometry.pointIntersectsRectangle(mousePos, bounds)) {
if (listener != null) {
listener.onClick(this);
}
}
}
}