mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
format apps/ball
This commit is contained in:
parent
faafa9078b
commit
145e156fe6
2 changed files with 76 additions and 47 deletions
|
|
@ -1,13 +1,14 @@
|
|||
use super::*;
|
||||
use gam::*;
|
||||
use gam::menu::*;
|
||||
use gam::menu::api::DrawStyle;
|
||||
use gam::menu::*;
|
||||
use gam::*;
|
||||
use locales::t;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum BallMode {
|
||||
Random,
|
||||
Tilt
|
||||
Tilt,
|
||||
}
|
||||
|
||||
const BALL_RADIUS: i16 = 10;
|
||||
|
|
@ -32,25 +33,35 @@ impl Ball {
|
|||
let xns = xous_names::XousNames::new().expect("couldn't connect to Xous Namespace Server");
|
||||
let gam = gam::Gam::new(&xns).expect("can't connect to Graphical Abstraction Manager");
|
||||
|
||||
let token = gam.register_ux(UxRegistration {
|
||||
app_name: xous_ipc::String::<128>::from_str(gam::APP_NAME_BALL),
|
||||
ux_type: gam::UxType::Framebuffer,
|
||||
predictor: None,
|
||||
listener: sid.to_array(), // note disclosure of our SID to the GAM -- the secret is now shared with the GAM!
|
||||
redraw_id: AppOp::Redraw.to_u32().unwrap(),
|
||||
gotinput_id: None,
|
||||
audioframe_id: None,
|
||||
focuschange_id: Some(AppOp::FocusChange.to_u32().unwrap()),
|
||||
rawkeys_id: Some(AppOp::Rawkeys.to_u32().unwrap()),
|
||||
}).expect("couldn't register Ux context for shellchat");
|
||||
let token = gam
|
||||
.register_ux(UxRegistration {
|
||||
app_name: xous_ipc::String::<128>::from_str(gam::APP_NAME_BALL),
|
||||
ux_type: gam::UxType::Framebuffer,
|
||||
predictor: None,
|
||||
listener: sid.to_array(), /* note disclosure of our SID to the GAM -- the secret is now
|
||||
* shared with the GAM! */
|
||||
redraw_id: AppOp::Redraw.to_u32().unwrap(),
|
||||
gotinput_id: None,
|
||||
audioframe_id: None,
|
||||
focuschange_id: Some(AppOp::FocusChange.to_u32().unwrap()),
|
||||
rawkeys_id: Some(AppOp::Rawkeys.to_u32().unwrap()),
|
||||
})
|
||||
.expect("couldn't register Ux context for shellchat");
|
||||
|
||||
let gid = gam.request_content_canvas(token.unwrap()).expect("couldn't get content canvas");
|
||||
let screensize = gam.get_canvas_bounds(gid).expect("couldn't get dimensions of content canvas");
|
||||
|
||||
gam.draw_rectangle(gid,
|
||||
Rectangle::new_coords_with_style(0, 0, screensize.x, screensize.y,
|
||||
DrawStyle::new(PixelColor::Light, PixelColor::Dark, 2))
|
||||
).expect("couldn't draw our rectangle");
|
||||
gam.draw_rectangle(
|
||||
gid,
|
||||
Rectangle::new_coords_with_style(
|
||||
0,
|
||||
0,
|
||||
screensize.x,
|
||||
screensize.y,
|
||||
DrawStyle::new(PixelColor::Light, PixelColor::Dark, 2),
|
||||
),
|
||||
)
|
||||
.expect("couldn't draw our rectangle");
|
||||
|
||||
let trng = trng::Trng::new(&xns).unwrap();
|
||||
let x = ((trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||
|
|
@ -74,6 +85,7 @@ impl Ball {
|
|||
com,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update(&mut self) {
|
||||
// send a list of objects to draw to the GAM, to avoid race conditions in between operations
|
||||
let mut draw_list = GamObjectList::new(self.gid);
|
||||
|
|
@ -90,10 +102,7 @@ impl Ball {
|
|||
// positive x => tilt to left
|
||||
// negative y => tilt toward top
|
||||
// positive y => tilt toward bottom
|
||||
self.momentum = Point::new(
|
||||
-(ix / 200),
|
||||
iy / 200
|
||||
);
|
||||
self.momentum = Point::new(-(ix / 200), iy / 200);
|
||||
}
|
||||
// update the ball position based on the momentum vector
|
||||
self.ball.translate(self.momentum);
|
||||
|
|
@ -124,7 +133,7 @@ impl Ball {
|
|||
let mut x = ((self.trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||
let mut y = ((self.trng.get_u32().unwrap() / 2) as i32) % (MOMENTUM_LIMIT * 2) - MOMENTUM_LIMIT;
|
||||
if hit_right {
|
||||
x = - x.abs();
|
||||
x = -x.abs();
|
||||
}
|
||||
if hit_left {
|
||||
x = x.abs();
|
||||
|
|
@ -133,7 +142,7 @@ impl Ball {
|
|||
y = y.abs();
|
||||
}
|
||||
if hit_bott {
|
||||
y = - y.abs();
|
||||
y = -y.abs();
|
||||
}
|
||||
self.momentum = Point::new(x as i16, y as i16);
|
||||
}
|
||||
|
|
@ -145,22 +154,35 @@ impl Ball {
|
|||
log::trace!("ball app redraw##");
|
||||
self.gam.redraw().unwrap();
|
||||
}
|
||||
|
||||
pub(crate) fn focus(&mut self) {
|
||||
// draw the background entirely
|
||||
self.gam.draw_rectangle(self.gid,
|
||||
Rectangle::new_coords_with_style(0, 0, self.screensize.x, self.screensize.y,
|
||||
DrawStyle::new(PixelColor::Light, PixelColor::Dark, BORDER_WIDTH))
|
||||
).expect("couldn't draw our rectangle");
|
||||
self.gam
|
||||
.draw_rectangle(
|
||||
self.gid,
|
||||
Rectangle::new_coords_with_style(
|
||||
0,
|
||||
0,
|
||||
self.screensize.x,
|
||||
self.screensize.y,
|
||||
DrawStyle::new(PixelColor::Light, PixelColor::Dark, BORDER_WIDTH),
|
||||
),
|
||||
)
|
||||
.expect("couldn't draw our rectangle");
|
||||
}
|
||||
|
||||
pub(crate) fn rawkeys(&mut self, keys: [char; 4]) {
|
||||
log::debug!("got rawkey {:?}", keys); // you could use the raw keypresses, but modals are easier...
|
||||
let mut note = String::new();
|
||||
use std::fmt::Write;
|
||||
write!(note, "{}'{}'.\n\n{}",
|
||||
write!(
|
||||
note,
|
||||
"{}'{}'.\n\n{}",
|
||||
t!("ballapp.notification_a", locales::LANG),
|
||||
keys[0],
|
||||
t!("ballapp.notification_b", locales::LANG),
|
||||
).unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
self.modals.show_notification(¬e, None).unwrap();
|
||||
self.modals.add_list_item(t!("ballapp.random", locales::LANG)).unwrap();
|
||||
self.modals.add_list_item(t!("ballapp.tilt", locales::LANG)).unwrap();
|
||||
|
|
@ -191,21 +213,24 @@ pub(crate) fn ball_pump_thread(cid_to_main: xous::CID, pump_sid: xous::SID) {
|
|||
run = true;
|
||||
xous::send_message(
|
||||
cid_to_self,
|
||||
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't pump the main loop event thread");
|
||||
},
|
||||
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
||||
)
|
||||
.expect("couldn't pump the main loop event thread");
|
||||
}
|
||||
Some(PumpOp::Stop) => run = false,
|
||||
Some(PumpOp::Pump) => {
|
||||
xous::send_message(
|
||||
cid_to_main,
|
||||
Message::new_blocking_scalar(AppOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't pump the main loop event thread");
|
||||
Message::new_blocking_scalar(AppOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
||||
)
|
||||
.expect("couldn't pump the main loop event thread");
|
||||
if run {
|
||||
tt.sleep_ms(BALL_UPDATE_RATE_MS).unwrap();
|
||||
xous::send_message(
|
||||
cid_to_self,
|
||||
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't pump the main loop event thread");
|
||||
Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0),
|
||||
)
|
||||
.expect("couldn't pump the main loop event thread");
|
||||
}
|
||||
}
|
||||
Some(PumpOp::Quit) => {
|
||||
|
|
|
|||
|
|
@ -65,7 +65,8 @@ fn main() -> ! {
|
|||
ball.update();
|
||||
}
|
||||
}
|
||||
Some(AppOp::Pump) => { // this is a blocking scalar so that pump calls don't initiate faster than the app can draw
|
||||
Some(AppOp::Pump) => {
|
||||
// this is a blocking scalar so that pump calls don't initiate faster than the app can draw
|
||||
if into_allow_redraw {
|
||||
ball.focus();
|
||||
into_allow_redraw = false;
|
||||
|
|
@ -92,29 +93,32 @@ fn main() -> ! {
|
|||
allow_redraw = false; // this instantly terminates future updates, even if Pump messages are in our input queue
|
||||
xous::send_message(
|
||||
cid_to_pump,
|
||||
Message::new_scalar(PumpOp::Stop.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't send stop message to the pump thread");
|
||||
Message::new_scalar(PumpOp::Stop.to_usize().unwrap(), 0, 0, 0, 0),
|
||||
)
|
||||
.expect("couldn't send stop message to the pump thread");
|
||||
}
|
||||
gam::FocusState::Foreground => {
|
||||
into_allow_redraw = true;
|
||||
allow_redraw = true;
|
||||
xous::send_message(
|
||||
cid_to_pump,
|
||||
Message::new_scalar(PumpOp::Run.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't send run message to the pump thread");
|
||||
Message::new_scalar(PumpOp::Run.to_usize().unwrap(), 0, 0, 0, 0),
|
||||
)
|
||||
.expect("couldn't send run message to the pump thread");
|
||||
}
|
||||
}
|
||||
}),
|
||||
Some(AppOp::Quit) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
||||
xous::send_message(
|
||||
cid_to_pump,
|
||||
Message::new_blocking_scalar(PumpOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't send run message to the pump thread");
|
||||
unsafe{xous::disconnect(cid_to_pump).ok()};
|
||||
Message::new_blocking_scalar(PumpOp::Quit.to_usize().unwrap(), 0, 0, 0, 0),
|
||||
)
|
||||
.expect("couldn't send run message to the pump thread");
|
||||
unsafe { xous::disconnect(cid_to_pump).ok() };
|
||||
xous::return_scalar(msg.sender, 1).expect("couldn't acknowledge quit message");
|
||||
break;
|
||||
}),
|
||||
_ => log::error!("couldn't convert opcode: {:?}", msg)
|
||||
_ => log::error!("couldn't convert opcode: {:?}", msg),
|
||||
}
|
||||
}
|
||||
// clean up our program
|
||||
|
|
|
|||
Loading…
Reference in a new issue