forked from viridia/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter.rs
121 lines (105 loc) · 3.27 KB
/
iter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! Example of a simple UI layout
#![feature(impl_trait_in_assoc_type)]
#[path = "./common/lib.rs"]
mod common;
use bevy::{color::palettes, prelude::*};
use bevy_mod_stylebuilder::*;
use bevy_quill::{Cx, Element, For, QuillPlugin, View, ViewTemplate};
use common::*;
fn main() {
App::new()
.init_resource::<List>()
.init_resource::<Random32>()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
QuillPlugin,
))
// .add_plugins((CorePlugin, InputPlugin, InteractionPlugin, BevyUiBackend))
.add_systems(Startup, (setup, setup_view_root))
.add_systems(Update, (close_on_esc, rotate, update_counter))
.run();
}
fn style_test(ss: &mut StyleBuilder) {
ss.display(Display::Flex)
.flex_direction(FlexDirection::Row)
.border_color(palettes::css::LIME)
.border(3)
.padding(3);
}
const SUITS: &[&str] = &["hearts", "spades", "clubs", "diamonds"];
fn setup_view_root(mut commands: Commands) {
commands.spawn(
Element::<NodeBundle>::new()
.style(style_test)
.children((IterExample, IndexedIterExample))
.to_root(),
);
}
/// Example of a view template that displays a string.
#[derive(Clone, PartialEq)]
struct IterExample;
impl ViewTemplate for IterExample {
type View = impl View;
fn create(&self, cx: &mut Cx) -> Self::View {
let items = cx.use_resource::<List>();
Element::<NodeBundle>::new().children((
"Suits: ",
For::each(items.items.clone(), |item| format!("[{}]", item)).with_fallback("No items"),
))
}
}
#[derive(Clone, PartialEq)]
struct IndexedIterExample;
impl ViewTemplate for IndexedIterExample {
type View = impl View;
fn create(&self, cx: &mut Cx) -> Self::View {
let items = cx.use_resource::<List>();
Element::<NodeBundle>::new().children((
"Suits: ",
For::index(&items.items, |item, index| format!("[{}:{}]", item, index))
.with_fallback("No items"),
))
}
}
#[derive(Resource, Default)]
pub struct List {
pub items: Vec<String>,
}
fn update_counter(
mut list: ResMut<List>,
key: Res<ButtonInput<KeyCode>>,
mut random: ResMut<Random32>,
) {
if key.pressed(KeyCode::Space) {
println!("-- Space pressed --");
let i = (random.next() as usize) % SUITS.len();
list.items.push(SUITS[i].to_string());
while list.items.len() > 10 {
list.items.remove(0);
}
} else if key.pressed(KeyCode::Minus) {
println!("-- Minus pressed --");
list.items.pop();
}
}
#[derive(Resource)]
struct Random32 {
state: u32,
}
impl Random32 {
// Generate a pseudo-random number
fn next(&mut self) -> u32 {
// Constants for 32-bit LCG (example values, you might want to choose different ones)
let a: u32 = 1664525; // Multiplier
let c: u32 = 1013904223; // Increment
let m: u32 = 2u32.pow(31); // Modulus, often set to 2^31 for a 32-bit generator
// Simple LCG formula: X_{n+1} = (aX_n + c) mod m
self.state = (a.wrapping_mul(self.state).wrapping_add(c)) % m;
self.state
}
}
impl Default for Random32 {
fn default() -> Self {
Self { state: 17 }
}
}