#include "stdafx.h" #include "UI.h" #include "UIScene_EditServerMenu.h" #ifdef _WINDOWS64 #include "../../Windows64/KeyboardMouseInput.h" #endif namespace { const int kNameLimit = 64; const int kAddressLimit = 128; } UIScene_EditServerMenu::UIScene_EditServerMenu(int iPad, void *initData, UILayer *parentLayer) : UIScene(iPad, parentLayer) { initialiseMovie(); if (initData != nullptr) m_data = *static_cast(initData); m_name = m_data.name; m_address = m_data.address; m_iOKEnabled = -1; m_bIgnoreInput = false; m_bFinished = false; m_title.init(L"Edit Server Info"); m_labelName.init(L"Server Name"); m_labelAddress.init(L"Server Address"); m_editName.init(m_name, eControl_Name); m_editAddress.init(m_address, eControl_Address); m_editName.SetCharLimit(kNameLimit); m_editAddress.SetCharLimit(kAddressLimit); m_buttonCancel.init(app.GetString(IDS_CANCEL), eControl_Cancel); m_buttonOK.init(app.GetString(IDS_CONFIRM_OK), eControl_OK); } wstring UIScene_EditServerMenu::getMoviePath() { return L"EditServerMenu"; } bool UIScene_EditServerMenu::canConfirm() { wstring address = m_address; #ifdef _WINDOWS64 if (m_editAddress.isDirectEditing()) address = m_editAddress.getEditBuffer(); #endif for (size_t i = 0; i < address.length(); ++i) { if (address[i] != L' ') return true; } return false; } void UIScene_EditServerMenu::syncOKButton() { int enabled = canConfirm() ? 1 : 0; if (enabled == m_iOKEnabled) return; m_iOKEnabled = enabled; IggyDataValue result; IggyDataValue value[1]; value[0].type = IGGY_DATATYPE_boolean; value[0].boolval = enabled != 0; IggyPlayerCallMethodRS(getMovie(), &result, IggyPlayerRootPath(getMovie()), m_funcSetOKEnabled, 1, value); } void UIScene_EditServerMenu::tick() { UIScene::tick(); syncOKButton(); } void UIScene_EditServerMenu::updateTooltips() { ui.SetTooltips(m_iPad, IDS_TOOLTIPS_SELECT, IDS_TOOLTIPS_BACK); } void UIScene_EditServerMenu::updateComponents() { m_parentLayer->showComponent(m_iPad, eUIComponent_Panorama, true); m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, false); } wstring UIScene_EditServerMenu::ReadKeyboardText() { uint16_t text[256]; ZeroMemory(text, sizeof(text)); #ifdef _WINDOWS64 Win64_GetKeyboardText(text, 256); #endif wstring result; for (int i = 0; i < 255 && text[i]; ++i) result += static_cast(text[i]); return result; } int UIScene_EditServerMenu::NameKeyboardCallback(LPVOID lpParam, const bool bRes) { UIScene_EditServerMenu *pClass = static_cast(lpParam); pClass->m_bIgnoreInput = false; if (bRes) { pClass->m_name = ReadKeyboardText(); pClass->m_editName.setLabel(pClass->m_name); } return 0; } int UIScene_EditServerMenu::AddressKeyboardCallback(LPVOID lpParam, const bool bRes) { UIScene_EditServerMenu *pClass = static_cast(lpParam); pClass->m_bIgnoreInput = false; if (bRes) { pClass->m_address = ReadKeyboardText(); pClass->m_editAddress.setLabel(pClass->m_address); } return 0; } void UIScene_EditServerMenu::openKeyboard(int control) { bool isName = control == eControl_Name; UIControl_TextInput &input = isName ? m_editName : m_editAddress; int limit = isName ? kNameLimit : kAddressLimit; #ifdef _WINDOWS64 if (g_KBMInput.IsKBMActive()) { input.beginDirectEdit(limit); return; } UIKeyboardInitData kbData; kbData.title = isName ? L"Server Name" : L"Server Address"; kbData.defaultText = isName ? m_name.c_str() : m_address.c_str(); kbData.maxChars = limit; kbData.callback = isName ? &UIScene_EditServerMenu::NameKeyboardCallback : &UIScene_EditServerMenu::AddressKeyboardCallback; kbData.lpParam = this; kbData.pcMode = false; m_bIgnoreInput = true; ui.NavigateToScene(m_iPad, eUIScene_Keyboard, &kbData); #endif } void UIScene_EditServerMenu::finish(bool confirmed) { if (m_bFinished) return; if (confirmed && !canConfirm()) { ui.PlayUISFX(eSFX_CraftFail); return; } m_bFinished = true; ui.PlayUISFX(eSFX_Press); wstring host = m_address; while (!host.empty() && host[0] == L' ') host.erase(0, 1); while (!host.empty() && host[host.length() - 1] == L' ') host.erase(host.length() - 1); wstring port = L"25565"; size_t colon = host.rfind(L':'); if (colon != wstring::npos && host.find(L':') == colon) { wstring tail = host.substr(colon + 1); host = host.substr(0, colon); if (!tail.empty()) port = tail; } wstring name = m_name; if (name.empty()) name = L"Minecraft Server"; EditServerInitData data = m_data; navigateBack(); if (data.callback != nullptr) data.callback(data.lpParam, confirmed, name, host, port); } void UIScene_EditServerMenu::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled) { if (m_bIgnoreInput || m_bFinished) return; #ifdef _WINDOWS64 if (isDirectEditBlocking()) { handled = true; return; } #endif ui.AnimateKeyPress(iPad, key, repeat, pressed, released); switch (key) { case ACTION_MENU_CANCEL: if (pressed) finish(false); handled = true; break; case ACTION_MENU_OK: case ACTION_MENU_UP: case ACTION_MENU_DOWN: case ACTION_MENU_LEFT: case ACTION_MENU_RIGHT: sendInputToMovie(key, repeat, pressed, released); handled = true; break; } } void UIScene_EditServerMenu::handlePress(F64 controlId, F64 childId) { if (m_bIgnoreInput || m_bFinished) return; #ifdef _WINDOWS64 if (isDirectEditBlocking()) return; #endif switch (static_cast(controlId)) { case eControl_Name: case eControl_Address: ui.PlayUISFX(eSFX_Press); openKeyboard(static_cast(controlId)); break; case eControl_Cancel: finish(false); break; case eControl_OK: finish(true); break; } } #ifdef _WINDOWS64 void UIScene_EditServerMenu::getDirectEditInputs(vector &inputs) { inputs.push_back(&m_editName); inputs.push_back(&m_editAddress); } void UIScene_EditServerMenu::onDirectEditFinished(UIControl_TextInput *input, UIControl_TextInput::EDirectEditResult result) { if (result != UIControl_TextInput::eDirectEdit_Confirmed) return; if (input == &m_editName) m_name = input->getEditBuffer(); else if (input == &m_editAddress) m_address = input->getEditBuffer(); } #endif