cocos2d-x二維TableView

原文請猛戳:
http://galoisplusplus.coding.me/blog/2014/09/20/gridview-in-cocos2d-x/c++

對於剛開始接觸cocos2d-x的TableView的人來講,這個UI類看上去並不是顧名思義的是個Table,而僅僅是個一維的List。
由於項目須要,我封裝了一個TableView的子類來實現二維的功能。
具體代碼和測試代碼詳見:
cocos2d-x-GridViewgit

<!--
(有時間的話我會解釋爲什麼要設計成繼承TableView和採用TableView實現2D效果的workaround爲什麼不適合項目需求)
-->github

GridView的使用與TableView基本相同,只不過多了兩個接口:ide

  • 固定行數:調用setRowNum測試

  • 固定列數:調用setColNumthis

這還須要修改一下cocos2d-x的TableView類的聲明,主要是聲明一些member function爲virtual:lua

class TableView : public ScrollView, public ScrollViewDelegate
{
public:
    
    enum class VerticalFillOrder
    {
        TOP_DOWN,
        BOTTOM_UP
    };
    
    /** Empty contructor of TableView */
    static TableView* create();
    
    /**
     * An intialized table view object
     *
     * @param dataSource data source
     * @param size view size
     * @return table view
     * @code
     * when this function bound to js or lua,the input params are changed
     * in js:var create(var jsObject,var size)
     * in lua:local create(var size)
     * in lua:
     * @endcode
     */
    static TableView* create(TableViewDataSource* dataSource, Size size);
    /**
     * An initialized table view object
     *
     * @param dataSource data source;
     * @param size view size
     * @param container parent object for cells
     * @return table view
     * @code
     * when this function bound to js or lua,the input params are changed
     * in js:var create(var jsObject,var size,var container)
     * in lua:local create(var size, var container)
     * in lua:
     * @endcode
     */
    static TableView* create(TableViewDataSource* dataSource, Size size, Node *container);
    /**
     * @js ctor
     */
    TableView();
    /**
     * @js NA
     * @lua NA
     */
    virtual ~TableView();

    virtual bool initWithViewSize(Size size, Node* container = NULL);

    /**
     * data source
     * @js NA
     * @lua NA
     */
    virtual TableViewDataSource* getDataSource() { return _dataSource; }
    /**
     * when this function bound to js or lua,the input params are changed
     * in js:var setDataSource(var jsSource)
     * in lua:local setDataSource()
     * @endcode
     */
    virtual void setDataSource(TableViewDataSource* source) { _dataSource = source; }
    /**
     * delegate
     * @js NA
     * @lua NA
     */
    virtual TableViewDelegate* getDelegate() { return _tableViewDelegate; }
    /**
     * @code
     * when this function bound to js or lua,the input params are changed
     * in js:var setDelegate(var jsDelegate)
     * in lua:local setDelegate()
     * @endcode
     */
    virtual void setDelegate(TableViewDelegate* pDelegate) { _tableViewDelegate = pDelegate; }

    /**
     * determines how cell is ordered and filled in the view.
     */
    virtual void setVerticalFillOrder(VerticalFillOrder order);
    virtual VerticalFillOrder getVerticalFillOrder();

    /**
     * Updates the content of the cell at a given index.
     *
     * @param idx index to find a cell
     */
    virtual void updateCellAtIndex(ssize_t idx);
    /**
     * Inserts a new cell at a given index
     *
     * @param idx location to insert
     */
    virtual void insertCellAtIndex(ssize_t idx);
    /**
     * Removes a cell at a given index
     *
     * @param idx index to find a cell
     */
    virtual void removeCellAtIndex(ssize_t idx);
    /**
     * reloads data from data source.  the view will be refreshed.
     */
    virtual void reloadData();
    /**
     * Dequeues a free cell if available. nil if not.
     *
     * @return free cell
     */
    virtual TableViewCell *dequeueCell();

    /**
     * Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query.
     *
     * @param idx index
     * @return a cell at a given index
     */
    virtual TableViewCell *cellAtIndex(ssize_t idx);

    // Overrides
    virtual void scrollViewDidScroll(ScrollView* view) override;
    virtual void scrollViewDidZoom(ScrollView* view)  override {}
    virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override;
    virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override;
    virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override;
    virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override;

protected:
    virtual long __indexFromOffset(Vec2 offset);
    virtual long _indexFromOffset(Vec2 offset);
    virtual Vec2 __offsetFromIndex(ssize_t index);
    virtual Vec2 _offsetFromIndex(ssize_t index);

    virtual void _moveCellOutOfSight(TableViewCell *cell);
    virtual void _setIndexForCell(ssize_t index, TableViewCell *cell);
    virtual void _addCellIfNecessary(TableViewCell * cell);

    virtual void _updateCellPositions();


    TableViewCell *_touchedCell;
    /**
     * vertical direction of cell filling
     */
    VerticalFillOrder _vordering;

    /**
     * index set to query the indexes of the cells used.
     */
    std::set<ssize_t>* _indices;

    /**
     * vector with all cell positions
     */
    std::vector<float> _vCellsPositions;
    //NSMutableIndexSet *indices_;
    /**
     * cells that are currently in the table
     */
    Vector<TableViewCell*> _cellsUsed;
    /**
     * free list of cells
     */
    Vector<TableViewCell*> _cellsFreed;
    /**
     * weak link to the data source object
     */
    TableViewDataSource* _dataSource;
    /**
     * weak link to the delegate object
     */
    TableViewDelegate* _tableViewDelegate;

    Direction _oldDirection;

    bool _isUsedCellsDirty;

public:
    virtual void _updateContentSize();

};

後記

雖然我當時折騰了一陣弄了GridView,但後來我仍是傾向於直接用TableView實現二維列表的。好比在m行的TableView的一行裏放n個按鈕就是mxn的列表,而按鈕的響應能夠是個c++
lambda,有上下文信息,徹底能夠實現比較複雜的功能。設計

相關文章
相關標籤/搜索