-
Notifications
You must be signed in to change notification settings - Fork 46
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단계 제출합니다. #32
Changes from all commits
150aaa4
a268808
5f84342
bb05430
2abb2b3
8253c03
cde6d59
75af299
b911f82
10ec0d1
c0abff7
d550c72
83f99c2
efeed27
9b47dc0
349e7a3
e0745ed
5e375a4
05c9212
2dc35ca
72ac21c
0a527af
8a34250
0141913
0a2ae50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,91 @@ | ||
package woowacourse.paint | ||
|
||
import android.os.Bundle | ||
import android.view.Menu | ||
import android.view.MenuItem | ||
import android.widget.LinearLayout | ||
import androidx.appcompat.app.AppCompatActivity | ||
import woowacourse.paint.view.CanvasView | ||
import woowacourse.paint.view.PaletteColor | ||
import woowacourse.paint.view.PaletteView | ||
import androidx.appcompat.widget.AppCompatButton | ||
import woowacourse.paint.view.canvas.CanvasView | ||
import woowacourse.paint.view.palette.color.PaletteColor | ||
import woowacourse.paint.view.palette.PaletteMode | ||
import woowacourse.paint.view.palette.shape.PaletteShape | ||
import woowacourse.paint.view.palette.PaletteView | ||
|
||
class MainActivity : AppCompatActivity() { | ||
private val canvasView: CanvasView by lazy { findViewById(R.id.canvas_view) } | ||
private val paletteView: PaletteView by lazy { findViewById(R.id.palette_view) } | ||
private val paletteModeView: LinearLayout by lazy { findViewById(R.id.palette_mode_view) } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
setupCanvasView() | ||
setupPaletteView() | ||
setupPaletteModeView() | ||
} | ||
|
||
private fun setupCanvasView() { | ||
canvasView.setPaintThickness(paletteView.selectedPaintThickness) | ||
canvasView.setPaintColor(paletteView.selectedPaintColor) | ||
canvasView.setPaletteColor(paletteView.selectedPaletteColor) | ||
} | ||
|
||
private fun setupPaletteView() { | ||
paletteView.setOnPropertyChangeListener(object : PaletteView.OnPaintPropertyChangeListener { | ||
override fun onColorSelected(paintColor: PaletteColor) { | ||
canvasView.setPaintColor(paintColor) | ||
override fun onColorSelected(paletteColor: PaletteColor) { | ||
canvasView.setPaletteColor(paletteColor) | ||
} | ||
|
||
override fun onShapeSelected(paletteShape: PaletteShape) { | ||
canvasView.setPaletteShape(paletteShape) | ||
} | ||
|
||
override fun onStrokeThicknessChanged(paintThickness: Float) { | ||
canvasView.setPaintThickness(paintThickness) | ||
} | ||
}) | ||
paletteView.changePaletteMode(PaletteMode.BRUSH) | ||
} | ||
|
||
private fun setupPaletteModeView() { | ||
PaletteMode.values().forEach { paletteMode -> | ||
val modeButton = createModeButton(paletteMode) { newPaletteMode -> | ||
paletteView.changePaletteMode(newPaletteMode) | ||
canvasView.changePaletteMode(newPaletteMode) | ||
} | ||
|
||
paletteModeView.addView(modeButton) | ||
modeButton.layoutParams = (modeButton.layoutParams as LinearLayout.LayoutParams).apply { | ||
weight = 1F | ||
marginStart = 10 | ||
marginEnd = 10 | ||
} | ||
} | ||
} | ||
|
||
private fun createModeButton( | ||
mode: PaletteMode, | ||
onClick: (PaletteMode) -> Unit, | ||
): AppCompatButton = AppCompatButton(this).apply { | ||
text = mode.modeName | ||
textSize = 20F | ||
setTextColor(getColor(R.color.white)) | ||
setBackgroundColor(getColor(R.color.purple_500)) | ||
setOnClickListener { onClick(mode) } | ||
} | ||
|
||
override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||
menuInflater.inflate(R.menu.menu_main, menu) | ||
return true | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
when (item.itemId) { | ||
R.id.undo -> canvasView.undo() | ||
R.id.redo -> canvasView.redo() | ||
R.id.reset -> canvasView.reset() | ||
} | ||
return true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package woowacourse.paint.common | ||
|
||
import android.graphics.Paint | ||
import android.graphics.PorterDuff | ||
import android.graphics.PorterDuffXfermode | ||
import woowacourse.paint.view.palette.color.PaletteColor | ||
|
||
fun Paint.softPainter( | ||
paletteColor: PaletteColor = PaletteColor.RED, | ||
thickness: Float = 0.1F, | ||
paintStyle: Paint.Style = Paint.Style.STROKE, | ||
porterDuffMode: PorterDuff.Mode = PorterDuff.Mode.SRC_OVER, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존에 그려져 있는 리소스 위에 작성하겠다는 의미입니다 : ) |
||
): Paint = apply { | ||
isAntiAlias = true | ||
strokeJoin = Paint.Join.ROUND | ||
strokeCap = Paint.Cap.ROUND | ||
color = paletteColor.color | ||
strokeWidth = thickness | ||
style = paintStyle | ||
xfermode = PorterDuffXfermode(porterDuffMode) | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package woowacourse.paint.view.canvas | ||
|
||
import android.content.Context | ||
import android.graphics.Canvas | ||
import android.util.AttributeSet | ||
import android.view.MotionEvent | ||
import android.view.View | ||
import woowacourse.paint.view.palette.PaletteMode | ||
import woowacourse.paint.view.palette.color.PaletteColor | ||
import woowacourse.paint.view.palette.shape.PaletteShape | ||
|
||
class CanvasView : View { | ||
constructor(context: Context) : super(context) | ||
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) | ||
|
||
private val painterHistory = PainterHistory() | ||
|
||
init { | ||
setLayerType(LAYER_TYPE_HARDWARE, null) | ||
} | ||
|
||
override fun onDraw(canvas: Canvas) { | ||
super.onDraw(canvas) | ||
painterHistory.draw(canvas) | ||
} | ||
|
||
override fun onTouchEvent(event: MotionEvent): Boolean { | ||
val pointX = event.x | ||
val pointY = event.y | ||
|
||
when (event.action) { | ||
MotionEvent.ACTION_DOWN -> painterHistory.onActionDown(pointX, pointY) | ||
MotionEvent.ACTION_MOVE -> painterHistory.onActionMove(pointX, pointY) | ||
MotionEvent.ACTION_UP -> painterHistory.onActionUp() | ||
} | ||
invalidate() | ||
return true | ||
} | ||
|
||
fun setPaletteColor(paletteColor: PaletteColor) { | ||
painterHistory.setPaletteColor(paletteColor) | ||
} | ||
|
||
fun setPaintThickness(painterThickness: Float) { | ||
painterHistory.setPaintThickness(painterThickness) | ||
} | ||
|
||
fun setPaletteShape(paletteShape: PaletteShape) { | ||
painterHistory.setPaletteShape(paletteShape) | ||
} | ||
|
||
fun changePaletteMode(paletteMode: PaletteMode) { | ||
painterHistory.changePaletteMode(paletteMode) | ||
} | ||
|
||
fun undo() { | ||
painterHistory.undo() | ||
invalidate() | ||
} | ||
|
||
fun redo() { | ||
painterHistory.redo() | ||
invalidate() | ||
} | ||
|
||
fun reset() { | ||
painterHistory.clear() | ||
invalidate() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
팔레트 색상이나 굵기가 유지되면 사용자 입장에서 더 자연스러울 것 같아요! 현재는 지우개나 브러시를 선택했을 때 슬라이더 위치와 실제 그려지는 두께가 다르고, 버튼을 누를 때마다 색상이 빨간색으로 초기화되어 불편합니다.
이건 요구사항은 아니니까 자유롭게 반영해주세요! (어디에 코멘트 다는게 좋을지 모르겠어서 아무데나 남깁니다..ㅋㅋ)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아무래도 Painter의 changePainter() 메서드에서 매번 새로운 Painter를 반환해주니 생기는 문제인 것 같습니다 🥲
현재 PainterHistory에서 두께, 도형, 색상에 대한 상태를 가지고 있도록 하였고, 브러시 변경시 Painter.changePainter()의 인자로 이전의 상태를 전달하도록 변경하였습니다.