CCTableViewLoader.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // CCTableViewLoader.cpp
  3. // redream_runtime_mac
  4. //
  5. // Created by zhuge on 2023/3/15.
  6. //
  7. #include "CCTableViewLoader.h"
  8. #define PROPERTY_CONTAINER "container"
  9. #define PROPERTY_VORDERING "vordering"
  10. #define PROPERTY_TABLEVIEW_REDFILE "tableviewRedFile"
  11. namespace redream {
  12. cocos2d::Size TableViewLoader::TableViewDataSource::tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx) {
  13. auto cellSize = cellSizeForTable(table);
  14. return cellSize;
  15. }
  16. cocos2d::extension::TableViewCell* TableViewLoader::TableViewDataSource::TableViewDataSource::tableCellAtIndex(cocos2d::extension::TableView* table, ssize_t idx) {
  17. cocos2d::extension::TableViewCell* cell = table->dequeueCell();
  18. if (nullptr == cell) {
  19. cell = new (std::nothrow) cocos2d::extension::TableViewCell();
  20. cell->setIdx(idx);
  21. cell->autorelease();
  22. if (idx < _subRedNodes.size()) {
  23. auto node = _subRedNodes.at(idx);
  24. cell->addChild(node);
  25. }
  26. } else {
  27. if (idx < _subRedNodes.size()) {
  28. auto node = _subRedNodes.at(idx);
  29. node->removeFromParentAndCleanup(false);
  30. cell->addChild(node);
  31. }
  32. }
  33. return cell;
  34. }
  35. cocos2d::Size TableViewLoader::TableViewDataSource::cellSizeForTable(cocos2d::extension::TableView* table) {
  36. return cocos2d::Size(50, 50);
  37. }
  38. ssize_t TableViewLoader::TableViewDataSource::numberOfCellsInTableView(cocos2d::extension::TableView* table) {
  39. return 3;
  40. }
  41. cocos2d::extension::TableView* TableViewLoader::createNode(cocos2d::Node * pParent, redream::REDReader * ccbReader) {
  42. auto dataSource = new TableViewDataSource();
  43. auto tv = cocos2d::extension::TableView::create(dataSource, Size(200, 200));
  44. tv->setDataSource(dataSource);
  45. return tv;
  46. }
  47. #pragma mark - table view loader
  48. void TableViewLoader::onHandlePropTypeREDFile(Node * pNode, Node * pParent, const char * pPropertyName, Node * pREDFileNode, REDReader * ccbReader) {
  49. if(strcmp(pPropertyName, PROPERTY_CONTAINER) == 0) {
  50. // do nothing, 添加节点交到TableViewRedFile属性
  51. } else {
  52. NodeLoader::onHandlePropTypeREDFile(pNode, pParent, pPropertyName, pREDFileNode, ccbReader);
  53. }
  54. }
  55. void TableViewLoader::onHandlePropTypeTableviewRedFile(cocos2d::Node * pNode, cocos2d::Node * pParent, const char * pPropertyName, std::vector<cocos2d::Node*> pREDFileNodes, REDReader * ccbReader) {
  56. if (strcmp(pPropertyName, PROPERTY_TABLEVIEW_REDFILE) == 0) {
  57. auto tableView = (cocos2d::extension::TableView*)pNode;
  58. auto dataSource = dynamic_cast<redream::TableViewLoader::TableViewDataSource*>(tableView->getDataSource());
  59. if (dataSource != nullptr) {
  60. for (auto node: pREDFileNodes) {
  61. dataSource->addSubRedNode(node);
  62. }
  63. tableView->reloadData();
  64. }
  65. } else {
  66. NodeLoader::onHandlePropTypeTableviewRedFile(pNode, pParent, pPropertyName, pREDFileNodes, ccbReader);
  67. }
  68. }
  69. void TableViewLoader::onHandlePropTypeIntegerLabeled(Node * pNode, Node * pParent, const char * pPropertyName, int pIntegerLabeled, REDReader * ccbReader) {
  70. if(strcmp(pPropertyName, PROPERTY_VORDERING) == 0) {
  71. ((cocos2d::extension::TableView *)pNode)->setVerticalFillOrder(cocos2d::extension::TableView::VerticalFillOrder(pIntegerLabeled));
  72. } else {
  73. ScrollViewLoader::onHandlePropTypeIntegerLabeled(pNode, pParent, pPropertyName, pIntegerLabeled, ccbReader);
  74. }
  75. }
  76. } // namespace