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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::time::Instant;

use crate::comms::{protocol::Message, viewer::ControlViewerHandle};
use rerun::external::{
    eframe,
    egui::{self, ScrollArea},
    re_log::ResultExt,
    re_ui::{list_item, UiExt},
    re_viewer,
};

#[derive(Default)]
pub struct ControlStates {
    pub last_resource_update: Option<Instant>,
    pub controls_view: ControlsView,
    pub message_kind: ObjectKind,
    pub entity_path: String,
    pub position: (f32, f32, f32),
    pub half_size: (f32, f32, f32),
    pub radius: f32,
    pub dynamic_offset: f32,
    pub dynamic_radius: f32,
}

#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub enum ObjectKind {
    #[default]
    Point3d,
    Box3d,
}

#[derive(Default)]
pub struct ControlsView {
    pub key_sequence: Vec<String>,
}

pub struct Control {
    app: re_viewer::App,
    states: ControlStates,
    handle: ControlViewerHandle,
}

impl eframe::App for Control {
    fn save(&mut self, storage: &mut dyn eframe::Storage) {
        // Store viewer state on disk
        self.app.save(storage);
    }

    /// Called whenever we need repainting, which could be 60 Hz.
    fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
        // First add our panel(s):
        egui::SidePanel::right("Control Panel")
            .default_width(400.0)
            .show(ctx, |ui| {
                ScrollArea::vertical().show(ui, |ui| {
                    self.ui(ui);
                });
            });

        self.app.update(ctx, frame);
    }
}

impl Control {
    pub fn new(app: re_viewer::App, handle: ControlViewerHandle) -> Self {
        Control {
            app,
            states: ControlStates {
                entity_path: "foo".to_string(),
                dynamic_radius: 0.1,
                ..Default::default()
            },
            handle,
        }
    }

    fn ui(&mut self, ui: &mut egui::Ui) {
        ui.spacing_mut().item_spacing.y = 9.0;

        ui.vertical_centered(|ui| {
            ui.strong("Control panel");
        });

        list_item::list_item_scope(ui, "Message properties", |ui| {
            ui.spacing_mut().item_spacing.y = ui.ctx().style().spacing.item_spacing.y;
            ui.section_collapsing_header("Message properties")
                .default_open(true)
                .show(ui, |ui| {
                    ui.horizontal(|ui| {
                        ui.label("Message kind:");
                        ui.drop_down_menu(
                            "kind",
                            format!("{:?}", self.states.message_kind),
                            |ui| {
                                ui.selectable_value(
                                    &mut self.states.message_kind,
                                    ObjectKind::Point3d,
                                    "Point3d",
                                );
                                ui.selectable_value(
                                    &mut self.states.message_kind,
                                    ObjectKind::Box3d,
                                    "Box3d",
                                );
                            },
                        );
                    });

                    ui.horizontal(|ui| {
                        ui.label("Entity path:");
                        ui.text_edit_singleline(&mut self.states.entity_path);
                    });

                    match self.states.message_kind {
                        ObjectKind::Point3d => point_message_ui(ui, &mut self.states),
                        ObjectKind::Box3d => box_message_ui(ui, &mut self.states),
                    }
                });

            ui.horizontal(|ui| {
                if ui.button("Send message").clicked() {
                    match self.states.message_kind {
                        ObjectKind::Point3d => {
                            self.handle
                                .send(Message::Point3d {
                                    path: self.states.entity_path.clone(),
                                    position: self.states.position,
                                    radius: self.states.radius,
                                })
                                .expect("e");
                        }
                        ObjectKind::Box3d => {
                            self.handle
                                .send(Message::Box3d {
                                    path: self.states.entity_path.clone(),
                                    position: self.states.position,
                                    half_size: self.states.half_size,
                                })
                                .expect("b");
                        }
                    }
                }
            });
        });

        list_item::list_item_scope(ui, "dynamic", |ui| {
            ui.spacing_mut().item_spacing.y = ui.ctx().style().spacing.item_spacing.y;
            ui.section_collapsing_header("Dynamic position")
                .default_open(true)
                .show(ui, |ui| {
                    dynamic_control_ui(ui, self.handle.clone(), &mut self.states);
                });
        });
    }
}

fn point_message_ui(ui: &mut egui::Ui, states: &mut ControlStates) {
    position_ui(states, ui);

    ui.horizontal(|ui| {
        ui.label("Radius:");
        ui.add(egui::widgets::DragValue::new(&mut states.radius).speed(0.1));
    });
}

fn box_message_ui(ui: &mut egui::Ui, states: &mut ControlStates) {
    position_ui(states, ui);

    let mut half_size = states.half_size;
    ui.horizontal(|ui| {
        ui.label("Half size:");
        ui.add(egui::widgets::DragValue::new(&mut half_size.0).speed(0.1));
        ui.add(egui::widgets::DragValue::new(&mut half_size.1).speed(0.1));
        ui.add(egui::widgets::DragValue::new(&mut half_size.2).speed(0.1));
    });

    states.half_size = half_size;
}

fn position_ui(states: &mut ControlStates, ui: &mut egui::Ui) {
    let mut position = states.position;

    ui.horizontal(|ui| {
        ui.label("Position:");
        ui.add(egui::widgets::DragValue::new(&mut position.0).speed(0.1));
        ui.add(egui::widgets::DragValue::new(&mut position.1).speed(0.1));
        ui.add(egui::widgets::DragValue::new(&mut position.2).speed(0.1));
    });

    states.position = position;
}

fn dynamic_control_ui(ui: &mut egui::Ui, handle: ControlViewerHandle, states: &mut ControlStates) {
    ui.horizontal(|ui| {
        ui.label("Offset:");
        if ui
            .add(egui::Slider::new(
                &mut states.dynamic_offset,
                0.1_f32..=10_f32,
            ))
            .changed()
        {
            handle
                .send(Message::DynamicPosition {
                    radius: states.dynamic_radius,
                    offset: states.dynamic_offset,
                })
                .warn_on_err_once("Failed to send message");
        }
    });

    ui.horizontal(|ui| {
        ui.label("Radius:");
        if ui
            .add(egui::Slider::new(
                &mut states.dynamic_radius,
                0.1_f32..=1_f32,
            ))
            .changed()
        {
            handle
                .send(Message::DynamicPosition {
                    radius: states.dynamic_radius,
                    offset: states.dynamic_offset,
                })
                .warn_on_err_once("Failed to send message");
        }
    });
}