const String GeneratedCode::getCallbackDefinitions() const { String s; for (int i = 0; i < callbacks.size(); ++i) { CallbackMethod* const cm = callbacks.getUnchecked(i); const String userCodeBlockName (T("User") + makeValidCppIdentifier (cm->prototype.upToFirstOccurrenceOf (T("("), false, false), true, true, false).trim()); if (userCodeBlockName.isNotEmpty() && cm->hasPrePostUserSections) { s << cm->returnType << " " << className << "::" << cm->prototype << "\n{\n //[" << userCodeBlockName << "_Pre]\n //[/" << userCodeBlockName << "_Pre]\n\n " << indentCode (cm->content.trim(), 4) << "\n\n //[" << userCodeBlockName << "_Post]\n //[/" << userCodeBlockName << "_Post]\n}\n\n"; } else { s << cm->returnType << " " << className << "::" << cm->prototype << "\n{\n " << indentCode (cm->content.trim(), 4) << "\n}\n\n"; } } return s; }
QString CodeGenerator::generateCode(const DraggableElement* element, int sub) { QString str; bool stop = false; for(const DraggableElement* next = element; next && !stop; next = next->getNextElem()) { QString name = next->getIdentifier(); // TODO: indent should be more dynamic QString tmp; if (next->getType() == DraggableElement::Hat && _events.contains(name)) { tmp = indentCode(&_events[name]._code, sub, generateCode(next->getNextElem(), sub+1)); if(_eventCounters.contains(name)) _eventCounters[name]++; else _eventCounters[name] = 1; tmp.replace(QLatin1String("%counter%"), QString::number(_eventCounters[name])); stop = true; } else if (next->getType() == DraggableElement::Wrapper && _controls.contains(name)) { tmp = indentCode(&_controls[name], sub, generateCode(next->getWrapElem(), sub+1)); } else if (next->getType() == DraggableElement::Command && _commands.contains(name)) { tmp = indentCode(&_commands[name], sub); } else { qDebug() << "block: " << name << "not supported yet"; str += indent(sub) + "pass\n"; continue; } if(tmp.contains(QLatin1String("%indent_counter%"))) { tmp.replace(QLatin1String("%indent_counter%"), QString::number(_indentCounter)); _indentCounter++; } const std::vector<ParamBase*>* params = next->getParamsVector(); for(std::vector<ParamBase*>::const_iterator it = params->begin(); it != params->end(); it++) { tmp = tmp.arg(generateParam(*it)); } str += tmp; } if(str.isEmpty()) { str = indent(sub) + "pass\n"; } return str; }
QString CodeGenerator::generateSprite(const Sprite *sprite, bool stage) { QString str; _eventCounters.clear(); _indentCounter = 1; QStringList *costumeRegSnippet = &_snippets["costume_register"]; QStringList *constructSnippet = stage ? &_snippets["construct_stage"] : &_snippets["construct"]; QString costumeRegister; for (CostumeVector::const_iterator it = sprite->getCostumeVector()->begin(); it != sprite->getCostumeVector()->end(); it++) { costumeRegister += indentCode(costumeRegSnippet, 2) .replace(QLatin1String("%entity%"), QLatin1String("self")) .replace(QLatin1String("%name%"), addQuotes((*it)->getName())) .replace(QLatin1String("%file%"), addQuotes((*it)->getFilename())); } // create class file str += indentCode(&_snippets["import"]); str += indentCode(&_snippets["class"]).replace(QLatin1String("%name%"), sprite->getName()); str += indentCode(constructSnippet, 1, costumeRegister); str += "%events%\n"; // check every block for "header"-block const DragElemVector *eleVec = sprite->getDragElemVector(); for(DragElemVector::const_iterator elemIt = eleVec->begin(); elemIt != eleVec->end(); elemIt++) { if ((*elemIt)->getType() == DraggableElement::Hat) { str += generateCode(*elemIt, 1) + '\n'; } } // add Event registration QString events; for(QMap<QString, int>::const_iterator eventIt = _eventCounters.constBegin(); eventIt != _eventCounters.constEnd(); eventIt++) { for(int i = 1; i <= eventIt.value(); i++) { QString event = indent(2) + _events[eventIt.key()]._register + "\n"; events += event.replace(QLatin1String("%counter%"), QString::number(i)); } } str.replace(QLatin1String("%events%"), events); return str; }
QString CodeGenerator::generateMain(const Sprite *bgSprite, const SpriteVector *spriteVec) { QString entityImport; QString entityRegister; QStringList *entImpSnippet = &_snippets["entity_import"]; QStringList *entRegSnippet = &_snippets["entity_register"]; entityImport += indentCode(entImpSnippet).replace(QLatin1String("%name%"), bgSprite->getName()); entityRegister += indentCode(entRegSnippet, 1).replace(QLatin1String("%name%"), bgSprite->getName()); // sprites for(SpriteVector::const_iterator it = spriteVec->begin(); it != spriteVec->end(); it++) { entityImport += indentCode(entImpSnippet).replace(QLatin1String("%name%"), (*it)->getName()); entityRegister += indentCode(entRegSnippet, 1).replace(QLatin1String("%name%"), (*it)->getName()); } // main file QString str; str += indentCode(&_snippets["main_import"]); str += entityImport + "\n"; str += indentCode(&_snippets["main"], 0, entityRegister); return str; }
//============================================================================== static void replaceTemplate (String& text, const String& itemName, const String& value) { for (;;) { const int index = text.indexOf (T("%%") + itemName + T("%%")); if (index < 0) break; int indentLevel = 0; for (int i = index; --i >= 0;) { if (text[i] == T('\n')) break; ++indentLevel; } text = text.replaceSection (index, itemName.length() + 4, indentCode (value, indentLevel)); } }
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 Button", "void", "paintButton (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 " << indentCode (paintCode [normalOn], 4).trimEnd(); } else if (paintStatesEnabled [overOff]) { s << "if (isButtonDown || isMouseOverButton)\n{\n " << indentCode (paintCode [overOff], 4).trimEnd(); } else { s << "if (isButtonDown)\n{\n " << indentCode (paintCode [downOff], 4).trimEnd(); } s << "\n}\nelse\n{\n " << indentCode (paintCode [normalOff], 4).trimEnd() << "\n}\n"; } else if (numPaintRoutines == normalOn && paintStatesEnabled [overOff] && paintStatesEnabled [downOff]) { s << "if (isButtonDown)\n{\n " << indentCode (paintCode [downOff], 4).trimEnd() << "\n}\nelse if (isMouseOverButton)\n{\n " << indentCode (paintCode [overOff], 4).trimEnd() << "\n}\nelse\n{\n " << indentCode (paintCode [normalOff], 4).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 " << indentCode (paintCode [i], 8).trimEnd() << "\n break;\n }\n\n"; } } s << "default:\n break;\n}\n"; } }