Move inventory menu description text widget 2 pixels down

Also move description text logic out of TableWidget
This commit is contained in:
UnknownShadow200 2022-11-02 20:04:32 +11:00
parent 18ab062c68
commit c1219ffef4
4 changed files with 45 additions and 48 deletions

View File

@ -886,8 +886,8 @@ static cc_result Sound_ReadWaveData(struct Stream* stream, struct Sound* snd) {
if ((res = Stream_Read(stream, tmp, 12))) return res; if ((res = Stream_Read(stream, tmp, 12))) return res;
fourCC = Stream_GetU32_BE(tmp + 0); fourCC = Stream_GetU32_BE(tmp + 0);
if (fourCC == WAV_FourCC('I', 'D', '3', 2)) return AUDIO_ERR_MP3_SIG; /* ID3 v2.2 tags header */ if (fourCC == WAV_FourCC('I','D','3', 2)) return AUDIO_ERR_MP3_SIG; /* ID3 v2.2 tags header */
if (fourCC == WAV_FourCC('I', 'D', '3', 3)) return AUDIO_ERR_MP3_SIG; /* ID3 v2.3 tags header */ if (fourCC == WAV_FourCC('I','D','3', 3)) return AUDIO_ERR_MP3_SIG; /* ID3 v2.3 tags header */
if (fourCC != WAV_FourCC('R','I','F','F')) return WAV_ERR_STREAM_HDR; if (fourCC != WAV_FourCC('R','I','F','F')) return WAV_ERR_STREAM_HDR;
/* tmp[4] (4) file size */ /* tmp[4] (4) file size */

View File

@ -1396,9 +1396,40 @@ static struct InventoryScreen {
Screen_Body Screen_Body
struct FontDesc font; struct FontDesc font;
struct TableWidget table; struct TableWidget table;
struct TextWidget desc;
cc_bool releasedInv, deferredSelect; cc_bool releasedInv, deferredSelect;
} InventoryScreen_Instance; } InventoryScreen_Instance;
static void InventoryScreen_MakeBlockDesc(cc_string* desc, BlockID block) {
cc_string name;
int block_ = block;
if (Game_PureClassic) { String_AppendConst(desc, "Select block"); return; }
if (block == BLOCK_AIR) return;
name = Block_UNSAFE_GetName(block);
String_AppendString(desc, &name);
if (Game_ClassicMode) return;
String_Format1(desc, " (ID %i&f", &block_);
if (!Blocks.CanPlace[block]) { String_AppendConst(desc, ", place &cNo&f"); }
if (!Blocks.CanDelete[block]) { String_AppendConst(desc, ", delete &cNo&f"); }
String_Append(desc, ')');
}
static void InventoryScreen_UpdateDesc(struct InventoryScreen* s, BlockID block) {
cc_string desc; char descBuffer[STRING_SIZE * 2];
String_InitArray(desc, descBuffer);
InventoryScreen_MakeBlockDesc(&desc, block);
TextWidget_Set(&s->desc, &desc, &s->font);
}
static void InventoryScreen_OnUpdateDesc(BlockID block) {
InventoryScreen_UpdateDesc(&InventoryScreen_Instance, block);
}
static void InventoryScreen_OnBlockChanged(void* screen) { static void InventoryScreen_OnBlockChanged(void* screen) {
struct InventoryScreen* s = (struct InventoryScreen*)screen; struct InventoryScreen* s = (struct InventoryScreen*)screen;
TableWidget_OnInventoryChanged(&s->table); TableWidget_OnInventoryChanged(&s->table);
@ -1408,6 +1439,7 @@ static void InventoryScreen_ContextLost(void* screen) {
struct InventoryScreen* s = (struct InventoryScreen*)screen; struct InventoryScreen* s = (struct InventoryScreen*)screen;
Font_Free(&s->font); Font_Free(&s->font);
Elem_Free(&s->table); Elem_Free(&s->table);
Elem_Free(&s->desc);
} }
static void InventoryScreen_ContextRecreated(void* screen) { static void InventoryScreen_ContextRecreated(void* screen) {
@ -1426,16 +1458,17 @@ static void InventoryScreen_MoveToSelected(struct InventoryScreen* s) {
s->deferredSelect = false; s->deferredSelect = false;
/* User is holding invalid block */ /* User is holding invalid block */
if (table->selectedIndex == -1) { if (table->selectedIndex == -1) {
TableWidget_MakeDescTex(table, Inventory_SelectedBlock); InventoryScreen_UpdateDesc(s, Inventory_SelectedBlock);
} }
} }
static void InventoryScreen_Init(void* screen) { static void InventoryScreen_Init(void* screen) {
struct InventoryScreen* s = (struct InventoryScreen*)screen; struct InventoryScreen* s = (struct InventoryScreen*)screen;
TextWidget_Init(&s->desc);
TableWidget_Create(&s->table); TableWidget_Create(&s->table);
s->table.font = &s->font;
s->table.blocksPerRow = Inventory.BlocksPerRow; s->table.blocksPerRow = Inventory.BlocksPerRow;
s->table.UpdateDesc = InventoryScreen_OnUpdateDesc;
TableWidget_RecreateBlocks(&s->table); TableWidget_RecreateBlocks(&s->table);
/* Can't immediately move to selected here, because cursor grabbed */ /* Can't immediately move to selected here, because cursor grabbed */
@ -1451,12 +1484,17 @@ static void InventoryScreen_Render(void* screen, double delta) {
struct InventoryScreen* s = (struct InventoryScreen*)screen; struct InventoryScreen* s = (struct InventoryScreen*)screen;
if (s->deferredSelect) InventoryScreen_MoveToSelected(s); if (s->deferredSelect) InventoryScreen_MoveToSelected(s);
Elem_Render(&s->table, delta); Elem_Render(&s->table, delta);
Elem_Render(&s->desc, delta);
} }
static void InventoryScreen_Layout(void* screen) { static void InventoryScreen_Layout(void* screen) {
struct InventoryScreen* s = (struct InventoryScreen*)screen; struct InventoryScreen* s = (struct InventoryScreen*)screen;
s->table.scale = Gui_GetInventoryScale(); s->table.scale = Gui_GetInventoryScale();
Widget_Layout(&s->table); Widget_Layout(&s->table);
Widget_SetLocation(&s->desc, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0);
s->desc.yOffset = s->table.y - s->desc.height - 3;
Widget_Layout(&s->desc);
} }
static void InventoryScreen_Free(void* screen) { static void InventoryScreen_Free(void* screen) {

View File

@ -664,27 +664,6 @@ static void TableWidget_MoveCursorToSelected(struct TableWidget* w) {
Cursor_SetPosition(x, y); Cursor_SetPosition(x, y);
} }
static void TableWidget_MakeBlockDesc(struct TableWidget* w, cc_string* desc, BlockID block) {
cc_string name;
int block_ = block;
if (Game_PureClassic) { String_AppendConst(desc, "Select block"); return; }
if (block == BLOCK_AIR) return;
name = Block_UNSAFE_GetName(block);
String_AppendString(desc, &name);
if (Game_ClassicMode) return;
String_Format1(desc, " (ID %i&f", &block_);
if (!Blocks.CanPlace[block]) { String_AppendConst(desc, ", place &cNo&f"); }
if (!Blocks.CanDelete[block]) { String_AppendConst(desc, ", delete &cNo&f"); }
String_Append(desc, ')');
}
static void TableWidget_UpdateDescTexPos(struct TableWidget* w) {
w->descTex.X = w->x + w->width / 2 - w->descTex.Width / 2;
w->descTex.Y = w->y - w->descTex.Height - 5;
}
static void TableWidget_RecreateDescTex(struct TableWidget* w) { static void TableWidget_RecreateDescTex(struct TableWidget* w) {
BlockID block; BlockID block;
if (w->selectedIndex == w->lastCreatedIndex) return; if (w->selectedIndex == w->lastCreatedIndex) return;
@ -692,21 +671,7 @@ static void TableWidget_RecreateDescTex(struct TableWidget* w) {
w->lastCreatedIndex = w->selectedIndex; w->lastCreatedIndex = w->selectedIndex;
block = w->selectedIndex == -1 ? BLOCK_AIR : w->blocks[w->selectedIndex]; block = w->selectedIndex == -1 ? BLOCK_AIR : w->blocks[w->selectedIndex];
TableWidget_MakeDescTex(w, block); w->UpdateDesc(block);
}
void TableWidget_MakeDescTex(struct TableWidget* w, BlockID block) {
cc_string desc; char descBuffer[STRING_SIZE * 2];
struct DrawTextArgs args;
Gfx_DeleteTexture(&w->descTex.ID);
String_InitArray(desc, descBuffer);
TableWidget_MakeBlockDesc(w, &desc, block);
if (!desc.length) return;
DrawTextArgs_Make(&args, &desc, w->font, true);
Drawer2D_MakeTextTexture(&w->descTex, &args);
TableWidget_UpdateDescTexPos(w);
} }
void TableWidget_RecreateBlocks(struct TableWidget* w) { void TableWidget_RecreateBlocks(struct TableWidget* w) {
@ -791,14 +756,11 @@ static void TableWidget_Render(void* widget, double delta) {
x + cellSizeX / 2, y + cellSizeY / 2); x + cellSizeX / 2, y + cellSizeY / 2);
} }
IsometricDrawer_EndBatch(); IsometricDrawer_EndBatch();
if (w->descTex.ID) { Texture_Render(&w->descTex); }
} }
static void TableWidget_Free(void* widget) { static void TableWidget_Free(void* widget) {
struct TableWidget* w = (struct TableWidget*)widget; struct TableWidget* w = (struct TableWidget*)widget;
Gfx_DeleteDynamicVb(&w->vb); Gfx_DeleteDynamicVb(&w->vb);
Gfx_DeleteTexture(&w->descTex.ID);
w->lastCreatedIndex = -1000; w->lastCreatedIndex = -1000;
} }
@ -824,7 +786,6 @@ static void TableWidget_Reposition(void* widget) {
w->width = w->cellSizeX * w->blocksPerRow; w->width = w->cellSizeX * w->blocksPerRow;
w->height = w->cellSizeY * w->rowsVisible; w->height = w->cellSizeY * w->rowsVisible;
Widget_CalcPosition(w); Widget_CalcPosition(w);
TableWidget_UpdateDescTexPos(w);
/* Does the table fit on screen? */ /* Does the table fit on screen? */
if (Game_ClassicMode || Table_Y(w) >= 0) break; if (Game_ClassicMode || Table_Y(w) >= 0) break;

View File

@ -87,7 +87,6 @@ struct TableWidget {
int blocksCount, blocksPerRow; int blocksCount, blocksPerRow;
int rowsTotal, rowsVisible; int rowsTotal, rowsVisible;
int lastCreatedIndex; int lastCreatedIndex;
struct FontDesc* font;
int selectedIndex, cellSizeX, cellSizeY; int selectedIndex, cellSizeX, cellSizeY;
float selBlockExpand; float selBlockExpand;
GfxResourceID vb; GfxResourceID vb;
@ -96,9 +95,9 @@ struct TableWidget {
BlockID blocks[BLOCK_COUNT]; BlockID blocks[BLOCK_COUNT];
struct ScrollbarWidget scroll; struct ScrollbarWidget scroll;
struct Texture descTex;
int lastX, lastY, paddingX; int lastX, lastY, paddingX;
int paddingTopY, paddingMaxY; int paddingTopY, paddingMaxY;
void (*UpdateDesc)(BlockID block);
}; };
CC_NOINLINE void TableWidget_Create(struct TableWidget* w); CC_NOINLINE void TableWidget_Create(struct TableWidget* w);
@ -107,7 +106,6 @@ CC_NOINLINE void TableWidget_Create(struct TableWidget* w);
CC_NOINLINE void TableWidget_SetBlockTo(struct TableWidget* w, BlockID block); CC_NOINLINE void TableWidget_SetBlockTo(struct TableWidget* w, BlockID block);
CC_NOINLINE void TableWidget_RecreateBlocks(struct TableWidget* w); CC_NOINLINE void TableWidget_RecreateBlocks(struct TableWidget* w);
CC_NOINLINE void TableWidget_OnInventoryChanged(struct TableWidget* w); CC_NOINLINE void TableWidget_OnInventoryChanged(struct TableWidget* w);
CC_NOINLINE void TableWidget_MakeDescTex(struct TableWidget* w, BlockID block);
CC_NOINLINE void TableWidget_Recreate(struct TableWidget* w); CC_NOINLINE void TableWidget_Recreate(struct TableWidget* w);