Skip to content

Commit dc990fe

Browse files
authored
Rework constants in Python (#213)
1 parent 67cc0a4 commit dc990fe

10 files changed

Lines changed: 458 additions & 407 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
pub const CORNER: &str = "corner";
2+
pub const CORNERS: &str = "corners";
3+
pub const RADIUS: &str = "radius";
4+
pub const CENTER: &str = "center"; // also the middle mouse button
5+
6+
pub const ROUND: &str = "round"; // caps and joins
7+
pub const SQUARE: &str = "square";
8+
pub const PROJECT: &str = "project";
9+
pub const MITER: &str = "miter";
10+
pub const BEVEL: &str = "bevel";
11+
12+
pub const POLYGON: &str = "polygon";
13+
pub const POINTS: &str = "points";
14+
pub const LINES: &str = "lines";
15+
pub const TRIANGLES: &str = "triangles";
16+
pub const TRIANGLE_FAN: &str = "triangle_fan";
17+
pub const TRIANGLE_STRIP: &str = "triangle_strip";
18+
pub const QUADS: &str = "quads";
19+
pub const QUAD_STRIP: &str = "quad_strip";
20+
pub const LINE_STRIP: &str = "line_strip"; // geometry topology; no begin_shape equivalent
21+
22+
pub const OPEN: &str = "open";
23+
pub const CHORD: &str = "chord";
24+
pub const PIE: &str = "pie";
25+
pub const CLOSE: bool = true;
26+
27+
pub const LEFT: &str = "left";
28+
pub const RIGHT: &str = "right";
29+
30+
pub const NEAREST: &str = "nearest";
31+
pub const CLAMP: &str = "clamp";
32+
pub const REPEAT: &str = "repeat";
33+
pub const MIRROR: &str = "mirror";
34+
35+
pub const SRGB: &str = "srgb";
36+
pub const LINEAR: &str = "linear"; // also the Sampler filter
37+
pub const HSL: &str = "hsl";
38+
pub const HSV: &str = "hsv";
39+
pub const HWB: &str = "hwb";
40+
pub const OKLAB: &str = "oklab";
41+
pub const OKLCH: &str = "oklch";
42+
pub const LAB: &str = "lab";
43+
pub const LCH: &str = "lch";
44+
pub const XYZ: &str = "xyz";
45+
46+
pub const PI: f32 = std::f32::consts::PI;
47+
pub const TWO_PI: f32 = std::f32::consts::TAU;
48+
pub const HALF_PI: f32 = std::f32::consts::FRAC_PI_2;
49+
pub const QUARTER_PI: f32 = std::f32::consts::FRAC_PI_4;
50+
pub const TAU: f32 = std::f32::consts::TAU;
51+
pub const DEG_TO_RAD: f32 = std::f32::consts::PI / 180.0;
52+
pub const RAD_TO_DEG: f32 = 180.0 / std::f32::consts::PI;
53+
54+
// Key codes stay numeric (compared against `key_code`); values must match `key_code_to_u32`.
55+
56+
pub const KEY_A: u32 = 65;
57+
pub const KEY_B: u32 = 66;
58+
pub const KEY_C: u32 = 67;
59+
pub const KEY_D: u32 = 68;
60+
pub const KEY_E: u32 = 69;
61+
pub const KEY_F: u32 = 70;
62+
pub const KEY_G: u32 = 71;
63+
pub const KEY_H: u32 = 72;
64+
pub const KEY_I: u32 = 73;
65+
pub const KEY_J: u32 = 74;
66+
pub const KEY_K: u32 = 75;
67+
pub const KEY_L: u32 = 76;
68+
pub const KEY_M: u32 = 77;
69+
pub const KEY_N: u32 = 78;
70+
pub const KEY_O: u32 = 79;
71+
pub const KEY_P: u32 = 80;
72+
pub const KEY_Q: u32 = 81;
73+
pub const KEY_R: u32 = 82;
74+
pub const KEY_S: u32 = 83;
75+
pub const KEY_T: u32 = 84;
76+
pub const KEY_U: u32 = 85;
77+
pub const KEY_V: u32 = 86;
78+
pub const KEY_W: u32 = 87;
79+
pub const KEY_X: u32 = 88;
80+
pub const KEY_Y: u32 = 89;
81+
pub const KEY_Z: u32 = 90;
82+
83+
pub const KEY_0: u32 = 48;
84+
pub const KEY_1: u32 = 49;
85+
pub const KEY_2: u32 = 50;
86+
pub const KEY_3: u32 = 51;
87+
pub const KEY_4: u32 = 52;
88+
pub const KEY_5: u32 = 53;
89+
pub const KEY_6: u32 = 54;
90+
pub const KEY_7: u32 = 55;
91+
pub const KEY_8: u32 = 56;
92+
pub const KEY_9: u32 = 57;
93+
94+
pub const SPACE: u32 = 32;
95+
pub const QUOTE: u32 = 39;
96+
pub const COMMA: u32 = 44;
97+
pub const MINUS: u32 = 45;
98+
pub const PERIOD: u32 = 46;
99+
pub const SLASH: u32 = 47;
100+
pub const SEMICOLON: u32 = 59;
101+
pub const EQUAL: u32 = 61;
102+
pub const BRACKET_LEFT: u32 = 91;
103+
pub const BACKSLASH: u32 = 92;
104+
pub const BRACKET_RIGHT: u32 = 93;
105+
pub const BACKQUOTE: u32 = 96;
106+
107+
pub const ESCAPE: u32 = 256;
108+
pub const ENTER: u32 = 257;
109+
pub const TAB: u32 = 258;
110+
pub const BACKSPACE: u32 = 259;
111+
pub const INSERT: u32 = 260;
112+
pub const DELETE: u32 = 261;
113+
pub const UP: u32 = 265;
114+
pub const DOWN: u32 = 264;
115+
pub const LEFT_ARROW: u32 = 263;
116+
pub const RIGHT_ARROW: u32 = 262;
117+
pub const PAGE_UP: u32 = 266;
118+
pub const PAGE_DOWN: u32 = 267;
119+
pub const HOME: u32 = 268;
120+
pub const END: u32 = 269;
121+
122+
pub const SHIFT: u32 = 340;
123+
pub const CONTROL: u32 = 341;
124+
pub const ALT: u32 = 342;
125+
pub const SUPER: u32 = 343;
126+
127+
pub const F1: u32 = 290;
128+
pub const F2: u32 = 291;
129+
pub const F3: u32 = 292;
130+
pub const F4: u32 = 293;
131+
pub const F5: u32 = 294;
132+
pub const F6: u32 = 295;
133+
pub const F7: u32 = 296;
134+
pub const F8: u32 = 297;
135+
pub const F9: u32 = 298;
136+
pub const F10: u32 = 299;
137+
pub const F11: u32 = 300;
138+
pub const F12: u32 = 301;

crates/processing_core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod config;
2+
pub mod constants;
23
pub mod error;
34

45
use std::cell::RefCell;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use processing::prelude::BlendMode;
2+
use processing::prelude::constants as c;
3+
use pyo3::prelude::*;
4+
use pyo3::types::PyModule;
5+
6+
use crate::PyBlendMode;
7+
8+
macro_rules! add {
9+
($m:expr, $($name:ident),+ $(,)?) => {
10+
$( $m.add(stringify!($name), c::$name)?; )+
11+
};
12+
}
13+
14+
pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
15+
add!(m, ROUND, SQUARE, PROJECT, MITER, BEVEL);
16+
add!(
17+
m,
18+
POLYGON,
19+
POINTS,
20+
LINES,
21+
TRIANGLES,
22+
TRIANGLE_FAN,
23+
TRIANGLE_STRIP,
24+
QUADS,
25+
QUAD_STRIP,
26+
LINE_STRIP
27+
);
28+
add!(m, CORNER, CORNERS, CENTER, RADIUS);
29+
add!(m, OPEN, CHORD, PIE, CLOSE);
30+
add!(m, LEFT, RIGHT);
31+
add!(m, NEAREST, CLAMP, REPEAT, MIRROR);
32+
add!(m, SRGB, LINEAR, HSL, HSV, HWB, OKLAB, OKLCH, LAB, LCH, XYZ);
33+
add!(
34+
m, PI, TWO_PI, HALF_PI, QUARTER_PI, TAU, DEG_TO_RAD, RAD_TO_DEG
35+
);
36+
37+
add!(
38+
m, KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L,
39+
KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y,
40+
KEY_Z
41+
);
42+
add!(
43+
m, KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9
44+
);
45+
add!(
46+
m,
47+
SPACE,
48+
QUOTE,
49+
COMMA,
50+
MINUS,
51+
PERIOD,
52+
SLASH,
53+
SEMICOLON,
54+
EQUAL,
55+
BRACKET_LEFT,
56+
BACKSLASH,
57+
BRACKET_RIGHT,
58+
BACKQUOTE
59+
);
60+
add!(
61+
m,
62+
ESCAPE,
63+
ENTER,
64+
TAB,
65+
BACKSPACE,
66+
INSERT,
67+
DELETE,
68+
UP,
69+
DOWN,
70+
LEFT_ARROW,
71+
RIGHT_ARROW,
72+
PAGE_UP,
73+
PAGE_DOWN,
74+
HOME,
75+
END
76+
);
77+
add!(m, SHIFT, CONTROL, ALT, SUPER);
78+
add!(m, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12);
79+
80+
// Objects rather than strings, passed straight to blend_mode().
81+
m.add("BLEND", PyBlendMode::from_preset(BlendMode::Blend))?;
82+
m.add("ADD", PyBlendMode::from_preset(BlendMode::Add))?;
83+
m.add("SUBTRACT", PyBlendMode::from_preset(BlendMode::Subtract))?;
84+
m.add("DARKEST", PyBlendMode::from_preset(BlendMode::Darkest))?;
85+
m.add("LIGHTEST", PyBlendMode::from_preset(BlendMode::Lightest))?;
86+
m.add(
87+
"DIFFERENCE",
88+
PyBlendMode::from_preset(BlendMode::Difference),
89+
)?;
90+
m.add("EXCLUSION", PyBlendMode::from_preset(BlendMode::Exclusion))?;
91+
m.add("MULTIPLY", PyBlendMode::from_preset(BlendMode::Multiply))?;
92+
m.add("SCREEN", PyBlendMode::from_preset(BlendMode::Screen))?;
93+
m.add("REPLACE", PyBlendMode::from_preset(BlendMode::Replace))?;
94+
95+
Ok(())
96+
}

0 commit comments

Comments
 (0)