blob: 904cb9c99225c14ce0dd6c9352b3fa5deba7948f [file] [log] [blame]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00001#import "SkUIView.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00002#include "SkCanvas.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00003#include "SkCGUtils.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00004@implementation SkUIView
5
yangsu@google.com688823f2011-08-30 19:14:13 +00006@synthesize fWind, fTitleItem, fOptionsDelegate;
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00007
yangsu@google.com688823f2011-08-30 19:14:13 +00008- (id)initWithDefaults {
9 fWind = NULL;
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000010 return self;
11}
12
13- (id)initWithCoder:(NSCoder*)coder {
14 if ((self = [super initWithCoder:coder])) {
yangsu@google.com688823f2011-08-30 19:14:13 +000015 self = [self initWithDefaults];
16 [self setUpWindow];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000017 }
18 return self;
19}
20
21- (id)initWithFrame:(CGRect)frame {
22 if (self = [super initWithFrame:frame]) {
yangsu@google.com688823f2011-08-30 19:14:13 +000023 self = [self initWithDefaults];
24 [self setUpWindow];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000025 }
26 return self;
27}
28
yangsu@google.com688823f2011-08-30 19:14:13 +000029- (void)setUpWindow {
30 if (NULL != fWind) {
31 fWind->setVisibleP(true);
commit-bot@chromium.orgb45c56e2014-02-18 23:32:05 +000032 fWind->resize(self.frame.size.width, self.frame.size.height,
commit-bot@chromium.org757ebd22014-04-10 22:36:34 +000033 kPMColor_SkColorType);
yangsu@google.com688823f2011-08-30 19:14:13 +000034 }
35}
36
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000037- (void)dealloc {
38 delete fWind;
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000039 [fTitleItem release];
40 [super dealloc];
41}
yangsu@google.com2ba30c02011-07-19 15:17:44 +000042
yangsu@google.com688823f2011-08-30 19:14:13 +000043- (void)forceRedraw {
44 [self drawInRaster];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000045}
46
yangsu@google.com2ba30c02011-07-19 15:17:44 +000047- (void)drawInRaster {
yangsu@google.com688823f2011-08-30 19:14:13 +000048 SkCanvas canvas(fWind->getBitmap());
49 fWind->draw(&canvas);
yangsu@google.com2ba30c02011-07-19 15:17:44 +000050 CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap());
yangsu@google.com688823f2011-08-30 19:14:13 +000051 self.layer.contents = (id)cgimage;
yangsu@google.com2ba30c02011-07-19 15:17:44 +000052 CGImageRelease(cgimage);
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000053}
yangsu@google.com2ba30c02011-07-19 15:17:44 +000054
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000055//Gesture Handlers
56- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
57 for (UITouch *touch in touches) {
58 CGPoint loc = [touch locationInView:self];
59 fWind->handleClick(loc.x, loc.y, SkView::Click::kDown_State, touch);
60 }
61}
62
63- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
64 for (UITouch *touch in touches) {
65 CGPoint loc = [touch locationInView:self];
66 fWind->handleClick(loc.x, loc.y, SkView::Click::kMoved_State, touch);
67 }
68}
69
70- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
71 for (UITouch *touch in touches) {
72 CGPoint loc = [touch locationInView:self];
73 fWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
74 }
75}
76
77- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
78 for (UITouch *touch in touches) {
79 CGPoint loc = [touch locationInView:self];
80 fWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
81 }
82}
83
84///////////////////////////////////////////////////////////////////////////////
85
86- (void)setSkTitle:(const char *)title {
yangsu@google.com688823f2011-08-30 19:14:13 +000087 if (fTitleItem) {
88 fTitleItem.title = [NSString stringWithUTF8String:title];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000089 }
90}
91
92- (BOOL)onHandleEvent:(const SkEvent&)evt {
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000093 return false;
94}
95
bsalomon@google.com64cc8102013-03-05 20:06:05 +000096- (void)getAttachmentInfo:(SkOSWindow::AttachmentInfo*)info {
97 // we don't have a GL context.
98 info->fSampleCount = 0;
99 info->fStencilBits = 0;
100}
101
yangsu@google.com59870452011-08-02 13:20:22 +0000102#include "SkOSMenu.h"
103- (void)onAddMenu:(const SkOSMenu*)menu {
104 [self.fOptionsDelegate view:self didAddMenu:menu];
105}
caryclark@google.com867cbd82012-09-20 15:45:41 +0000106- (void)onUpdateMenu:(SkOSMenu*)menu {
yangsu@google.com59870452011-08-02 13:20:22 +0000107 [self.fOptionsDelegate view:self didUpdateMenu:menu];
108}
109
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000110- (void)postInvalWithRect:(const SkIRect*)r {
yangsu@google.com688823f2011-08-30 19:14:13 +0000111 [self performSelector:@selector(drawInRaster) withObject:nil afterDelay:0];
112 [self setNeedsDisplay];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000113}
114
115@end