2013-06-05 21:03:23 +00:00
|
|
|
#include "UI/OnScreenDisplay.h"
|
|
|
|
#include "UI/ui_atlas.h"
|
|
|
|
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "base/colorutil.h"
|
2013-06-05 21:03:23 +00:00
|
|
|
#include "base/timeutil.h"
|
|
|
|
#include "gfx_es2/draw_buffer.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
|
2014-12-31 15:50:23 +00:00
|
|
|
#include "ui/ui_context.h"
|
|
|
|
|
2013-05-22 16:00:06 +00:00
|
|
|
OnScreenMessages osm;
|
|
|
|
|
2014-12-31 15:50:23 +00:00
|
|
|
void OnScreenMessagesView::Draw(UIContext &dc) {
|
2013-05-22 16:00:06 +00:00
|
|
|
// First, clean out old messages.
|
2014-12-31 15:50:23 +00:00
|
|
|
osm.Lock();
|
|
|
|
osm.Clean();
|
2013-05-22 16:00:06 +00:00
|
|
|
|
|
|
|
// Get height
|
|
|
|
float w, h;
|
2016-08-07 23:49:50 +00:00
|
|
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, "Wg", &w, &h);
|
2013-05-22 16:00:06 +00:00
|
|
|
|
|
|
|
float y = 10.0f;
|
|
|
|
// Then draw them all.
|
2014-12-31 15:50:23 +00:00
|
|
|
const std::list<OnScreenMessages::Message> &messages = osm.Messages();
|
|
|
|
for (auto iter = messages.begin(); iter != messages.end(); ++iter) {
|
2013-10-13 10:05:50 +00:00
|
|
|
float alpha = (iter->endTime - time_now_d()) * 4.0f;
|
|
|
|
if (alpha > 1.0) alpha = 1.0f;
|
|
|
|
if (alpha < 0.0) alpha = 0.0f;
|
|
|
|
// Messages that are wider than the screen are left-aligned instead of centered.
|
|
|
|
float tw, th;
|
2016-08-07 23:49:50 +00:00
|
|
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, iter->text.c_str(), &tw, &th);
|
2014-12-31 15:50:23 +00:00
|
|
|
float x = bounds_.centerX();
|
2013-10-13 10:05:50 +00:00
|
|
|
int align = ALIGN_TOP | ALIGN_HCENTER;
|
2014-12-31 15:50:23 +00:00
|
|
|
if (tw > bounds_.w) {
|
2013-10-13 10:05:50 +00:00
|
|
|
align = ALIGN_TOP | ALIGN_LEFT;
|
|
|
|
x = 2;
|
|
|
|
}
|
2015-05-07 03:14:04 +00:00
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
|
|
|
dc.DrawTextShadow(iter->text.c_str(), x, y, colorAlpha(iter->color, alpha), align);
|
2013-05-22 16:00:06 +00:00
|
|
|
y += h;
|
|
|
|
}
|
2014-12-31 15:50:23 +00:00
|
|
|
|
|
|
|
osm.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnScreenMessages::Clean() {
|
|
|
|
restart:
|
|
|
|
double now = time_now_d();
|
|
|
|
for (auto iter = messages_.begin(); iter != messages_.end(); iter++) {
|
|
|
|
if (iter->endTime < now) {
|
|
|
|
messages_.erase(iter);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 16:00:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 17:08:48 +00:00
|
|
|
void OnScreenMessages::Show(const std::string &text, float duration_s, uint32_t color, int icon, bool checkUnique, const char *id) {
|
2013-12-08 10:51:33 +00:00
|
|
|
double now = time_now_d();
|
2016-03-06 21:30:31 +00:00
|
|
|
lock_guard guard(mutex_);
|
2013-06-06 08:05:31 +00:00
|
|
|
if (checkUnique) {
|
|
|
|
for (auto iter = messages_.begin(); iter != messages_.end(); ++iter) {
|
2015-09-27 09:42:26 +00:00
|
|
|
if (iter->text == text || (id && iter->id && !strcmp(iter->id, id))) {
|
2013-12-08 10:51:33 +00:00
|
|
|
Message msg = *iter;
|
|
|
|
msg.endTime = now + duration_s;
|
2015-09-25 17:08:48 +00:00
|
|
|
msg.text = text;
|
|
|
|
msg.color = color;
|
2013-12-08 10:51:33 +00:00
|
|
|
messages_.erase(iter);
|
|
|
|
messages_.insert(messages_.begin(), msg);
|
2013-06-06 08:05:31 +00:00
|
|
|
return;
|
2013-12-08 10:51:33 +00:00
|
|
|
}
|
2013-06-06 08:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-22 16:00:06 +00:00
|
|
|
Message msg;
|
2015-09-25 17:08:48 +00:00
|
|
|
msg.text = text;
|
2013-05-22 16:00:06 +00:00
|
|
|
msg.color = color;
|
|
|
|
msg.endTime = now + duration_s;
|
|
|
|
msg.icon = icon;
|
2015-09-25 17:08:48 +00:00
|
|
|
msg.id = id;
|
2013-05-22 16:00:06 +00:00
|
|
|
messages_.insert(messages_.begin(), msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnScreenMessages::ShowOnOff(const std::string &message, bool b, float duration_s, uint32_t color, int icon) {
|
|
|
|
Show(message + (b ? ": on" : ": off"), duration_s, color, icon);
|
|
|
|
}
|