summaryrefslogtreecommitdiffstats
path: root/Project/ConnectionLine.cpp
blob: fefe14431dcd9f00d442a7f5f2e5be02d7aedef4 (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
#include "ConnectionLine.h"

ConnectionLine::ConnectionLine(Node* firstNode)
    : ControlElement()
{
    wxPoint2DDouble pt = firstNode->GetPosition();
    m_tmpSndPt = pt;
    for(int i = 0; i < 6; i++) {
        m_pointList.push_back(pt);
    }
    m_nodeList.push_back(firstNode);
    firstNode->SetConnected();
}

ConnectionLine::~ConnectionLine() {}

void ConnectionLine::Draw(wxPoint2DDouble translation, double scale) const
{
    // Line selected (Layer 1).
    if(m_selected) {
        glLineWidth(1.5 + m_borderSize * 2.0);
        glColor4dv(m_selectionColour.GetRGBA());
        DrawLine(m_pointList);
    }

    // Draw line (Layer 2)
    glLineWidth(1.5);
    glColor4d(0.0, 0.0, 0.0, 1.0);
    DrawLine(m_pointList);
}

bool ConnectionLine::Contains(wxPoint2DDouble position) const
{
    if(PointToLineDistance(position) < 5.0) {
        return true;
    }
    return false;
}

bool ConnectionLine::Intersects(wxRect2DDouble rect) const
{
    for(auto it = m_pointList.begin(); it != m_pointList.end(); ++it) {
        if(rect.Contains(*it)) return true;
    }
    return false;
}

void ConnectionLine::UpdatePoints()
{
    if(m_type == ELEMENT_ELEMENT) {
        bool hasOneNode = true;
        wxPoint2DDouble pt1 = m_nodeList[0]->GetPosition();
        wxPoint2DDouble pt2;
        if(m_nodeList.size() == 1)
            pt2 = m_tmpSndPt;
        else {
            pt2 = m_nodeList[1]->GetPosition();
            hasOneNode = false;
        }
        wxPoint2DDouble midPt = (pt1 + pt2) / 2.0 + wxPoint2DDouble(0.0, m_lineOffset);

        m_pointList[0] = pt1;
        if(m_nodeList[0]->GetAngle() == 0.0)
            m_pointList[1] = m_pointList[0] + wxPoint2DDouble(-10, 0);
        else if(m_nodeList[0]->GetAngle() == 90.0)
            m_pointList[1] = m_pointList[0] + wxPoint2DDouble(0, -10);
        else if(m_nodeList[0]->GetAngle() == 180.0)
            m_pointList[1] = m_pointList[0] + wxPoint2DDouble(10, 0);
        else if(m_nodeList[0]->GetAngle() == 270.0)
            m_pointList[1] = m_pointList[0] + wxPoint2DDouble(0, 10);

        m_pointList[2] = m_pointList[1] + wxPoint2DDouble(0.0, midPt.m_y - m_pointList[1].m_y);

        m_pointList[5] = pt2;
        if(hasOneNode)
            m_pointList[4] = pt2;
        else {
            if(m_nodeList[1]->GetAngle() == 0.0)
                m_pointList[4] = m_pointList[5] + wxPoint2DDouble(-10, 0);
            else if(m_nodeList[1]->GetAngle() == 90.0)
                m_pointList[4] = m_pointList[5] + wxPoint2DDouble(0, -10);
            else if(m_nodeList[1]->GetAngle() == 180.0)
                m_pointList[4] = m_pointList[5] + wxPoint2DDouble(10, 0);
            else if(m_nodeList[1]->GetAngle() == 270.0)
                m_pointList[4] = m_pointList[5] + wxPoint2DDouble(0, 10);
        }

        m_pointList[3] = m_pointList[4] + wxPoint2DDouble(0.0, midPt.m_y - m_pointList[4].m_y);
    } else if(m_type == ELEMENT_LINE) {
        wxPoint2DDouble pt1 = m_nodeList[0]->GetPosition();
        wxPoint2DDouble pt2 = m_parentLine->GetMidPoint();
        //CONTINUE...
    }
}

bool ConnectionLine::AppendNode(Node* node, ControlElement* parent)
{
    if(m_nodeList.size() != 1) return false;
    if(m_nodeList[0] == node) return false;
    if(m_nodeList[0]->GetNodeType() == node->GetNodeType()) return false;
    auto nodeList = parent->GetNodeList();
    for(auto it = nodeList.begin(), itEnd = nodeList.end(); it != itEnd; ++it) {
        Node* parentNode = *it;
        if(parentNode == m_nodeList[0]) return false;
    }

    m_nodeList.push_back(node);
    node->SetConnected();
    return true;
}

void ConnectionLine::Move(wxPoint2DDouble position)
{
    m_lineOffset = m_moveStartOffset + position.m_y - m_moveStartPtY;
    UpdatePoints();
}

void ConnectionLine::StartMove(wxPoint2DDouble position)
{
    m_moveStartPtY = position.m_y;
    m_moveStartOffset = m_lineOffset;
}

wxPoint2DDouble ConnectionLine::GetMidPoint() const { return ((m_pointList[2] + m_pointList[3]) / 2.0); }

void ConnectionLine::SetParentLine(ConnectionLine* parent)
{
    if(parent) {
        m_type = ELEMENT_LINE;
        m_parentLine = parent;
    }
}