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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use re_types::{
    blueprint::archetypes::TensorSliceSelection,
    datatypes::{TensorDimension, TensorDimensionIndexSelection},
};
use re_ui::UiExt as _;
use re_viewer_context::ViewerContext;
use re_viewport_blueprint::ViewProperty;

#[derive(Clone, Copy, PartialEq, Eq)]
enum DragDropAddress {
    None,
    Width,
    Height,
    Selector(usize),
    NewSelector,
}

impl DragDropAddress {
    fn is_some(&self) -> bool {
        *self != Self::None
    }

    fn read_from_address(
        &self,
        slice_selection: &TensorSliceSelection,
        shape: &[TensorDimension],
    ) -> Option<TensorDimensionIndexSelection> {
        match self {
            Self::None => unreachable!(),
            Self::Width => slice_selection
                .width
                .map(|w| TensorDimensionIndexSelection {
                    dimension: w.dimension,
                    index: shape[w.dimension as usize].size / 2, // Select middle if this becomes index fixed.
                }),
            Self::Height => slice_selection
                .height
                .map(|h| TensorDimensionIndexSelection {
                    dimension: h.dimension,
                    index: shape[h.dimension as usize].size / 2, // Select middle if this becomes index fixed.
                }),
            #[allow(clippy::unwrap_used)]
            Self::Selector(selector_idx) => {
                Some(slice_selection.indices.as_ref().unwrap()[*selector_idx].0)
            }
            Self::NewSelector => None,
        }
    }

    fn write_to_address(
        &self,
        ctx: &ViewerContext<'_>,
        slice_selection: &TensorSliceSelection,
        slice_property: &ViewProperty,
        new_selection: Option<TensorDimensionIndexSelection>,
    ) {
        match self {
            Self::None => unreachable!(),
            Self::Width => {
                let width = new_selection.map(|new_selection| {
                    let mut width = slice_selection.width.unwrap_or_default();
                    width.dimension = new_selection.dimension;
                    width
                });
                slice_property.save_blueprint_component(ctx, &width);
            }
            Self::Height => {
                let height = new_selection.map(|new_selection| {
                    let mut height = slice_selection.height.unwrap_or_default();
                    height.dimension = new_selection.dimension;
                    height
                });
                slice_property.save_blueprint_component(ctx, &height);
            }
            Self::Selector(selector_idx) => {
                let mut indices = slice_selection.indices.clone().unwrap_or_default();
                let mut slider = slice_selection.slider.clone().unwrap_or_default();
                if let Some(new_selection) = new_selection {
                    indices[*selector_idx] = new_selection.into();
                    slider.push(new_selection.dimension.into()); // Enable slider by default.
                } else {
                    let removed_dim = indices[*selector_idx].dimension;
                    slider.retain(|s| s.dimension != removed_dim); // purge slider if there was any.
                    indices.remove(*selector_idx);
                }
                slice_property.save_blueprint_component(ctx, &indices);
                slice_property.save_blueprint_component(ctx, &slider);
            }
            Self::NewSelector => {
                // NewSelector can only be a drop *target*, therefore dim_idx can't be None!
                if let Some(new_selection) = new_selection {
                    let mut indices = slice_selection.indices.clone().unwrap_or_default();
                    let mut slider = slice_selection.slider.clone().unwrap_or_default();
                    indices.push(new_selection.into());
                    slider.push(new_selection.dimension.into()); // Enable slider by default.
                    slice_property.save_blueprint_component(ctx, &indices);
                    slice_property.save_blueprint_component(ctx, &slider);
                }
            }
        };
    }
}

fn drag_source_ui_id(drag_context_id: egui::Id, dim_idx: u32) -> egui::Id {
    drag_context_id.with("tensor_dimension_ui").with(dim_idx)
}

#[allow(clippy::too_many_arguments)]
fn tensor_dimension_ui(
    ui: &mut egui::Ui,
    drag_context_id: egui::Id,
    bound_dim_idx: Option<u32>,
    location: DragDropAddress,
    shape: &[TensorDimension],
    drag_source: &mut DragDropAddress,
    drop_target: &mut DragDropAddress,
) {
    let frame = egui::Frame::default().inner_margin(4.0);

    let (_response, dropped) = ui.dnd_drop_zone::<DragDropAddress, _>(frame, |ui| {
        ui.set_min_size(egui::vec2(80., 15.));

        if let Some(dim_idx) = bound_dim_idx {
            let dim = &shape[dim_idx as usize];
            let dim_ui_id = drag_source_ui_id(drag_context_id, dim_idx);

            let label_text = if let Some(dim_name) = dim.name.as_ref() {
                format!("▓ {dim_name} ({})", dim.size)
            } else {
                format!("▓ {dim_idx} ({})", dim.size)
            };

            ui.dnd_drag_source(dim_ui_id, location, |ui| {
                // TODO(emilk): make these buttons respond on hover.
                ui.colored_label(ui.visuals().widgets.inactive.fg_stroke.color, label_text);
            });
        }
    });

    if let Some(dropped) = dropped {
        *drag_source = *dropped;
        *drop_target = location;
    }
}

pub fn dimension_mapping_ui(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    shape: &[TensorDimension],
    slice_selection: &TensorSliceSelection,
    slice_property: &ViewProperty,
) {
    let mut drag_source = DragDropAddress::None; // Drag this…
    let mut drop_target = DragDropAddress::None; // …onto this.

    let drag_context_id = ui.id();

    ui.vertical(|ui| {
        ui.vertical(|ui| {
            ui.label("Image");
            egui::Grid::new("imagegrid").num_columns(2).show(ui, |ui| {
                tensor_dimension_ui(
                    ui,
                    drag_context_id,
                    slice_selection.width.map(|w| w.dimension),
                    DragDropAddress::Width,
                    shape,
                    &mut drag_source,
                    &mut drop_target,
                );
                ui.horizontal(|ui| {
                    if let Some(mut width) = slice_selection.width {
                        if ui.toggle_value(&mut width.invert, "Flip").changed() {
                            slice_property.save_blueprint_component(ctx, &width);
                        }
                    }
                    ui.label("width");
                });
                ui.end_row();

                tensor_dimension_ui(
                    ui,
                    drag_context_id,
                    slice_selection.height.map(|h| h.dimension),
                    DragDropAddress::Height,
                    shape,
                    &mut drag_source,
                    &mut drop_target,
                );

                ui.horizontal(|ui| {
                    if let Some(mut height) = slice_selection.height {
                        if ui.toggle_value(&mut height.invert, "Flip").changed() {
                            slice_property.save_blueprint_component(ctx, &height);
                        }
                    }
                    ui.label("height");
                });
                ui.end_row();
            });
        });

        ui.add_space(4.0);

        ui.vertical(|ui| {
            ui.label("Selectors");

            let Some(indices) = &slice_selection.indices else {
                return;
            };

            // Use Grid instead of Vertical layout to match styling of the parallel Grid for
            egui::Grid::new("selectiongrid")
                .num_columns(2)
                .show(ui, |ui| {
                    for (selector_idx, selector) in indices.iter().enumerate() {
                        tensor_dimension_ui(
                            ui,
                            drag_context_id,
                            Some(selector.dimension),
                            DragDropAddress::Selector(selector_idx),
                            shape,
                            &mut drag_source,
                            &mut drop_target,
                        );

                        let mut has_slider =
                            slice_selection.slider.as_ref().map_or(false, |slider| {
                                slider
                                    .iter()
                                    .any(|slider| slider.dimension == selector.dimension)
                            });

                        let response = ui.visibility_toggle_button(&mut has_slider);
                        let response = if has_slider {
                            response.on_hover_text("Hide dimension slider")
                        } else {
                            response.on_hover_text("Show dimension slider")
                        };
                        if response.changed() {
                            let mut slider = slice_selection.slider.clone().unwrap_or_default();
                            if has_slider {
                                slider.push(selector.dimension.into());
                            } else {
                                slider.retain(|slider| slider.dimension != selector.dimension);
                            }
                            slice_property.save_blueprint_component(ctx, &slider);
                        }

                        ui.end_row();
                    }
                    // Don't expose `NewSelector` for the moment since it doesn't add any value.
                    // We might need it again though if there is a way to park a selector somewhere else than width/height/selector!
                    if false {
                        tensor_dimension_ui(
                            ui,
                            drag_context_id,
                            None,
                            DragDropAddress::NewSelector,
                            shape,
                            &mut drag_source,
                            &mut drop_target,
                        );
                        ui.end_row();
                    }
                });
        });
    });

    // persist drag/drop
    if drag_source.is_some() && drop_target.is_some() {
        let previous_value_source = drag_source.read_from_address(slice_selection, shape);
        let previous_value_target = drop_target.read_from_address(slice_selection, shape);
        drag_source.write_to_address(ctx, slice_selection, slice_property, previous_value_target);
        drop_target.write_to_address(ctx, slice_selection, slice_property, previous_value_source);
    }
}