Selection cache is also updated when objects are updated

This commit is contained in:
Yousef Khadadeh 2025-07-31 19:28:18 +03:00
parent 069fc66565
commit 0b1cc8d4c2
8 changed files with 37 additions and 8 deletions

View file

@ -108,20 +108,20 @@ bool DrawComponent::check_timers(DrawingProgram& drawP, const std::shared_ptr<Dr
void DrawComponent::commit_update(DrawingProgram& drawP, bool invalidateCache) {
auto lockedCollabInfo = collabListInfo.lock();
if(invalidateCache && lockedCollabInfo && worldAABB)
drawP.compCache.preupdate_component(lockedCollabInfo, worldAABB);
drawP.preupdate_component(lockedCollabInfo);
initialize_draw_data(drawP);
calculate_world_bounds();
if(invalidateCache && lockedCollabInfo && worldAABB)
drawP.compCache.preupdate_component(lockedCollabInfo, worldAABB);
drawP.preupdate_component(lockedCollabInfo);
}
void DrawComponent::commit_transform(DrawingProgram& drawP, bool invalidateCache) {
auto lockedCollabInfo = collabListInfo.lock();
if(invalidateCache && lockedCollabInfo && worldAABB)
drawP.compCache.preupdate_component(lockedCollabInfo, worldAABB);
drawP.preupdate_component(lockedCollabInfo);
calculate_world_bounds();
if(invalidateCache && lockedCollabInfo && worldAABB)
drawP.compCache.preupdate_component(lockedCollabInfo, worldAABB);
drawP.preupdate_component(lockedCollabInfo);
}
void DrawComponent::client_send_place_many(DrawingProgram& drawP, std::vector<CollabListType::ObjectInfoPtr>& comps) {

View file

@ -71,7 +71,7 @@ void DrawImage::update(DrawingProgram& drawP) {
std::shared_ptr<ResourceDisplay> display = drawP.world.drawData.rMan->get_display_data(d.imageID);
if(display) {
if(display->update_draw() && collabListInfo.lock())
drawP.compCache.invalidate_cache_at_optional_aabb_before_pos(worldAABB, collabListInfo.lock()->pos);
drawP.invalidate_cache_at_component(collabListInfo.lock());
}
}

View file

@ -531,6 +531,20 @@ void DrawingProgram::add_undo_place_component(const CollabListType::ObjectInfoPt
});
}
void DrawingProgram::invalidate_cache_at_component(const CollabListType::ObjectInfoPtr& objToCheck) {
if(selection.is_selected(objToCheck))
selection.invalidate_cache_at_optional_aabb_before_pos(objToCheck->obj->worldAABB, objToCheck->pos);
else
compCache.invalidate_cache_at_optional_aabb_before_pos(objToCheck->obj->worldAABB, objToCheck->pos);
}
void DrawingProgram::preupdate_component(const CollabListType::ObjectInfoPtr& objToCheck) {
if(selection.is_selected(objToCheck))
selection.preupdate_component(objToCheck);
else
compCache.preupdate_component(objToCheck);
}
void DrawingProgram::add_undo_place_components(const std::unordered_set<CollabListType::ObjectInfoPtr>& objSetToUndo) {
world.undo.push(UndoManager::UndoRedoPair{
[&, objSetToUndo = objSetToUndo]() {

View file

@ -47,6 +47,10 @@ class DrawingProgram {
void parallel_loop_all_components(std::function<void(const std::shared_ptr<CollabList<std::shared_ptr<DrawComponent>, ServerClientID>::ObjectInfo>&)> func);
// This function, unlike preupdate_component, does not take the object out of the BVH. Use for a draw update that doesn't change the object's bounding box
void invalidate_cache_at_component(const CollabListType::ObjectInfoPtr& objToCheck);
void preupdate_component(const CollabListType::ObjectInfoPtr& objToCheck);
DrawingProgramCache compCache;
CollabListType components;

View file

@ -165,14 +165,14 @@ void DrawingProgramCache::traverse_bvh_run_function_recursive(const std::shared_
}
}
void DrawingProgramCache::preupdate_component(const CollabListType::ObjectInfoPtr& c, const std::optional<SCollision::AABB<WorldScalar>>& aabb) {
void DrawingProgramCache::preupdate_component(const CollabListType::ObjectInfoPtr& c) {
auto parentBvhNode = c->obj->parentBvhNode.lock();
if(parentBvhNode) {
unsortedComponents.emplace_back(c);
std::erase(parentBvhNode->components, c);
c->obj->parentBvhNode.reset();
}
invalidate_cache_at_optional_aabb_before_pos(aabb, c->pos);
invalidate_cache_at_optional_aabb_before_pos(c->obj->worldAABB, c->pos);
}
void DrawingProgramCache::build_bvh_node(const std::shared_ptr<DrawingProgramCacheBVHNode>& bvhNode, const std::vector<CollabListType::ObjectInfoPtr>& components) {

View file

@ -52,7 +52,7 @@ class DrawingProgramCache {
void traverse_bvh_run_function(const SCollision::AABB<WorldScalar>& aabb, std::function<bool(const std::shared_ptr<DrawingProgramCacheBVHNode>& node, const std::vector<CollabListType::ObjectInfoPtr>& components)> f);
void traverse_bvh_erase_function(const SCollision::AABB<WorldScalar>& aabb, std::function<bool(const std::shared_ptr<DrawingProgramCacheBVHNode>& node, std::vector<CollabListType::ObjectInfoPtr>& components)> f);
// Run before actually updating the component
void preupdate_component(const CollabListType::ObjectInfoPtr& c, const std::optional<SCollision::AABB<WorldScalar>>& aabb);
void preupdate_component(const CollabListType::ObjectInfoPtr& c);
void invalidate_cache_before_pos(uint64_t placementToInvalidateAt);
void invalidate_cache_at_aabb_before_pos(const SCollision::AABB<WorldScalar>& aabb, uint64_t placementToInvalidateAt);
void invalidate_cache_at_optional_aabb_before_pos(const std::optional<SCollision::AABB<WorldScalar>>& aabb, uint64_t placementToInvalidateAt);

View file

@ -170,6 +170,14 @@ void DrawingProgramSelection::deselect_all() {
}
}
void DrawingProgramSelection::invalidate_cache_at_optional_aabb_before_pos(const std::optional<SCollision::AABB<WorldScalar>>& aabb, uint64_t placementToInvalidateAt) {
cache.invalidate_cache_at_optional_aabb_before_pos(aabb, placementToInvalidateAt);
}
void DrawingProgramSelection::preupdate_component(const CollabListType::ObjectInfoPtr& objToCheck) {
cache.preupdate_component(objToCheck);
}
bool DrawingProgramSelection::is_selected(const CollabListType::ObjectInfoPtr& objToCheck) {
return selectedSet.contains(objToCheck);
}

View file

@ -16,6 +16,9 @@ class DrawingProgramSelection {
void erase_component(const CollabListType::ObjectInfoPtr& objToCheck);
bool is_selected(const CollabListType::ObjectInfoPtr& objToCheck);
const std::unordered_set<CollabListType::ObjectInfoPtr>& get_selected_set();
void invalidate_cache_at_optional_aabb_before_pos(const std::optional<SCollision::AABB<WorldScalar>>& aabb, uint64_t placementToInvalidateAt);
void preupdate_component(const CollabListType::ObjectInfoPtr& objToCheck);
private:
void delete_all();
bool mouse_collided_with_selection_aabb();