Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[둘리] 뷰 챌린지 미션 2단계 제출합니다. #38

Merged
merged 28 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
dc16a7d
feat: button bindingAdapter로 isSelected 생성
hyemdooly Oct 9, 2023
dee7ae1
feat: 선택된 버튼 색깔 생성
hyemdooly Oct 9, 2023
74f09cb
feat: 브러쉬 요소 추가
hyemdooly Oct 9, 2023
4e324fd
feat: 브러쉬 선택 기능 UI 추가
hyemdooly Oct 9, 2023
2e8ae10
feat: 함수 일부 네이밍 변경 및 Circle, Rectangle 그리기 기능 추가
hyemdooly Oct 9, 2023
4c813d6
refactor: 함수 분리 및 중복 코드 삭제
hyemdooly Oct 9, 2023
c56475f
feat: 지우개 기능 구현
hyemdooly Oct 9, 2023
dc53032
refactor: Drawing 클래스에서 Path, Paint 복사하도록 수정
hyemdooly Oct 9, 2023
7da23af
feat: 전체 삭제, undo, redo 구현
hyemdooly Oct 9, 2023
e22dfd6
refactor: 함수 순서 수정
hyemdooly Oct 9, 2023
87daa31
refactor: SettingState sealed class -> enum class로 변경, 상수 위치 이동
hyemdooly Oct 9, 2023
0b210df
refactor: width로 굵기 네이밍 통일
hyemdooly Oct 9, 2023
943e72c
refactor: Brush -> Tool로 네이밍 변경
hyemdooly Oct 10, 2023
1cf3673
refactor: enum class 상수 네이밍 변경
hyemdooly Oct 10, 2023
316933e
refactor: Drawing 클래스 세분화
hyemdooly Oct 10, 2023
2d10fa3
refactor: tool이 drawing 객체를 리턴하도록 수정
hyemdooly Oct 10, 2023
f67b8ac
refactor: CanvasView가 하던 일을 Drawing 클래스로 이동
hyemdooly Oct 11, 2023
ee553e0
refactor: undo, redo 코드 줄 수 줄이기
hyemdooly Oct 11, 2023
cd6ff78
design: 도구에 따른 설정 화면 변경
hyemdooly Oct 11, 2023
dd8e201
refactor: 도구에 따라 버튼 동적 생성
hyemdooly Oct 11, 2023
26e18fe
refactor: brushes -> tools로 네이밍 수정
hyemdooly Oct 11, 2023
f412171
refactor: 함수명 수정
hyemdooly Oct 11, 2023
acb317f
refactor: tool 바꿀 때 설정창 닫도록 수정
hyemdooly Oct 11, 2023
f55275e
fix: Rectangle 버그 수정
hyemdooly Oct 11, 2023
b10fda7
refactor: 도구 모음 일치
hyemdooly Oct 11, 2023
d4a46c4
refactor: tool -> drawingTool로 네이밍 변경
hyemdooly Oct 11, 2023
1d90521
refactor: Drawings 일급컬렉션 생성
hyemdooly Oct 11, 2023
1264668
refactor: 팩토리 함수 삭제
hyemdooly Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: 함수 일부 네이밍 변경 및 Circle, Rectangle 그리기 기능 추가
  • Loading branch information
hyemdooly committed Oct 9, 2023
commit 2e8ae10592c65e53127a1192a6a09e5755aebfa8
6 changes: 5 additions & 1 deletion app/src/main/java/woowacourse/paint/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class MainActivity : AppCompatActivity() {
adapter.submitList(colors)
}
}
viewModel.selectedBrush.observe(this) { brush ->
canvasView.setupBrush(brush)
}
}

private fun setupCanvas() {
Expand Down Expand Up @@ -69,6 +72,7 @@ class MainActivity : AppCompatActivity() {
private fun setupWidthSlider() {
binding.rsThicknessChanger.valueFrom = minWidth
binding.rsThicknessChanger.valueTo = maxWidth
binding.rsThicknessChanger.setValues(viewModel.width.value)
binding.rsThicknessChanger.addOnChangeListener(
RangeSlider.OnChangeListener { _, value, _ ->
viewModel.pickWidth(value)
Expand All @@ -77,7 +81,7 @@ class MainActivity : AppCompatActivity() {
}

companion object {
private const val minWidth = 0f
private const val minWidth = 1f
private const val maxWidth = 100f
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/woowacourse/paint/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MainViewModel : ViewModel() {
}

companion object {
const val DEFAULT_WIDTH = 0F
const val DEFAULT_WIDTH = 1F
val DEFAULT_SELECTED_COLOR = PaletteColor.RED
}
}
75 changes: 66 additions & 9 deletions app/src/main/java/woowacourse/paint/canvas/CanvasView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,45 @@ class CanvasView(context: Context, attr: AttributeSet) : View(
context,
attr,
) {
private var path = Path()
private var brush = Brush.PEN
private var paint = Paint()
private var path = Path()

private var startPoint: Point = Point(0f, 0f)
private val lines = mutableListOf<Line>()
private val drawings = mutableListOf<Drawing>()

fun initPaint(width: Float, color: PaletteColor) {
paint = getPaint(width, color.colorCode)
}

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
lines.forEach { line ->
drawings.forEach { line ->
canvas.drawPath(line.path, line.paint)
}
canvas.drawPath(path, paint)
}

override fun onTouchEvent(event: MotionEvent): Boolean {
when (brush) {
Brush.PEN -> drawByPen(event)
Brush.RECTANGLE -> drawRect(event)
Brush.CIRCLE -> drawCircle(event)
Brush.ERASER -> TODO()
}
return true
}

private fun drawByPen(event: MotionEvent) {
val x = event.x
val y = event.y

when (event.action) {
MotionEvent.ACTION_DOWN -> drawDot(x, y)
MotionEvent.ACTION_MOVE -> drawLine(x, y)
MotionEvent.ACTION_UP -> addDrawing()
else -> super.onTouchEvent(event)
}
return true
}

private fun drawDot(x: Float, y: Float) {
Expand All @@ -56,26 +68,71 @@ class CanvasView(context: Context, attr: AttributeSet) : View(
invalidate()
}

private fun drawRect(event: MotionEvent) {
val x = event.x
val y = event.y

when (event.action) {
MotionEvent.ACTION_DOWN -> {
startPoint = Point(x, y)
}

MotionEvent.ACTION_MOVE -> {
path.reset()
path.addRect(startPoint.x, startPoint.y, x, y, Path.Direction.CW)
invalidate()
}

MotionEvent.ACTION_UP -> addDrawing()
else -> super.onTouchEvent(event)
}
}

private fun drawCircle(event: MotionEvent) {
val x = event.x
val y = event.y

when (event.action) {
MotionEvent.ACTION_DOWN -> {
startPoint = Point(x, y)
}

MotionEvent.ACTION_MOVE -> {
path.reset()
path.addOval(startPoint.x, startPoint.y, x, y, Path.Direction.CW)
invalidate()
}

MotionEvent.ACTION_UP -> addDrawing()
else -> super.onTouchEvent(event)
}
}

private fun erasePath() {
}

fun setupBrush(selectedBrush: Brush) {
brush = selectedBrush
}

fun setupWidth(width: Float) {
addLine()
paint = getPaint(width, paint.color)
}

fun setupColor(color: PaletteColor) {
addLine()
paint = getPaint(paint.strokeWidth, color.colorCode)
}

private fun addLine() {
private fun addDrawing() {
if (path.isEmpty) return
lines.add(Line(path, paint))
drawings.add(Drawing(path, paint))
path = Path()
}

private fun getPaint(width: Float, @ColorInt selectedColor: Int): Paint {
return Paint().apply {
isAntiAlias = true
style = Paint.Style.STROKE
style = Paint.Style.FILL_AND_STROKE
strokeJoin = Paint.Join.ROUND
color = selectedColor
strokeWidth = width
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package woowacourse.paint.canvas
import android.graphics.Paint
import android.graphics.Path

class Line(val path: Path, val paint: Paint)
class Drawing(val path: Path, val paint: Paint)