#include "stdafx.h" #include "UI.h" #include "UIScene_SuperflatMenu.h" #include "SuperflatData.h" #include "UICustomStrings.h" #include "../../../Minecraft.World/net.minecraft.world.item.h" #include "../../../Minecraft.World/net.minecraft.world.level.tile.h" namespace { const float kTabX = 284.0f; const float kTabY = 110.0f; const float kTabW = 266.0f; const float kTabH = 76.0f; } UIScene_SuperflatMenu::UIScene_SuperflatMenu(int iPad, void *initData, UILayer *parentLayer) : UIScene(iPad, parentLayer) { initialiseMovie(); m_params = static_cast(initData); if (m_params != nullptr && !m_params->superflatOptions.empty()) m_info = FlatGeneratorInfo::fromValue(m_params->superflatOptions); else m_info = SuperflatData::MakePreset(0); m_editData.info = &m_info; m_editData.layerIndex = 0; m_editData.insert = false; m_activeTab = eTab_Layers; m_layerSelection = 0; m_biomeSelection = SuperflatData::GetBiomeIndex(m_info.getBiome()); m_paramSelection = 0; m_pendingAction = ePending_None; m_pendingBiome = -1; m_pendingSelectRow = -1; m_pendingSliderId = -1; m_pendingSliderValue = 0; m_bMovingLayer = false; m_bBusy = false; m_bPopulated = false; m_bNeedsPopulate = true; m_labelLayersTab.init(UICustomStrings::Lookup(UICustomStrings::Get(L"Layers"))); m_labelBiomesTab.init(UICustomStrings::Lookup(UICustomStrings::Get(L"Biomes"))); m_labelPropertiesTab.init(UICustomStrings::Lookup(UICustomStrings::Get(L"Properties"))); m_layersList.init(eControl_LayersList); m_biomesList.init(eControl_BiomesList); m_propertiesList.init(eControl_PropertiesList); m_layersList.UseIggyIcons(); m_biomesList.UseIggyIcons(); } wstring UIScene_SuperflatMenu::getMoviePath() { return L"SuperflatMenu"; } int UIScene_SuperflatMenu::layerCount() { return static_cast(m_info.getLayers()->size()); } int UIScene_SuperflatMenu::rowToLayer(int row) { return layerCount() - 1 - row; } bool UIScene_SuperflatMenu::isBottomRow(int row) { return row >= layerCount() - 1; } void UIScene_SuperflatMenu::populateAll() { populateLayers(); populateBiomes(); populateProperties(); m_bPopulated = true; } void UIScene_SuperflatMenu::populateLayers() { m_layersList.clearList(); vector &layers = *m_info.getLayers(); int row = 0; for (int i = static_cast(layers.size()) - 1; i >= 0; --i, ++row) { wstring name = SuperflatData::GetBlockName(layers[i].getId(), layers[i].getData()); wstring height = std::to_wstring(layers[i].getHeight()) + L"x"; m_layersList.AddNewItem(name, height, row); } if (m_layerSelection >= layerCount()) m_layerSelection = layerCount() - 1; if (m_layerSelection < 0) m_layerSelection = 0; } void UIScene_SuperflatMenu::populateBiomes() { m_biomesList.clearList(); for (int i = 0; i < SuperflatData::GetBiomeCount(); ++i) m_biomesList.AddNewItem(SuperflatData::GetBiomeName(i), i, i == m_biomeSelection); } void UIScene_SuperflatMenu::updateBiomeChecks() { int selected = SuperflatData::GetBiomeIndex(m_info.getBiome()); for (int i = 0; i < SuperflatData::GetBiomeCount(); ++i) m_biomesList.SetCheckBox(i, i == selected); } void UIScene_SuperflatMenu::populateProperties() { m_propertiesList.clearList(); for (int i = 0; i < SuperflatData::GetParamCount(); ++i) { const SuperflatData::Param ¶m = SuperflatData::GetParam(i); int value = SuperflatData::ReadParamValue(m_info, i); if (param.kind == SuperflatData::eParam_Toggle) m_propertiesList.AddNewCheckbox(param.name, i, value != 0); else m_propertiesList.AddNewSlider(SuperflatData::GetParamLabel(i, value), i, param.minValue, param.maxValue, 1, value); } updateParamStates(); } void UIScene_SuperflatMenu::updateParamStates() { for (int i = 0; i < SuperflatData::GetParamCount(); ++i) { const SuperflatData::Param ¶m = SuperflatData::GetParam(i); if (param.kind == SuperflatData::eParam_Toggle) continue; int parent = SuperflatData::GetParamParent(i); bool enabled = parent >= 0 && m_info.hasStructure(SuperflatData::GetParam(parent).structure); m_propertiesList.EnableItem(i, enabled); m_propertiesList.SetSliderLabel(i, enabled ? SuperflatData::GetParamLabel(i, m_propertiesList.GetSliderValue(i)) : wstring(param.name)); } } void UIScene_SuperflatMenu::readParams() { for (int i = 0; i < SuperflatData::GetParamCount(); ++i) { const SuperflatData::Param ¶m = SuperflatData::GetParam(i); if (param.kind != SuperflatData::eParam_Toggle) continue; bool checked = m_propertiesList.GetCheckboxValue(i); bool had = m_info.hasStructure(param.structure); if (checked == had) continue; SuperflatData::WriteParamValue(m_info, i, checked ? 1 : 0); if (checked) { for (int c = i + 1; c < SuperflatData::GetParamCount() && SuperflatData::GetParam(c).kind != SuperflatData::eParam_Toggle; ++c) SuperflatData::WriteParamValue(m_info, c, m_propertiesList.GetSliderValue(c)); } } updateParamStates(); commit(); } void UIScene_SuperflatMenu::commit() { if (m_params != nullptr) m_params->superflatOptions = m_info.toString(); } void UIScene_SuperflatMenu::stepSelection(int direction) { if (!m_bPopulated || m_bBusy) return; int count = 0; int current = 0; if (m_activeTab == eTab_Layers) { count = layerCount(); current = m_layerSelection; } else if (m_activeTab == eTab_Biomes) { count = SuperflatData::GetBiomeCount(); current = m_biomeSelection; } else { count = SuperflatData::GetParamCount(); current = m_paramSelection; } int next = current + direction; if (next < 0 || next >= count) return; ui.PlayUISFX(eSFX_Focus); if (m_activeTab == eTab_Layers) { m_layerSelection = next; m_layersList.setCurrentSelection(next); m_layersList.updateChildFocus(next); } else if (m_activeTab == eTab_Biomes) { m_biomeSelection = next; m_biomesList.setCurrentSelection(next); m_biomesList.updateChildFocus(next); } else { m_paramSelection = next; m_propertiesList.HighlightItem(next); } updateHelp(); updateTooltips(); } void UIScene_SuperflatMenu::focusList(int tab) { IggyDataValue result; IggyDataValue value[1]; value[0].type = IGGY_DATATYPE_number; value[0].number = static_cast(tab); IggyPlayerCallMethodRS(getMovie(), &result, IggyPlayerRootPath(getMovie()), m_funcFocusList, 1, value); } void UIScene_SuperflatMenu::setActiveTab(int tab, bool setFocus) { if (tab < 0) tab = eTab_Count - 1; if (tab >= eTab_Count) tab = 0; m_activeTab = tab; IggyDataValue result; IggyDataValue value[1]; value[0].type = IGGY_DATATYPE_number; value[0].number = static_cast(tab); IggyPlayerCallMethodRS(getMovie(), &result, IggyPlayerRootPath(getMovie()), m_funcSetActiveTab, 1, value); m_layersPanel.setVisible(tab == eTab_Layers); m_biomesPanel.setVisible(tab == eTab_Biomes); m_propertiesPanel.setVisible(tab == eTab_Properties); m_layersList.setVisible(tab == eTab_Layers); m_biomesList.setVisible(tab == eTab_Biomes); m_propertiesList.setVisible(tab == eTab_Properties); if (setFocus) { if (tab == eTab_Layers) { SetFocusToElement(eControl_LayersList); focusList(tab); m_layersList.setCurrentSelection(m_layerSelection); } else if (tab == eTab_Biomes) { SetFocusToElement(eControl_BiomesList); focusList(tab); m_biomesList.setCurrentSelection(m_biomeSelection); } else { SetFocusToElement(eControl_PropertiesList); focusList(tab); m_propertiesList.HighlightItem(m_paramSelection); } } updateHelp(); updateTooltips(); } void UIScene_SuperflatMenu::setHelp(const wstring &title, const wstring &body) { wchar_t startTags[64]; swprintf(startTags, 64, L"", app.GetHTMLColour(eHTMLColor_White)); wstring text = startTags + title + L"

" + body + L"
"; m_description.setLabel(text); } void UIScene_SuperflatMenu::updateHelp() { if (m_activeTab == eTab_Layers) { int layer = rowToLayer(m_layerSelection); vector &layers = *m_info.getLayers(); if (layer >= 0 && layer < static_cast(layers.size())) { int id = layers[layer].getId(); int data = layers[layer].getData(); setHelp(SuperflatData::GetBlockName(id, data), SuperflatData::GetBlockDescription(id, data)); } else { setHelp(L"", L""); } } else if (m_activeTab == eTab_Biomes) { setHelp(SuperflatData::GetBiomeName(m_biomeSelection), SuperflatData::GetBiomeDescription(m_biomeSelection)); } else { if (m_paramSelection < 0 || m_paramSelection >= SuperflatData::GetParamCount()) { setHelp(L"", L""); return; } int parent = SuperflatData::GetParamParent(m_paramSelection); const SuperflatData::Param &parentParam = SuperflatData::GetParam(parent); wstring body = parentParam.description; if (parent != m_paramSelection) { const SuperflatData::Param ¶m = SuperflatData::GetParam(m_paramSelection); body += wstring(L"

") + param.name + L"
" + param.description; } setHelp(parentParam.name, body); } } void UIScene_SuperflatMenu::openEditLayer(int layerIndex, bool insert) { commit(); m_editData.layerIndex = layerIndex; m_editData.insert = insert; ui.NavigateToScene(m_iPad, eUIScene_SuperflatEditLayerMenu, &m_editData); } void UIScene_SuperflatMenu::addLayerAbove(int row) { if (m_info.getTotalHeight() >= FlatGeneratorInfo::MAX_HEIGHT || layerCount() >= FlatGeneratorInfo::MAX_LAYERS) { m_pendingAction = ePending_MaxHeight; return; } int layer = rowToLayer(row); vector &layers = *m_info.getLayers(); int insertAt = layer + 1; if (insertAt < 0) insertAt = 0; if (insertAt > static_cast(layers.size())) insertAt = static_cast(layers.size()); layers.insert(layers.begin() + insertAt, FlatLayerInfo(1, Tile::stone_Id)); m_info.updateLayers(); m_layerSelection = layerCount() - 1 - insertAt; openEditLayer(insertAt, true); } void UIScene_SuperflatMenu::moveSelectedLayer(int direction) { int row = m_layerSelection; int target = row + direction; if (target < 0 || isBottomRow(target) || isBottomRow(row)) { ui.PlayUISFX(eSFX_CraftFail); return; } vector &layers = *m_info.getLayers(); std::swap(layers[rowToLayer(row)], layers[rowToLayer(target)]); m_info.updateLayers(); m_layerSelection = target; populateLayers(); m_layersList.setCurrentSelection(m_layerSelection); ui.PlayUISFX(eSFX_Press); commit(); updateHelp(); } void UIScene_SuperflatMenu::showLayerOptions() { UINT options[3]; options[0] = UICustomStrings::Get(L"Edit Layer"); options[1] = UICustomStrings::Get(L"Add Layer"); options[2] = UICustomStrings::Get(L"Delete Layer"); m_bBusy = true; ui.RequestAlertMessage(UICustomStrings::Get(L"Layer Options"), UICustomStrings::Get(L"Add, edit or delete a layer."), options, 3, m_iPad, &UIScene_SuperflatMenu::LayerOptionsCallback, this); } int UIScene_SuperflatMenu::LayerOptionsCallback(LPVOID lpParam, int iPad, const C4JStorage::EMessageResult result) { UIScene_SuperflatMenu *scene = static_cast(lpParam); scene->m_bBusy = false; switch (result) { case C4JStorage::EMessage_ResultAccept: scene->m_pendingAction = ePending_EditLayer; break; case C4JStorage::EMessage_ResultDecline: scene->m_pendingAction = ePending_AddLayer; break; case C4JStorage::EMessage_ResultThirdOption: scene->m_pendingAction = ePending_DeleteLayer; break; default: break; } return 0; } void UIScene_SuperflatMenu::tick() { if (m_bNeedsPopulate) { m_bNeedsPopulate = false; populateAll(); setActiveTab(m_activeTab, true); } if (m_pendingSliderId >= 0) { int id = m_pendingSliderId; m_pendingSliderId = -1; applySliderChange(id, m_pendingSliderValue); } int action = m_pendingAction; m_pendingAction = ePending_None; switch (action) { case ePending_EditLayer: openEditLayer(rowToLayer(m_layerSelection), false); break; case ePending_AddLayer: addLayerAbove(m_layerSelection); break; case ePending_DeleteLayer: if (layerCount() > 1 && !isBottomRow(m_layerSelection)) { vector &layers = *m_info.getLayers(); layers.erase(layers.begin() + rowToLayer(m_layerSelection)); m_info.updateLayers(); populateLayers(); m_layersList.setCurrentSelection(m_layerSelection); commit(); updateHelp(); updateTooltips(); } break; case ePending_Presets: commit(); ui.NavigateToScene(m_iPad, eUIScene_SuperflatPresetsMenu, &m_info); break; case ePending_MaxHeight: { UINT options[1]; options[0] = IDS_CONFIRM_OK; ui.RequestAlertMessage(UICustomStrings::Get(L"Maximum Height Reached"), UICustomStrings::Get(L"The world cannot be any taller. Remove or shrink a layer before adding another."), options, 1, m_iPad); } break; case ePending_ReadParams: readParams(); updateHelp(); break; case ePending_Biome: if (m_pendingBiome >= 0) { m_info.setBiome(SuperflatData::GetBiomeId(m_pendingBiome)); m_pendingBiome = -1; updateBiomeChecks(); commit(); } break; case ePending_Help: updateHelp(); break; } UIScene::tick(); } void UIScene_SuperflatMenu::updateTooltips() { int a = -1; int x = -1; int y = -1; int lb = -1; int rb = -1; if (m_bMovingLayer) { a = IDS_TOOLTIPS_PLACE; } else { x = UICustomStrings::Get(L"Presets"); lb = -2; rb = UICustomStrings::Get(L"Select Tab"); if (m_activeTab == eTab_Layers) { if (isBottomRow(m_layerSelection)) { a = UICustomStrings::Get(L"Add Layer"); } else { a = UICustomStrings::Get(L"Layer Options"); y = UICustomStrings::Get(L"Move Layer"); } } else { a = IDS_TOOLTIPS_SELECT; } } ui.SetTooltips(m_iPad, a, IDS_TOOLTIPS_BACK, x, y, -1, -1, lb, rb); } void UIScene_SuperflatMenu::updateComponents() { m_parentLayer->showComponent(m_iPad, eUIComponent_Panorama, true); m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, false); } void UIScene_SuperflatMenu::handleGainFocus(bool navBack) { UIScene::handleGainFocus(navBack); if (navBack) { m_bMovingLayer = false; m_bBusy = false; m_biomeSelection = SuperflatData::GetBiomeIndex(m_info.getBiome()); m_bNeedsPopulate = true; commit(); } } void UIScene_SuperflatMenu::customDraw(IggyCustomDrawCallbackRegion *region) { int list = -1; int index = -1; if (swscanf(static_cast(region->name), L"slot_%d_%d", &list, &index) != 2) return; shared_ptr item = nullptr; if (list == eControl_LayersList) { int layer = rowToLayer(index); vector &layers = *m_info.getLayers(); if (layer >= 0 && layer < static_cast(layers.size())) item = SuperflatData::GetBlockIcon(layers[layer].getId(), layers[layer].getData()); } else if (list == eControl_BiomesList) { item = SuperflatData::GetBiomeIcon(index); } if (item != nullptr) customDrawSlotControl(region, m_iPad, item, 1.0f, false, false); } void UIScene_SuperflatMenu::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled) { if (m_bBusy) return; ui.AnimateKeyPress(iPad, key, repeat, pressed, released); switch (key) { case ACTION_MENU_CANCEL: if (pressed) { if (m_bMovingLayer) { m_bMovingLayer = false; updateTooltips(); } else { commit(); navigateBack(); } } handled = true; break; case ACTION_MENU_X: if (pressed && !repeat && !m_bMovingLayer) { ui.PlayUISFX(eSFX_Press); m_pendingAction = ePending_Presets; } handled = true; break; case ACTION_MENU_Y: if (pressed && !repeat && m_activeTab == eTab_Layers && !isBottomRow(m_layerSelection)) { ui.PlayUISFX(eSFX_Press); m_bMovingLayer = !m_bMovingLayer; updateTooltips(); } handled = true; break; case ACTION_MENU_LEFT_SCROLL: case ACTION_MENU_RIGHT_SCROLL: if (pressed && !repeat && !m_bMovingLayer) { ui.PlayUISFX(eSFX_Focus); setActiveTab(m_activeTab + (key == ACTION_MENU_RIGHT_SCROLL ? 1 : -1), true); } handled = true; break; case ACTION_MENU_UP: case ACTION_MENU_DOWN: if (m_bMovingLayer) { if (pressed) moveSelectedLayer(key == ACTION_MENU_UP ? -1 : 1); handled = true; break; } if (pressed) stepSelection(key == ACTION_MENU_UP ? -1 : 1); handled = true; break; case ACTION_MENU_OK: if (m_bMovingLayer) { if (pressed) { ui.PlayUISFX(eSFX_Press); m_bMovingLayer = false; updateTooltips(); } handled = true; break; } sendInputToMovie(key, repeat, pressed, released); handled = true; break; case ACTION_MENU_LEFT: case ACTION_MENU_RIGHT: case ACTION_MENU_PAGEUP: case ACTION_MENU_OTHER_STICK_UP: case ACTION_MENU_OTHER_STICK_DOWN: if (!m_bMovingLayer && (key == ACTION_MENU_OTHER_STICK_UP || key == ACTION_MENU_OTHER_STICK_DOWN)) { if (pressed) stepSelection(key == ACTION_MENU_OTHER_STICK_UP ? -1 : 1); handled = true; break; } case ACTION_MENU_PAGEDOWN: if (!m_bMovingLayer) sendInputToMovie(key, repeat, pressed, released); handled = true; break; } } void UIScene_SuperflatMenu::handlePress(F64 controlId, F64 childId) { if (!m_bPopulated || m_bBusy || m_bMovingLayer) return; int list = static_cast(controlId); int id = static_cast(childId); switch (list) { case eControl_LayersList: ui.PlayUISFX(eSFX_Press); m_layerSelection = id; if (isBottomRow(id)) m_pendingAction = ePending_AddLayer; else showLayerOptions(); break; case eControl_BiomesList: ui.PlayUISFX(eSFX_Press); m_biomeSelection = id; m_pendingBiome = id; m_pendingAction = ePending_Biome; break; case eControl_PropertiesList: ui.PlayUISFX(eSFX_Press); m_paramSelection = id; m_pendingAction = ePending_ReadParams; break; } } void UIScene_SuperflatMenu::handleCheckboxToggled(F64 controlId, bool selected) { if (!m_bPopulated || m_bBusy || m_bMovingLayer) return; if (static_cast(controlId) == eControl_PropertiesList) m_pendingAction = ePending_ReadParams; } void UIScene_SuperflatMenu::handleFocusChange(F64 controlId, F64 childId) { int list = static_cast(controlId); int id = static_cast(childId); if (list == eControl_LayersList && m_activeTab == eTab_Layers) { m_layersList.updateChildFocus(id); m_layerSelection = id; } else if (list == eControl_BiomesList && m_activeTab == eTab_Biomes) { m_biomesList.updateChildFocus(id); m_biomeSelection = id; } else if (list == eControl_PropertiesList && m_activeTab == eTab_Properties) { m_propertiesList.updateChildFocus(id); m_paramSelection = id; } else { return; } if (m_pendingAction == ePending_None) m_pendingAction = ePending_Help; updateTooltips(); } void UIScene_SuperflatMenu::handleSliderMove(F64 sliderId, F64 currentValue) { int id = static_cast(sliderId); int value = static_cast(currentValue); m_propertiesList.handleSliderMove(id, value); if (!m_bPopulated) return; if (id >= 0 && id < SuperflatData::GetParamCount()) { m_pendingSliderId = id; m_pendingSliderValue = value; } } void UIScene_SuperflatMenu::applySliderChange(int id, int value) { SuperflatData::WriteParamValue(m_info, id, value); m_propertiesList.SetSliderLabel(id, SuperflatData::GetParamLabel(id, value)); int next = id + 1; if (next < SuperflatData::GetParamCount() && SuperflatData::GetParam(next).kind == SuperflatData::eParam_SeparationSlider && SuperflatData::GetParamParent(next) == SuperflatData::GetParamParent(id)) SuperflatData::WriteParamValue(m_info, next, m_propertiesList.GetSliderValue(next)); commit(); } #ifdef _WINDOWS64 bool UIScene_SuperflatMenu::handleMouseClick(F32 x, F32 y) { if (m_bBusy) return true; if (y >= kTabY && y <= kTabY + kTabH && x >= kTabX && x <= kTabX + kTabW * eTab_Count) { int tab = static_cast((x - kTabX) / kTabW); if (!m_bMovingLayer && tab != m_activeTab) { ui.PlayUISFX(eSFX_Focus); setActiveTab(tab, true); } return true; } return UIScene::handleMouseClick(x, y); } #endif