|
| 1 | +package com.github.mikephil.charting.listener |
| 2 | + |
| 3 | +import android.view.GestureDetector |
| 4 | +import android.view.GestureDetector.SimpleOnGestureListener |
| 5 | +import android.view.MotionEvent |
| 6 | +import android.view.View.OnTouchListener |
| 7 | +import com.github.mikephil.charting.charts.Chart |
| 8 | +import com.github.mikephil.charting.highlight.Highlight |
| 9 | +import kotlin.math.sqrt |
| 10 | + |
| 11 | +abstract class ChartTouchListener<T : Chart<*>?>( |
| 12 | + @JvmField protected var chart: T?) : SimpleOnGestureListener(), OnTouchListener { |
| 13 | + enum class ChartGesture { |
| 14 | + NONE, DRAG, X_ZOOM, Y_ZOOM, PINCH_ZOOM, ROTATE, SINGLE_TAP, DOUBLE_TAP, LONG_PRESS, FLING |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Returns the last gesture that has been performed on the chart. |
| 19 | + * |
| 20 | + * @return |
| 21 | + */ |
| 22 | + /** |
| 23 | + * the last touch gesture that has been performed |
| 24 | + */ |
| 25 | + var lastGesture: ChartGesture = ChartGesture.NONE |
| 26 | + protected set |
| 27 | + |
| 28 | + /** |
| 29 | + * returns the touch mode the listener is currently in |
| 30 | + * |
| 31 | + * @return |
| 32 | + */ |
| 33 | + /** |
| 34 | + * integer field that holds the current touch-state |
| 35 | + */ |
| 36 | + var touchMode: Int = NONE |
| 37 | + protected set |
| 38 | + |
| 39 | + /** |
| 40 | + * the last highlighted object (via touch) |
| 41 | + */ |
| 42 | + protected var mLastHighlighted: Highlight? = null |
| 43 | + |
| 44 | + /** |
| 45 | + * the gesturedetector used for detecting taps and longpresses, ... |
| 46 | + */ |
| 47 | + @JvmField |
| 48 | + protected var gestureDetector: GestureDetector? = GestureDetector(chart!!.getContext(), this) |
| 49 | + |
| 50 | + /** |
| 51 | + * Calls the OnChartGestureListener to do the start callback |
| 52 | + * |
| 53 | + * @param me |
| 54 | + */ |
| 55 | + fun startAction(me: MotionEvent) { |
| 56 | + val l = chart!!.getOnChartGestureListener() |
| 57 | + |
| 58 | + if (l != null) l.onChartGestureStart(me, this.lastGesture) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Calls the OnChartGestureListener to do the end callback |
| 63 | + * |
| 64 | + * @param me |
| 65 | + */ |
| 66 | + fun endAction(me: MotionEvent) { |
| 67 | + val l = chart!!.getOnChartGestureListener() |
| 68 | + |
| 69 | + if (l != null) l.onChartGestureEnd(me, this.lastGesture) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Sets the last value that was highlighted via touch. |
| 74 | + * |
| 75 | + * @param high |
| 76 | + */ |
| 77 | + fun setLastHighlighted(high: Highlight?) { |
| 78 | + mLastHighlighted = high |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Perform a highlight operation. |
| 83 | + * |
| 84 | + * @param motionEvent |
| 85 | + */ |
| 86 | + protected fun performHighlight(highlight: Highlight?, motionEvent: MotionEvent?) { |
| 87 | + if (highlight == null || highlight.equalTo(mLastHighlighted)) { |
| 88 | + chart!!.highlightValue(null, true) |
| 89 | + mLastHighlighted = null |
| 90 | + } else { |
| 91 | + chart!!.highlightValue(highlight, true) |
| 92 | + mLastHighlighted = highlight |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + companion object { |
| 97 | + // states |
| 98 | + protected const val NONE: Int = 0 |
| 99 | + protected const val DRAG: Int = 1 |
| 100 | + protected const val X_ZOOM: Int = 2 |
| 101 | + protected const val Y_ZOOM: Int = 3 |
| 102 | + protected const val PINCH_ZOOM: Int = 4 |
| 103 | + protected const val POST_ZOOM: Int = 5 |
| 104 | + protected const val ROTATE: Int = 6 |
| 105 | + |
| 106 | + /** |
| 107 | + * returns the distance between two points |
| 108 | + * |
| 109 | + * @param eventX |
| 110 | + * @param startX |
| 111 | + * @param eventY |
| 112 | + * @param startY |
| 113 | + * @return |
| 114 | + */ |
| 115 | + @JvmStatic |
| 116 | + protected fun distance(eventX: Float, startX: Float, eventY: Float, startY: Float): Float { |
| 117 | + val dx = eventX - startX |
| 118 | + val dy = eventY - startY |
| 119 | + return sqrt((dx * dx + dy * dy).toDouble()).toFloat() |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments