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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
/*
==============================================================================
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_PaintRoutine.h"
#include "jucer_JucerDocument.h"
#include "jucer_ObjectTypes.h"
#include "PaintElements/jucer_PaintElementUndoableAction.h"
#include "PaintElements/jucer_PaintElementPath.h"
#include "PaintElements/jucer_PaintElementImage.h"
#include "PaintElements/jucer_PaintElementGroup.h"
#include "UI/jucer_JucerDocumentEditor.h"
#include "../Application/jucer_Application.h"
//==============================================================================
PaintRoutine::PaintRoutine()
: document (nullptr),
backgroundColour (ProjucerApplication::getApp().lookAndFeel.findColour (backgroundColourId))
{
clear();
}
PaintRoutine::~PaintRoutine()
{
elements.clear(); // do this explicitly before the scalar destructor because these
// objects will be listeners on this object
}
//==============================================================================
void PaintRoutine::changed()
{
if (document != nullptr)
document->changed();
}
bool PaintRoutine::perform (UndoableAction* action, const String& actionName)
{
if (document != nullptr)
return document->getUndoManager().perform (action, actionName);
std::unique_ptr<UndoableAction> deleter (action);
action->perform();
return false;
}
void PaintRoutine::setBackgroundColour (Colour newColour) noexcept
{
backgroundColour = newColour;
changed();
}
void PaintRoutine::clear()
{
if (elements.size() > 0)
{
elements.clear();
changed();
}
}
//==============================================================================
class AddXmlElementAction : public UndoableAction
{
public:
AddXmlElementAction (PaintRoutine& routine_, XmlElement* xml_)
: routine (routine_), xml (xml_)
{
}
bool perform()
{
showCorrectTab();
PaintElement* newElement = routine.addElementFromXml (*xml, -1, false);
jassert (newElement != nullptr);
indexAdded = routine.indexOfElement (newElement);
jassert (indexAdded >= 0);
return indexAdded >= 0;
}
bool undo()
{
showCorrectTab();
routine.removeElement (routine.getElement (indexAdded), false);
return true;
}
int getSizeInUnits() { return 10; }
int indexAdded;
private:
PaintRoutine& routine;
std::unique_ptr<XmlElement> xml;
void showCorrectTab() const
{
if (JucerDocumentEditor* const ed = JucerDocumentEditor::getActiveDocumentHolder())
ed->showGraphics (&routine);
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AddXmlElementAction)
};
PaintElement* PaintRoutine::addElementFromXml (const XmlElement& xml, const int index, const bool undoable)
{
selectedPoints.deselectAll();
if (undoable && document != nullptr)
{
AddXmlElementAction* action = new AddXmlElementAction (*this, new XmlElement (xml));
document->getUndoManager().perform (action, "Add new element");
return elements [action->indexAdded];
}
if (PaintElement* const newElement = ObjectTypes::createElementForXml (&xml, this))
{
elements.insert (index, newElement);
changed();
return newElement;
}
return nullptr;
}
PaintElement* PaintRoutine::addNewElement (PaintElement* e, const int index, const bool undoable)
{
if (e != nullptr)
{
std::unique_ptr<PaintElement> deleter (e);
std::unique_ptr<XmlElement> xml (e->createXml());
e = addElementFromXml (*xml, index, undoable);
}
return e;
}
//==============================================================================
class DeleteElementAction : public PaintElementUndoableAction <PaintElement>
{
public:
explicit DeleteElementAction (PaintElement* const element)
: PaintElementUndoableAction <PaintElement> (element),
oldIndex (-1)
{
xml.reset (element->createXml());
oldIndex = routine.indexOfElement (element);
}
bool perform()
{
showCorrectTab();
routine.removeElement (getElement(), false);
return true;
}
bool undo()
{
PaintElement* newElement = routine.addElementFromXml (*xml, oldIndex, false);
showCorrectTab();
return newElement != nullptr;
}
int getSizeInUnits() { return 10; }
private:
std::unique_ptr<XmlElement> xml;
int oldIndex;
};
void PaintRoutine::removeElement (PaintElement* element, const bool undoable)
{
if (elements.contains (element))
{
if (undoable)
{
perform (new DeleteElementAction (element),
"Delete " + element->getTypeName());
}
else
{
selectedElements.deselect (element);
selectedPoints.deselectAll();
selectedPoints.changed (true);
selectedElements.changed (true);
elements.removeObject (element);
changed();
}
}
}
//==============================================================================
class FrontOrBackElementAction : public PaintElementUndoableAction <PaintElement>
{
public:
FrontOrBackElementAction (PaintElement* const element, int newIndex_)
: PaintElementUndoableAction <PaintElement> (element),
newIndex (newIndex_)
{
oldIndex = routine.indexOfElement (element);
}
bool perform()
{
showCorrectTab();
PaintElement* e = routine.getElement (oldIndex);
routine.moveElementZOrder (oldIndex, newIndex);
newIndex = routine.indexOfElement (e);
return true;
}
bool undo()
{
showCorrectTab();
routine.moveElementZOrder (newIndex, oldIndex);
return true;
}
private:
int newIndex, oldIndex;
};
void PaintRoutine::moveElementZOrder (int oldIndex, int newIndex)
{
jassert (elements [oldIndex] != nullptr);
if (oldIndex != newIndex && elements [oldIndex] != nullptr)
{
elements.move (oldIndex, newIndex);
changed();
}
}
void PaintRoutine::elementToFront (PaintElement* element, const bool undoable)
{
if (element != nullptr && elements.contains (element))
{
if (undoable)
perform (new FrontOrBackElementAction (element, -1), "Move elements to front");
else
moveElementZOrder (elements.indexOf (element), -1);
}
}
void PaintRoutine::elementToBack (PaintElement* element, const bool undoable)
{
if (element != nullptr && elements.contains (element))
{
if (undoable)
perform (new FrontOrBackElementAction (element, 0), "Move elements to back");
else
moveElementZOrder (elements.indexOf (element), 0);
}
}
//==============================================================================
const char* const PaintRoutine::clipboardXmlTag = "PAINTELEMENTS";
void PaintRoutine::copySelectedToClipboard()
{
if (selectedElements.getNumSelected() == 0)
return;
XmlElement clip (clipboardXmlTag);
for (auto* pe : elements)
if (selectedElements.isSelected (pe))
clip.addChildElement (pe->createXml());
SystemClipboard::copyTextToClipboard (clip.toString());
}
void PaintRoutine::paste()
{
if (auto doc = parseXMLIfTagMatches (SystemClipboard::getTextFromClipboard(), clipboardXmlTag))
{
selectedElements.deselectAll();
selectedPoints.deselectAll();
for (auto* e : doc->getChildIterator())
if (PaintElement* newElement = addElementFromXml (*e, -1, true))
selectedElements.addToSelection (newElement);
}
}
void PaintRoutine::deleteSelected()
{
const SelectedItemSet<PaintElement*> temp1 (selectedElements);
const SelectedItemSet<PathPoint*> temp2 (selectedPoints);
if (temp2.getNumSelected() > 0)
{
selectedPoints.deselectAll();
selectedPoints.changed (true); // synchronous message to get rid of any property components
// if any points are selected, just delete them, and not the element, which may
// also be selected..
for (int i = temp2.getNumSelected(); --i >= 0;)
temp2.getSelectedItem (i)->deleteFromPath();
changed();
}
else if (temp1.getNumSelected() > 0)
{
selectedElements.deselectAll();
selectedElements.changed (true);
for (int i = temp1.getNumSelected(); --i >= 0;)
removeElement (temp1.getSelectedItem (i), true);
changed();
}
}
void PaintRoutine::selectAll()
{
if (selectedPoints.getNumSelected() > 0)
{
if (const PaintElementPath* path = selectedPoints.getSelectedItem (0)->owner)
for (int i = 0; i < path->getNumPoints(); ++i)
selectedPoints.addToSelection (path->getPoint (i));
}
else
{
for (int i = 0; i < elements.size(); ++i)
selectedElements.addToSelection (elements.getUnchecked (i));
}
}
void PaintRoutine::selectedToFront()
{
const SelectedItemSet<PaintElement*> temp (selectedElements);
for (int i = temp.getNumSelected(); --i >= 0;)
elementToFront (temp.getSelectedItem(i), true);
}
void PaintRoutine::selectedToBack()
{
const SelectedItemSet<PaintElement*> temp (selectedElements);
for (int i = 0; i < temp.getNumSelected(); ++i)
elementToBack (temp.getSelectedItem(i), true);
}
void PaintRoutine::alignTop()
{
if (selectedElements.getNumSelected() > 1)
{
auto* main = selectedElements.getSelectedItem (0);
auto yPos = main->getY();
for (auto* other : selectedElements)
{
if (other != main)
other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (other->getX(),
yPos), main, true);
}
}
}
void PaintRoutine::alignRight()
{
if (selectedElements.getNumSelected() > 1)
{
auto* main = selectedElements.getSelectedItem (0);
auto rightPos = main->getRight();
for (auto* other : selectedElements)
{
if (other != main)
other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (rightPos - other->getWidth(),
other->getY()), main, true);
}
}
}
void PaintRoutine::alignBottom()
{
if (selectedElements.getNumSelected() > 1)
{
auto* main = selectedElements.getSelectedItem (0);
auto bottomPos = main->getBottom();
for (auto* other : selectedElements)
{
if (other != main)
other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (other->getX(),
bottomPos - other->getHeight()), main, true);
}
}
}
void PaintRoutine::alignLeft()
{
if (selectedElements.getNumSelected() > 1)
{
auto* main = selectedElements.getSelectedItem (0);
auto xPos = main->getX();
for (auto* other : selectedElements)
{
if (other != main)
other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (xPos,
other->getY()), main, true);
}
}
}
void PaintRoutine::groupSelected()
{
PaintElementGroup::groupSelected (this);
}
void PaintRoutine::ungroupSelected()
{
const SelectedItemSet<PaintElement*> temp (selectedElements);
for (int i = 0; i < temp.getNumSelected(); ++i)
if (PaintElementGroup* const pg = dynamic_cast<PaintElementGroup*> (temp.getSelectedItem (i)))
pg->ungroup (true);
}
void PaintRoutine::bringLostItemsBackOnScreen (const Rectangle<int>& parentArea)
{
for (auto* c : elements)
{
auto r = c->getCurrentBounds (parentArea);
if (! r.intersects (parentArea))
{
r.setPosition (parentArea.getCentreX(), parentArea.getCentreY());
c->setCurrentBounds (r, parentArea, true);
}
}
}
void PaintRoutine::startDragging (const Rectangle<int>& parentArea)
{
for (auto* c : elements)
{
auto r = c->getCurrentBounds (parentArea);
c->getProperties().set ("xDragStart", r.getX());
c->getProperties().set ("yDragStart", r.getY());
}
getDocument()->beginTransaction();
}
void PaintRoutine::dragSelectedComps (int dx, int dy, const Rectangle<int>& parentArea)
{
getDocument()->getUndoManager().undoCurrentTransactionOnly();
if (document != nullptr && selectedElements.getNumSelected() > 1)
{
dx = document->snapPosition (dx);
dy = document->snapPosition (dy);
}
for (int i = 0; i < selectedElements.getNumSelected(); ++i)
{
PaintElement* const c = selectedElements.getSelectedItem (i);
const int startX = c->getProperties() ["xDragStart"];
const int startY = c->getProperties() ["yDragStart"];
Rectangle<int> r (c->getCurrentBounds (parentArea));
if (document != nullptr && selectedElements.getNumSelected() == 1)
{
r.setPosition (document->snapPosition (startX + dx),
document->snapPosition (startY + dy));
}
else
{
r.setPosition (startX + dx,
startY + dy);
}
c->setCurrentBounds (r, parentArea, true);
}
changed();
}
void PaintRoutine::endDragging()
{
getDocument()->beginTransaction();
}
//==============================================================================
void PaintRoutine::fillWithBackground (Graphics& g, const bool drawOpaqueBackground)
{
if ((! backgroundColour.isOpaque()) && drawOpaqueBackground)
{
g.fillCheckerBoard (Rectangle<float> ((float) g.getClipBounds().getRight(),
(float) g.getClipBounds().getBottom()),
50.0f, 50.0f,
Colour (0xffdddddd).overlaidWith (backgroundColour),
Colour (0xffffffff).overlaidWith (backgroundColour));
}
else
{
g.fillAll (backgroundColour);
}
}
void PaintRoutine::drawElements (Graphics& g, const Rectangle<int>& relativeTo)
{
Component temp;
temp.setBounds (relativeTo);
for (auto* e : elements)
e->draw (g, getDocument()->getComponentLayout(), relativeTo);
}
//==============================================================================
void PaintRoutine::dropImageAt (const File& f, int x, int y)
{
std::unique_ptr<Drawable> d (Drawable::createFromImageFile (f));
if (d != nullptr)
{
auto bounds = d->getDrawableBounds();
d.reset();
auto* newElement = addNewElement (ObjectTypes::createNewImageElement (this), -1, true);
if (auto* pei = dynamic_cast<PaintElementImage*> (newElement))
{
String resourceName (getDocument()->getResources().findUniqueName (f.getFileName()));
if (auto* existingResource = getDocument()->getResources().getResourceForFile (f))
{
resourceName = existingResource->name;
}
else
{
MemoryBlock data;
f.loadFileAsData (data);
getDocument()->getResources().add (resourceName, f.getFullPathName(), data);
}
pei->setResource (resourceName, true);
const int imageW = (int) (bounds.getRight() + 0.999f);
const int imageH = (int) (bounds.getBottom() + 0.999f);
RelativePositionedRectangle pr;
pr.rect.setX (x - imageW / 2);
pr.rect.setY (y - imageH / 2);
pr.rect.setWidth (imageW);
pr.rect.setHeight (imageH);
pei->setPosition (pr, true);
getSelectedElements().selectOnly (pei);
}
}
}
//==============================================================================
const char* PaintRoutine::xmlTagName = "BACKGROUND";
XmlElement* PaintRoutine::createXml() const
{
auto* xml = new XmlElement (xmlTagName);
xml->setAttribute ("backgroundColour", backgroundColour.toString());
for (auto* e : elements)
xml->addChildElement (e->createXml());
return xml;
}
bool PaintRoutine::loadFromXml (const XmlElement& xml)
{
if (xml.hasTagName (xmlTagName))
{
backgroundColour = Colour::fromString (xml.getStringAttribute ("backgroundColour", Colours::white.toString()));
clear();
for (auto* e : xml.getChildIterator())
if (auto* newElement = ObjectTypes::createElementForXml (e, this))
elements.add (newElement);
return true;
}
return false;
}
void PaintRoutine::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) const
{
if (! backgroundColour.isTransparent())
paintMethodCode << "g.fillAll (" << CodeHelpers::colourToCode (backgroundColour) << ");\n\n";
for (auto* e : elements)
e->fillInGeneratedCode (code, paintMethodCode);
}
void PaintRoutine::applyCustomPaintSnippets (StringArray& snippets)
{
for (auto* e : elements)
e->applyCustomPaintSnippets (snippets);
}
|