Skip to content

Commit

Permalink
libcommon|Menu: Don't push FR attribute state unnecessarily
Browse files Browse the repository at this point in the history
The ver 1.0 font renderer is not intelligent enough to defer copying
of the entire text state until an actual change occurs.
  • Loading branch information
danij-deng committed Sep 22, 2014
1 parent 8757010 commit 3a22760
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 52 deletions.
9 changes: 1 addition & 8 deletions doomsday/plugins/common/include/menu/page.h
Expand Up @@ -177,7 +177,7 @@ class Page
void tick();

/**
* Draw this menu page.
* Draw this page.
*/
void draw(float opacity = 1.f, bool showFocusCursor = true);

Expand Down Expand Up @@ -209,13 +209,6 @@ class Page
*/
fontid_t predefinedFont(mn_page_fontid_t id);

/**
* Returns the effective line height for the predefined @c MENU_FONT1.
*
* @param lineOffset If not @c 0 the line offset is written here.
*/
int lineHeight(int *lineOffset = 0);

void activate();

int handleCommand(menucommand_e cmd);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/hu_menu.cpp
Expand Up @@ -2540,7 +2540,7 @@ int Hu_MenuFallbackResponder(event_t *ev)
{
if(ev->type == EV_KEY && (ev->state == EVS_DOWN || ev->state == EVS_REPEAT))
{
foreach(Widget *wi, page->children())
for(Widget *wi : page->children())
{
if(wi->isDisabled() || wi->isHidden())
continue;
Expand Down
67 changes: 26 additions & 41 deletions doomsday/plugins/common/src/menu/page.cpp
Expand Up @@ -55,7 +55,7 @@ DENG2_PIMPL(Page)
String title; ///< Title of this page.
Page *previous = nullptr; ///< Previous page.
int focus = -1; ///< Index of the currently focused widget else @c -1
Flags flags = DefaultFlags; ///< @ref menuPageFlags
Flags flags = DefaultFlags;
int timer = 0;

fontid_t fonts[MENU_FONT_COUNT]; ///< Predefined. Used by all widgets.
Expand Down Expand Up @@ -88,26 +88,42 @@ DENG2_PIMPL(Page)

void updateAllChildGeometry()
{
// Update objects.
for(Widget *wi : children)
{
FR_PushAttrib();
wi->geometry().moveTopLeft(Vector2i(0, 0));
wi->updateGeometry();
FR_PopAttrib();
}
}

/**
* Returns the effective line height for the predefined @c MENU_FONT1.
*
* @param lineOffset If not @c 0 the line offset is written here.
*/
int lineHeight(int *lineOffset = 0)
{
fontid_t oldFont = FR_Font();

/// @kludge We cannot yet query line height from the font...
FR_SetFont(self.predefinedFont(MENU_FONT1));
int lh = FR_TextHeight("{case}WyQ");
if(lineOffset)
{
*lineOffset = de::max(1.f, .5f + lh * .34f);
}
// Restore the old font.
FR_SetFont(oldFont);

return lh;
}

void applyLayout()
{
geometry.topLeft = Vector2i(0, 0);
geometry.setSize(Vector2ui(0, 0));

// Apply layout logic to this page.

if(flags & FixedLayout)
{
// This page uses a fixed layout.
for(Widget *wi : children)
{
if(wi->isHidden()) continue;
Expand All @@ -120,7 +136,7 @@ DENG2_PIMPL(Page)

// This page uses a dynamic layout.
int lineOffset;
int lh = self.lineHeight(&lineOffset);
int const lh = lineHeight(&lineOffset);

Vector2i origin;

Expand Down Expand Up @@ -369,26 +385,6 @@ Page::Children const &Page::children() const
return d->children;
}

int Page::lineHeight(int *lineOffset)
{
LOG_AS("Page");

fontid_t oldFont = FR_Font();

/// @kludge We cannot yet query line height from the font...
FR_SetFont(predefinedFont(MENU_FONT1));
int lh = FR_TextHeight("{case}WyQ");
if(lineOffset)
{
*lineOffset = de::max(1.f, .5f + lh * .34f);
}

// Restore the old font.
FR_SetFont(oldFont);

return lh;
}

void Page::setOnActiveCallback(Page::OnActiveCallback newCallback)
{
d->onActiveCallback = newCallback;
Expand Down Expand Up @@ -434,16 +430,11 @@ static void drawNavigation(Vector2i const origin)
#endif
}

static void drawTitle(String const &title, Point2Raw const *offset = nullptr)
static void drawTitle(String const &title)
{
if(title.isEmpty()) return;

Vector2i origin(SCREENWIDTH / 2, (SCREENHEIGHT / 2) - ((SCREENHEIGHT / 2 - 5) / cfg.menuScale));
if(offset)
{
origin.x += offset->x;
origin.y += offset->y;
}

FR_PushAttrib();
Hu_MenuDrawPageTitle(title, origin); origin.y += 16;
Expand Down Expand Up @@ -484,14 +475,8 @@ void Page::draw(float alpha, bool showFocusCursor)
// render state, so configure render state before we begin.
setupRenderStateForPageDrawing(*this, alpha);

// Update object geometry. We'll push the font renderer state because
// updating geometry may require changing the current values.
FR_PushAttrib();
d->updateAllChildGeometry();

// Back to default page render state.
FR_PopAttrib();

// We can now layout the widgets of this page.
/// @todo Do not modify the page layout here.
d->applyLayout();
Expand All @@ -507,7 +492,7 @@ void Page::draw(float alpha, bool showFocusCursor)
int focusedHeight = 0;
if(focused)
{
focusedHeight = d->cursorSizeFor(focused, lineHeight());
focusedHeight = d->cursorSizeFor(focused, d->lineHeight());

// Determine the origin and dimensions of the cursor.
/// @todo Each object should define a focus origin...
Expand Down
2 changes: 2 additions & 0 deletions doomsday/plugins/common/src/menu/widgets/buttonwidget.cpp
Expand Up @@ -134,10 +134,12 @@ void ButtonWidget::updateGeometry()
}
}

FR_PushAttrib();
Size2Raw size;
FR_SetFont(page().predefinedFont(mn_page_fontid_t(font())));
FR_TextSize(&size, useText.toUtf8().constData());
geometry().setSize(Vector2ui(size.width, size.height));
FR_PopAttrib();
}

String ButtonWidget::text() const
Expand Down
Expand Up @@ -157,10 +157,11 @@ void CVarTextualSliderWidget::updateGeometry()
{
String const valueAsText = d->valueAsText();

FR_PushAttrib();
FR_SetFont(page().predefinedFont(mn_page_fontid_t(font())));
Size2Raw size; FR_TextSize(&size, valueAsText.toUtf8().constData());

geometry().setSize(Vector2ui(size.width, size.height));
FR_PopAttrib();
}

CVarTextualSliderWidget &CVarTextualSliderWidget::setEmptyText(String const &newEmptyText)
Expand Down
Expand Up @@ -91,11 +91,13 @@ int InlineListWidget::handleCommand(menucommand_e cmd)
void InlineListWidget::updateGeometry()
{
Item *item = items()[selection()];
Size2Raw size;

FR_PushAttrib();
Size2Raw size;
FR_SetFont(page().predefinedFont(mn_page_fontid_t(font())));
FR_TextSize(&size, item->text().toUtf8().constData());
geometry().setSize(Vector2ui(size.width, size.height));
FR_PopAttrib();
}

} // namespace menu
Expand Down
2 changes: 2 additions & 0 deletions doomsday/plugins/common/src/menu/widgets/labelwidget.cpp
Expand Up @@ -101,10 +101,12 @@ void LabelWidget::updateGeometry()
return;
}

FR_PushAttrib();
Size2Raw size;
FR_SetFont(page().predefinedFont(mn_page_fontid_t(font())));
FR_TextSize(&size, d->text.toUtf8().constData());
geometry().setSize(Vector2ui(size.width, size.height));
FR_PopAttrib();
}

LabelWidget &LabelWidget::setPatch(patchid_t *newPatch)
Expand Down
2 changes: 2 additions & 0 deletions doomsday/plugins/common/src/menu/widgets/listwidget.cpp
Expand Up @@ -98,6 +98,7 @@ ListWidget::Items const &ListWidget::items() const
void ListWidget::updateGeometry()
{
geometry().setSize(Vector2ui(0, 0));
FR_PushAttrib();
FR_SetFont(page().predefinedFont(mn_page_fontid_t(font())));

RectRaw itemGeometry;
Expand All @@ -115,6 +116,7 @@ void ListWidget::updateGeometry()

itemGeometry.origin.y += itemGeometry.size.height;
}
FR_PopAttrib();
}

void ListWidget::draw() const
Expand Down

0 comments on commit 3a22760

Please sign in to comment.