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

Revision 6901, 6.7 kB (checked in by robert, 8 years ago)

From Eric Wing, "So, something strange happened to the repo and some of this project's
files got messed up, most notiably the Nib and also the Localized
strings file. I didn't notice the latter until now so Martin is
missing this file.

Anyway, the attached tar contains all new versions of all the
necessary files. There are cleanups and fixes to a lot of things.
Martin did a good job porting the thing to osg::Viewer so most of the
code changes I made address other areas.

Two things I noticed in the new port you might want to consider as
feedback. First, there might be a bug with osgViewer when the view
size goes to 0. If you play with the splitviews in this program and
shrink the view until it is closed, and then re-expand it, the model
doesn't come back, not even after a home() call. SimpleViewer? didn't
have this problem.

Second, a more minor thing, this program has a
take-screenshot--and-copy-to-clipboard feature via Cmd-C (or Menu
item). I achieve this by using osg::Camera to render to an FBO and
then copy the contents to Cocoa. To insert the camera, I manipulate
the scenegraph so I can get the camera node in and out. I end up
calling setSceneData at the end of eveything to restore everything to
the original state before I started mucking with the scenegraph. This
unfortunately, triggers a home() reset. So in this particular case, it
make Copy look like it's changing the scene. The old SimpleViewer? had
the same problem, but I was able to work around it by directly
invoking the underlying SceneView?'s setSceneData so the home()
mechanism was bypassed. The viewer design seems to protect this data
more carefully so the bypass trick won't work. My feedback is that
maybe a flag or extra parameter can be introduced so a reset is not
triggered if not desired.

I have checked in a ton of Xcode fixes for the entire build process in
general so once this piece gets checked in, hopefully everything will
build cleanly."

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