pub struct State<Action> { /* private fields */ }Implementations§
Source§impl<Action> State<Action>
impl<Action> State<Action>
Sourcepub fn ui(&mut self) -> &mut Ui<Action>
pub fn ui(&mut self) -> &mut Ui<Action>
Examples found in repository?
examples/example-ui.rs (line 18)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
fn new(state: &mut onyx::State<Action>) -> Self {
let scene = Scene { count: 0 };
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Vertical)
.child(onyx::ui::button(|| Action::Increment)
.child(onyx::ui::label("Increment"))
)
.child(onyx::ui::button(|| Action::Decrement)
.child(onyx::ui::label("Decrement"))
)
.child(onyx::ui::label("0").id("count"))
);
scene
}
fn action(&mut self, action: Action, state: &mut onyx::State<Action>) -> onyx::Transition {
match action {
Action::Increment => self.count += 1,
Action::Decrement => self.count -= 1,
}
state.ui().set_text("count", &self.count.to_string());
state.none()
}More examples
examples/example-transition.rs (line 25)
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
fn new(state: &mut onyx::State<Self::Action>) -> Self {
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Vertical)
.child(onyx::ui::button(|| Action1::Push2)
.child(onyx::ui::label("Push 2"))
)
.child(onyx::ui::button(|| Action1::Switch3)
.child(onyx::ui::label("Switch 3"))
)
.child(onyx::ui::button(|| Action1::Quit)
.child(onyx::ui::label("Quit"))
)
);
Scene1
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
match action {
Action1::Push2 => state.push::<Scene2>(),
Action1::Switch3 => state.switch::<Scene3>(),
Action1::Quit => state.quit(),
}
}
}
impl onyx::Scene for Scene2 {
type Action = Action2;
fn new(state: &mut onyx::State<Self::Action>) -> Self {
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Vertical)
.child(onyx::ui::button(|| Action2::Pop)
.child(onyx::ui::label("Pop"))
)
);
Scene2
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
match action {
Action2::Pop => state.pop(),
}
}
}
impl onyx::Scene for Scene3 {
type Action = Action3;
fn new(state: &mut onyx::State<Self::Action>) -> Self {
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Vertical)
.child(onyx::ui::button(|| Action3::Switch1)
.child(onyx::ui::label("Switch 1"))
)
);
Scene3
}examples/example-pong.rs (line 73)
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
fn new(state: &mut onyx::State<Action>) -> Self {
let size = state.context().size();
let plank_size = Vec2::new(10.0, 100.0);
let left_plank = Rect::new(state, 0.5 * Vec2::new(plank_size.x, size.y), plank_size, Vec2::zero());
let right_plank = Rect::new(state, Vec2::new(size.x - 0.5 * plank_size.x, 0.5 * size.y), plank_size, Vec2::zero());
let ball_size = Vec2::new(20.0, 20.0);
let ball = Rect::new(state, 0.5 * size, ball_size, Vec2::new(100.0, 100.0));
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Horizontal)
.alignment(onyx::ui::Alignment::Center, onyx::ui::Alignment::Min)
.child(onyx::ui::label("0").id("left"))
.child(onyx::ui::label(":"))
.child(onyx::ui::label("0").id("right"))
);
let mut keys = HashMap::new();
keys.insert(onyx::Key::W, (Direction::Up, Plank::Left));
keys.insert(onyx::Key::S, (Direction::Down, Plank::Left));
keys.insert(onyx::Key::Up, (Direction::Up, Plank::Right));
keys.insert(onyx::Key::Down, (Direction::Down, Plank::Right));
Scene {
left_plank,
right_plank,
ball,
left_score: 0,
right_score: 0,
keys,
}
}
fn event(&mut self, event: onyx::Event) -> Option<Action> {
match event {
onyx::Event::KeyPressed(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Start })
} else {
None
}
},
onyx::Event::KeyReleased(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Stop })
} else {
None
}
},
_ => None,
}
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
let plank = match action.plank {
Plank::Left => &mut self.left_plank,
Plank::Right => &mut self.right_plank,
};
let velocity = match action.direction {
Direction::Down => Vec2::new(0.0, 100.0),
Direction::Up => Vec2::new(0.0, -100.0),
};
match action.kind {
Kind::Start => plank.velocity += velocity,
Kind::Stop => plank.velocity -= velocity,
}
state.none()
}
fn update(&mut self, state: &mut onyx::State<Action>) -> onyx::Transition {
let min = Vec2::zero();
let max = state.context().size();
self.ball.center += self.ball.velocity * state.context().dt();
self.left_plank.center += self.left_plank.velocity * state.context().dt();
self.right_plank.center += self.right_plank.velocity * state.context().dt();
if self.ball.max().x > max.x - self.right_plank.size.x && self.ball.max().x < max.x {
if self.ball.min().y < self.right_plank.max().y && self.ball.max().y > self.right_plank.min().y {
self.ball.center.x = max.x - self.right_plank.size.x - 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.min().x > max.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.left_score += 1;
state.ui().set_text("left", &self.left_score.to_string());
}
if self.ball.min().x < min.x + self.left_plank.size.x && self.ball.max().x > min.x {
if self.ball.min().y < self.left_plank.max().y && self.ball.max().y > self.left_plank.min().y {
self.ball.center.x = min.x + self.left_plank.size.x + 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.max().x < min.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.right_score += 1;
state.ui().set_text("right", &self.right_score.to_string());
}
if self.ball.min().y < min.y {
self.ball.center.y = min.y + 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
if self.ball.max().y > max.y {
self.ball.center.y = max.y - 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
state.graph().set_position(self.ball.transform, self.ball.center);
state.graph().set_position(self.left_plank.transform, self.left_plank.center);
state.graph().set_position(self.right_plank.transform, self.right_plank.center);
state.none()
}Sourcepub fn context(&self) -> Rc<Context>
pub fn context(&self) -> Rc<Context>
Examples found in repository?
examples/example-pong.rs (line 67)
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
fn new(state: &mut onyx::State<Action>) -> Self {
let size = state.context().size();
let plank_size = Vec2::new(10.0, 100.0);
let left_plank = Rect::new(state, 0.5 * Vec2::new(plank_size.x, size.y), plank_size, Vec2::zero());
let right_plank = Rect::new(state, Vec2::new(size.x - 0.5 * plank_size.x, 0.5 * size.y), plank_size, Vec2::zero());
let ball_size = Vec2::new(20.0, 20.0);
let ball = Rect::new(state, 0.5 * size, ball_size, Vec2::new(100.0, 100.0));
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Horizontal)
.alignment(onyx::ui::Alignment::Center, onyx::ui::Alignment::Min)
.child(onyx::ui::label("0").id("left"))
.child(onyx::ui::label(":"))
.child(onyx::ui::label("0").id("right"))
);
let mut keys = HashMap::new();
keys.insert(onyx::Key::W, (Direction::Up, Plank::Left));
keys.insert(onyx::Key::S, (Direction::Down, Plank::Left));
keys.insert(onyx::Key::Up, (Direction::Up, Plank::Right));
keys.insert(onyx::Key::Down, (Direction::Down, Plank::Right));
Scene {
left_plank,
right_plank,
ball,
left_score: 0,
right_score: 0,
keys,
}
}
fn event(&mut self, event: onyx::Event) -> Option<Action> {
match event {
onyx::Event::KeyPressed(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Start })
} else {
None
}
},
onyx::Event::KeyReleased(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Stop })
} else {
None
}
},
_ => None,
}
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
let plank = match action.plank {
Plank::Left => &mut self.left_plank,
Plank::Right => &mut self.right_plank,
};
let velocity = match action.direction {
Direction::Down => Vec2::new(0.0, 100.0),
Direction::Up => Vec2::new(0.0, -100.0),
};
match action.kind {
Kind::Start => plank.velocity += velocity,
Kind::Stop => plank.velocity -= velocity,
}
state.none()
}
fn update(&mut self, state: &mut onyx::State<Action>) -> onyx::Transition {
let min = Vec2::zero();
let max = state.context().size();
self.ball.center += self.ball.velocity * state.context().dt();
self.left_plank.center += self.left_plank.velocity * state.context().dt();
self.right_plank.center += self.right_plank.velocity * state.context().dt();
if self.ball.max().x > max.x - self.right_plank.size.x && self.ball.max().x < max.x {
if self.ball.min().y < self.right_plank.max().y && self.ball.max().y > self.right_plank.min().y {
self.ball.center.x = max.x - self.right_plank.size.x - 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.min().x > max.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.left_score += 1;
state.ui().set_text("left", &self.left_score.to_string());
}
if self.ball.min().x < min.x + self.left_plank.size.x && self.ball.max().x > min.x {
if self.ball.min().y < self.left_plank.max().y && self.ball.max().y > self.left_plank.min().y {
self.ball.center.x = min.x + self.left_plank.size.x + 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.max().x < min.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.right_score += 1;
state.ui().set_text("right", &self.right_score.to_string());
}
if self.ball.min().y < min.y {
self.ball.center.y = min.y + 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
if self.ball.max().y > max.y {
self.ball.center.y = max.y - 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
state.graph().set_position(self.ball.transform, self.ball.center);
state.graph().set_position(self.left_plank.transform, self.left_plank.center);
state.graph().set_position(self.right_plank.transform, self.right_plank.center);
state.none()
}Sourcepub fn graph(&mut self) -> &mut TransformSystem
pub fn graph(&mut self) -> &mut TransformSystem
Examples found in repository?
examples/example-pong.rs (line 48)
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
fn new(state: &mut onyx::State<Action>, center: Vec2, size: Vec2, velocity: Vec2) -> Rect {
let transform = state.graph().add(center, Vec2::new(1.0, 1.0));
let child = state.graph().add_child(transform, -0.5 * size, size);
state.renderer().add(child);
Self { center, size, velocity, transform }
}
fn min(&self) -> Vec2 {
self.center - 0.5 * self.size
}
fn max(&self) -> Vec2 {
self.center + 0.5 * self.size
}
}
impl onyx::Scene for Scene {
type Action = Action;
fn new(state: &mut onyx::State<Action>) -> Self {
let size = state.context().size();
let plank_size = Vec2::new(10.0, 100.0);
let left_plank = Rect::new(state, 0.5 * Vec2::new(plank_size.x, size.y), plank_size, Vec2::zero());
let right_plank = Rect::new(state, Vec2::new(size.x - 0.5 * plank_size.x, 0.5 * size.y), plank_size, Vec2::zero());
let ball_size = Vec2::new(20.0, 20.0);
let ball = Rect::new(state, 0.5 * size, ball_size, Vec2::new(100.0, 100.0));
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Horizontal)
.alignment(onyx::ui::Alignment::Center, onyx::ui::Alignment::Min)
.child(onyx::ui::label("0").id("left"))
.child(onyx::ui::label(":"))
.child(onyx::ui::label("0").id("right"))
);
let mut keys = HashMap::new();
keys.insert(onyx::Key::W, (Direction::Up, Plank::Left));
keys.insert(onyx::Key::S, (Direction::Down, Plank::Left));
keys.insert(onyx::Key::Up, (Direction::Up, Plank::Right));
keys.insert(onyx::Key::Down, (Direction::Down, Plank::Right));
Scene {
left_plank,
right_plank,
ball,
left_score: 0,
right_score: 0,
keys,
}
}
fn event(&mut self, event: onyx::Event) -> Option<Action> {
match event {
onyx::Event::KeyPressed(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Start })
} else {
None
}
},
onyx::Event::KeyReleased(key) => {
if let Some(&(direction, plank)) = self.keys.get(&key) {
Some(Action { direction, plank, kind: Kind::Stop })
} else {
None
}
},
_ => None,
}
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
let plank = match action.plank {
Plank::Left => &mut self.left_plank,
Plank::Right => &mut self.right_plank,
};
let velocity = match action.direction {
Direction::Down => Vec2::new(0.0, 100.0),
Direction::Up => Vec2::new(0.0, -100.0),
};
match action.kind {
Kind::Start => plank.velocity += velocity,
Kind::Stop => plank.velocity -= velocity,
}
state.none()
}
fn update(&mut self, state: &mut onyx::State<Action>) -> onyx::Transition {
let min = Vec2::zero();
let max = state.context().size();
self.ball.center += self.ball.velocity * state.context().dt();
self.left_plank.center += self.left_plank.velocity * state.context().dt();
self.right_plank.center += self.right_plank.velocity * state.context().dt();
if self.ball.max().x > max.x - self.right_plank.size.x && self.ball.max().x < max.x {
if self.ball.min().y < self.right_plank.max().y && self.ball.max().y > self.right_plank.min().y {
self.ball.center.x = max.x - self.right_plank.size.x - 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.min().x > max.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.left_score += 1;
state.ui().set_text("left", &self.left_score.to_string());
}
if self.ball.min().x < min.x + self.left_plank.size.x && self.ball.max().x > min.x {
if self.ball.min().y < self.left_plank.max().y && self.ball.max().y > self.left_plank.min().y {
self.ball.center.x = min.x + self.left_plank.size.x + 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.max().x < min.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.right_score += 1;
state.ui().set_text("right", &self.right_score.to_string());
}
if self.ball.min().y < min.y {
self.ball.center.y = min.y + 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
if self.ball.max().y > max.y {
self.ball.center.y = max.y - 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
state.graph().set_position(self.ball.transform, self.ball.center);
state.graph().set_position(self.left_plank.transform, self.left_plank.center);
state.graph().set_position(self.right_plank.transform, self.right_plank.center);
state.none()
}Sourcepub fn renderer(&mut self) -> &mut RenderSystem
pub fn renderer(&mut self) -> &mut RenderSystem
pub fn is_pressed(&self, key: Key) -> bool
Sourcepub fn none(&self) -> Transition
pub fn none(&self) -> Transition
Examples found in repository?
More examples
examples/example-pong.rs (line 128)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Action>) -> onyx::Transition {
let plank = match action.plank {
Plank::Left => &mut self.left_plank,
Plank::Right => &mut self.right_plank,
};
let velocity = match action.direction {
Direction::Down => Vec2::new(0.0, 100.0),
Direction::Up => Vec2::new(0.0, -100.0),
};
match action.kind {
Kind::Start => plank.velocity += velocity,
Kind::Stop => plank.velocity -= velocity,
}
state.none()
}
fn update(&mut self, state: &mut onyx::State<Action>) -> onyx::Transition {
let min = Vec2::zero();
let max = state.context().size();
self.ball.center += self.ball.velocity * state.context().dt();
self.left_plank.center += self.left_plank.velocity * state.context().dt();
self.right_plank.center += self.right_plank.velocity * state.context().dt();
if self.ball.max().x > max.x - self.right_plank.size.x && self.ball.max().x < max.x {
if self.ball.min().y < self.right_plank.max().y && self.ball.max().y > self.right_plank.min().y {
self.ball.center.x = max.x - self.right_plank.size.x - 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.min().x > max.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.left_score += 1;
state.ui().set_text("left", &self.left_score.to_string());
}
if self.ball.min().x < min.x + self.left_plank.size.x && self.ball.max().x > min.x {
if self.ball.min().y < self.left_plank.max().y && self.ball.max().y > self.left_plank.min().y {
self.ball.center.x = min.x + self.left_plank.size.x + 0.5 * self.ball.size.x;
self.ball.velocity.x = -self.ball.velocity.x;
}
}
if self.ball.max().x < min.x {
self.ball.center.x = 0.5 * (max.x - min.x);
self.right_score += 1;
state.ui().set_text("right", &self.right_score.to_string());
}
if self.ball.min().y < min.y {
self.ball.center.y = min.y + 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
if self.ball.max().y > max.y {
self.ball.center.y = max.y - 0.5 * self.ball.size.y;
self.ball.velocity.y = -self.ball.velocity.y;
}
state.graph().set_position(self.ball.transform, self.ball.center);
state.graph().set_position(self.left_plank.transform, self.left_plank.center);
state.graph().set_position(self.right_plank.transform, self.right_plank.center);
state.none()
}Sourcepub fn quit(&self) -> Transition
pub fn quit(&self) -> Transition
Sourcepub fn pop(&self) -> Transition
pub fn pop(&self) -> Transition
Sourcepub fn push<S: Scene + 'static>(&self) -> Transition
pub fn push<S: Scene + 'static>(&self) -> Transition
Sourcepub fn switch<S: Scene + 'static>(&self) -> Transition
pub fn switch<S: Scene + 'static>(&self) -> Transition
Examples found in repository?
examples/example-transition.rs (line 43)
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
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
match action {
Action1::Push2 => state.push::<Scene2>(),
Action1::Switch3 => state.switch::<Scene3>(),
Action1::Quit => state.quit(),
}
}
}
impl onyx::Scene for Scene2 {
type Action = Action2;
fn new(state: &mut onyx::State<Self::Action>) -> Self {
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Vertical)
.child(onyx::ui::button(|| Action2::Pop)
.child(onyx::ui::label("Pop"))
)
);
Scene2
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
match action {
Action2::Pop => state.pop(),
}
}
}
impl onyx::Scene for Scene3 {
type Action = Action3;
fn new(state: &mut onyx::State<Self::Action>) -> Self {
state.ui().build(onyx::ui::panel()
.layout(onyx::ui::Layout::Vertical)
.child(onyx::ui::button(|| Action3::Switch1)
.child(onyx::ui::label("Switch 1"))
)
);
Scene3
}
fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
match action {
Action3::Switch1 => state.switch::<Scene1>(),
}
}Auto Trait Implementations§
impl<Action> Freeze for State<Action>
impl<Action> !RefUnwindSafe for State<Action>
impl<Action> !Send for State<Action>
impl<Action> !Sync for State<Action>
impl<Action> Unpin for State<Action>
impl<Action> !UnwindSafe for State<Action>
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