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
|
/*
==============================================================================
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_ButtonDocument.h"
#include "../jucer_UtilityFunctions.h"
//==============================================================================
static const int normalOff = 0;
static const int overOff = 1;
static const int downOff = 2;
static const int normalOn = 3;
static const int overOn = 4;
static const int downOn = 5;
static const int background = 6;
//==============================================================================
ButtonDocument::ButtonDocument (SourceCodeDocument* c)
: JucerDocument (c)
{
paintStatesEnabled [normalOff] = true;
paintStatesEnabled [overOff] = true;
paintStatesEnabled [downOff] = true;
paintStatesEnabled [normalOn] = false;
paintStatesEnabled [overOn] = false;
paintStatesEnabled [downOn] = false;
paintStatesEnabled [background] = false;
parentClasses = "public juce::Button";
for (int i = 7; --i >= 0;)
{
paintRoutines[i].reset (new PaintRoutine());
paintRoutines[i]->setDocument (this);
paintRoutines[i]->setBackgroundColour (Colours::transparentBlack);
}
}
ButtonDocument::~ButtonDocument()
{
}
static const char* const stateNames[] =
{
"normal", "over", "down",
"normal on", "over on", "down on",
"common background"
};
static int stateNameToIndex (const String& name)
{
for (int i = 7; --i >= 0;)
if (name.equalsIgnoreCase (stateNames[i]))
return i;
jassertfalse;
return normalOff;
}
int ButtonDocument::getNumPaintRoutines() const
{
int n = 0;
for (int i = 7; --i >= 0;)
if (paintStatesEnabled [i])
++n;
return n;
}
StringArray ButtonDocument::getPaintRoutineNames() const
{
StringArray s;
for (int i = 0; i < 7; ++i)
if (paintStatesEnabled [i])
s.add (stateNames [i]);
return s;
}
PaintRoutine* ButtonDocument::getPaintRoutine (const int index) const
{
int n = 0;
for (int i = 0; i < 7; ++i)
{
if (paintStatesEnabled [i])
{
if (index == n)
return paintRoutines[i].get();
++n;
}
}
jassertfalse;
return {};
}
void ButtonDocument::setStatePaintRoutineEnabled (const int index, bool b)
{
jassert (index > 0 && index < 7);
if (paintStatesEnabled [index] != b)
{
paintStatesEnabled [index] = b;
changed();
}
}
bool ButtonDocument::isStatePaintRoutineEnabled (const int index) const
{
return paintStatesEnabled [index];
}
int ButtonDocument::chooseBestEnabledPaintRoutine (int paintRoutineWanted) const
{
switch (paintRoutineWanted)
{
case normalOff: return normalOff;
case overOff: return paintStatesEnabled [overOff] ? overOff : normalOff;
case downOff: return paintStatesEnabled [downOff] ? downOff : chooseBestEnabledPaintRoutine (overOff);
case normalOn: return paintStatesEnabled [normalOn] ? normalOn : normalOff;
case overOn: return paintStatesEnabled [overOn] ? overOn : (paintStatesEnabled [normalOn] ? normalOn : chooseBestEnabledPaintRoutine (overOff));
case downOn: return paintStatesEnabled [downOn] ? downOn : ((paintStatesEnabled [overOn] || paintStatesEnabled [normalOn])
? chooseBestEnabledPaintRoutine (overOn)
: chooseBestEnabledPaintRoutine (downOff));
default: jassertfalse; break;
}
return normalOff;
}
//==============================================================================
String ButtonDocument::getTypeName() const
{
return "Button";
}
JucerDocument* ButtonDocument::createCopy()
{
auto newOne = new ButtonDocument (cpp);
newOne->resources = resources;
newOne->loadFromXml (*createXml());
return newOne;
}
std::unique_ptr<XmlElement> ButtonDocument::createXml() const
{
auto doc = JucerDocument::createXml();
for (int i = 0; i < 7; ++i)
{
auto e = paintRoutines[i]->createXml();
e->setAttribute ("buttonState", stateNames [i]);
e->setAttribute ("enabled", paintStatesEnabled [i]);
doc->addChildElement (e);
}
return doc;
}
bool ButtonDocument::loadFromXml (const XmlElement& xml)
{
if (JucerDocument::loadFromXml (xml))
{
for (int i = 7; --i >= 0;)
paintStatesEnabled [i] = false;
for (auto* e : xml.getChildWithTagNameIterator (PaintRoutine::xmlTagName))
{
const int stateIndex = stateNameToIndex (e->getStringAttribute ("buttonState"));
paintRoutines [stateIndex]->loadFromXml (*e);
paintStatesEnabled [stateIndex] = e->getBoolAttribute ("enabled", stateIndex < normalOn);
}
changed();
getUndoManager().clearUndoHistory();
return true;
}
return false;
}
void ButtonDocument::getOptionalMethods (StringArray& baseClasses,
StringArray& returnValues,
StringArray& methods,
StringArray& initialContents) const
{
JucerDocument::getOptionalMethods (baseClasses, returnValues, methods, initialContents);
addMethod ("juce::Button", "void", "clicked()", "", baseClasses, returnValues, methods, initialContents);
addMethod ("juce::Button", "void", "buttonStateChanged()", "", baseClasses, returnValues, methods, initialContents);
}
//==============================================================================
class ButtonStatePaintEnabledProperty : public BooleanPropertyComponent,
private ChangeListener
{
public:
ButtonStatePaintEnabledProperty (const String& name, ButtonDocument& doc, const int stateMethod_)
: BooleanPropertyComponent (name, "enabled", "disabled"),
document (doc),
stateMethod (stateMethod_)
{
document.addChangeListener (this);
}
~ButtonStatePaintEnabledProperty()
{
document.removeChangeListener (this);
}
void setState (bool newState)
{
document.setStatePaintRoutineEnabled (stateMethod, newState);
}
bool getState() const
{
return document.isStatePaintRoutineEnabled (stateMethod);
}
private:
void changeListenerCallback (ChangeBroadcaster*)
{
refresh();
}
ButtonDocument& document;
const int stateMethod;
};
void ButtonDocument::addExtraClassProperties (PropertyPanel& panel)
{
Array <PropertyComponent*> props;
for (int i = 1; i < 7; ++i)
props.add (new ButtonStatePaintEnabledProperty (stateNames[i], *this, i));
panel.addSection ("Button paint routines", props);
}
//==============================================================================
class ButtonTestComponent : public Button
{
public:
ButtonTestComponent (ButtonDocument* const doc, const bool fillBackground)
: Button (String()),
document (doc),
alwaysFillBackground (fillBackground)
{
setClickingTogglesState (true);
}
void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
{
if (document->paintStatesEnabled [background])
{
document->paintRoutines [background]->fillWithBackground (g, alwaysFillBackground);
document->paintRoutines [background]->drawElements (g, getLocalBounds());
}
const int stateIndex
= getToggleState()
? (isButtonDown ? document->chooseBestEnabledPaintRoutine (downOn)
: (isMouseOverButton ? document->chooseBestEnabledPaintRoutine (overOn)
: document->chooseBestEnabledPaintRoutine (normalOn)))
: (isButtonDown ? document->chooseBestEnabledPaintRoutine (downOff)
: (isMouseOverButton ? document->chooseBestEnabledPaintRoutine (overOff)
: normalOff));
document->paintRoutines [stateIndex]->fillWithBackground (g, ! document->paintStatesEnabled [background]);
document->paintRoutines [stateIndex]->drawElements (g, getLocalBounds());
}
private:
ButtonDocument* const document;
const bool alwaysFillBackground;
};
Component* ButtonDocument::createTestComponent (const bool alwaysFillBackground)
{
return new ButtonTestComponent (this, alwaysFillBackground);
}
//==============================================================================
void ButtonDocument::fillInGeneratedCode (GeneratedCode& code) const
{
JucerDocument::fillInGeneratedCode (code);
code.parentClassInitialiser = "Button (" + quotedString (code.componentName, false) + ")";
code.removeCallback ("void", "paint (Graphics& g)");
}
void ButtonDocument::fillInPaintCode (GeneratedCode& code) const
{
jassert (paintStatesEnabled [normalOff]);
String paintCode [7];
for (int i = 0; i < 7; ++i)
if (paintStatesEnabled [i])
paintRoutines[i]->fillInGeneratedCode (code, paintCode [i]);
String& s = code.getCallbackCode ("public juce::Button",
"void",
"paintButton (juce::Graphics& g, bool isMouseOverButton, bool isButtonDown)",
false);
int numPaintRoutines = getNumPaintRoutines();
if (paintStatesEnabled [background])
{
s << paintCode [background] << "\n";
--numPaintRoutines;
}
if (numPaintRoutines == 1)
{
s << paintCode [normalOff];
}
else if (numPaintRoutines == downOff && (paintStatesEnabled [overOff] || paintStatesEnabled [downOff] || paintStatesEnabled [normalOn]))
{
if (paintStatesEnabled [normalOn])
{
s << "if (getToggleState())\n{\n "
<< CodeHelpers::indent (paintCode [normalOn], 4, false).trimEnd();
}
else if (paintStatesEnabled [overOff])
{
s << "if (isButtonDown || isMouseOverButton)\n{\n "
<< CodeHelpers::indent (paintCode [overOff], 4, false).trimEnd();
}
else
{
s << "if (isButtonDown)\n{\n "
<< CodeHelpers::indent (paintCode [downOff], 4, false).trimEnd();
}
s << "\n}\nelse\n{\n "
<< CodeHelpers::indent (paintCode [normalOff], 4, false).trimEnd()
<< "\n}\n";
}
else if (numPaintRoutines == normalOn && paintStatesEnabled [overOff] && paintStatesEnabled [downOff])
{
s << "if (isButtonDown)\n{\n "
<< CodeHelpers::indent (paintCode [downOff], 4, false).trimEnd()
<< "\n}\nelse if (isMouseOverButton)\n{\n "
<< CodeHelpers::indent (paintCode [overOff], 4, false).trimEnd()
<< "\n}\nelse\n{\n "
<< CodeHelpers::indent (paintCode [normalOff], 4, false).trimEnd()
<< "\n}\n";
}
else
{
if (paintStatesEnabled [normalOn] || paintStatesEnabled [overOn] || paintStatesEnabled [downOn])
{
s << "switch (getToggleState() ? (isButtonDown ? "
<< chooseBestEnabledPaintRoutine (downOn) << " : (isMouseOverButton ? "
<< chooseBestEnabledPaintRoutine (overOn) << " : "
<< chooseBestEnabledPaintRoutine (normalOn) << "))\n : (isButtonDown ? "
<< chooseBestEnabledPaintRoutine (downOff) << " : (isMouseOverButton ? "
<< chooseBestEnabledPaintRoutine (overOff) << " : 0)))\n{\n";
}
else
{
s << "switch (isButtonDown ? " << chooseBestEnabledPaintRoutine (downOff)
<< " : (isMouseOverButton ? " << chooseBestEnabledPaintRoutine (overOff)
<< " : 0))\n{\n";
}
for (int i = 0; i < 6; ++i)
{
if (paintStatesEnabled [i])
{
s << "case " << i << ":\n {\n "
<< CodeHelpers::indent (paintCode [i], 8, false).trimEnd()
<< "\n break;\n }\n\n";
}
}
s << "default:\n break;\n}\n";
}
}
|