trying to eat the spaghetti (make it less)
This commit is contained in:
parent
3dd1a0caee
commit
4ad7d2ebd0
42 changed files with 189 additions and 668 deletions
8
crates/moonhare_window/Cargo.toml
Normal file
8
crates/moonhare_window/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "moonhare_window"
|
||||
edition = "2024"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
winit = "0.30.12"
|
||||
1
crates/moonhare_window/README.md
Normal file
1
crates/moonhare_window/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
# MoonhareWindow
|
||||
2
crates/moonhare_window/src/lib.rs
Normal file
2
crates/moonhare_window/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod window_config;
|
||||
pub mod winit_window;
|
||||
55
crates/moonhare_window/src/window_config.rs
Normal file
55
crates/moonhare_window/src/window_config.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
use std::{any::Any, string};
|
||||
|
||||
/// General Config for [`WinitWindow`](crate::winit::winit_window::WinitWindow)
|
||||
pub struct WindowConfig {
|
||||
pub title: String,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub visble: bool,
|
||||
pub decorations: bool,
|
||||
}
|
||||
|
||||
impl Default for WindowConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
title: "Moonhare Engine".to_owned(),
|
||||
width: 1280,
|
||||
height: 720,
|
||||
visble: default_visibility(),
|
||||
decorations: default_decorations(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Todo: Set Functions should be inside WinitWindow i guess?
|
||||
impl WindowConfig {
|
||||
fn set_window_name(mut self, name: String) {
|
||||
self.title = name;
|
||||
}
|
||||
|
||||
fn set_window_height(mut self, new_height: u32) {
|
||||
self.height = new_height;
|
||||
}
|
||||
|
||||
fn set_window_width(mut self, new_width: u32) {
|
||||
self.width = new_width;
|
||||
}
|
||||
|
||||
fn set_window_visible(mut self, visible: bool) {
|
||||
self.visble = visible;
|
||||
}
|
||||
|
||||
fn set_window_decoration(mut self, decoration: bool) {
|
||||
self.decorations = decoration;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn default_visibility() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_decorations() -> bool {
|
||||
true
|
||||
}
|
||||
54
crates/moonhare_window/src/winit_window.rs
Normal file
54
crates/moonhare_window/src/winit_window.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use std::error::Error;
|
||||
|
||||
use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::{ActiveEventLoop, EventLoop}, window::{Window, WindowAttributes, WindowId}};
|
||||
|
||||
use crate::window_config::WindowConfig;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WinitWindow {
|
||||
pub window: Option<Window>,
|
||||
}
|
||||
|
||||
impl WinitWindow {
|
||||
pub fn create_window(&mut self, event_loop: &ActiveEventLoop, config: WindowConfig) -> WinitWindow {
|
||||
let mut window_attributes = WindowAttributes::default()
|
||||
.with_title(config.title);
|
||||
|
||||
let logical_size = LogicalSize::new(config.width, config.height);
|
||||
window_attributes = window_attributes.with_inner_size(logical_size);
|
||||
window_attributes = window_attributes.with_max_inner_size(logical_size);
|
||||
|
||||
// Set Visible
|
||||
window_attributes = window_attributes.with_visible(config.visble);
|
||||
window_attributes = window_attributes.with_decorations(config.decorations);
|
||||
|
||||
let window = event_loop.create_window(window_attributes).unwrap();
|
||||
|
||||
Self { window: Some(window) }
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for WinitWindow {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
window_id: winit::window::WindowId,
|
||||
event: winit::event::WindowEvent,
|
||||
) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
event_loop.exit();
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue