summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/adaptagrams/libavoid/actioninfo.cpp
blob: b896cb9609bf3d0c07501dc3119701521be59519 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * vim: ts=4 sw=4 et tw=0 wm=0
 *
 * libavoid - Fast, Incremental, Object-avoiding Line Router
 *
 * Copyright (C) 2004-2011  Monash University
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * See the file LICENSE.LGPL distributed with the library.
 *
 * Licensees holding a valid commercial license may use this file in
 * accordance with the commercial license agreement provided with the 
 * library.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *
 * Author(s):  Michael Wybrow
*/


#include <algorithm>

#include "libavoid/actioninfo.h"
#include "libavoid/shape.h"
#include "libavoid/connector.h"
#include "libavoid/junction.h"

namespace Avoid {


ActionInfo::ActionInfo(ActionType t, ShapeRef *s, const Polygon& p, bool fM)
    : type(t),
      objPtr(s),
      newPoly(p),
      firstMove(fM)
{
    COLA_ASSERT(type == ShapeMove);
}


ActionInfo::ActionInfo(ActionType t, ShapeRef *s)
    : type(t),
      objPtr(s)

{
    COLA_ASSERT((type == ShapeAdd) || (type == ShapeRemove) ||
            (type == ShapeMove));
}


ActionInfo::ActionInfo(ActionType t, JunctionRef *j, const Point& p)
    : type(t),
      objPtr(j),
      newPosition(p)
{
    COLA_ASSERT(type == JunctionMove);
}


ActionInfo::ActionInfo(ActionType t, JunctionRef *j)
    : type(t),
      objPtr(j)
{
    COLA_ASSERT((type == JunctionAdd) || (type == JunctionRemove) ||
            (type == JunctionMove));
}

ActionInfo::ActionInfo(ActionType t, ConnRef *c)
    : type(t),
      objPtr(c)
{
    COLA_ASSERT(type == ConnChange);
}


ActionInfo::ActionInfo(ActionType t, ShapeConnectionPin *p)
    : type(t),
      objPtr(p)
{
    COLA_ASSERT(type == ConnectionPinChange);
}


ActionInfo::~ActionInfo()
{
}


Obstacle *ActionInfo::obstacle(void) const
{
    COLA_ASSERT((type == ShapeMove) || (type == ShapeAdd) || 
            (type == ShapeRemove) || (type == JunctionMove) || 
            (type == JunctionAdd) || (type == JunctionRemove));
    return (static_cast<Obstacle *> (objPtr));
}


ShapeRef *ActionInfo::shape(void) const
{
    return (dynamic_cast<ShapeRef *> (obstacle()));
}


ConnRef *ActionInfo::conn(void) const
{
    COLA_ASSERT(type == ConnChange);
    return (static_cast<ConnRef *> (objPtr));
}

JunctionRef *ActionInfo::junction(void) const
{
    return (dynamic_cast<JunctionRef *> (obstacle()));
}


void ActionInfo::addConnEndUpdate(const unsigned int type, 
        const ConnEnd& connEnd, bool isConnPinMoveUpdate)
{
    bool alreadyExists = false;
    for (ConnUpdateList::iterator conn = conns.begin();
            conn != conns.end(); ++conn)
    {
        // Look for an existing queued change to the same end.
        if (conn->first == type)
        {
            // Found a queued change to the same endpoint of the
            // connector. If this is a pin change as a result of a
            // shape move, then leave the user created update
            // that was found (since it may be moving the connection
            // to connect to a different shape/pin.  But if this is a
            // user change, then overwrite the previous change.
            alreadyExists = true;
            if (!isConnPinMoveUpdate)
            {
                // Overwrite the queued change with this one.
                conn->second = connEnd;
            }
            break;
        }
    }

    if (!alreadyExists)
    {
        // Matching change not found, so add this one.
        conns.push_back(std::make_pair(type, connEnd));
    }
}


bool ActionInfo::operator==(const ActionInfo& rhs) const
{
    return (type == rhs.type) && (objPtr == rhs.objPtr);
}


bool ActionInfo::operator<(const ActionInfo& rhs) const
{
    if (type != rhs.type)
    {
        return type < rhs.type;
    }

    if (type == ConnChange)
    {
        return conn()->id() < rhs.conn()->id();
    }
    else if (type == ConnectionPinChange)
    {
        // NOTE Comparing pointers may not preserve the order of
        //      objects, but the order of Connection Pins is not
        //      used so this is not an issue here.
        return objPtr < rhs.objPtr;
    }
    else
    {
        return obstacle()->id() < rhs.obstacle()->id();
    }
}


}