faterhenry 5 лет назад
Родитель
Сommit
4d1b36063b

+ 60 - 2
src/main/java/com/bomostory/sceneeditmodule/SceneEditActivity.kt

@@ -7,11 +7,13 @@ import android.content.Context
 import android.content.DialogInterface
 import android.content.Intent
 import android.graphics.Bitmap
+import android.graphics.BitmapFactory
 import android.graphics.Canvas
 import android.os.Bundle
 import android.support.v7.app.AppCompatActivity
 import android.support.v7.widget.LinearLayoutManager
 import android.support.v7.widget.RecyclerView
+import android.support.v7.widget.helper.ItemTouchHelper
 import android.util.DisplayMetrics
 import android.view.*
 import android.widget.PopupWindow
@@ -41,6 +43,7 @@ import kotlinx.android.synthetic.main.view_control_dialogue_dialog.view.*
 import com.bomostory.sceneeditmodule.DialogueColorData
 import com.bomostory.sceneeditmodule.SceneDrawer
 import com.bomostory.sceneeditmodule.cover.CoverEditorDialog
+import com.bomostory.sceneeditmodule.layermanagement.ItemTouchHelpCallback
 import com.bomostory.sceneeditmodule.navigationbar.actor.ObjectView
 import com.bomostory.sceneeditmodule.navigationbar.actor.NonInterceptTouchRecycleView
 import com.bomostory.sceneeditmodule.screen.draw.DrawActivity
@@ -50,6 +53,7 @@ import io.reactivex.schedulers.Schedulers
 import kotlinx.android.synthetic.main.actor_select_view.view.*
 import kotlinx.android.synthetic.main.add_scene_view.view.*
 import kotlinx.android.synthetic.main.layer_management_fragment.*
+import kotlinx.android.synthetic.main.layer_management_fragment.view.*
 import kotlinx.android.synthetic.main.navigation_bar_view.view.*
 import kotlinx.android.synthetic.main.popupview_adjustment_dialog.view.*
 import kotlinx.android.synthetic.main.popupview_color_dialog.view.*
@@ -66,7 +70,7 @@ import kotlin.collections.ArrayList
 class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener, EditActorView.OnActorChangeListener,
         OnTouchBoMoSceneListener.OnSceneMoveListener, DialogInterface.OnDismissListener, FileUtils.OnSaveActorImage,
         OnTouchSceneListener.OnSceneTouchListener,LayerManagementDialog.OnLayerChange, EditTextDialog.OnSetDialogFinish,
-        BrushView.OnSelectBrush, FileUtils.OnSaveCover {
+        BrushView.OnSelectBrush, FileUtils.OnSaveCover, SceneAdapter.ItemMoveListener {
 
     private lateinit var project: Project
     private var currentSceneIndex = 0
@@ -429,6 +433,7 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
             switchLayer(0)
             initBrushView()
         } else {
+            unSelectActor()
             var intent = Intent(this, DrawActivity::class.java).apply {
                 putExtra("project", Gson().toJson(project))
             }
@@ -512,6 +517,7 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
             }
         }
         sceneAdapter = SceneAdapter(this, projectBitmaps, onSceneSelectedArrayList, currentSceneIndex, recordTimeArrayList)
+        sceneAdapter.listener = this@SceneEditActivity
         var controlSceneView = ControlSceneView(this)
         controlSceneView.setonClickAddBtn(View.OnClickListener {
             viewContainer.removeAllViews()
@@ -650,6 +656,9 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
         viewContainer.removeAllViews()
         viewContainer.addView(controlSceneView)
         startRecord.visibility = View.VISIBLE
+        var itemTouchHelperCallBack = ItemTouchHelpCallback(sceneAdapter)
+        var itemTouchHelper = ItemTouchHelper(itemTouchHelperCallBack)
+        itemTouchHelper.attachToRecyclerView(sceneRecyclerView)
     }
 
     override fun onPause() {
@@ -1095,6 +1104,33 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
         sceneEditView.setLayerVisible(currentLayerIndex)
     }
 
+    private fun addBrushView(positionX: Int, positionY: Int, resourcePath: String) {
+        val metrics = DisplayMetrics()
+        val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
+        windowManager.defaultDisplay.getMetrics(metrics);
+        val screenWidth = metrics.widthPixels;
+        val screenHeight = screenWidth / 2
+
+        project.story?.let {
+            it.scenes?.let{
+                val actorData = Actor()
+                actorData.sideLength = BitmapFactory.decodeFile(resourcePath).width + (resources.getDimension(R.dimen.actor_padding_for_edit).toInt() * 2)
+                actorData.sideHeight = BitmapFactory.decodeFile(resourcePath).height + (resources.getDimension(R.dimen.actor_padding_for_edit).toInt() * 2)
+                actorData.positionX = positionX - resources.getDimension(R.dimen.actor_padding_for_edit).toInt()
+                actorData.positionY = positionY - resources.getDimension(R.dimen.actor_padding_for_edit).toInt()
+                actorData.positionZ = it[currentSceneIndex].layers[currentLayerIndex].actors.size
+                actorData.resourcePath = resourcePath
+                actorData.parentLayerIndex = currentLayerIndex
+                actorData.isMovable = true
+                it[currentSceneIndex].layers[currentLayerIndex].actors.add(actorData)
+                sceneEditView.scene = it[currentSceneIndex]
+            }
+        }
+        viewContainer.removeAllViews()
+        startRecord.visibility = View.VISIBLE
+        sceneEditView.setLayerVisible(currentLayerIndex)
+    }
+
     private fun addObject(positionX: Int, positionY: Int, resourcePath: String) {
         val metrics = DisplayMetrics()
         val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
@@ -1631,7 +1667,7 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
             ACTOR_FROM_BRUSH -> {
                 when (resultCode) {
                     Activity.RESULT_OK -> if (data != null) {
-                        addActor(ACTOR_INIT_POSITION_X, ACTOR_INIT_POSITION_Y, data.getStringExtra("brush_bitmap"))
+                        addBrushView(data.getIntExtra("brush_bitmap_positionX",0), data.getIntExtra("brush_bitmap_positionY",0), data.getStringExtra("brush_bitmap"))
                     }
                     Activity.RESULT_CANCELED -> {
                     }
@@ -1944,4 +1980,26 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
         }
         dialog.show(supportFragmentManager)
     }
+
+    override fun clearView() {
+
+    }
+
+    override fun onItemMove(fromPosition: Int, toPosition: Int) {
+        project.story?.let {
+            val scene = it.scenes[fromPosition]
+            it.scenes.removeAt(fromPosition)
+            it.scenes.add(toPosition, scene)
+            when(currentSceneIndex){
+                fromPosition -> {
+                    currentSceneIndex = toPosition
+                    sceneEditView.scene = it.scenes[toPosition]
+                }
+                toPosition -> {
+                    currentSceneIndex = fromPosition
+                    sceneEditView.scene = it.scenes[fromPosition]
+                }
+            }
+        }
+    }
 }

+ 0 - 1
src/main/java/com/bomostory/sceneeditmodule/layermanagement/LayerAdapter.kt

@@ -58,6 +58,5 @@ class LayerAdapter (actorData: ArrayList<Actor>, actorListeners: ArrayList<View.
     interface ItemMoveListener{
         fun onItemMove(layerNum: Int, fromPosition: Int, toPosition: Int)
         fun clearView()
-
     }
 }

+ 30 - 4
src/main/java/com/bomostory/sceneeditmodule/navigationbar/scene/SceneAdapter.kt

@@ -7,23 +7,27 @@ import android.support.v7.widget.RecyclerView
 import android.view.*
 import android.widget.ImageView
 import android.widget.TextView
+import com.bomostory.sceneeditmodule.layermanagement.ItemMoveSwipeListener
+import com.bomostory.sceneeditmodule.layermanagement.LayerAdapter
 import com.bomostory.sceneeditmodule.utils.Utils
 import com.example.tfat.myapplication.R
 import kotlinx.android.synthetic.main.item_scene.view.*
+import java.util.*
 import kotlin.collections.ArrayList
 
-class SceneAdapter(var context : Context, var data: ArrayList<Bitmap?>, onClickListenerList: ArrayList<View.OnClickListener>, sceneIndex : Int, recordTime: ArrayList<Long?>) : RecyclerView.Adapter<SceneAdapter.ViewHolder>() {
+class SceneAdapter(var context : Context, var data: ArrayList<Bitmap?>, onClickListenerList: ArrayList<View.OnClickListener>, sceneIndex : Int, recordTime: ArrayList<Long?>) : RecyclerView.Adapter<SceneAdapter.ViewHolder>(), ItemMoveSwipeListener {
     private var onClickListenerList = onClickListenerList
     private var sceneIndex = sceneIndex
     private var recordTimeArrayList = recordTime
     private var cover = context.resources.getString(R.string.uikit_edit_cover)
+    lateinit var listener: ItemMoveListener
 
     override fun getItemCount(): Int {
         return data.size
     }
 
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
-        holder.bind(data?.get(position), position, onClickListenerList, sceneIndex, recordTimeArrayList[position], cover)
+        holder.bind(data?.get(position), position, onClickListenerList[position], sceneIndex, recordTimeArrayList[position], cover)
     }
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@@ -32,9 +36,9 @@ class SceneAdapter(var context : Context, var data: ArrayList<Bitmap?>, onClickL
     }
 
     class ViewHolder (view : View) : RecyclerView.ViewHolder(view) {
-        fun bind(bitmap: Bitmap?, position : Int, onClickListenerList: ArrayList<View.OnClickListener>, sceneIndex: Int, recordTime: Long?, cover: String) {
+        fun bind(bitmap: Bitmap?, position : Int, onClickListener:View.OnClickListener, sceneIndex: Int, recordTime: Long?, cover: String) {
             itemView.findViewById<ImageView>(R.id.scene_image).setImageBitmap(bitmap)
-            itemView.findViewById<ImageView>(R.id.scene_image).setOnClickListener(onClickListenerList[position])
+            itemView.findViewById<ImageView>(R.id.scene_image).setOnClickListener(onClickListener)
             if (position == 0){
                 itemView.findViewById<TextView>(R.id.scene_number).text = cover
             } else {
@@ -59,4 +63,26 @@ class SceneAdapter(var context : Context, var data: ArrayList<Bitmap?>, onClickL
         this.recordTimeArrayList = recordTimeArrayList
         this.notifyDataSetChanged()
     }
+
+    override fun clearView() {
+        listener.clearView()
+    }
+
+    override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
+        if (fromPosition != 0 && toPosition != 0) {
+            Collections.swap(onClickListenerList,fromPosition,toPosition)
+            notifyItemMoved(fromPosition, toPosition)
+            listener.onItemMove(fromPosition, toPosition)
+        }
+        return true
+    }
+
+    override fun onItemSwipe(position: Int) {
+
+    }
+
+    interface ItemMoveListener{
+        fun onItemMove(fromPosition: Int, toPosition: Int)
+        fun clearView()
+    }
 }

+ 67 - 1
src/main/java/com/bomostory/sceneeditmodule/screen/draw/DrawActivity.kt

@@ -4,9 +4,13 @@ import android.app.Activity
 import android.arch.lifecycle.Observer
 import android.arch.lifecycle.ViewModelProviders
 import android.content.Intent
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
 import android.graphics.Color
 import android.support.v7.app.AppCompatActivity
 import android.os.Bundle
+import android.support.v4.graphics.BitmapCompat
+import android.util.Log
 import android.view.Gravity
 import android.view.LayoutInflater
 import android.view.View
@@ -20,6 +24,7 @@ import com.google.gson.Gson
 import com.kdanmobile.kdanbrushlib.model.KdanBrush
 import com.kdanmobile.kdanbrushlib.widget.DrawView
 import kotlinx.android.synthetic.main.activity_draw.*
+import kotlinx.android.synthetic.main.activity_scene_edit.*
 import kotlinx.android.synthetic.main.popupview_set_color.view.*
 import kotlinx.android.synthetic.main.scene_brush_view.view.*
 
@@ -27,6 +32,11 @@ class DrawActivity : AppCompatActivity() , FileUtils.OnSaveActorImage{
     private lateinit var viewModel: DrawViewModel
     private lateinit var drawView: DrawView
     private lateinit var popupView: View
+    private lateinit var project: Project
+    private var left = 0
+    private var right = 0
+    private var top = 0
+    private var bottom = 0
     private var popupWindow = PopupWindow()
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -38,6 +48,18 @@ class DrawActivity : AppCompatActivity() , FileUtils.OnSaveActorImage{
         drawView.enableUndoRedo(true)
         drawViewLayout.addView(drawView)
 
+        project = Gson().fromJson<Project>(intent.getStringExtra("project"), Project::class.java)
+        project.story?.let {
+            it.scenes?.let {
+                var layoutPrams = view_drawActivity_sceneEditView.layoutParams
+                layoutPrams.width = it[1].sceneWidth
+                layoutPrams.height = it[1].sceneWidth.div(2)
+                view_drawActivity_sceneEditView.scene = it[1]
+                var drawViewLayoutPrams = drawViewLayout.layoutParams
+                drawViewLayoutPrams.width = it[1].sceneWidth
+                drawViewLayoutPrams.height = it[1].sceneWidth.div(2)
+            }
+        }
         //TODO change button to your widget
         brush_view.iv_brush_view_brush1.setOnClickListener(this::onClickPencil)
         brush_view.iv_brush_view_brush2.setOnClickListener(this::onClickCrayon)
@@ -184,7 +206,49 @@ class DrawActivity : AppCompatActivity() , FileUtils.OnSaveActorImage{
     }
 
     private fun onDoneClick(view: View?) {
-        FileUtils.saveBitmapForActor(this, Gson().fromJson<Project>(intent.getStringExtra("project"), Project::class.java), drawView.drawerBitmap, System.currentTimeMillis().toString())
+        for(i in 0 until drawView.drawerBitmap.width) {
+            for (j in 0 until drawView.drawerBitmap.height) {
+                if(drawView.drawerBitmap.getPixel(i, j) != 0) {
+                    left = i
+                    break
+                }
+                if (left > 0) break
+            }
+        }
+        for(i in 0 until drawView.drawerBitmap.height) {
+            for (j in 0 until drawView.drawerBitmap.width) {
+                if(drawView.drawerBitmap.getPixel(j, i) != 0) {
+                    top = i
+                    break
+                }
+                if (top > 0) break
+            }
+        }
+        for(i in drawView.drawerBitmap.width - 1 downTo 0) {
+            for (j in 0 until drawView.drawerBitmap.height) {
+                if(drawView.drawerBitmap.getPixel(i, j) != 0) {
+                    right = i
+                    break
+                }
+                if (right > 0) break
+            }
+        }
+        for(i in drawView.drawerBitmap.height - 1 downTo 0) {
+            for (j in 0 until drawView.drawerBitmap.width) {
+                if(drawView.drawerBitmap.getPixel(j, i) != 0) {
+                    bottom = i
+                    Log.d("TEST12345", "TT" + i.toString())
+                    break
+                }
+                if (bottom > 0) break
+            }
+        }
+        if ((right - left) <= 0 || (bottom - top <=0)) this.finish()
+        else {
+            val bitmap = Bitmap.createBitmap(drawView.drawerBitmap, left, top, right - left, bottom - top)
+            val project = Gson().fromJson<Project>(intent.getStringExtra("project"), Project::class.java)
+            FileUtils.saveBitmapForActor(this, project, bitmap, System.currentTimeMillis().toString())
+        }
     }
 
     private fun onBrushSizeChanged(brushSize: Int) {
@@ -290,6 +354,8 @@ class DrawActivity : AppCompatActivity() , FileUtils.OnSaveActorImage{
     override fun onSaveActorImage(filePath: String) {
         var intent = Intent().apply {
             putExtra("brush_bitmap", filePath)
+            putExtra("brush_bitmap_positionX", left)
+            putExtra("brush_bitmap_positionY", top)
         }
         this.setResult(Activity.RESULT_OK, intent)
         this.finish()

+ 2 - 2
src/main/java/com/bomostory/sceneeditmodule/screen/view/LayerView.kt

@@ -31,7 +31,7 @@ class LayerView : RelativeLayout {
             for (actor in value) {
                 if (actor.isDialogue){
                     var dialogueView = DialogueView(context)
-                    dialogueView.actorCallback = context as EditActorView.OnActorChangeListener
+                    if(context is EditActorView.OnActorChangeListener) dialogueView.actorCallback = context as EditActorView.OnActorChangeListener
                     addView(dialogueView)
                     val layoutParams = RelativeLayout.LayoutParams(actor.sideLength, actor.sideLength)
                     layoutParams.leftMargin = actor.positionX
@@ -39,7 +39,7 @@ class LayerView : RelativeLayout {
                     dialogueView.dialogue = actor
                 } else if (!actor.isDialogue) {
                     var actorView = EditActorView(context)
-                    actorView.actorCallback = context as EditActorView.OnActorChangeListener
+                    if(context is EditActorView.OnActorChangeListener) actorView.actorCallback = context as EditActorView.OnActorChangeListener
                     addView(actorView)
                     val layoutParams = RelativeLayout.LayoutParams(actor.sideLength, actor.sideLength)
                     layoutParams.leftMargin = actor.positionX

+ 14 - 2
src/main/res/layout/activity_draw.xml

@@ -4,16 +4,28 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:background="#212121"
     tools:context="com.bomostory.sceneeditmodule.screen.draw.DrawActivity">
 
+    <com.bomostory.sceneeditmodule.screen.view.SceneView1
+        android:id="@+id/view_drawActivity_sceneEditView"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintBottom_toBottomOf="parent"/>
+
     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent">
 
         <RelativeLayout
             android:id="@+id/drawViewLayout"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent" />
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_centerHorizontal="true"
+            android:layout_centerVertical="true"/>
     </RelativeLayout>
 
     <com.bomostory.sceneeditmodule.navigationbar.brush.BrushView

+ 1 - 1
src/main/res/layout/add_scene_view.xml

@@ -30,7 +30,7 @@ android:background="#212121">
             android:drawableLeft="@drawable/ic_layer_bg_w"
             android:text="Scene"
             android:layout_marginLeft="182dp"
-            app:layout_constraintLeft_toRightOf="@id/btn_add_scene_view_choose_theme"
+            app:layout_constraintLeft_toRightOf="@id/btn_replace_scene_view_choose_theme"
             app:layout_constraintRight_toLeftOf="@id/btn_add_scene_view_character"/>
         <Button
             android:id="@+id/btn_add_scene_view_character"

+ 2 - 0
src/main/res/values/dimens.xml

@@ -28,4 +28,6 @@
     <dimen name="popup_view_start_over_width">324dp</dimen>
     <dimen name="popup_view_start_over_height">220dp</dimen>
     <dimen name="popup_view_start_over_padding_bottom">146dp</dimen>
+
+    <dimen name="actor_padding_for_edit">24dp</dimen>
 </resources>