Item View Slot Cursor Manager: Make The Drag Begin From Cursor Position.

Hello and good evening,

As i further progress into the development of my inventory system i came along one issue, and that is when i start dragging an item view, the item view thats spawned in order to move has to be where i started dragging and not always at the same position.

Example: If i start dragging from the top left of an item, i expect the dragged view's position should be the same, if i start from bottom right i expect the drag to begin from bottom right and so on.

As in the moment i am writing this i also came along one re-occuring issue with the way the documentation is structured, and that is while the info on several scripts is covered on a surface level, several of us programmers willing to fix small issues in our dev cycle as they appear face the fact we got 0 info on how the script workflow is structured in order to properly inject code where we need.

As an example of an asset that covers this in depth i present Interactor for IK:
diagram_v070.png
In their code analysis they have developed a handy cheatsheet that properly shows how each script communicates with one another and that makes fixing issues and injecting code much easier.

I bring the above points as i am currently going through the cursor manager script, and i dont understand where the dragging behaviour begins (Is it the drag handler? is it the ItemViewSlotDragHandler? Or is it the ItemViewSlotMoveCursor?

At what point do the above read the input feedback? and so on, so many questions, and i keep having to come in contact in order to properly follow the flow that opsive has put together.

Designing something that allows to inspect the flow of the code in your documentation as an API of sorts would allow several questions to not be repeated and so on.

Thank you for your time.
 
Hi

When making the Drag and Drop I didn't take into account the offset probably because I made it such that it could work with both keyboard or mouse.

The Drag is started either by the ItemViewSlot pointer drag event or by the MoveItemAction.
The Drag is only possible if the game supports a pointer device such as a mouse. For gamepad or keyboard only games the MoveItemAction allows you to move items without a mouse.

The ItemView Slot pointer drag event gets detected by the ItemViewSlotDragHandler which notifies the ItemViewSlotCursorManager (on the canvas).

The MoveItemAction finds and calls a function on the ItemViewSlotMoveCursor which notifier the ItemViewSlotCursorManager.

So we essentially have two different paths to start moving an ItemViewSlot. In your case though it only has to do with the Drag, so I changed the ItemViewSlotDragHandler to have the option to keep the offset.

Here I changed the ItemViewSlotDragHandler:

You will need to add this field and member variable:
Code:
[Tooltip("Take the offset into account")]
[SerializeField] protected bool m_KeepOffset = true;

protected Vector3[] m_RectWorldCorners;

Then you will need to replace those two functions

Code:
/// <summary>
/// Initialize the grid.
/// </summary>
public virtual void Initialize()
{
    if (m_IsInitialized) { return; }
    m_RectWorldCorners = new Vector3[4];
    if (m_ItemViewSlotCursorManager == null) {
        m_ItemViewSlotCursorManager = GetComponentInParent<ItemViewSlotCursorManager>();
        if (m_ItemViewSlotCursorManager == null) {
            Debug.LogWarning("The item view cursor manager is missing, please add one on your canvas.");
        }
    }
    m_ViewSlotsContainer = GetComponent<ItemViewSlotsContainerBase>();
    m_ViewSlotsContainer.OnItemViewSlotBeginDragE += HandleItemViewSlotBeginDrag;
    m_ViewSlotsContainer.OnItemViewSlotEndDragE += HandleItemViewSlotEndDrag;
    m_ViewSlotsContainer.OnItemViewSlotDragE += HandleItemViewSlotDrag;
    m_IsInitialized = true;
}

Code:
/// <summary>
/// Handle the Item View slot beginning to drag.
/// </summary>
/// <param name="eventData">The event data.</param>
protected virtual void HandleItemViewSlotBeginDrag(ItemViewSlotPointerEventData eventData)
{
    if(DragEventCondition(eventData) == false){return;}
    var position = eventData.PointerEventData.position;
    if (m_KeepOffset) {
        
        // Find the ItemViewSlot center.
        var rectTransform = eventData.ItemViewSlot.transform as RectTransform;
        rectTransform.GetWorldCorners(m_RectWorldCorners);
        
        // corners start from bottom left and turns clockwise to end on bottom right
        var bottomLeft = m_RectWorldCorners[0];
        var topRight = m_RectWorldCorners[2];
        var rectCenter = bottomLeft + (topRight - bottomLeft) / 2f;
        
        // World position is the same as screen space position for Canvas Screen Overlay.
        position = rectCenter;
    }
    
    m_ItemViewSlotCursorManager.StartMove(eventData, position);
    OnDragStarted?.Invoke(eventData);
}


As for adding a high level Code Flow chart to the documentation it is something I am considering. But it won't be for any time soon, perhaps for the next major update.
 
Top