Skip to content

Commit 6b34aeb

Browse files
authored
Cleanup, revolution (#173)
1 parent 856e152 commit 6b34aeb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2005
-355
lines changed

client/src/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn render_active_dialog(rc: &RenderContext) -> StateUpdate {
164164
ActiveDialog::UnitTypeRequest(r) => custom_phase_ui::unit_request_dialog(rc, r),
165165
ActiveDialog::UnitsRequest(r) => custom_phase_ui::select_units_dialog(rc, r),
166166
ActiveDialog::StructuresRequest(r) => custom_phase_ui::select_structures_dialog(rc, r),
167-
ActiveDialog::BoolRequest => custom_phase_ui::bool_request_dialog(rc),
167+
ActiveDialog::BoolRequest(d) => custom_phase_ui::bool_request_dialog(rc, d),
168168
ActiveDialog::PositionRequest(r) => custom_phase_ui::position_request_dialog(rc, r),
169169
}
170170
}

client/src/client_state.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::collect_ui::CollectResources;
44
use crate::construct_ui::ConstructionPayment;
55
use crate::custom_phase_ui::{MultiSelection, UnitsSelection};
66
use crate::dialog_ui::{BaseOrCustomAction, BaseOrCustomDialog};
7-
use crate::event_ui::{custom_phase_event_help, event_help, pay_help};
7+
use crate::event_ui::{custom_phase_event_help, custom_phase_event_origin, event_help, pay_help};
88
use crate::happiness_ui::IncreaseHappinessConfig;
99
use crate::layout_ui::FONT_SIZE;
1010
use crate::map_ui::ExploreResolutionConfig;
@@ -18,7 +18,7 @@ use server::action::Action;
1818
use server::city::{City, MoodState};
1919
use server::content::custom_phase_actions::{
2020
AdvanceRequest, ChangeGovernmentRequest, CurrentEventRequest, CurrentEventResponse,
21-
PlayerRequest, SelectedStructure, UnitTypeRequest,
21+
CurrentEventType, PlayerRequest, SelectedStructure, UnitTypeRequest,
2222
};
2323
use server::events::EventOrigin;
2424
use server::game::{Game, GameState};
@@ -57,7 +57,7 @@ pub enum ActiveDialog {
5757
UnitTypeRequest(UnitTypeRequest),
5858
UnitsRequest(UnitsSelection),
5959
StructuresRequest(MultiSelection<SelectedStructure>),
60-
BoolRequest,
60+
BoolRequest(String),
6161
ChangeGovernmentType(ChangeGovernmentRequest),
6262
ChooseAdditionalAdvances(ChooseAdditionalAdvances),
6363
}
@@ -94,7 +94,7 @@ impl ActiveDialog {
9494
ActiveDialog::UnitTypeRequest(_) => "custom phase unit request",
9595
ActiveDialog::UnitsRequest(_) => "custom phase units request",
9696
ActiveDialog::StructuresRequest(_) => "custom phase structures request",
97-
ActiveDialog::BoolRequest => "custom phase bool request",
97+
ActiveDialog::BoolRequest(_) => "custom phase bool request",
9898
}
9999
}
100100

@@ -151,19 +151,19 @@ impl ActiveDialog {
151151
}
152152
ActiveDialog::ResourceRewardRequest(_)
153153
| ActiveDialog::AdvanceRequest(_)
154-
| ActiveDialog::PaymentRequest(_)
155-
| ActiveDialog::BoolRequest => custom_phase_event_help(rc, None),
156-
ActiveDialog::UnitTypeRequest(r) => custom_phase_event_help(rc, r.description.as_ref()),
154+
| ActiveDialog::PaymentRequest(_) => {
155+
event_help(rc, &custom_phase_event_origin(rc), true)
156+
}
157+
ActiveDialog::BoolRequest(d) => custom_phase_event_help(rc, d),
158+
ActiveDialog::UnitTypeRequest(r) => custom_phase_event_help(rc, &r.description),
157159
ActiveDialog::UnitsRequest(r) => {
158-
custom_phase_event_help(rc, r.selection.request.description.as_ref())
160+
custom_phase_event_help(rc, &r.selection.request.description)
159161
}
160162
ActiveDialog::StructuresRequest(r) => {
161-
custom_phase_event_help(rc, r.request.description.as_ref())
162-
}
163-
ActiveDialog::PositionRequest(r) => {
164-
custom_phase_event_help(rc, r.request.description.as_ref())
163+
custom_phase_event_help(rc, &r.request.description)
165164
}
166-
ActiveDialog::PlayerRequest(r) => custom_phase_event_help(rc, Some(&r.description)),
165+
ActiveDialog::PositionRequest(r) => custom_phase_event_help(rc, &r.request.description),
166+
ActiveDialog::PlayerRequest(r) => custom_phase_event_help(rc, &r.description),
167167
}
168168
}
169169

@@ -535,15 +535,20 @@ impl State {
535535
ActiveDialog::StructuresRequest(MultiSelection::new(r.clone()))
536536
}
537537
CurrentEventRequest::SelectPlayer(r) => ActiveDialog::PlayerRequest(r.clone()),
538-
CurrentEventRequest::BoolRequest => ActiveDialog::BoolRequest,
538+
CurrentEventRequest::BoolRequest(d) => ActiveDialog::BoolRequest(d.clone()),
539539
CurrentEventRequest::ChangeGovernment(r) => {
540540
ActiveDialog::ChangeGovernmentType(r.clone())
541541
}
542-
CurrentEventRequest::ExploreResolution(r) => {
543-
ActiveDialog::ExploreResolution(ExploreResolutionConfig {
544-
block: r.block.clone(),
545-
rotation: r.block.position.rotation,
546-
})
542+
CurrentEventRequest::ExploreResolution => {
543+
if let CurrentEventType::ExploreResolution(r) = &game.current_event().event_type
544+
{
545+
ActiveDialog::ExploreResolution(ExploreResolutionConfig {
546+
block: r.block.clone(),
547+
rotation: r.block.position.rotation,
548+
})
549+
} else {
550+
panic!("invalid state");
551+
}
547552
}
548553
};
549554
}

client/src/custom_phase_ui.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub fn advance_reward_dialog(rc: &RenderContext, r: &AdvanceRequest, name: &str)
6262
}
6363

6464
pub fn unit_request_dialog(rc: &RenderContext, r: &UnitTypeRequest) -> StateUpdate {
65+
bottom_centered_text(rc, &r.description);
66+
6567
let c = &r.choices;
6668
let anchor = bottom_center_anchor(rc) + vec2(0., 60.);
6769
for (i, u) in c.iter().enumerate() {
@@ -118,7 +120,12 @@ impl UnitSelection for UnitsSelection {
118120
pub fn select_units_dialog(rc: &RenderContext, s: &UnitsSelection) -> StateUpdate {
119121
bottom_centered_text(
120122
rc,
121-
format!("{} units selected", s.selection.selected.len()).as_str(),
123+
format!(
124+
"{}: {} units selected",
125+
s.selection.request.description,
126+
s.selection.selected.len()
127+
)
128+
.as_str(),
122129
);
123130

124131
if ok_button(
@@ -175,7 +182,12 @@ pub fn select_structures_dialog(
175182
) -> StateUpdate {
176183
bottom_centered_text(
177184
rc,
178-
format!("{} structures selected", s.selected.len()).as_str(),
185+
format!(
186+
"{}: {} structures selected",
187+
s.request.description,
188+
s.selected.len()
189+
)
190+
.as_str(),
179191
);
180192

181193
if ok_button(
@@ -204,7 +216,8 @@ fn multi_select_tooltip<T: Clone>(s: &MultiSelection<T>, valid: bool, name: &str
204216
}
205217
}
206218

207-
pub fn bool_request_dialog(rc: &RenderContext) -> StateUpdate {
219+
pub fn bool_request_dialog(rc: &RenderContext, description: &str) -> StateUpdate {
220+
bottom_centered_text(rc, description);
208221
if ok_button(rc, OkTooltip::Valid("OK".to_string())) {
209222
return bool_answer(true);
210223
}
@@ -248,7 +261,7 @@ pub(crate) fn position_request_dialog(
248261
) -> StateUpdate {
249262
bottom_centered_text(
250263
rc,
251-
format!("{} positions selected", s.selected.len()).as_str(),
264+
format!("{}: {} selected", s.request.description, s.selected.len()).as_str(),
252265
);
253266
if ok_button(rc, multi_select_tooltip(s, s.is_valid(), "positions")) {
254267
StateUpdate::response(CurrentEventResponse::SelectPositions(s.selected.clone()))

client/src/event_ui.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ pub fn event_help(rc: &RenderContext, origin: &EventOrigin, do_break: bool) -> V
4848
}
4949

5050
#[must_use]
51-
pub fn custom_phase_event_help(rc: &RenderContext, description: Option<&String>) -> Vec<String> {
51+
pub fn custom_phase_event_help(rc: &RenderContext, description: &str) -> Vec<String> {
5252
let mut h = event_help(rc, &custom_phase_event_origin(rc), true);
53-
if let Some(d) = description {
54-
h.push(d.clone());
55-
}
53+
h.push(description.to_string());
5654
h
5755
}
5856

client/src/recruit_unit_ui.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use server::unit::{Unit, UnitType, Units};
88

99
use crate::client_state::{ActiveDialog, StateUpdate};
1010
use crate::construct_ui::{ConstructionPayment, ConstructionProject};
11-
use crate::dialog_ui::OkTooltip;
11+
use crate::dialog_ui::{cancel_button, ok_button, OkTooltip};
1212
use crate::render_context::RenderContext;
13-
use crate::select_ui::{ConfirmSelection, CountSelector, HasCountSelectableObject, HighlightType};
13+
use crate::select_ui::{CountSelector, HasCountSelectableObject, HighlightType};
1414
use crate::unit_ui::{draw_unit_type, UnitSelection};
1515
use crate::{select_ui, unit_ui};
1616

@@ -181,24 +181,8 @@ impl RecruitSelection {
181181
pub fn is_finished(&self) -> bool {
182182
self.need_replacement.is_empty()
183183
}
184-
}
185-
186-
impl UnitSelection for RecruitSelection {
187-
fn selected_units_mut(&mut self) -> &mut Vec<u32> {
188-
&mut self.replaced_units
189-
}
190-
191-
fn can_select(&self, _game: &Game, unit: &Unit) -> bool {
192-
self.need_replacement.has_unit(&unit.unit_type)
193-
}
194-
195-
fn player_index(&self) -> usize {
196-
self.player
197-
}
198-
}
199184

200-
impl ConfirmSelection for RecruitSelection {
201-
fn confirm(&self, game: &Game) -> OkTooltip {
185+
pub fn confirm(&self, game: &Game) -> OkTooltip {
202186
if recruit_cost(
203187
game.get_player(self.amount.player_index),
204188
&self.amount.units,
@@ -216,6 +200,20 @@ impl ConfirmSelection for RecruitSelection {
216200
}
217201
}
218202

203+
impl UnitSelection for RecruitSelection {
204+
fn selected_units_mut(&mut self) -> &mut Vec<u32> {
205+
&mut self.replaced_units
206+
}
207+
208+
fn can_select(&self, _game: &Game, unit: &Unit) -> bool {
209+
self.need_replacement.has_unit(&unit.unit_type)
210+
}
211+
212+
fn player_index(&self) -> usize {
213+
self.player
214+
}
215+
}
216+
219217
pub fn select_dialog(rc: &RenderContext, a: &RecruitAmount) -> StateUpdate {
220218
let game = rc.game;
221219
select_ui::count_dialog(
@@ -311,7 +309,11 @@ fn update_selection(
311309
}
312310

313311
pub fn replace_dialog(rc: &RenderContext, sel: &RecruitSelection) -> StateUpdate {
314-
unit_ui::unit_selection_dialog::<RecruitSelection>(rc, sel, |new: RecruitSelection| {
315-
open_dialog(rc, new.amount.city_position, new)
316-
})
312+
if ok_button(rc, sel.confirm(rc.game)) {
313+
open_dialog(rc, sel.amount.city_position, sel.clone())
314+
} else if cancel_button(rc) {
315+
StateUpdate::Cancel
316+
} else {
317+
StateUpdate::None
318+
}
317319
}

client/src/select_ui.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::client_state::StateUpdate;
2-
use crate::dialog_ui::{cancel_button, cancel_button_with_tooltip, ok_button, OkTooltip};
2+
use crate::dialog_ui::{cancel_button, ok_button, OkTooltip};
33
use crate::layout_ui::{bottom_center_anchor, bottom_center_texture, ICON_SIZE};
44
use crate::render_context::RenderContext;
55
use macroquad::color::{Color, BLACK, BLUE, WHITE};
66
use macroquad::math::{bool, vec2, Vec2};
77
use macroquad::prelude::TextParams;
88
use macroquad::text::draw_text_ex;
9-
use server::game::Game;
109

1110
#[derive(PartialEq, Eq, Debug, Clone)]
1211
pub struct CountSelector {
@@ -89,26 +88,6 @@ pub fn count_dialog<C, O: HasCountSelectableObject>(
8988
StateUpdate::None
9089
}
9190

92-
pub trait ConfirmSelection: Clone {
93-
fn cancel_name(&self) -> Option<&str> {
94-
Some("Cancel")
95-
}
96-
97-
fn confirm(&self, game: &Game) -> OkTooltip;
98-
}
99-
100-
pub fn may_cancel(sel: &impl ConfirmSelection, rc: &RenderContext) -> StateUpdate {
101-
if let Some(cancel_name) = sel.cancel_name() {
102-
if cancel_button_with_tooltip(rc, cancel_name) {
103-
StateUpdate::Cancel
104-
} else {
105-
StateUpdate::None
106-
}
107-
} else {
108-
StateUpdate::None
109-
}
110-
}
111-
11291
#[derive(Clone, Copy)]
11392
pub enum HighlightType {
11493
None,

client/src/status_phase_ui.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn change_government_type_dialog(
4444
}
4545
show_advance_menu(
4646
rc,
47-
"Change government - or click cancel",
47+
&format!("Change government for {}", r.cost),
4848
|a, p| {
4949
if get_governments()
5050
.iter()

client/src/unit_ui.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use server::unit::{carried_units, MovementRestriction, Unit, UnitType};
77

88
use crate::client_state::{ActiveDialog, StateUpdate};
99
use crate::hex_ui;
10-
use crate::select_ui::{may_cancel, ConfirmSelection, HighlightType};
10+
use crate::select_ui::HighlightType;
1111

12-
use crate::dialog_ui::ok_button;
1312
use crate::layout_ui::{draw_scaled_icon, is_in_circle};
1413
use crate::move_ui::MoveDestination;
1514
use crate::render_context::RenderContext;
@@ -269,18 +268,6 @@ pub fn unit_selection_click<T: UnitSelection + Clone>(
269268
StateUpdate::None
270269
}
271270

272-
pub fn unit_selection_dialog<T: UnitSelection + ConfirmSelection>(
273-
rc: &RenderContext,
274-
sel: &T,
275-
on_ok: impl FnOnce(T) -> StateUpdate,
276-
) -> StateUpdate {
277-
if ok_button(rc, sel.confirm(rc.game)) {
278-
on_ok(sel.clone())
279-
} else {
280-
may_cancel(sel, rc)
281-
}
282-
}
283-
284271
pub fn units_on_tile(game: &Game, pos: Position) -> impl Iterator<Item = (usize, Unit)> + '_ {
285272
game.players.iter().flat_map(move |p| {
286273
p.units.iter().filter_map(move |unit| {

server/src/ability_initializer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ pub(crate) trait AbilityInitializerSetup: Sized {
399399
self,
400400
event: E,
401401
priority: i32,
402-
request: impl Fn(&mut Game, usize, &V) -> bool + 'static + Clone,
402+
request: impl Fn(&mut Game, usize, &V) -> Option<String> + 'static + Clone,
403403
gain_reward: impl Fn(&mut Game, &SelectedChoice<bool, V>) + 'static + Clone,
404404
) -> Self
405405
where
@@ -409,10 +409,10 @@ pub(crate) trait AbilityInitializerSetup: Sized {
409409
event,
410410
priority,
411411
move |game, player_index, _player_name, details| {
412-
request(game, player_index, details).then_some(CurrentEventRequest::BoolRequest)
412+
request(game, player_index, details).map(CurrentEventRequest::BoolRequest)
413413
},
414414
move |game, player_index, player_name, action, request, details| {
415-
if let CurrentEventRequest::BoolRequest = &request {
415+
if let CurrentEventRequest::BoolRequest(_) = &request {
416416
if let CurrentEventResponse::Bool(reward) = action {
417417
gain_reward(
418418
game,

0 commit comments

Comments
 (0)