|
@@ -1,9 +1,22 @@
|
|
|
package com.bomostory.sceneeditmodule.screen.view
|
|
|
|
|
|
import android.content.Context
|
|
|
+import android.graphics.*
|
|
|
+import android.support.v4.content.ContextCompat
|
|
|
import android.util.AttributeSet
|
|
|
+import android.view.View
|
|
|
+import com.example.tfat.myapplication.R
|
|
|
+import android.graphics.drawable.VectorDrawable
|
|
|
+import android.graphics.BitmapFactory
|
|
|
+import android.graphics.drawable.BitmapDrawable
|
|
|
+import android.graphics.Bitmap
|
|
|
+import android.view.MotionEvent
|
|
|
+import android.widget.RelativeLayout
|
|
|
+import com.bomostory.sceneeditmodule.basicdata.Actor
|
|
|
+import com.bomostory.sceneeditmodule.basicdata.Dialogue
|
|
|
+import kotlinx.android.synthetic.main.actor_view.view.*
|
|
|
|
|
|
-class DialogueView : EditActorView {
|
|
|
+class DialogueView : EditActorView{
|
|
|
|
|
|
constructor(context: Context) : super(context)
|
|
|
|
|
@@ -11,4 +24,285 @@ class DialogueView : EditActorView {
|
|
|
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)
|
|
|
|
|
|
+ var dialogue = Dialogue()
|
|
|
+ set(value) {
|
|
|
+ value?.let {
|
|
|
+ field = value
|
|
|
+ val layoutParams = RelativeLayout.LayoutParams(it.sideLength, it.sideHeight)
|
|
|
+ layoutParams.leftMargin = it.positionX
|
|
|
+ layoutParams.topMargin = it.positionY
|
|
|
+ actor_layout.layoutParams = layoutParams
|
|
|
+ val actorLayoutParams = RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
|
|
|
+ actor_view_layout.layoutParams = actorLayoutParams
|
|
|
+ when(it.isMoveable) {
|
|
|
+ true -> actor_view_layout.setOnTouchListener(actorOnTouchListener())
|
|
|
+ }
|
|
|
+ when(it.isSelect){
|
|
|
+ true -> change_size_view.visibility = View.VISIBLE
|
|
|
+ false -> change_size_view.visibility = View.GONE
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override var actor = Actor()
|
|
|
+
|
|
|
+ override fun onDraw(canvas: Canvas?) {
|
|
|
+ super.onDraw(canvas)
|
|
|
+ val mPaint = Paint()
|
|
|
+ mPaint.style = Paint.Style.STROKE
|
|
|
+ mPaint.strokeWidth = 1f
|
|
|
+ mPaint.isAntiAlias = true
|
|
|
+ mPaint.color = Color.parseColor("#000000")
|
|
|
+ mPaint.textSize = 40f
|
|
|
+ mPaint.textAlign = Paint.Align.CENTER
|
|
|
+ var mResId = dialogue.resource
|
|
|
+ val drawable = ContextCompat.getDrawable(context, mResId)
|
|
|
+ var valuePaddingInPixels = resources.getDimension(R.dimen.edit_actor_padding)
|
|
|
+ if (drawable is BitmapDrawable) {
|
|
|
+ var bitmap = BitmapFactory.decodeResource(context.resources, mResId)
|
|
|
+ canvas?.drawBitmap(bitmap,0f,0f, null)
|
|
|
+ } else if (drawable is VectorDrawable) {
|
|
|
+ canvas?.drawBitmap(getBitmap(drawable),null, RectF(dialogue.positionX.toFloat() + valuePaddingInPixels, dialogue.positionY.toFloat() + valuePaddingInPixels,
|
|
|
+ dialogue.positionX.toFloat() + dialogue.sideLength.toFloat() - valuePaddingInPixels,
|
|
|
+ dialogue.positionY.toFloat() + dialogue.sideHeight.toFloat() - valuePaddingInPixels), null)
|
|
|
+ } else {
|
|
|
+ throw IllegalArgumentException("unsupported drawable type")
|
|
|
+ }
|
|
|
+ canvas?.drawText("Sample Text",dialogue.positionX.toFloat() + dialogue.sideLength.div(2),dialogue.positionY.toFloat() + dialogue.sideHeight.div(2), mPaint)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun getBitmap(vectorDrawable: VectorDrawable): Bitmap {
|
|
|
+ val bitmap = Bitmap.createBitmap(vectorDrawable.intrinsicWidth,
|
|
|
+ vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
|
|
|
+ val canvas = Canvas(bitmap)
|
|
|
+ vectorDrawable.setBounds(0, 0, canvas.width, canvas.height)
|
|
|
+ vectorDrawable.draw(canvas)
|
|
|
+ return bitmap
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun actorOnTouchListener() : View.OnTouchListener {
|
|
|
+ var clickPeriod = 0L
|
|
|
+ var positionStart = 0
|
|
|
+ var positionEnd = 0
|
|
|
+ var mx: Int
|
|
|
+ var my: Int
|
|
|
+ return View.OnTouchListener { view, motionEvent ->
|
|
|
+ when (motionEvent.action) {
|
|
|
+ MotionEvent.ACTION_DOWN -> {
|
|
|
+ clickPeriod = System.currentTimeMillis()
|
|
|
+ positionStart = (motionEvent.x).toInt()
|
|
|
+ positionEnd = (motionEvent.y).toInt()
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_MOVE -> {
|
|
|
+ if (!(Math.abs(motionEvent.x - positionStart) < 10 && Math.abs(motionEvent.y - positionEnd) < 10) && actor.isSelect) {
|
|
|
+ mx = motionEvent.rawX.toInt() - positionStart
|
|
|
+ my = motionEvent.rawY.toInt() - positionEnd
|
|
|
+ var laParams = actor_layout.layoutParams as RelativeLayout.LayoutParams
|
|
|
+ laParams.setMargins(mx, my - 200, 0, 0)
|
|
|
+ actor_layout.layoutParams = laParams
|
|
|
+ val actorLayoutParams = RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
|
|
|
+ view.layoutParams = actorLayoutParams
|
|
|
+ actor.positionX = mx
|
|
|
+ actor.positionY = my - 200
|
|
|
+ }
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_UP -> {
|
|
|
+ if (Math.abs(motionEvent.x - positionStart) < 10 && Math.abs(motionEvent.y - positionEnd) < 10 && (System.currentTimeMillis() - clickPeriod) > 1000) {
|
|
|
+ actorCallback?.onActorLongClick(actor)
|
|
|
+ } else if (Math.abs(motionEvent.x - positionStart) < 10 && Math.abs(motionEvent.y - positionEnd) < 10) {
|
|
|
+ actorCallback?.onActorSelected(actor)
|
|
|
+ } else {
|
|
|
+ if (actor.isSelect){
|
|
|
+ actorCallback?.onActorChange(actor)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return@OnTouchListener true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun rightBottomSizeChangeListener() : View.OnTouchListener {
|
|
|
+ var positionX = 0
|
|
|
+ var positionY = 0
|
|
|
+ var sizeX = 0
|
|
|
+ var sizeY = 0
|
|
|
+ var x = 0
|
|
|
+ var y = 0
|
|
|
+ return View.OnTouchListener { view, motionEvent ->
|
|
|
+ when (motionEvent.action) {
|
|
|
+ MotionEvent.ACTION_DOWN -> {
|
|
|
+ val layoutParams = actor_layout.layoutParams as RelativeLayout.LayoutParams
|
|
|
+ sizeX = layoutParams.width
|
|
|
+ sizeY = layoutParams.height
|
|
|
+ x = layoutParams.leftMargin
|
|
|
+ y = layoutParams.topMargin
|
|
|
+ positionX = (motionEvent.rawX).toInt()
|
|
|
+ positionY = (motionEvent.rawY).toInt()
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_MOVE -> {
|
|
|
+ var mx = motionEvent.rawX.toInt() - positionX
|
|
|
+ var my = motionEvent.rawY.toInt() - positionY
|
|
|
+ var dP = when (Math.abs(mx) > Math.abs(my)){
|
|
|
+ true -> my
|
|
|
+ false -> mx
|
|
|
+ }
|
|
|
+ var dialogueData = Dialogue().apply {
|
|
|
+ resource = dialogue.resource
|
|
|
+ this.positionX = x
|
|
|
+ this.positionY = y
|
|
|
+ positionZ = actor.positionZ
|
|
|
+ sideLength = sizeX + dP
|
|
|
+ sideHeight = sizeY + dP
|
|
|
+ isSelect = actor.isSelect
|
|
|
+ parentLayerIndex = actor.parentLayerIndex
|
|
|
+ isMoveable = actor.isMoveable
|
|
|
+ }
|
|
|
+ dialogue = dialogueData
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_UP -> {
|
|
|
+ actor.isSelect = true
|
|
|
+ actorCallback?.onActorChange(dialogue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return@OnTouchListener true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun leftTopSizeChangeListener() : View.OnTouchListener {
|
|
|
+ var positionX = 0
|
|
|
+ var positionY = 0
|
|
|
+ var sizeX = 0
|
|
|
+ var sizeY = 0
|
|
|
+ var x = 0
|
|
|
+ var y = 0
|
|
|
+ return View.OnTouchListener { view, motionEvent ->
|
|
|
+ when (motionEvent.action) {
|
|
|
+ MotionEvent.ACTION_DOWN -> {
|
|
|
+ val layoutParams = actor_layout.layoutParams as RelativeLayout.LayoutParams
|
|
|
+ sizeX = layoutParams.width
|
|
|
+ sizeY = layoutParams.height
|
|
|
+ x = layoutParams.leftMargin
|
|
|
+ y = layoutParams.topMargin
|
|
|
+ positionX = (motionEvent.rawX).toInt()
|
|
|
+ positionY = (motionEvent.rawY).toInt()
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_MOVE -> {
|
|
|
+ val mx = motionEvent.rawX.toInt() - positionX
|
|
|
+ val my = motionEvent.rawY.toInt() - positionY
|
|
|
+ var dP = when (Math.abs(mx) > Math.abs(my)){
|
|
|
+ true -> my
|
|
|
+ false -> mx
|
|
|
+ }
|
|
|
+ var dialogueData = Dialogue().apply {
|
|
|
+ resource = dialogue.resource
|
|
|
+ this.positionX = x + dP
|
|
|
+ this.positionY = y + dP
|
|
|
+ positionZ = actor.positionZ
|
|
|
+ sideLength = sizeX - dP
|
|
|
+ sideHeight = sizeY - dP
|
|
|
+ isSelect = actor.isSelect
|
|
|
+ parentLayerIndex = actor.parentLayerIndex
|
|
|
+ isMoveable = actor.isMoveable
|
|
|
+ }
|
|
|
+ dialogue = dialogueData
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_UP -> {
|
|
|
+ actor.isSelect = true
|
|
|
+ actorCallback?.onActorChange(dialogue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return@OnTouchListener true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun rightTopSizeChangeListener() : View.OnTouchListener {
|
|
|
+ var positionX = 0
|
|
|
+ var positionY = 0
|
|
|
+ var sizeX = 0
|
|
|
+ var sizeY = 0
|
|
|
+ var x = 0
|
|
|
+ var y = 0
|
|
|
+ return View.OnTouchListener { view, motionEvent ->
|
|
|
+ when (motionEvent.action) {
|
|
|
+ MotionEvent.ACTION_DOWN -> {
|
|
|
+ val layoutParams = actor_layout.layoutParams as RelativeLayout.LayoutParams
|
|
|
+ sizeX = layoutParams.width
|
|
|
+ sizeY = layoutParams.height
|
|
|
+ x = layoutParams.leftMargin
|
|
|
+ y = layoutParams.topMargin
|
|
|
+ positionX = (motionEvent.rawX).toInt()
|
|
|
+ positionY = (motionEvent.rawY).toInt()
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_MOVE -> {
|
|
|
+ var mx = motionEvent.rawX.toInt() - positionX
|
|
|
+ var my = motionEvent.rawY.toInt() - positionY
|
|
|
+ var dP = when (Math.abs(mx) > Math.abs(my)){
|
|
|
+ true -> my
|
|
|
+ false -> mx
|
|
|
+ }
|
|
|
+ var dialogueData = Dialogue().apply {
|
|
|
+ resource = dialogue.resource
|
|
|
+ this.positionX = x
|
|
|
+ this.positionY = y + my
|
|
|
+ positionZ = actor.positionZ
|
|
|
+ sideLength = sizeX + mx
|
|
|
+ sideHeight = sizeY - my
|
|
|
+ isSelect = actor.isSelect
|
|
|
+ parentLayerIndex = actor.parentLayerIndex
|
|
|
+ isMoveable = actor.isMoveable
|
|
|
+ }
|
|
|
+ dialogue = dialogueData
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_UP -> {
|
|
|
+ actor.isSelect = true
|
|
|
+ actorCallback?.onActorChange(dialogue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return@OnTouchListener true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun leftBottomSizeChangeListener() : View.OnTouchListener {
|
|
|
+ var positionX = 0
|
|
|
+ var positionY = 0
|
|
|
+ var sizeX = 0
|
|
|
+ var sizeY = 0
|
|
|
+ var x = 0
|
|
|
+ var y = 0
|
|
|
+ return View.OnTouchListener { view, motionEvent ->
|
|
|
+ when (motionEvent.action) {
|
|
|
+ MotionEvent.ACTION_DOWN -> {
|
|
|
+ val layoutParams = actor_layout.layoutParams as RelativeLayout.LayoutParams
|
|
|
+ sizeX = layoutParams.width
|
|
|
+ sizeY = layoutParams.height
|
|
|
+ x = layoutParams.leftMargin
|
|
|
+ y = layoutParams.topMargin
|
|
|
+ positionX = (motionEvent.rawX).toInt()
|
|
|
+ positionY = (motionEvent.rawY).toInt()
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_MOVE -> {
|
|
|
+ var mx = motionEvent.rawX.toInt() - positionX
|
|
|
+ var my = motionEvent.rawY.toInt() - positionY
|
|
|
+ var dialogueData = Dialogue().apply {
|
|
|
+ resource = dialogue.resource
|
|
|
+ this.positionX = x + mx
|
|
|
+ this.positionY = y
|
|
|
+ positionZ = actor.positionZ
|
|
|
+ sideLength = sizeX - mx
|
|
|
+ sideHeight = sizeY + my
|
|
|
+ isSelect = actor.isSelect
|
|
|
+ parentLayerIndex = actor.parentLayerIndex
|
|
|
+ isMoveable = actor.isMoveable
|
|
|
+ }
|
|
|
+ dialogue = dialogueData
|
|
|
+ }
|
|
|
+ MotionEvent.ACTION_UP -> {
|
|
|
+ actor.isSelect = true
|
|
|
+ actorCallback?.onActorChange(dialogue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return@OnTouchListener true
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|