aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/iOSSampleApp/SkUIView_shell.mm
blob: 7fe35d79d3074a864111c0e5c2978cc5002c1f56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "SkCanvas.h"
#include "SkCGUtils.h"
#include "SkEvent.h"
#include "SkOSWindow_iOS.h"
#include "SkView.h"
#import "SkUIView_shell.h"

@implementation SkUIView_shell
@synthesize fTitle;

//Overwritten from UIView
- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize s = self.bounds.size;
    fSkWind->resize(s.width, s.height);
}

- (void)drawRect:(CGRect)rect {
    //TODO -- check if our UIView is backed by a CALayer, and possibly use
    //skia's gpu backend
    if (fSkWind != nil) {
        SkCanvas canvas;
        SkIRect dirtyRect = SkIRect::MakeWH(rect.size.width, rect.size.height);
        fSkWind->update(&dirtyRect, &canvas);

        CGImageRef cgimage = SkCreateCGImageRef(fSkWind->getBitmap());
        [[UIImage imageWithCGImage:cgimage] drawAtPoint:CGPointMake(0, 44)];
        CGImageRelease(cgimage);
    }
}

- (void)dealloc {
    [fTitle release];
    [super dealloc];
}

//Instance methods
- (void)setSkWindow:(SkOSWindow*)anSkWindow {
    fSkWind = anSkWindow;
}

//Handlers for SkOSWindow
- (void)setSkTitle:(const char *)title {
    fTitle.title = [NSString stringWithUTF8String:title];
}

- (BOOL)onHandleEvent:(const SkEvent&)event {
    return false;
}

- (void)postInvalWithRect:(const SkIRect*)rect {
    if (rect) {
        [self setNeedsDisplayInRect:CGRectMake(rect->fLeft, rect->fTop,
                                               rect->width(), rect->height())];
    } else {
        [self setNeedsDisplay];
    }
}

//Gesture Handlers
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint loc = [touch locationInView:self];
        fSkWind->handleClick(loc.x, loc.y, SkView::Click::kDown_State, touch);
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint loc = [touch locationInView:self];
        fSkWind->handleClick(loc.x, loc.y, SkView::Click::kMoved_State, touch);
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint loc = [touch locationInView:self];
        fSkWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint loc = [touch locationInView:self];
        fSkWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
    }
}
@end