added multiplayer menu
This commit is contained in:
1 parent
a8700af1d0
commit
0babfff28b
4 files changed
+323
-19
No files matched your search
+121
-2
@@ -168,6 +168,20 @@ namespace ZL {
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
}
|
||||
|
||||
void UiTextField::draw(Renderer& renderer) const {
|
||||
if (textRenderer) {
|
||||
float textX = rect.x + 10.0f;
|
||||
float textY = rect.y + rect.h / 2.0f;
|
||||
|
||||
if (text.empty()) {
|
||||
textRenderer->drawText(placeholder, textX, textY, 1.0f, false, placeholderColor);
|
||||
}
|
||||
else {
|
||||
textRenderer->drawText(text, textX, textY, 1.0f, false, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UiManager::loadFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile) {
|
||||
std::string content;
|
||||
try {
|
||||
@@ -207,6 +221,7 @@ namespace ZL {
|
||||
buttons.clear();
|
||||
sliders.clear();
|
||||
textViews.clear();
|
||||
textFields.clear();
|
||||
collectButtonsAndSliders(root);
|
||||
|
||||
nodeActiveAnims.clear();
|
||||
@@ -277,7 +292,7 @@ namespace ZL {
|
||||
if (!t.contains(key) || !t[key].is_string()) return nullptr;
|
||||
std::string path = t[key].get<std::string>();
|
||||
try {
|
||||
std::cout << "UiManager: --loading texture for button '" << "' : " << path << " Zip file: " << zipFile << std::endl;
|
||||
std::cout << "UiManager: --loading texture for slider '" << s->name << "' : " << path << " Zip file: " << zipFile << std::endl;
|
||||
auto data = CreateTextureDataFromPng(path.c_str(), zipFile.c_str());
|
||||
return std::make_shared<Texture>(data);
|
||||
}
|
||||
@@ -299,6 +314,44 @@ namespace ZL {
|
||||
|
||||
node->slider = s;
|
||||
}
|
||||
else if (node->type == "TextField") {
|
||||
auto tf = std::make_shared<UiTextField>();
|
||||
tf->name = node->name;
|
||||
tf->rect = node->rect;
|
||||
|
||||
if (j.contains("placeholder")) tf->placeholder = j["placeholder"].get<std::string>();
|
||||
if (j.contains("fontPath")) tf->fontPath = j["fontPath"].get<std::string>();
|
||||
if (j.contains("fontSize")) tf->fontSize = j["fontSize"].get<int>();
|
||||
if (j.contains("maxLength")) tf->maxLength = j["maxLength"].get<int>();
|
||||
|
||||
if (j.contains("color") && j["color"].is_array() && j["color"].size() == 4) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
tf->color[i] = j["color"][i].get<float>();
|
||||
}
|
||||
}
|
||||
if (j.contains("placeholderColor") && j["placeholderColor"].is_array() && j["placeholderColor"].size() == 4) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
tf->placeholderColor[i] = j["placeholderColor"][i].get<float>();
|
||||
}
|
||||
}
|
||||
if (j.contains("backgroundColor") && j["backgroundColor"].is_array() && j["backgroundColor"].size() == 4) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
tf->backgroundColor[i] = j["backgroundColor"][i].get<float>();
|
||||
}
|
||||
}
|
||||
if (j.contains("borderColor") && j["borderColor"].is_array() && j["borderColor"].size() == 4) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
tf->borderColor[i] = j["borderColor"][i].get<float>();
|
||||
}
|
||||
}
|
||||
|
||||
tf->textRenderer = std::make_unique<TextRenderer>();
|
||||
if (!tf->textRenderer->init(renderer, tf->fontPath, tf->fontSize, zipFile)) {
|
||||
std::cerr << "Failed to init TextRenderer for TextField: " << tf->name << std::endl;
|
||||
}
|
||||
|
||||
node->textField = tf;
|
||||
}
|
||||
|
||||
if (j.contains("animations") && j["animations"].is_object()) {
|
||||
for (auto it = j["animations"].begin(); it != j["animations"].end(); ++it) {
|
||||
@@ -406,7 +459,10 @@ namespace ZL {
|
||||
if (node->textView) {
|
||||
textViews.push_back(node->textView);
|
||||
}
|
||||
for (auto& c : node->children) collectButtonsAndSliders(c); // ìîæíî ïåðåèìåíîâàòü â collectControls
|
||||
if (node->textField) {
|
||||
textFields.push_back(node->textField);
|
||||
}
|
||||
for (auto& c : node->children) collectButtonsAndSliders(c);
|
||||
}
|
||||
|
||||
bool UiManager::setButtonCallback(const std::string& name, std::function<void(const std::string&)> cb) {
|
||||
@@ -476,13 +532,36 @@ namespace ZL {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<UiTextField> UiManager::findTextField(const std::string& name) {
|
||||
for (auto& tf : textFields) if (tf->name == name) return tf;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool UiManager::setTextFieldCallback(const std::string& name, std::function<void(const std::string&, const std::string&)> cb) {
|
||||
auto tf = findTextField(name);
|
||||
if (!tf) {
|
||||
std::cerr << "UiManager: setTextFieldCallback failed, textfield not found: " << name << std::endl;
|
||||
return false;
|
||||
}
|
||||
tf->onTextChanged = std::move(cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string UiManager::getTextFieldValue(const std::string& name) {
|
||||
auto tf = findTextField(name);
|
||||
if (!tf) return "";
|
||||
return tf->text;
|
||||
}
|
||||
|
||||
bool UiManager::pushMenuFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile) {
|
||||
MenuState prev;
|
||||
prev.root = root;
|
||||
prev.buttons = buttons;
|
||||
prev.sliders = sliders;
|
||||
prev.textFields = textFields;
|
||||
prev.pressedButton = pressedButton;
|
||||
prev.pressedSlider = pressedSlider;
|
||||
prev.focusedTextField = focusedTextField;
|
||||
prev.path = "";
|
||||
|
||||
prev.animCallbacks = animCallbacks;
|
||||
@@ -490,6 +569,7 @@ namespace ZL {
|
||||
try {
|
||||
nodeActiveAnims.clear();
|
||||
animCallbacks.clear();
|
||||
focusedTextField = nullptr;
|
||||
for (auto& b : buttons) {
|
||||
if (b) {
|
||||
b->animOffsetX = 0.0f;
|
||||
@@ -523,8 +603,10 @@ namespace ZL {
|
||||
root = s.root;
|
||||
buttons = s.buttons;
|
||||
sliders = s.sliders;
|
||||
textFields = s.textFields;
|
||||
pressedButton = s.pressedButton;
|
||||
pressedSlider = s.pressedSlider;
|
||||
focusedTextField = s.focusedTextField;
|
||||
|
||||
animCallbacks = s.animCallbacks;
|
||||
|
||||
@@ -565,6 +647,9 @@ namespace ZL {
|
||||
for (const auto& tv : textViews) {
|
||||
tv->draw(renderer);
|
||||
}
|
||||
for (const auto& tf : textFields) {
|
||||
tf->draw(renderer);
|
||||
}
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
@@ -785,6 +870,16 @@ namespace ZL {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& tf : textFields) {
|
||||
if (tf->rect.contains((float)x, (float)y)) {
|
||||
focusedTextField = tf;
|
||||
tf->focused = true;
|
||||
}
|
||||
else {
|
||||
tf->focused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UiManager::onMouseUp(int x, int y) {
|
||||
@@ -806,6 +901,30 @@ namespace ZL {
|
||||
}
|
||||
}
|
||||
|
||||
void UiManager::onKeyPress(unsigned char key) {
|
||||
if (!focusedTextField) return;
|
||||
|
||||
if (key >= 32 && key <= 126) {
|
||||
if (focusedTextField->text.length() < (size_t)focusedTextField->maxLength) {
|
||||
focusedTextField->text += key;
|
||||
if (focusedTextField->onTextChanged) {
|
||||
focusedTextField->onTextChanged(focusedTextField->name, focusedTextField->text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UiManager::onKeyBackspace() {
|
||||
if (!focusedTextField) return;
|
||||
|
||||
if (!focusedTextField->text.empty()) {
|
||||
focusedTextField->text.pop_back();
|
||||
if (focusedTextField->onTextChanged) {
|
||||
focusedTextField->onTextChanged(focusedTextField->name, focusedTextField->text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<UiButton> UiManager::findButton(const std::string& name) {
|
||||
for (auto& b : buttons) if (b->name == name) return b;
|
||||
return nullptr;
|
||||
|
||||
Reference in new issue
Block a user