Skip to content

Commit

Permalink
* remove debug messages and add listener support
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianTerhorst committed Apr 6, 2017
1 parent f64719b commit f31774b
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/src/main/java/io/fabianterhorst/isometric/IsometricView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

Expand All @@ -14,8 +14,14 @@

public class IsometricView extends View {

public interface OnItemClickListener {
void onClick(@NonNull Isometric.Item item);
}

private final Isometric isometric = new Isometric();

private OnItemClickListener listener;

private boolean sort = true;

public IsometricView(Context context) {
Expand All @@ -26,6 +32,10 @@ public void setSort(boolean sort) {
this.sort = sort;
}

public void setClickListener(OnItemClickListener listener) {
this.listener = listener;
}

public void clear() {
isometric.clear();
}
Expand Down Expand Up @@ -54,17 +64,13 @@ public IsometricView(Context context, AttributeSet attrs, int defStyleAttr, int
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
long time = System.nanoTime();
isometric.measure(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec), sort);
Log.d("measure time", String.valueOf(System.nanoTime() - time));
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long time = System.nanoTime();
isometric.draw(canvas);
Log.d("draw time", String.valueOf(System.nanoTime() - time));
}

@Override
Expand All @@ -74,14 +80,16 @@ public boolean performClick() {

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Isometric.Item item = isometric.findItemForPosition(new Point(event.getX(), event.getY()));
if (item != null) {
Log.d("item", "found");
if (listener != null) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Isometric.Item item = isometric.findItemForPosition(new Point(event.getX(), event.getY()));
if (item != null) {
listener.onClick(item);
}
performClick();
}
performClick();
}
return super.onTouchEvent(event);
}
Expand Down

0 comments on commit f31774b

Please sign in to comment.