Browse Source

Otacons eyes properly follow the mouse cursor

Thomas Buck 8 years ago
parent
commit
7245d17686
5 changed files with 52 additions and 9 deletions
  1. 1
    0
      OtaClock/Base.lproj/MainMenu.xib
  2. 1
    1
      OtaClock/Info.plist
  3. 4
    0
      OtaClock/MainView.h
  4. 0
    5
      OtaClock/MainView.m
  5. 46
    3
      OtaClock/MainWindow.m

+ 1
- 0
OtaClock/Base.lproj/MainMenu.xib View File

@@ -90,6 +90,7 @@
90 90
                 <outlet property="changeSize5" destination="Qji-aB-KLD" id="a79-d3-RIN"/>
91 91
                 <outlet property="keepOnTopItem" destination="bk2-WG-9eq" id="vBf-vA-eXc"/>
92 92
                 <outlet property="lockPositionItem" destination="YfQ-b7-bXC" id="CYs-rk-6GP"/>
93
+                <outlet property="mainView" destination="EiT-Mj-1SZ" id="sQZ-BE-YrX"/>
93 94
             </connections>
94 95
             <point key="canvasLocation" x="129.5" y="81"/>
95 96
         </window>

+ 1
- 1
OtaClock/Info.plist View File

@@ -19,7 +19,7 @@
19 19
 	<key>CFBundleSignature</key>
20 20
 	<string>????</string>
21 21
 	<key>CFBundleVersion</key>
22
-	<string>107</string>
22
+	<string>140</string>
23 23
 	<key>LSApplicationCategoryType</key>
24 24
 	<string>public.app-category.utilities</string>
25 25
 	<key>LSMinimumSystemVersion</key>

+ 4
- 0
OtaClock/MainView.h View File

@@ -8,6 +8,10 @@
8 8
 
9 9
 #import <Cocoa/Cocoa.h>
10 10
 
11
+#import "Render.h"
12
+
11 13
 @interface MainView : NSView
12 14
 
15
+@property (strong) Render *render;
16
+
13 17
 @end

+ 0
- 5
OtaClock/MainView.m View File

@@ -7,13 +7,10 @@
7 7
 //
8 8
 
9 9
 #import "MainWindow.h"
10
-#import "Render.h"
11 10
 #import "MainView.h"
12 11
 
13 12
 @interface MainView ()
14 13
 
15
-@property (strong) Render *render;
16
-
17 14
 @end
18 15
 
19 16
 @implementation MainView
@@ -29,8 +26,6 @@
29 26
     // Clear background
30 27
     [[NSColor clearColor] set];
31 28
     NSRectFill([self frame]);
32
-    
33
-    [render drawWith:0]; // Should be done somewhere else!
34 29
     [render drawInto:self]; // Draw composite image
35 30
 }
36 31
 

+ 46
- 3
OtaClock/MainWindow.m View File

@@ -6,6 +6,7 @@
6 6
 //  Copyright (c) 2015 xythobuz. All rights reserved.
7 7
 //
8 8
 
9
+#import "MainView.h"
9 10
 #import "MainWindow.h"
10 11
 
11 12
 #define RESIZE_START 1
@@ -16,10 +17,22 @@
16 17
 #define CONFIG_KEEP_POSITION @"keep_position"
17 18
 #define CONFIG_KEEP_ON_TOP @"keep_on_top"
18 19
 
20
+#define MOUSE_CENTER_X 337
21
+#define MOUSE_CENTER_Y 237
22
+
23
+#define EYE_BLINK 0
24
+#define EYE_TOP_RIGHT 4
25
+#define EYE_TOP_LEFT 2
26
+#define EYE_BOTTOM_RIGHT 3
27
+#define EYE_BOTTOM_LEFT 1
28
+
19 29
 @interface MainWindow ()
20 30
 
21 31
 @property (assign) NSSize defaultSize;
22 32
 @property (assign) NSInteger startScale;
33
+@property (assign) NSInteger lastEyeState;
34
+
35
+@property (weak) IBOutlet MainView *mainView;
23 36
 
24 37
 @property (weak) IBOutlet NSMenuItem *lockPositionItem;
25 38
 @property (weak) IBOutlet NSMenuItem *keepOnTopItem;
@@ -39,8 +52,7 @@
39 52
 
40 53
 @synthesize defaultSize;
41 54
 @synthesize startScale;
42
-
43
-// TODO window position should be remembered!
55
+@synthesize lastEyeState;
44 56
 
45 57
 - (id)initWithContentRect:(NSRect)contentRect
46 58
                styleMask:(NSUInteger)aStyle
@@ -50,6 +62,7 @@
50 62
     if (self != nil) {
51 63
         [self setAlphaValue:1.0];
52 64
         [self setOpaque:NO];
65
+        lastEyeState = EYE_TOP_LEFT;
53 66
         
54 67
         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
55 68
         
@@ -74,6 +87,10 @@
74 87
             startScale = [defaults integerForKey:CONFIG_START_SCALE];
75 88
         }
76 89
     }
90
+    
91
+    [self setAcceptsMouseMovedEvents:YES];
92
+    [NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *mouseMovedEvent) { [self mouseMoved:mouseMovedEvent]; }];
93
+    
77 94
     return self;
78 95
 }
79 96
 
@@ -94,6 +111,8 @@
94 111
     if (startScale == 4) [self.changeSize4 setState:NSOnState];
95 112
     if (startScale == 5) [self.changeSize5 setState:NSOnState];
96 113
     
114
+    [[self.mainView render] drawWith:lastEyeState]; // Initialize render image
115
+    
97 116
     [self setFrame:frame display:YES];
98 117
 }
99 118
 
@@ -181,7 +200,7 @@
181 200
 }
182 201
 
183 202
 - (void)mouseDown:(NSEvent *)theEvent {
184
-    self.dragStart = [theEvent locationInWindow];
203
+    dragStart = [theEvent locationInWindow];
185 204
 }
186 205
 
187 206
 - (void)mouseDragged:(NSEvent *)theEvent {
@@ -202,4 +221,28 @@
202 221
     }
203 222
 }
204 223
 
224
+- (void)mouseMoved:(NSEvent *)theEvent {
225
+    NSPoint mousePoint = [NSEvent mouseLocation];
226
+    mousePoint.x -= [self frame].origin.x;
227
+    mousePoint.y -= [self frame].origin.y;
228
+    
229
+    BOOL top = (mousePoint.y > MOUSE_CENTER_Y);
230
+    BOOL right = (mousePoint.x > MOUSE_CENTER_X);
231
+    
232
+    NSInteger eyeState = EYE_BOTTOM_LEFT;
233
+    if (top && right) {
234
+        eyeState = EYE_TOP_RIGHT;
235
+    } else if (top && (!right)) {
236
+        eyeState = EYE_TOP_LEFT;
237
+    } else if ((!top) && right) {
238
+        eyeState = EYE_BOTTOM_RIGHT;
239
+    }
240
+    
241
+    if (eyeState != lastEyeState) {
242
+        lastEyeState = eyeState;
243
+        [[self.mainView render] drawWith:lastEyeState];
244
+        self.mainView.needsDisplay = YES;
245
+    }
246
+}
247
+
205 248
 @end

Loading…
Cancel
Save