|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Samarium |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>. |
| 16 | + */ |
| 17 | +package io.github.samarium150.minecraft.mod.structures_compass.client.gui |
| 18 | + |
| 19 | +import io.github.samarium150.minecraft.mod.structures_compass.client.gui.widget.StructureSearchEntry |
| 20 | +import io.github.samarium150.minecraft.mod.structures_compass.client.gui.widget.StructureSearchList |
| 21 | +import io.github.samarium150.minecraft.mod.structures_compass.client.gui.widget.TransparentButton |
| 22 | +import io.github.samarium150.minecraft.mod.structures_compass.client.gui.widget.TransparentTextField |
| 23 | +import io.github.samarium150.minecraft.mod.structures_compass.client.util.getLocalizedStructureName |
| 24 | +import io.github.samarium150.minecraft.mod.structures_compass.client.util.sort.Category |
| 25 | +import io.github.samarium150.minecraft.mod.structures_compass.client.util.sort.NameCategory |
| 26 | +import io.github.samarium150.minecraft.mod.structures_compass.data.StructuresCompassData |
| 27 | +import io.github.samarium150.minecraft.mod.structures_compass.network.packet.c2s.SearchPacket |
| 28 | +import io.github.samarium150.minecraft.mod.structures_compass.network.packet.c2s.SetSkipPacket |
| 29 | +import io.github.samarium150.minecraft.mod.structures_compass.util.* |
| 30 | +import net.fabricmc.api.EnvType |
| 31 | +import net.fabricmc.api.Environment |
| 32 | +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking |
| 33 | +import net.minecraft.client.gui.screen.Screen |
| 34 | +import net.minecraft.client.gui.widget.ButtonWidget |
| 35 | +import net.minecraft.client.util.math.MatrixStack |
| 36 | +import net.minecraft.item.ItemStack |
| 37 | +import net.minecraft.text.TranslatableText |
| 38 | +import net.minecraft.world.gen.feature.StructureFeature |
| 39 | +import java.util.* |
| 40 | + |
| 41 | +@Environment(EnvType.CLIENT) |
| 42 | +class StructuresCompassScreen(private val itemStack: ItemStack) : Screen( |
| 43 | + TranslatableText("${prefix}select_structure") |
| 44 | +) { |
| 45 | + private val allowedStructures: List<StructureFeature<*>> = StructuresCompassData.allowedStructures |
| 46 | + private var structuresMatchingSearch: MutableList<StructureFeature<*>> = allowedStructures.toMutableList() |
| 47 | + private var sortingCategory: Category = NameCategory |
| 48 | + private lateinit var selectionList: StructureSearchList |
| 49 | + private var skip: Boolean = itemStack.getSkip() |
| 50 | + private lateinit var startSearchButton: ButtonWidget |
| 51 | + private lateinit var sortByButton: ButtonWidget |
| 52 | + private lateinit var skipExistingChunksButton: ButtonWidget |
| 53 | + private lateinit var searchTextField: TransparentTextField |
| 54 | + private var selected: StructureFeature<*>? = null |
| 55 | + |
| 56 | + private fun setup() { |
| 57 | + buttons.clear() |
| 58 | + startSearchButton = addButton( |
| 59 | + TransparentButton( |
| 60 | + 10, |
| 61 | + 40, |
| 62 | + 110, |
| 63 | + 20, |
| 64 | + TranslatableText("${prefix}start_searching") |
| 65 | + ) { |
| 66 | + selectionList.selected?.search() |
| 67 | + }) |
| 68 | + sortByButton = addButton( |
| 69 | + TransparentButton( |
| 70 | + 10, |
| 71 | + 65, |
| 72 | + 110, |
| 73 | + 20, |
| 74 | + TranslatableText("${prefix}sort_by").append(": ${sortingCategory.localizedName}") |
| 75 | + ) { |
| 76 | + sortingCategory = sortingCategory.next() |
| 77 | + sortByButton.message = TranslatableText("${prefix}sort_by").append(": ${sortingCategory.localizedName}") |
| 78 | + selectionList.refresh() |
| 79 | + }) |
| 80 | + skipExistingChunksButton = addButton(TransparentButton( |
| 81 | + 10, 90, 110, 20, |
| 82 | + TranslatableText("${prefix}skip_existing_chunks").append(": $skip") |
| 83 | + ) { |
| 84 | + skip = !skip |
| 85 | + skipExistingChunksButton.message = TranslatableText("${prefix}skip_existing_chunks").append(": $skip") |
| 86 | + }) |
| 87 | + addButton(TransparentButton( |
| 88 | + 10, height - 30, 110, 20, |
| 89 | + TranslatableText("gui.cancel") |
| 90 | + ) { |
| 91 | + minecraftClient.currentScreen = null |
| 92 | + }) |
| 93 | + searchTextField = addChild(TransparentTextField( |
| 94 | + textRenderer, |
| 95 | + 130, 10, 140, 20, |
| 96 | + TranslatableText("${prefix}search") |
| 97 | + )) |
| 98 | + startSearchButton.active = false |
| 99 | + } |
| 100 | + |
| 101 | + fun selectStructure(entry: StructureSearchEntry?) { |
| 102 | + startSearchButton.active = entry != null |
| 103 | + if (entry != null) selected = entry.structure |
| 104 | + } |
| 105 | + |
| 106 | + fun sortStructures(): List<StructureFeature<*>> { |
| 107 | + Collections.sort(structuresMatchingSearch, NameCategory) |
| 108 | + Collections.sort(structuresMatchingSearch, sortingCategory) |
| 109 | + return structuresMatchingSearch |
| 110 | + } |
| 111 | + |
| 112 | + fun search(structure: StructureFeature<*>) { |
| 113 | + ClientPlayNetworking.send(SetSkipPacket.ID, SetSkipPacket(skip)) |
| 114 | + ClientPlayNetworking.send(SearchPacket.ID, SearchPacket(structure.getIdentifier()!!)) |
| 115 | + minecraftClient.currentScreen = null |
| 116 | + } |
| 117 | + |
| 118 | + private fun restoreSelected() { |
| 119 | + if (selected != null && structuresMatchingSearch.contains(selected)) selectionList.selectStructure(selected!!) |
| 120 | + selectionList.restoreScrollAmount() |
| 121 | + } |
| 122 | + |
| 123 | + private fun processSearchTerm() { |
| 124 | + structuresMatchingSearch = mutableListOf() |
| 125 | + for (structure in allowedStructures) { |
| 126 | + var temp = "" |
| 127 | + if (searchTextField.text.isNotEmpty() && searchTextField.text[0] == '#') temp = |
| 128 | + structure.getDimensions().toString() |
| 129 | + if ((searchTextField.text.isNotEmpty() && // source search |
| 130 | + searchTextField.text[0] == '@' && |
| 131 | + structure.getNamespace().toString().lowercase(Locale.ROOT) |
| 132 | + .contains(searchTextField.text.substring(1).lowercase(Locale.ROOT)) || |
| 133 | + (temp.isNotEmpty() && temp.lowercase(Locale.ROOT) // dim search |
| 134 | + .contains(searchTextField.text.substring(1).lowercase(Locale.ROOT)))) || |
| 135 | + (structure.getIdentifier()?.getLocalizedStructureName()?.lowercase(Locale.ROOT) // normal search |
| 136 | + ?.contains(searchTextField.text.lowercase(Locale.getDefault())) == true) |
| 137 | + ) { |
| 138 | + structuresMatchingSearch.add(structure) |
| 139 | + } |
| 140 | + } |
| 141 | + selectionList.refresh() |
| 142 | + restoreSelected() |
| 143 | + } |
| 144 | + |
| 145 | + override fun init() { |
| 146 | + minecraftClient.keyboard.setRepeatEvents(true) |
| 147 | + setup() |
| 148 | + if (!this::selectionList.isInitialized) |
| 149 | + selectionList = StructureSearchList( |
| 150 | + this, width + 110, height, 40, height, 45 |
| 151 | + ) |
| 152 | + val structureId = itemStack.getStructure() |
| 153 | + if (structureId != null) { |
| 154 | + selectionList.selectStructure(structureId.getStructureFeature()!!) |
| 155 | + selectionList.changeFocus(true) |
| 156 | + selectionList.restoreScrollAmount() |
| 157 | + } |
| 158 | + addChild(selectionList) |
| 159 | + } |
| 160 | + |
| 161 | + override fun tick() { |
| 162 | + searchTextField.tick() |
| 163 | + } |
| 164 | + |
| 165 | + override fun render(matrices: MatrixStack, mouseX: Int, mouseY: Int, delta: Float) { |
| 166 | + renderBackground(matrices) |
| 167 | + selectionList.render(matrices, mouseX, mouseY, delta) |
| 168 | + searchTextField.render(matrices, mouseX, mouseY, delta) |
| 169 | + drawCenteredText(matrices, textRenderer, title, 65, 15, 0xFFFFFF) |
| 170 | + super.render(matrices, mouseX, mouseY, delta) |
| 171 | + } |
| 172 | + |
| 173 | + override fun isPauseScreen(): Boolean { |
| 174 | + return false |
| 175 | + } |
| 176 | + |
| 177 | + override fun mouseScrolled(mouseX: Double, mouseY: Double, amount: Double): Boolean { |
| 178 | + return selectionList.mouseScrolled(mouseX, mouseY, amount) |
| 179 | + } |
| 180 | + |
| 181 | + override fun charTyped(codePoint: Char, modifiers: Int): Boolean { |
| 182 | + val ret = super.charTyped(codePoint, modifiers) |
| 183 | + if (searchTextField.isFocused) { |
| 184 | + processSearchTerm() |
| 185 | + return true |
| 186 | + } |
| 187 | + return ret |
| 188 | + } |
| 189 | + |
| 190 | + override fun removed() { |
| 191 | + super.removed() |
| 192 | + minecraftClient.keyboard.setRepeatEvents(false) |
| 193 | + } |
| 194 | +} |
0 commit comments