pub struct CellSpec {
pub policy: Policy,
pub range: Range,
pub initial: f64,
pub exact_bit: bool,
pub grid: Option<f64>,
}Fields§
§policy: Policy§range: Range§initial: f64§exact_bit: bool§grid: Option<f64>Implementations§
Source§impl CellSpec
impl CellSpec
Sourcepub fn initial(self, value: f64) -> CellSpec
pub fn initial(self, value: f64) -> CellSpec
Examples found in repository?
examples/toggle.rs (line 71)
67 pub fn new() -> Toggle {
68 // Every number the program reads, or keeps from one evaluation to the next.
69 let mut table = SourceTable::new();
70 let coordinate = || input(-16_384.0, 16_384.0).grid(1.0 / 64.0);
71 let viewport_width = table.declare("viewport.width", input(0.0, 16_384.0).grid(1.0).initial(1280.0));
72 let viewport_height = table.declare("viewport.height", input(0.0, 16_384.0).grid(1.0).initial(800.0));
73 let mut pointer = |name: &str| Pointer {
74 active: table.declare(format!("{name}.active"), event()),
75 x: table.declare(format!("{name}.x"), coordinate()),
76 y: table.declare(format!("{name}.y"), coordinate()),
77 };
78 let down = pointer("down");
79 let up = pointer("up");
80 let cancel = table.declare("cancel", event());
81 let on_cell = table.declare("on", state_bit());
82 let armed_cell = table.declare("armed", state_bit());
83 let mut circuit = table.create_circuit();
84 let c = &mut circuit;
85
86 // Layout. trackLeft = viewportWidth / 2 - trackWidth / 2
87 let track_left = c.affine(viewport_width * 0.5 - TRACK_WIDTH / 2.0);
88 let track_top = c.affine(viewport_height * 0.5 - TRACK_HEIGHT / 2.0);
89 let track_right = c.affine(track_left + TRACK_WIDTH);
90 let track_bottom = c.affine(track_top + TRACK_HEIGHT);
91 // x >= trackLeft && x < trackLeft + trackWidth && y >= trackTop && y < trackTop + trackHeight
92 let inside_track = |c: &mut Circuit, point: &Pointer| {
93 let bits = [
94 emit_compare(c, point.x, Relation::AtLeast, track_left),
95 emit_compare(c, point.x, Relation::Less, track_right),
96 emit_compare(c, point.y, Relation::AtLeast, track_top),
97 emit_compare(c, point.y, Relation::Less, track_bottom),
98 ];
99 emit_and(c, &bits)
100 };
101
102 // Handle inputs in a fixed order: down, then up, then cancel.
103 // if (down) armed = insideTrack(down)
104 let down_inside = inside_track(c, &down);
105 let armed_after_down = emit_select(c, down.active, armed_cell, down_inside);
106 // if (up && armed && insideTrack(up)) on = !on
107 let up_inside = inside_track(c, &up);
108 let flips = emit_and(c, &[up.active, armed_after_down, up_inside]);
109 let flipped = emit_not(c, on_cell);
110 let on = emit_select(c, flips, on_cell, flipped);
111 // if (up || cancel) armed = false
112 let no_up = emit_not(c, up.active);
113 let no_cancel = emit_not(c, cancel);
114 let armed = emit_and(c, &[armed_after_down, no_up, no_cancel]);
115
116 // Commit state: each state cell gets exactly one next value.
117 table.next(on_cell, on);
118 table.next(armed_cell, armed);
119 table.finish();
120 let outputs = [track_left, track_top, on, armed];
121 let mut roots = outputs.to_vec();
122 roots.extend(table.next_wires());
123 let program = circuit.compile(Some(&roots));
124 let source = table.initial();
125 Toggle {
126 table,
127 circuit,
128 program,
129 source,
130 viewport: (viewport_width, viewport_height),
131 down,
132 up,
133 cancel,
134 outputs,
135 }
136 }Sourcepub fn grid(self, step: f64) -> CellSpec
pub fn grid(self, step: f64) -> CellSpec
Every value is a multiple of step, a power of two, e.g. 1/64 for pointer coordinates.
Examples found in repository?
examples/toggle.rs (line 70)
67 pub fn new() -> Toggle {
68 // Every number the program reads, or keeps from one evaluation to the next.
69 let mut table = SourceTable::new();
70 let coordinate = || input(-16_384.0, 16_384.0).grid(1.0 / 64.0);
71 let viewport_width = table.declare("viewport.width", input(0.0, 16_384.0).grid(1.0).initial(1280.0));
72 let viewport_height = table.declare("viewport.height", input(0.0, 16_384.0).grid(1.0).initial(800.0));
73 let mut pointer = |name: &str| Pointer {
74 active: table.declare(format!("{name}.active"), event()),
75 x: table.declare(format!("{name}.x"), coordinate()),
76 y: table.declare(format!("{name}.y"), coordinate()),
77 };
78 let down = pointer("down");
79 let up = pointer("up");
80 let cancel = table.declare("cancel", event());
81 let on_cell = table.declare("on", state_bit());
82 let armed_cell = table.declare("armed", state_bit());
83 let mut circuit = table.create_circuit();
84 let c = &mut circuit;
85
86 // Layout. trackLeft = viewportWidth / 2 - trackWidth / 2
87 let track_left = c.affine(viewport_width * 0.5 - TRACK_WIDTH / 2.0);
88 let track_top = c.affine(viewport_height * 0.5 - TRACK_HEIGHT / 2.0);
89 let track_right = c.affine(track_left + TRACK_WIDTH);
90 let track_bottom = c.affine(track_top + TRACK_HEIGHT);
91 // x >= trackLeft && x < trackLeft + trackWidth && y >= trackTop && y < trackTop + trackHeight
92 let inside_track = |c: &mut Circuit, point: &Pointer| {
93 let bits = [
94 emit_compare(c, point.x, Relation::AtLeast, track_left),
95 emit_compare(c, point.x, Relation::Less, track_right),
96 emit_compare(c, point.y, Relation::AtLeast, track_top),
97 emit_compare(c, point.y, Relation::Less, track_bottom),
98 ];
99 emit_and(c, &bits)
100 };
101
102 // Handle inputs in a fixed order: down, then up, then cancel.
103 // if (down) armed = insideTrack(down)
104 let down_inside = inside_track(c, &down);
105 let armed_after_down = emit_select(c, down.active, armed_cell, down_inside);
106 // if (up && armed && insideTrack(up)) on = !on
107 let up_inside = inside_track(c, &up);
108 let flips = emit_and(c, &[up.active, armed_after_down, up_inside]);
109 let flipped = emit_not(c, on_cell);
110 let on = emit_select(c, flips, on_cell, flipped);
111 // if (up || cancel) armed = false
112 let no_up = emit_not(c, up.active);
113 let no_cancel = emit_not(c, cancel);
114 let armed = emit_and(c, &[armed_after_down, no_up, no_cancel]);
115
116 // Commit state: each state cell gets exactly one next value.
117 table.next(on_cell, on);
118 table.next(armed_cell, armed);
119 table.finish();
120 let outputs = [track_left, track_top, on, armed];
121 let mut roots = outputs.to_vec();
122 roots.extend(table.next_wires());
123 let program = circuit.compile(Some(&roots));
124 let source = table.initial();
125 Toggle {
126 table,
127 circuit,
128 program,
129 source,
130 viewport: (viewport_width, viewport_height),
131 down,
132 up,
133 cancel,
134 outputs,
135 }
136 }Trait Implementations§
Auto Trait Implementations§
impl Freeze for CellSpec
impl RefUnwindSafe for CellSpec
impl Send for CellSpec
impl Sync for CellSpec
impl Unpin for CellSpec
impl UnsafeUnpin for CellSpec
impl UnwindSafe for CellSpec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more