2022-03-02 17:05:19 +00:00
|
|
|
interface Update {
|
|
|
|
mut fn update(f64) bool;
|
|
|
|
fn display() string;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Clock12Hr {
|
2022-03-04 03:26:45 +00:00
|
|
|
// TODO structure member initialization expressions
|
|
|
|
bool pm, // = true,
|
|
|
|
u8 hours, // = 12,
|
|
|
|
u8 minutes, // = 0,
|
|
|
|
u8 seconds, // = 0,
|
|
|
|
f64 sub, // = 0.0,
|
2022-03-02 17:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Clock12Hr {
|
|
|
|
mut fn tick_seconds() {
|
|
|
|
.seconds = .seconds + 1;
|
|
|
|
if .seconds >= 60 {
|
|
|
|
.seconds = 0;
|
|
|
|
.tick_minutes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mut fn tick_minutes() {
|
|
|
|
.minutes = .minutes + 1;
|
|
|
|
if .minutes >= 60 {
|
|
|
|
.minutes = 0;
|
|
|
|
.tick_hours();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mut fn tick_hours() {
|
|
|
|
.hours = .hours + 1;
|
|
|
|
if .hours >= 13 {
|
|
|
|
.hours = 1;
|
2022-03-04 03:26:45 +00:00
|
|
|
} else { // TODO else-if statements
|
|
|
|
if hours == 12 {
|
|
|
|
.pm = not .pm;
|
|
|
|
}
|
2022-03-02 17:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clock12Hr Update {
|
|
|
|
mut fn update(f64 dt) bool {
|
|
|
|
let mut dirty = false;
|
|
|
|
|
|
|
|
.sub = .sub + dt;
|
|
|
|
while .sub > 1.0 {
|
|
|
|
.sub = .sub - 1.0;
|
|
|
|
.tick_seconds();
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty
|
|
|
|
}
|
|
|
|
|
|
|
|
fn display() string {
|
|
|
|
let pm = if .pm { "PM" } else { "AM" };
|
|
|
|
"{}:{}.{} {}".format(.hours, .minutes, .seconds, pm)
|
|
|
|
}
|
|
|
|
}
|