Skip to content

Commit

Permalink
Merge #187
Browse files Browse the repository at this point in the history
187: Flash penalty button r=TristanDebrunner a=elsald

fixes #183 

Co-authored-by: elsald <elsald@icloud.com>
  • Loading branch information
bors[bot] and elsald authored Oct 3, 2023
2 parents 4e1d6d4 + 39bf9f4 commit 627ca9b
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions refbox/src/app/view_builders/main_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use iced::{

use uwh_common::{
config::Game as GameConfig,
game_snapshot::{
Color as GameColor, GamePeriod, GameSnapshot, PenaltySnapshot, TimeoutSnapshot,
},
game_snapshot::{Color as GameColor, GamePeriod, GameSnapshot, PenaltyTime, TimeoutSnapshot},
};

pub(in super::super) fn build_main_view<'a>(
Expand Down Expand Up @@ -78,7 +76,40 @@ pub(in super::super) fn build_main_view<'a>(
.on_press(Message::EditGameConfig),
);

let make_penalty_button = |penalties: &[PenaltySnapshot]| {
let make_penalty_button = |snapshot: &GameSnapshot, color: GameColor| {
let penalties = match color {
GameColor::Black => &snapshot.b_penalties,
GameColor::White => &snapshot.w_penalties,
};

let time = penalties
.iter()
.filter_map(|penalty| match penalty.time {
PenaltyTime::Seconds(s) if s != 0 => Some(s),
PenaltyTime::Seconds(_) => None,
PenaltyTime::TotalDismissal => None,
})
.min();

let make_penalties_red = if snapshot.timeout == TimeoutSnapshot::None {
if let Some(t) = time {
t <= 10 && (t % 2 == 0) && (t != 0)
} else {
false
}
} else {
false
};

let button_style = if make_penalties_red {
style::Button::Red
} else {
match color {
GameColor::Black => style::Button::Black,
GameColor::White => style::Button::White,
}
};

button(
column()
.spacing(SPACING)
Expand All @@ -102,6 +133,7 @@ pub(in super::super) fn build_main_view<'a>(
.width(Length::Fill)
.height(Length::Fill)
.on_press(Message::PenaltyOverview)
.style(button_style)
};

let mut black_score_btn = button(
Expand Down Expand Up @@ -147,15 +179,15 @@ pub(in super::super) fn build_main_view<'a>(
.width(Length::Fill)
.push(black_score_btn)
.push(black_new_score_btn)
.push(make_penalty_button(&snapshot.b_penalties).style(style::Button::Black));
.push(make_penalty_button(snapshot, GameColor::Black));

let white_col = column()
.spacing(SPACING)
.align_items(Alignment::Center)
.width(Length::Fill)
.push(white_score_btn)
.push(white_new_score_btn)
.push(make_penalty_button(&snapshot.w_penalties).style(style::Button::White));
.push(make_penalty_button(snapshot, GameColor::White));

row()
.spacing(0)
Expand Down

0 comments on commit 627ca9b

Please sign in to comment.