use std::collections::vec_deque::IntoIter as VecDequeIter;
use std::default::Default;
use Api;
use BuilderAttribs;
use CreationError;
use Event;
use GlRequest;
use MouseCursor;
use gl_common;
use libc;
use winimpl;
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
}
#[deprecated = "Use with_gl instead"]
pub fn with_gl_version(mut self, version: (u32, u32)) -> WindowBuilder<'a> {
self.attribs.gl_version = GlRequest::Specific(::Api::OpenGl, (version.0 as u8, version.1 as u8));
self
}
pub fn with_gl(mut self, request: GlRequest) -> WindowBuilder<'a> {
self.attribs.gl_version = request;
self
}
pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder<'a> {
self.attribs.gl_debug = flag;
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> {
use std::num::UnsignedInt;
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 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));
}
winimpl::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: winimpl::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 is_closed(&self) -> bool {
self.window.is_closed()
}
#[inline]
#[deprecated = "Use is_closed instead"]
pub fn should_close(&self) -> bool {
self.is_closed()
}
#[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) {
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) {
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()
}
#[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()
}
}
impl gl_common::GlFunctionsSource for Window {
fn get_proc_addr(&self, addr: &str) -> *const libc::c_void {
self.get_proc_address(addr)
}
}
#[derive(Clone)]
pub struct WindowProxy {
proxy: winimpl::WindowProxy,
}
impl WindowProxy {
#[inline]
pub fn wakeup_event_loop(&self) {
self.proxy.wakeup_event_loop();
}
}
pub struct PollEventsIterator<'a>(winimpl::PollEventsIterator<'a>);
impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
self.0.next()
}
}
pub struct WaitEventsIterator<'a>(winimpl::WaitEventsIterator<'a>);
impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
self.0.next()
}
}
pub struct AvailableMonitorsIter {
data: VecDequeIter<winimpl::MonitorID>,
}
impl Iterator for AvailableMonitorsIter {
type Item = MonitorID;
fn next(&mut self) -> Option<MonitorID> {
self.data.next().map(|id| MonitorID(id))
}
}
pub fn get_available_monitors() -> AvailableMonitorsIter {
let data = winimpl::get_available_monitors();
AvailableMonitorsIter{ data: data.into_iter() }
}
pub fn get_primary_monitor() -> MonitorID {
MonitorID(winimpl::get_primary_monitor())
}
pub struct MonitorID(winimpl::MonitorID);
impl MonitorID {
pub fn get_name(&self) -> Option<String> {
let &MonitorID(ref id) = self;
id.get_name()
}
pub fn get_dimensions(&self) -> (u32, u32) {
let &MonitorID(ref id) = self;
id.get_dimensions()
}
}