Subscribing to item inserted and removing events

Rotorz ReorderableList

Subscribing to item inserted and removing events

This example demonstrates how to subscribe to events by creating a local instance of the list control. It is also necessary to instantiate the appropriate list adaptor which can be cached alongside list control instance.

For this example we will subscribe to item added and removing events so that we can echo these to the Unity console.

using Rotorz.ReorderableList;
using System.Collections.Generic;
using UnityEditor;

public class ExampleWindow : EditorWindow {

    private ReorderableListControl _listControl;
    private IReorderableListAdaptor _listAdaptor;

    private List<string> _someList = new List<string>();

    private void OnEnable() {
        // Create list control and optionally pass flags into constructor.
        _listControl = new ReorderableListControl();

        // Subscribe to events for item insertion and removal.
        _listControl.ItemInserted += OnItemInserted;
        _listControl.ItemRemoving += OnItemRemoving;

        // Create adaptor for example list.
        _listAdaptor = new GenericListAdaptor(_someList);
    }

    private void OnDisable() {
        // Unsubscribe from events, good practice.
        if (_listControl != null) {
            _listControl.ItemInserted -= OnItemInserted;
            _listControl.ItemRemoving -= OnItemRemoving;
        }
    }

    private void OnItemInserted(object sender, ItemInsertedEventArgs args) {
        string item = _someList[args.ItemIndex];
        if (args.WasDuplicated)
            Debug.Log("Duplicated: " + item);
        else
            Debug.Log("Inserted: " + item);
    }

    private void OnItemRemoving(object sender, ItemRemovingEventArgs args) {
        string item = _someList[args.ItemIndex];
        Debug.Log("Removing: " + item);

        // You can cancel item removal at this stage!
        if (item == "Keep Me!")
            args.Cancel = true;
    }

    private void OnGUI() {
        // Draw layout version of reorderable list control.
        _listControl.Draw(_listAdaptor);

        // OR

        // Draw absolute version of reorderable list control.
        Rect position = default(Rect);
        position.x = 100;
        position.y = 100;
        position.width = 200;
        position.height = _listControl.CalculateListHeight(_listAdaptor);
        _listControl.Draw(position, _listAdaptor);
    }

}
#pragma strict
import Rotorz.ReorderableList;
import System.Collections.Generic;

class ExampleWindow extends EditorWindow {

    var _listControl:ReorderableListControl;
    var _listAdaptor:IReorderableListAdaptor;

    var _someList:List.<String> = new List.<String>();

    function OnEnable() {
        // Create list control and optionally pass flags into constructor.
        _listControl = new ReorderableListControl();

        // Subscribe to events for item insertion and removal.
        _listControl.ItemInserted += OnItemInserted;
        _listControl.ItemRemoving += OnItemRemoving;

        // Create adaptor for example list.
        _listAdaptor = new GenericListAdaptor.<String>(_someList);
    }

    function OnDisable() {
        // Unsubscribe from events, good practice.
        if (_listControl != null) {
            _listControl.ItemInserted -= OnItemInserted;
            _listControl.ItemRemoving -= OnItemRemoving;
        }
    }

    function OnItemInserted(sender:object, args:ItemInsertedEventArgs) {
        var item:String = _someList[args.ItemIndex];
        if (args.WasDuplicated)
            Debug.Log('Duplicated: ' + item);
        else
            Debug.Log('Inserted: ' + item);
    }

    function OnItemRemoving(sender:object, args:ItemRemovingEventArgs) {
        var item:String = _someList[args.ItemIndex];
        Debug.Log('Removing: ' + item);

        // You can cancel item removal at this stage!
        if (item == 'Keep Me!')
            args.Cancel = true;
    }

    function OnGUI() {
        // Draw layout version of reorderable list control.
        _listControl.Draw(_listAdaptor);

        // OR

        // Draw absolute version of reorderable list control.
        var position:Rect = new Rect();
        position.x = 100;
        position.y = 100;
        position.width = 200;
        position.height = _listControl.CalculateListHeight(_listAdaptor);
        _listControl.Draw(position, listAdaptor);
    }

}