Browse Source

Merge remote-tracking branch 'origin/master'

cooperku_kdanmobile 6 năm trước cách đây
mục cha
commit
6c4c2a5da7

+ 7 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/AlignStyle.kt

@@ -0,0 +1,7 @@
+package com.bomostory.sceneeditmodule.cover
+
+enum class AlignStyle() {
+    Center,
+    Left,
+    Right,
+}

+ 4 - 1
src/main/java/com/bomostory/sceneeditmodule/cover/CoverEditorDialog.kt

@@ -13,6 +13,7 @@ import android.view.ViewGroup
 import com.bomostory.sceneeditmodule.SceneDrawer
 import com.bomostory.sceneeditmodule.basicdata.Project
 import com.bomostory.sceneeditmodule.utils.FileUtils
+import com.bomostory.sceneeditmodule.utils.Utils.crop
 import com.example.tfat.myapplication.R
 import kotlinx.android.synthetic.main.dialog_cover_editor.*
 import kotlinx.android.synthetic.main.dialog_cover_editor.view.*
@@ -143,9 +144,11 @@ class CoverEditorDialog : DialogFragment() {
                     val selectedSceneIndex = this@CoverEditorDialog.frontCoverSceneChooser_frontCoverEditor.selected
                     val scenes = this@CoverEditorDialog.project?.story?.scenes ?: return@OnClickListener
                     val scene = scenes[selectedSceneIndex]
+                    val alignStyle = this@CoverEditorDialog.frontCoverSceneChooser_frontCoverEditor.alignStyle
                     val width = context.resources.getDimension(R.dimen.share_dialog_screenshot_width).toInt()
                     val height = context.resources.getDimension(R.dimen.share_dialog_screenshot_height).toInt()
-                    val bitmap = SceneDrawer.drawScene(context, scene, 0, width, height) ?: return@OnClickListener
+                    val bitmap = SceneDrawer.drawScene(context, scene, 0, width, height)?.crop(alignStyle)
+                            ?: return@OnClickListener
                     val file = FileUtils.getCoverFile(project!!)
                     val fos = FileOutputStream(file)
                     bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos)

+ 37 - 8
src/main/java/com/bomostory/sceneeditmodule/cover/FrontCoverSceneChooserView.kt

@@ -9,11 +9,13 @@ import android.util.AttributeSet
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.AdapterView
+import android.widget.ArrayAdapter
 import android.widget.RadioButton
 import com.bomostory.sceneeditmodule.SceneDrawer
 import com.bomostory.sceneeditmodule.basicdata.Project
-import com.bomostory.sceneeditmodule.basicdata.Scene
 import com.bomostory.sceneeditmodule.utils.Utils
+import com.bomostory.sceneeditmodule.utils.Utils.crop
 import com.example.tfat.myapplication.R
 import kotlinx.android.synthetic.main.item_front_cover_scene_chooser.view.*
 import kotlinx.android.synthetic.main.view_front_cover_scene_chooser.view.*
@@ -24,6 +26,11 @@ class FrontCoverSceneChooserView @JvmOverloads constructor(
         defStyleAttr: Int = 0
 ) : ConstraintLayout(context, attrs, defStyleAttr) {
 
+    private val alignStyleStringMap = HashMap<AlignStyle, Int>().apply {
+        put(AlignStyle.Center, R.string.front_cover_scene_choose_dialog_align_center)
+        put(AlignStyle.Left, R.string.front_cover_scene_choose_dialog_align_left)
+        put(AlignStyle.Right, R.string.front_cover_scene_choose_dialog_align_right)
+    }
     var selected: Int = 0
         private set
     private var project: Project? = null
@@ -31,6 +38,11 @@ class FrontCoverSceneChooserView @JvmOverloads constructor(
             field = value
             update()
         }
+    val alignStyle: AlignStyle
+        get() {
+            val position = spinner_frontCoverSceneChooserDialog_align.selectedItemPosition
+            return AlignStyle.values()[position]
+        }
     var title: String = ""
         set(value) {
             tv_frontCoverSceneChooserDialog_coverTitle.text = value
@@ -57,6 +69,23 @@ class FrontCoverSceneChooserView @JvmOverloads constructor(
             orientation = LinearLayoutManager.VERTICAL
         }
         recyclerView_frontCoverSceneChooserDialog_sceneList.adapter = adapter
+
+        // align style spinner
+        val itemLayoutResId = android.R.layout.simple_dropdown_item_1line
+        spinner_frontCoverSceneChooserDialog_align.adapter = ArrayAdapter<String>(context, itemLayoutResId).apply {
+            AlignStyle.values().forEach {
+                val resId = alignStyleStringMap[it] ?: return@forEach
+                val s = context.getString(resId)
+                add(s)
+            }
+        }
+        spinner_frontCoverSceneChooserDialog_align.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
+            override fun onNothingSelected(p0: AdapterView<*>?) {}
+
+            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
+                updateCover()
+            }
+        }
     }
 
     fun setProjectAndSelected(project: Project, selected: Int) {
@@ -81,25 +110,25 @@ class FrontCoverSceneChooserView @JvmOverloads constructor(
                     lastSelectedData?.isSelected = true
                     project?.story?.scenes?.also { scenes ->
                         selected = dataList.indexOf(data)
-                        val s = scenes[selected]
-                        onSceneSelected(s)
+                        updateCover()
                     }
                 }
             }
             val data = Data(isSelected, bitmap, name, onSelectedListener)
             dataList.add(data)
-            if (isSelected) {
-                onSceneSelected(scene)
-            }
             i++
         }
         adapter.notifyDataSetChanged()
+        updateCover()
     }
 
-    private fun onSceneSelected(scene: Scene) {
+    private fun updateCover() {
+        val scenes = project?.story?.scenes ?: return
+        val scene = scenes[selected]
         val width = context.resources.getDimension(R.dimen.share_dialog_screenshot_width).toInt()
         val height = context.resources.getDimension(R.dimen.share_dialog_screenshot_height).toInt()
-        val bitmap = SceneDrawer.drawScene(context, scene, 0, width, height) ?: return
+        val bitmap = SceneDrawer.drawScene(context, scene, 0, width, height)?.crop(alignStyle)
+                ?: return
         iv_frontCoverSceneChooserDialog_cover.setImageBitmap(bitmap)
     }
 

+ 17 - 0
src/main/java/com/bomostory/sceneeditmodule/utils/Utils.kt

@@ -3,13 +3,30 @@ package com.bomostory.sceneeditmodule.utils
 import android.app.AlertDialog
 import android.content.Context
 import android.content.Intent
+import android.graphics.Bitmap
+import android.graphics.Canvas
 import android.support.v4.content.FileProvider
 import android.widget.Toast
+import com.bomostory.sceneeditmodule.cover.AlignStyle
 import com.example.tfat.myapplication.R
 import java.io.File
 
 object Utils {
 
+    fun Bitmap.crop(alignStyle: AlignStyle): Bitmap {
+        val src = this
+        val size = Math.min(src.width, src.height)
+        val dst = Bitmap.createBitmap(size, size, src.config)
+        val canvas = Canvas(dst)
+        val left = when (alignStyle) {
+            AlignStyle.Left -> 0
+            AlignStyle.Center -> -(src.width - dst.width)
+            AlignStyle.Right -> -(src.width - dst.width) / 2
+        }
+        canvas.drawBitmap(src, left.toFloat(), 0f, null)
+        return dst
+    }
+
     fun generateSceneName(index: Int): String {
         val book = ('A'.toInt() + ((index) / 12)).toChar()
         val num = (index) % 12 + 1

+ 0 - 2
src/main/res/layout/view_front_cover_scene_chooser.xml

@@ -86,9 +86,7 @@
         app:layout_constraintTop_toBottomOf="@id/tv_frontCoverSceneChooserDialog_coverTitle"
         tools:text="Author" />
 
-    <!-- Remove visibility="gone" when implemented it -->
     <Spinner
-        android:visibility="invisible"
         android:id="@+id/spinner_frontCoverSceneChooserDialog_align"
         android:foreground="@drawable/bg_bomo_spinner"
         app:layout_constraintTop_toTopOf="@id/iv_frontCoverSceneChooserDialog_cover"

+ 3 - 0
src/main/res/values/strings.xml

@@ -56,4 +56,7 @@
     <string name="front_cover_scene_choose_dialog_title">Choose a scene as the front cover</string>
     <string name="front_cover_scene_choose_dialog_btn_apply">Apply</string>
     <string name="front_cover_scene_choose_dialog_btn_discard">Discard</string>
+    <string name="front_cover_scene_choose_dialog_align_center">Align to center</string>
+    <string name="front_cover_scene_choose_dialog_align_left">Align to left</string>
+    <string name="front_cover_scene_choose_dialog_align_right">Align to right</string>
 </resources>