|
@@ -0,0 +1,240 @@
|
|
|
+package com.bomostory.sceneeditmodule.screen.draw
|
|
|
+
|
|
|
+import android.arch.lifecycle.LiveData
|
|
|
+import android.arch.lifecycle.MutableLiveData
|
|
|
+import android.arch.lifecycle.ViewModel
|
|
|
+import android.content.res.Resources
|
|
|
+import android.graphics.Color
|
|
|
+import android.graphics.drawable.ColorDrawable
|
|
|
+import android.support.v4.content.res.ResourcesCompat
|
|
|
+import com.example.tfat.myapplication.R
|
|
|
+import com.kdanmobile.kdanbrushlib.brush.*
|
|
|
+import com.kdanmobile.kdanbrushlib.model.KdanBrush
|
|
|
+import com.kdanmobile.kdanbrushlib.widget.DrawView
|
|
|
+
|
|
|
+class DrawViewModel : ViewModel() {
|
|
|
+
|
|
|
+ val currentBrushLiveData: LiveData<KdanBrush>
|
|
|
+ get() = mCurrentBrushLiveData
|
|
|
+
|
|
|
+ val currentColorIndexLiveData: LiveData<Int>
|
|
|
+ get() = mCurrentColorIndexLiveData
|
|
|
+ val color1LiveData: LiveData<ColorDrawable>
|
|
|
+ get() = mColor1LiveData
|
|
|
+ val color2LiveData: LiveData<ColorDrawable>
|
|
|
+ get() = mColor2LiveData
|
|
|
+ val color3LiveData: LiveData<ColorDrawable>
|
|
|
+ get() = mColor3LiveData
|
|
|
+
|
|
|
+ private var mCurrentBrushLiveData = MutableLiveData<KdanBrush>()
|
|
|
+
|
|
|
+ private var mCurrentColorIndexLiveData = MutableLiveData<Int>()
|
|
|
+ private var mColor1LiveData = MutableLiveData<ColorDrawable>()
|
|
|
+ private var mColor2LiveData = MutableLiveData<ColorDrawable>()
|
|
|
+ private var mColor3LiveData = MutableLiveData<ColorDrawable>()
|
|
|
+
|
|
|
+ private lateinit var pencilBrush: PencilBrush
|
|
|
+ private lateinit var crayonBrush: CrayonBrush
|
|
|
+ private lateinit var penBrush: FountainPenBrush
|
|
|
+ private lateinit var chineseBrush: ChineseBrush
|
|
|
+ private lateinit var eraser: DefaultEraser
|
|
|
+
|
|
|
+
|
|
|
+ fun initBrush(drawView: DrawView) {
|
|
|
+ //TODO remove drawView from view model
|
|
|
+ pencilBrush = PencilBrush(drawView)
|
|
|
+ crayonBrush = CrayonBrush(drawView)
|
|
|
+ penBrush = FountainPenBrush(drawView)
|
|
|
+ chineseBrush = ChineseBrush(drawView)
|
|
|
+ eraser = DefaultEraser(drawView)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun initColor(resource: Resources) {
|
|
|
+ //TODO add other color
|
|
|
+ mColor1LiveData.value = ColorDrawable(ResourcesCompat.getColor(resource, R.color.defaultColor1, null))
|
|
|
+ mColor2LiveData.value = ColorDrawable(ResourcesCompat.getColor(resource, R.color.defaultColor2, null))
|
|
|
+ mColor3LiveData.value = ColorDrawable(ResourcesCompat.getColor(resource, R.color.defaultColor3, null))
|
|
|
+ mCurrentColorIndexLiveData.value = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ fun selectPencil() {
|
|
|
+ mCurrentBrushLiveData.value = pencilBrush
|
|
|
+ currentColorIndexLiveData.value?.let {
|
|
|
+ selectColor(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun selectCrayon() {
|
|
|
+ mCurrentBrushLiveData.value = crayonBrush
|
|
|
+ currentColorIndexLiveData.value?.let {
|
|
|
+ selectColor(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun selectPen() {
|
|
|
+ mCurrentBrushLiveData.value = penBrush
|
|
|
+ currentColorIndexLiveData.value?.let {
|
|
|
+ selectColor(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun selectChineseBrush() {
|
|
|
+ mCurrentBrushLiveData.value = chineseBrush
|
|
|
+ currentColorIndexLiveData.value?.let {
|
|
|
+ selectColor(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun selectEraser() {
|
|
|
+ mCurrentBrushLiveData.value = eraser
|
|
|
+ }
|
|
|
+
|
|
|
+ fun selectColor(index: Int) {
|
|
|
+ mCurrentColorIndexLiveData.value = index
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setColorRValue(rValue: Int) {
|
|
|
+ var colorDrawable: ColorDrawable? = null
|
|
|
+
|
|
|
+ when (mCurrentColorIndexLiveData.value) {
|
|
|
+ 0 -> {
|
|
|
+ mColor1LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), rValue, Color.green(it.color), Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor1LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 1 -> {
|
|
|
+ mColor2LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), rValue, Color.green(it.color), Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor2LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 2 -> {
|
|
|
+ mColor3LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), rValue, Color.green(it.color), Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor3LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ setBrushColor(currentColorIndexLiveData.value)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setColorGValue(gValue: Int) {
|
|
|
+ var colorDrawable: ColorDrawable? = null
|
|
|
+
|
|
|
+ when (mCurrentColorIndexLiveData.value) {
|
|
|
+ 0 -> {
|
|
|
+ mColor1LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), Color.red(it.color), gValue, Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor1LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 1 -> {
|
|
|
+ mColor2LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), Color.red(it.color), gValue, Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor2LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 2 -> {
|
|
|
+ mColor3LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), Color.red(it.color), gValue, Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor3LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ setBrushColor(currentColorIndexLiveData.value)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setColorBValue(bValue: Int) {
|
|
|
+ var colorDrawable: ColorDrawable? = null
|
|
|
+
|
|
|
+ when (mCurrentColorIndexLiveData.value) {
|
|
|
+ 0 -> {
|
|
|
+ mColor1LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), Color.red(it.color), Color.green(it.color), bValue))
|
|
|
+ }
|
|
|
+ mColor1LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 1 -> {
|
|
|
+ mColor2LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), Color.red(it.color), Color.green(it.color), bValue))
|
|
|
+ }
|
|
|
+ mColor2LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 2 -> {
|
|
|
+ mColor3LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(Color.alpha(it.color), Color.red(it.color), Color.green(it.color), bValue))
|
|
|
+ }
|
|
|
+ mColor3LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ setBrushColor(currentColorIndexLiveData.value)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setColorAValue(aValue: Int) {
|
|
|
+ var colorDrawable: ColorDrawable? = null
|
|
|
+
|
|
|
+ when (mCurrentColorIndexLiveData.value) {
|
|
|
+ 0 -> {
|
|
|
+ mColor1LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(aValue, Color.red(it.color), Color.green(it.color), Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor1LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 1 -> {
|
|
|
+ mColor2LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(aValue, Color.red(it.color), Color.green(it.color), Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor2LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ 2 -> {
|
|
|
+ mColor3LiveData.value?.let {
|
|
|
+ colorDrawable = ColorDrawable(Color.argb(aValue, Color.red(it.color), Color.green(it.color), Color.blue(it.color)))
|
|
|
+ }
|
|
|
+ mColor3LiveData.value = colorDrawable
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ setBrushColor(currentColorIndexLiveData.value)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setBrushSize(brushSize: Int) {
|
|
|
+ val brush = mCurrentBrushLiveData.value
|
|
|
+ brush?.radius?.value = brushSize
|
|
|
+ mCurrentBrushLiveData.value = brush
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setBrushColor(index: Int?) {
|
|
|
+ val brush = mCurrentBrushLiveData.value
|
|
|
+ var color: Int? = null
|
|
|
+
|
|
|
+ when (index) {
|
|
|
+ 0 -> {
|
|
|
+ mColor1LiveData.value?.let {
|
|
|
+ color = it.color
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 1 -> {
|
|
|
+ mColor2LiveData.value?.let {
|
|
|
+ color = it.color
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 2 -> {
|
|
|
+ mColor3LiveData.value?.let {
|
|
|
+ color = it.color
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ color?.let {
|
|
|
+ if (brush?.color != it) {
|
|
|
+ brush?.color = it
|
|
|
+ mCurrentBrushLiveData.value = brush
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|