Skip to content

Commit 0f80c7e

Browse files
authored
Add style stack (#214)
1 parent dc990fe commit 0f80c7e

8 files changed

Lines changed: 398 additions & 184 deletions

File tree

crates/processing_ffi/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,60 @@ pub extern "C" fn processing_pop_matrix(graphics_id: u64) {
448448
error::check(|| graphics_record_command(graphics_entity, DrawCommand::PopMatrix));
449449
}
450450

451+
/// Push the current style onto the style stack.
452+
///
453+
/// SAFETY:
454+
/// - graphics_id is a valid ID returned from graphics_create.
455+
/// - This is called from the same thread as init.
456+
#[unsafe(no_mangle)]
457+
pub extern "C" fn processing_push_style(graphics_id: u64) {
458+
error::clear_error();
459+
let graphics_entity = Entity::from_bits(graphics_id);
460+
error::check(|| graphics_record_command(graphics_entity, DrawCommand::PushStyle));
461+
}
462+
463+
/// Pop the most recently saved style off the style stack.
464+
///
465+
/// SAFETY:
466+
/// - graphics_id is a valid ID returned from graphics_create.
467+
/// - This is called from the same thread as init.
468+
#[unsafe(no_mangle)]
469+
pub extern "C" fn processing_pop_style(graphics_id: u64) {
470+
error::clear_error();
471+
let graphics_entity = Entity::from_bits(graphics_id);
472+
error::check(|| graphics_record_command(graphics_entity, DrawCommand::PopStyle));
473+
}
474+
475+
/// Push both the style and the transformation matrix onto their stacks.
476+
///
477+
/// SAFETY:
478+
/// - graphics_id is a valid ID returned from graphics_create.
479+
/// - This is called from the same thread as init.
480+
#[unsafe(no_mangle)]
481+
pub extern "C" fn processing_push(graphics_id: u64) {
482+
error::clear_error();
483+
let graphics_entity = Entity::from_bits(graphics_id);
484+
error::check(|| {
485+
graphics_record_command(graphics_entity, DrawCommand::PushStyle)?;
486+
graphics_record_command(graphics_entity, DrawCommand::PushMatrix)
487+
});
488+
}
489+
490+
/// Pop both the style and the transformation matrix off their stacks.
491+
///
492+
/// SAFETY:
493+
/// - graphics_id is a valid ID returned from graphics_create.
494+
/// - This is called from the same thread as init.
495+
#[unsafe(no_mangle)]
496+
pub extern "C" fn processing_pop(graphics_id: u64) {
497+
error::clear_error();
498+
let graphics_entity = Entity::from_bits(graphics_id);
499+
error::check(|| {
500+
graphics_record_command(graphics_entity, DrawCommand::PopStyle)?;
501+
graphics_record_command(graphics_entity, DrawCommand::PopMatrix)
502+
});
503+
}
504+
451505
/// Reset the transformation matrix to identity.
452506
///
453507
/// SAFETY:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Style Stack
2+
#
3+
# A grid of spinners. Each cell sets up its own transform and color inside
4+
# push()/pop(), so nothing leaks to its neighbours.
5+
from mewnala import *
6+
from math import sin
7+
8+
GRID = 6
9+
t = 0.0
10+
11+
12+
def setup():
13+
size(600, 600)
14+
15+
16+
def draw():
17+
global t
18+
background(16, 16, 20)
19+
20+
cell = width / GRID
21+
for gy in range(GRID):
22+
for gx in range(GRID):
23+
spin = t + (gx + gy) * 0.4
24+
pulse = 0.5 + 0.5 * sin(spin)
25+
26+
push()
27+
translate((gx + 0.5) * cell, (gy + 0.5) * cell)
28+
rotate(spin)
29+
30+
no_stroke()
31+
fill(235, 90 + 130 * pulse, 60)
32+
rect_mode(CENTER)
33+
rect(0, 0, cell * (0.3 + 0.15 * pulse), cell * 0.3)
34+
35+
no_fill()
36+
stroke(255, 255, 255, 40)
37+
circle(0, 0, cell * 0.72)
38+
pop()
39+
40+
t += 0.02
41+
42+
43+
run()

crates/processing_pyo3/src/graphics.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,30 @@ impl Graphics {
13711371
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
13721372
}
13731373

1374+
pub fn push_style(&self) -> PyResult<()> {
1375+
graphics_record_command(self.entity, DrawCommand::PushStyle)
1376+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1377+
}
1378+
1379+
pub fn pop_style(&self) -> PyResult<()> {
1380+
graphics_record_command(self.entity, DrawCommand::PopStyle)
1381+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1382+
}
1383+
1384+
pub fn push(&self) -> PyResult<()> {
1385+
graphics_record_command(self.entity, DrawCommand::PushStyle)
1386+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
1387+
graphics_record_command(self.entity, DrawCommand::PushMatrix)
1388+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1389+
}
1390+
1391+
pub fn pop(&self) -> PyResult<()> {
1392+
graphics_record_command(self.entity, DrawCommand::PopStyle)
1393+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
1394+
graphics_record_command(self.entity, DrawCommand::PopMatrix)
1395+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1396+
}
1397+
13741398
#[pyo3(signature = (*args))]
13751399
pub fn translate(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
13761400
let v = if args.len() == 3 {

crates/processing_pyo3/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,30 @@ mod mewnala {
990990
graphics!(module).pop_matrix()
991991
}
992992

993+
#[pyfunction]
994+
#[pyo3(pass_module)]
995+
fn push_style(module: &Bound<'_, PyModule>) -> PyResult<()> {
996+
graphics!(module).push_style()
997+
}
998+
999+
#[pyfunction]
1000+
#[pyo3(pass_module)]
1001+
fn pop_style(module: &Bound<'_, PyModule>) -> PyResult<()> {
1002+
graphics!(module).pop_style()
1003+
}
1004+
1005+
#[pyfunction]
1006+
#[pyo3(pass_module)]
1007+
fn push(module: &Bound<'_, PyModule>) -> PyResult<()> {
1008+
graphics!(module).push()
1009+
}
1010+
1011+
#[pyfunction]
1012+
#[pyo3(pass_module)]
1013+
fn pop(module: &Bound<'_, PyModule>) -> PyResult<()> {
1014+
graphics!(module).pop()
1015+
}
1016+
9931017
#[pyfunction]
9941018
#[pyo3(pass_module, signature = (*args))]
9951019
fn translate(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {

crates/processing_render/src/render/command.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ pub enum DrawCommand {
573573
PushMatrix,
574574
PopMatrix,
575575
ResetMatrix,
576+
PushStyle,
577+
PopStyle,
576578
Translate(Vec3),
577579
Rotate {
578580
angle: f32,

0 commit comments

Comments
 (0)