Skip to content

Commit 16c7008

Browse files
committedFeb 7, 2016
small drawItemStack cleanup
-> Replace the three bool params with an enum -> Add struct for the static content, leads to less repetition -> cache enable_animations setting
1 parent 6cd2b3b commit 16c7008

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed
 

Diff for: ‎src/guiFormSpecMenu.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,8 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
21962196
&& m_selected_item->listname == s.listname
21972197
&& m_selected_item->i == item_i;
21982198
bool hovering = rect.isPointInside(m_pointer);
2199+
ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
2200+
(hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
21992201

22002202
if (phase == 0) {
22012203
if (hovering) {
@@ -2238,7 +2240,7 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
22382240
{
22392241
drawItemStack(driver, m_font, item,
22402242
rect, &AbsoluteClippingRect, m_gamedef,
2241-
selected, hovering, false);
2243+
rotation_kind);
22422244
}
22432245

22442246
// Draw tooltip
@@ -2284,7 +2286,7 @@ void GUIFormSpecMenu::drawSelectedItem()
22842286
if (!m_selected_item) {
22852287
drawItemStack(driver, m_font, ItemStack(),
22862288
core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2287-
NULL, m_gamedef, false, false, true);
2289+
NULL, m_gamedef, IT_ROT_DRAGGED);
22882290
return;
22892291
}
22902292

@@ -2297,7 +2299,7 @@ void GUIFormSpecMenu::drawSelectedItem()
22972299

22982300
core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
22992301
core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
2300-
drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, false, false, true);
2302+
drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
23012303
}
23022304

23032305
void GUIFormSpecMenu::drawMenu()
@@ -2434,7 +2436,7 @@ void GUIFormSpecMenu::drawMenu()
24342436
// Viewport rectangle on screen
24352437
core::rect<s32> rect = imgrect + spec.pos;
24362438
drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
2437-
m_gamedef, false, false, false);
2439+
m_gamedef, IT_ROT_NONE);
24382440
}
24392441

24402442
/*
@@ -2452,7 +2454,7 @@ void GUIFormSpecMenu::drawMenu()
24522454
if (!item_hovered) {
24532455
drawItemStack(driver, m_font, ItemStack(),
24542456
core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
2455-
NULL, m_gamedef, false, true, false);
2457+
NULL, m_gamedef, IT_ROT_HOVERED);
24562458
}
24572459

24582460
/* TODO find way to show tooltips on touchscreen */

Diff for: ‎src/hud.cpp

+17-36
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
156156
if (!use_hotbar_image)
157157
driver->draw2DRectangle(bgcolor2, rect, NULL);
158158
drawItemStack(driver, g_fontengine->getFont(), item, rect, NULL,
159-
gamedef, selected, false, false);
159+
gamedef, selected ? IT_ROT_SELECTED : IT_ROT_NONE);
160160
}
161161

162162
//NOTE: selectitem = 0 -> no selected; selectitem 1-based
@@ -486,32 +486,26 @@ void Hud::resizeHotbar() {
486486
}
487487
}
488488

489+
struct MeshTimeInfo {
490+
s32 time;
491+
scene::IMesh *mesh;
492+
};
493+
489494
void drawItemStack(video::IVideoDriver *driver,
490495
gui::IGUIFont *font,
491496
const ItemStack &item,
492497
const core::rect<s32> &rect,
493498
const core::rect<s32> *clip,
494499
IGameDef *gamedef,
495-
bool selected,
496-
bool hovered,
497-
bool dragged)
500+
ItemRotationKind rotation_kind)
498501
{
499-
static s32 hovered_time;
500-
static s32 selected_time;
501-
static s32 dragged_time;
502-
static scene::IMesh *hovered_mesh;
503-
static scene::IMesh *selected_mesh;
504-
static scene::IMesh *dragged_mesh;
505-
bool enable_animations =
502+
static MeshTimeInfo rotation_time_infos[IT_ROT_NONE];
503+
static bool enable_animations =
506504
g_settings->getBool("inventory_items_animations");
507505

508506
if (item.empty()) {
509-
if (selected) {
510-
selected_mesh = NULL;
511-
} else if (hovered) {
512-
hovered_mesh = NULL;
513-
} else if (dragged) {
514-
dragged_mesh = NULL;
507+
if (rotation_kind < IT_ROT_NONE) {
508+
rotation_time_infos[rotation_kind].mesh = NULL;
515509
}
516510
return;
517511
}
@@ -522,26 +516,13 @@ void drawItemStack(video::IVideoDriver *driver,
522516
if (mesh) {
523517
driver->clearZBuffer();
524518
s32 delta = 0;
525-
if (selected) {
526-
if (mesh != selected_mesh) {
527-
selected_mesh = mesh;
528-
selected_time = getTimeMs();
529-
} else {
530-
delta = porting::getDeltaMs(selected_time, getTimeMs()) % 100000;
531-
}
532-
} else if (hovered) {
533-
if (mesh != hovered_mesh) {
534-
hovered_mesh = mesh;
535-
hovered_time = getTimeMs();
536-
} else {
537-
delta = porting::getDeltaMs(hovered_time, getTimeMs()) % 100000;
538-
}
539-
} else if (dragged) {
540-
if (mesh != dragged_mesh) {
541-
dragged_mesh = mesh;
542-
dragged_time = getTimeMs();
519+
if (rotation_kind < IT_ROT_NONE) {
520+
MeshTimeInfo &ti = rotation_time_infos[rotation_kind];
521+
if (mesh != ti.mesh) {
522+
ti.mesh = mesh;
523+
ti.time = getTimeMs();
543524
} else {
544-
delta = porting::getDeltaMs(dragged_time, getTimeMs()) % 100000;
525+
delta = porting::getDeltaMs(ti.time, getTimeMs()) % 100000;
545526
}
546527
}
547528
core::rect<s32> oldViewPort = driver->getViewPort();

Diff for: ‎src/hud.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,20 @@ class Hud {
147147
video::SColor hbar_colors[4];
148148
};
149149

150+
enum ItemRotationKind {
151+
IT_ROT_SELECTED,
152+
IT_ROT_HOVERED,
153+
IT_ROT_DRAGGED,
154+
IT_ROT_NONE, // Must be last, also serves as number
155+
};
156+
150157
void drawItemStack(video::IVideoDriver *driver,
151158
gui::IGUIFont *font,
152159
const ItemStack &item,
153160
const core::rect<s32> &rect,
154161
const core::rect<s32> *clip,
155162
IGameDef *gamedef,
156-
bool selected,
157-
bool hovered,
158-
bool dragged);
163+
ItemRotationKind rotation_kind);
159164

160165
#endif
161166

0 commit comments

Comments
 (0)
Please sign in to comment.