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
54 changes: 54 additions & 0 deletions crates/processing_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,60 @@ pub extern "C" fn processing_pop_matrix(graphics_id: u64) {
error::check(|| graphics_record_command(graphics_entity, DrawCommand::PopMatrix));
}

/// Push the current style onto the style stack.
///
/// SAFETY:
/// - graphics_id is a valid ID returned from graphics_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_push_style(graphics_id: u64) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| graphics_record_command(graphics_entity, DrawCommand::PushStyle));
}

/// Pop the most recently saved style off the style stack.
///
/// SAFETY:
/// - graphics_id is a valid ID returned from graphics_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_pop_style(graphics_id: u64) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| graphics_record_command(graphics_entity, DrawCommand::PopStyle));
}

/// Push both the style and the transformation matrix onto their stacks.
///
/// SAFETY:
/// - graphics_id is a valid ID returned from graphics_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_push(graphics_id: u64) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| {
graphics_record_command(graphics_entity, DrawCommand::PushStyle)?;
graphics_record_command(graphics_entity, DrawCommand::PushMatrix)
});
}

/// Pop both the style and the transformation matrix off their stacks.
///
/// SAFETY:
/// - graphics_id is a valid ID returned from graphics_create.
/// - This is called from the same thread as init.
#[unsafe(no_mangle)]
pub extern "C" fn processing_pop(graphics_id: u64) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| {
graphics_record_command(graphics_entity, DrawCommand::PopStyle)?;
graphics_record_command(graphics_entity, DrawCommand::PopMatrix)
});
}

/// Reset the transformation matrix to identity.
///
/// SAFETY:
Expand Down
43 changes: 43 additions & 0 deletions crates/processing_pyo3/examples/style_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Style Stack
#
# A grid of spinners. Each cell sets up its own transform and color inside
# push()/pop(), so nothing leaks to its neighbours.
from mewnala import *
from math import sin

GRID = 6
t = 0.0


def setup():
size(600, 600)


def draw():
global t
background(16, 16, 20)

cell = width / GRID
for gy in range(GRID):
for gx in range(GRID):
spin = t + (gx + gy) * 0.4
pulse = 0.5 + 0.5 * sin(spin)

push()
translate((gx + 0.5) * cell, (gy + 0.5) * cell)
rotate(spin)

no_stroke()
fill(235, 90 + 130 * pulse, 60)
rect_mode(CENTER)
rect(0, 0, cell * (0.3 + 0.15 * pulse), cell * 0.3)

no_fill()
stroke(255, 255, 255, 40)
circle(0, 0, cell * 0.72)
pop()

t += 0.02


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

pub fn push_style(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::PushStyle)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn pop_style(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::PopStyle)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn push(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::PushStyle)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
graphics_record_command(self.entity, DrawCommand::PushMatrix)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn pop(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::PopStyle)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
graphics_record_command(self.entity, DrawCommand::PopMatrix)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

#[pyo3(signature = (*args))]
pub fn translate(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
let v = if args.len() == 3 {
Expand Down
24 changes: 24 additions & 0 deletions crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,30 @@ mod mewnala {
graphics!(module).pop_matrix()
}

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

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

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

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

#[pyfunction]
#[pyo3(pass_module, signature = (*args))]
fn translate(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {
Expand Down
2 changes: 2 additions & 0 deletions crates/processing_render/src/render/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ pub enum DrawCommand {
PushMatrix,
PopMatrix,
ResetMatrix,
PushStyle,
PopStyle,
Translate(Vec3),
Rotate {
angle: f32,
Expand Down
Loading
Loading