-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigFontView.m
More file actions
1040 lines (772 loc) · 39.1 KB
/
Copy pathBigFontView.m
File metadata and controls
1040 lines (772 loc) · 39.1 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// BigFontView.m
// CyborgameSubtitler
//
// Created by Andrew on 6/5/13.
// Copyright (c) 2013 Vox Fera. All rights reserved.
//
#import "BigFontView.h"
#import "MainView.h"
#import "FRAppCommon.h"
#import "NSView+snapshot.h"
#import <OpenGL/OpenGL.h>
#import <OpenGL/glu.h>
#import <Syphon/Syphon.h>
#import "AppController.h"
#import "AppDelegate.h"
#import "TextSlice.h"
@implementation BigFontView
- (BOOL) acceptsFirstResponder{return YES;}
- (BOOL) resignFirstResponder{return YES;}
- (BOOL) becomeFirstResponder{return YES;}
- (BOOL) canBecomeKeyView{return YES;}
- (BOOL) isFlipped{return NO;}
#define fequal(a,b) (fabs((a) - (b)) < FLT_EPSILON)
#define fequalzero(a) (fabs(a) < FLT_EPSILON)
///////////////////////////////////////////////////////////
// NSView
///////////////////////////////////////////////////////////
- (id) initWithFrame:(NSRect)frameRect{
return [self initWithFrame:frameRect shareContext:nil];
}
- (id) initWithFrame:(NSRect)frameRect shareContext:(NSOpenGLContext*)context{
if (self) {
// Create a double-buffered view
NSOpenGLPixelFormatAttribute attribs[] =
{
kCGLPFAAccelerated,
kCGLPFANoRecovery,
kCGLPFADoubleBuffer,
kCGLPFAColorSize, 24,
kCGLPFADepthSize, 16,
0
};
pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if(!pixelFormat){NSLog(@"No OpenGL pixel format");}
// NSOpenGLView does not handle context sharing, so we draw to a custom NSView instead
openGLContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:context];
if (self = [super initWithFrame:frameRect]) {
[[self openGLContext] makeCurrentContext];
// Synchronize buffer swaps with vertical refresh rate
GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
[self setupDisplayLink];
// Look for changes in view size
// Note, -reshape will not be called automatically on size changes because NSView does not export it to override
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reshape)
name:NSViewGlobalFrameDidChangeNotification
object:self];
//Drag and Drop Setup
[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}
[self stopTimer];
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"f_typingEffect"];
[[FRAppCommon sharedFRAppCommon] setFontViewController:self];
[[self openGLContext] makeCurrentContext];
glLoadIdentity();
glGenTextures(1, &glTex_floor);
}
return self;
}
// Invert a range (used to flip slider vals)
-(float)invertValue:(float)value rangeMin:(float)rangeMin rangeMax:(float)rangeMax{
return ((value * -1)+rangeMax) + rangeMin;
}
///////////////////////////////////////////////////////////
// Displaylink
///////////////////////////////////////////////////////////
static bool isAnimating=false;
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
{
dispatch_async(dispatch_get_main_queue(), ^{
[(__bridge BigFontView*)displayLinkContext setNeedsDisplay:YES];
});
return kCVReturnSuccess;
}
- (void) setupDisplayLink{
// Create a display link capable of being used with all active displays
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
// Set the renderer output callback function
CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void *)(self));
}
- (void) startAnimation{
isAnimating=true;
if (displayLink && !CVDisplayLinkIsRunning(displayLink)){
CVDisplayLinkStart(displayLink);
}
}
- (void) stopAnimation{
isAnimating=false;
if (displayLink && CVDisplayLinkIsRunning(displayLink))
CVDisplayLinkStop(displayLink);
}
- (NSOpenGLContext*) openGLContext{return openGLContext;}
- (void) drawRect:(NSRect)dirtyRect{
// If we're not animating, start
if (!isAnimating) {[self startAnimation];}
// Draw the view into an NSImage context (so we can set the resolution)
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"outputResolution"] == 0){
renderDimensions = NSMakeSize(320, 240);
}else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"outputResolution"] == 1){
renderDimensions = NSMakeSize(640, 480);
}else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"outputResolution"] == 2){
renderDimensions = NSMakeSize(1024, 768);
}else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"outputResolution"] == 3){
//CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);
renderDimensions = NSMakeSize([[NSScreen mainScreen] frame].size.width,[[NSScreen mainScreen] frame].size.height);
}
NSImage *drawIntoImage = [[NSImage alloc] initWithSize:renderDimensions];
[drawIntoImage lockFocus];
[self drawViewOfSize:renderDimensions];
[drawIntoImage unlockFocus];
//[self syphonSendImage:drawIntoImage];
// Resize to fit preview area and draw
NSSize newSize = NSMakeSize(self.frame.size.width, self.frame.size.height);
[drawIntoImage setSize: newSize];
[[NSColor blackColor] set];
[self lockFocus];
[NSBezierPath fillRect:self.frame];
[drawIntoImage drawAtPoint:NSZeroPoint fromRect:self.frame operation:NSCompositeCopy fraction:1];
[self unlockFocus];
// OUtput syphon
[self syphonSendImage:drawIntoImage];
/*
Experimental screen capture code
/////
CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);
CFArrayRef onScreenWindows = CGWindowListCreate(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
CFArrayRef nonDesktopElements = CGWindowListCreate(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
CFRange range = CFRangeMake(0, CFArrayGetCount(nonDesktopElements));
CFMutableArrayRef desktopElements = CFArrayCreateMutableCopy(NULL, 0, onScreenWindows);
for (int i = CFArrayGetCount(desktopElements) - 1; i >= 0; i--)
{
CGWindowID window = (CGWindowID)(uintptr_t)CFArrayGetValueAtIndex(desktopElements, i);
if (CFArrayContainsValue(nonDesktopElements, range, (void*)(uintptr_t)window))
CFArrayRemoveValueAtIndex(desktopElements, i);
}
CGImageRef cgimage = CGWindowListCreateImageFromArray(CGRectInfinite, desktopElements, kCGWindowListOptionAll);
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage];
NSData* data = [rep representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]];
//[data writeToFile:@"/tmp/foo.png" atomically:YES];
[self syphonSendImage:[[NSImage alloc] initWithCGImage:cgimage size:renderDimensions]];
*/
}
-(void) syphonSend{
[self syphonSendImage:[[FRAppCommon sharedFRAppCommon] screenShot]];
}
-(void) syphonSendImage:(NSImage*)Image{
// Set current context
[openGLContext makeCurrentContext];
// Initialize Syphon
if (!syphonServer){
NSLog(@"Initializing Syphon Server...");
syphonServer = [[SyphonServer alloc] initWithName:nil context:[openGLContext CGLContextObj] options:nil];
if(!syphonServer){NSLog(@"Error initializing Syphon server!");}
if(![openGLContext CGLContextObj]){NSLog(@"Error missing opengl context");}
// Send frame
}else{
NSImage *screenShot = Image;
[self loadTextureFromNSImage:screenShot];
if(syphonServer.hasClients){[syphonServer publishFrameTexture:glTex_floor
textureTarget:GL_TEXTURE_2D
imageRegion:NSMakeRect(0,0,screenShot.size.width,screenShot.size.height)
textureDimensions:NSMakeSize(screenShot.size.width,screenShot.size.height)
flipped:NO];}
}
}
-(void)loadTextureFromNSImage:(NSImage*)floorTextureNSImage{
// If we are passed an empty image, just quit
if (floorTextureNSImage == nil){
//NSLog(@"LOADTEXTUREFROMNSIMAGE: Error: you called me with an empty image!");
return;
}
// We need to save and restore the pixel state
[self GLpushPixelState];
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, glTex_floor);
// Aquire and flip the data
NSSize imageSize = floorTextureNSImage.size;
if (![floorTextureNSImage isFlipped]) {
NSImage *drawImage = [[NSImage alloc] initWithSize:imageSize];
NSAffineTransform *transform = [NSAffineTransform transform];
[drawImage lockFocus];
[transform translateXBy:0 yBy:imageSize.height];
[transform scaleXBy:1 yBy:-1];
[transform concat];
[floorTextureNSImage drawAtPoint:NSZeroPoint
fromRect:(NSRect){NSZeroPoint, imageSize}
operation:NSCompositeCopy
fraction:1];
[drawImage unlockFocus];
floorTextureNSImage = drawImage;
}
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithData:[floorTextureNSImage TIFFRepresentation]];
// Now make a texture out of the bitmap data
// Set proper unpacking row length for bitmap.
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)[bitmap pixelsWide]);
// Set byte aligned unpacking (needed for 3 byte per pixel bitmaps).
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
NSInteger samplesPerPixel = [bitmap samplesPerPixel];
// Nonplanar, RGB 24 bit bitmap, or RGBA 32 bit bitmap.
if(![bitmap isPlanar] && (samplesPerPixel == 3 || samplesPerPixel == 4)) {
// Create one OpenGL texture
glTexImage2D(GL_TEXTURE_2D, 0,
GL_RGBA,//samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
(GLint)[bitmap pixelsWide],
(GLint)[bitmap pixelsHigh],
0,
GL_RGBA,//samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
[bitmap bitmapData]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}else{
[[NSException exceptionWithName:@"ImageFormat" reason:@"Unsupported image format" userInfo:nil] raise];
}
[self GLpopPixelState];
}
static GLint swapbytes, lsbfirst, rowlength, skiprows, skippixels, alignment;
static GLint swapbytes2, lsbfirst2, rowlength2, skiprows2, skippixels2, alignment2;
-(void)GLpushPixelState{
glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes);
glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst);
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength);
glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows);
glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels);
glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
glGetIntegerv(GL_PACK_SWAP_BYTES, &swapbytes2);
glGetIntegerv(GL_PACK_LSB_FIRST, &lsbfirst2);
glGetIntegerv(GL_PACK_ROW_LENGTH, &rowlength2);
glGetIntegerv(GL_PACK_SKIP_ROWS, &skiprows2);
glGetIntegerv(GL_PACK_SKIP_PIXELS, &skippixels2);
glGetIntegerv(GL_PACK_ALIGNMENT, &alignment2);
}
-(void)GLpopPixelState{
// Restore current pixel store state
glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
glPixelStorei(GL_PACK_SWAP_BYTES, swapbytes2);
glPixelStorei(GL_PACK_LSB_FIRST, lsbfirst2);
glPixelStorei(GL_PACK_ROW_LENGTH, rowlength2);
glPixelStorei(GL_PACK_SKIP_ROWS, skiprows2);
glPixelStorei(GL_PACK_SKIP_PIXELS, skippixels2);
glPixelStorei(GL_PACK_ALIGNMENT, alignment2);
}
///////////////////////////////////////////////////////////////////////
// Setup and Draw
///////////////////////////////////////////////////////////////////////
-(void)refreshDisplayText{
if([(AppDelegate*)[[NSApplication sharedApplication] delegate] isLoading]){
return;
}
// Input text can come from three different sources...
// What input mode?
AppDelegate* appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
int sliceSelectionIndex = (int)[[(NSCollectionView*)[appDelegate slicedTextCollection] selectionIndexes] firstIndex];
// 0=manual
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"inputSource"] == 0){
// 1=textslicer
} else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"inputSource"] == 1){
NSString* string=@"";
if ([[[appDelegate slicedText] arrangedObjects] count] >= sliceSelectionIndex){
TextSlice *thisSlice = [[[appDelegate slicedText] arrangedObjects] objectAtIndex:sliceSelectionIndex];
string = thisSlice.displayText;
}
// Allow for magic \n which will insert a blankline
string=[string stringByReplacingOccurrencesOfString:@" *^*" withString:@"\n"];
if(![previousLoadString isEqualToString:string]){
NSLog(@"Switch...");
[self stopTimer];
[self resetTimer];
[[NSUserDefaults standardUserDefaults] setValue:string forKey:@"displayText"];
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"f_typingEffectPause"];
[self setRangeMax:0];
}
previousLoadString = string;
// 2=watchfile
} else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"inputSource"] == 2){
NSError* error = nil;
NSURL *url = [[NSURL alloc] initWithString:[[NSUserDefaults standardUserDefaults] valueForKey:@"externalFilename"]];
NSString* string = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if(![string isEqualToString:previousLoadString]){
NSLog(@"Something changed!");
[self stopTimer];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_typingEffect"]){
[self resetTimer];
}
}
// Do we need to refresh displaytext?
previousLoadString = string;
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_stripLinebreaks"]){
NSCharacterSet *charactersToRemove =
[[ NSCharacterSet alphanumericCharacterSet ] invertedSet ];
string =
[[ string componentsSeparatedByCharactersInSet:charactersToRemove ]
componentsJoinedByString:@" " ];
}
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"f_watchFile"];
[[NSUserDefaults standardUserDefaults] setValue:string forKey:@"displayText"];
}
}
// Draw loop
-(void)drawViewOfSize:(NSSize)renderSize{
// Three possible sources for displaytext
[self refreshDisplayText];
NSString *displayText =[[NSUserDefaults standardUserDefaults] valueForKey:@"displayText"];
// Recalculate string if typing effect on
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_typingEffect"]){
NSRange range={0,_rangeMax};
if(range.length > [displayText length]){
_rangeMax=0;
}else{
displayText = [displayText substringWithRange:range];
}
}
// At least one space
if(!displayText){displayText=@" ";}
// Are we in fullsreen mode or not?
[[FRAppCommon sharedFRAppCommon] setIsFullscreen:f_fullscreenMode];
// Check origin
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_correctOrigin"]){
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"f_centerText"];
}
//Typing timer
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_typingEffect"]){
if(![typingTimer isValid]){
[self resetTimer];
}
}else{
if([typingTimer isValid]){
[self stopTimer];
}
}
// Push the state onto the stack
[NSGraphicsContext saveGraphicsState];
// Actually display the text
NSAffineTransform* transformation = [NSAffineTransform transform];
// The very first time this gets called all this is nil
NSFont* fontSelected = [NSFont fontWithName:@"Helvetica" size:10];
NSColor* fontColor = [NSColor whiteColor];
NSColor* backgroundColor = [NSColor blackColor];
NSColor* backgroundColor2 = [NSColor blackColor];
NSColor* colorFontShadow=[NSColor blackColor];
@try {
fontSelected = (NSFont *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"fontSelected"]];
fontColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorFont"]];
backgroundColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorBackground"]];
backgroundColor2 = (NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorBackground2"]];
colorFontShadow = (NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorFontShadow"]];
}
@catch (NSException *exception) {
NSLog(@"Running for the first time, setting defaults..");
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:fontSelected] forKey:@"fontSelected"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:fontColor] forKey:@"colorFont"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:backgroundColor] forKey:@"colorBackground"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:backgroundColor2] forKey:@"colorBackground2"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:backgroundColor2] forKey:@"colorFontShadow"];
}
NSMutableDictionary *drawStringAttributes = [[NSMutableDictionary alloc] init];
[drawStringAttributes setObject:fontSelected
forKey:NSFontAttributeName];
[drawStringAttributes setValue:(NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorFont"]] forKey:NSForegroundColorAttributeName];
NSSize stringSize = [displayText sizeWithAttributes:drawStringAttributes];
// If autoscaling, pick the scale that fills the render window
float scaleBy = [[NSUserDefaults standardUserDefaults] doubleForKey:@"scaleFactor"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"f_autoScale"]){
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scaleTextType"] == 0){
scaleBy=renderSize.height/stringSize.height;
}else{
scaleBy=renderSize.width/stringSize.width;
}
}
//Text alignment
BOOL f_centerText=false;
BOOL f_correctOrigin=false;
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"textAlignment"] == 0){
f_centerText=true;
f_correctOrigin=false;
}else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"textAlignment"] == 1){
f_centerText=false;
f_correctOrigin=false;
}else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"textAlignment"] == 2){
f_centerText=false;
f_correctOrigin=true;
}
// Center the text
float adjustedHeight;
if (f_centerText){
[transformation translateXBy:renderSize.width/2 yBy:renderSize.height/2];
// Apply the scaling
[transformation scaleXBy:scaleBy yBy:scaleBy];
// Slide text over halfway
[transformation translateXBy:-stringSize.width/2 yBy:-stringSize.height/2];
// Not centering text
}else{
// Apply the scaling
[transformation scaleXBy:scaleBy yBy:scaleBy];
// This is a hardcoded visual correction, ideally should be line width of font
// (good luck finding that)
[transformation translateXBy:-1.0 yBy:-5.0];
//Calculate the offset for origin correction
if (!fequalzero(scaleBy)){
adjustedHeight=(renderSize.height/scaleBy)-stringSize.height;
}else{
adjustedHeight=(renderSize.height)-stringSize.height;
}
}
// Gradient fill background
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"f_transparentBackground"]){
NSColor* backgroundColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorBackground"]];
NSColor* backgroundColor2 = (NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorBackground2"]];
NSGradient *backgroundGradient = [[NSGradient alloc] initWithStartingColor:backgroundColor endingColor:backgroundColor2];
[backgroundGradient drawInRect:NSMakeRect(0,0,renderSize.width,renderSize.height) angle:[[NSUserDefaults standardUserDefaults] floatForKey:@"gradientAngle"]];
}
// Add dropshadow if requested
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"f_drawShadow"]){
NSShadow *stringShadow = [[NSShadow alloc] init];
[stringShadow setShadowColor:(NSColor *)[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"colorFontShadow"]]];
NSSize shadowSize;
shadowSize.width = 2;
shadowSize.height = -2;
[stringShadow setShadowOffset:shadowSize];
[stringShadow setShadowBlurRadius:6];
[drawStringAttributes setValue:stringShadow forKey:NSShadowAttributeName];
}
//Flip
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"f_flipText"]){
[transformation scaleXBy:1.0 yBy:-1.0];
[transformation translateXBy:1.0 yBy:-stringSize.height];
}
//Mirror
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"f_mirrorText"]){
[transformation scaleXBy:-1.0 yBy:1.0];
[transformation translateXBy:-stringSize.width yBy:1.0];
}
// Correct the origin if requested
if (f_correctOrigin){
//NSLog(@"Adjusting y:%f",adjustedHeight);
[transformation translateXBy:0.0 yBy:adjustedHeight];
// Global offset
[transformation translateXBy:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetX"] yBy:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetY"]];
}else{
// Global offset
[transformation translateXBy:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetX"] yBy:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetY"]];
}
// Scroll the text
float scaledLineWidth = stringSize.width*scaleBy;
float scaledLineHeight = stringSize.height*scaleBy;
float displayWidth = renderSize.width;
float displayHeight =renderSize.height;
float rolloverAt;
// Horizontal scroll
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scrollDirection"] == HORIZONTAL){
// We rollover at half the line width plus half the display width
rolloverAt=scaledLineWidth/2 + displayWidth/2;
// But scaling only changes the linewidth coordinates, so we divide to adjust
if (!fequalzero(scaleBy)){
rolloverAt=rolloverAt/scaleBy;
}
// Vertical scroll
}else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scrollDirection"] == VERTICAL){
// We rollover at half the line width plus half the display width
rolloverAt=scaledLineHeight/2 + displayHeight/2;
// But scaling only changes the linewidth coordinates, so we divide to adjust
if (!fequalzero(scaleBy)){
rolloverAt=rolloverAt/scaleBy;
}
}
// We should scale the scrollstep as well
//float scrollUnit=1;
// Make a unit that is 1 character
float scrollUnit=1;
if(stringSize.width){
scrollUnit = stringSize.width/displayText.length;
}
if (!fequalzero(scaleBy)){
scrollUnit=scrollUnit/scaleBy;
}
// Adjust for width of window
scrollUnit = scrollUnit * (renderSize.width/858.0);
// Multiply it by the rate
scrollUnit = scrollUnit*[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollRate"];
// Scroll if not paused
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"f_scrollPause"]){
[[NSUserDefaults standardUserDefaults] setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollPosition"]+scrollUnit forKey:@"scrollPosition"];
// If we are off the screen, wrap
if (abs([[NSUserDefaults standardUserDefaults] floatForKey:@"scrollPosition"]) > rolloverAt){
if (scrollUnit < 0){
[[NSUserDefaults standardUserDefaults] setFloat:rolloverAt forKey:@"scrollPosition"];
}else{
[[NSUserDefaults standardUserDefaults] setFloat:-1*rolloverAt forKey:@"scrollPosition"];
}
}
}
// Apply scroll offset
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scrollDirection"] == HORIZONTAL){
[transformation translateXBy:[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollPosition"] yBy:1.0];
}else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scrollDirection"] == VERTICAL){
[transformation translateXBy:1.0 yBy:[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollPosition"]];
}
// Actually apply transformation to view
[transformation concat];
// NOW draw the text into the transformed view
[displayText drawAtPoint:NSMakePoint(0.0, 0.0) withAttributes:drawStringAttributes];
// Pop the stack
[NSGraphicsContext restoreGraphicsState];
[NSThread sleepUntilDate: [[NSDate date] addTimeInterval: .01]];
}
// Typing timer
-(void)incrementRange{
// Exit if paused
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_typingEffectPause"]){return;}
// Stop timer if we are out of range
if(_rangeMax >= [(NSString*)[[NSUserDefaults standardUserDefaults] valueForKey:@"displayText"] length]){
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_typingEffectLoop"]){
_rangeMax=0;
}else{
//[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"f_typingEffect"];
}
return;
}
// Add some "humanity" to the typing (microdelay)
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_typingEffectHumanize"]){
// If we're out of range
if(_rangeMax+1 >= [(NSString*)[[NSUserDefaults standardUserDefaults] valueForKey:@"displayText"] length]){
_rangeMax++;
return;
}else{
// What char are we going to type next?
NSString* nextChar=@"x";
NSRange range={_rangeMax+1,1};
nextChar=[[[NSUserDefaults standardUserDefaults] valueForKey:@"displayText"] substringWithRange:range];
// If it's uppercase or punctuation, introduce a delay
if([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:nextChar] ||
[[NSCharacterSet punctuationCharacterSet] characterIsMember:nextChar]){
if(arc4random() % 2 == 1){_rangeMax++;}
// If it's whitespace, introduce a slightly longer delay
}else if([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:nextChar]){
if(arc4random() % 5 == 1){_rangeMax++;}
// No delay
}else{ _rangeMax++;}
}
// No microdelay
}else{
_rangeMax++;
}
// Pause if EOL and requested
if (_rangeMax > 0 && [[NSUserDefaults standardUserDefaults] boolForKey:@"f_typingEffectAutoPause"]){
if(_rangeMax+1 >= [(NSString*)[[NSUserDefaults standardUserDefaults] valueForKey:@"displayText"] length]){
_rangeMax++;
return;
}
// What char are we going to type next?
NSRange range={_rangeMax,1};
NSString* thisChar=[[[NSUserDefaults standardUserDefaults] valueForKey:@"displayText"] substringWithRange:range];
if ([thisChar isEqualToString:@"\n"]){
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"f_typingEffectPause"];
}
}
//[NSThread sleepUntilDate: [[NSDate date] addTimeInterval: .01]];
// Reset timer rate if requested
[self adjustTimerToRate:[[NSUserDefaults standardUserDefaults] doubleForKey:@"typingRate"]];
}
// Key handler
- (void)keyDown:(NSEvent *)event {
NSUInteger flags = [[NSApp currentEvent] modifierFlags];
unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
float stepSizeScrollRate=0.1;
float stepSizeOffset=2;
switch (key){
case NSLeftArrowFunctionKey:{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_lockScrollOptions"]){return;}
if((flags & NSCommandKeyMask)){
//CMD+ <-
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetX"]-stepSizeOffset
forKey:@"global_offsetX"];
}else{
// <-
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollRate"]-stepSizeScrollRate
forKey:@"scrollRate"];
}
break;
}
case NSRightArrowFunctionKey:{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_lockScrollOptions"]){return;}
if((flags & NSCommandKeyMask)){
//CMD+ ->
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetX"]+stepSizeOffset
forKey:@"global_offsetX"];
}else{
// ->
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollRate"]+stepSizeScrollRate
forKey:@"scrollRate"];
}
break;
}
case NSUpArrowFunctionKey:{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_lockScrollOptions"]){return;}
if((flags & NSCommandKeyMask)){
//CMD+ ^
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetY"]+stepSizeOffset
forKey:@"global_offsetY"];
}else{
// ^
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollRate"]+stepSizeScrollRate
forKey:@"scrollRate"];
}
break;
}
case NSDownArrowFunctionKey:{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"f_lockScrollOptions"]){return;}
//CMD+ \/
if((flags & NSCommandKeyMask)){
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"global_offsetY"]-stepSizeOffset
forKey:@"global_offsetY"];
// \/
}else{
[[NSUserDefaults standardUserDefaults]
setFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"scrollRate"]-stepSizeScrollRate
forKey:@"scrollRate"];
}
break;
}
}
switch([event keyCode]) {
// ESC
case 53:{
if (f_fullscreenMode){
[self goWindowed];
}
break;
}
}
}
- (void) stopTimer{
[[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"f_typingEffectPause"];
_rangeMax=0;
[typingTimer invalidate];
}
- (void) resetTimer{
NSLog(@"reset");
_rangeMax=0;
[self adjustTimerToRate:[[NSUserDefaults standardUserDefaults] doubleForKey:@"typingRate"]];
}
-(void) adjustTimerToRate:(float)newTimerRate{
if (![typingTimer isValid] || !fequal(currentTimerRate, [[NSUserDefaults standardUserDefaults] doubleForKey:@"typingRate"])){
//NSLog(@"START Timer: %f",newTimerRate);
currentTimerRate=newTimerRate;
newTimerRate = (double)([self invertValue:newTimerRate rangeMin:0.01 rangeMax:1]);
[typingTimer invalidate];
typingTimer = [NSTimer scheduledTimerWithTimeInterval:newTimerRate
target:self
selector:@selector(incrementRange)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:typingTimer forMode:NSEventTrackingRunLoopMode];
}
}
// Screencap code based on 002.vade.info
-(GLuint) createWindowTexture:(CGLContextObj) internalContext
mainDrawingContext:(CGLContextObj) mainDrawingContext
width:(NSInteger) width
height:(NSInteger) height
originX:(NSInteger) originX
originY:(NSInteger) originY{
CGLContextObj cgl_ctx = internalContext;
if (CGLSetCurrentContext(cgl_ctx) != kCGLNoError){
NSLog(@"CANNOT MAKE CURRENT CONTEzXT");
return nil;
};
// Make sure our context is valid
if(!cgl_ctx){
NSLog(@"INVALID CONTEXT");
return nil;
}
// Thread lock OpenGL Context
CGLLockContext(cgl_ctx);
GLuint mTextureName;
GLenum theError = GL_NO_ERROR;
// set up our texture storage for copying
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
// Create and configure the texture - rectangluar coords
glGenTextures(1, &mTextureName);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mTextureName);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// define our texture - we're allowd to supply a null pointer since we are letting the GPU handle texture storage.
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGBA, (int)width,(int)height,0,GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
// read from the front buffer
glReadBuffer(GL_FRONT);
// glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mTextureName);
// copy contents of a portion of the buffer to our texture
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, (int)originX, (int)originY, (int)width, (int)height);
// fin
glFlush();
// Thread unlock
CGLUnlockContext(cgl_ctx);
//Check for OpenGL errors
theError = glGetError();
if(theError) {
NSLog(@"v002ScreenCapture: OpenGL texture creation failed (error 0x%04X)", theError);
CGLUnlockContext(cgl_ctx); // Thread unlock
return 0;
}
return mTextureName;
}
///////////////////////////////////////////////////////////////////////
// Drag and Drop
///////////////////////////////////////////////////////////////////////
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {return NSDragOperationCopy;}
-(BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {return YES;}
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender {return [AppController performDragOperation:sender];}
///////////////////////////////////////////////////////////////////////
// Fullscreen
///////////////////////////////////////////////////////////////////////
- (void)goFullscreen{
if(f_fullscreenMode){
NSLog(@"Already full...");
return;
}