-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_phase_ui.rs
182 lines (165 loc) · 4.97 KB
/
custom_phase_ui.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
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
172
173
174
175
176
177
178
179
180
181
182
use crate::advance_ui::{show_advance_menu, AdvanceState};
use crate::client_state::{ActiveDialog, StateUpdate};
use crate::dialog_ui::{cancel_button_with_tooltip, ok_button, OkTooltip};
use crate::hex_ui::Point;
use crate::layout_ui::{bottom_center_anchor, icon_pos};
use crate::payment_ui::{multi_payment_dialog, payment_dialog, Payment};
use crate::player_ui::choose_player_dialog;
use crate::render_context::RenderContext;
use crate::select_ui::ConfirmSelection;
use crate::unit_ui;
use crate::unit_ui::{draw_unit_type, UnitHighlightType, UnitSelection};
use macroquad::math::vec2;
use server::action::Action;
use server::content::custom_phase_actions::{
AdvanceRewardRequest, CustomPhaseEventAction, PlayerRequest, UnitTypeRequest,
};
use server::game::Game;
use server::unit::Unit;
pub fn custom_phase_payment_dialog(rc: &RenderContext, payments: &[Payment]) -> StateUpdate {
multi_payment_dialog(
rc,
payments,
|p| ActiveDialog::PaymentRequest(p.clone()),
false,
|p| {
StateUpdate::Execute(Action::CustomPhaseEvent(CustomPhaseEventAction::Payment(
p.clone(),
)))
},
)
}
pub fn payment_reward_dialog(rc: &RenderContext, payment: &Payment) -> StateUpdate {
payment_dialog(
rc,
payment,
false,
|p| ActiveDialog::ResourceRewardRequest(p.clone()),
|p| {
StateUpdate::Execute(Action::CustomPhaseEvent(
CustomPhaseEventAction::ResourceReward(p),
))
},
)
}
pub fn advance_reward_dialog(
rc: &RenderContext,
r: &AdvanceRewardRequest,
name: &str,
) -> StateUpdate {
let possible = &r.choices;
show_advance_menu(
rc,
&format!("Select advance for {name}"),
|a, _| {
if possible.contains(&a.name) {
AdvanceState::Available
} else {
AdvanceState::Unavailable
}
},
|a| {
StateUpdate::Execute(Action::CustomPhaseEvent(
CustomPhaseEventAction::AdvanceReward(a.name.clone()),
))
},
)
}
pub fn unit_request_dialog(rc: &RenderContext, r: &UnitTypeRequest) -> StateUpdate {
let c = &r.choices;
let anchor = bottom_center_anchor(rc) + vec2(0., 60.);
for (i, u) in c.iter().enumerate() {
let x = (c.len() - i) as i8 - 1;
let p = icon_pos(x, -2) + anchor;
if draw_unit_type(
rc,
UnitHighlightType::None,
Point::from_vec2(p),
*u,
r.player_index,
unit_ui::name(u),
20.,
) {
return StateUpdate::Execute(Action::CustomPhaseEvent(
CustomPhaseEventAction::SelectUnitType(*u),
));
}
}
StateUpdate::None
}
#[derive(Clone)]
pub struct UnitsSelection {
pub needed: u8,
pub player: usize,
pub selectable: Vec<u32>,
pub units: Vec<u32>,
pub description: Option<String>,
}
impl UnitsSelection {
pub fn new(
player: usize,
needed: u8,
selectable: Vec<u32>,
description: Option<String>,
) -> Self {
UnitsSelection {
player,
needed,
units: Vec::new(),
selectable,
description,
}
}
}
impl UnitSelection for UnitsSelection {
fn selected_units_mut(&mut self) -> &mut Vec<u32> {
&mut self.units
}
fn can_select(&self, _game: &Game, unit: &Unit) -> bool {
self.selectable.contains(&unit.id)
}
fn player_index(&self) -> usize {
self.player
}
}
impl ConfirmSelection for UnitsSelection {
fn cancel_name(&self) -> Option<&str> {
None
}
fn confirm(&self, _game: &Game) -> OkTooltip {
if self.units.len() as u8 == self.needed {
OkTooltip::Valid("Select units".to_string())
} else {
OkTooltip::Invalid(format!(
"Need to select {} units",
self.needed as i8 - self.units.len() as i8
))
}
}
}
pub fn select_units_dialog(rc: &RenderContext, sel: &UnitsSelection) -> StateUpdate {
unit_ui::unit_selection_dialog::<UnitsSelection>(rc, sel, |new: UnitsSelection| {
StateUpdate::Execute(Action::CustomPhaseEvent(
CustomPhaseEventAction::SelectUnits(new.units.clone()),
))
})
}
pub fn bool_request_dialog(rc: &RenderContext) -> StateUpdate {
if ok_button(rc, OkTooltip::Valid("OK".to_string())) {
return bool_answer(true);
}
if cancel_button_with_tooltip(rc, "Decline") {
return bool_answer(false);
}
StateUpdate::None
}
fn bool_answer(answer: bool) -> StateUpdate {
StateUpdate::Execute(Action::CustomPhaseEvent(CustomPhaseEventAction::Bool(
answer,
)))
}
pub fn player_request_dialog(rc: &RenderContext, r: &PlayerRequest) -> StateUpdate {
choose_player_dialog(rc, &r.choices, |p| {
Action::CustomPhaseEvent(CustomPhaseEventAction::SelectPlayer(p))
})
}