Browse Source

Fix SceneDrawer::drawScene

cooperku_kdanmobile 6 years ago
parent
commit
a78ed5193c
1 changed files with 64 additions and 18 deletions
  1. 64 18
      src/main/java/com/bomostory/sceneeditmodule/SceneDrawer.kt

+ 64 - 18
src/main/java/com/bomostory/sceneeditmodule/SceneDrawer.kt

@@ -1,10 +1,10 @@
 package com.bomostory.sceneeditmodule
 
 import android.content.Context
-import android.graphics.Bitmap
-import android.graphics.BitmapFactory
-import android.graphics.Canvas
-import android.graphics.Paint
+import android.graphics.*
+import android.util.DisplayMetrics
+import android.view.WindowManager
+import com.bomostory.sceneeditmodule.basicdata.Actor
 import com.bomostory.sceneeditmodule.basicdata.Scene
 import kotlin.math.pow
 
@@ -17,31 +17,77 @@ object SceneDrawer {
             sceneBitmap = Bitmap.createScaledBitmap(sceneBitmap, scaleWidth, scaleHeight, true)
             sceneBitmap = sceneBitmap?.copy(Bitmap.Config.ARGB_8888, true)
 
+            val metrics = DisplayMetrics()
+            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
+            windowManager.defaultDisplay.getMetrics(metrics);
+            val screenWidth = metrics.widthPixels
+            val screenHeight = metrics.widthPixels / 2
+            val widthScaleFactor: Float = scaleWidth / screenWidth.toFloat()
+            val heightScaleFactor: Float = scaleHeight / screenHeight.toFloat()
             val canvas = Canvas(sceneBitmap)
             for (layer in layers) {
                 for (actor in layer.actors) {
-                    if (!actor.isDialogue) {
-                        var actorBitmap = BitmapFactory.decodeFile(actor.resourcePath)
-                        actorBitmap = Bitmap.createScaledBitmap(actorBitmap, actor.sideLength, actor.sideHeight, true)
-
+                    var bitmap = if (actor.isDialogue) DialogueDrawer.drawDialogue(context, actor) else BitmapFactory.decodeFile(actor.resourcePath)
+                    if (bitmap != null) {
                         canvas.save()
                         canvas.translate(trackX / 2f.pow(layers.indexOf(layer)), 0f)
-                        canvas.drawBitmap(actorBitmap, actor.positionX.toFloat(), actor.positionY.toFloat(), Paint())
+                        drawActor(canvas, actor, bitmap, widthScaleFactor, heightScaleFactor)
                         canvas.restore()
-
-                        if (!actorBitmap.isRecycled) {
-                            actorBitmap.recycle()
+                        if (!bitmap.isRecycled) {
+                            bitmap.recycle()
                         }
-                    } else {
-                        canvas.save()
-                        canvas.translate(trackX / 2f.pow(layers.indexOf(layer)), 0f)
-                        canvas?.drawBitmap(DialogueDrawer.drawDialogue(context, actor), actor.positionX.toFloat(), actor.positionY.toFloat(), null)
-                        canvas.restore()
+                        bitmap = null
                     }
-
                 }
             }
         }
         return sceneBitmap
     }
+
+    private fun drawActor(canvas: Canvas, actor: Actor, bitmap: Bitmap, widthScaleFactor: Float, heightScaleFactor: Float) {
+
+        val CONTROLLER_RADIUS = 25
+        var drawBitmap: Bitmap? = null
+        if (!actor.isDialogue) {
+            var actorWidth = (actor.sideLength - CONTROLLER_RADIUS * 4).toFloat()
+            var actorHeight = (actor.sideHeight - CONTROLLER_RADIUS * 4).toFloat()
+            if (bitmap.width > bitmap.height) {
+                actorHeight = actorHeight * bitmap.height / bitmap.width
+            } else {
+                actorWidth = actorWidth * bitmap.width / bitmap.height
+            }
+            var bitmapWidth = actorWidth * widthScaleFactor
+            var bitmapHeight = actorHeight * heightScaleFactor
+            drawBitmap = Bitmap.createScaledBitmap(bitmap, bitmapWidth.toInt(), bitmapHeight.toInt(), true)
+
+            var actorX = (actor.positionX + CONTROLLER_RADIUS * 2).toFloat()
+            var actorY = (actor.positionY + CONTROLLER_RADIUS * 2).toFloat()
+            actorX += (Math.max(actorWidth, actorHeight) - actorWidth) * 0.5f
+            actorY += (Math.max(actorWidth, actorHeight) - actorHeight) * 0.5f
+            actorX *= widthScaleFactor
+            actorY *= heightScaleFactor
+
+            val paint = Paint()
+            paint.alpha = (actor.opacity * 255).toInt()
+            if (actor.isMirror)
+                canvas.scale(-1f, 1f, actorX + bitmapWidth / 2, actorY + bitmapHeight / 2)
+            canvas.drawBitmap(drawBitmap, actorX, actorY, paint)
+        } else {
+            var actorWidth = (actor.sideLength).toFloat()
+            var actorHeight = (actor.sideHeight).toFloat()
+            var bitmapWidth = actorWidth * widthScaleFactor
+            var bitmapHeight = actorHeight * heightScaleFactor
+            drawBitmap = Bitmap.createScaledBitmap(bitmap, bitmapWidth.toInt(), bitmapHeight.toInt(), true)
+
+            var actorX = (actor.positionX).toFloat()
+            var actorY = (actor.positionY).toFloat()
+            actorX *= widthScaleFactor
+            actorY *= heightScaleFactor
+            canvas?.drawBitmap(drawBitmap, actorX, actorY, null)
+        }
+        if (!drawBitmap.isRecycled) {
+            drawBitmap.recycle()
+        }
+        drawBitmap = null
+    }
 }