Skip to content
Snippets Groups Projects
Commit 049c0c30 authored by Test's avatar Test Committed by Hiroku
Browse files

Add functionality to search by species, ability, or drops in dex

parent a96880b8
No related branches found
No related tags found
No related merge requests found
......@@ -94,7 +94,7 @@ abstract class AbstractPokedexManager {
if (formRecord == null || formRecord.knowledge == PokedexEntryProgress.NONE) {
return PokedexLearnedInformation.FORM
}
if (pokedexEntityData.aspects.none(speciesRecord::hasAspect) || pokedexEntityData.gender !in formRecord.getGenders() || !formRecord.hasSeenShinyState(pokedexEntityData.shiny)) {
if (pokedexEntityData.aspects.any{ !speciesRecord.hasAspect(it) } || pokedexEntityData.gender !in formRecord.getGenders() || !formRecord.hasSeenShinyState(pokedexEntityData.shiny)) {
return PokedexLearnedInformation.VARIATION
}
return PokedexLearnedInformation.NONE
......
/*
* Copyright (C) 2023 Cobblemon Contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.cobblemon.mod.common.api.pokedex.filter
enum class SearchByType {
SPECIES,
ABILITIES,
DROPS;
}
\ No newline at end of file
......@@ -8,10 +8,16 @@
package com.cobblemon.mod.common.api.pokedex.filter
import com.cobblemon.mod.common.api.drop.ItemDropEntry
import com.cobblemon.mod.common.api.pokedex.AbstractPokedexManager
import com.cobblemon.mod.common.api.pokedex.PokedexEntryProgress
import com.cobblemon.mod.common.api.pokedex.entry.PokedexEntry
import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
import com.cobblemon.mod.common.pokemon.abilities.HiddenAbility
import com.cobblemon.mod.common.util.asTranslated
import com.cobblemon.mod.common.util.itemRegistry
import net.minecraft.client.Minecraft
import net.minecraft.world.item.ItemStack
/**
* A Pokedex [EntryFilter] that filters out entries that do not contain the current search.
......@@ -20,12 +26,45 @@ import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
* @since September 4th, 2024
* @param searchString The string to use when checking.
*/
class SearchFilter(val pokedexManager: AbstractPokedexManager, val searchString: String) : EntryFilter() {
class SearchFilter(val pokedexManager: AbstractPokedexManager, val searchString: String, val searchByType: SearchByType = SearchByType.SPECIES) : EntryFilter() {
override fun test(entry: PokedexEntry): Boolean {
if (searchString == "") return true
val species = PokemonSpecies.getByIdentifier(entry.speciesId) ?: return false
if (pokedexManager.getHighestKnowledgeFor(entry) == PokedexEntryProgress.NONE) return false
return species.translatedName.string.contains(searchString.trim(), true)
when(searchByType) {
SearchByType.ABILITIES -> {
if (pokedexManager.getHighestKnowledgeFor(entry) !== PokedexEntryProgress.CAUGHT) return false
val abilityList = mutableListOf<String>()
val formsList = if (species.forms.isEmpty()) mutableListOf(species.standardForm) else species.forms
formsList.forEach {
it.abilities.sortedBy { it is HiddenAbility }.map { ability -> ability.template }.forEach {
abilityList.add(it.displayName.asTranslated().string.lowercase())
}
}
return abilityList.any { it.contains(searchString.trim().lowercase()) }
}
SearchByType.DROPS -> {
if (pokedexManager.getHighestKnowledgeFor(entry) !== PokedexEntryProgress.CAUGHT) return false
val dropsList = mutableListOf<String>()
val formsList = if (species.forms.isEmpty()) mutableListOf(species.standardForm) else species.forms
formsList.forEach {
it.drops.entries.forEach {
if (it is ItemDropEntry) {
val itemStack = Minecraft.getInstance().player?.level()?.itemRegistry?.get(it.item)?.defaultInstance ?: ItemStack.EMPTY
if (!itemStack.isEmpty) dropsList.add(itemStack.displayName.string.lowercase())
}
}
}
return dropsList.any { it.contains(searchString.trim().lowercase()) }
}
// Search by species name
else -> {
return species.translatedName.string.contains(searchString.trim(), true)
}
}
}
}
\ No newline at end of file
......@@ -19,17 +19,18 @@ import com.cobblemon.mod.common.api.pokedex.def.PokedexDef
import com.cobblemon.mod.common.api.pokedex.entry.PokedexEntry
import com.cobblemon.mod.common.api.pokedex.entry.PokedexForm
import com.cobblemon.mod.common.api.pokedex.filter.EntryFilter
import com.cobblemon.mod.common.api.pokedex.filter.SearchByType
import com.cobblemon.mod.common.api.pokedex.filter.SearchFilter
import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
import com.cobblemon.mod.common.api.storage.player.client.ClientPokedexManager
import com.cobblemon.mod.common.api.text.bold
import com.cobblemon.mod.common.api.text.font
import com.cobblemon.mod.common.api.text.text
import com.cobblemon.mod.common.client.ClientMoLangFunctions.setupClient
import com.cobblemon.mod.common.client.CobblemonClient
import com.cobblemon.mod.common.client.CobblemonResources
import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.BASE_HEIGHT
import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.BASE_WIDTH
import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.HALF_OVERLAY_WIDTH
import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.HEADER_BAR_HEIGHT
import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.SCALE
import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.TAB_ABILITIES
......@@ -50,6 +51,7 @@ import com.cobblemon.mod.common.client.pokedex.PokedexTypes
import com.cobblemon.mod.common.client.render.drawScaledText
import com.cobblemon.mod.common.pokemon.abilities.HiddenAbility
import com.cobblemon.mod.common.util.cobblemonResource
import com.cobblemon.mod.common.util.lang
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.Renderable
......@@ -71,6 +73,37 @@ class PokedexGUI private constructor(
val type: PokedexTypes,
val initSpecies: ResourceLocation?
): Screen(Component.translatable("cobblemon.ui.pokedex.title")) {
companion object {
private val screenBackground = cobblemonResource("textures/gui/pokedex/pokedex_screen.png")
private val globeIcon = cobblemonResource("textures/gui/pokedex/globe_icon.png")
private val caughtSeenIcon = cobblemonResource("textures/gui/pokedex/caught_seen_icon.png")
private val arrowUpIcon = cobblemonResource("textures/gui/pokedex/arrow_up.png")
private val arrowDownIcon = cobblemonResource("textures/gui/pokedex/arrow_down.png")
private val tooltipEdge = cobblemonResource("textures/gui/pokedex/tooltip_edge.png")
private val tooltipBackground = cobblemonResource("textures/gui/pokedex/tooltip_background.png")
private val tabSelectArrow = cobblemonResource("textures/gui/pokedex/select_arrow.png")
private val tabIcons = arrayOf(
cobblemonResource("textures/gui/pokedex/tab_info.png"),
cobblemonResource("textures/gui/pokedex/tab_abilities.png"),
cobblemonResource("textures/gui/pokedex/tab_size.png"),
cobblemonResource("textures/gui/pokedex/tab_stats.png"),
cobblemonResource("textures/gui/pokedex/tab_drops.png")
)
/**
* Attempts to open this screen for a client.
*/
fun open(pokedex: ClientPokedexManager, type: PokedexTypes, species: ResourceLocation? = null) {
val mc = Minecraft.getInstance()
val screen = PokedexGUI(type, species)
mc.setScreen(screen)
}
}
var oldDragPosX = 0.0
var canDragRender = false
......@@ -88,12 +121,14 @@ class PokedexGUI private constructor(
private var availableRegions = emptyList<ResourceLocation>()
private var selectedRegionIndex = 0
private lateinit var regionSelectWidgetUp: ScaledButton
private lateinit var regionSelectWidgetDown: ScaledButton
private lateinit var searchByTypeButton: ScaledButton
private lateinit var scrollScreen: EntriesScrollingWidget
private lateinit var pokemonInfoWidget: PokemonInfoWidget
private lateinit var searchWidget: SearchWidget
private lateinit var regionSelectWidgetUp: ScaledButton
private lateinit var regionSelectWidgetDown: ScaledButton
private var selectedSearchByType: SearchByType = SearchByType.SPECIES
private val tabButtons: MutableList<ScaledButton> = mutableListOf()
lateinit var tabInfoElement: GuiEventListener
......@@ -132,7 +167,7 @@ class PokedexGUI private constructor(
displaytabInfoElement(tabInfoIndex, false)
if (::searchWidget.isInitialized) removeWidget(searchWidget)
searchWidget = SearchWidget(x + 26, y + 28, HALF_OVERLAY_WIDTH, HEADER_BAR_HEIGHT, update = ::updateFilters)
searchWidget = SearchWidget(x + 26, y + 28, 128, HEADER_BAR_HEIGHT, update = ::updateFilters)
addRenderableWidget(searchWidget)
if (::regionSelectWidgetUp.isInitialized) removeWidget(regionSelectWidgetUp)
......@@ -157,9 +192,26 @@ class PokedexGUI private constructor(
resource = arrowDownIcon,
clickAction = { updatePokedexRegion(true) }
)
addRenderableWidget(regionSelectWidgetDown)
if (::searchByTypeButton.isInitialized) removeWidget(searchByTypeButton)
searchByTypeButton = ScaledButton(
buttonX = (x + 154.5).toFloat(),
buttonY = (y + 29.5).toFloat(),
buttonWidth = 16,
buttonHeight = 16,
scale = SCALE,
resource = cobblemonResource("textures/gui/pokedex/tab_${selectedSearchByType.name.lowercase()}.png"),
clickAction = {
val searchTypes = SearchByType.entries.toList()
val selectedIndex = searchTypes.indexOf(selectedSearchByType)
selectedSearchByType = searchTypes.get(if (selectedIndex == searchTypes.lastIndex) 0 else (selectedIndex + 1))
(it as ScaledButton).resource = cobblemonResource("textures/gui/pokedex/tab_${selectedSearchByType.name.lowercase()}.png")
updateFilters()
}
)
addRenderableWidget(searchByTypeButton)
updateFilters(true)
}
......@@ -271,6 +323,21 @@ class PokedexGUI private constructor(
}
super.render(context, mouseX, mouseY, delta)
// Search type tooltip
if (searchByTypeButton.isButtonHovered(mouseX, mouseY)) {
matrices.pushPose()
matrices.translate(0.0, 0.0, 1000.0)
val searchTypeText = lang("ui.pokedex.search.search_by", lang("ui.pokedex.search.type.${selectedSearchByType.name.lowercase()}")).bold()
val searchTypeTextWidth = Minecraft.getInstance().font.width(searchTypeText.font(CobblemonResources.DEFAULT_LARGE))
val tooltipWidth = searchTypeTextWidth + 6
blitk(matrixStack = matrices, texture = tooltipEdge, x = mouseX - (tooltipWidth / 2) - 1, y = mouseY - 16, width = 1, height = 11)
blitk(matrixStack = matrices, texture = tooltipBackground, x = mouseX - (tooltipWidth / 2), y = mouseY - 16, width = tooltipWidth, height = 11)
blitk(matrixStack = matrices, texture = tooltipEdge, x = mouseX + (tooltipWidth / 2), y = mouseY - 16, width = 1, height = 11)
drawScaledText(context = context, font = CobblemonResources.DEFAULT_LARGE, text = searchTypeText, x = mouseX, y = mouseY - 15, shadow = true, centered = true)
matrices.popPose()
}
}
override fun onClose() {
......@@ -363,7 +430,7 @@ class PokedexGUI private constructor(
fun getFilters(): Collection<EntryFilter> {
val filters: MutableList<EntryFilter> = mutableListOf()
filters.add(SearchFilter(CobblemonClient.clientPokedexData, searchWidget.value))
filters.add(SearchFilter(CobblemonClient.clientPokedexData, searchWidget.value, selectedSearchByType))
return filters
}
......@@ -511,33 +578,4 @@ class PokedexGUI private constructor(
fun playSound(soundEvent: SoundEvent) {
Minecraft.getInstance().soundManager.play(SimpleSoundInstance.forUI(soundEvent, 1.0F))
}
companion object {
private val screenBackground = cobblemonResource("textures/gui/pokedex/pokedex_screen.png")
private val globeIcon = cobblemonResource("textures/gui/pokedex/globe_icon.png")
private val caughtSeenIcon = cobblemonResource("textures/gui/pokedex/caught_seen_icon.png")
private val arrowUpIcon = cobblemonResource("textures/gui/pokedex/arrow_up.png")
private val arrowDownIcon = cobblemonResource("textures/gui/pokedex/arrow_down.png")
private val tabSelectArrow = cobblemonResource("textures/gui/pokedex/select_arrow.png")
private val tabIcons = arrayOf(
cobblemonResource("textures/gui/pokedex/tab_info.png"),
cobblemonResource("textures/gui/pokedex/tab_abilities.png"),
cobblemonResource("textures/gui/pokedex/tab_size.png"),
cobblemonResource("textures/gui/pokedex/tab_stats.png"),
cobblemonResource("textures/gui/pokedex/tab_drops.png")
)
/**
* Attempts to open this screen for a client.
*/
fun open(pokedex: ClientPokedexManager, type: PokedexTypes, species: ResourceLocation? = null) {
val mc = Minecraft.getInstance()
val screen = PokedexGUI(type, species)
mc.setScreen(screen)
}
}
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.HALF_OVER
import com.cobblemon.mod.common.client.gui.pokedex.PokedexGUIConstants.SCALE
import com.cobblemon.mod.common.client.render.drawScaledText
import com.cobblemon.mod.common.util.cobblemonResource
import com.cobblemon.mod.common.util.lang
import net.minecraft.Util
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiGraphics
......@@ -42,10 +43,8 @@ class SearchWidget(
var focusedTime: Long = 0
init {
this.setMaxLength(24)
this.setResponder {
update.invoke()
}
this.setMaxLength(23)
this.setResponder { update.invoke() }
focusedTime = Util.getMillis()
}
......@@ -82,7 +81,7 @@ class SearchWidget(
val showCursor = isFocused && ((Util.getMillis() - this.focusedTime) / 300L % 2L == 0L)
val input = if (isFocused) "${value}${if ((cursorPosition == value.length) && showCursor) "_" else ""}".text()
else (if(value.isEmpty()) Component.translatable("cobblemon.ui.pokedex.search") else value.text())
else (if(value.isEmpty()) lang("ui.pokedex.search") else value.text())
val startX = posX.toInt() + 13
val startY = posY.toInt() + 1
drawScaledText(
......
......@@ -755,6 +755,11 @@
"cobblemon.ui.pokedex.region.unknown": "Unknown",
"cobblemon.ui.pokedex.region.unova": "Unova",
 
"cobblemon.ui.pokedex.search.search_by": "Search by %1$s",
"cobblemon.ui.pokedex.search.type.species": "Species Name",
"cobblemon.ui.pokedex.search.type.abilities": "Ability",
"cobblemon.ui.pokedex.search.type.drops": "Drops",
"cobblemon.ui.pokedex.info.ability": "Ability: %1$s",
"cobblemon.ui.pokedex.info.entry": "Entry",
"cobblemon.ui.pokedex.info.stats": "Base Stats",
......
common/src/main/resources/assets/cobblemon/textures/gui/pokedex/tab_species.png

320 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment