示例#1
0
    // ================================= private functions

    private void SetUpListItems()
    {
        Debug.Assert(m_units.Count == 0, "SetUpListItems called twice.");
        Debug.Assert(m_unitViewModels.Count == 0, "SetUpListItems called twice.");

        int unitCount = Units.Count;

        m_unitGridManager.UpdateElementCount(unitCount);
        m_listContainer.SetHeight(m_unitGridManager.GetScrollRectHeight());

        System.Func <bool> IsAllStarted = () =>
        {
            if (m_units.Count < m_unitGridManager.GetGridCount())
            {
                return(false);
            }

            for (int i = 0; i < m_units.Count; i++)
            {
                var unit = m_units[i];
                if (!unit.isStarted)
                {
                    return(false);
                }
            }
            return(true);
        };

        UnitIconImageProvider.Instance.Stop();

        for (int i = 0; i < m_unitGridManager.GetGridCount(); i++)
        {
            int index     = i;
            var model     = new ListItemModel((uint)i);
            var viewModel = new UnitGridContext(model);

            m_listContainer.Items.Add(viewModel);

            m_unitViewModels.Add(viewModel);

            model.OnStarted += () =>
            {
                model.SetPosition(m_unitGridManager.GetInitialPosition(index));

                ApplyUnitData(m_unitGridManager.GetModifiedIndex(index), viewModel);

                if (IsAllStarted())
                {
                    AllStarted();
                }
            };

            model.OnClicked += () =>
            {
                if (ClickUnitAction != null)
                {
                    var tag = "UnitGridOpenUnitDetail";
                    ButtonBlocker.Instance.Block(tag);
                    ClickUnitAction(viewModel);
                    ButtonBlocker.Instance.Unblock(tag);
                }
            };

            model.OnLongPressed += () =>
            {
                if (LongPressUnitAction != null)
                {
                    LongPressUnitAction(viewModel);
                }
            };

            m_units.Add(model);
        }

        UnitIconImageProvider.Instance.Tick();
    }