Skip to content

Instantly share code, notes, and snippets.

@mutle
Created May 15, 2009 19:38
Show Gist options
  • Save mutle/112397 to your computer and use it in GitHub Desktop.
Save mutle/112397 to your computer and use it in GitHub Desktop.
HotCocoa::Mappings.map :gl_view => :NSOpenGLView do
defaults :frame => DefaultEmptyRect,
:layout => {}
constant :auto_resize, {
:none => NSViewNotSizable,
:width => NSViewWidthSizable,
:height => NSViewHeightSizable,
:min_x => NSViewMinXMargin,
:min_y => NSViewMinYMargin,
:max_x => NSViewMaxXMargin,
:max_y => NSViewMaxYMargin
}
constant :border, {
:none => NSNoBorder,
:line => NSLineBorder,
:bezel => NSBezelBorder,
:groove => NSGrooveBorder
}
def init_with_options(view, options)
attribs = [
NSOpenGLPFANoRecovery,
NSOpenGLPFAWindow,
NSOpenGLPFAAccelerated,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAStencilSize, 8,
NSOpenGLPFAAccumSize, 0,
0
]
fmt = NSOpenGLPixelFormat.alloc.initWithAttributes(attribs.pack('i*'))
view.initWithFrame options.delete(:frame), pixelFormat: fmt
end
custom_methods do
def auto_resize=(value)
setAutoresizingMask(value)
end
def drawRect(rect)
glViewport(0, 0, frame.size.width, frame.size.height)
glClearColor(*clear_color)
glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT + GL_STENCIL_BUFFER_BIT)
send(:draw) if respond_to?(:draw)
openGLContext.flushBuffer
end
def <<(view)
addSubview(view)
end
def clear_color
@clear_color || [0.0, 0.0, 0.0, 0.0]
end
def clear_color=(colors)
if colors.is_a? Array
@clear_color = colors
end
end
def layout=(options)
@layout = LayoutOptions.new(self, options)
@layout.update_layout_views!
end
def layout
@layout
end
def on_notification(options={}, &block)
options[:sent_by] = self
NotificationListener.new(options, &block)
end
def remove(subview, options = {})
raise ArgumentError, "#{subview} is not a subview of #{self} and cannot be removed." unless subview.superview == self
options[:needs_display] == false ? subview.removeFromSuperviewWithoutNeedingDisplay : subview.removeFromSuperview
end
def enter_full_screen(options={})
enterFullScreenMode((options.delete(:screen) || NSScreen.mainScreen), withOptions:options)
end
def leave_full_screen(options={})
exitFullScreenModeWithOptions(options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment