/
WandaWindow.m
193 lines (165 loc) · 4.69 KB
/
WandaWindow.m
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Wanda, the Cocoa port of GNOME Wanda
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The program from which this was derived can be found at:
* http://git.gnome.org/cgit/gnome-panel/tree/gnome-panel/nothing.cP
*
* (C) 2009 Tim Horton
*
*/
#import "WandaWindow.h"
#import "WandaController.h"
#import <QuartzCore/QuartzCore.h>
@implementation WandaWindow
- (id) initWithContentRect:(NSRect)contentRect
styleMask:(NSUInteger)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
if (self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:bufferingType
defer:flag])
{
[self setMovableByWindowBackground:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setOpaque:NO];
[self setHasShadow:NO];
// Float over all windows, don't react to Exposé
[self setLevel:NSScreenSaverWindowLevel];
[self setCollectionBehavior:NSWindowCollectionBehaviorStationary];
// This MUST be set to the actual size of a single frame in the image.
[self setAspectRatio:NSMakeSize(90.0, 55.0)];
// Generate a random initial direction
xDirection = (rand() % 2 ? -1 : 1) * 2;
yDirection = (rand() % 2 ? -1 : 1);
swimToHiding = hid = NO;
// Generate a random initial position
NSRect screenFrame = [[self screen] visibleFrame];
[self setFrameOrigin:NSMakePoint(rand() % (int)(screenFrame.size.width - contentRect.size.width),
rand() % (int)(screenFrame.size.height - contentRect.size.height))];
// Update frame to proper size, start swimming timer
NSRect newFrame = [self frame];
newFrame.size.width = 36.0;
newFrame.size.height = ([self aspectRatio].height /
[self aspectRatio].width) * newFrame.size.width;
[self setFrame:newFrame display:YES];
}
return self;
}
- (void)mouseDown:(NSEvent *)event
{
swimToHiding = YES;
// Update faster while we're running away
[(WandaController*)controller updateTimer:YES];
}
- (void)updateFrame
{
NSRect newFrame = [self frame];
NSRect screenFrame = [[self screen] visibleFrame];
if(swimToHiding)
screenFrame = [[self screen] frame];
// Occasionally come out of hiding
if(rand() % 2000 == 0 && swimToHiding && hid)
{
// Fade back in
[wanda layer].opacity = 1.0;
swimToHiding = hid = NO;
newFrame.origin.x = 0;
newFrame.origin.y = (rand() % (int)(screenFrame.size.height - newFrame.size.height));
xDirection = abs(xDirection);
yDirection = abs(yDirection);
xLastSwapped = yLastSwapped = 0;
// Return to a normal update speed
[(WandaController*)controller updateTimer:NO];
}
if(hid)
return;
// Occasionally randomly change direction, but only if we haven't recently
// done so, in order to reduce sudden movements, and only if we're not hiding
if(rand() % 50 == 0 && yLastSwapped > 20 && !swimToHiding)
{
yDirection = -yDirection;
yLastSwapped = 0;
}
if(rand() % 200 == 0 && xLastSwapped > 50 && !swimToHiding)
{
xDirection = -xDirection;
xLastSwapped = 0;
}
xLastSwapped++;
yLastSwapped++;
// Don't go past the bounds of the display, unless we're trying to hide!!
if(newFrame.origin.x >= ((screenFrame.size.width + screenFrame.origin.x) - newFrame.size.width))
{
if(swimToHiding)
{
[self fishHid];
return;
}
else
{
xDirection = -abs(xDirection);
xLastSwapped = 0;
}
}
else if(newFrame.origin.x <= screenFrame.origin.x)
{
if(swimToHiding)
{
[self fishHid];
return;
}
else
{
xDirection = abs(xDirection);
xLastSwapped = 0;
}
}
if(newFrame.origin.y >= ((screenFrame.size.height + screenFrame.origin.y) - newFrame.size.height))
{
if(swimToHiding)
{
[self fishHid];
return;
}
else
{
yDirection = -abs(yDirection);
yLastSwapped = 0;
}
}
else if(newFrame.origin.y <= screenFrame.origin.y)
{
if(swimToHiding)
{
[self fishHid];
return;
}
else
{
yDirection = abs(yDirection);
yLastSwapped = 0;
}
}
newFrame.origin.x += xDirection * (swimToHiding ? 3.5 : 1.0);
newFrame.origin.y += yDirection;
[wanda setBackwards:(xDirection >= 0 ? YES : NO)];
[self setFrame:newFrame display:YES];
}
- (void)fishHid
{
// Fade out
[wanda layer].opacity = 0.0;
hid = YES;
}
@end