-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcomponent_debugger.cpp
221 lines (182 loc) · 5.81 KB
/
component_debugger.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* You may need to uncomment the following line. */
//#include "credland_component_debugger.h"
/**
@internal - allows the components position to be edited
using a property panel.
*/
class ComponentBoundsEditor
:
public Value::Listener,
public Component
{
public:
ComponentBoundsEditor (ComponentDebugger::Debugger* owner)
:
owner (owner)
{
Array<PropertyComponent*> p;
p.add (new TextPropertyComponent (x, "x", 10, false));
p.add (new TextPropertyComponent (y, "y", 10, false));
p.add (new TextPropertyComponent (w, "w", 10, false));
p.add (new TextPropertyComponent (h, "h", 10, false));
panel.addProperties (p);
addAndMakeVisible (panel);
x.addListener (this);
y.addListener (this);
w.addListener (this);
h.addListener (this);
}
void resized() override
{
panel.setBounds (getLocalBounds());
}
void setComponent (Component* c)
{
component = c;
auto r = component->getBounds();
x = r.getX();
y = r.getY();
w = r.getWidth();
h = r.getHeight();
}
String getBoundsString()
{
return
x.getValue().toString() + ", " +
y.getValue().toString() + ", " +
w.getValue().toString() + ", " +
h.getValue().toString();
}
void valueChanged (Value&) override
{
if (component)
{
component->setBounds (x.getValue(),
y.getValue(),
w.getValue(),
h.getValue());
owner->setHighlight (component);
}
}
private:
ComponentDebugger::Debugger* owner;
WeakReference<Component> component;
Value x, y, h, w;
PropertyPanel panel;
};
/**********************************************************/
ComponentDebugger::Debugger::Debugger (Component* rootComponent)
:
root (rootComponent),
refreshButton ("Refresh"),
copyBoundsButton ("Copy Bounds"),
hideHighlightButton ("Hide Highlight")
{
addAndMakeVisible (refreshButton);
refreshButton.addListener (this);
refreshButton.setLookAndFeel (&lookAndFeel);
addAndMakeVisible (hideHighlightButton);
hideHighlightButton.addListener (this);
hideHighlightButton.setLookAndFeel (&lookAndFeel);
addAndMakeVisible (copyBoundsButton);
copyBoundsButton.addListener (this);
copyBoundsButton.setLookAndFeel (&lookAndFeel);
addAndMakeVisible (tree);
refresh();
tree.setLookAndFeel (&lookAndFeel);
boundsEditor = std::make_unique<ComponentBoundsEditor>(this);
addAndMakeVisible (boundsEditor.get());
}
ComponentDebugger::Debugger::~Debugger()
{}
void ComponentDebugger::Debugger::buttonClicked (Button* b)
{
if (b == &refreshButton)
refresh();
if (b == ©BoundsButton)
copyBoundsToClipboard();
if (b == &hideHighlightButton)
highlight = nullptr;
}
void ComponentDebugger::Debugger::copyBoundsToClipboard()
{
SystemClipboard::copyTextToClipboard (boundsEditor->getBoundsString());
}
void ComponentDebugger::Debugger::refresh()
{
tree.setRootItem (new ComponentTreeViewItem (this, root));
}
Rectangle<int> ComponentDebugger::Debugger::getLocationOf (Component* c)
{
return root->getLocalArea (c, c->getLocalBounds());
}
void ComponentDebugger::Debugger::resized()
{
const int boundsEditorHeight = 110;
{
int bw = 100;
int padding = 5;
int xpos = 0;
refreshButton.setBounds (0, 0, bw, 20);
xpos += bw + padding;
copyBoundsButton.setBounds (xpos, 0, bw, 20);
xpos += bw + padding;
hideHighlightButton.setBounds (xpos, 0, bw, 20);
}
tree.setBounds (0, 20, getWidth(), getHeight() - boundsEditorHeight - 20);
boundsEditor->setBounds (0, getHeight() - boundsEditorHeight, getWidth(), boundsEditorHeight);
}
void ComponentDebugger::Debugger::setComponentToEdit (Component* c)
{
boundsEditor->setComponent (c);
}
/**********************************************************/
ComponentDebugger::ComponentTreeViewItem::ComponentTreeViewItem (Debugger* owner,
Component* c)
:
owner (owner),
outsideBoundsFlag (false),
component (c)
{
numChildren = c->getNumChildComponents();
name = c->getName() + "[id=" + c->getComponentID() + "]";
type = typeid (*c).name();
isVisible = c->isVisible();
auto localBounds = c->getBounds();
zeroSizeFlag = localBounds.getWidth() == 0 || localBounds.getHeight() == 0;
auto parent = c->getParentComponent();
if (parent)
{
auto parentBounds = parent->getLocalBounds();
outsideBoundsFlag = ! (parentBounds.contains (localBounds));
}
/* We have to build the whole tree now as the components might be deleted in operation... */
for (int i = 0; i < numChildren; ++i)
addSubItem (new ComponentTreeViewItem (owner, c->getChildComponent (i)));
}
bool ComponentDebugger::ComponentTreeViewItem::mightContainSubItems()
{
return numChildren > 0;
}
void ComponentDebugger::ComponentTreeViewItem::paintItem (Graphics& g, int w, int h)
{
if (zeroSizeFlag)
g.fillAll (Colours::red.withAlpha (0.5f));
else if (outsideBoundsFlag)
g.fillAll (Colours::yellow.withAlpha (0.5f));
if (isVisible)
g.setColour (Colours::black);
else
g.setColour (Colours::grey);
auto bounds = component ? component->getBounds().toString() : "Component Deleted";
g.drawText (type + "name=" + name + " bounds=" + bounds,
0, 0, w, h, Justification::left, true);
}
void ComponentDebugger::ComponentTreeViewItem::itemSelectionChanged (bool nowSelected)
{
if (nowSelected && component != nullptr)
{
owner->setHighlight (component);
owner->setComponentToEdit (component);
}
}