Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type_complexity = "allow"
too_many_arguments = "allow"

[workspace.dependencies]
bevy = { git = "https://github.com/bevyengine/bevy", branch = "main", features = ["file_watcher", "shader_format_wesl"] }
bevy = { git = "https://github.com/bevyengine/bevy", branch = "main", features = ["file_watcher", "shader_format_wesl", "free_camera", "pan_camera"] }
bevy_naga_reflect = { git = "https://github.com/tychedelia/bevy_naga_reflect" }
naga = { version = "28", features = ["wgsl-in"] }
wesl = { version = "0.3", default-features = false }
Expand Down Expand Up @@ -148,6 +148,10 @@ path = "examples/shapes.rs"
name = "blend_modes"
path = "examples/blend_modes.rs"

[[example]]
name = "camera_controllers"
path = "examples/camera_controllers.rs"

[profile.wasm-release]
inherits = "release"
opt-level = "z"
Expand Down
27 changes: 24 additions & 3 deletions crates/processing_glfw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use bevy::prelude::Entity;
use glfw::{Action, Glfw, GlfwReceiver, PWindow, WindowEvent, WindowMode};
use processing_core::error::Result;
use processing_input::{
input_flush, input_set_char, input_set_cursor_enter, input_set_cursor_leave, input_set_focus,
input_set_key, input_set_mouse_button, input_set_mouse_move, input_set_scroll,
input_cursor_grab_mode, input_cursor_visible, input_flush, input_set_char,
input_set_cursor_enter, input_set_cursor_leave, input_set_focus, input_set_key,
input_set_mouse_button, input_set_mouse_move, input_set_scroll,
};

pub struct GlfwContext {
Expand Down Expand Up @@ -173,10 +174,30 @@ impl GlfwContext {
return false;
}

input_flush().unwrap();
let Ok(_) = input_flush() else {
return false;
};
self.sync_cursor(surface);

true
}

fn sync_cursor(&mut self, surface: Entity) {
use bevy::window::CursorGrabMode;

let grab = input_cursor_grab_mode(surface).unwrap_or(CursorGrabMode::None);
let visible = input_cursor_visible(surface).unwrap_or(true);

let mode = match grab {
CursorGrabMode::Locked | CursorGrabMode::Confined => glfw::CursorMode::Disabled,
CursorGrabMode::None if !visible => glfw::CursorMode::Hidden,
CursorGrabMode::None => glfw::CursorMode::Normal,
};

if self.window.get_cursor_mode() != mode {
self.window.set_cursor_mode(mode);
}
}
}

fn glfw_button_to_bevy(button: glfw::MouseButton) -> Option<MouseButton> {
Expand Down
31 changes: 30 additions & 1 deletion crates/processing_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,38 @@ pub fn input_set_focus(surface: Entity, focused: bool) -> error::Result<()> {
})
}

pub fn input_cursor_grab_mode(surface: Entity) -> error::Result<bevy::window::CursorGrabMode> {
app_mut(|app| {
let cursor = app
.world()
.get::<bevy::window::CursorOptions>(surface)
.map(|c| c.grab_mode)
.unwrap_or(bevy::window::CursorGrabMode::None);
Ok(cursor)
})
}

pub fn input_cursor_visible(surface: Entity) -> error::Result<bool> {
app_mut(|app| {
let visible = app
.world()
.get::<bevy::window::CursorOptions>(surface)
.map(|c| c.visible)
.unwrap_or(true);
Ok(visible)
})
}

/// Flushes the input state by running the relevant schedules. This is required to ensure that
/// Bevy's bookkeeping of input state is up to date after manually sending input events.
/// It should be called after sending any input events and before querying input state
/// to ensure that the state reflects the events that were sent.
pub fn input_flush() -> error::Result<()> {
app_mut(|app| {
app.world_mut().run_schedule(PreUpdate);
let world = app.world_mut();
world.run_schedule(First);
world.run_schedule(PreUpdate);
world.run_schedule(RunFixedMainLoop);
Ok(())
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/processing_pyo3/assets
1 change: 0 additions & 1 deletion crates/processing_pyo3/examples/assets

This file was deleted.

46 changes: 46 additions & 0 deletions crates/processing_pyo3/examples/camera_controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from mewnala import *

angle = 0.0
mode = 0

def setup():
size(800, 600)
mode_3d()
orbit_camera()

dir_light = create_directional_light((1.0, 0.98, 0.95), 1500.0)
dir_light.position(300.0, 400.0, 300.0)
dir_light.look_at(0.0, 0.0, 0.0)

def draw():
global angle, mode

if key_just_pressed(KEY_1):
mode_3d()
orbit_camera()
mode = 0
if key_just_pressed(KEY_2):
mode_3d()
free_camera()
mode = 1
if key_just_pressed(KEY_3):
mode_2d()
pan_camera()
mode = 2

background(13, 13, 18)
if mode < 2:
fill(255, 217, 145)
roughness(0.3)
metallic(0.8)
push_matrix()
rotate(angle)
box(100.0, 100.0, 100.0)
pop_matrix()
else:
fill(204, 77, 51)
rect(300.0, 200.0, 200.0, 200.0)

angle += 0.02

run()
46 changes: 46 additions & 0 deletions crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,52 @@ impl Graphics {
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn orbit_camera(&self) -> PyResult<()> {
graphics_orbit_camera(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn free_camera(&self) -> PyResult<()> {
graphics_free_camera(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn pan_camera(&self) -> PyResult<()> {
graphics_pan_camera(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn disable_camera(&self) -> PyResult<()> {
graphics_disable_camera_controller(self.entity)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn camera_distance(&self, distance: f32) -> PyResult<()> {
camera_set_distance(self.entity, distance)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

#[pyo3(signature = (*args))]
pub fn camera_center(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
let v = extract_vec3(args)?;
camera_set_center(self.entity, v).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn camera_min_distance(&self, min: f32) -> PyResult<()> {
camera_set_min_distance(self.entity, min)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn camera_max_distance(&self, max: f32) -> PyResult<()> {
camera_set_max_distance(self.entity, max)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn camera_speed(&self, speed: f32) -> PyResult<()> {
camera_set_speed(self.entity, speed).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn camera_reset(&self) -> PyResult<()> {
camera_reset(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn light_directional(
&self,
color: crate::color::ColorLike,
Expand Down
66 changes: 66 additions & 0 deletions crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,12 @@ mod mewnala {
graphics!(module).mode_3d()
}

#[pyfunction]
#[pyo3(pass_module)]
fn mode_2d(module: &Bound<'_, PyModule>) -> PyResult<()> {
graphics!(module).mode_2d()
}

#[pyfunction]
#[pyo3(pass_module, signature = (*args))]
fn camera_position(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {
Expand All @@ -751,6 +757,66 @@ mod mewnala {
graphics!(module).camera_look_at(args)
}

#[pyfunction]
#[pyo3(pass_module)]
fn orbit_camera(module: &Bound<'_, PyModule>) -> PyResult<()> {
graphics!(module).orbit_camera()
}

#[pyfunction]
#[pyo3(pass_module)]
fn free_camera(module: &Bound<'_, PyModule>) -> PyResult<()> {
graphics!(module).free_camera()
}

#[pyfunction]
#[pyo3(pass_module)]
fn pan_camera(module: &Bound<'_, PyModule>) -> PyResult<()> {
graphics!(module).pan_camera()
}

#[pyfunction]
#[pyo3(pass_module)]
fn disable_camera(module: &Bound<'_, PyModule>) -> PyResult<()> {
graphics!(module).disable_camera()
}

#[pyfunction]
#[pyo3(pass_module)]
fn camera_distance(module: &Bound<'_, PyModule>, distance: f32) -> PyResult<()> {
graphics!(module).camera_distance(distance)
}

#[pyfunction]
#[pyo3(pass_module, signature = (*args))]
fn camera_center(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {
graphics!(module).camera_center(args)
}

#[pyfunction]
#[pyo3(pass_module)]
fn camera_min_distance(module: &Bound<'_, PyModule>, min: f32) -> PyResult<()> {
graphics!(module).camera_min_distance(min)
}

#[pyfunction]
#[pyo3(pass_module)]
fn camera_max_distance(module: &Bound<'_, PyModule>, max: f32) -> PyResult<()> {
graphics!(module).camera_max_distance(max)
}

#[pyfunction]
#[pyo3(pass_module)]
fn camera_speed(module: &Bound<'_, PyModule>, speed: f32) -> PyResult<()> {
graphics!(module).camera_speed(speed)
}

#[pyfunction]
#[pyo3(pass_module)]
fn camera_reset(module: &Bound<'_, PyModule>) -> PyResult<()> {
graphics!(module).camera_reset()
}

#[pyfunction]
#[pyo3(pass_module)]
fn push_matrix(module: &Bound<'_, PyModule>) -> PyResult<()> {
Expand Down
Loading
Loading