2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup editorui
|
|
|
|
|
*
|
|
|
|
|
* API for simple creation of grid UIs, supporting typically needed features.
|
2024-01-18 16:10:52 +01:00
|
|
|
* https://developer.blender.org/docs/features/interface/views/grid_views/
|
2022-06-16 11:29:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "BLI_function_ref.hh"
|
|
|
|
|
#include "BLI_map.hh"
|
|
|
|
|
#include "BLI_vector.hh"
|
|
|
|
|
|
UI: Add AbstractView base class for views, unify reconstruction in there
No user visible changes expected.
There's plenty of duplicated code in the grid and the tree view, and I expect
this to become more. This starts the process of unifying these parts, which
should also make it easier to add new views. Complexity in the view classes is
reduced, and some type shenanigans for C compatibility and general view
management can be removed, since there is now a common base type.
For the start this ports some of the view reconstruction, where the view and
its items are compared to the version of itself in the previous redraw, so that
state (highlighted, active, renaming, collapsed, ...) can be preserved.
Notifier listening is also ported.
2022-07-02 21:49:21 +02:00
|
|
|
#include "UI_abstract_view.hh"
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "UI_resources.hh"
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
struct bContext;
|
|
|
|
|
struct uiBlock;
|
UI: Port view item features to base class, merge view item button types
No user visible changes expected.
Merges the tree row and grid tile button types, which were mostly doing
the same things. The idea is that there is a button type for
highlighting, as well as supporting general view item features (e.g.
renaming, drag/drop, etc.). So instead there is a view item button type
now. Also ports view item features like renaming, custom context menus,
drag controllers and drop controllers to `ui::AbstractViewItem` (the new
base class for all view items).
This should be quite an improvement because:
- Merges code that was duplicated over view items.
- Mentioned features (renaming, drag & drop, ...) are much easier to
implement in new view types now. Most of it comes "for free".
- Further features will immediately become availalbe to all views (e.g.
selection).
- Simplifies APIs, there don't have to be functions for individual view
item types anymore.
- View item classes are split and thus less overwhelming visually.
- View item buttons now share all code (drawing, handling, etc.)
- We're soon running out of available button types, this commit merges
two into one.
I was hoping I could do this in multiple smaller commits, but things
were quite intertwined so that would've taken quite some effort.
2022-07-19 16:14:42 +02:00
|
|
|
struct uiButViewItem;
|
2022-06-16 11:29:20 +02:00
|
|
|
struct uiLayout;
|
|
|
|
|
struct View2D;
|
|
|
|
|
|
|
|
|
|
namespace blender::ui {
|
|
|
|
|
|
|
|
|
|
class AbstractGridView;
|
UI: Basic tree-view drag & drop reordering and inserting support
No user visible changes expected, these are just the internal API preparations.
Modifies the Drop API for views so that tree-views can choose to insert items
before, after and into other items.
Note: While there is support for drag-tooltips that can explain how an item
will be inserted, there is no drawing yet like in the Outliner, that indicates
if an item is inserted before, after or into. There is some work on that but
that can be done separately.
Changes:
- Removes `AbstractViewDropTarget` that was shared between tree- and
grid-views, and adds `AbstractTreeViewDropTarget` and
`AbstractGridViewDropTarget`. The tree-view needs specialized handling now,
and although they could share some code still, it's not worth having another
level of inheritance.
- Modifies the drop-target API to use `DragInfo` which contains more info about
the dragging operation than just the `wmDrag`.
- Adds `determine_drop_location()` to the `DropTargetInterface` which drop
targets can use to determine when dropping means inserting before, after or
into.
- Store the block and region in the view. This is needed unfortunately but
shouldn't be an issue since the tree view is recreated on redraws, together
with the block.
- Various smaller tweaks and additions to views as needed.
TODO (outside scope of this change): Increase row height so there is no gap
between tree view items, but keep things visually the same otherwise. This
reduces flickering while dragging.
Pull Request: https://projects.blender.org/blender/blender/pulls/109825
2023-07-11 14:30:26 +02:00
|
|
|
class GridViewItemDropTarget;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
/** \name Grid-View Item Type
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2022-07-18 16:51:57 +02:00
|
|
|
class AbstractGridViewItem : public AbstractViewItem {
|
2022-06-16 11:29:20 +02:00
|
|
|
friend class AbstractGridView;
|
|
|
|
|
friend class GridViewLayoutBuilder;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/** Reference to a string that uniquely identifies this item in the view. */
|
|
|
|
|
StringRef identifier_{};
|
|
|
|
|
|
|
|
|
|
public:
|
2023-07-26 15:08:21 +02:00
|
|
|
/* virtual */ ~AbstractGridViewItem() override = default;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
virtual void build_grid_tile(uiLayout &layout) const = 0;
|
|
|
|
|
|
2023-06-13 18:11:14 +02:00
|
|
|
AbstractGridView &get_view() const;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
AbstractGridViewItem(StringRef identifier);
|
|
|
|
|
|
UI: Port view item features to base class, merge view item button types
No user visible changes expected.
Merges the tree row and grid tile button types, which were mostly doing
the same things. The idea is that there is a button type for
highlighting, as well as supporting general view item features (e.g.
renaming, drag/drop, etc.). So instead there is a view item button type
now. Also ports view item features like renaming, custom context menus,
drag controllers and drop controllers to `ui::AbstractViewItem` (the new
base class for all view items).
This should be quite an improvement because:
- Merges code that was duplicated over view items.
- Mentioned features (renaming, drag & drop, ...) are much easier to
implement in new view types now. Most of it comes "for free".
- Further features will immediately become availalbe to all views (e.g.
selection).
- Simplifies APIs, there don't have to be functions for individual view
item types anymore.
- View item classes are split and thus less overwhelming visually.
- View item buttons now share all code (drawing, handling, etc.)
- We're soon running out of available button types, this commit merges
two into one.
I was hoping I could do this in multiple smaller commits, but things
were quite intertwined so that would've taken quite some effort.
2022-07-19 16:14:42 +02:00
|
|
|
/** See AbstractViewItem::matches(). */
|
2023-07-26 15:08:21 +02:00
|
|
|
/* virtual */ bool matches(const AbstractViewItem &other) const override;
|
UI: Port view item features to base class, merge view item button types
No user visible changes expected.
Merges the tree row and grid tile button types, which were mostly doing
the same things. The idea is that there is a button type for
highlighting, as well as supporting general view item features (e.g.
renaming, drag/drop, etc.). So instead there is a view item button type
now. Also ports view item features like renaming, custom context menus,
drag controllers and drop controllers to `ui::AbstractViewItem` (the new
base class for all view items).
This should be quite an improvement because:
- Merges code that was duplicated over view items.
- Mentioned features (renaming, drag & drop, ...) are much easier to
implement in new view types now. Most of it comes "for free".
- Further features will immediately become availalbe to all views (e.g.
selection).
- Simplifies APIs, there don't have to be functions for individual view
item types anymore.
- View item classes are split and thus less overwhelming visually.
- View item buttons now share all code (drawing, handling, etc.)
- We're soon running out of available button types, this commit merges
two into one.
I was hoping I could do this in multiple smaller commits, but things
were quite intertwined so that would've taken quite some effort.
2022-07-19 16:14:42 +02:00
|
|
|
|
2023-07-26 15:08:21 +02:00
|
|
|
/* virtual */ std::unique_ptr<DropTargetInterface> create_item_drop_target() final;
|
UI: Basic tree-view drag & drop reordering and inserting support
No user visible changes expected, these are just the internal API preparations.
Modifies the Drop API for views so that tree-views can choose to insert items
before, after and into other items.
Note: While there is support for drag-tooltips that can explain how an item
will be inserted, there is no drawing yet like in the Outliner, that indicates
if an item is inserted before, after or into. There is some work on that but
that can be done separately.
Changes:
- Removes `AbstractViewDropTarget` that was shared between tree- and
grid-views, and adds `AbstractTreeViewDropTarget` and
`AbstractGridViewDropTarget`. The tree-view needs specialized handling now,
and although they could share some code still, it's not worth having another
level of inheritance.
- Modifies the drop-target API to use `DragInfo` which contains more info about
the dragging operation than just the `wmDrag`.
- Adds `determine_drop_location()` to the `DropTargetInterface` which drop
targets can use to determine when dropping means inserting before, after or
into.
- Store the block and region in the view. This is needed unfortunately but
shouldn't be an issue since the tree view is recreated on redraws, together
with the block.
- Various smaller tweaks and additions to views as needed.
TODO (outside scope of this change): Increase row height so there is no gap
between tree view items, but keep things visually the same otherwise. This
reduces flickering while dragging.
Pull Request: https://projects.blender.org/blender/blender/pulls/109825
2023-07-11 14:30:26 +02:00
|
|
|
virtual std::unique_ptr<GridViewItemDropTarget> create_drop_target();
|
|
|
|
|
|
2022-06-16 11:29:20 +02:00
|
|
|
private:
|
|
|
|
|
static void grid_tile_click_fn(bContext *, void *but_arg1, void *);
|
|
|
|
|
void add_grid_tile_button(uiBlock &block);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
/** \name Grid-View Base Class
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
struct GridViewStyle {
|
|
|
|
|
GridViewStyle(int width, int height);
|
|
|
|
|
int tile_width = 0;
|
|
|
|
|
int tile_height = 0;
|
|
|
|
|
};
|
|
|
|
|
|
UI: Add AbstractView base class for views, unify reconstruction in there
No user visible changes expected.
There's plenty of duplicated code in the grid and the tree view, and I expect
this to become more. This starts the process of unifying these parts, which
should also make it easier to add new views. Complexity in the view classes is
reduced, and some type shenanigans for C compatibility and general view
management can be removed, since there is now a common base type.
For the start this ports some of the view reconstruction, where the view and
its items are compared to the version of itself in the previous redraw, so that
state (highlighted, active, renaming, collapsed, ...) can be preserved.
Notifier listening is also ported.
2022-07-02 21:49:21 +02:00
|
|
|
class AbstractGridView : public AbstractView {
|
2022-06-16 11:29:20 +02:00
|
|
|
friend class AbstractGridViewItem;
|
|
|
|
|
friend class GridViewBuilder;
|
|
|
|
|
friend class GridViewLayoutBuilder;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Vector<std::unique_ptr<AbstractGridViewItem>> items_;
|
2023-06-12 11:41:00 +02:00
|
|
|
/** Store this to avoid recomputing. */
|
|
|
|
|
mutable std::optional<int> item_count_filtered_;
|
2022-06-16 11:29:20 +02:00
|
|
|
/** <identifier, item> map to lookup items by identifier, used for efficient lookups in
|
|
|
|
|
* #update_from_old(). */
|
|
|
|
|
Map<StringRef, AbstractGridViewItem *> item_map_;
|
|
|
|
|
GridViewStyle style_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
AbstractGridView();
|
2023-07-26 15:08:21 +02:00
|
|
|
/* virtual */ ~AbstractGridView() override = default;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
using ItemIterFn = FunctionRef<void(AbstractGridViewItem &)>;
|
|
|
|
|
void foreach_item(ItemIterFn iter_fn) const;
|
2023-06-12 11:41:00 +02:00
|
|
|
void foreach_filtered_item(ItemIterFn iter_fn) const;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience wrapper constructing the item by forwarding given arguments to the constructor of
|
|
|
|
|
* the type (\a ItemT).
|
|
|
|
|
*
|
|
|
|
|
* E.g. if your grid-item type has the following constructor:
|
|
|
|
|
* \code{.cpp}
|
|
|
|
|
* MyGridItem(std::string str, int i);
|
|
|
|
|
* \endcode
|
|
|
|
|
* You can add an item like this:
|
|
|
|
|
* \code
|
|
|
|
|
* add_item<MyGridItem>("blabla", 42);
|
|
|
|
|
* \endcode
|
|
|
|
|
*/
|
|
|
|
|
template<class ItemT, typename... Args> inline ItemT &add_item(Args &&...args);
|
|
|
|
|
const GridViewStyle &get_style() const;
|
|
|
|
|
int get_item_count() const;
|
2023-06-12 11:41:00 +02:00
|
|
|
int get_item_count_filtered() const;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
2023-06-13 18:16:30 +02:00
|
|
|
void set_tile_size(int tile_width, int tile_height);
|
|
|
|
|
|
2022-06-16 11:29:20 +02:00
|
|
|
protected:
|
|
|
|
|
virtual void build_items() = 0;
|
|
|
|
|
|
|
|
|
|
private:
|
2023-07-26 16:33:28 +02:00
|
|
|
void foreach_view_item(FunctionRef<void(AbstractViewItem &)> iter_fn) const final;
|
UI: Add AbstractView base class for views, unify reconstruction in there
No user visible changes expected.
There's plenty of duplicated code in the grid and the tree view, and I expect
this to become more. This starts the process of unifying these parts, which
should also make it easier to add new views. Complexity in the view classes is
reduced, and some type shenanigans for C compatibility and general view
management can be removed, since there is now a common base type.
For the start this ports some of the view reconstruction, where the view and
its items are compared to the version of itself in the previous redraw, so that
state (highlighted, active, renaming, collapsed, ...) can be preserved.
Notifier listening is also ported.
2022-07-02 21:49:21 +02:00
|
|
|
void update_children_from_old(const AbstractView &old_view) override;
|
2022-06-16 11:29:20 +02:00
|
|
|
AbstractGridViewItem *find_matching_item(const AbstractGridViewItem &item_to_match,
|
|
|
|
|
const AbstractGridView &view_to_search_in) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add an already constructed item, moving ownership to the grid-view.
|
|
|
|
|
* All items must be added through this, it handles important invariants!
|
|
|
|
|
*/
|
|
|
|
|
AbstractGridViewItem &add_item(std::unique_ptr<AbstractGridViewItem> item);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
UI: Basic tree-view drag & drop reordering and inserting support
No user visible changes expected, these are just the internal API preparations.
Modifies the Drop API for views so that tree-views can choose to insert items
before, after and into other items.
Note: While there is support for drag-tooltips that can explain how an item
will be inserted, there is no drawing yet like in the Outliner, that indicates
if an item is inserted before, after or into. There is some work on that but
that can be done separately.
Changes:
- Removes `AbstractViewDropTarget` that was shared between tree- and
grid-views, and adds `AbstractTreeViewDropTarget` and
`AbstractGridViewDropTarget`. The tree-view needs specialized handling now,
and although they could share some code still, it's not worth having another
level of inheritance.
- Modifies the drop-target API to use `DragInfo` which contains more info about
the dragging operation than just the `wmDrag`.
- Adds `determine_drop_location()` to the `DropTargetInterface` which drop
targets can use to determine when dropping means inserting before, after or
into.
- Store the block and region in the view. This is needed unfortunately but
shouldn't be an issue since the tree view is recreated on redraws, together
with the block.
- Various smaller tweaks and additions to views as needed.
TODO (outside scope of this change): Increase row height so there is no gap
between tree view items, but keep things visually the same otherwise. This
reduces flickering while dragging.
Pull Request: https://projects.blender.org/blender/blender/pulls/109825
2023-07-11 14:30:26 +02:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
/** \name Drag & Drop
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to define the behavior when dropping something onto/into a view item, plus the behavior
|
|
|
|
|
* when dragging over this item. An item can return a drop target for itself via a custom
|
|
|
|
|
* implementation of #AbstractGridViewItem::create_drop_target().
|
|
|
|
|
*/
|
|
|
|
|
class GridViewItemDropTarget : public DropTargetInterface {
|
|
|
|
|
protected:
|
|
|
|
|
AbstractGridView &view_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
GridViewItemDropTarget(AbstractGridView &view);
|
|
|
|
|
|
|
|
|
|
/** Request the view the item is registered for as type #ViewType. Throws a `std::bad_cast`
|
|
|
|
|
* exception if the view is not of the requested type. */
|
|
|
|
|
template<class ViewType> inline ViewType &get_view() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
2022-06-16 11:29:20 +02:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
/** \name Grid-View Builder
|
|
|
|
|
*
|
|
|
|
|
* TODO unify this with `TreeViewBuilder` and call view-specific functions via type erased view?
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
class GridViewBuilder {
|
|
|
|
|
public:
|
|
|
|
|
GridViewBuilder(uiBlock &block);
|
|
|
|
|
|
|
|
|
|
/** Build \a grid_view into the previously provided block, clipped by \a view_bounds (view space,
|
|
|
|
|
* typically `View2D.cur`). */
|
2023-03-20 11:35:45 +01:00
|
|
|
void build_grid_view(AbstractGridView &grid_view, const View2D &v2d, uiLayout &layout);
|
2022-06-16 11:29:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
/** \name Predefined Grid-View Item Types
|
|
|
|
|
*
|
|
|
|
|
* Common, Basic Grid-View Item Types.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A grid item that shows preview image icons at a nicely readable size (multiple of the normal UI
|
|
|
|
|
* unit size).
|
|
|
|
|
*/
|
|
|
|
|
class PreviewGridItem : public AbstractGridViewItem {
|
|
|
|
|
public:
|
|
|
|
|
using IsActiveFn = std::function<bool()>;
|
2023-07-26 16:47:17 +02:00
|
|
|
using ActivateFn = std::function<void(bContext &C, PreviewGridItem &new_active)>;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/** See #set_on_activate_fn() */
|
|
|
|
|
ActivateFn activate_fn_;
|
|
|
|
|
/** See #set_is_active_fn() */
|
|
|
|
|
IsActiveFn is_active_fn_;
|
2023-08-03 16:54:39 +02:00
|
|
|
bool hide_label_ = false;
|
2022-06-16 11:29:20 +02:00
|
|
|
|
|
|
|
|
public:
|
2024-02-02 11:08:07 -05:00
|
|
|
std::string label;
|
2022-06-16 11:29:20 +02:00
|
|
|
int preview_icon_id = ICON_NONE;
|
|
|
|
|
|
|
|
|
|
PreviewGridItem(StringRef identifier, StringRef label, int preview_icon_id);
|
|
|
|
|
|
|
|
|
|
void build_grid_tile(uiLayout &layout) const override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a custom callback to execute when activating this view item. This way users don't have to
|
|
|
|
|
* sub-class #PreviewGridItem, just to implement custom activation behavior (a common thing to
|
|
|
|
|
* do).
|
|
|
|
|
*/
|
|
|
|
|
void set_on_activate_fn(ActivateFn fn);
|
|
|
|
|
/**
|
|
|
|
|
* Set a custom callback to check if this item should be active.
|
|
|
|
|
*/
|
|
|
|
|
void set_is_active_fn(IsActiveFn fn);
|
|
|
|
|
|
2023-08-03 16:54:39 +02:00
|
|
|
void hide_label();
|
|
|
|
|
|
2022-06-16 11:29:20 +02:00
|
|
|
private:
|
|
|
|
|
std::optional<bool> should_be_active() const override;
|
2023-07-26 16:47:17 +02:00
|
|
|
void on_activate(bContext &C) override;
|
2022-06-16 11:29:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
template<class ItemT, typename... Args> inline ItemT &AbstractGridView::add_item(Args &&...args)
|
|
|
|
|
{
|
|
|
|
|
static_assert(std::is_base_of<AbstractGridViewItem, ItemT>::value,
|
|
|
|
|
"Type must derive from and implement the AbstractGridViewItem interface");
|
|
|
|
|
|
|
|
|
|
return dynamic_cast<ItemT &>(add_item(std::make_unique<ItemT>(std::forward<Args>(args)...)));
|
|
|
|
|
}
|
|
|
|
|
|
UI: Basic tree-view drag & drop reordering and inserting support
No user visible changes expected, these are just the internal API preparations.
Modifies the Drop API for views so that tree-views can choose to insert items
before, after and into other items.
Note: While there is support for drag-tooltips that can explain how an item
will be inserted, there is no drawing yet like in the Outliner, that indicates
if an item is inserted before, after or into. There is some work on that but
that can be done separately.
Changes:
- Removes `AbstractViewDropTarget` that was shared between tree- and
grid-views, and adds `AbstractTreeViewDropTarget` and
`AbstractGridViewDropTarget`. The tree-view needs specialized handling now,
and although they could share some code still, it's not worth having another
level of inheritance.
- Modifies the drop-target API to use `DragInfo` which contains more info about
the dragging operation than just the `wmDrag`.
- Adds `determine_drop_location()` to the `DropTargetInterface` which drop
targets can use to determine when dropping means inserting before, after or
into.
- Store the block and region in the view. This is needed unfortunately but
shouldn't be an issue since the tree view is recreated on redraws, together
with the block.
- Various smaller tweaks and additions to views as needed.
TODO (outside scope of this change): Increase row height so there is no gap
between tree view items, but keep things visually the same otherwise. This
reduces flickering while dragging.
Pull Request: https://projects.blender.org/blender/blender/pulls/109825
2023-07-11 14:30:26 +02:00
|
|
|
template<class ViewType> ViewType &GridViewItemDropTarget::get_view() const
|
|
|
|
|
{
|
|
|
|
|
static_assert(std::is_base_of<AbstractGridView, ViewType>::value,
|
|
|
|
|
"Type must derive from and implement the ui::AbstractGridView interface");
|
|
|
|
|
return dynamic_cast<ViewType &>(view_);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 11:29:20 +02:00
|
|
|
} // namespace blender::ui
|