From c36f30ea947f9fd30f564cbd648e6489b7fe73c6 Mon Sep 17 00:00:00 2001 From: Jack Mac Date: Fri, 15 Oct 2021 16:38:46 -0400 Subject: [PATCH] drag drop changes --- CHANGELOG.markdown | 4 ++- imgui/src/drag_drop.rs | 65 +++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 6815fbb..07d6f45 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -24,7 +24,9 @@ - Added `add_polyline` method to `DrawListMut`, which binds to Dear ImGui's `AddPolyline` and `AddConvexPolyFilled` -- BREAKING: `MenuItem::new` now takes `&ui`, but has been deprecated. Instead, use `ui.menu_item`. Additionally, a `ui.menu_item_config` has been created to access the builder pattern on `MenuItem`. +- BREAKING: The following structs have had their `new` method changed and deprecated; they now also take `ui` in their `new`, but you should create them on the `Ui` struct instead. These each now have a simple and a `_config` version: + - `MenuItem` should be made with `ui.menu_item` and `ui.menu_item_config`. + - `DragDropSource` and `DragDropTarget` should be made with `ui.drag_drop_source_config` and `ui.drag_drop_target`. Both of these methods, and the DragDrop API in general, are likely to change. ## [0.8.0] - 2021-09-17 diff --git a/imgui/src/drag_drop.rs b/imgui/src/drag_drop.rs index 1f209a5..a71dcb7 100644 --- a/imgui/src/drag_drop.rs +++ b/imgui/src/drag_drop.rs @@ -70,6 +70,18 @@ bitflags!( } ); +impl Ui { + /// Creates a new [DragDropSource] with the given name. + pub fn drag_drop_source_config>(&self, name: T) -> DragDropSource<'_, T> { + DragDropSource { + name, + flags: DragDropFlags::empty(), + cond: Condition::Always, + ui: self, + } + } +} + /// Creates a source for drag drop data out of the last ID created. /// /// ```no_run @@ -92,21 +104,22 @@ bitflags!( /// will manage, and then give to a [DragDropTarget], which will received the payload. The /// simplest and safest Payload is the empty payload, created with [begin](Self::begin). #[derive(Debug)] -pub struct DragDropSource { +pub struct DragDropSource<'ui, T> { name: T, flags: DragDropFlags, cond: Condition, + ui: &'ui Ui, } -impl> DragDropSource { - /// Creates a new [DragDropSource] with no flags and the `Condition::Always` with the given name. - /// ImGui refers to this `name` field as a `type`, but really it's just an identifier to match up - /// Source/Target for DragDrop. - pub fn new(name: T) -> Self { +impl<'ui, T: AsRef> DragDropSource<'ui, T> { + /// Creates a new [DragDropSource] with the given name. + #[deprecated(since = "0.9.0", note = "use `ui.drag_drop_source_config` instead")] + pub fn new(name: T, ui: &'ui Ui) -> Self { Self { name, flags: DragDropFlags::empty(), cond: Condition::Always, + ui, } } @@ -175,8 +188,8 @@ impl> DragDropSource { /// If you want to pass a simple integer or other "plain old data", take a look at /// [begin_payload](Self::begin_payload). #[inline] - pub fn begin<'ui>(self, ui: &Ui) -> Option> { - self.begin_payload(ui, ()) + pub fn begin(self) -> Option> { + self.begin_payload(()) } /// Creates the source of a drag and returns a handle on the tooltip. @@ -222,15 +235,13 @@ impl> DragDropSource { /// } /// ``` #[inline] - pub fn begin_payload<'ui, P: Copy + 'static>( + pub fn begin_payload( self, - ui: &Ui, payload: P, ) -> Option> { unsafe { let payload = TypedPayload::new(payload); self.begin_payload_unchecked( - ui, &payload as *const _ as *const ffi::c_void, std::mem::size_of::>(), ) @@ -264,16 +275,15 @@ impl> DragDropSource { /// Overall, users should be very sure that this function is needed before they reach for it, and instead /// should consider either [begin](Self::begin) or [begin_payload](Self::begin_payload). #[inline] - pub unsafe fn begin_payload_unchecked<'ui>( + pub unsafe fn begin_payload_unchecked( &self, - ui: &Ui, ptr: *const ffi::c_void, size: usize, ) -> Option> { let should_begin = sys::igBeginDragDropSource(self.flags.bits() as i32); if should_begin { - sys::igSetDragDropPayload(ui.scratch_txt(&self.name), ptr, size, self.cond as i32); + sys::igSetDragDropPayload(self.ui.scratch_txt(&self.name), ptr, size, self.cond as i32); Some(DragDropSourceToolTip::push()) } else { @@ -306,6 +316,20 @@ impl Drop for DragDropSourceToolTip<'_> { } } +impl Ui { + /// Creates a new DragDropTarget, which gives methods for handling + /// accepting payloads. + #[doc(alias = "BeginDragDropTarget")] + pub fn drag_drop_target(&self) -> Option> { + let should_begin = unsafe { sys::igBeginDragDropTarget() }; + if should_begin { + Some(DragDropTarget(self)) + } else { + None + } + } +} + /// Creates a target for drag drop data out of the last ID created. /// /// ```no_run @@ -342,17 +366,12 @@ impl Drop for DragDropSourceToolTip<'_> { pub struct DragDropTarget<'ui>(&'ui Ui); impl<'ui> DragDropTarget<'ui> { - /// Creates a new DragDropTarget, holding the [Ui]'s lifetime for the duration - /// of its existence. This is required since this struct runs some code on its Drop - /// to end the DragDropTarget code. + /// Creates a new DragDropTarget, which gives methods for handling + /// accepting payloads. #[doc(alias = "BeginDragDropTarget")] + #[deprecated(since = "0.9.0", note = "Use `ui.drag_drop_taget() instead")] pub fn new(ui: &'ui Ui) -> Option { - let should_begin = unsafe { sys::igBeginDragDropTarget() }; - if should_begin { - Some(Self(ui)) - } else { - None - } + ui.drag_drop_target() } /// Accepts an empty payload. This is the safest option for raising named events