File size: 3,606 Bytes
8be366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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


#include "NodeComponent.h"


NodeComponent::NodeComponent(const GraphViewTheme& theme, Graph::Node* model) : theme(theme), model(model) {
    
    model->addListener(this);
    
    cNodeBackgroundCurrent = theme.cNodeBackground;
    
    for(auto i = 0; i < model->ins.size(); i++) {
        auto pin = std::make_unique<PinComponent>(this, model->ins[i].get());
        ins.push_back(std::move(pin));
    }
    
    for(auto i = 0; i < model->outs.size(); i++) {
        auto pin = std::make_unique<PinComponent>(this, model->outs[i].get());
        outs.push_back(std::move(pin));
    }
    
    translation = AffineTransform::translation(0, 0);
    
    scaleFactor = theme.initialScaleFactor;
    scale = AffineTransform::scale(scaleFactor);
    
}

NodeComponent::~NodeComponent() {
    model->removeListener(this);
}

bool NodeComponent::hasIns() {
    return ins.size() > 0;
}

bool NodeComponent::hasOuts() {
    return outs.size() > 0;
}

int NodeComponent::boxHeight() {
    auto h = getHeight();
    if (hasIns()) h -= theme.pinHeight;
    if (hasOuts()) h -= theme.pinHeight;
    return h;
}

Rectangle<int> NodeComponent::boxBounds() {
    auto local = getLocalBounds();
    if (hasIns()) local.removeFromTop(theme.pinHeight);
    if (hasOuts()) local.removeFromBottom(theme.pinHeight);
    return local;
}

void NodeComponent::paint(Graphics& g) {
    Path p;
    auto bounds = boxBounds();
    //p.addRectangle(bounds);
    p.addRoundedRectangle(bounds, 3);
    
    if (selected)
        g.setColour(Colour(theme.cNodeBackgroundSelected));
    else
        g.setColour(Colour(cNodeBackgroundCurrent));
    
    g.fillPath(p);
    g.setColour(Colours::white);
    g.drawText(model->name, boxBounds(), Justification::centred, true);
}

void NodeComponent::resized() {
    
    {
        auto x = theme.pinSpacing;
        auto y = 0;
        auto w = theme.pinWidth;
        auto h = theme.pinHeight;
        for (auto& p : ins) {
            p->setBounds(x, y, w, h);
            addAndMakeVisible(p.get());
            x += w + theme.pinSpacing;
        }
    }
    
    {
        auto x = theme.pinSpacing;
        auto y = getHeight() - theme.pinHeight;
        auto w = theme.pinWidth;
        auto h = theme.pinHeight;
        for (auto& p : outs) {
            p->setBounds(x, y, w, h);
            addAndMakeVisible(p.get());
            x += w + theme.pinSpacing;
        }
    }
    
}

bool NodeComponent::hitTest(int x, int y) {
    auto inBox = boxBounds().contains(x, y);
    auto inTopPins = false;
    auto inBottomPins = false;
    
    for (auto& p : ins) {
        if (p->getBounds().contains(x, y)) {
            inTopPins = true;
            break;
        }
    }
    
    for (auto& p : outs) {
        if (p->getBounds().contains(x, y)) {
            inBottomPins = true;
            break;
        }
    }
    
    return inBox || inTopPins || inBottomPins;
}

void NodeComponent::mouseDown(const MouseEvent& e) {
}

void NodeComponent::mouseDrag(const MouseEvent& e) {
    
    
}

void NodeComponent::mouseUp(const MouseEvent& e) {
}


void NodeComponent::mouseMove(const MouseEvent& e) {
    if (boxBounds().contains(e.x, e.y)) {
        cNodeBackgroundCurrent = theme.cNodeBackgroundHover;
        repaint();
    }
}


void NodeComponent::mouseExit(const MouseEvent& e) {
    cNodeBackgroundCurrent = theme.cNodeBackground;
    repaint();
}


void NodeComponent::mouseDoubleClick(const MouseEvent& e) {
    //model->flow(model->name);
    //model->graph->dfs(model, [&](const auto& n) -> void { printf("visiting %s\n", n->name.c_str()); } );
}