Skip to content

Commit

Permalink
PAINTROID-434 An option to enable/disable and adjust Zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenkomotive committed Mar 13, 2023
1 parent 8bb6bb2 commit cfae324
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
R.id.pocketpaint_options_about -> presenterMain.showAboutClicked()
R.id.pocketpaint_share_image_button -> presenterMain.shareImageClicked()
R.id.pocketpaint_options_feedback -> presenterMain.sendFeedback()
R.id.pocketpaint_zoom_window_settings ->
presenterMain.showZoomWindowSettingsClicked(
UserPreferences(getPreferences(MODE_PRIVATE))
)
R.id.pocketpaint_advanced_settings -> presenterMain.showAdvancedSettingsClicked()
android.R.id.home -> presenterMain.backToPocketCodeClicked()
else -> return false
Expand Down Expand Up @@ -454,7 +458,8 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
this,
layerModel,
workspace,
toolReference
toolReference,
UserPreferences(getPreferences(MODE_PRIVATE))
)
model = MainActivityModel()
defaultToolController = DefaultToolController(
Expand Down Expand Up @@ -536,6 +541,7 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
toolOptionsViewController,
drawerLayoutViewHolder,
zoomWindowController,
UserPreferences(getPreferences(MODE_PRIVATE))
)
layerPresenter.setDrawingSurface(drawingSurface)
appFragment.perspective = perspective
Expand Down
27 changes: 27 additions & 0 deletions Paintroid/src/main/java/org/catrobat/paintroid/UserPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ package org.catrobat.paintroid
import android.content.SharedPreferences
import org.catrobat.paintroid.common.IMAGE_NUMBER_SHARED_PREFERENCES_TAG
import org.catrobat.paintroid.common.SHOW_LIKE_US_DIALOG_SHARED_PREFERENCES_TAG
import org.catrobat.paintroid.common.ZOOM_WINDOW_ENABLED_SHARED_PREFERENCES_TAG
import org.catrobat.paintroid.common.ZOOM_WINDOW_ZOOM_PERCENTAGE_SHARED_PREFERENCES_TAG

open class UserPreferences(var preferences: SharedPreferences) {

open val preferenceLikeUsDialogValue: Boolean
get() = preferences.getBoolean(SHOW_LIKE_US_DIALOG_SHARED_PREFERENCES_TAG, false)
open var preferenceImageNumber: Int
Expand All @@ -33,11 +36,35 @@ open class UserPreferences(var preferences: SharedPreferences) {
.putInt(IMAGE_NUMBER_SHARED_PREFERENCES_TAG, value)
.apply()
}
open var preferenceZoomWindowEnabled: Boolean
get() = preferences.getBoolean(ZOOM_WINDOW_ENABLED_SHARED_PREFERENCES_TAG, true)
set(value) {
preferences
.edit()
.putBoolean(ZOOM_WINDOW_ENABLED_SHARED_PREFERENCES_TAG, value)
.apply()
}

open var preferenceZoomWindowZoomPercentage: Int
get() = preferences.getInt(
ZOOM_WINDOW_ZOOM_PERCENTAGE_SHARED_PREFERENCES_TAG,
initialZoomPercent
)
set(value) {
preferences
.edit()
.putInt(ZOOM_WINDOW_ZOOM_PERCENTAGE_SHARED_PREFERENCES_TAG, value)
.apply()
}

open fun setPreferenceLikeUsDialogValue() {
preferences
.edit()
.putBoolean(SHOW_LIKE_US_DIALOG_SHARED_PREFERENCES_TAG, true)
.apply()
}

companion object {
const val initialZoomPercent: Int = 100
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const val ABOUT_DIALOG_FRAGMENT_TAG = "aboutdialogfragment"
const val LIKE_US_DIALOG_FRAGMENT_TAG = "likeusdialogfragment"
const val RATE_US_DIALOG_FRAGMENT_TAG = "rateusdialogfragment"
const val FEEDBACK_DIALOG_FRAGMENT_TAG = "feedbackdialogfragment"
const val ZOOM_WINDOW_SETTINGS_DIALOG_FRAGMENT_TAG = "zoomwindowsettingsdialogfragment"
const val ADVANCED_SETTINGS_DIALOG_FRAGMENT_TAG = "advancedsettingsdialogfragment"
const val SAVE_DIALOG_FRAGMENT_TAG = "savedialogerror"
const val LOAD_DIALOG_FRAGMENT_TAG = "loadbitmapdialogerror"
Expand All @@ -43,6 +44,8 @@ const val CATROBAT_INFORMATION_DIALOG_TAG = "catrobatinformationdialogfragment"
const val CATROID_MEDIA_GALLERY_FRAGMENT_TAG = "catroidmediagalleryfragment"
const val PERMISSION_DIALOG_FRAGMENT_TAG = "permissiondialogfragment"
const val SHOW_LIKE_US_DIALOG_SHARED_PREFERENCES_TAG = "showlikeusdialog"
const val ZOOM_WINDOW_ENABLED_SHARED_PREFERENCES_TAG = "zoomwindowenabled"
const val ZOOM_WINDOW_ZOOM_PERCENTAGE_SHARED_PREFERENCES_TAG = "zoomwindowzoompercentage"
const val IMAGE_NUMBER_SHARED_PREFERENCES_TAG = "imagenumbertag"
const val SCALE_IMAGE_FRAGMENT_TAG = "showscaleimagedialog"
const val INDETERMINATE_PROGRESS_DIALOG_TAG = "indeterminateprogressdialogfragment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.util.DisplayMetrics
import android.view.Menu
import androidx.annotation.ColorInt
import androidx.annotation.StringRes
import org.catrobat.paintroid.UserPreferences
import org.catrobat.paintroid.command.serialization.CommandSerializer
import org.catrobat.paintroid.colorpicker.ColorHistory
import org.catrobat.paintroid.common.MainActivityConstants.ActivityRequestCode
Expand Down Expand Up @@ -58,6 +59,8 @@ interface MainActivityContracts {

fun showFeedbackDialog()

fun showZoomWindowSettingsDialog(sharedPreferences: UserPreferences)

fun showAdvancedSettingsDialog()

fun showOverwriteDialog(permissionCode: Int, isExport: Boolean)
Expand Down Expand Up @@ -215,6 +218,8 @@ interface MainActivityContracts {

fun showAboutClicked()

fun showZoomWindowSettingsClicked(sharedPreferences: UserPreferences)

fun showAdvancedSettingsClicked()

fun showRateUsDialog()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.catrobat.paintroid.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
import com.google.android.material.slider.Slider
import org.catrobat.paintroid.R
import org.catrobat.paintroid.UserPreferences

class ZoomWindowSettingsDialog(
private val sharedPreferences: UserPreferences
) : MainActivityDialogFragment() {

private val initialEnabledValue = sharedPreferences.preferenceZoomWindowEnabled
private val initialPercentageValue = sharedPreferences.preferenceZoomWindowZoomPercentage

private var enabled = sharedPreferences.preferenceZoomWindowEnabled
private var percentage = sharedPreferences.preferenceZoomWindowZoomPercentage

@SuppressLint("SetTextI18n")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val enabledSwitch = view.findViewById<SwitchCompat>(R.id.pocketpaint_zoom_window_enabled)
val slider = view.findViewById<Slider>(R.id.pocketpaint_zoom_window_slider)
val sliderTextView = view.findViewById<TextView>(R.id.pocketpaint_zoom_window_slider_progress)

enabledSwitch.isChecked = initialEnabledValue
sliderTextView.text = "$initialPercentageValue%"
slider.value = initialPercentageValue.toFloat()

enabledSwitch.setOnCheckedChangeListener { _, isChecked ->
enabled = isChecked
}

slider.addOnChangeListener { _, value, _ ->
var percentageValue = value.toInt().toString()
sliderTextView.text = "$percentageValue%"

percentage = value.toInt()
}

slider.setLabelFormatter { value: Float ->
value.toInt().toString() + '%'
}
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val inflater = requireActivity().layoutInflater
val layout = inflater.inflate(R.layout.dialog_pocketpaint_zoomwindow_settings, null)
onViewCreated(layout, savedInstanceState)

return AlertDialog.Builder(requireContext(), R.style.PocketPaintAlertDialog)
.setTitle(R.string.menu_zoom_settings)
.setView(layout)
.setPositiveButton(R.string.pocketpaint_ok) { _, _ ->
sharedPreferences.preferenceZoomWindowEnabled = enabled
sharedPreferences.preferenceZoomWindowZoomPercentage = percentage
dismiss()
}
.setNegativeButton(R.string.cancel_button_text) { _, _ ->
sharedPreferences.preferenceZoomWindowEnabled = initialEnabledValue
sharedPreferences.preferenceZoomWindowZoomPercentage = initialPercentageValue
dismiss()
}
.create()
}

override fun onCancel(dialog: DialogInterface) {
sharedPreferences.preferenceZoomWindowEnabled = initialEnabledValue
sharedPreferences.preferenceZoomWindowZoomPercentage = initialPercentageValue
super.onCancel(dialog)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.os.Handler
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import org.catrobat.paintroid.UserPreferences
import org.catrobat.paintroid.tools.Tool
import org.catrobat.paintroid.tools.Tool.StateChange
import org.catrobat.paintroid.tools.ToolType
Expand Down Expand Up @@ -58,6 +59,8 @@ open class DrawingSurfaceListener(
private var autoScroll = true
private var timerStartDraw = 0.toLong()
private lateinit var zoomController: ZoomWindowController
private var callZoomWindow: Boolean = true
private lateinit var sharedPreferences: UserPreferences

private var recentTouchEventsData: MutableList<TouchEventData> = mutableListOf()

Expand Down Expand Up @@ -106,9 +109,11 @@ open class DrawingSurfaceListener(
}

fun setZoomController(
zoomWindowController: ZoomWindowController
zoomWindowController: ZoomWindowController,
sharedPreferences: UserPreferences
) {
zoomController = zoomWindowController
this.sharedPreferences = sharedPreferences
}

private fun handleActionMove(currentTool: Tool?, view: View, event: MotionEvent) {
Expand Down Expand Up @@ -139,11 +144,7 @@ open class DrawingSurfaceListener(
}
currentTool.handleMove(canvasTouchPoint)
}
if (!callback.getCurrentTool()?.toolType?.name.equals(ToolType.CURSOR.name)) {
zoomController.onMove(canvasTouchPoint, PointF(event.x, event.y))
} else {
zoomController.onMove(currentTool.toolPositionCoordinates(canvasTouchPoint), PointF(event.x, event.y))
}
handleZoomWindowOnMove(currentTool, event)
} else {
disableAutoScroll()
if (touchMode == TouchMode.DRAW) {
Expand All @@ -167,6 +168,16 @@ open class DrawingSurfaceListener(
}
}

private fun handleZoomWindowOnMove(currentTool: Tool, event: MotionEvent) {
if (sharedPreferences.preferenceZoomWindowEnabled) {
if (!callback.getCurrentTool()?.toolType?.name.equals(ToolType.CURSOR.name)) {
zoomController.onMove(canvasTouchPoint, PointF(event.x, event.y))
} else {
zoomController.onMove(currentTool.toolPositionCoordinates(canvasTouchPoint), PointF(event.x, event.y))
}
}
}

private fun saveToolActionBeforeZoom(point: PointF) {
val currentTool = callback.getCurrentTool()
if (currentTool?.toolType?.name.equals(ToolType.CURSOR.name) ||
Expand Down Expand Up @@ -196,12 +207,7 @@ open class DrawingSurfaceListener(
setEvenPointAndViewDimensionsForAutoScrollTask(view)
autoScrollTask.start()
}
if (!currentTool?.toolType?.name.equals(ToolType.CURSOR.name)) {
zoomController.show(canvasTouchPoint, PointF(event.x, event.y))
} else {
currentTool?.toolPositionCoordinates(canvasTouchPoint)
?.let { zoomController.show(it, PointF(event.x, event.y)) }
}
handleZoomWindowOnTouch(currentTool, event)
}
MotionEvent.ACTION_MOVE -> handleActionMove(currentTool, view, event)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
Expand Down Expand Up @@ -237,14 +243,28 @@ open class DrawingSurfaceListener(
eventX = 0f
eventY = 0f
touchMode = TouchMode.DRAW
zoomController.dismiss()
currentTool?.handToolMode()
if (callZoomWindow) zoomController.dismiss()
callback.getCurrentTool()?.handToolMode()
}
}
drawingSurface.refreshDrawingSurface()
return true
}

private fun handleZoomWindowOnTouch(currentTool: Tool?, event: MotionEvent) {
callZoomWindow = if (sharedPreferences.preferenceZoomWindowEnabled) {
if (!currentTool?.toolType?.name.equals(ToolType.CURSOR.name)) {
zoomController.show(canvasTouchPoint, PointF(event.x, event.y))
} else {
currentTool?.toolPositionCoordinates(canvasTouchPoint)
?.let { zoomController.show(it, PointF(event.x, event.y)) }
}
true
} else {
false
}
}

private fun removeObsoleteTouchEventsData(timeStamp: Long) {
val obsoleteTouchEventsData: MutableList<TouchEventData> = ArrayList()
for (touchEventData in recentTouchEventsData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ open class MainActivityPresenter(
navigator.showAboutDialog()
}

override fun showZoomWindowSettingsClicked(sharedPreferences: UserPreferences) {
navigator.showZoomWindowSettingsDialog(sharedPreferences)
}

override fun showAdvancedSettingsClicked() {
navigator.showAdvancedSettingsDialog()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import androidx.core.content.ContextCompat
import androidx.test.espresso.idling.CountingIdlingResource
import androidx.fragment.app.FragmentManager
import org.catrobat.paintroid.R
import org.catrobat.paintroid.UserPreferences
import org.catrobat.paintroid.colorpicker.ColorPickerDialog
import org.catrobat.paintroid.common.COLOR_PICKER_DIALOG_TAG
import org.catrobat.paintroid.contract.LayerContracts
Expand Down Expand Up @@ -77,6 +78,7 @@ open class DrawingSurface : SurfaceView, SurfaceHolder.Callback {
private lateinit var fragmentManager: FragmentManager
private lateinit var idlingResource: CountingIdlingResource
private lateinit var zoomController: ZoomWindowController
private lateinit var sharedPreferences: UserPreferences

constructor(context: Context?, attrSet: AttributeSet?) : super(context, attrSet)

Expand Down Expand Up @@ -133,7 +135,8 @@ open class DrawingSurface : SurfaceView, SurfaceHolder.Callback {
fragmentManager: FragmentManager,
toolOptionsViewController: ToolOptionsViewController,
drawerLayoutViewHolder: DrawerLayoutViewHolder,
zoomController: ZoomWindowController
zoomController: ZoomWindowController,
sharedPreferences: UserPreferences
) {
this.layerModel = layerModel
this.perspective = perspective
Expand All @@ -143,7 +146,8 @@ open class DrawingSurface : SurfaceView, SurfaceHolder.Callback {
this.fragmentManager = fragmentManager
this.drawerLayoutViewHolder = drawerLayoutViewHolder
this.zoomController = zoomController
drawingSurfaceListener.setZoomController(zoomWindowController = zoomController)
drawingSurfaceListener.setZoomController(zoomController, sharedPreferences)
this.sharedPreferences = sharedPreferences
}

@Synchronized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.fragment.app.Fragment
import org.catrobat.paintroid.FileIO
import org.catrobat.paintroid.MainActivity
import org.catrobat.paintroid.R
import org.catrobat.paintroid.UserPreferences
import org.catrobat.paintroid.WelcomeActivity
import org.catrobat.paintroid.colorpicker.ColorPickerDialog
import org.catrobat.paintroid.colorpicker.OnColorPickedListener
Expand Down Expand Up @@ -64,6 +65,7 @@ import org.catrobat.paintroid.common.SAVE_DIALOG_FRAGMENT_TAG
import org.catrobat.paintroid.common.SAVE_INFORMATION_DIALOG_TAG
import org.catrobat.paintroid.common.SAVE_QUESTION_FRAGMENT_TAG
import org.catrobat.paintroid.common.SCALE_IMAGE_FRAGMENT_TAG
import org.catrobat.paintroid.common.ZOOM_WINDOW_SETTINGS_DIALOG_FRAGMENT_TAG
import org.catrobat.paintroid.contract.MainActivityContracts
import org.catrobat.paintroid.dialog.AboutDialog
import org.catrobat.paintroid.dialog.AdvancedSettingsDialog
Expand All @@ -86,6 +88,7 @@ import org.catrobat.paintroid.dialog.SaveBeforeLoadImageDialog
import org.catrobat.paintroid.dialog.SaveBeforeNewImageDialog
import org.catrobat.paintroid.dialog.SaveInformationDialog
import org.catrobat.paintroid.dialog.ScaleImageOnLoadDialog
import org.catrobat.paintroid.dialog.ZoomWindowSettingsDialog
import org.catrobat.paintroid.tools.ToolReference
import org.catrobat.paintroid.tools.ToolType
import org.catrobat.paintroid.ui.fragments.CatroidMediaGalleryFragment
Expand Down Expand Up @@ -299,6 +302,14 @@ class MainActivityNavigator(
)
}

override fun showZoomWindowSettingsDialog(sharedPreferences: UserPreferences) {
val zoomWindowSettingsDialog = ZoomWindowSettingsDialog(sharedPreferences)
zoomWindowSettingsDialog.show(
mainActivity.supportFragmentManager,
ZOOM_WINDOW_SETTINGS_DIALOG_FRAGMENT_TAG
)
}

override fun showAdvancedSettingsDialog() {
val advancedSettingsDialog = AdvancedSettingsDialog()
advancedSettingsDialog.show(
Expand Down
Loading

0 comments on commit cfae324

Please sign in to comment.