Welcome to mirror list, hosted at ThFree Co, Russian Federation.

information_display.cpp « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 967106e3f6649eac8abd0cf2b997bf6c8557cf29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "information_display.hpp"
#include "drawer_yg.hpp"

#include "../indexer/mercator.hpp"
#include "../yg/defines.hpp"
#include "../yg/skin.hpp"
#include "../yg/pen_info.hpp"
#include "../yg/straight_text_element.hpp"

#include "../version/version.hpp"

#include "../base/string_utils.hpp"
#include "../base/logging.hpp"
#include "../base/mutex.hpp"
#include "../base/macros.hpp"

#include "../geometry/transformations.hpp"

#include "../std/fstream.hpp"
#include "../std/iomanip.hpp"

InformationDisplay::InformationDisplay()
  : m_ruler(Ruler::Params())
{
  enableDebugPoints(false);
  enableRuler(false);
  enableCenter(false);
  enableDebugInfo(false);
  enableMemoryWarning(false);
  enableBenchmarkInfo(false);
  enableEmptyModelMessage(false);

  for (int i = 0; i < sizeof(m_DebugPts) / sizeof(m2::PointD); ++i)
    m_DebugPts[i] = m2::PointD(0, 0);

  m_fontDesc = yg::FontDesc(12);
  m_emptyMessageFont = yg::FontDesc(14);
}

void InformationDisplay::setScreen(ScreenBase const & screen)
{
  m_screen = screen;
  m_ruler.setScreen(screen);
}

void InformationDisplay::setBottomShift(double bottomShift)
{
  m_bottomShift = bottomShift;
}

void InformationDisplay::setDisplayRect(m2::RectI const & rect)
{
  m_displayRect = rect;
}

void InformationDisplay::enableDebugPoints(bool doEnable)
{
  m_isDebugPointsEnabled = doEnable;
}

void InformationDisplay::setDebugPoint(int pos, m2::PointD const & pt)
{
  m_DebugPts[pos] = pt;
}

void InformationDisplay::drawDebugPoints(DrawerYG * pDrawer)
{
  for (int i = 0; i < sizeof(m_DebugPts) / sizeof(m2::PointD); ++i)
    if (m_DebugPts[i] != m2::PointD(0, 0))
    {
    pDrawer->screen()->drawArc(m_DebugPts[i], 0, math::pi * 2, 30, yg::Color(0, 0, 255, 32), yg::maxDepth);
    pDrawer->screen()->fillSector(m_DebugPts[i], 0, math::pi * 2, 30, yg::Color(0, 0, 255, 32), yg::maxDepth);
  }
}

void InformationDisplay::enableRuler(bool doEnable)
{
  m_isRulerEnabled = doEnable;
}

void InformationDisplay::setRulerParams(unsigned pxMinWidth, double metresMinWidth, double metresMaxWidth)
{
  m_ruler.setMinPxWidth(pxMinWidth);
  m_ruler.setMinUnitsWidth(metresMinWidth);
  m_ruler.setMaxUnitsWidth(metresMaxWidth);
}

void InformationDisplay::drawRuler(DrawerYG * pDrawer)
{
  m_ruler.setFontDesc(m_fontDesc);
  m_ruler.setVisualScale(m_visualScale);

#ifdef OMIM_OS_IPHONE
  m2::PointD pivot(m2::PointD(m_displayRect.maxX(), m_displayRect.maxY() - m_bottomShift * m_visualScale)
                 + m2::PointD(-10 * m_visualScale, -10 * m_visualScale));
  m_ruler.setPosition(yg::EPosAboveLeft);
#else
  m2::PointD pivot(m2::PointD(m_displayRect.minX(), m_displayRect.maxY() - m_bottomShift * m_visualScale)
                 + m2::PointD(10 * m_visualScale, -10 * m_visualScale));

  m_ruler.setPosition(yg::EPosAboveRight);
#endif
  m_ruler.setPivot(pivot);
  m_ruler.update();

  m_ruler.draw(pDrawer->screen().get(), math::Identity<double, 3>());
}

void InformationDisplay::setVisualScale(double visualScale)
{
  m_visualScale = visualScale;

  m_fontDesc.m_size = static_cast<uint32_t>(12 * visualScale);
  m_emptyMessageFont.m_size = static_cast<uint32_t>(14 * visualScale);
}

void InformationDisplay::enableCenter(bool doEnable)
{
  m_isCenterEnabled = doEnable;
}

void InformationDisplay::setCenter(m2::PointD const & pt)
{
  m_centerPtLonLat = pt;
}

void InformationDisplay::drawCenter(DrawerYG * drawer)
{
  ostringstream out;

  out << "(" << fixed << setprecision(4) << m_centerPtLonLat.y << ", "
             << fixed << setprecision(4) << setw(8) << m_centerPtLonLat.x << ")";

  yg::StraightTextElement::Params params;

  params.m_depth = yg::maxDepth;
  params.m_fontDesc = m_fontDesc;
  params.m_log2vis = false;

#ifdef OMIM_OS_IPHONE
  params.m_pivot = m2::PointD(m_displayRect.maxX() - 10 * m_visualScale,
                              m_displayRect.maxY() - 20 * m_visualScale - 5);
#else
  params.m_pivot = m2::PointD(m_displayRect.maxX() - 10 * m_visualScale,
                              m_displayRect.maxY() - (/*m_bottomShift*/ + 14) * m_visualScale - 5);
#endif

  params.m_position = yg::EPosAboveLeft;
  params.m_glyphCache = drawer->screen()->glyphCache();
  params.m_logText = strings::MakeUniString(out.str());

  yg::StraightTextElement ste(params);

  m2::RectD bgRect = m2::Inflate(ste.roughBoundRect(), 5.0, 5.0);

  drawer->screen()->drawRectangle(
        bgRect,
        yg::Color(187, 187, 187, 128),
        yg::maxDepth - 1);

  ste.draw(drawer->screen().get(), math::Identity<double, 3>());
}

void InformationDisplay::enableDebugInfo(bool doEnable)
{
  m_isDebugInfoEnabled = doEnable;
}

void InformationDisplay::setDebugInfo(double frameDuration, int currentScale)
{
  m_frameDuration = frameDuration;
  m_currentScale = currentScale;
}

void InformationDisplay::drawDebugInfo(DrawerYG * drawer)
{
  ostringstream out;
  out << "SPF: " << m_frameDuration;
  if (m_frameDuration == 0.0)
    out << " FPS: inf";
  else
    out << " FPS: " << 1.0 / m_frameDuration;

  m_yOffset += 20;

  m2::PointD pos = m2::PointD(m_displayRect.minX() + 10, m_displayRect.minY() + m_yOffset);

  drawer->screen()->drawText(m_fontDesc,
                             pos,
                             yg::EPosAboveRight,
                             out.str().c_str(),
                             yg::maxDepth,
                             false);
}

void InformationDisplay::enableMemoryWarning(bool flag)
{
  m_isMemoryWarningEnabled = flag;
}

void InformationDisplay::memoryWarning()
{
  enableMemoryWarning(true);
  m_lastMemoryWarning = my::Timer();
}

void InformationDisplay::drawMemoryWarning(DrawerYG * drawer)
{
  m_yOffset += 20;
  m2::PointD pos(m_displayRect.minX() + 10, m_displayRect.minY() + m_yOffset);

  ostringstream out;
  out << "MemoryWarning : " << m_lastMemoryWarning.ElapsedSeconds() << " sec. ago.";

  drawer->screen()->drawText(m_fontDesc,
                             pos,
                             yg::EPosAboveRight,
                             out.str().c_str(),
                             yg::maxDepth,
                             false);

  if (m_lastMemoryWarning.ElapsedSeconds() > 5)
    enableMemoryWarning(false);
}

bool InformationDisplay::s_isLogEnabled = false;
list<string> InformationDisplay::s_log;
size_t InformationDisplay::s_logSize = 10;
my::LogMessageFn InformationDisplay::s_oldLogFn = 0;
threads::Mutex s_logMutex;
WindowHandle * InformationDisplay::s_windowHandle = 0;
size_t s_msgNum = 0;

void InformationDisplay::logMessage(my::LogLevel level, my::SrcPoint const & /*srcPoint*/,
                                    string const & msg)
{
  {
    threads::MutexGuard guard(s_logMutex);
    ostringstream out;
    char const * names[] = { "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL" };
    out << s_msgNum++ << ":";
    if (level >= 0 && level <= static_cast<int>(ARRAY_SIZE(names)))
      out << names[level];
    else
      out << level;
    out << msg << endl;
    s_log.push_back(out.str());
    while (s_log.size() > s_logSize)
      s_log.pop_front();
  }

  /// call redisplay
  s_windowHandle->invalidate();
}

void InformationDisplay::enableLog(bool doEnable, WindowHandle * windowHandle)
{
  if (doEnable == s_isLogEnabled)
    return;
  s_isLogEnabled = doEnable;
  if (s_isLogEnabled)
  {
    s_oldLogFn = my::LogMessage;
    my::LogMessage = logMessage;
    s_windowHandle = windowHandle;
  }
  else
  {
    my::LogMessage = s_oldLogFn;
    s_windowHandle = 0;
  }
}

void InformationDisplay::drawLog(DrawerYG * drawer)
{
  threads::MutexGuard guard(s_logMutex);

  for (list<string>::const_iterator it = s_log.begin(); it != s_log.end(); ++it)
  {
    m_yOffset += 20;

    m2::PointD startPt(m_displayRect.minX() + 10, m_displayRect.minY() + m_yOffset);

    yg::StraightTextElement::Params params;
    params.m_depth = yg::maxDepth;
    params.m_fontDesc = m_fontDesc;
    params.m_log2vis = false;
    params.m_pivot = startPt;
    params.m_position = yg::EPosAboveRight;
    params.m_glyphCache = drawer->screen()->glyphCache();
    params.m_logText = strings::MakeUniString(*it);

    yg::StraightTextElement ste(params);

    drawer->screen()->drawRectangle(
        m2::Inflate(ste.roughBoundRect(), m2::PointD(2, 2)),
        yg::Color(0, 0, 0, 128),
        yg::maxDepth - 1
        );

    ste.draw(drawer->screen().get(), math::Identity<double, 3>());
  }
}

void InformationDisplay::enableEmptyModelMessage(bool doEnable)
{
  m_isEmptyModelMessageEnabled = doEnable;
}

#ifdef OMIM_OS_IPHONE
void InformationDisplay::drawEmptyModelMessage(DrawerYG * pDrawer)
{
  m2::PointD pt = m_screen.PixelRect().Center() - m2::PointD(0, m_bottomShift * m_visualScale);

  char const s [] = "Nothing found. Have you tried\n"\
                    "downloading maps of the countries?\n"\
                    "Just click the button at the bottom\n"\
                    "right corner to download the maps.";

//  yg::FontDesc bigFont(false, 30 * m_visualScale);

  yg::StraightTextElement::Params params;
  params.m_depth = yg::maxDepth;
  params.m_fontDesc = m_emptyMessageFont;
  params.m_log2vis = false;
  params.m_pivot = pt;
  params.m_position = yg::EPosCenter;
  params.m_glyphCache = pDrawer->screen()->glyphCache();
  params.m_logText = strings::MakeUniString(s);
  params.m_doSplit = true;
  params.m_delimiters = "\n";

  yg::StraightTextElement ste(params);

  ste.draw(pDrawer->screen().get(), math::Identity<double, 3>());
}
#endif

void InformationDisplay::enableBenchmarkInfo(bool doEnable)
{
  m_isBenchmarkInfoEnabled = doEnable;
}

bool InformationDisplay::addBenchmarkInfo(string const & name, m2::RectD const & globalRect, double frameDuration)
{
  BenchmarkInfo info;
  info.m_name = name;
  info.m_duration = frameDuration;
  info.m_rect = globalRect;
  m_benchmarkInfo.push_back(info);

/*  string deviceID = GetPlatform().DeviceID();
  transform(deviceID.begin(), deviceID.end(), deviceID.begin(), ::tolower);

  ofstream fout(GetPlatform().WritablePathForFile(deviceID + "_benchmark_results.txt").c_str(), ios::app);
  fout << GetPlatform().DeviceID() << " "
       << VERSION_STRING << " "
       << info.m_name << " "
       << info.m_rect.minX() << " "
       << info.m_rect.minY() << " "
       << info.m_rect.maxX() << " "
       << info.m_rect.maxY() << " "
       << info.m_duration << endl;
*/
  return true;
}

void InformationDisplay::drawBenchmarkInfo(DrawerYG * pDrawer)
{
  m_yOffset += 20;
  m2::PointD pos(m_displayRect.minX() + 10, m_displayRect.minY() + m_yOffset);
  pDrawer->screen()->drawText(m_fontDesc,
                              pos,
                              yg::EPosAboveRight,
                              "benchmark info :",
                              yg::maxDepth,
                              false);

  for (unsigned i = max(0, (int)m_benchmarkInfo.size() - 5); i < m_benchmarkInfo.size(); ++i)
  {
    ostringstream out;
    m2::RectD const & r = m_benchmarkInfo[i].m_rect;
    out << "  " << m_benchmarkInfo[i].m_name
                << ", " << "rect: (" << r.minX()
                << ", " << r.minY()
                << ", " << r.maxX()
                << ", " << r.maxY()
                << "), duration : " << m_benchmarkInfo[i].m_duration;
    m_yOffset += 20;
    pos.y += 20;
    pDrawer->screen()->drawText(m_fontDesc,
                                pos,
                                yg::EPosAboveRight,
                                out.str().c_str(),
                                yg::maxDepth,
                                false
                                );
  }

}

void InformationDisplay::doDraw(DrawerYG *drawer)
{
  m_yOffset = 0;
  if (m_isDebugPointsEnabled)
    drawDebugPoints(drawer);
  if (m_isRulerEnabled)
    drawRuler(drawer);
  if (m_isCenterEnabled)
    drawCenter(drawer);
  if (m_isDebugInfoEnabled)
    drawDebugInfo(drawer);
  if (m_isMemoryWarningEnabled)
    drawMemoryWarning(drawer);
  if (m_isBenchmarkInfoEnabled)
    drawBenchmarkInfo(drawer);
  if (s_isLogEnabled)
    drawLog(drawer);
#ifdef OMIM_OS_IPHONE
  if (m_isEmptyModelMessageEnabled)
    drawEmptyModelMessage(drawer);
#endif
}