Skip to content

Commit

Permalink
libcommon|Menu: Use de::lerp() for menu font color interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Sep 22, 2014
1 parent 3a22760 commit 46196f6
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 84 deletions.
2 changes: 0 additions & 2 deletions doomsday/plugins/common/include/hu_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ typedef enum mn_page_fontid_e

#define VALID_MNPAGE_FONTID(v) ((v) >= MENU_FONT1 && (v) < MENU_FONT_COUNT)

void lerpColor(float *dst, float const *a, float const *b, float t, dd_bool rgbaMode);

typedef enum {
GUI_NONE,
GUI_BOX,
Expand Down
6 changes: 2 additions & 4 deletions doomsday/plugins/common/include/menu/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
namespace common {
namespace menu {


/// @todo refactor away.
struct mn_rendstate_t
{
float pageAlpha;
float textGlitter;
float textShadow;
float textColors[MENU_COLOR_COUNT][4];
de::Vector4f textColors[MENU_COLOR_COUNT];
fontid_t textFonts[MENU_FONT_COUNT];
};
extern mn_rendstate_t const *mnRendState;
Expand Down Expand Up @@ -193,9 +192,8 @@ class Page
* page color identifier.
*
* @param id Unique identifier of the predefined color being retrieved.
* @param rgb Found color values are written here, else set to white.
*/
void predefinedColor(mn_page_colorid_t id, float rgb[3]);
de::Vector3f predefinedColor(mn_page_colorid_t id);

void setPredefinedFont(mn_page_fontid_t id, fontid_t fontId);

Expand Down
24 changes: 0 additions & 24 deletions doomsday/plugins/common/src/hu_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,6 @@ static void errorIfNotInited(char const *callerName)
exit(1);
}

void lerpColor(float *dst, float const *a, float const *b, float t, dd_bool rgbaMode)
{
if(t <= 0)
{
dst[CR] = a[CR];
dst[CG] = a[CG];
dst[CB] = a[CB];
if(rgbaMode) dst[CA] = a[CA];
return;
}
if(t >= 1)
{
dst[CR] = b[CR];
dst[CG] = b[CG];
dst[CB] = b[CB];
if(rgbaMode) dst[CA] = b[CA];
return;
}
dst[CR] = (1 - t) * a[CR] + t * b[CR];
dst[CG] = (1 - t) * a[CG] + t * b[CG];
dst[CB] = (1 - t) * a[CB] + t * b[CB];
if(rgbaMode) dst[CA] = (1 - t) * a[CA] + t * b[CA];
}

static uiwidgetid_t nextUnusedId()
{
return uiwidgetid_t(numWidgets);
Expand Down
12 changes: 4 additions & 8 deletions doomsday/plugins/common/src/menu/page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ static void setupRenderStateForPageDrawing(Page &page, float alpha)
}
for(int i = 0; i < MENU_COLOR_COUNT; ++i)
{
page.predefinedColor(mn_page_colorid_t(i), rs.textColors[i]);
rs.textColors[i][CA] = alpha; // For convenience.
rs.textColors[i] = Vector4f(page.predefinedColor(mn_page_colorid_t(i)), alpha);
}

// Configure the font renderer (assume state has already been pushed if necessary).
Expand Down Expand Up @@ -730,14 +729,11 @@ void Page::setPredefinedFont(mn_page_fontid_t id, fontid_t fontId)
d->fonts[id] = fontId;
}

void Page::predefinedColor(mn_page_colorid_t id, float rgb[3])
Vector3f Page::predefinedColor(mn_page_colorid_t id)
{
DENG2_ASSERT(rgb != 0);
DENG2_ASSERT(VALID_MNPAGE_COLORID(id));
uint colorIndex = d->colors[id];
rgb[CR] = cfg.menuTextColors[colorIndex][CR];
rgb[CG] = cfg.menuTextColors[colorIndex][CG];
rgb[CB] = cfg.menuTextColors[colorIndex][CB];
uint const colorIndex = d->colors[id];
return Vector3f(cfg.menuTextColors[colorIndex]);
}

int Page::timer()
Expand Down
13 changes: 6 additions & 7 deletions doomsday/plugins/common/src/menu/widgets/buttonwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ ButtonWidget::~ButtonWidget()

void ButtonWidget::draw() const
{
fontid_t const fontId = mnRendState->textFonts[font()];
float textColor[4], t = (isFocused()? 1 : 0);
fontid_t const fontId = mnRendState->textFonts[font()];
Vector4f const &textColor = mnRendState->textColors[color()];
float t = (isFocused()? 1 : 0);

// Flash if focused.
if(isFocused() && cfg.menuTextFlashSpeed > 0)
Expand All @@ -62,12 +63,10 @@ void ButtonWidget::draw() const
t = (1 + sin(page().timer() / (float)TICSPERSEC * speed * DD_PI)) / 2;
}

lerpColor(textColor, mnRendState->textColors[color()], cfg.menuTextFlashColor, t, false/*rgb mode*/);
textColor[CA] = mnRendState->textColors[color()][CA];

Vector4f const color = de::lerp(textColor, Vector4f(Vector3f(cfg.menuTextFlashColor), 1), t);
FR_SetFont(fontId);
FR_SetColorAndAlphav(textColor);
DGL_Color4f(1, 1, 1, textColor[CA]);
FR_SetColorAndAlpha(color.x, color.y, color.z, color.w);
DGL_Color4f(1, 1, 1, color.w);

if(d->patch >= 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,17 @@ CVarTextualSliderWidget::~CVarTextualSliderWidget()

void CVarTextualSliderWidget::draw() const
{
Vector2i const &origin = geometry().topLeft;
String const valueAsText = d->valueAsText();
Vector2i const &origin = geometry().topLeft;
String const valueAsText = d->valueAsText();
Vector4f const &textColor = mnRendState->textColors[color()];

DGL_MatrixMode(DGL_MODELVIEW);
DGL_Translatef(origin.x, origin.y, 0);

DGL_Enable(DGL_TEXTURE_2D);

FR_SetFont(mnRendState->textFonts[font()]);
FR_SetColorAndAlphav(mnRendState->textColors[color()]);
FR_SetColorAndAlpha(textColor.x, textColor.y, textColor.z, textColor.w);
FR_DrawTextXY3(valueAsText.toUtf8().constData(), 0, 0, ALIGN_TOPLEFT, Hu_MenuMergeEffectWithDrawTextFlags(0));

DGL_Disable(DGL_TEXTURE_2D);
Expand Down
5 changes: 3 additions & 2 deletions doomsday/plugins/common/src/menu/widgets/inlinelistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ InlineListWidget::~InlineListWidget()

void InlineListWidget::draw() const
{
Item const *item = items()[selection()];
Item const *item = items()[selection()];
Vector4f const &textColor = mnRendState->textColors[color()];

DGL_Enable(DGL_TEXTURE_2D);
FR_SetFont(mnRendState->textFonts[font()]);
FR_SetColorAndAlphav(mnRendState->textColors[color()]);
FR_SetColorAndAlpha(textColor.x, textColor.y, textColor.z, textColor.w);
FR_DrawTextXY3(item->text().toUtf8().constData(), geometry().topLeft.x, geometry().topLeft.y,
ALIGN_TOPLEFT, Hu_MenuMergeEffectWithDrawTextFlags(0));

Expand Down
14 changes: 6 additions & 8 deletions doomsday/plugins/common/src/menu/widgets/labelwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "menu/widgets/labelwidget.h"

#include "hu_menu.h" // Hu_MenuMergeEffectWithDrawTextFlags
#include "hu_lib.h" // lerpColor
#include "menu/page.h" // mnRendState

using namespace de;
Expand Down Expand Up @@ -53,8 +52,9 @@ LabelWidget::~LabelWidget()

void LabelWidget::draw() const
{
fontid_t fontId = mnRendState->textFonts[font()];
float textColor[4], t = (isFocused()? 1 : 0);
fontid_t fontId = mnRendState->textFonts[font()];
Vector4f const &textColor = mnRendState->textColors[color()];
float t = (isFocused()? 1 : 0);

// Flash if focused.
if(isFocused() && cfg.menuTextFlashSpeed > 0)
Expand All @@ -63,12 +63,10 @@ void LabelWidget::draw() const
t = (1 + sin(page().timer() / (float)TICSPERSEC * speed * DD_PI)) / 2;
}

lerpColor(textColor, mnRendState->textColors[color()], cfg.menuTextFlashColor, t, false/*rgb mode*/);
textColor[CA] = mnRendState->textColors[color()][CA];

DGL_Color4f(1, 1, 1, textColor[CA]);
Vector4f const color = de::lerp(textColor, Vector4f(Vector3f(cfg.menuTextFlashColor), 1), t);
DGL_Color4f(1, 1, 1, color.w);
FR_SetFont(fontId);
FR_SetColorAndAlphav(textColor);
FR_SetColorAndAlpha(color.x, color.y, color.z, color.w);

if(d->patch)
{
Expand Down
12 changes: 5 additions & 7 deletions doomsday/plugins/common/src/menu/widgets/lineeditwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void LineEditWidget::draw() const

//if(string)
{
float textColor[4], t = 0;
float t = 0;

// Flash if focused?
if(!isActive() && isFocused() && cfg.menuTextFlashSpeed > 0)
Expand All @@ -136,14 +136,12 @@ void LineEditWidget::draw() const
t = (1 + sin(page().timer() / (float)TICSPERSEC * speed * DD_PI)) / 2;
}

lerpColor(textColor, cfg.menuTextColors[MNDATA_EDIT_TEXT_COLORIDX], cfg.menuTextFlashColor, t, false/*rgb mode*/);
textColor[CA] = textAlpha;

// Light the text.
textColor[CR] *= light; textColor[CG] *= light; textColor[CB] *= light;
Vector4f color = de::lerp(Vector3f(cfg.menuTextColors[MNDATA_EDIT_TEXT_COLORIDX]), Vector3f(cfg.menuTextFlashColor), t);
color *= light;
color.w = textAlpha;

// Draw the text:
FR_SetColorAndAlphav(textColor);
FR_SetColorAndAlpha(color.x, color.y, color.z, color.w);
FR_DrawTextXY3(useText.toUtf8().constData(), origin.x, origin.y, ALIGN_TOPLEFT, Hu_MenuMergeEffectWithDrawTextFlags(0));

// Are we drawing a cursor?
Expand Down
27 changes: 8 additions & 19 deletions doomsday/plugins/common/src/menu/widgets/listwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "common.h"
#include "menu/widgets/listwidget.h"

#include "hu_lib.h" // lerpColor
#include "hu_menu.h" // menu sounds
#include "menu/page.h"

Expand Down Expand Up @@ -122,22 +121,19 @@ void ListWidget::updateGeometry()
void ListWidget::draw() const
{
bool const flashSelection = (isActive() && selectionIsVisible());
float const *textColor = mnRendState->textColors[color()];
float dimColor[4], flashColor[4], t = flashSelection? 1 : 0;
Vector4f const &textColor = mnRendState->textColors[color()];
float t = flashSelection? 1 : 0;

if(flashSelection && cfg.menuTextFlashSpeed > 0)
{
float const speed = cfg.menuTextFlashSpeed / 2.f;
t = (1 + sin(page().timer() / (float)TICSPERSEC * speed * DD_PI)) / 2;
}

lerpColor(flashColor, mnRendState->textColors[color()], cfg.menuTextFlashColor, t, false/*rgb mode*/);
flashColor[CA] = textColor[CA];
Vector4f const flashColor = de::lerp(textColor, Vector4f(Vector3f(cfg.menuTextFlashColor), 1), t);

std::memcpy(dimColor, textColor, sizeof(dimColor));
dimColor[CR] *= MNDATA_LIST_NONSELECTION_LIGHT;
dimColor[CG] *= MNDATA_LIST_NONSELECTION_LIGHT;
dimColor[CB] *= MNDATA_LIST_NONSELECTION_LIGHT;
Vector4f dimColor = textColor * MNDATA_LIST_NONSELECTION_LIGHT;
dimColor.w = textColor.w;

if(d->first < d->items.count() && d->numvis > 0)
{
Expand All @@ -148,17 +144,10 @@ void ListWidget::draw() const
int itemIdx = d->first;
do
{
Item const *item = d->items[itemIdx];

if(d->selection == itemIdx)
{
FR_SetColorAndAlphav(flashSelection? flashColor : textColor);
}
else
{
FR_SetColorAndAlphav(dimColor);
}
Item const *item = d->items[itemIdx];
Vector4f const &color = d->selection == itemIdx? (flashSelection? flashColor : textColor) : dimColor;

FR_SetColorAndAlpha(color.x, color.y, color.z, color.w);
FR_DrawTextXY3(item->text().toUtf8().constData(), origin.x, origin.y, ALIGN_TOPLEFT, Hu_MenuMergeEffectWithDrawTextFlags(0));
origin.y += FR_TextHeight(item->text().toUtf8().constData()) * (1 + MNDATA_LIST_LEADING);
} while(++itemIdx < d->items.count() && itemIdx < d->first + d->numvis);
Expand Down

0 comments on commit 46196f6

Please sign in to comment.