use std::collections::vec_deque::IntoIter as VecDequeIter;
use std::default::Default;
use Api;
use BuilderAttribs;
use ContextError;
use CreationError;
use CursorState;
use Event;
use GlContext;
use GlProfile;
use GlRequest;
use MouseCursor;
use PixelFormat;
use Robustness;
use native_monitor::NativeMonitorId;
use gl_common;
use libc;
use platform;
pub struct WindowBuilder<'a> {
attribs: BuilderAttribs<'a>
}
impl<'a> WindowBuilder<'a> {
pub fn new() -> WindowBuilder<'a> {
WindowBuilder {
attribs: BuilderAttribs::new(),
}
}
pub fn with_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> {
self.attribs.dimensions = Some((width, height));
self
}
pub fn with_title(mut self, title: String) -> WindowBuilder<'a> {
self.attribs.title = title;
self
}
pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder<'a> {
let MonitorID(monitor) = monitor;
self.attribs.monitor = Some(monitor);
self
}
pub fn with_shared_lists(mut self, other: &'a Window) -> WindowBuilder<'a> {
self.attribs.sharing = Some(&other.window);
self
}
pub fn with_gl(mut self, request: GlRequest) -> WindowBuilder<'a> {
self.attribs.gl_version = request;
self
}
pub fn with_gl_profile(mut self, profile: GlProfile) -> WindowBuilder<'a> {
self.attribs.gl_profile = Some(profile);
self
}
pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder<'a> {
self.attribs.gl_debug = flag;
self
}
pub fn with_gl_robustness(mut self, robustness: Robustness) -> WindowBuilder<'a> {
self.attribs.gl_robustness = robustness;
self
}
pub fn with_vsync(mut self) -> WindowBuilder<'a> {
self.attribs.vsync = true;
self
}
pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> {
self.attribs.visible = visible;
self
}
pub fn with_multisampling(mut self, samples: u16) -> WindowBuilder<'a> {
assert!(samples.is_power_of_two());
self.attribs.multisampling = Some(samples);
self
}
pub fn with_depth_buffer(mut self, bits: u8) -> WindowBuilder<'a> {
self.attribs.depth_bits = Some(bits);
self
}
pub fn with_stencil_buffer(mut self, bits: u8) -> WindowBuilder<'a> {
self.attribs.stencil_bits = Some(bits);
self
}
pub fn with_pixel_format(mut self, color_bits: u8, alpha_bits: u8) -> WindowBuilder<'a> {
self.attribs.color_bits = Some(color_bits);
self.attribs.alpha_bits = Some(alpha_bits);
self
}
pub fn with_stereoscopy(mut self) -> WindowBuilder<'a> {
self.attribs.stereoscopy = true;
self
}
pub fn with_srgb(mut self, srgb_enabled: Option<bool>) -> WindowBuilder<'a> {
self.attribs.srgb = srgb_enabled;
self
}
pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder<'a> {
self.attribs.transparent = transparent;
self
}
pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder<'a> {
self.attribs.decorations = decorations;
self
}
pub fn build(mut self) -> Result<Window, CreationError> {
if self.attribs.dimensions.is_none() && self.attribs.monitor.is_some() {
self.attribs.dimensions = Some(self.attribs.monitor.as_ref().unwrap().get_dimensions())
}
if self.attribs.dimensions.is_none() {
self.attribs.dimensions = Some((1024, 768));
}
platform::Window::new(self.attribs).map(|w| Window { window: w })
}
pub fn build_strict(mut self) -> Result<Window, CreationError> {
self.attribs.strict = true;
self.build()
}
}
pub struct Window {
window: platform::Window,
}
impl Default for Window {
fn default() -> Window {
Window::new().unwrap()
}
}
impl Window {
#[inline]
pub fn new() -> Result<Window, CreationError> {
let builder = WindowBuilder::new();
builder.build()
}
#[inline]
pub fn set_title(&self, title: &str) {
self.window.set_title(title)
}
#[inline]
pub fn show(&self) {
self.window.show()
}
#[inline]
pub fn hide(&self) {
self.window.hide()
}
#[inline]
pub fn get_position(&self) -> Option<(i32, i32)> {
self.window.get_position()
}
#[inline]
pub fn set_position(&self, x: i32, y: i32) {
self.window.set_position(x, y)
}
#[inline]
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
self.window.get_inner_size()
}
#[inline]
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
self.window.get_outer_size()
}
#[inline]
pub fn set_inner_size(&self, x: u32, y: u32) {
self.window.set_inner_size(x, y)
}
#[inline]
pub fn poll_events(&self) -> PollEventsIterator {
PollEventsIterator(self.window.poll_events())
}
#[inline]
pub fn wait_events(&self) -> WaitEventsIterator {
WaitEventsIterator(self.window.wait_events())
}
#[inline]
pub unsafe fn make_current(&self) -> Result<(), ContextError> {
self.window.make_current()
}
#[inline]
pub fn is_current(&self) -> bool {
self.window.is_current()
}
#[inline]
pub fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
self.window.get_proc_address(addr) as *const libc::c_void
}
#[inline]
pub fn swap_buffers(&self) -> Result<(), ContextError> {
self.window.swap_buffers()
}
#[inline]
pub unsafe fn platform_display(&self) -> *mut libc::c_void {
self.window.platform_display()
}
#[inline]
pub unsafe fn platform_window(&self) -> *mut libc::c_void {
self.window.platform_window()
}
pub fn get_api(&self) -> Api {
self.window.get_api()
}
pub fn get_pixel_format(&self) -> PixelFormat {
self.window.get_pixel_format()
}
#[inline]
pub fn create_window_proxy(&self) -> WindowProxy {
WindowProxy {
proxy: self.window.create_window_proxy()
}
}
pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
self.window.set_window_resize_callback(callback);
}
pub fn set_cursor(&self, cursor: MouseCursor) {
self.window.set_cursor(cursor);
}
pub fn hidpi_factor(&self) -> f32 {
self.window.hidpi_factor()
}
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
self.window.set_cursor_position(x, y)
}
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
self.window.set_cursor_state(state)
}
}
impl gl_common::GlFunctionsSource for Window {
fn get_proc_addr(&self, addr: &str) -> *const libc::c_void {
self.get_proc_address(addr)
}
}
impl GlContext for Window {
unsafe fn make_current(&self) -> Result<(), ContextError> {
self.make_current()
}
fn is_current(&self) -> bool {
self.is_current()
}
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
self.get_proc_address(addr)
}
fn swap_buffers(&self) -> Result<(), ContextError> {
self.swap_buffers()
}
fn get_api(&self) -> Api {
self.get_api()
}
fn get_pixel_format(&self) -> PixelFormat {
self.get_pixel_format()
}
}
#[derive(Clone)]
pub struct WindowProxy {
proxy: platform::WindowProxy,
}
impl WindowProxy {
#[inline]
pub fn wakeup_event_loop(&self) {
self.proxy.wakeup_event_loop();
}
}
pub struct PollEventsIterator<'a>(platform::PollEventsIterator<'a>);
impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct WaitEventsIterator<'a>(platform::WaitEventsIterator<'a>);
impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct AvailableMonitorsIter {
data: VecDequeIter<platform::MonitorID>,
}
impl Iterator for AvailableMonitorsIter {
type Item = MonitorID;
fn next(&mut self) -> Option<MonitorID> {
self.data.next().map(|id| MonitorID(id))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.data.size_hint()
}
}
pub fn get_available_monitors() -> AvailableMonitorsIter {
let data = platform::get_available_monitors();
AvailableMonitorsIter{ data: data.into_iter() }
}
pub fn get_primary_monitor() -> MonitorID {
MonitorID(platform::get_primary_monitor())
}
pub struct MonitorID(platform::MonitorID);
impl MonitorID {
pub fn get_name(&self) -> Option<String> {
let &MonitorID(ref id) = self;
id.get_name()
}
pub fn get_native_identifier(&self) -> NativeMonitorId {
let &MonitorID(ref id) = self;
id.get_native_identifier()
}
pub fn get_dimensions(&self) -> (u32, u32) {
let &MonitorID(ref id) = self;
id.get_dimensions()
}
}