From f31774bf5aae659efd04b7912a82c10cc41a3f86 Mon Sep 17 00:00:00 2001 From: fabianterhorst Date: Thu, 6 Apr 2017 17:15:44 +0200 Subject: [PATCH] * remove debug messages and add listener support --- .../isometric/IsometricView.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/src/main/java/io/fabianterhorst/isometric/IsometricView.java b/lib/src/main/java/io/fabianterhorst/isometric/IsometricView.java index 76bb5ca..8593178 100644 --- a/lib/src/main/java/io/fabianterhorst/isometric/IsometricView.java +++ b/lib/src/main/java/io/fabianterhorst/isometric/IsometricView.java @@ -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; @@ -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) { @@ -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(); } @@ -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 @@ -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); }