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::{
    types::{Rect, Vector2},
    ui::{Drag, DragState},
    Id, Layout, Ui,
};

#[derive(Debug, Clone)]
pub struct Group {
    id: Id,
    position: Option<Vector2>,
    size: Vector2,
    draggable: bool,
    highlight: bool,
    hoverable: bool,
}

impl Group {
    pub fn new(id: Id, size: Vector2) -> Group {
        Group {
            id,
            size,
            position: None,
            draggable: false,
            highlight: false,
            hoverable: false,
        }
    }

    pub fn position(self, position: Vector2) -> Group {
        Group {
            position: Some(position),
            ..self
        }
    }

    pub fn draggable(self, draggable: bool) -> Group {
        Group { draggable, ..self }
    }

    pub fn hoverable(self, hoverable: bool) -> Group {
        Group { hoverable, ..self }
    }

    pub fn highlight(self, highlight: bool) -> Group {
        Group { highlight, ..self }
    }

    pub fn ui<F: FnOnce(&mut Ui)>(self, ui: &mut Ui, f: F) -> Drag {
        let mut drag = Drag::No;

        let parent = ui.get_active_window_context();

        let parent_rect = parent.window.content_rect();

        parent.window.childs.push(self.id);

        let pos = parent.window.cursor.fit(
            self.size,
            self.position.map_or(Layout::Horizontal, Layout::Free),
        );
        let rect = Rect::new(pos.x, pos.y, self.size.x, self.size.y);
        let parent_id = Some(parent.window.id);

        let mut context = ui.begin_window(self.id, parent_id, pos, self.size, 0., true);

        let hovered =
            (self.hoverable || self.draggable) && rect.contains(context.input.mouse_position);

        if self.draggable && context.dragging.is_none() && hovered && context.input.click_down {
            *context.dragging = Some((self.id, DragState::Clicked(context.input.mouse_position)));
        }

        if let Some((id, DragState::Clicked(orig))) = context.dragging {
            if *id == self.id
                && context.input.is_mouse_down
                && context.input.mouse_position.distance(*orig) > 5.
            {
                *context.dragging = Some((self.id, DragState::Dragging(*orig)));
            }
            if context.input.is_mouse_down == false {
                *context.dragging = None;
            }
        }

        if let Some((id, DragState::Dragging(_))) = context.dragging {
            let id = *id;

            if id == self.id {
                drag = Drag::Dragging;

                if context.input.is_mouse_down == false {
                    *context.dragging = None;
                    drag = Drag::Dropped(context.input.mouse_position, *context.drag_hovered_previous_frame);
                }
            }

            if id != self.id && hovered {
                *context.drag_hovered = Some(self.id);
            }
        }

	context.window.draw_commands.clip(parent_rect);

	context.scroll_area();

        let clip_rect = context.window.content_rect();
        context.window.draw_commands.clip(clip_rect);
        context.window.draw_commands.draw_rect(
            rect,
            context
                .global_style
                .drag_border(context.focused, hovered, self.highlight),
            None,
        );

        f(ui);

        let context = ui.get_active_window_context();

        context.window.draw_commands.clip(None);

        if context.focused && self.draggable {
            if
            //parent.dragging.is_none()
            context.input.is_mouse_down
                && Rect::new(pos.x, pos.y, self.size.x, self.size.y)
                    .contains(context.input.mouse_position)
            {
                // *context.dragging = Some((
                //     id,
                //     DragState::Clicked(context.input.mouse_position, Vector2::new(rect.x, rect.y)),
                // ));
            }
        }

        ui.end_window();

        drag
    }
}

impl Ui {
    pub fn group<F: FnOnce(&mut Ui)>(&mut self, id: Id, size: Vector2, f: F) -> Drag {
        Group::new(id, size).ui(self, f)
    }
}