Skip to content

Commit

Permalink
PE-72 Set the eraser back and set @deprecated annotations to legacy m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
stephanepechard authored and burhanrashid52 committed Jul 10, 2021
1 parent 5c470cf commit 358bf13
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ We can customize our brush and paint with different set of property. To start dr
| ------------- | ------------- |
| Enable/Disable | `mPhotoEditor.setBrushDrawingMode(true);` |
| Shape (brush, line, oval, rectangle) | `mPhotoEditor.addShape(shape)` |
| Shape size (px) | `mPhotoEditor.setBrushSize(brushSize)` |
| Shape opacity (In %) | `mPhotoEditor.setOpacity(opacity)` |
| Shape color | `mPhotoEditor.setBrushColor(colorCode)` |
| Shape size (px) | `mPhotoEditor.setBrushSize(brushSize)` or through the a ShapeBuilder |
| Shape opacity (In %) | `mPhotoEditor.setOpacity(opacity)` or through the a ShapeBuilder |
| Shape color | `mPhotoEditor.setBrushColor(colorCode)` or through the a ShapeBuilder |
| Brush Eraser | `mPhotoEditor.brushEraser()` |

**Note**: Whenever we set any property of a brush for drawing it will automatically enable the drawing mode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public void checkIfBrushIsEnabledWhenClickedOnBrushTool() {
assertTrue(editImageActivity.mPhotoEditor.getBrushDrawableMode());
}

@Test
public void checkIfEraserIsEnabledWhenClickedOnEraserTool() {
mActivityRule.launchActivity(null);
onView(withText(R.string.label_eraser)).perform(click());
onView(withText(R.string.label_eraser_mode)).check(matches(isDisplayed()));
}

@Test
public void checkIfShapeIsEnabledWhenClickedOnBrushTool() {
EditImageActivity editImageActivity = mActivityRule.launchActivity(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,25 +356,25 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

@Override
public void onColorChanged(int colorCode) {
mPhotoEditor.updateShape(mShapeBuilder.withShapeColor(colorCode));
mPhotoEditor.setShape(mShapeBuilder.withShapeColor(colorCode));
mTxtCurrentTool.setText(R.string.label_brush);
}

@Override
public void onOpacityChanged(int opacity) {
mPhotoEditor.updateShape(mShapeBuilder.withShapeOpacity(opacity));
mPhotoEditor.setShape(mShapeBuilder.withShapeOpacity(opacity));
mTxtCurrentTool.setText(R.string.label_brush);
}

@Override
public void onShapeSizeChanged(int shapeSize) {
mPhotoEditor.updateShape(mShapeBuilder.withShapeSize(shapeSize));
mPhotoEditor.setShape(mShapeBuilder.withShapeSize(shapeSize));
mTxtCurrentTool.setText(R.string.label_brush);
}

@Override
public void onShapePicked(ShapeType shapeType) {
mPhotoEditor.updateShape(mShapeBuilder.withShapeType(shapeType));
mPhotoEditor.setShape(mShapeBuilder.withShapeType(shapeType));
}

@Override
Expand Down Expand Up @@ -417,7 +417,7 @@ public void onToolSelected(ToolType toolType) {
case SHAPE:
mPhotoEditor.setBrushDrawingMode(true);
mShapeBuilder = new ShapeBuilder();
mPhotoEditor.updateShape(mShapeBuilder);
mPhotoEditor.setShape(mShapeBuilder);
mTxtCurrentTool.setText(R.string.label_shape);
showBottomSheetDialogFragment(mShapeBSFragment);
break;
Expand All @@ -431,6 +431,10 @@ public void onToolSelected(ToolType toolType) {
mTxtCurrentTool.setText(R.string.label_text);
});
break;
case ERASER:
mPhotoEditor.brushEraser();
mTxtCurrentTool.setText(R.string.label_eraser_mode);
break;
case FILTER:
mTxtCurrentTool.setText(R.string.label_filter);
showFilter(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public EditingToolsAdapter(OnItemSelected onItemSelected) {
mOnItemSelected = onItemSelected;
mToolList.add(new ToolModel("Shape", R.drawable.ic_oval, ToolType.SHAPE));
mToolList.add(new ToolModel("Text", R.drawable.ic_text, ToolType.TEXT));
mToolList.add(new ToolModel("Eraser", R.drawable.ic_eraser, ToolType.ERASER));
mToolList.add(new ToolModel("Filter", R.drawable.ic_photo_filter, ToolType.FILTER));
mToolList.add(new ToolModel("Emoji", R.drawable.ic_insert_emoticon, ToolType.EMOJI));
mToolList.add(new ToolModel("Sticker", R.drawable.ic_sticker, ToolType.STICKER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum ToolType {
SHAPE,
TEXT,
ERASER,
FILTER,
EMOJI,
STICKER
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<string name="label_line">Line</string>
<string name="label_emoji">Emoji</string>
<string name="label_sticker">Sticker</string>
<string name="label_eraser">Eraser</string>
<string name="label_eraser_mode">Eraser Mode</string>
<string name="label_text">Text</string>
<string name="label_filter">Filter</string>
<string name="label_adjust">Adjust</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ public class DrawingView extends View {
private final Stack<ShapeAndPaint> redoShapes = new Stack<>();
private ShapeAndPaint currentShape;
private ShapeBuilder currentShapeBuilder;

private boolean isEnabled;

private BrushViewChangeListener viewChangeListener;

// eraser parameters
private boolean isErasing = false;
static final float DEFAULT_ERASER_SIZE = 50.0f;
private float mBrushEraserSize = DEFAULT_ERASER_SIZE;
private Paint eraserPaint;
private ShapeAndPaint eraserShape;

// region constructors
public DrawingView(Context context) {
this(context, null);
Expand Down Expand Up @@ -85,6 +90,9 @@ private void setupBrushDrawing() {
setLayerType(LAYER_TYPE_HARDWARE, null);
setVisibility(View.GONE);
currentShapeBuilder = new ShapeBuilder();
eraserPaint = createPaint();
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
eraserShape = new ShapeAndPaint(new BrushShape(), eraserPaint);
}

void clearAll() {
Expand All @@ -99,9 +107,10 @@ void setBrushViewChangeListener(BrushViewChangeListener brushViewChangeListener)

@Override
protected void onDraw(Canvas canvas) {
for (ShapeAndPaint data : drawShapes) {
data.getShape().draw(canvas, data.getPaint());
for (ShapeAndPaint shape : drawShapes) {
shape.getShape().draw(canvas, shape.getPaint());
}
eraserShape.getShape().draw(canvas, eraserPaint);
}

/**
Expand Down Expand Up @@ -156,6 +165,11 @@ private void onTouchEventUp(float touchX, float touchY) {


private void createShape() {
if (isErasing) {
currentShape = eraserShape;
return;
}

final AbstractShape shape;
if (currentShapeBuilder.getShapeType() == ShapeType.OVAL) {
shape = new OvalShape();
Expand All @@ -175,6 +189,10 @@ private void createShape() {
}

private void endShape(float touchX, float touchY) {
if (isErasing) {
return;
}

if (viewChangeListener != null) {
viewChangeListener.onStopDrawing();
viewChangeListener.onViewAdd(this);
Expand Down Expand Up @@ -209,13 +227,31 @@ boolean redo() {
return !redoShapes.empty();
}

// region eraser
void brushEraser() {
isEnabled = true;
isErasing = true;
currentShape = eraserShape;
eraserPaint.setStrokeWidth(mBrushEraserSize);
}

void setBrushEraserSize(float brushEraserSize) {
mBrushEraserSize = brushEraserSize;
}

float getEraserSize() {
return mBrushEraserSize;
}
// endregion

// region Setters/Getters
public void setShapeBuilder(ShapeBuilder shapeBuilder) {
currentShapeBuilder = shapeBuilder;
}

void enableDrawing(boolean brushDrawMode) {
isEnabled = brushDrawMode;
isErasing = !brushDrawMode;
if (brushDrawMode) {
setVisibility(View.VISIBLE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;
Expand Down Expand Up @@ -118,6 +120,69 @@ public interface PhotoEditor {
*/
Boolean getBrushDrawableMode();

/**
* Set the size of brush user want to paint on canvas i.e {@link DrawingView}
* @deprecated use {@code setShape} of a ShapeBuilder
*
* @param size size of brush
*/
@Deprecated
void setBrushSize(float size);

/**
* set opacity/transparency of brush while painting on {@link DrawingView}
* @deprecated use {@code setShape} of a ShapeBuilder
*
* @param opacity opacity is in form of percentage
*/
@Deprecated
void setOpacity(@IntRange(from = 0, to = 100) int opacity);

/**
* set brush color which user want to paint
* @deprecated use {@code setShape} of a ShapeBuilder
*
* @param color color value for paint
*/
@Deprecated
void setBrushColor(@ColorInt int color);

/**
* set the eraser size
* <b>Note :</b> Eraser size is different from the normal brush size
*
* @param brushEraserSize size of eraser
*/
void setBrushEraserSize(float brushEraserSize);

/**
* @return provide the size of eraser
* @see PhotoEditor#setBrushEraserSize(float)
*/
float getEraserSize();

/**
* @return provide the size of eraser
* @see PhotoEditor#setBrushSize(float)
*/
float getBrushSize();

/**
* @return provide the size of eraser
* @see PhotoEditor#setBrushColor(int)
*/
int getBrushColor();

/**
* <p>
* Its enables eraser mode after that whenever user drags on screen this will erase the existing
* paint
* <br>
* <b>Note</b> : This eraser will work on paint views only
* <p>
*/
void brushEraser();

/**
* Undo the last operation perform on the {@link PhotoEditor}
*
Expand Down Expand Up @@ -322,7 +387,7 @@ interface OnSaveListener {
* Update the current shape to be drawn,
* through the use of a ShapeBuilder.
*/
void updateShape(ShapeBuilder shapebuilder);
void setShape(ShapeBuilder shapebuilder);
// endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;
Expand Down Expand Up @@ -199,6 +201,63 @@ public Boolean getBrushDrawableMode() {
return drawingView != null && drawingView.isDrawingEnabled();
}


@Override
public void setBrushSize(float size) {
if (drawingView != null && drawingView.getCurrentShapeBuilder() != null) {
drawingView.getCurrentShapeBuilder().withShapeSize(size);
}
}

@Override
public void setOpacity(@IntRange(from = 0, to = 100) int opacity) {
if (drawingView != null && drawingView.getCurrentShapeBuilder() != null) {
opacity = (int) ((opacity / 100.0) * 255.0);
drawingView.getCurrentShapeBuilder().withShapeOpacity(opacity);
}
}

@Override
public void setBrushColor(@ColorInt int color) {
if (drawingView != null && drawingView.getCurrentShapeBuilder() != null) {
drawingView.getCurrentShapeBuilder().withShapeColor(color);
}
}

@Override
public float getBrushSize() {
if (drawingView != null && drawingView.getCurrentShapeBuilder() != null) {
return drawingView.getCurrentShapeBuilder().getShapeSize();
}
return 0;
}

@Override
public int getBrushColor() {
if (drawingView != null && drawingView.getCurrentShapeBuilder() != null) {
return drawingView.getCurrentShapeBuilder().getShapeColor();
}
return 0;
}

@Override
public void setBrushEraserSize(float brushEraserSize) {
if (drawingView != null) {
drawingView.setBrushEraserSize(brushEraserSize);
}
}

@Override
public float getEraserSize() {
return drawingView != null ? drawingView.getEraserSize() : 0;
}

@Override
public void brushEraser() {
if (drawingView != null)
drawingView.brushEraser();
}

@Override
public boolean undo() {
return mGraphicManager.undoView();
Expand Down Expand Up @@ -296,7 +355,7 @@ public boolean isCacheEmpty() {

// region Shape
@Override
public void updateShape(ShapeBuilder shapeBuilder) {
public void setShape(ShapeBuilder shapeBuilder) {
drawingView.setShapeBuilder(shapeBuilder);
}
// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,17 @@ public void testCanvasIsDrawingCorrectlyOnDraw() {
BrushViewChangeListener brushViewChangeListener = Mockito.mock(BrushViewChangeListener.class);
DrawingView drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener);

touchView(drawingView, MotionEvent.ACTION_DOWN);
touchView(drawingView, MotionEvent.ACTION_DOWN);
touchView(drawingView, MotionEvent.ACTION_DOWN);
touchView(drawingView, MotionEvent.ACTION_DOWN);
int numberOfTouch = 4;
for (int i = 0; i < numberOfTouch; i++) {
touchView(drawingView, MotionEvent.ACTION_DOWN);
}

Canvas canvas = Mockito.mock(Canvas.class);
drawingView.onDraw(canvas);

verify(canvas, times(4)).drawPath(any(Path.class), any(Paint.class));
int numberOfDraw = numberOfTouch + 1; // +1 for eraser
verify(canvas, times(numberOfDraw))
.drawPath(any(Path.class), any(Paint.class));
}

}

0 comments on commit 358bf13

Please sign in to comment.