From 9ea581aabfaf015f6b1259b8f8f04d2023618591 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 24 Feb 2020 22:12:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/page/ContentTextView.kt | 28 ++++++++++++++----- .../app/ui/book/read/page/ContentView.kt | 12 ++++++-- .../page/delegate/HorizontalPageDelegate.kt | 17 +++++++++-- .../book/read/page/delegate/PageDelegate.kt | 9 +++++- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index c06c74618..7d2262824 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -176,7 +176,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at linePos = 0 } - fun selectText(x: Float, y: Float): Boolean { + fun selectText(x: Float, y: Float, select: (lineIndex: Int, charIndex: Int) -> Unit) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) { if (y > textLine.lineTop && y < textLine.lineBottom) { for ((charIndex, textChar) in textLine.textChars.withIndex()) { @@ -189,13 +189,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at selectCharEnd = charIndex upSelectedStart(textChar.start, textLine.lineBottom) upSelectedEnd(textChar.end, textLine.lineBottom) - return true + select(lineIndex, charIndex) } } break } } - return false } fun selectStartMove(x: Float, y: Float) { @@ -217,6 +216,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } } + fun selectStartMoveIndex(lineIndex: Int, charIndex: Int) { + selectLineStart = lineIndex + selectCharStart = charIndex + val textLine = textPage.textLines[lineIndex] + val textChar = textLine.textChars[charIndex] + upSelectedStart(textChar.start, textLine.lineBottom) + upSelectChars(textPage) + } + fun selectEndMove(x: Float, y: Float) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) { if (y > textLine.lineTop && y < textLine.lineBottom) { @@ -225,10 +233,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at if (selectLineEnd != lineIndex || selectCharEnd != charIndex) { selectLineEnd = lineIndex selectCharEnd = charIndex - upSelectedEnd( - textChar.end, - textLine.lineBottom - ) + upSelectedEnd(textChar.end, textLine.lineBottom) upSelectChars(textPage) } break @@ -239,6 +244,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } } + fun selectEndMoveIndex(lineIndex: Int, charIndex: Int) { + selectLineEnd = lineIndex + selectCharEnd = charIndex + val textLine = textPage.textLines[lineIndex] + val textChar = textLine.textChars[charIndex] + upSelectedEnd(textChar.end, textLine.lineBottom) + upSelectChars(textPage) + } + private fun upSelectChars(textPage: TextPage) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index bcf3098a8..512f778a7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -120,19 +120,27 @@ class ContentView(context: Context) : FrameLayout(context) { content_text_view.selectAble = selectAble } - fun selectText(e: MotionEvent): Boolean { + fun selectText(e: MotionEvent, select: (lineIndex: Int, charIndex: Int) -> Unit) { val y = e.y - headerHeight - return content_text_view.selectText(e.x, y) + return content_text_view.selectText(e.x, y, select) } fun selectStartMove(x: Float, y: Float) { content_text_view.selectStartMove(x, y - headerHeight) } + fun selectStartMoveIndex(lineIndex: Int, charIndex: Int) { + content_text_view.selectStartMoveIndex(lineIndex, charIndex) + } + fun selectEndMove(x: Float, y: Float) { content_text_view.selectEndMove(x, y - headerHeight) } + fun selectEndMoveIndex(lineIndex: Int, charIndex: Int) { + content_text_view.selectEndMoveIndex(lineIndex, charIndex) + } + fun cancelSelect() { content_text_view.cancelSelect() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt index 91e8fb252..703fe4e17 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt @@ -1,6 +1,5 @@ package io.legado.app.ui.book.read.page.delegate -import android.util.Log import android.view.MotionEvent import io.legado.app.ui.book.read.page.PageView import kotlin.math.abs @@ -11,7 +10,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie when (event.action) { MotionEvent.ACTION_MOVE -> { if (isTextSelected) { - Log.e("selected", "--------------------------") + selectText(event) } else { scroll(event) } @@ -51,4 +50,18 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie setTouchPoint(event.x, event.y) } } + + private fun selectText(event: MotionEvent) { + curPage.selectText(event) { lineIndex, charIndex -> + if (lineIndex > firstLineIndex + || (lineIndex == firstLineIndex && charIndex > firstCharIndex) + ) { + curPage.selectStartMoveIndex(firstLineIndex, firstCharIndex) + curPage.selectEndMoveIndex(lineIndex, charIndex) + } else { + curPage.selectEndMoveIndex(firstLineIndex, firstCharIndex) + curPage.selectStartMoveIndex(lineIndex, charIndex) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt index 61dbda2d8..a8a42660c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt @@ -67,6 +67,9 @@ abstract class PageDelegate(protected val pageView: PageView) : var isTextSelected = false var selectedOnDown = false + var firstLineIndex: Int = 0 + var firstCharIndex: Int = 0 + open fun setStartPoint(x: Float, y: Float, invalidate: Boolean = true) { startX = x startY = y @@ -283,7 +286,11 @@ abstract class PageDelegate(protected val pageView: PageView) : * 长按选择 */ override fun onLongPress(e: MotionEvent) { - isTextSelected = curPage.selectText(e) + curPage.selectText(e) { lineIndex, charIndex -> + isTextSelected = true + firstLineIndex = lineIndex + firstCharIndex = charIndex + } } /**