root/OpenSceneGraph/trunk/examples/osgviewerCocoa/ViewerCocoa.h @ 6941

Revision 6941, 7.6 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

Line 
1/* -*-c++-*-
2*
3*  OpenSceneGraph example, osgviewerCacoa.
4*
5*  Permission is hereby granted, free of charge, to any person obtaining a copy
6*  of this software and associated documentation files (the "Software"), to deal
7*  in the Software without restriction, including without limitation the rights
8*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9*  copies of the Software, and to permit persons to whom the Software is
10*  furnished to do so, subject to the following conditions:
11*
12*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18*  THE SOFTWARE.
19*/
20
21//
22//  ViewerCocoa.h
23//  osgviewerCocoa
24//
25//  Created by Eric Wing on 11/12/06.
26//  Copyright 2006. Released under the OSGPL.
27//  Ported to osgViewer::Viewer by Martin Lavery 7/06/07
28//
29/* This is the class interface for a custom NSView that interfaces with an osgViewer.
30 * Because Cocoa is written in Objective-C, but OSG is written in C++, we rely on
31 * Objective-C++ to make the integration easy.
32 *
33 * One thing to remember is that any Objective-C files that include this header
34 * must also be compiled as Objective-C++ because there are C++ constructs in
35 * this file (such as namespaces) which the normal Objective-C compiler
36 * won't understand. (The easy way to do this is rename the .m files to .mm.)
37 *
38 * In the event that you have a large, pre-existing code base written in
39 * pure Objective-C and you find that the header include propagates to a
40 * large number of your files, forcing you to mark many files to be compiled as
41 * Objective-C++, and you find that you don't want to change these files because
42 * they are shared with other pure Obj-C projects, you might consider further
43 * wrapping the C++ code so there are only C or Obj-C constructs in
44 * this header. There are several different techniques ranging from, wrapping
45 * the C++ code in pure C interfaces, to simply using void pointers in this
46
47* file for any C++ pointers and casting appropriately in the implementation
48 * file.
49 */
50
51
52#import <Cocoa/Cocoa.h>
53
54namespace osgViewer
55{
56        // Just a forward declaration so I don't need the #include in the header.
57        class Viewer;
58        class GraphicsWindowEmbedded;
59}
60
61// Subclass NSOpenGLView. We could subclass NSView instead, but NSOpenGLView is easy.
62@interface ViewerCocoa : NSOpenGLView
63{
64        // Note: In Objective-C++, if you use objects instead of pointers as
65        // member instance variables, you MUST turn on "Call C++ Default Ctors/Dtors in Objective-C".
66        // -fobjc-call-cxx-cdtors
67        // This option makes sure constructors and destructors are run.
68        // This option is only available for gcc 4.0+ (Mac OS X 10.4+)
69
70        // Is SimpleViewer supposed use ref_ptr? (Doesn't look like it to me.)
71        // If so, remember ref_ptr is an object on the stack and the cdtors option must be activated.
72        // We could also make simpleViewer an object instead of a pointer, but again, turn on the option.
73        osgViewer::Viewer* theViewer;
74        osgViewer::GraphicsWindowEmbedded* graphicsWindow;
75               
76
77        // This timer is used to trigger animation callbacks since everything is event driven.
78        NSTimer* animationTimer;
79
80        // Flags to help track whether ctrl-clicking or option-clicking is being used
81        BOOL isUsingCtrlClick;
82        BOOL isUsingOptionClick;
83       
84        // Flag to track whether the OpenGL multithreading engine is enabled or not
85        BOOL isUsingMultithreadedOpenGLEngine;
86       
87}
88
89// My custom static method to create a basic pixel format
90+ (NSOpenGLPixelFormat*) basicPixelFormat;
91
92
93// Official init methods
94- (id) initWithFrame:(NSRect)frame_rect pixelFormat:(NSOpenGLPixelFormat*)pixel_format;
95- (id) initWithCoder:(NSCoder*)the_coder;
96- (id) initWithFrame:(NSRect)frame_rect;
97
98// Official function, overridden by this class to prevent flashing/tearing when in splitviews, scrollviews, etc.
99- (void) renewGState;
100
101// My custom function for minimization.
102- (void) prepareForMiniaturization:(NSNotification*)notification;
103
104
105// Custom function to allow users to know if the Multithreaded OpenGL Engine is enabled
106- (BOOL) isUsingMultithreadedOpenGLEngine;
107
108// Private init helper methods
109- (void) initSharedOpenGLContext;
110- (void) commonInit;
111- (void) initOSGViewer;
112- (void) initAnimationTimer;
113
114// Official/Special NSOpenGLView method that gets called for you to prepare your OpenGL state.
115- (void) prepareOpenGL;
116// Class dealloc method
117- (void) dealloc;
118- (void) finalize;
119
120// Official mouse event methods
121- (void) mouseDown:(NSEvent*)the_event;
122- (void) mouseDragged:(NSEvent*)the_event;
123- (void) mouseUp:(NSEvent*)the_event;
124- (void) rightMouseDown:(NSEvent*)the_event;
125- (void) rightMouseDragged:(NSEvent*)the_event;
126- (void) rightMouseUp:(NSEvent*)the_event;
127- (void) otherMouseDown:(NSEvent*)the_event;
128- (void) otherMouseDragged:(NSEvent*)the_event;
129- (void) otherMouseUp:(NSEvent*)the_event;
130
131// Private setter/getter methods to track ctrl/option-clicking
132- (void) setIsUsingCtrlClick:(BOOL)is_using_ctrl_click;
133- (BOOL) isUsingCtrlClick;
134- (void) setIsUsingOptionClick:(BOOL)is_using_option_click;
135- (BOOL) isUsingOptionClick;
136// Private helper methods to help deal with mouse events
137- (void) doLeftMouseButtonDown:(NSEvent*)the_event;
138- (void) doLeftMouseButtonUp:(NSEvent*)the_event;
139- (void) doRightMouseButtonDown:(NSEvent*)the_event;
140- (void) doRightMouseButtonUp:(NSEvent*)the_event;
141- (void) doMiddleMouseButtonDown:(NSEvent*)the_event;
142- (void) doExtraMouseButtonDown:(NSEvent*)the_event buttonNumber:(int)button_number;
143- (void) doMiddleMouseButtonUp:(NSEvent*)the_event;
144- (void) doExtraMouseButtonUp:(NSEvent*)the_event buttonNumber:(int)button_number;
145
146// Official scrollWheel (scrollball) method
147- (void) scrollWheel:(NSEvent*)the_event;
148
149// Official methods for keyboard events
150- (BOOL) acceptsFirstResponder;
151- (void) keyDown:(NSEvent*)the_event;
152- (void) keyUp:(NSEvent*)the_event;
153
154// My custom method to handle timer callbacks
155- (void) animationCallback;
156
157// Official methods for view stuff and drawing
158- (BOOL) isOpaque;
159- (void) resizeViewport;
160- (void) reshape;
161- (void) drawRect:(NSRect)the_rect;
162
163// Private helper methods for drawing
164- (NSBitmapImageRep*) renderOpenGLSceneToFramebuffer;
165- (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height;
166- (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height clearColorRed:(float)clear_red clearColorGreen:(float)clear_green clearColorBlue:(float)clear_blue clearColorAlpha:(float)clear_alpha;
167- (NSImage*)imageFromBitmapImageRep:(NSBitmapImageRep*)bitmap_image_rep;
168
169
170// Official methods for drag and drop (view as target)
171- (unsigned int) draggingEntered:(id <NSDraggingInfo>)the_sender;
172- (void) draggingExited:(id <NSDraggingInfo>)the_sender;
173- (BOOL) prepareForDragOperation:(id <NSDraggingInfo>)the_sender;
174- (BOOL) performDragOperation:(id <NSDraggingInfo>)the_sender;
175- (void) concludeDragOperation:(id <NSDraggingInfo>)the_sender;
176
177// Official method for copy (i.e. copy & paste)
178- (IBAction) copy:(id)sender;
179
180// Private helper methods for drag and drop and copy/paste (view as source)
181- (NSData*) dataWithTIFFOfContentView;
182- (NSData*) contentsAsDataOfType:(NSString *)pboardType;
183- (void) startDragAndDropAsSource:(NSEvent*)the_event;
184
185
186// Examples of providing an action to connect to.
187- (IBAction) resetPosition:(id)the_sender;
188- (IBAction) takeBackgroundColorFrom:(id)the_sender;
189
190
191@end
Note: See TracBrowser for help on using the browser.