mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2025-01-26 02:36:32 +11:00
Started with implementing get_size()
Currently Mac is implemented with stub versions for Linux and Windows
This commit is contained in:
parent
3565e1cb3f
commit
f540aae1b9
5 changed files with 73 additions and 17 deletions
15
src/lib.rs
15
src/lib.rs
|
@ -251,6 +251,21 @@ impl Window {
|
||||||
self.0.set_position(x, y)
|
self.0.set_position(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Returns the current size of the window
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```ignore
|
||||||
|
/// let size = window.get_size();
|
||||||
|
/// println!("width {} height {}", size.0, size.1);
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
#[inline]
|
||||||
|
pub fn get_size(&self) -> (usize, usize) {
|
||||||
|
self.0.get_size()
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Get the current position of the mouse relative to the current window
|
/// Get the current position of the mouse relative to the current window
|
||||||
/// The coordinate system is as 0, 0 as the upper left corner
|
/// The coordinate system is as 0, 0 as the upper left corner
|
||||||
|
|
|
@ -27,9 +27,9 @@
|
||||||
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
|
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
|
||||||
|
|
||||||
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
|
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
|
||||||
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, draw_buffer, width * height * 4, NULL);
|
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, draw_buffer, width * height * 4, NULL);
|
||||||
|
|
||||||
CGImageRef img = CGImageCreate(width, height, 8, 32, width * 4, space, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
|
CGImageRef img = CGImageCreate(width, height, 8, 32, width * 4, space, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
|
||||||
provider, NULL, false, kCGRenderingIntentDefault);
|
provider, NULL, false, kCGRenderingIntentDefault);
|
||||||
|
|
||||||
CGColorSpaceRelease(space);
|
CGColorSpaceRelease(space);
|
||||||
|
@ -95,5 +95,32 @@
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
- (void)viewDidMoveToWindow
|
||||||
|
{
|
||||||
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||||
|
selector:@selector(windowResized:) name:NSWindowDidResizeNotification
|
||||||
|
object:[self window]];
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
- (void)dealloc
|
||||||
|
{
|
||||||
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
- (void)windowResized:(NSNotification *)notification;
|
||||||
|
{
|
||||||
|
NSSize size = [[self window] frame].size;
|
||||||
|
OSXWindow* window = (OSXWindow*)[self window];
|
||||||
|
window->shared_data->width = (int)size.width;
|
||||||
|
window->shared_data->height = (int)size.height;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
|
@ -280,6 +280,10 @@ impl Window {
|
||||||
unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) }
|
unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_size(&self) -> (usize, usize) {
|
||||||
|
(self.shared_data.width as usize, self.shared_data.height as usize)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> {
|
pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> {
|
||||||
let sx = self.shared_data.scroll_x;
|
let sx = self.shared_data.scroll_x;
|
||||||
let sy = self.shared_data.scroll_y;
|
let sy = self.shared_data.scroll_y;
|
||||||
|
|
|
@ -233,6 +233,11 @@ impl Window {
|
||||||
unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) }
|
unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_size(&self) -> (usize, usize) {
|
||||||
|
(0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> {
|
pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> {
|
||||||
let s = self.shared_data.scale as f32;
|
let s = self.shared_data.scale as f32;
|
||||||
let w = self.shared_data.width as f32;
|
let w = self.shared_data.width as f32;
|
||||||
|
|
|
@ -331,7 +331,7 @@ pub struct Window {
|
||||||
#[link(name = "user32")]
|
#[link(name = "user32")]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
extern "system" {
|
extern "system" {
|
||||||
fn TranslateAcceleratorW(hWnd: HWND, accel: *const ACCEL, pmsg: *const MSG) -> INT;
|
fn TranslateAcceleratorW(hWnd: HWND, accel: *const ACCEL, pmsg: *const MSG) -> INT;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
|
@ -465,6 +465,11 @@ impl Window {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_size(&self) -> (usize, usize) {
|
||||||
|
(0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> {
|
pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> {
|
||||||
let s = self.scale_factor as f32;
|
let s = self.scale_factor as f32;
|
||||||
let w = self.width as f32;
|
let w = self.width as f32;
|
||||||
|
@ -743,11 +748,11 @@ impl Window {
|
||||||
let menu_height = user32::GetSystemMetrics(winapi::winuser::SM_CYMENU);
|
let menu_height = user32::GetSystemMetrics(winapi::winuser::SM_CYMENU);
|
||||||
|
|
||||||
user32::GetWindowRect(handle, &mut rect);
|
user32::GetWindowRect(handle, &mut rect);
|
||||||
user32::MoveWindow(handle,
|
user32::MoveWindow(handle,
|
||||||
rect.left,
|
rect.left,
|
||||||
rect.top,
|
rect.top,
|
||||||
rect.right - rect.left,
|
rect.right - rect.left,
|
||||||
(rect.bottom - rect.top) + menu_height,
|
(rect.bottom - rect.top) + menu_height,
|
||||||
1);
|
1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -787,7 +792,7 @@ impl Window {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_virt_key(menu_item: &Menu, key: raw::c_int) -> u32 {
|
fn get_virt_key(menu_item: &Menu, key: raw::c_int) -> u32 {
|
||||||
let mut virt = Self::is_key_virtual_range(key);
|
let mut virt = Self::is_key_virtual_range(key);
|
||||||
|
|
||||||
if (menu_item.modifier & MENU_KEY_ALT) == MENU_KEY_ALT {
|
if (menu_item.modifier & MENU_KEY_ALT) == MENU_KEY_ALT {
|
||||||
virt |= 0x10;
|
virt |= 0x10;
|
||||||
|
@ -806,10 +811,10 @@ impl Window {
|
||||||
|
|
||||||
fn add_accel(accel_table: &mut Vec<ACCEL>, menu_item: &Menu) {
|
fn add_accel(accel_table: &mut Vec<ACCEL>, menu_item: &Menu) {
|
||||||
let vk_accel = Self::map_key_to_vk_accel(menu_item.key);
|
let vk_accel = Self::map_key_to_vk_accel(menu_item.key);
|
||||||
let virt = Self::get_virt_key(menu_item, vk_accel.0);
|
let virt = Self::get_virt_key(menu_item, vk_accel.0);
|
||||||
let accel = winuser::ACCEL {
|
let accel = winuser::ACCEL {
|
||||||
fVirt: virt as BYTE,
|
fVirt: virt as BYTE,
|
||||||
cmd: menu_item.id as WORD,
|
cmd: menu_item.id as WORD,
|
||||||
key: vk_accel.0 as WORD };
|
key: vk_accel.0 as WORD };
|
||||||
|
|
||||||
accel_table.push(accel);
|
accel_table.push(accel);
|
||||||
|
@ -845,8 +850,8 @@ impl Window {
|
||||||
user32::DestroyAcceleratorTable(self.accel_table);
|
user32::DestroyAcceleratorTable(self.accel_table);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.accel_table = user32::CreateAcceleratorTableW(temp_accel_table.as_mut_ptr(),
|
self.accel_table = user32::CreateAcceleratorTableW(temp_accel_table.as_mut_ptr(),
|
||||||
temp_accel_table.len() as i32);
|
temp_accel_table.len() as i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -886,7 +891,7 @@ impl Window {
|
||||||
for m in menu.iter() {
|
for m in menu.iter() {
|
||||||
if let Some(ref sub_menu) = m.sub_menu {
|
if let Some(ref sub_menu) = m.sub_menu {
|
||||||
Self::clone_menu(accel_dest, sub_menu);
|
Self::clone_menu(accel_dest, sub_menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.key != Key::Unknown {
|
if m.key != Key::Unknown {
|
||||||
Self::add_accel(accel_dest, m);
|
Self::add_accel(accel_dest, m);
|
||||||
|
@ -947,7 +952,7 @@ impl Window {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Proper return here
|
// TODO: Proper return here
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue