File: jucer_PaintElementGroup.cpp

package info (click to toggle)
juce 6.1.3~ds0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,612 kB
  • sloc: cpp: 431,694; java: 2,592; ansic: 797; xml: 259; sh: 164; python: 126; makefile: 64
file content (226 lines) | stat: -rw-r--r-- 7,535 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
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
222
223
224
225
226
/*
  ==============================================================================

   This file is part of the JUCE library.
   Copyright (c) 2020 - Raw Material Software Limited

   JUCE is an open source library subject to commercial or open-source
   licensing.

   By using JUCE, you agree to the terms of both the JUCE 6 End-User License
   Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).

   End User License Agreement: www.juce.com/juce-6-licence
   Privacy Policy: www.juce.com/juce-privacy-policy

   Or: You may also use this code under the terms of the GPL v3 (see
   www.gnu.org/licenses).

   JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
   EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
   DISCLAIMED.

  ==============================================================================
*/

#include "../../Application/jucer_Headers.h"
#include "jucer_PaintElementGroup.h"

PaintElementGroup::PaintElementGroup (PaintRoutine* pr)
    : PaintElement (pr, "Group")
{
}

PaintElementGroup::~PaintElementGroup() {}

void PaintElementGroup::ungroup (const bool undoable)
{
    getOwner()->getSelectedElements().deselectAll();
    getOwner()->getSelectedPoints().deselectAll();

    const int index = getOwner()->indexOfElement (this);

    for (int i = 0; i < subElements.size(); ++i)
    {
        std::unique_ptr<XmlElement> xml (subElements.getUnchecked(i)->createXml());

        PaintElement* newOne = getOwner()->addElementFromXml (*xml, index, undoable);
        getOwner()->getSelectedElements().addToSelection (newOne);
    }

    getOwner()->removeElement (this, undoable);
}

void PaintElementGroup::groupSelected (PaintRoutine* routine)
{
    if (routine->getSelectedElements().getNumSelected() > 1)
    {
        auto* newGroup = new PaintElementGroup (routine);
        int frontIndex = -1;

        for (int i = 0; i < routine->getNumElements(); ++i)
        {
            if (routine->getSelectedElements().isSelected (routine->getElement (i)))
            {
                std::unique_ptr<XmlElement> xml (routine->getElement(i)->createXml());

                if (auto* newOne = ObjectTypes::createElementForXml (xml.get(), routine))
                    newGroup->subElements.add (newOne);

                if (i > frontIndex)
                    frontIndex = i;
            }
        }

        routine->deleteSelected();

        auto* g = routine->addNewElement (newGroup, frontIndex, true);
        routine->getSelectedElements().selectOnly (g);
    }
}

int PaintElementGroup::getNumElements() const noexcept                              { return subElements.size(); }

PaintElement* PaintElementGroup::getElement (const int index) const noexcept        { return subElements [index]; }

int PaintElementGroup::indexOfElement (const PaintElement* element) const noexcept  { return subElements.indexOf (element); }

bool PaintElementGroup::containsElement (const PaintElement* element) const
{
    if (subElements.contains (element))
        return true;

    for (int i = subElements.size(); --i >= 0;)
        if (PaintElementGroup* pg = dynamic_cast<PaintElementGroup*> (subElements.getUnchecked(i)))
            if (pg->containsElement (element))
                return true;

    return false;
}

//==============================================================================
void PaintElementGroup::setInitialBounds (int /*parentWidth*/, int /*parentHeight*/)
{
}

Rectangle<int> PaintElementGroup::getCurrentBounds (const Rectangle<int>& parentArea) const
{
    Rectangle<int> r;

    if (subElements.size() > 0)
    {
        r = subElements.getUnchecked(0)->getCurrentBounds (parentArea);

        for (int i = 1; i < subElements.size(); ++i)
            r = r.getUnion (subElements.getUnchecked(i)->getCurrentBounds (parentArea));
    }

    return r;
}

void PaintElementGroup::setCurrentBounds (const Rectangle<int>& b, const Rectangle<int>& parentArea, const bool undoable)
{
    Rectangle<int> newBounds (b);
    newBounds.setSize (jmax (1, newBounds.getWidth()),
                       jmax (1, newBounds.getHeight()));

    const Rectangle<int> current (getCurrentBounds (parentArea));

    if (newBounds != current)
    {
        const int dx = newBounds.getX() - current.getX();
        const int dy = newBounds.getY() - current.getY();

        const double scaleStartX = current.getX();
        const double scaleStartY = current.getY();
        const double scaleX = newBounds.getWidth() / (double) current.getWidth();
        const double scaleY = newBounds.getHeight() / (double) current.getHeight();

        for (int i = 0; i < subElements.size(); ++i)
        {
            PaintElement* const e = subElements.getUnchecked(i);

            Rectangle<int> pos (e->getCurrentBounds (parentArea));

            const int newX = roundToInt ((pos.getX() - scaleStartX) * scaleX + scaleStartX + dx);
            const int newY = roundToInt ((pos.getY() - scaleStartY) * scaleY + scaleStartY + dy);

            pos.setBounds (newX, newY,
                           roundToInt ((pos.getRight() - scaleStartX) * scaleX + scaleStartX + dx) - newX,
                           roundToInt ((pos.getBottom() - scaleStartY) * scaleY + scaleStartY + dy) - newY);

            e->setCurrentBounds (pos, parentArea, undoable);
        }
    }
}

//==============================================================================
void PaintElementGroup::draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
{
    for (int i = 0; i < subElements.size(); ++i)
        subElements.getUnchecked(i)->draw (g, layout, parentArea);
}

void PaintElementGroup::getEditableProperties (Array<PropertyComponent*>& props, bool multipleSelected)
{
    if (! multipleSelected)
        props.add (new UngroupProperty (this));
}

void PaintElementGroup::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
{
    for (int i = 0; i < subElements.size(); ++i)
        subElements.getUnchecked(i)->fillInGeneratedCode (code, paintMethodCode);
}

const char* PaintElementGroup::getTagName() noexcept        { return "GROUP"; }

XmlElement* PaintElementGroup::createXml() const
{
    XmlElement* e = new XmlElement (getTagName());

    for (int i = 0; i < subElements.size(); ++i)
    {
        XmlElement* const sub = subElements.getUnchecked(i)->createXml();
        e->addChildElement (sub);
    }

    return e;
}

bool PaintElementGroup::loadFromXml (const XmlElement& xml)
{
    if (xml.hasTagName (getTagName()))
    {
        for (auto* e : xml.getChildIterator())
            if (PaintElement* const pe = ObjectTypes::createElementForXml (e, owner))
                subElements.add (pe);

        return true;
    }

    jassertfalse;
    return false;
}

void PaintElementGroup::applyCustomPaintSnippets (StringArray& snippets)
{
    for (auto* e : subElements)
        e->applyCustomPaintSnippets (snippets);
}

PaintElementGroup::UngroupProperty::UngroupProperty (PaintElementGroup* const e)
    : ButtonPropertyComponent ("ungroup", false),
      element (e)
{
}

void PaintElementGroup::UngroupProperty::buttonClicked()
{
    element->ungroup (true);
}

String PaintElementGroup::UngroupProperty::getButtonText() const
{
    return "Ungroup";
}