Merge pull request 'multi window support' from rendering into main
This commit is contained in:
commit
ce4702ec5f
8 changed files with 150 additions and 135 deletions
64
.vscode/launch.json
vendored
Normal file
64
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug executable 'zenyx'",
|
||||||
|
"cargo": {
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"--bin=zenyx",
|
||||||
|
"--package=zenyx"
|
||||||
|
],
|
||||||
|
"filter": {
|
||||||
|
"name": "zenyx",
|
||||||
|
"kind": "bin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug unit tests in executable 'zenyx'",
|
||||||
|
"cargo": {
|
||||||
|
"args": [
|
||||||
|
"test",
|
||||||
|
"--no-run",
|
||||||
|
"--bin=zenyx",
|
||||||
|
"--package=zenyx"
|
||||||
|
],
|
||||||
|
"filter": {
|
||||||
|
"name": "zenyx",
|
||||||
|
"kind": "bin"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug unit tests in library 'zen_core'",
|
||||||
|
"cargo": {
|
||||||
|
"args": [
|
||||||
|
"test",
|
||||||
|
"--no-run",
|
||||||
|
"--lib",
|
||||||
|
"--package=zen_core"
|
||||||
|
],
|
||||||
|
"filter": {
|
||||||
|
"name": "zen_core",
|
||||||
|
"kind": "lib"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
17
Cargo.lock
generated
17
Cargo.lock
generated
|
@ -2200,14 +2200,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0"
|
checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
|
"tracing-attributes",
|
||||||
"tracing-core",
|
"tracing-core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tracing-attributes"
|
||||||
|
version = "0.1.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tracing-core"
|
name = "tracing-core"
|
||||||
version = "0.1.33"
|
version = "0.1.33"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
|
checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ttf-parser"
|
name = "ttf-parser"
|
||||||
|
@ -3109,13 +3124,13 @@ dependencies = [
|
||||||
"dirs-next",
|
"dirs-next",
|
||||||
"futures",
|
"futures",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"log",
|
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"regex",
|
"regex",
|
||||||
"rustyline",
|
"rustyline",
|
||||||
"thiserror 2.0.12",
|
"thiserror 2.0.12",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tracing",
|
||||||
"wgpu",
|
"wgpu",
|
||||||
"winit",
|
"winit",
|
||||||
]
|
]
|
||||||
|
|
|
@ -12,18 +12,18 @@ crashreport = "1.0.1"
|
||||||
dirs-next = "2.0.0"
|
dirs-next = "2.0.0"
|
||||||
|
|
||||||
lazy_static.workspace = true
|
lazy_static.workspace = true
|
||||||
log = "0.4.22"
|
once_cell = "1.21.1"
|
||||||
once_cell = "1.20.2"
|
|
||||||
parking_lot.workspace = true
|
parking_lot.workspace = true
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
rustyline = { version = "15.0.0", features = ["derive", "rustyline-derive"] }
|
rustyline = { version = "15.0.0", features = ["derive", "rustyline-derive"] }
|
||||||
thiserror = "2.0.11"
|
thiserror = "2.0.11"
|
||||||
tokio = { version = "1.42.0", features = ["macros", "parking_lot","rt-multi-thread"] }
|
tokio = { version = "1.44.1", features = ["macros", "parking_lot","rt-multi-thread"] }
|
||||||
wgpu = "24.0.1"
|
wgpu = "24.0.3"
|
||||||
winit = "0.30.8"
|
winit = "0.30.9"
|
||||||
bytemuck = "1.21.0"
|
bytemuck = "1.21.0"
|
||||||
futures = "0.3.31"
|
futures = "0.3.31"
|
||||||
cgmath = "0.18.0"
|
cgmath = "0.18.0"
|
||||||
|
tracing = "0.1.41"
|
||||||
|
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
|
|
@ -1,84 +0,0 @@
|
||||||
use std::fs::OpenOptions;
|
|
||||||
use std::io::{self, Write};
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use colored::Colorize;
|
|
||||||
use log::{Level, Log, Metadata, Record};
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
|
|
||||||
pub static LOGGER: Lazy<DynamicLogger> = Lazy::new(DynamicLogger::new);
|
|
||||||
|
|
||||||
// A logger that dynamically switches between file and stdout
|
|
||||||
pub struct DynamicLogger {
|
|
||||||
pub writer: Arc<Mutex<Box<dyn Write + Send>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for DynamicLogger {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DynamicLogger {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
writer: Arc::new(Mutex::new(Box::new(io::stdout()))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_to_file(&self, file_path: &str) {
|
|
||||||
let file = OpenOptions::new()
|
|
||||||
.create(true)
|
|
||||||
.append(true)
|
|
||||||
.open(file_path)
|
|
||||||
.expect("Failed to open log file");
|
|
||||||
|
|
||||||
*self.writer.lock() = Box::new(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_to_stdout(&self) {
|
|
||||||
*self.writer.lock() = Box::new(io::stdout());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn colorize_level(level: Level) -> colored::ColoredString {
|
|
||||||
match level {
|
|
||||||
Level::Error => "ERROR".red(),
|
|
||||||
Level::Warn => "WARN".yellow(),
|
|
||||||
Level::Info => "INFO".green(),
|
|
||||||
Level::Debug => "DEBUG".blue(),
|
|
||||||
Level::Trace => "TRACE".cyan(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Log for DynamicLogger {
|
|
||||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
|
||||||
let target = metadata.target();
|
|
||||||
let is_relevant_target = target.starts_with("wgpu")
|
|
||||||
|| target.starts_with("winit")
|
|
||||||
|| target.starts_with(env!("CARGO_PKG_NAME")); // Current crate name
|
|
||||||
|
|
||||||
is_relevant_target && metadata.level() <= Level::Debug
|
|
||||||
}
|
|
||||||
|
|
||||||
fn log(&self, record: &Record) {
|
|
||||||
if self.enabled(record.metadata()) {
|
|
||||||
let mut writer = self.writer.lock();
|
|
||||||
let level = Self::colorize_level(record.level()); // Apply coloring
|
|
||||||
writeln!(
|
|
||||||
writer,
|
|
||||||
"{} [{}] - {}",
|
|
||||||
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
|
|
||||||
level,
|
|
||||||
record.args()
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&self) {
|
|
||||||
let mut writer = self.writer.lock();
|
|
||||||
writer.flush().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,4 @@
|
||||||
pub mod ecs;
|
pub mod ecs;
|
||||||
pub mod logger;
|
|
||||||
pub mod panic;
|
pub mod panic;
|
||||||
pub mod repl;
|
pub mod repl;
|
||||||
pub mod splash;
|
pub mod splash;
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ctx::WgpuCtx;
|
use ctx::WgpuCtx;
|
||||||
use log::{debug, trace};
|
use tracing::{debug, error, info, trace, warn};
|
||||||
use winit::application::ApplicationHandler;
|
use winit::application::ApplicationHandler;
|
||||||
use winit::event::WindowEvent;
|
use winit::event::WindowEvent;
|
||||||
use winit::event_loop::ControlFlow;
|
use winit::event_loop::ControlFlow;
|
||||||
|
@ -9,76 +10,97 @@ use winit::event_loop::{ActiveEventLoop, EventLoop};
|
||||||
use winit::window::{Window, WindowId};
|
use winit::window::{Window, WindowId};
|
||||||
pub mod ctx;
|
pub mod ctx;
|
||||||
|
|
||||||
|
struct WindowContext<'window> {
|
||||||
|
window: Arc<Window>,
|
||||||
|
ctx: WgpuCtx<'window>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct App<'window> {
|
pub struct App<'window> {
|
||||||
window: Option<Arc<Window>>,
|
windows: HashMap<WindowId, WindowContext<'window>>,
|
||||||
ctx: Option<WgpuCtx<'window>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ApplicationHandler for App<'_> {
|
impl ApplicationHandler for App<'_> {
|
||||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||||
if self.window.is_none() {
|
if self.windows.is_empty() {
|
||||||
let win_attr = Window::default_attributes().with_title("Zenyx");
|
let win_attr = Window::default_attributes().with_title("Zenyx");
|
||||||
let window = Arc::new(
|
let window = Arc::new(
|
||||||
event_loop
|
event_loop
|
||||||
.create_window(win_attr)
|
.create_window(win_attr)
|
||||||
.expect("create window err."),
|
.expect("create window err."),
|
||||||
);
|
);
|
||||||
self.window = Some(window.clone());
|
let window_id = window.id();
|
||||||
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
|
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
|
||||||
self.ctx = Some(wgpu_ctx)
|
self.windows.insert(window_id, WindowContext { window, ctx: wgpu_ctx });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn window_event(
|
fn window_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
event_loop: &ActiveEventLoop,
|
event_loop: &ActiveEventLoop,
|
||||||
_window_id: WindowId,
|
window_id: WindowId,
|
||||||
event: WindowEvent,
|
event: WindowEvent,
|
||||||
) {
|
) {
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested => {
|
WindowEvent::CloseRequested => {
|
||||||
event_loop.exit();
|
if let Some(window_context) = self.windows.remove(&window_id) {
|
||||||
debug!("Window closed, exiting");
|
drop(window_context);
|
||||||
std::process::exit(0)
|
println!("Window: {:?} closed, exiting", window_id);
|
||||||
|
}
|
||||||
|
if self.windows.is_empty() {
|
||||||
|
event_loop.exit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
WindowEvent::KeyboardInput { device_id, event, is_synthetic } => {
|
WindowEvent::KeyboardInput {
|
||||||
match event.physical_key {
|
device_id,
|
||||||
winit::keyboard::PhysicalKey::Code(code) => {
|
event,
|
||||||
if event.state.is_pressed() == false {
|
is_synthetic,
|
||||||
return;
|
} => match event.physical_key {
|
||||||
}
|
winit::keyboard::PhysicalKey::Code(code) => {
|
||||||
if code == winit::keyboard::KeyCode::Escape {
|
if event.state.is_pressed() == false {
|
||||||
// event_loop.exit();
|
return;
|
||||||
debug!("Window closed, exiting");
|
}
|
||||||
if let Some(ctx) = &mut self.ctx {
|
match code {
|
||||||
match ctx.bg_color() {
|
winit::keyboard::KeyCode::Space => {
|
||||||
wgpu::Color::WHITE => ctx.change_bg_color(wgpu::Color::BLACK),
|
debug!("Space key pressed");
|
||||||
wgpu::Color::BLACK => ctx.change_bg_color(wgpu::Color::WHITE),
|
if let Some(window_context) = self.windows.values_mut().next() {
|
||||||
_ => ctx.change_bg_color(wgpu::Color::WHITE),
|
match window_context.ctx.bg_color() {
|
||||||
|
wgpu::Color::WHITE => window_context.ctx.change_bg_color(wgpu::Color::BLACK),
|
||||||
|
wgpu::Color::BLACK => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
|
||||||
|
_ => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// std::process::exit(0)
|
|
||||||
}
|
}
|
||||||
|
winit::keyboard::KeyCode::Escape => {
|
||||||
|
debug!("Escape key pressed, spawning new window");
|
||||||
|
let win_attr = Window::default_attributes()
|
||||||
|
.with_title(format!("Zenyx - New Window {}", self.windows.len()));
|
||||||
|
let new_window = Arc::new(
|
||||||
|
event_loop
|
||||||
|
.create_window(win_attr)
|
||||||
|
.expect("create window err."),
|
||||||
|
);
|
||||||
|
let window_id = new_window.id();
|
||||||
|
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
|
||||||
|
self.windows.insert(window_id, WindowContext { window: new_window, ctx: wgpu_ctx });
|
||||||
|
}
|
||||||
|
_ => info!("Unimplemented keycode: {:?}", code),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
},
|
||||||
}
|
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
if let Some(ctx) = &mut self.ctx {
|
if let Some(window_context) = self.windows.get_mut(&window_id) {
|
||||||
ctx.draw();
|
window_context.ctx.draw();
|
||||||
}
|
window_context.window.request_redraw();
|
||||||
if let Some(window) = &self.window {
|
|
||||||
window.request_redraw();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WindowEvent::Resized(size) => {
|
WindowEvent::Resized(size) => {
|
||||||
if let (Some(wgpu_ctx), Some(window)) = (&mut self.ctx, &self.window) {
|
if let Some(window_context) = self.windows.get_mut(&window_id) {
|
||||||
wgpu_ctx.resize(size.into());
|
window_context.ctx.resize(size.into());
|
||||||
window.request_redraw();
|
window_context.window.request_redraw();
|
||||||
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
|
let size_str: String =
|
||||||
//self.window.as_ref().unwrap().set_title(&format!("you reszed the window to
|
size.height.to_string() + "x" + &size.width.to_string();
|
||||||
// {size_str}"));
|
|
||||||
debug!("Window resized to {:?}", size_str);
|
debug!("Window resized to {:?}", size_str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ use std::{
|
||||||
|
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use log::debug;
|
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rustyline::{
|
use rustyline::{
|
||||||
|
@ -13,9 +12,9 @@ use rustyline::{
|
||||||
Hinter, KeyEvent, RepeatCount, Validator, completion::Completer, error::ReadlineError,
|
Hinter, KeyEvent, RepeatCount, Validator, completion::Completer, error::ReadlineError,
|
||||||
highlight::Highlighter, hint::HistoryHinter, history::DefaultHistory,
|
highlight::Highlighter, hint::HistoryHinter, history::DefaultHistory,
|
||||||
};
|
};
|
||||||
|
use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
use super::handler::COMMAND_MANAGER;
|
use super::handler::COMMAND_MANAGER;
|
||||||
use crate::core::logger::LOGGER;
|
|
||||||
|
|
||||||
struct CommandCompleter;
|
struct CommandCompleter;
|
||||||
impl CommandCompleter {
|
impl CommandCompleter {
|
||||||
|
@ -89,9 +88,9 @@ impl ConditionalEventHandler for BacktickEventHandler {
|
||||||
if *state { "ON".green() } else { "OFF".red() }
|
if *state { "ON".green() } else { "OFF".red() }
|
||||||
);
|
);
|
||||||
if *state {
|
if *state {
|
||||||
LOGGER.write_to_stdout();
|
// LOGGER.write_to_stdout();
|
||||||
} else {
|
} else {
|
||||||
LOGGER.write_to_file("z.log");
|
// LOGGER.write_to_file("z.log");
|
||||||
}
|
}
|
||||||
*state = !*state;
|
*state = !*state;
|
||||||
Some(Cmd::Noop)
|
Some(Cmd::Noop)
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use core::{panic::set_panic_hook, repl::setup, splash, workspace};
|
use core::{panic::set_panic_hook, repl::setup, splash, workspace};
|
||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use log::info;
|
|
||||||
use tokio::runtime;
|
use tokio::runtime;
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use tracing::{debug, error, info, warn};
|
||||||
use winit::event_loop::EventLoop;
|
use winit::event_loop::EventLoop;
|
||||||
|
|
||||||
pub mod core;
|
pub mod core;
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue