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
|
/*
==============================================================================
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.
==============================================================================
*/
#pragma once
#include "../UI/jucer_PaintRoutineEditor.h"
#include "../UI/jucer_ComponentLayoutEditor.h"
//==============================================================================
/**
Base class for a property that edits the x, y, w, or h of a PositionedRectangle.
*/
class PositionPropertyBase : public PropertyComponent,
protected ChangeListener
{
public:
enum ComponentPositionDimension
{
componentX = 0,
componentY = 1,
componentWidth = 2,
componentHeight = 3
};
PositionPropertyBase (Component* comp,
const String& name,
ComponentPositionDimension dimension_,
const bool includeAnchorOptions_,
const bool allowRelativeOptions_,
ComponentLayout* layout_)
: PropertyComponent (name),
layout (layout_),
button ("mode"),
component (comp),
dimension (dimension_),
includeAnchorOptions (includeAnchorOptions_),
allowRelativeOptions (allowRelativeOptions_)
{
addAndMakeVisible (button);
button.setTriggeredOnMouseDown (true);
button.setConnectedEdges (TextButton::ConnectedOnLeft | TextButton::ConnectedOnRight);
button.onClick = [this]
{
SafePointer<PositionPropertyBase> safeThis { this };
showMenu (layout, [safeThis] (bool shouldRefresh)
{
if (safeThis == nullptr)
return;
if (shouldRefresh)
safeThis->refresh(); // (to clear the text editor if it's got focus)
});
};
textEditor.reset (new PositionPropLabel (*this));
addAndMakeVisible (textEditor.get());
}
String getText() const
{
RelativePositionedRectangle rpr (getPosition());
PositionedRectangle& p = rpr.rect;
String s;
switch (dimension)
{
case componentX:
if (p.getPositionModeX() == PositionedRectangle::proportionOfParentSize)
s << valueToString (p.getX() * 100.0) << '%';
else
s << valueToString (p.getX());
break;
case componentY:
if (p.getPositionModeY() == PositionedRectangle::proportionOfParentSize)
s << valueToString (p.getY() * 100.0) << '%';
else
s << valueToString (p.getY());
break;
case componentWidth:
if (p.getWidthMode() == PositionedRectangle::proportionalSize)
s << valueToString (p.getWidth() * 100.0) << '%';
else
s << valueToString (p.getWidth());
break;
case componentHeight:
if (p.getHeightMode() == PositionedRectangle::proportionalSize)
s << valueToString (p.getHeight() * 100.0) << '%';
else
s << valueToString (p.getHeight());
break;
default:
jassertfalse;
break;
};
return s;
}
static String valueToString (const double n)
{
return String (roundToInt (n * 1000.0) / 1000.0);
}
void setText (const String& newText)
{
RelativePositionedRectangle rpr (getPosition());
PositionedRectangle p (rpr.rect);
const double value = newText.getDoubleValue();
switch (dimension)
{
case componentX:
if (p.getPositionModeX() == PositionedRectangle::proportionOfParentSize)
p.setX (value / 100.0);
else
p.setX (value);
break;
case componentY:
if (p.getPositionModeY() == PositionedRectangle::proportionOfParentSize)
p.setY (value / 100.0);
else
p.setY (value);
break;
case componentWidth:
if (p.getWidthMode() == PositionedRectangle::proportionalSize)
p.setWidth (value / 100.0);
else
p.setWidth (value);
break;
case componentHeight:
if (p.getHeightMode() == PositionedRectangle::proportionalSize)
p.setHeight (value / 100.0);
else
p.setHeight (value);
break;
default:
jassertfalse;
break;
};
if (p != rpr.rect)
{
rpr.rect = p;
setPosition (rpr);
}
}
void changeListenerCallback (ChangeBroadcaster*)
{
refresh();
}
void showMenu (ComponentLayout* compLayout, std::function<void (bool)> callback)
{
RelativePositionedRectangle rpr (getPosition());
PositionedRectangle p (rpr.rect);
PositionedRectangle::AnchorPoint xAnchor = p.getAnchorPointX();
PositionedRectangle::AnchorPoint yAnchor = p.getAnchorPointY();
PositionedRectangle::PositionMode xMode = p.getPositionModeX();
PositionedRectangle::PositionMode yMode = p.getPositionModeY();
PositionedRectangle::SizeMode sizeW = p.getWidthMode();
PositionedRectangle::SizeMode sizeH = p.getHeightMode();
String relCompName ("parent");
if (Component* const relComp = compLayout != nullptr ? compLayout->getComponentRelativePosTarget (component, (int) dimension)
: nullptr)
relCompName = compLayout->getComponentMemberVariableName (relComp);
jassert (relCompName.isNotEmpty());
PopupMenu m;
if (dimension == componentX || dimension == componentY)
{
const PositionedRectangle::PositionMode posMode = (dimension == componentX) ? xMode : yMode;
m.addItem (10, ((dimension == componentX) ? "Absolute distance from left of "
: "Absolute distance from top of ") + relCompName,
true, posMode == PositionedRectangle::absoluteFromParentTopLeft);
m.addItem (11, ((dimension == componentX) ? "Absolute distance from right of "
: "Absolute distance from bottom of ") + relCompName,
true, posMode == PositionedRectangle::absoluteFromParentBottomRight);
m.addItem (12, "Absolute distance from centre of " + relCompName,
true, posMode == PositionedRectangle::absoluteFromParentCentre);
m.addItem (13, ((dimension == componentX) ? "Percentage of width of "
: "Percentage of height of ") + relCompName,
true, posMode == PositionedRectangle::proportionOfParentSize);
m.addSeparator();
if (includeAnchorOptions)
{
const PositionedRectangle::AnchorPoint anchor = (dimension == componentX) ? xAnchor : yAnchor;
m.addItem (14, (dimension == componentX) ? "Anchored at left of component"
: "Anchored at top of component",
true, anchor == PositionedRectangle::anchorAtLeftOrTop);
m.addItem (15, "Anchored at centre of component", true, anchor == PositionedRectangle::anchorAtCentre);
m.addItem (16, (dimension == componentX) ? "Anchored at right of component"
: "Anchored at bottom of component",
true, anchor == PositionedRectangle::anchorAtRightOrBottom);
}
}
else
{
const PositionedRectangle::SizeMode sizeMode = (dimension == componentWidth) ? sizeW : sizeH;
m.addItem (20, (dimension == componentWidth) ? "Absolute width"
: "Absolute height",
true, sizeMode == PositionedRectangle::absoluteSize);
m.addItem (21, ((dimension == componentWidth) ? "Percentage of width of "
: "Percentage of height of ") + relCompName,
true, sizeMode == PositionedRectangle::proportionalSize);
m.addItem (22, ((dimension == componentWidth) ? "Subtracted from width of "
: "Subtracted from height of ") + relCompName,
true, sizeMode == PositionedRectangle::parentSizeMinusAbsolute);
}
if (allowRelativeOptions && compLayout != nullptr)
{
m.addSeparator();
m.addSubMenu ("Relative to", compLayout->getRelativeTargetMenu (component, (int) dimension));
}
m.showMenuAsync (PopupMenu::Options().withTargetComponent (&button),
[compLayout, callback, xAnchor, yAnchor, xMode, yMode, sizeW, sizeH, p, rpr,
ref = SafePointer<PositionPropertyBase> { this }] (int menuResult) mutable
{
if (menuResult == 0 || ref == nullptr)
{
callback (false);
return;
}
switch (menuResult)
{
case 10:
if (ref->dimension == componentX)
xMode = PositionedRectangle::absoluteFromParentTopLeft;
else
yMode = PositionedRectangle::absoluteFromParentTopLeft;
break;
case 11:
if (ref->dimension == componentX)
xMode = PositionedRectangle::absoluteFromParentBottomRight;
else
yMode = PositionedRectangle::absoluteFromParentBottomRight;
break;
case 12:
if (ref->dimension == componentX)
xMode = PositionedRectangle::absoluteFromParentCentre;
else
yMode = PositionedRectangle::absoluteFromParentCentre;
break;
case 13:
if (ref->dimension == componentX)
xMode = PositionedRectangle::proportionOfParentSize;
else
yMode = PositionedRectangle::proportionOfParentSize;
break;
case 14:
if (ref->dimension == componentX)
xAnchor = PositionedRectangle::anchorAtLeftOrTop;
else
yAnchor = PositionedRectangle::anchorAtLeftOrTop;
break;
case 15:
if (ref->dimension == componentX)
xAnchor = PositionedRectangle::anchorAtCentre;
else
yAnchor = PositionedRectangle::anchorAtCentre;
break;
case 16:
if (ref->dimension == componentX)
xAnchor = PositionedRectangle::anchorAtRightOrBottom;
else
yAnchor = PositionedRectangle::anchorAtRightOrBottom;
break;
case 20:
if (ref->dimension == componentWidth)
sizeW = PositionedRectangle::absoluteSize;
else
sizeH = PositionedRectangle::absoluteSize;
break;
case 21:
if (ref->dimension == componentWidth)
sizeW = PositionedRectangle::proportionalSize;
else
sizeH = PositionedRectangle::proportionalSize;
break;
case 22:
if (ref->dimension == componentWidth)
sizeW = PositionedRectangle::parentSizeMinusAbsolute;
else
sizeH = PositionedRectangle::parentSizeMinusAbsolute;
break;
default:
if (ref->allowRelativeOptions && compLayout != nullptr)
compLayout->processRelativeTargetMenuResult (ref->component, (int) ref->dimension, menuResult);
break;
}
const auto parentArea = [&]() -> Rectangle<int>
{
if (ref->component->findParentComponentOfClass<ComponentLayoutEditor>() != nullptr)
return { ref->component->getParentWidth(), ref->component->getParentHeight() };
if (auto pre = dynamic_cast<PaintRoutineEditor*> (ref->component->getParentComponent()))
return pre->getComponentArea();
jassertfalse;
return {};
}();
int x, xw, y, yh, w, h;
rpr.getRelativeTargetBounds (parentArea, compLayout, x, xw, y, yh, w, h);
PositionedRectangle xyRect (p);
PositionedRectangle whRect (p);
xyRect.setModes (xAnchor, xMode, yAnchor, yMode, sizeW, sizeH,
Rectangle<int> (x, y, xw, yh));
whRect.setModes (xAnchor, xMode, yAnchor, yMode, sizeW, sizeH,
Rectangle<int> (x, y, w, h));
p.setModes (xAnchor, xMode, yAnchor, yMode, sizeW, sizeH,
Rectangle<int> (x, y, xw, yh));
p.setX (xyRect.getX());
p.setY (xyRect.getY());
p.setWidth (whRect.getWidth());
p.setHeight (whRect.getHeight());
if (p != rpr.rect)
{
rpr.rect = p;
ref->setPosition (rpr);
}
callback (true);
});
}
void resized()
{
const Rectangle<int> r (getLookAndFeel().getPropertyComponentContentPosition (*this));
button.changeWidthToFitText (r.getHeight());
button.setTopRightPosition (r.getRight(), r.getY());
textEditor->setBounds (r.getX(), r.getY(), button.getX() - r.getX(), r.getHeight());
}
void refresh()
{
textEditor->setText (getText(), dontSendNotification);
}
void textWasEdited()
{
const String newText (textEditor->getText());
if (getText() != newText)
setText (newText);
}
//==============================================================================
virtual void setPosition (const RelativePositionedRectangle& newPos) = 0;
virtual RelativePositionedRectangle getPosition() const = 0;
protected:
class PositionPropLabel : public Label
{
PositionPropertyBase& owner;
public:
PositionPropLabel (PositionPropertyBase& owner_)
: Label (String(), String()),
owner (owner_)
{
setEditable (true, true, false);
lookAndFeelChanged();
}
TextEditor* createEditorComponent() override
{
TextEditor* ed = Label::createEditorComponent();
ed->setInputRestrictions (14, "0123456789.-%");
return ed;
}
void textWasEdited() override
{
owner.textWasEdited();
}
void lookAndFeelChanged() override
{
setColour (backgroundColourId, findColour (widgetBackgroundColourId));
setColour (textColourId, findColour (widgetTextColourId));
}
};
ComponentLayout* layout;
std::unique_ptr<PositionPropLabel> textEditor;
TextButton button;
Component* component;
ComponentPositionDimension dimension;
const bool includeAnchorOptions, allowRelativeOptions;
};
|