File: jucer_PaintElementImage.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 (428 lines) | stat: -rw-r--r-- 14,213 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
  ==============================================================================

   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_PaintElementImage.h"

PaintElementImage::PaintElementImage (PaintRoutine* pr)
    : PaintElement (pr, "Image"),
      opacity (1.0),
      mode (stretched)
{
}

PaintElementImage::~PaintElementImage() {}

const Drawable* PaintElementImage::getDrawable()
{
    if (JucerDocument* const document = getDocument())
        return document->getResources().getDrawable (resourceName);

    return nullptr;
}

void PaintElementImage::draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
{
    const Rectangle<int> r (position.getRectangle (parentArea, layout));

    if (const Drawable* const image = getDrawable())
    {
        image->drawWithin (g, r.toFloat(),
                           mode == stretched ? RectanglePlacement::stretchToFit
                                             : (mode == proportionalReducingOnly ? (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize)
                                                                                 : RectanglePlacement::centred),
                           (float) opacity);
    }
    else
    {
        g.setColour (Colours::grey.withAlpha (0.5f));
        g.fillRect (r);

        g.setColour (Colours::black);
        g.drawText ("(image missing)",
                    r.getX(), r.getY(), r.getWidth(), r.getHeight(),
                    Justification::centred, true);
    }
}

//==============================================================================
void PaintElementImage::getEditableProperties (Array <PropertyComponent*>& props, bool multipleSelected)
{
    PaintElement::getEditableProperties (props, multipleSelected);

    props.add (new ImageElementResourceProperty (this));
    props.add (new StretchModeProperty (this));
    props.add (new OpacityProperty (this));
    props.add (new ResetSizeProperty (this));
}

void PaintElementImage::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
{
    if (opacity > 0)
    {
        String x, y, w, h, r;
        positionToCode (position, getDocument()->getComponentLayout(), x, y, w, h);
        r << "{\n"
          << "    int x = " << x << ", y = " << y << ", width = " << w << ", height = " << h << ";\n"
          << "    //[UserPaintCustomArguments] Customize the painting arguments here..\n"
          << customPaintCode
          << "    //[/UserPaintCustomArguments]\n";

        if (dynamic_cast<const DrawableImage*> (getDrawable()))
        {
            const String imageVariable ("cachedImage_" + resourceName.replace ("::", "_") + "_" + String (code.getUniqueSuffix()));

            code.addImageResourceLoader (imageVariable, resourceName);

            if (opacity >= 254.0 / 255.0)
                r << "    g.setColour (juce::Colours::black);\n";
            else
                r << "    g.setColour (juce::Colours::black.withAlpha (" << CodeHelpers::floatLiteral (opacity, 3) << "));\n";

            if (mode == stretched)
            {
                r << "    g.drawImage (" << imageVariable << ",\n"
                  << "                 x, y, width, height,\n"
                  << "                 0, 0, " << imageVariable << ".getWidth(), " << imageVariable << ".getHeight());\n";
            }
            else
            {
                r << "    g.drawImageWithin (" << imageVariable << ",\n"
                  << "                       x, y, width, height,\n"
                  << "                       ";

                if (mode == proportionalReducingOnly)
                    r << "juce::RectanglePlacement::centred | juce::RectanglePlacement::onlyReduceInSize";
                else
                    r << "juce::RectanglePlacement::centred";

                r << ",\n"
                  << "                       false);\n";
            }

        }
        else
        {
            if (resourceName.isNotEmpty())
            {
                const String imageVariable ("drawable" + String (code.getUniqueSuffix()));

                code.privateMemberDeclarations
                    << "std::unique_ptr<juce::Drawable> " << imageVariable << ";\n";

                code.constructorCode
                    << imageVariable << " = juce::Drawable::createFromImageData ("
                    << resourceName << ", " << resourceName << "Size);\n";

                code.destructorCode
                    << imageVariable << " = nullptr;\n";

                if (opacity >= 254.0 / 255.0)
                    r << "    g.setColour (juce::Colours::black);\n";
                else
                    r << "    g.setColour (juce::Colours::black.withAlpha (" << CodeHelpers::floatLiteral (opacity, 3) << "));\n";

                r << "    jassert (" << imageVariable << " != nullptr);\n"
                  << "    if (" << imageVariable << " != nullptr)\n"
                  << "        " << imageVariable  << "->drawWithin (g, juce::Rectangle<int> (x, y, width, height).toFloat(),\n"
                  << "    " << String::repeatedString (" ", imageVariable.length() + 18)
                  << (mode == stretched ? "juce::RectanglePlacement::stretchToFit"
                                        : (mode == proportionalReducingOnly ? "juce::RectanglePlacement::centred | juce::RectanglePlacement::onlyReduceInSize"
                                                                            : "juce::RectanglePlacement::centred"))
                  << ", " << CodeHelpers::floatLiteral (opacity, 3) << ");\n";
            }
        }

        r << "}\n\n";

        paintMethodCode += r;
    }
}

void PaintElementImage::applyCustomPaintSnippets (StringArray& snippets)
{
    customPaintCode.clear();

    if (! snippets.isEmpty() && opacity > 0)
    {
        customPaintCode = snippets[0];
        snippets.remove (0);
    }
}

//==============================================================================
PaintElementImage::SetResourceAction::SetResourceAction (PaintElementImage* const element, const String& newResource_)
    : PaintElementUndoableAction <PaintElementImage> (element),
      newResource (newResource_)
{
    oldResource = element->getResource();
}

bool PaintElementImage::SetResourceAction::perform()
{
    showCorrectTab();
    getElement()->setResource (newResource, false);
    return true;
}

bool PaintElementImage::SetResourceAction::undo()
{
    showCorrectTab();
    getElement()->setResource (oldResource, false);
    return true;
}

void PaintElementImage::setResource (const String& newName, const bool undoable)
{
    if (resourceName != newName)
    {
        if (undoable)
        {
            perform (new SetResourceAction (this, newName),
                     "Change image resource");
        }
        else
        {
            resourceName = newName;
            changed();
        }
    }

    repaint();
}

String PaintElementImage::getResource() const
{
    return resourceName;
}

//==============================================================================
PaintElementImage::SetOpacityAction::SetOpacityAction (PaintElementImage* const element, const double newOpacity_)
    : PaintElementUndoableAction <PaintElementImage> (element),
      newOpacity (newOpacity_)
{
    oldOpacity = element->getOpacity();
}

bool PaintElementImage::SetOpacityAction::perform()
{
    showCorrectTab();
    getElement()->setOpacity (newOpacity, false);
    return true;
}

bool PaintElementImage::SetOpacityAction::undo()
{
    showCorrectTab();
    getElement()->setOpacity (oldOpacity, false);
    return true;
}

void PaintElementImage::setOpacity (double newOpacity, const bool undoable)
{
    newOpacity = jlimit (0.0, 1.0, newOpacity);

    if (opacity != newOpacity)
    {
        if (undoable)
        {
            perform (new SetOpacityAction (this, newOpacity),
                     "Change image opacity");
        }
        else
        {
            opacity = newOpacity;
            changed();
        }
    }
}

double PaintElementImage::getOpacity() const noexcept               { return opacity; }

//==============================================================================
const char* PaintElementImage::getTagName() noexcept         { return "IMAGE"; }

void PaintElementImage::resetToImageSize()
{
    if (const Drawable* const image = getDrawable())
    {
        if (PaintRoutineEditor* ed = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
        {
            const Rectangle<int> parentArea (ed->getComponentArea());

            Rectangle<int> r (getCurrentBounds (parentArea));
            Rectangle<float> b (image->getDrawableBounds());

            r.setSize ((int) (b.getWidth()  + 0.999f),
                       (int) (b.getHeight() + 0.999f));

            setCurrentBounds (r, parentArea, true);
        }
    }
}

//==============================================================================
PaintElementImage::SetStretchModeAction::SetStretchModeAction (PaintElementImage* const element, const StretchMode newValue_)
    : PaintElementUndoableAction <PaintElementImage> (element),
      newValue (newValue_)
{
    oldValue = element->getStretchMode();
}

bool PaintElementImage::SetStretchModeAction::perform()
{
    showCorrectTab();
    getElement()->setStretchMode (newValue, false);
    return true;
}

bool PaintElementImage::SetStretchModeAction::undo()
{
    showCorrectTab();
    getElement()->setStretchMode (oldValue, false);
    return true;
}

PaintElementImage::StretchMode PaintElementImage::getStretchMode() const noexcept   { return mode; }

void PaintElementImage::setStretchMode (const StretchMode newMode, const bool undoable)
{
    if (mode != newMode)
    {
        if (undoable)
        {
            perform (new SetStretchModeAction (this, newMode),
                     "Change image mode");
        }
        else
        {
            mode = newMode;
            changed();
        }
    }
}

//==============================================================================
XmlElement* PaintElementImage::createXml() const
{
    XmlElement* e = new XmlElement (getTagName());
    position.applyToXml (*e);
    e->setAttribute ("resource", resourceName);
    e->setAttribute ("opacity", opacity);
    e->setAttribute ("mode", (int) mode);

    return e;
}

bool PaintElementImage::loadFromXml (const XmlElement& xml)
{
    if (xml.hasTagName (getTagName()))
    {
        position.restoreFromXml (xml, position);
        resourceName = xml.getStringAttribute ("resource", String());
        opacity = xml.getDoubleAttribute ("opacity", 1.0);
        mode = (StretchMode) xml.getIntAttribute ("mode", (int) stretched);

        repaint();
        return true;
    }

    jassertfalse;
    return false;
}

//==============================================================================
PaintElementImage::ImageElementResourceProperty::ImageElementResourceProperty (PaintElementImage* const e)
    : ImageResourceProperty <PaintElementImage> (e, "image source")
{
}

void PaintElementImage::ImageElementResourceProperty::setResource (const String& newName)
{
    if (element != nullptr)
        element->setResource (newName, true);
}

String PaintElementImage::ImageElementResourceProperty::getResource() const
{
    if (element != nullptr)
        return element->getResource();

    return {};
}


//==============================================================================
PaintElementImage::OpacityProperty::OpacityProperty (PaintElementImage* const e)
    : SliderPropertyComponent ("opacity", 0.0, 1.0, 0.001),
      listener (e)
{
    listener.setPropertyToRefresh (*this);
}

void PaintElementImage::OpacityProperty::setValue (double newValue)
{
    listener.owner->getDocument()->getUndoManager().undoCurrentTransactionOnly();
    listener.owner->setOpacity (newValue, true);
}

double PaintElementImage::OpacityProperty::getValue() const
{
    return listener.owner->getOpacity();
}

PaintElementImage::StretchModeProperty::StretchModeProperty (PaintElementImage* const e)
    : ChoicePropertyComponent ("stretch mode"),
      listener (e)
{
    listener.setPropertyToRefresh (*this);

    choices.add ("Stretched to fit");
    choices.add ("Maintain aspect ratio");
    choices.add ("Maintain aspect ratio, only reduce in size");
}

void PaintElementImage::StretchModeProperty::setIndex (int newIndex)
{
    listener.owner->setStretchMode ((StretchMode) newIndex, true);
}

int PaintElementImage::StretchModeProperty::getIndex() const
{
    return (int) listener.owner->getStretchMode();
}

PaintElementImage::ResetSizeProperty::ResetSizeProperty (PaintElementImage* const e)
    : ButtonPropertyComponent ("reset", false),
      element (e)
{
}

void PaintElementImage::ResetSizeProperty::buttonClicked()
{
    element->resetToImageSize();
}

String PaintElementImage::ResetSizeProperty::getButtonText() const   { return "reset to image size"; }