yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 1 | #import "SkUIView.h" |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 2 | #include "SkCanvas.h" |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 3 | #include "SkCGUtils.h" |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 4 | @implementation SkUIView |
| 5 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 6 | @synthesize fWind, fTitleItem, fOptionsDelegate; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 7 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 8 | - (id)initWithDefaults { |
| 9 | fWind = NULL; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 10 | return self; |
| 11 | } |
| 12 | |
| 13 | - (id)initWithCoder:(NSCoder*)coder { |
| 14 | if ((self = [super initWithCoder:coder])) { |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 15 | self = [self initWithDefaults]; |
| 16 | [self setUpWindow]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 17 | } |
| 18 | return self; |
| 19 | } |
| 20 | |
| 21 | - (id)initWithFrame:(CGRect)frame { |
| 22 | if (self = [super initWithFrame:frame]) { |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 23 | self = [self initWithDefaults]; |
| 24 | [self setUpWindow]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 25 | } |
| 26 | return self; |
| 27 | } |
| 28 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 29 | - (void)setUpWindow { |
| 30 | if (NULL != fWind) { |
| 31 | fWind->setVisibleP(true); |
| 32 | fWind->resize(self.frame.size.width, self.frame.size.height, |
| 33 | SkBitmap::kARGB_8888_Config); |
| 34 | } |
| 35 | } |
| 36 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 37 | - (void)dealloc { |
| 38 | delete fWind; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 39 | [fTitleItem release]; |
| 40 | [super dealloc]; |
| 41 | } |
yangsu@google.com | 2ba30c0 | 2011-07-19 15:17:44 +0000 | [diff] [blame] | 42 | |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 43 | - (void)forceRedraw { |
| 44 | [self drawInRaster]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 45 | } |
| 46 | |
yangsu@google.com | 2ba30c0 | 2011-07-19 15:17:44 +0000 | [diff] [blame] | 47 | - (void)drawInRaster { |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 48 | SkCanvas canvas(fWind->getBitmap()); |
| 49 | fWind->draw(&canvas); |
yangsu@google.com | 2ba30c0 | 2011-07-19 15:17:44 +0000 | [diff] [blame] | 50 | CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap()); |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 51 | self.layer.contents = (id)cgimage; |
yangsu@google.com | 2ba30c0 | 2011-07-19 15:17:44 +0000 | [diff] [blame] | 52 | CGImageRelease(cgimage); |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 53 | } |
yangsu@google.com | 2ba30c0 | 2011-07-19 15:17:44 +0000 | [diff] [blame] | 54 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 55 | //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.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 87 | if (fTitleItem) { |
| 88 | fTitleItem.title = [NSString stringWithUTF8String:title]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | - (BOOL)onHandleEvent:(const SkEvent&)evt { |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 96 | #include "SkOSMenu.h" |
| 97 | - (void)onAddMenu:(const SkOSMenu*)menu { |
| 98 | [self.fOptionsDelegate view:self didAddMenu:menu]; |
| 99 | } |
| 100 | - (void)onUpdateMenu:(const SkOSMenu*)menu { |
| 101 | [self.fOptionsDelegate view:self didUpdateMenu:menu]; |
| 102 | } |
| 103 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 104 | - (void)postInvalWithRect:(const SkIRect*)r { |
yangsu@google.com | 688823f | 2011-08-30 19:14:13 +0000 | [diff] [blame] | 105 | [self performSelector:@selector(drawInRaster) withObject:nil afterDelay:0]; |
| 106 | [self setNeedsDisplay]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @end |