Skip to content

Commit 762f4ff

Browse files
committed
Add style stack.
1 parent ac26288 commit 762f4ff

8 files changed

Lines changed: 396 additions & 182 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
@@ -1366,6 +1366,30 @@ impl Graphics {
13661366
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
13671367
}
13681368

1369+
pub fn push_style(&self) -> PyResult<()> {
1370+
graphics_record_command(self.entity, DrawCommand::PushStyle)
1371+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1372+
}
1373+
1374+
pub fn pop_style(&self) -> PyResult<()> {
1375+
graphics_record_command(self.entity, DrawCommand::PopStyle)
1376+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1377+
}
1378+
1379+
pub fn push(&self) -> PyResult<()> {
1380+
graphics_record_command(self.entity, DrawCommand::PushStyle)
1381+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
1382+
graphics_record_command(self.entity, DrawCommand::PushMatrix)
1383+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1384+
}
1385+
1386+
pub fn pop(&self) -> PyResult<()> {
1387+
graphics_record_command(self.entity, DrawCommand::PopStyle)
1388+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
1389+
graphics_record_command(self.entity, DrawCommand::PopMatrix)
1390+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1391+
}
1392+
13691393
#[pyo3(signature = (*args))]
13701394
pub fn translate(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
13711395
let v = extract_vec2(args)?;

crates/processing_pyo3/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,30 @@ mod mewnala {
12711271
graphics!(module).pop_matrix()
12721272
}
12731273

1274+
#[pyfunction]
1275+
#[pyo3(pass_module)]
1276+
fn push_style(module: &Bound<'_, PyModule>) -> PyResult<()> {
1277+
graphics!(module).push_style()
1278+
}
1279+
1280+
#[pyfunction]
1281+
#[pyo3(pass_module)]
1282+
fn pop_style(module: &Bound<'_, PyModule>) -> PyResult<()> {
1283+
graphics!(module).pop_style()
1284+
}
1285+
1286+
#[pyfunction]
1287+
#[pyo3(pass_module)]
1288+
fn push(module: &Bound<'_, PyModule>) -> PyResult<()> {
1289+
graphics!(module).push()
1290+
}
1291+
1292+
#[pyfunction]
1293+
#[pyo3(pass_module)]
1294+
fn pop(module: &Bound<'_, PyModule>) -> PyResult<()> {
1295+
graphics!(module).pop()
1296+
}
1297+
12741298
#[pyfunction]
12751299
#[pyo3(pass_module, signature = (*args))]
12761300
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
@@ -511,6 +511,8 @@ pub enum DrawCommand {
511511
PushMatrix,
512512
PopMatrix,
513513
ResetMatrix,
514+
PushStyle,
515+
PopStyle,
514516
Translate(Vec2),
515517
Rotate {
516518
angle: f32,

0 commit comments

Comments
 (0)