Showing the changes to interface/src/scripting/SelectionScriptingInterface.cpp (1 of 3 files). Show all
Unified · Split
| @@ -12,40 +12,61 @@ · 11 unmodified lines | ||
| 12 | 12 | #include "SelectionScriptingInterface.h" |
| 13 | 13 | #include "Application.h" |
| 14 | 14 | |
| 15 | +GameplayObjects::GameplayObjects() { | |
| 16 | +} | |
| 17 | + | |
| 18 | +bool GameplayObjects::addToGameplayObjects(const QUuid& avatarID) { | |
| 19 | + _avatarIDs.push_back(avatarID); | |
| 20 | + return true; | |
| 21 | +} | |
| 22 | +bool GameplayObjects::removeFromGameplayObjects(const QUuid& avatarID) { | |
| 23 | + _avatarIDs.erase(std::remove(_avatarIDs.begin(), _avatarIDs.end(), avatarID), _avatarIDs.end()); | |
| 24 | + return true; | |
| 25 | +} | |
| 26 | + | |
| 27 | +bool GameplayObjects::addToGameplayObjects(const EntityItemID& entityID) { | |
| 28 | + _entityIDs.push_back(entityID); | |
| 29 | + return true; | |
| 30 | +} | |
| 31 | +bool GameplayObjects::removeFromGameplayObjects(const EntityItemID& entityID) { | |
| 32 | + _entityIDs.erase(std::remove(_entityIDs.begin(), _entityIDs.end(), entityID), _entityIDs.end()); | |
| 33 | + return true; | |
| 34 | +} | |
| 35 | + | |
| 36 | +bool GameplayObjects::addToGameplayObjects(const OverlayID& overlayID) { | |
| 37 | + _overlayIDs.push_back(overlayID); | |
| 38 | + return true; | |
| 39 | +} | |
| 40 | +bool GameplayObjects::removeFromGameplayObjects(const OverlayID& overlayID) { | |
| 41 | + _overlayIDs.erase(std::remove(_overlayIDs.begin(), _overlayIDs.end(), overlayID), _overlayIDs.end()); | |
| 42 | + return true; | |
| 43 | +} | |
| 44 | + | |
| 15 | 45 | SelectionScriptingInterface::SelectionScriptingInterface(AbstractViewStateInterface* viewState) { |
| 16 | 46 | _viewState = viewState; |
| 17 | 47 | } |
| 18 | 48 | |
| 19 | 49 | // |
| 20 | −// START HANDLING ENTITIES | |
| 50 | +// START HANDLING AVATARS | |
| 21 | 51 | // |
| 22 | −render::ItemID getItemIDFromEntityID(const EntityItemID& entityID) { | |
| 23 | − auto entityTree = qApp->getEntities()->getTree(); | |
| 24 | − entityTree->withReadLock([&] { | |
| 25 | − auto entityItem = entityTree->findEntityByEntityItemID(entityID); | |
| 26 | − if (entityItem != NULL) { | |
| 27 | − auto renderableInterface = entityItem->getRenderableInterface(); | |
| 28 | − if (renderableInterface != NULL) { | |
| 29 | − return renderableInterface->getMetaRenderItemID(); | |
| 30 | − } | |
| 31 | − } | |
| 32 | − return render::Item::INVALID_ITEM_ID; | |
| 33 | − }); | |
| 34 | − return render::Item::INVALID_ITEM_ID; | |
| 52 | +bool SelectionScriptingInterface::addToSelectedItemsList(const render::Selection::Name& listName, const QUuid& avatarSessionID) { | |
| 53 | + return addToGameplayObjects(listName, avatarSessionID); | |
| 35 | 54 | } |
| 55 | +bool SelectionScriptingInterface::removeFromSelectedItemsList(const render::Selection::Name& listName, const QUuid& avatarSessionID) { | |
| 56 | + return addToGameplayObjects(listName, avatarSessionID); | |
| 57 | +} | |
| 58 | +// | |
| 59 | +// END HANDLING AVATARS | |
| 60 | +// | |
| 61 | + | |
| 62 | +// | |
| 63 | +// START HANDLING ENTITIES | |
| 64 | +// | |
| 36 | 65 | bool SelectionScriptingInterface::addToSelectedItemsList(const render::Selection::Name& listName, const EntityItemID& entityID) { |
| 37 | − render::ItemID itemID = getItemIDFromEntityID(entityID); | |
| 38 | − if (itemID != render::Item::INVALID_ITEM_ID) { | |
| 39 | − return addToSelectedItemsList(listName, itemID); | |
| 40 | − } | |
| 41 | − return false; | |
| 66 | + return addToGameplayObjects(listName, entityID); | |
| 42 | 67 | } |
| 43 | 68 | bool SelectionScriptingInterface::removeFromSelectedItemsList(const render::Selection::Name& listName, const EntityItemID& entityID) { |
| 44 | − render::ItemID itemID = getItemIDFromEntityID(entityID); | |
| 45 | − if (itemID != render::Item::INVALID_ITEM_ID) { | |
| 46 | − return removeFromSelectedItemsList(listName, itemID); | |
| 47 | − } | |
| 48 | − return false; | |
| 69 | + return addToGameplayObjects(listName, entityID); | |
| 49 | 70 | } |
| 50 | 71 | // |
| 51 | 72 | // END HANDLING ENTITIES |
| @@ -54,30 +75,11 @@ bool SelectionScriptingInterface::removeFromSelectedItemsList(const render::Sele · 2 unmodified lines | ||
| 54 | 75 | // |
| 55 | 76 | // START HANDLING OVERLAYS |
| 56 | 77 | // |
| 57 | −render::ItemID getItemIDFromOverlayID(const OverlayID& overlayID) { | |
| 58 | − auto& overlays = qApp->getOverlays(); | |
| 59 | − auto overlay = overlays.getOverlay(overlayID); | |
| 60 | − if (overlay != NULL) { | |
| 61 | − auto itemID = overlay->getRenderItemID(); | |
| 62 | − if (itemID != render::Item::INVALID_ITEM_ID) { | |
| 63 | − return overlay->getRenderItemID(); | |
| 64 | − } | |
| 65 | − } | |
| 66 | − return render::Item::INVALID_ITEM_ID; | |
| 67 | −} | |
| 68 | 78 | bool SelectionScriptingInterface::addToSelectedItemsList(const render::Selection::Name& listName, const OverlayID& overlayID) { |
| 69 | − render::ItemID itemID = getItemIDFromOverlayID(overlayID); | |
| 70 | − if (itemID != render::Item::INVALID_ITEM_ID) { | |
| 71 | − return addToSelectedItemsList(listName, itemID); | |
| 72 | − } | |
| 73 | − return false; | |
| 79 | + return addToGameplayObjects(listName, overlayID); | |
| 74 | 80 | } |
| 75 | 81 | bool SelectionScriptingInterface::removeFromSelectedItemsList(const render::Selection::Name& listName, const OverlayID& overlayID) { |
| 76 | − render::ItemID itemID = getItemIDFromOverlayID(overlayID); | |
| 77 | − if (itemID != render::Item::INVALID_ITEM_ID) { | |
| 78 | − return removeFromSelectedItemsList(listName, itemID); | |
| 79 | − } | |
| 80 | − return false; | |
| 82 | + return addToGameplayObjects(listName, overlayID); | |
| 81 | 83 | } |
| 82 | 84 | // |
| 83 | 85 | // END HANDLING OVERLAYS |
| @@ -86,31 +88,25 @@ bool SelectionScriptingInterface::removeFromSelectedItemsList(const render::Sele · 2 unmodified lines | ||
| 86 | 88 | // |
| 87 | 89 | // START HANDLING GENERIC ITEMS |
| 88 | 90 | // |
| 89 | −bool SelectionScriptingInterface::addToSelectedItemsList(const render::Selection::Name& listName, render::ItemID idToAdd) { | |
| 91 | +template <class T> bool SelectionScriptingInterface::addToGameplayObjects(const render::Selection::Name& listName, T idToAdd) { | |
| 90 | 92 | if (_selectedItemsListMap.contains(listName)) { |
| 91 | 93 | auto currentList = _selectedItemsListMap.take(listName); |
| 92 | − currentList.push_back(idToAdd); // TODO: Ensure thread safety | |
| 94 | + currentList.addToGameplayObjects(idToAdd); | |
| 93 | 95 | _selectedItemsListMap.insert(listName, currentList); |
| 94 | 96 | updateRendererSelectedList(listName); |
| 95 | 97 | return true; |
| 96 | 98 | } else { |
| 97 | − _selectedItemsListMap.insert(listName, render::ItemIDs()); | |
| 99 | + _selectedItemsListMap.insert(listName, GameplayObjects()); | |
| 98 | 100 | return addToSelectedItemsList(listName, idToAdd); |
| 99 | 101 | } |
| 100 | 102 | } |
| 101 | −bool SelectionScriptingInterface::removeFromSelectedItemsList(const render::Selection::Name& listName, render::ItemID idToRemove) { | |
| 102 | − auto hashItr = _selectedItemsListMap.find(listName); | |
| 103 | − if (hashItr != _selectedItemsListMap.end()) { | |
| 103 | +template <class T> bool SelectionScriptingInterface::removeFromGameplayObjects(const render::Selection::Name& listName, T idToRemove) { | |
| 104 | + if (_selectedItemsListMap.contains(listName)) { | |
| 104 | 105 | auto currentList = _selectedItemsListMap.take(listName); |
| 105 | − auto listItr = std::find(currentList.begin(), currentList.end(), idToRemove); // TODO: Ensure thread safety | |
| 106 | − if (listItr == currentList.end()) { | |
| 107 | − return false; | |
| 108 | − } else { | |
| 109 | − currentList.erase(listItr); | |
| 110 | − _selectedItemsListMap.insert(listName, currentList); | |
| 111 | − updateRendererSelectedList(listName); | |
| 112 | − return true; | |
| 113 | − } | |
| 106 | + currentList.removeFromGameplayObjects(idToRemove); | |
| 107 | + _selectedItemsListMap.insert(listName, currentList); | |
| 108 | + updateRendererSelectedList(listName); | |
| 109 | + return true; | |
| 114 | 110 | } else { |
| 115 | 111 | return false; |
| 116 | 112 | } |
| @@ -133,12 +129,53 @@ void SelectionScriptingInterface::updateRendererSelectedList(const render::Selec · 16 unmodified lines | ||
| 133 | 129 | render::Transaction transaction; |
| 134 | 130 | |
| 135 | 131 | if (_selectedItemsListMap.contains(listName)) { |
| 136 | − render::Selection selection(listName, _selectedItemsListMap.value(listName)); | |
| 132 | + render::ItemIDs finalList; | |
| 133 | + render::ItemID currentID; | |
| 134 | + auto entityTree = qApp->getEntities()->getTree(); | |
| 135 | + auto& overlays = qApp->getOverlays(); | |
| 136 | + auto currentList = _selectedItemsListMap.value(listName); | |
| 137 | + | |
| 138 | + for (QUuid& currentAvatarID : currentList.getAvatarIDs()) { | |
| 139 | + auto avatar = std::static_pointer_cast<Avatar>(DependencyManager::get<AvatarManager>()->getAvatarBySessionID(currentAvatarID)); | |
| 140 | + if (avatar) { | |
| 141 | + currentID = avatar->getRenderItemID(); | |
| 142 | + if (currentID != render::Item::INVALID_ITEM_ID) { | |
| 143 | + finalList.push_back(currentID); | |
| 144 | + } | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + for (EntityItemID& currentEntityID : currentList.getEntityIDs()) { | |
| 149 | + entityTree->withReadLock([&] { | |
| 150 | + auto entityItem = entityTree->findEntityByEntityItemID(currentEntityID); | |
| 151 | + if (entityItem != NULL) { | |
| 152 | + auto renderableInterface = entityItem->getRenderableInterface(); | |
| 153 | + if (renderableInterface != NULL) { | |
| 154 | + currentID = renderableInterface->getMetaRenderItemID(); | |
| 155 | + if (currentID != render::Item::INVALID_ITEM_ID) { | |
| 156 | + finalList.push_back(currentID); | |
| 157 | + } | |
| 158 | + } | |
| 159 | + } | |
| 160 | + }); | |
| 161 | + } | |
| 162 | + | |
| 163 | + for (OverlayID& currentOverlayID : currentList.getOverlayIDs()) { | |
| 164 | + auto overlay = overlays.getOverlay(currentOverlayID); | |
| 165 | + if (overlay != NULL) { | |
| 166 | + currentID = overlay->getRenderItemID(); | |
| 167 | + if (currentID != render::Item::INVALID_ITEM_ID) { | |
| 168 | + finalList.push_back(currentID); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + } | |
| 172 | + | |
| 173 | + render::Selection selection(listName, finalList); | |
| 137 | 174 | transaction.resetSelection(selection); |
| 138 | 175 | |
| 139 | 176 | scene->enqueueTransaction(transaction); |
| 140 | 177 | } else { |
| 141 | − qWarning() << "List of ItemIDs doesn't exist in _selectedItemsListMap"; | |
| 178 | + qWarning() << "List of GameplayObjects doesn't exist in _selectedItemsListMap"; | |
| 142 | 179 | } |
| 143 | 180 | } else { |
| 144 | 181 | qWarning() << "SelectionScriptingInterface::updateRendererSelectedList(), Unexpected null scene, possibly during application shutdown"; |