Source code for bauiv1lib.docuitestitems

# Released under the MIT License. See LICENSE for details.
#
"""The doc-ui display-items test page.

A display-item stays what it always was -- *show this thing in this
style*. It turns into plain doc-ui decorations
(``bacommon.displayitem.DisplayItem.decorations``) rather than being
derived client-side from the item type. This page is a renderer
regression check -- every item type and style, drawable with no server
and no account.
"""

from __future__ import annotations  # Docs-generation hack.

from typing import TYPE_CHECKING
from dataclasses import replace

from bacommon.langstr import LangStrSpecValue
from bacommon.displayitem import DisplayItemStyle
import bacommon.legacydisplayitem as lditm
import bacommon.docui.v2 as dui2

if TYPE_CHECKING:
    import bacommon.docui.v2
    import bacommon.docui.routes.docuitest


#: Each (style, bounds, x-offset) slot on a test button, paired with the
#: y-offsets below. The left of each pair shows the item normally; the
#: right shows it with its type stripped, exercising the display-item
#: path's baked-description fallback.
_SLOTS: list[tuple[DisplayItemStyle, tuple[float, float], float]] = [
    (DisplayItemStyle.FULL, (120, 120), 62),
    (DisplayItemStyle.COMPACT, (80, 80), 55),
    (DisplayItemStyle.ICON, (100, 80), 55),
]
_SLOT_YS = [100.0, -20.0, -120.0]


[docs] def test_page_display_items( route: bacommon.docui.routes.docuitest.DisplayItems, ) -> bacommon.docui.v2.Response: """Testing display-items (v2 mirror of '/displayitems').""" import copy from bacommon.classic import ClassicChestAppearance, ClassicChestDisplayItem # pylint: disable=cyclic-import # Safe up-call: baclassic is fully imported by the time a doc-ui # page is being authored. from baclassic import display_item_decorations from bauiv1 import _docuiv2testassets strs = _docuiv2testassets.strings # Show some specific debug bits if they ask us to. debug = route.debug def _make_test_button( scale: float, wrapper: lditm.Wrapper, ) -> bacommon.docui.v2.Button: # See how this looks when unrecognized (relying on wrapper info # only). uwrapper = copy.deepcopy(wrapper) uwrapper.item = lditm.Unknown() decorations: list[dui2.Decoration] = [] for (style, size, xoffs), yoffs in zip(_SLOTS, _SLOT_YS): for wrp, xsign in ((wrapper, -1.0), (uwrapper, 1.0)): if wrp is uwrapper: # Skip the fallback copy: it is the *same* item with # its type stripped, and the depiction draws an # unrecognized item exactly as it draws a known # one, so it would just render twice. continue decorations.extend( display_item_decorations( wrp, position=(xoffs * xsign, yoffs), size=size, style=style, debug=debug, ) ) return dui2.Button( size=(300, 400), scale=scale, decorations=decorations, ) return dui2.Response( page=dui2.Page( title=strs.items.display_items.spec, rows=[ dui2.ButtonRow( debug=debug, title=strs.items.display_item_tests.spec, subtitle=LangStrSpecValue.literal( 'top=FULL, center=COMPACT, bottom=ICON' ), buttons=[ _make_test_button( 1.0, lditm.Wrapper.for_item(lditm.Tickets(count=213)), ), _make_test_button( 0.47, lditm.Wrapper.for_item(lditm.Tickets(count=213)), ), _make_test_button( 1.0, lditm.Wrapper.for_item( ClassicChestDisplayItem( appearance=ClassicChestAppearance.L3 ) ), ), _make_test_button( 1.0, lditm.Wrapper.for_item(lditm.Tokens(count=3)), ), _make_test_button( 1.0, lditm.Wrapper.for_item(lditm.Tokens(count=1414287)), ), _make_test_button( 1.0, lditm.Wrapper.for_item(lditm.Test()), ), ], ), dui2.ButtonRow( buttons=[ dui2.Button( label=( strs.common.hide_debug.spec if debug else strs.common.show_debug.spec ), style=dui2.ButtonStyle.MEDIUM, size=(240, 60), color=(0.6, 0.4, 0.8, 1.0), action=replace(route, debug=not debug).replace(), ), ], ), ], ) )
# Docs-generation hack; import some stuff that we likely only forward-declared # in our actual source code so that docs tools can find it. from typing import (Coroutine, Any, Literal, Callable, Generator, Awaitable, Sequence, Self, TypeIs) import asyncio from concurrent.futures import Future from pathlib import Path from enum import Enum