Trying to find a good solution for update message & data

This commit is contained in:
Sam Gateau 2015-05-30 13:57:05 -07:00
parent 7bde81e95f
commit ddec8fd26f
3 changed files with 45 additions and 15 deletions

View file

@ -3404,6 +3404,13 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se
WorldBoxRenderData::_item = _main3DScene->allocateID();
pendingChanges.resetItem(WorldBoxRenderData::_item, worldBoxRenderPayload);
} else {
pendingChanges.updateItem(WorldBoxRenderData::_item,
render::UpdateFunctor(
[](WorldBoxRenderData& payload) {
qCDebug(interfaceapp, "MyFirst update message!!!!!");
}));
}
{

View file

@ -66,8 +66,8 @@ void Item::kill() {
_key._flags.reset();
}
void Item::move() {
void Item::update(const UpdateFunctor& functor) {
_payload->update(functor);
}
void PendingChanges::resetItem(ItemID id, const PayloadPointer& payload) {
@ -79,8 +79,9 @@ void PendingChanges::removeItem(ItemID id) {
_removedItems.push_back(id);
}
void PendingChanges::moveItem(ItemID id) {
_movedItems.push_back(id);
void PendingChanges::updateItem(ItemID id, const UpdateFunctor& functor) {
_updatedItems.push_back(id);
_updateFunctors.push_back(functor);
}
@ -88,7 +89,8 @@ void PendingChanges::merge(PendingChanges& changes) {
_resetItems.insert(_resetItems.end(), changes._resetItems.begin(), changes._resetItems.end());
_resetPayloads.insert(_resetPayloads.end(), changes._resetPayloads.begin(), changes._resetPayloads.end());
_removedItems.insert(_removedItems.end(), changes._removedItems.begin(), changes._removedItems.end());
_movedItems.insert(_movedItems.end(), changes._movedItems.begin(), changes._movedItems.end());
_updatedItems.insert(_updatedItems.end(), changes._updatedItems.begin(), changes._updatedItems.end());
_updateFunctors.insert(_updateFunctors.end(), changes._updateFunctors.begin(), changes._updateFunctors.end());
}
Scene::Scene() {
@ -133,7 +135,7 @@ void Scene::processPendingChangesQueue() {
// capture anything coming from the pendingChanges
resetItems(consolidatedPendingChanges._resetItems, consolidatedPendingChanges._resetPayloads);
removeItems(consolidatedPendingChanges._removedItems);
moveItems(consolidatedPendingChanges._movedItems);
updateItems(consolidatedPendingChanges._updatedItems, consolidatedPendingChanges._updateFunctors);
// ready to go back to rendering activities
_itemsMutex.unlock();
@ -159,9 +161,11 @@ void Scene::removeItems(const ItemIDs& ids) {
}
}
void Scene::moveItems(const ItemIDs& ids) {
for (auto movedID :ids) {
_items[movedID].move();
void Scene::updateItems(const ItemIDs& ids, UpdateFunctors& functors) {
auto updateID = ids.begin();
auto updateFunctor = functors.begin();
for (;updateID != ids.end(); updateID++, updateFunctor++) {
_items[(*updateID)].update((*updateFunctor));
}
}

View file

@ -194,6 +194,11 @@ public:
int _firstFrame;
};
// Update Functor
struct UpdateFunctor {
void* _something;
};
// Payload is whatever is in this Item and implement the Payload Interface
class PayloadInterface {
public:
@ -201,18 +206,21 @@ public:
virtual const Bound getBound() const = 0;
virtual void render(RenderArgs* args) = 0;
virtual void update(const UpdateFunctor& functor) = 0;
~PayloadInterface() {}
protected:
};
typedef std::shared_ptr<PayloadInterface> PayloadPointer;
Item() {}
~Item() {}
void resetPayload(const PayloadPointer& payload);
void kill();
void move();
void update(const UpdateFunctor& updateFunctor);
// Check heuristic key
const ItemKey& getKey() const { return _key; }
@ -228,6 +236,10 @@ protected:
friend class Scene;
};
typedef Item::UpdateFunctor UpdateFunctor;
typedef std::vector<UpdateFunctor> UpdateFunctors;
inline QDebug operator<<(QDebug debug, const Item& item) {
debug << "[Item: _key:" << item.getKey() << ", bounds:" << item.getBound() << "]";
return debug;
@ -245,11 +257,14 @@ template <class T> void payloadRender(const std::shared_ptr<T>& payloadData, Ren
template <class T> class Payload : public Item::PayloadInterface {
public:
typedef std::shared_ptr<T> DataPointer;
typedef std::function<void(T&)> Updator;
virtual const ItemKey getKey() const { return payloadGetKey<T>(_data); }
virtual const Item::Bound getBound() const { return payloadGetBound<T>(_data); }
virtual void render(RenderArgs* args) { payloadRender<T>(_data, args); }
virtual void update(const UpdateFunctor& functor) { functor._f((*_data)); }
Payload(const DataPointer& data) : _data(data) {}
protected:
DataPointer _data;
@ -315,6 +330,7 @@ public:
class Engine;
class Observer;
class PendingChanges {
public:
PendingChanges() {}
@ -322,14 +338,17 @@ public:
void resetItem(ItemID id, const PayloadPointer& payload);
void removeItem(ItemID id);
void moveItem(ItemID id);
template <class T> void updateItem(ItemID id, std::function<void(T&)>) { updateItem_(id, std::weak_ptr<std::function<void(T&)>
void updateItem_(ItemID id, const UpdateFunctor& functor);
void merge(PendingChanges& changes);
Payloads _resetPayloads;
ItemIDs _resetItems;
ItemIDs _removedItems;
ItemIDs _movedItems;
ItemIDs _updatedItems;
UpdateFunctors _updateFunctors;
protected:
};
@ -413,7 +432,7 @@ protected:
void resetItems(const ItemIDs& ids, Payloads& payloads);
void removeItems(const ItemIDs& ids);
void moveItems(const ItemIDs& ids);
void updateItems(const ItemIDs& ids, UpdateFunctors& functors);
// The scene context listening for any change to the database