void BTSerializedEditor::editField(BTDisplay &d, ObjectSerializer &serial, const char *text, XMLAction *curField, int modField, int where) { int key; switch (curField->getType()) { case XMLTYPE_STDSTRING: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<std::string*>(curField->object)) = val; break; } case XMLTYPE_STRING: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) { char *str = *(reinterpret_cast<char**>(curField->object)); if (str) { delete [] str; } int len = val.length(); str = new char[len + 1]; strncpy(str, val.c_str(), len); str[len] = 0; *(reinterpret_cast<char**>(curField->object)) = str; } break; } case XMLTYPE_BOOL: { BTDisplay::selectItem vals[2]; vals[0].name = "false"; vals[1].name = "true"; int lookupStart(0); int lookupCurrent((*(reinterpret_cast<bool*>(curField->object)) ? 1 : 0)); d.addSelection(vals, 2, lookupStart, lookupCurrent); if (27 != d.process()) { *(reinterpret_cast<bool*>(curField->object)) = lookupCurrent; } break; } case XMLTYPE_BITFIELD: { ValueLookup *lookup = reinterpret_cast<ValueLookup*>(curField->data); BitField *bits = reinterpret_cast<BitField*>(curField->object); BTDisplay::selectItem lookupItem[lookup->size()]; for (int i = 0; i < lookup->size(); ++i) { lookupItem[i].name = lookup->getName(i); if (bits->isSet(i)) lookupItem[i].first = '*'; } int lookupStart(0); int lookupCurrent(0); d.addSelection(lookupItem, lookup->size(), lookupStart, lookupCurrent); int key; while (27 != (key = d.process())) { if (bits->toggle(lookupCurrent)) lookupItem[lookupCurrent].first = '*'; else lookupItem[lookupCurrent].first = 0; } break; } case XMLTYPE_INT: { if (curField->data) { ValueLookup *lookup = reinterpret_cast<ValueLookup*>(curField->data); bool extra = ((curField->extra == EXTRA_NONE) ? false : true); BTDisplay::selectItem lookupItem[lookup->size() + (extra ? 1 : 0)]; int i = 0; if (extra) { lookupItem[0].name = curField->extraText; lookupItem[0].value = -1; ++i; } int endIndex = lookup->getEndIndex(); int lookupCurrent(0); int valIndex((*(reinterpret_cast<int*>(curField->object))) + (extra ? 1 : 0)); for (int curIndex = lookup->getFirstIndex(); curIndex != endIndex; curIndex = lookup->getNextIndex(curIndex)) { lookupItem[i].name = lookup->getName(curIndex); lookupItem[i].value = curIndex; if (curIndex == valIndex) lookupCurrent = i; ++i; } int lookupStart(0); d.addSelection(lookupItem, lookup->size() + (extra ? 1 : 0), lookupStart, lookupCurrent); if (27 != d.process()) { *(reinterpret_cast<int*>(curField->object)) = lookupItem[lookupCurrent].value; } } else { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<int*>(curField->object)) = atol(val.c_str()); } break; } case XMLTYPE_UINT: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<unsigned int*>(curField->object)) = atol(val.c_str()); break; } case XMLTYPE_INT16: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<int16_t*>(curField->object)) = atol(val.c_str()); break; } case XMLTYPE_VECTORUINT: { std::vector<unsigned int> *vec = reinterpret_cast<std::vector<unsigned int> *>(curField->object); std::string val; for (size_t i = 0; i < vec->size(); ++i) { if (i != 0) val += ","; char convert[30]; sprintf(convert, "%u", (*vec)[i]); val += convert; } d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) { size_t i = 0; const char *start = val.c_str(); for (const char *comma = strchr(val.c_str(), ','); (start) && (*start); ++i) { if (i < vec->size()) (*vec)[i] = atol(start); else vec->push_back(atol(start)); start = comma; if (start) { if ((*start) == ',') ++start; comma = strchr(start, ','); } } if (i < vec->size()) vec->resize(i); } break; } case XMLTYPE_OBJECT: { XMLObject *obj = reinterpret_cast<XMLObject*>(curField->object); BTDice *dice = dynamic_cast<BTDice*>(obj); if (dice) { std::ostringstream stream; stream << dice->getNumber(); std::string val = stream.str(); d.addReadString(std::string(text) + "- Number of Dice: ", 100, val); key = d.process(); if ('\r' == key) dice->setNumber(atol(val.c_str())); d.clearText(); stream.str(""); stream << dice->getType(); val = stream.str(); d.addReadString(std::string(text) + "- Type of Dice: ", 100, val); key = d.process(); if ('\r' == key) dice->setType(atol(val.c_str())); d.clearText(); stream.str(""); stream << dice->getModifier(); val = stream.str(); d.addReadString(std::string(text) + "- Modifier to Roll: ", 100, val); key = d.process(); if ('\r' == key) dice->setModifier(atol(val.c_str())); } else printf("Unsuppported type: %d\n", curField->getType()); break; } case XMLTYPE_CREATE: { XMLArray *obj = reinterpret_cast<XMLArray*>(curField->object); handleObject(d, obj->get(where), modField); break; } case XMLTYPE_PICTURE: { BTDisplayConfig *oldConfig = d.getConfig(); BTDisplayConfig config; XMLSerializer parser; config.serialize(&parser); parser.parse(BTDisplay::applyDisplayDir("data/pictureselect.xml").c_str(), true); d.setConfig(&config); d.clearText(); d.addText(text); int val(reinterpret_cast<PictureIndex*>(curField->object)->value); d.addSelectImage(val); key = d.process(); if ('\r' == key) reinterpret_cast<PictureIndex*>(curField->object)->value = val; d.clearImage(); d.setConfig(oldConfig); break; } case XMLTYPE_VECTORSTRING: { std::vector<std::string> *obj = reinterpret_cast<std::vector<std::string>*>(curField->object); std::string val = (*obj)[where]; d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) (*obj)[where] = val; break; } default: printf("Unsuppported type: %d\n", curField->getType()); break; } d.clearText(); }
void BTEditor::editSpecial(BTDisplay &d, BTSpecial *special) { if (NULL == special) { special = new BTSpecial; levelMap->addSpecial(special); } BTDisplayConfig *oldConfig = d.getConfig(); BTDisplayConfig config; XMLSerializer parser; config.serialize(&parser); parser.parse(BTDisplay::applyDisplayDir("data/specialedit.xml").c_str(), true); d.setConfig(&config); int start(0); int current(0); BTSpecialBody *body = special->getBody(); std::vector<operationList> ops; std::vector<BTDisplay::selectItem> list(2); list[0].name = std::string("Name: ") + special->getName(); list[1].name = "Flags: " + special->printFlags(false); buildOperationList(d, body, list, ops); d.addSelection(list.data(), list.size(), start, current); int key; char extra[6] = {BTKEY_INS, BTKEY_DEL, BTKEY_CTRL_C, BTKEY_CTRL_V, BTKEY_CTRL_X, 0}; while (27 != (key = d.process(extra))) { d.clearText(); if (current == 0) { std::string name = special->getName(); d.addReadString("Name: ", 25, name); key = d.process(); if ('\r' == key) special->setName(name); d.clearText(); list[0].name = std::string("Name: ") + special->getName(); } else if (current == 1) { BTSpecialFlagList &lookup = getSpecialFlagList(); BitField bits = special->getFlag(); BTDisplay::selectItem lookupItem[lookup.size()]; for (size_t i = 0; i < lookup.size(); ++i) { lookupItem[i].name = lookup.getName(i); if (bits.isSet(i)) lookupItem[i].first = '*'; } int lookupStart(0); int lookupCurrent(0); d.addSelection(lookupItem, lookup.size(), lookupStart, lookupCurrent); int key; while (27 != (key = d.process())) { if (bits.toggle(lookupCurrent)) lookupItem[lookupCurrent].first = '*'; else lookupItem[lookupCurrent].first = 0; } special->setFlag(bits); d.clearText(); list[1].name = "Flags: " + special->printFlags(false); } else { if (BTKEY_INS == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { ops[list[current].value].parent->insertOperation(ops[list[current].value].op, new BTSpecialCommand(BTSPECIALCOMMAND_NOTHING)); } } else if (BTKEY_DEL == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { ops[list[current].value].parent->eraseOperation(ops[list[current].value].op); } } else if (BTKEY_CTRL_X == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { if (clipboard) delete clipboard; clipboard = ops[list[current].value].op->clone(); ops[list[current].value].parent->eraseOperation(ops[list[current].value].op); } } else if (BTKEY_CTRL_C == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { if (clipboard) delete clipboard; clipboard = ops[list[current].value].op->clone(); } } else if (BTKEY_CTRL_V == key) { if ((ops[list[current].value].parent != NULL) && (clipboard)) { if (ops[list[current].value].op) ops[list[current].value].parent->insertOperation(ops[list[current].value].op, clipboard->clone()); else ops[list[current].value].parent->addOperation(clipboard->clone()); } } else if ('\r' == key) { BTSpecialOperation *op = editSpecialOperation(d, ops[list[current].value].op); if (op) { if (ops[list[current].value].op) ops[list[current].value].parent->replaceOperation(ops[list[current].value].op, op); else ops[list[current].value].parent->addOperation(op); } } } ops.clear(); list.resize(2); buildOperationList(d, body, list, ops); d.addSelection(list.data(), list.size(), start, current); } d.clearText(); d.setConfig(oldConfig); }