Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Add eraser mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Jul 10, 2023
1 parent 6f50f4a commit f7fef8b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bnyro.recorder.ui.views
package com.bnyro.recorder.canvas_overlay

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.awaitEachGesture
Expand All @@ -22,6 +22,7 @@ import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChange
import androidx.lifecycle.viewmodel.compose.viewModel

enum class MotionEvent {
Up, Down, Idle, Move
Expand All @@ -32,13 +33,12 @@ enum class DrawMode {
}

@Composable
fun MainCanvas() {
fun MainCanvas(canvasViewModel: CanvasViewModel = viewModel()) {
val paths = remember {
mutableStateListOf<PathProperties>()
}
val pathsUndone = remember { mutableStateListOf<PathProperties>() }
var motionEvent by remember { mutableStateOf(MotionEvent.Idle) }
var currentPath by remember { mutableStateOf(PathProperties()) }

var currentPosition by remember { mutableStateOf(Offset.Unspecified) }

Expand Down Expand Up @@ -68,35 +68,35 @@ fun MainCanvas() {
when (motionEvent) {
MotionEvent.Idle -> Unit
MotionEvent.Down -> {
paths.add(currentPath)
currentPath.path.moveTo(
paths.add(canvasViewModel.currentPath)
canvasViewModel.currentPath.path.moveTo(
currentPosition.x, currentPosition.y
)
}

MotionEvent.Move -> {
currentPath.path.lineTo(
canvasViewModel.currentPath.path.lineTo(
currentPosition.x, currentPosition.y
)
drawCircle(
center = currentPosition,
color = Color.Gray,
radius = currentPath.strokeWidth / 2,
radius = canvasViewModel.currentPath.strokeWidth / 2,
style = Stroke(
width = 1f
)
)
}

MotionEvent.Up -> {
currentPath.path.lineTo(
canvasViewModel.currentPath.path.lineTo(
currentPosition.x, currentPosition.y
)
currentPath = PathProperties(
canvasViewModel.currentPath = PathProperties(
path = Path(),
strokeWidth = currentPath.strokeWidth,
color = currentPath.color,
drawMode = currentPath.drawMode
strokeWidth = canvasViewModel.currentPath.strokeWidth,
color = canvasViewModel.currentPath.color,
drawMode = canvasViewModel.currentPath.drawMode
)
pathsUndone.clear()
currentPosition = Offset.Unspecified
Expand Down Expand Up @@ -138,7 +138,7 @@ class PathProperties(
color = Color.Transparent,
path = path,
style = Stroke(
width = strokeWidth,
width = 50f,
cap = StrokeCap.Round,
join = StrokeJoin.Round
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bnyro.recorder.ui.views
package com.bnyro.recorder.canvas_overlay

import android.content.Context
import android.content.Context.WINDOW_SERVICE
Expand All @@ -11,8 +11,11 @@ import androidx.annotation.RequiresApi
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.lifecycle.setViewTreeViewModelStoreOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.bnyro.recorder.ui.theme.RecordYouTheme
import com.bnyro.recorder.util.CustomLifecycleOwner
import com.bnyro.recorder.util.CustomViewModelStoreOwner


@RequiresApi(Build.VERSION_CODES.O)
Expand All @@ -27,16 +30,20 @@ class CanvasOverlay(context: Context) {

private var composeView = ComposeView(context).apply {
setContent {
OverlayView(
onDismissRequest = { this@CanvasOverlay.remove() })
RecordYouTheme() {
OverlayView(
onDismissRequest = { this@CanvasOverlay.remove() })
}
}
}

init {
val lifecycleOwner = CustomLifecycleOwner()
val viewModelStoreOwner = CustomViewModelStoreOwner()
lifecycleOwner.performRestore(null)
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
composeView.setViewTreeLifecycleOwner(lifecycleOwner)
composeView.setViewTreeViewModelStoreOwner(viewModelStoreOwner)
composeView.setViewTreeSavedStateRegistryOwner(lifecycleOwner)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bnyro.recorder.canvas_overlay

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel

class CanvasViewModel: ViewModel() {
var currentPath by mutableStateOf(PathProperties())
}
54 changes: 54 additions & 0 deletions app/src/main/java/com/bnyro/recorder/canvas_overlay/OverlayView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.bnyro.recorder.canvas_overlay

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Draw
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.lifecycle.viewmodel.compose.viewModel
import com.bnyro.recorder.R

@Composable
fun OverlayView(onDismissRequest: () -> Unit, canvasViewModel: CanvasViewModel = viewModel()) {
Box(Modifier.fillMaxSize()) {
MainCanvas()
var currentDrawMode by remember { mutableStateOf(canvasViewModel.currentPath.drawMode) }
Card(Modifier.align(Alignment.TopEnd)) {
Row {
IconButton(
onClick = {
currentDrawMode = DrawMode.Pen
canvasViewModel.currentPath.drawMode = currentDrawMode
}) {
Icon(imageVector = Icons.Default.Draw, contentDescription = "Draw Mode")
}
IconButton(
onClick = {
currentDrawMode = DrawMode.Eraser
canvasViewModel.currentPath.drawMode = currentDrawMode
}) {
Icon(
painter = painterResource(id = R.drawable.ic_eraser_black_24dp),
contentDescription = "Erase Mode"
)
}
IconButton(onClick = { onDismissRequest() }) {
Icon(Icons.Default.Close, "Close Overlay")
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.bnyro.recorder.services.AudioRecorderService
import com.bnyro.recorder.services.LosslessRecorderService
import com.bnyro.recorder.services.RecorderService
import com.bnyro.recorder.services.ScreenRecorderService
import com.bnyro.recorder.ui.views.CanvasOverlay
import com.bnyro.recorder.canvas_overlay.CanvasOverlay
import com.bnyro.recorder.util.PermissionHelper
import com.bnyro.recorder.util.Preferences

Expand Down
28 changes: 0 additions & 28 deletions app/src/main/java/com/bnyro/recorder/ui/views/OverlayView.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bnyro.recorder.util

import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner

class CustomViewModelStoreOwner:ViewModelStoreOwner {
override val viewModelStore: ViewModelStore = ViewModelStore()

}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_eraser_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15.14,3C14.63,3 14.12,3.2 13.73,3.59L2.59,14.73C1.81,15.5 1.81,16.77 2.59,17.56L5.03,20H12.69L21.41,11.27C22.2,10.5 22.2,9.23 21.41,8.44L16.56,3.59C16.17,3.2 15.65,3 15.14,3M17,18L15,20H22V18"/>
</vector>

0 comments on commit f7fef8b

Please sign in to comment.