Added find functions to list and split the tokenparser from widget adder function

This commit is contained in:
Rebekah 2022-04-04 15:08:33 -04:00
parent 1cf551d149
commit d9b9cb7981
Signed by: oneechanhax
GPG Key ID: 183EB7902964DAE5
3 changed files with 263 additions and 236 deletions

View File

@ -1,2 +1,5 @@
BasedOnStyle: WebKit
BreakBeforeBraces: Attach
CompactNamespaces: true
FixNamespaceComments: true
NamespaceIndentation: None

View File

@ -26,6 +26,10 @@ namespace ncc {
void Fill(const std::string vec);
void Fill(const std::vector<ui::BaseVar*>& vec);
List* ForceGetSublist(std::string_view);
List* FindSublist(std::string_view);
List* CreateSublist(std::string_view);
void OpenSublist(List* sublist, int dy);
bool ShouldClose();

View File

@ -85,6 +85,22 @@ namespace ncc {
}
}
List* List::FindSublist(std::string_view name) {
auto find = std::find_if(this->m_children.begin(), this->m_children.end(), [&](auto i) { return i->GetName() == "ncc_item_sublist" && dynamic_cast<ItemSublist*>(i)->title == name; });
return find != this->m_children.end() ? dynamic_cast<ItemSublist*>(*find)->list : nullptr;
}
List* List::CreateSublist(std::string_view name) {
assert(this->FindSublist(name) == nullptr);
auto* list = new List(std::string(name));
this->AddChild(new ItemSublist(std::string(name), list));
return list;
}
List* List::ForceGetSublist(std::string_view name) {
if (auto find = this->FindSublist(name))
return find;
return this->CreateSublist(name);
}
bool List::ShouldClose() {
if (open_sublist) {
if (!open_sublist->ShouldClose())
@ -118,25 +134,7 @@ namespace ncc {
throw std::runtime_error("can't find catvar " + name);
}
// This is how it generates menues and submenues of menues
// abc def, ghj, [, fdg sgf saqw rter, ], gs
void FillFromTokens(List* list, const std::vector<std::string>& tokens) {
list->title = tokens[0];
list->AddChild(new ItemTitle(tokens[0]));
for (int i = 1; i < tokens.size(); i++) {
const std::string& str = tokens.at(i);
if (i == tokens.size() - 1 || tokens[i + 1] != "[") {
list->AddChild(new ItemVariable(*FindCatVar(str)));
} else {
list->AddChild(new ItemSublist(str, List::FromString(tokens[i + 2])));
i += 3;
}
}
}
// Cool string parser
List* List::FromString(const std::string& string) {
List* result = new List();
std::vector<std::string> ParseTokens(const std::string& string) {
bool readingkey = false;
std::string last_read_key = "";
std::stringstream readkey("");
@ -177,7 +175,29 @@ namespace ncc {
readkey << c;
}
}
FillFromTokens(result, tokens);
return tokens;
}
// This is how it generates menues and submenues of menues
// abc def, ghj, [, fdg sgf saqw rter, ], gs
void FillFromTokens(List* list, const std::vector<std::string>& tokens) {
list->title = tokens[0];
list->AddChild(new ItemTitle(tokens[0]));
for (int i = 1; i < tokens.size(); i++) {
const std::string& str = tokens.at(i);
if (i == tokens.size() - 1 || tokens[i + 1] != "[") {
list->AddChild(new ItemVariable(*FindCatVar(str)));
} else {
list->AddChild(new ItemSublist(str, List::FromString(tokens[i + 2])));
i += 3;
}
}
}
// Cool string parser
List* List::FromString(const std::string& string) {
List* result = new List();
FillFromTokens(result, ParseTokens(string));
printf("done making list %s - has %i children.\n", result->title.c_str(), result->ChildCount());
return result;
}
@ -270,4 +290,4 @@ namespace ncc {
}
}
}
} // namespace menu::ncc