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

Mod: Allow JotViewControllerDelegate To Know When Drawing Happens #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions jot/JotTouchBezier.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ + (instancetype)withColor:(UIColor *)color

- (void)jotDrawBezier
{
if (self.constantWidth) {
if (self.constantWidth || [self isErasing]) {
UIBezierPath *bezierPath = [UIBezierPath new];
[bezierPath moveToPoint:self.startPoint];
[bezierPath addCurveToPoint:self.endPoint controlPoint1:self.controlPoint1 controlPoint2:self.controlPoint2];
bezierPath.lineWidth = self.startWidth;
bezierPath.lineCapStyle = kCGLineCapRound;
[self.strokeColor setStroke];
[bezierPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.f];
[bezierPath strokeWithBlendMode:([self isErasing])?kCGBlendModeClear:kCGBlendModeNormal alpha:1.f];
} else {
[self.strokeColor setFill];

Expand Down Expand Up @@ -61,13 +61,17 @@ - (void)jotDrawBezier
}
}

- (Boolean) isErasing {
return [self.strokeColor isEqual: [UIColor clearColor]];
}

+ (void)jotDrawBezierPoint:(CGPoint)point withWidth:(CGFloat)width
{
CGContextRef context = UIGraphicsGetCurrentContext();
if (!context) {
return;
}

CGContextFillEllipseInRect(context, CGRectInset(CGRectMake(point.x, point.y, 0.f, 0.f), -width / 2.f, -width / 2.f));
}

Expand Down
38 changes: 38 additions & 0 deletions jot/JotViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ typedef NS_ENUM(NSUInteger, JotViewState){
*/
- (void)clearText;

/**
* Enter 'Erase' mode. User touch interaction will erase drawing
*/
- (void)startErasingDrawing;

/**
* Enter normal drawing mode
*/
- (void)endErasingDrawing;

/**
* Overlays the drawing and text on the given background image at the full
* resolution of the image.
Expand Down Expand Up @@ -215,6 +225,12 @@ typedef NS_ENUM(NSUInteger, JotViewState){
*/
- (UIImage *)renderImageWithScale:(CGFloat)scale onColor:(UIColor *)color;

-(void) startTouchAtPoint:(CGPoint) point withColor:(UIColor *) color;

-(void) moveTouchToPoint:(CGPoint) point withColor:(UIColor *) color;

-(void) endTouch;

@end

@protocol JotViewControllerDelegate <NSObject>
Expand All @@ -229,4 +245,26 @@ typedef NS_ENUM(NSUInteger, JotViewState){
*/
- (void)jotViewController:(JotViewController *)jotViewController isEditingText:(BOOL)isEditing;

/**
* Called whenever the user begins a stroke in JotDrawView
* @param jotViewController The JotDrawViewController to which the delegate is associated
* @param point the point of the touch
* @param color the current stroke color
*/
-(void) jotViewController:(JotViewController *)jotViewController touchesBeganAtPoint:(CGPoint)point color:(UIColor *) color;

/**
* Called whenever the user is drawing a stroke
* @param jotViewController The JotDrawViewController to which the delegate is associated
* @param point the point of the touch
* @param color the current stroke color
*/
-(void) jotViewController:(JotViewController *)jotViewController touchesMovedToPoint:(CGPoint)point color:(UIColor *) color;

/**
* Called whenever the user completes a stroke
* @param jotViewController The JotDrawViewController to which the delegate is associated
*/
-(void) jotViewControllerTouchesEnded:(JotViewController *)jotViewController;

@end
40 changes: 40 additions & 0 deletions jot/JotViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ @interface JotViewController () <UIGestureRecognizerDelegate, JotTextEditViewDel
@property (nonatomic, strong) JotDrawView *drawView;
@property (nonatomic, strong) JotTextEditView *textEditView;
@property (nonatomic, strong) JotTextView *textView;
@property (nonatomic, strong) NSMutableDictionary *eraseProperties;

@end

Expand All @@ -38,6 +39,7 @@ - (instancetype)init
_textEditView.delegate = self;
_textView = [JotTextView new];
_drawingContainer = [JotDrawingContainer new];
_eraseProperties = [NSMutableDictionary dictionary];
self.drawingContainer.delegate = self;

_font = self.textView.font;
Expand Down Expand Up @@ -243,6 +245,22 @@ - (void)setDrawingConstantStrokeWidth:(BOOL)drawingConstantStrokeWidth
}
}

#pragma mark - external draw point handlers
-(void) startTouchAtPoint:(CGPoint) point withColor:(UIColor *) color {
self.drawView.strokeColor = color;
[self.drawView drawTouchBeganAtPoint:point];
}

-(void) moveTouchToPoint:(CGPoint) point withColor:(UIColor *) color {
self.drawView.strokeColor = color;
[self.drawView drawTouchMovedToPoint:point];
}

-(void) endTouch {
[self.drawView drawTouchEnded];
}


#pragma mark - Undo

- (void)clearAll
Expand All @@ -262,6 +280,22 @@ - (void)clearText
[self.textView clearText];
}

- (void)startErasingDrawing {
self.eraseProperties[@"color"] = self.drawingColor;
self.eraseProperties[@"strokeWidth"] = @(self.drawingStrokeWidth);
self.eraseProperties[@"constantStrokeWidth"] = @(self.drawingConstantStrokeWidth);
[self setDrawingColor:[UIColor clearColor]];
[self setDrawingStrokeWidth:30.f];
[self setDrawingConstantStrokeWidth:YES];
}

- (void)endErasingDrawing {
[self setDrawingColor:self.eraseProperties[@"color"]];
[self setDrawingStrokeWidth:[self.eraseProperties[@"strokeWidth"] floatValue]];
[self setDrawingConstantStrokeWidth: [self.eraseProperties[@"constantStrokeWidth"] boolValue]];
[self.eraseProperties removeAllObjects];
}

#pragma mark - Output UIImage

- (UIImage *)drawOnImage:(UIImage *)image
Expand Down Expand Up @@ -335,20 +369,26 @@ - (void)jotDrawingContainerTouchBeganAtPoint:(CGPoint)touchPoint
{
if (self.state == JotViewStateDrawing) {
[self.drawView drawTouchBeganAtPoint:touchPoint];
if([self.delegate respondsToSelector:@selector(jotViewController:touchesBeganAtPoint:color:)])
[self.delegate jotViewController:self touchesBeganAtPoint:touchPoint color: self.drawingColor];
}
}

- (void)jotDrawingContainerTouchMovedToPoint:(CGPoint)touchPoint
{
if (self.state == JotViewStateDrawing) {
[self.drawView drawTouchMovedToPoint:touchPoint];
if([self.delegate respondsToSelector:@selector(jotViewController:touchesMovedToPoint:color:)])
[self.delegate jotViewController:self touchesMovedToPoint:touchPoint color: self.drawingColor];
}
}

- (void)jotDrawingContainerTouchEnded
{
if (self.state == JotViewStateDrawing) {
[self.drawView drawTouchEnded];
if([self.delegate respondsToSelector:@selector(jotViewControllerTouchesEnded:)])
[self.delegate jotViewControllerTouchesEnded:self];
}
}

Expand Down