use std::collections::vec_deque::IntoIter as VecDequeIter;
use std::default::Default;
use Api;
use ContextError;
use CreationError;
use CursorState;
use Event;
use GlAttributes;
use GlContext;
use GlProfile;
use GlRequest;
use MouseCursor;
use PixelFormat;
use PixelFormatRequirements;
use Robustness;
use Window;
use WindowAttributes;
use native_monitor::NativeMonitorId;
use libc;
use platform;
pub struct WindowBuilder<'a> {
pub window: WindowAttributes,
pub opengl: GlAttributes<&'a platform::Window>,
pf_reqs: PixelFormatRequirements,
}
impl<'a> WindowBuilder<'a> {
#[inline]
pub fn new() -> WindowBuilder<'a> {
WindowBuilder {
pf_reqs: Default::default(),
window: Default::default(),
opengl: Default::default(),
}
}
#[inline]
pub fn with_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> {
self.window.dimensions = Some((width, height));
self
}
#[inline]
pub fn with_min_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> {
self.window.min_dimensions = Some((width, height));
self
}
#[inline]
pub fn with_max_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> {
self.window.max_dimensions = Some((width, height));
self
}
#[inline]
pub fn with_title(mut self, title: String) -> WindowBuilder<'a> {
self.window.title = title;
self
}
#[inline]
pub fn with_fullscreen(mut self, monitor: MonitorId) -> WindowBuilder<'a> {
let MonitorId(monitor) = monitor;
self.window.monitor = Some(monitor);
self
}
#[inline]
pub fn with_shared_lists(mut self, other: &'a Window) -> WindowBuilder<'a> {
self.opengl.sharing = Some(&other.window);
self
}
#[inline]
pub fn with_gl(mut self, request: GlRequest) -> WindowBuilder<'a> {
self.opengl.version = request;
self
}
#[inline]
pub fn with_gl_profile(mut self, profile: GlProfile) -> WindowBuilder<'a> {
self.opengl.profile = Some(profile);
self
}
#[inline]
pub fn with_gl_debug_flag(mut self, flag: bool) -> WindowBuilder<'a> {
self.opengl.debug = flag;
self
}
#[inline]
pub fn with_gl_robustness(mut self, robustness: Robustness) -> WindowBuilder<'a> {
self.opengl.robustness = robustness;
self
}
#[inline]
pub fn with_vsync(mut self) -> WindowBuilder<'a> {
self.opengl.vsync = true;
self
}
#[inline]
pub fn with_visibility(mut self, visible: bool) -> WindowBuilder<'a> {
self.window.visible = visible;
self
}
#[inline]
pub fn with_multisampling(mut self, samples: u16) -> WindowBuilder<'a> {
assert!(samples.is_power_of_two());
self.pf_reqs.multisampling = Some(samples);
self
}
#[inline]
pub fn with_depth_buffer(mut self, bits: u8) -> WindowBuilder<'a> {
self.pf_reqs.depth_bits = Some(bits);
self
}
#[inline]
pub fn with_stencil_buffer(mut self, bits: u8) -> WindowBuilder<'a> {
self.pf_reqs.stencil_bits = Some(bits);
self
}
#[inline]
pub fn with_pixel_format(mut self, color_bits: u8, alpha_bits: u8) -> WindowBuilder<'a> {
self.pf_reqs.color_bits = Some(color_bits);
self.pf_reqs.alpha_bits = Some(alpha_bits);
self
}
#[inline]
pub fn with_stereoscopy(mut self) -> WindowBuilder<'a> {
self.pf_reqs.stereoscopy = true;
self
}
#[inline]
pub fn with_srgb(mut self, srgb_enabled: Option<bool>) -> WindowBuilder<'a> {
self.pf_reqs.srgb = srgb_enabled.unwrap_or(false);
self
}
#[inline]
pub fn with_transparency(mut self, transparent: bool) -> WindowBuilder<'a> {
self.window.transparent = transparent;
self
}
#[inline]
pub fn with_decorations(mut self, decorations: bool) -> WindowBuilder<'a> {
self.window.decorations = decorations;
self
}
#[inline]
pub fn with_multitouch(mut self) -> WindowBuilder<'a> {
self.window.multitouch = true;
self
}
pub fn build(mut self) -> Result<Window, CreationError> {
if self.window.dimensions.is_none() && self.window.monitor.is_some() {
self.window.dimensions = Some(self.window.monitor.as_ref().unwrap().get_dimensions())
}
if self.window.dimensions.is_none() {
self.window.dimensions = Some((1024, 768));
}
platform::Window::new(&self.window, &self.pf_reqs, &self.opengl)
.map(|w| Window { window: w })
}
#[inline]
pub fn build_strict(self) -> Result<Window, CreationError> {
self.build()
}
}
impl Default for Window {
#[inline]
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_inner_size_points(&self) -> Option<(u32, u32)> {
self.window.get_inner_size()
}
#[inline]
pub fn get_inner_size_pixels(&self) -> Option<(u32, u32)> {
self.window.get_inner_size().map(|(x, y)| {
let hidpi = self.hidpi_factor();
((x as f32 * hidpi) as u32, (y as f32 * hidpi) as u32)
})
}
#[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 () {
self.window.get_proc_address(addr)
}
#[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()
}
#[inline]
pub fn get_api(&self) -> Api {
self.window.get_api()
}
#[inline]
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()
}
}
#[inline]
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);
}
#[inline]
pub fn hidpi_factor(&self) -> f32 {
self.window.hidpi_factor()
}
#[inline]
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
self.window.set_cursor_position(x, y)
}
#[inline]
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
self.window.set_cursor_state(state)
}
}
impl GlContext for Window {
#[inline]
unsafe fn make_current(&self) -> Result<(), ContextError> {
self.make_current()
}
#[inline]
fn is_current(&self) -> bool {
self.is_current()
}
#[inline]
fn get_proc_address(&self, addr: &str) -> *const () {
self.get_proc_address(addr)
}
#[inline]
fn swap_buffers(&self) -> Result<(), ContextError> {
self.swap_buffers()
}
#[inline]
fn get_api(&self) -> Api {
self.get_api()
}
#[inline]
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;
#[inline]
fn next(&mut self) -> Option<Event> {
self.0.next()
}
#[inline]
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;
#[inline]
fn next(&mut self) -> Option<Event> {
self.0.next()
}
#[inline]
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;
#[inline]
fn next(&mut self) -> Option<MonitorId> {
self.data.next().map(|id| MonitorId(id))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.data.size_hint()
}
}
#[inline]
pub fn get_available_monitors() -> AvailableMonitorsIter {
let data = platform::get_available_monitors();
AvailableMonitorsIter{ data: data.into_iter() }
}
#[inline]
pub fn get_primary_monitor() -> MonitorId {
MonitorId(platform::get_primary_monitor())
}
pub struct MonitorId(platform::MonitorId);
impl MonitorId {
#[inline]
pub fn get_name(&self) -> Option<String> {
let &MonitorId(ref id) = self;
id.get_name()
}
#[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId {
let &MonitorId(ref id) = self;
id.get_native_identifier()
}
#[inline]
pub fn get_dimensions(&self) -> (u32, u32) {
let &MonitorId(ref id) = self;
id.get_dimensions()
}
}