display.rs (5953B)
1 use crate::bindings; 2 use enum_primitive_derive::Primitive; 3 4 #[derive(Clone)] 5 pub struct XPLMWindow { 6 inner: bindings::XPLMWindowID, 7 } 8 9 impl XPLMWindow { 10 pub fn get_inner(&self) -> bindings::XPLMWindowID { 11 self.inner 12 } 13 } 14 15 impl Drop for XPLMWindow { 16 fn drop(&mut self) { 17 unsafe { 18 bindings::XPLMDestroyWindow(self.inner); 19 } 20 } 21 } 22 23 unsafe impl Send for XPLMWindow {} 24 unsafe impl Sync for XPLMWindow {} 25 26 #[derive(Debug, Copy, Clone, PartialEq, Primitive)] 27 #[repr(i32)] 28 pub enum WindowDecoration { 29 None = bindings::xplm_WindowDecorationNone, 30 RoundRectangle = bindings::xplm_WindowDecorationRoundRectangle, 31 SelfDecorated = bindings::xplm_WindowDecorationSelfDecorated, 32 SelfDecoratedResizeable = bindings::xplm_WindowDecorationSelfDecoratedResizable, 33 } 34 35 #[derive(Debug, Copy, Clone, PartialEq, Primitive)] 36 #[repr(i32)] 37 pub enum WindowLayer { 38 FlightOverlay = bindings::xplm_WindowLayerFlightOverlay, 39 FloatingWindows = bindings::xplm_WindowLayerFloatingWindows, 40 Modal = bindings::xplm_WindowLayerModal, 41 GrowlNotifications = bindings::xplm_WindowLayerGrowlNotifications, 42 } 43 44 pub struct XPLMWindowBuilder { 45 left: i32, 46 top: i32, 47 right: i32, 48 bottom: i32, 49 visible: i32, 50 drawWindowFunc: bindings::XPLMDrawWindow_f, 51 handleMouseClickFunc: bindings::XPLMHandleMouseClick_f, 52 handleKeyFunc: bindings::XPLMHandleKey_f, 53 handleCursorFunc: bindings::XPLMHandleCursor_f, 54 handleMouseWheelFunc: bindings::XPLMHandleMouseWheel_f, 55 refcon: *mut ::std::os::raw::c_void, 56 decorateAsFloatingWindow: WindowDecoration, 57 layer: WindowLayer, 58 handleRightClickFunc: bindings::XPLMHandleMouseClick_f, 59 } 60 61 impl XPLMWindowBuilder { 62 pub fn new() -> Self { 63 XPLMWindowBuilder { 64 left: 0, 65 top: 0, 66 right: 0, 67 bottom: 0, 68 visible: 1, 69 drawWindowFunc: None, 70 handleMouseClickFunc: None, 71 handleKeyFunc: None, 72 handleCursorFunc: None, 73 handleMouseWheelFunc: None, 74 refcon: std::ptr::null_mut(), 75 decorateAsFloatingWindow: WindowDecoration::RoundRectangle, 76 layer: WindowLayer::FloatingWindows, 77 handleRightClickFunc: None, 78 } 79 } 80 pub fn left<'a>(&'a mut self, left: i32) -> &'a mut Self { 81 self.left = left; 82 self 83 } 84 pub fn top<'a>(&'a mut self, top: i32) -> &'a mut Self { 85 self.top = top; 86 self 87 } 88 pub fn right<'a>(&'a mut self, right: i32) -> &'a mut Self { 89 self.right = right; 90 self 91 } 92 pub fn bottom<'a>(&'a mut self, bottom: i32) -> &'a mut Self { 93 self.bottom = bottom; 94 self 95 } 96 pub fn visible<'a>(&'a mut self, visible: i32) -> &'a mut Self { 97 self.visible = visible; 98 self 99 } 100 pub fn drawWindowFunc<'a>( 101 &'a mut self, 102 drawWindowFunc: bindings::XPLMDrawWindow_f, 103 ) -> &'a mut Self { 104 self.drawWindowFunc = drawWindowFunc; 105 self 106 } 107 108 pub fn handleMouseClickFunc<'a>( 109 &'a mut self, 110 handleMouseClickFunc: bindings::XPLMHandleMouseClick_f, 111 ) -> &'a mut Self { 112 self.handleMouseClickFunc = handleMouseClickFunc; 113 self 114 } 115 pub fn handleKeyFunc<'a>( 116 &'a mut self, 117 handleKeyFunc: bindings::XPLMHandleKey_f, 118 ) -> &'a mut Self { 119 self.handleKeyFunc = handleKeyFunc; 120 self 121 } 122 pub fn handleCursorFunc<'a>( 123 &'a mut self, 124 handleCursorFunc: bindings::XPLMHandleCursor_f, 125 ) -> &'a mut Self { 126 self.handleCursorFunc = handleCursorFunc; 127 self 128 } 129 130 pub fn handleMouseWheelFunc<'a>( 131 &'a mut self, 132 handleMouseWheelFunc: bindings::XPLMHandleMouseWheel_f, 133 ) -> &'a mut Self { 134 self.handleMouseWheelFunc = handleMouseWheelFunc; 135 self 136 } 137 138 pub fn refcon<'a>(&'a mut self, refcon: *mut ::std::os::raw::c_void) -> &'a mut Self { 139 self.refcon = refcon; 140 self 141 } 142 143 pub fn decorateAsFloatingWindow<'a>( 144 &'a mut self, 145 decorateAsFloatingWindow: WindowDecoration, 146 ) -> &'a mut Self { 147 self.decorateAsFloatingWindow = decorateAsFloatingWindow; 148 self 149 } 150 151 pub fn layer<'a>(&'a mut self, layer: WindowLayer) -> &'a mut Self { 152 self.layer = layer; 153 self 154 } 155 156 pub fn handleRightClickFunc<'a>( 157 &'a mut self, 158 handleRightClickFunc: bindings::XPLMHandleMouseClick_f, 159 ) -> &'a mut Self { 160 self.handleRightClickFunc = handleRightClickFunc; 161 self 162 } 163 pub fn build(&self) -> XPLMWindow { 164 use num_traits::ToPrimitive; 165 let params = &mut bindings::XPLMCreateWindow_t { 166 structSize: std::mem::size_of::<bindings::XPLMCreateWindow_t>() as i32, 167 left: self.left, 168 top: self.top, 169 right: self.right, 170 bottom: self.bottom, 171 visible: self.visible, 172 drawWindowFunc: self.drawWindowFunc, 173 handleMouseClickFunc: self.handleMouseClickFunc, 174 handleKeyFunc: self.handleKeyFunc, 175 handleCursorFunc: self.handleCursorFunc, 176 handleMouseWheelFunc: self.handleMouseWheelFunc, 177 refcon: self.refcon, 178 decorateAsFloatingWindow: self.decorateAsFloatingWindow.to_i32().unwrap(), 179 layer: self.layer.to_i32().unwrap(), 180 handleRightClickFunc: self.handleRightClickFunc, 181 }; 182 let window: bindings::XPLMWindowID; 183 unsafe { 184 window = bindings::XPLMCreateWindowEx(params); 185 } 186 XPLMWindow { inner: window } 187 } 188 } 189 190 pub fn XPLMGetWindowGeometry(in_window_id: bindings::XPLMWindowID) -> (i32,i32,i32,i32) { 191 let mut l = 0; 192 let mut t = 0; 193 let mut r = 0; 194 let mut b = 0; 195 196 unsafe { 197 bindings::XPLMGetWindowGeometry(in_window_id, &mut l, &mut t, &mut r, &mut b); 198 } 199 200 (l,t,r,b) 201 }