File: EntitiesFirstSelector.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (112 lines) | stat: -rw-r--r-- 3,511 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once

#include <map>

#include "iselectable.h"
#include "iselectiontest.h"
#include "ientity.h"

namespace selection
{

/**
 * A Selector implementation sorting entities before primitives.
 * It's used by the selection tests when in SelectionMode::Primitive mode.
 */
class EntitiesFirstSelector :
    public Selector
{
private:
    using SelectableSortedSet = std::multimap<SelectionIntersection, ISelectable*>;

    SelectableSortedSet _entityPool;
    SelectableSortedSet _primitivePool;

    SelectionIntersection _curIntersection;
    ISelectable* _curSelectable;

    // A set of all current ISelectable* candidates, to prevent double-insertions
    // The iterator value points to an element in the SelectableSortedSet
    // to allow for fast lookup and removal.
    std::map<ISelectable*, SelectableSortedSet::iterator> _currentSelectables;

public:
    EntitiesFirstSelector() :
        _curSelectable(nullptr)
    {}

    void pushSelectable(ISelectable& selectable) override
    {
        _curIntersection = SelectionIntersection();
        _curSelectable = &selectable;
    }

    void popSelectable() override
    {
        addSelectable(_curIntersection, _curSelectable);
        _curIntersection = SelectionIntersection();
    }

    void addIntersection(const SelectionIntersection& intersection) override
    {
        _curIntersection.assignIfCloser(intersection);
    }

    void addSelectable(const SelectionIntersection& intersection, ISelectable* selectable)
    {
        if (!intersection.isValid()) return; // skip invalid intersections

        auto existing = _currentSelectables.find(selectable);
        auto isEntity = dynamic_cast<IEntityNode*>(selectable) != nullptr;
        auto& pool = isEntity ? _entityPool : _primitivePool;

        if (existing != _currentSelectables.end())
        {
            // greebo: We had that selectable before, check if the intersection is a better one
            // and update it if necessary. It's possible that the selectable is the parent of
            // two different child primitives, but both may want to add themselves to this pool.
            // To prevent the "worse" primitive from shadowing the "better" one, perform this check.

            // Check if the intersection is better
            if (intersection < existing->second->first)
            {
                // Yes, update the map, remove old stuff first
                pool.erase(existing->second);
                _currentSelectables.erase(existing);
            }
            else
            {
                // The existing intersection is better, we're done here
                return;
            }
        }

        // At this point, the selectable is ready for insertion into the pool
        // Either it's a completely new Selectable, or it is replacing an existing one
        auto result = pool.emplace(intersection, selectable);

        // Memorise the Selectable for fast lookups
        _currentSelectables.emplace(selectable, result);
    }

    bool empty() const override
    {
        return _entityPool.empty() && _primitivePool.empty();
    }

    // Visit each selectable, entities first, then primitives
    void foreachSelectable(const std::function<void(ISelectable*)>& functor) override
    { 
        for (const auto& [_, selectable] : _entityPool)
        {
            functor(selectable);
        }

        for (const auto& [_, selectable] : _primitivePool)
        {
            functor(selectable);
        }
    }
};

}