NSTextField over NSOpenGLView

前端 未结 7 1949
情歌与酒
情歌与酒 2021-02-08 05:42

I have made a window with an NSOpenGLView that I am rendering openGL content into.

I want to add some buttons and text fields to the view: I can add NSTextFields and NSB

7条回答
  •  迷失自我
    2021-02-08 06:04

    I'm not heavily into OpenGL yet, but it's my understanding that you can accomplish the visual effect of subviews with Quartz Extreme using layer-backed views; however, those may be problematic. Since subviews are not supported directly, any solution is liable to be a hack.

    Indeed, the solution in that link actually hacks a second window to appear over your OpenGL display, the second window displaying the Cocoa views you desire.

    The following code (from the above link) is something I've not tested (again not being an OpenGL guy by nature -- yet), but appears like a fairly clever approach:

    // This is the GL Window and view you already have
    glWindow = [[GLWindow alloc] initWithContentRect:windowRect];
    glView = [[[GLView alloc] initWithFrame:NSMakeRect(0, 0, windowRect.size.width, windowRect.size.height)] autorelease];
    [glView translateOriginToPoint:NSMakePoint(glView.bounds.size.width/2, glView.bounds.size.height/2)];
    [glWindow setContentView:glView];
    
    // And here's your transparent UI window
    uiWindow = [[TransparentWindow alloc] initWithContentRect:windowRect];
    uiView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, windowRect.size.width, windowRect.size.height)] autorelease];
    [uiView translateOriginToPoint:NSMakePoint(uiView.bounds.size.width/2, uiView.bounds.size.height/2)];
    uiView.wantsLayer = YES;
    
    [uiWindow setContentView:uiView];
    [glWindow addChildWindow:uiWindow ordered:NSWindowAbove];
    

    Again, I've not tested this, but it looks like it will get you the visual effect you desire.

提交回复
热议问题