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
|
use crate::solar_system::SolarSystem;
use crate::fleet::FleetsManager;
use crate::ui::contact::MapContact;
pub struct ContactsListWidget<'f>
{
contacts: Vec<(MapContact, &'f String)>,
selected: &'f mut Option<MapContact>
}
pub struct FocusedContactWidget<'f>
{
contact: Option<MapContact>,
star_system: &'f SolarSystem,
fleets_man: &'f FleetsManager
}
impl<'f> ContactsListWidget<'f>
{
pub fn new(
selected: &'f mut Option<MapContact>,
star_system: &'f SolarSystem,
fleets_man: &'f FleetsManager)
-> Self {
let system_fleets = fleets_man.all_in_system(star_system.id());
let system_bodies = star_system.bodies();
let mut contacts = system_fleets.iter().map(|id| {
let fleet = fleets_man.fleet(*id);
(MapContact::from_fleet(*id), fleet.unwrap().name())
}).collect::<Vec<_>>();
contacts.extend(system_bodies.iter().map(|body| {
(MapContact::from_body(body.id()), body.name())
}));
Self {
contacts: contacts,
selected
}
}
pub fn show_ui(
mut self,
ui: &mut egui::Ui)
-> egui::InnerResponse<Option<MapContact>>
{
self.show_entries_box(ui)
}
fn show_entries_box(
&mut self,
ui: &mut egui::Ui)
-> egui::InnerResponse<Option<MapContact>>
{
let mut ret = None;
egui::Frame::default()
.stroke(egui::Stroke::NONE)
.fill(egui::Color32::from_rgb(48, 48, 48))
.outer_margin(egui::Margin::same(4))
.inner_margin(egui::Margin::same(4))
.show(ui, |frame| {
frame.take_available_width();
frame.set_max_height(frame.available_height() / 2.0);
frame.vertical(|vertical| {
let mut table = egui_extras::TableBuilder::new(vertical)
.striped(true)
.resizable(false)
.auto_shrink(false)
.column(egui_extras::Column::remainder())
.body(
|body| {
let row_height = 20.0;
body.rows(row_height, self.contacts.len(), |mut row| {
let resp = self.show_contact(&mut row);
if resp.is_some() {
ret = resp;
}
});
});
});
ret
})
}
fn show_contact(
&mut self,
row: &mut egui_extras::TableRow)
-> Option<MapContact>
{
let contact = &self.contacts[row.index()];
let selected = self.selected.is_some_and(|sel| { sel == contact.0 });
let mut ret = None;
row.col(|col| {
let resp = col.selectable_label(selected, contact.1);
if resp.double_clicked() {
ret = Some(contact.0);
}else if resp.clicked() {
*self.selected = match selected {
true => None,
false => Some(contact.0)
};
}
});
ret
}
}
impl<'f> FocusedContactWidget<'f>
{
pub fn new(
contact: Option<MapContact>,
star_system: &'f SolarSystem,
fleets_man: &'f FleetsManager)
-> Self {
Self {
contact,
star_system,
fleets_man
}
}
}
impl egui::Widget for FocusedContactWidget<'_>
{
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
ui.vertical(|vertical| {
vertical.label("Focused contact");
vertical.shrink_width_to_current();
egui::Frame::new()
.fill(egui::Color32::from_gray(48))
.stroke(egui::Stroke::NONE)
.inner_margin(egui::Margin::same(2))
.show(vertical,
|frame| {
frame.take_available_width();
match self.contact {
Some(contact) => frame.label(contact.name(self.star_system, self.fleets_man)),
None => frame.label("None")
}
}).inner
}).inner
}
}
|