use crate::core::math::{Matrix, Ray, Vector2};
use crate::core::{RaylibHandle, RaylibThread};
use crate::ffi;
use std::ffi::{CStr, CString, IntoStringError, NulError};
#[derive(Clone, Debug)]
pub struct MonitorInfo {
width: i32,
height: i32,
physical_width: i32,
physical_height: i32,
name: String,
}
pub fn get_monitor_count() -> i32 {
unsafe { ffi::GetMonitorCount() }
}
pub fn get_monitor_info(index: i32) -> Result<MonitorInfo, IntoStringError> {
let len = get_monitor_count();
debug_assert!(index < len && index >= 0, "monitor index out of range");
let (width, height, physical_width, physical_height) = unsafe {
(
ffi::GetMonitorWidth(index),
ffi::GetMonitorHeight(index),
ffi::GetMonitorPhysicalWidth(index),
ffi::GetMonitorPhysicalHeight(index),
)
};
let name = unsafe {
let c = CString::from_raw(ffi::GetMonitorName(index) as *mut i8);
c.into_string()?
};
Ok(MonitorInfo {
width,
height,
physical_height,
physical_width,
name,
})
}
pub fn get_camera_matrix(camera: impl Into<ffi::Camera>) -> Matrix {
unsafe { ffi::GetCameraMatrix(camera.into()).into() }
}
impl RaylibHandle {
pub fn get_clipboard_text(&self) -> Result<String, std::str::Utf8Error> {
unsafe {
let c = ffi::GetClipboardText();
let c = CStr::from_ptr(c as *mut i8);
c.to_str().map(|s| s.to_owned())
}
}
pub fn set_clipboard_text(&mut self, text: &str) -> Result<(), NulError> {
let s = CString::new(text)?;
unsafe {
ffi::SetClipboardText(s.as_ptr());
}
Ok(())
}
}
impl RaylibHandle {
pub fn get_mouse_ray(
&self,
mouse_position: impl Into<ffi::Vector2>,
camera: impl Into<ffi::Camera>,
) -> Ray {
unsafe { ffi::GetMouseRay(mouse_position.into(), camera.into()).into() }
}
pub fn get_world_to_screen(
&self,
position: impl Into<ffi::Vector3>,
camera: impl Into<ffi::Camera>,
) -> Vector2 {
unsafe { ffi::GetWorldToScreen(position.into(), camera.into()).into() }
}
}
impl RaylibHandle {
pub fn set_target_fps(&mut self, fps: u32) {
unsafe {
ffi::SetTargetFPS(fps as i32);
}
}
pub fn get_fps(&self) -> u32 {
unsafe { ffi::GetFPS() as u32 }
}
pub fn get_frame_time(&self) -> f32 {
unsafe { ffi::GetFrameTime() }
}
pub fn get_time(&self) -> f64 {
unsafe { ffi::GetTime() }
}
}
impl RaylibHandle {
#[inline]
pub fn window_should_close(&self) -> bool {
unsafe { ffi::WindowShouldClose() }
}
#[inline]
pub fn is_window_ready(&self) -> bool {
unsafe { ffi::IsWindowReady() }
}
#[inline]
pub fn is_window_minimized(&self) -> bool {
unsafe { ffi::IsWindowMinimized() }
}
#[inline]
pub fn is_window_resized(&self) -> bool {
unsafe { ffi::IsWindowResized() }
}
#[inline]
pub fn is_window_hidden(&self) -> bool {
unsafe { ffi::IsWindowResized() }
}
#[inline]
pub fn toggle_fullscreen(&mut self) {
unsafe {
ffi::ToggleFullscreen();
}
}
#[inline]
pub fn unhide_window(&mut self) {
unsafe {
ffi::UnhideWindow();
}
}
#[inline]
pub fn hide_window(&mut self) {
unsafe {
ffi::HideWindow();
}
}
#[inline]
pub fn set_window_title(&self, _: &RaylibThread, title: &str) {
let c_title = CString::new(title).unwrap();
unsafe {
ffi::SetWindowTitle(c_title.as_ptr());
}
}
#[inline]
pub fn set_window_position(&mut self, x: i32, y: i32) {
unsafe {
ffi::SetWindowPosition(x, y);
}
}
#[inline]
pub fn set_window_monitor(&mut self, monitor: i32) {
unsafe {
ffi::SetWindowMonitor(monitor);
}
}
#[inline]
pub fn set_window_min_size(&mut self, width: i32, height: i32) {
unsafe {
ffi::SetWindowMinSize(width, height);
}
}
#[inline]
pub fn set_window_size(&mut self, width: i32, height: i32) {
unsafe {
ffi::SetWindowSize(width, height);
}
}
#[inline]
pub fn get_screen_width(&self) -> i32 {
unsafe { ffi::GetScreenWidth() }
}
#[inline]
pub fn get_screen_height(&self) -> i32 {
unsafe { ffi::GetScreenHeight() }
}
}
impl RaylibHandle {
#[inline]
pub fn show_cursor(&mut self) {
unsafe {
ffi::ShowCursor();
}
}
#[inline]
pub fn hide_cursor(&mut self) {
unsafe {
ffi::HideCursor();
}
}
#[inline]
pub fn is_cursor_hidden(&self) -> bool {
unsafe { ffi::IsCursorHidden() }
}
#[inline]
pub fn enable_cursor(&mut self) {
unsafe {
ffi::EnableCursor();
}
}
#[inline]
pub fn disable_cursor(&mut self) {
unsafe {
ffi::DisableCursor();
}
}
#[inline]
pub unsafe fn get_window_handle(&mut self) -> *mut ::std::os::raw::c_void {
ffi::GetWindowHandle()
}
}