forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeterHistogram.cpp
580 lines (506 loc) · 16.5 KB
/
MeterHistogram.cpp
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* Copyright (C) 2001 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "MeterHistogram.h"
#include "Measure.h"
#include "Rainmeter.h"
#include "../Common/Gfx/Canvas.h"
#include "../Common/Gfx/Shapes/Rectangle.h"
GeneralImageHelper_DefineOptionArray(MeterHistogram::c_PrimaryOptionArray, L"Primary");
GeneralImageHelper_DefineOptionArray(MeterHistogram::c_SecondaryOptionArray, L"Secondary");
GeneralImageHelper_DefineOptionArray(MeterHistogram::c_BothOptionArray, L"Both");
MeterHistogram::MeterHistogram(Skin* skin, const WCHAR* name) : Meter(skin, name),
m_PrimaryColor(D2D1::ColorF(D2D1::ColorF::Green)),
m_SecondaryColor(D2D1::ColorF(D2D1::ColorF::Red)),
m_OverlapColor(D2D1::ColorF(D2D1::ColorF::Yellow)),
m_MeterPos(0),
m_Autoscale(false),
m_Flip(false),
m_PrimaryImage(L"PrimaryImage", c_PrimaryOptionArray, false, skin),
m_SecondaryImage(L"SecondaryImage", c_SecondaryOptionArray, false, skin),
m_OverlapImage(L"BothImage", c_BothOptionArray, false, skin),
m_PrimaryValues(nullptr),
m_SecondaryValues(nullptr),
m_MaxPrimaryValue(1.0),
m_MinPrimaryValue(0.0),
m_MaxSecondaryValue(1.0),
m_MinSecondaryValue(0.0),
m_SizeChanged(true),
m_GraphStartLeft(false),
m_GraphHorizontalOrientation(false)
{
}
MeterHistogram::~MeterHistogram()
{
DisposeBuffer();
}
/*
** Disposes the buffers.
**
*/
void MeterHistogram::DisposeBuffer()
{
// Reset current position
m_MeterPos = 0;
// Delete buffers
delete [] m_PrimaryValues;
m_PrimaryValues = nullptr;
delete [] m_SecondaryValues;
m_SecondaryValues = nullptr;
}
/*
** Creates the buffers.
**
*/
void MeterHistogram::CreateBuffer()
{
DisposeBuffer();
// Create buffers for values
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (maxSize > 0)
{
m_PrimaryValues = new double[maxSize]();
if (m_Measures.size() >= 2)
{
m_SecondaryValues = new double[maxSize]();
}
}
}
/*
** Load the images and calculate the dimensions of the meter from them.
** Or create the brushes if solid color histogram is used.
**
*/
void MeterHistogram::Initialize()
{
Meter::Initialize();
Measure* secondaryMeasure = (m_Measures.size() >= 2) ? m_Measures[1] : nullptr;
// A sanity check
if (secondaryMeasure && !m_PrimaryImageName.empty() && (m_OverlapImageName.empty() || m_SecondaryImageName.empty()))
{
LogWarningF(this, L"Histogram: SecondaryImage and BothImage not defined");
m_PrimaryImage.DisposeImage();
m_SecondaryImage.DisposeImage();
m_OverlapImage.DisposeImage();
}
else
{
// Load the bitmaps if defined
if (!m_PrimaryImageName.empty())
{
m_PrimaryImage.LoadImage(m_PrimaryImageName);
if (m_PrimaryImage.IsLoaded())
{
int oldSize = m_GraphHorizontalOrientation ? m_H : m_W;
Gfx::D2DBitmap* bitmap = m_PrimaryImage.GetImage();
m_W = bitmap->GetWidth();
m_H = bitmap->GetHeight();
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (oldSize != maxSize)
{
m_SizeChanged = true;
}
m_W += GetWidthPadding();
m_H += GetHeightPadding();
}
}
else if (m_PrimaryImage.IsLoaded())
{
m_PrimaryImage.DisposeImage();
}
if (!m_SecondaryImageName.empty())
{
m_SecondaryImage.LoadImage(m_SecondaryImageName);
}
else if (m_SecondaryImage.IsLoaded())
{
m_SecondaryImage.DisposeImage();
}
if (!m_OverlapImageName.empty())
{
m_OverlapImage.LoadImage(m_OverlapImageName);
}
else if (m_OverlapImage.IsLoaded())
{
m_OverlapImage.DisposeImage();
}
}
if ((!m_PrimaryImageName.empty() && !m_PrimaryImage.IsLoaded()) ||
(!m_SecondaryImageName.empty() && !m_SecondaryImage.IsLoaded()) ||
(!m_OverlapImageName.empty() && !m_OverlapImage.IsLoaded()))
{
DisposeBuffer();
m_SizeChanged = false;
}
else if (m_SizeChanged)
{
CreateBuffer();
m_SizeChanged = false;
}
}
/*
** Read the options specified in the ini file.
**
*/
void MeterHistogram::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
// Store the current values so we know if the image needs to be updated
int oldW = m_W;
int oldH = m_H;
bool oldGraphHorizontalOrientation = m_GraphHorizontalOrientation;
Meter::ReadOptions(parser, section);
m_PrimaryColor = parser.ReadColor(section, L"PrimaryColor", D2D1::ColorF(D2D1::ColorF::Green));
m_SecondaryColor = parser.ReadColor(section, L"SecondaryColor", D2D1::ColorF(D2D1::ColorF::Red));
m_OverlapColor = parser.ReadColor(section, L"BothColor", D2D1::ColorF(D2D1::ColorF::Yellow));
m_PrimaryImageName = parser.ReadString(section, L"PrimaryImage", L"");
if (!m_PrimaryImageName.empty())
{
// Read tinting options
m_PrimaryImage.ReadOptions(parser, section);
}
m_SecondaryImageName = parser.ReadString(section, L"SecondaryImage", L"");
if (!m_SecondaryImageName.empty())
{
// Read tinting options
m_SecondaryImage.ReadOptions(parser, section);
}
m_OverlapImageName = parser.ReadString(section, L"BothImage", L"");
if (!m_OverlapImageName.empty())
{
// Read tinting options
m_OverlapImage.ReadOptions(parser, section);
}
m_Autoscale = parser.ReadBool(section, L"AutoScale", false);
m_Flip = parser.ReadBool(section, L"Flip", false);
const WCHAR* graph = parser.ReadString(section, L"GraphStart", L"RIGHT").c_str();
if (_wcsicmp(graph, L"RIGHT") == 0)
{
m_GraphStartLeft = false;
}
else if (_wcsicmp(graph, L"LEFT") == 0)
{
m_GraphStartLeft = true;
}
else
{
LogErrorF(this, L"GraphStart=%s is not valid", graph);
}
graph = parser.ReadString(section, L"GraphOrientation", L"VERTICAL").c_str();
if (_wcsicmp(graph, L"VERTICAL") == 0)
{
m_GraphHorizontalOrientation = false;
}
else if (_wcsicmp(graph, L"HORIZONTAL") == 0)
{
m_GraphHorizontalOrientation = true;
}
else
{
LogErrorF(this, L"GraphOrientation=%s is not valid", graph);
}
if (m_Initialized)
{
if (m_PrimaryImageName.empty())
{
int oldSize = oldGraphHorizontalOrientation ? oldH : oldW;
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (oldSize != maxSize || oldGraphHorizontalOrientation != m_GraphHorizontalOrientation)
{
m_SizeChanged = true;
Initialize(); // Reload the image
}
}
else
{
// Reset to old dimensions
m_W = oldW;
m_H = oldH;
m_SizeChanged = (oldGraphHorizontalOrientation != m_GraphHorizontalOrientation);
Initialize(); // Reload the image
if (m_SizeChanged)
{
CreateBuffer();
}
}
}
}
/*
** Updates the value(s) from the measures.
**
*/
bool MeterHistogram::Update()
{
if (Meter::Update() && !m_Measures.empty())
{
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (m_PrimaryValues && maxSize > 0) // m_PrimaryValues must not be nullptr
{
Measure* measure = m_Measures[0];
Measure* secondaryMeasure = (m_Measures.size() >= 2) ? m_Measures[1] : nullptr;
// Gather values
m_PrimaryValues[m_MeterPos] = measure->GetValue();
if (secondaryMeasure && m_SecondaryValues)
{
m_SecondaryValues[m_MeterPos] = secondaryMeasure->GetValue();
}
++m_MeterPos;
m_MeterPos %= maxSize;
m_MaxPrimaryValue = measure->GetMaxValue();
m_MinPrimaryValue = measure->GetMinValue();
m_MaxSecondaryValue = 0.0;
m_MinSecondaryValue = 0.0;
if (secondaryMeasure)
{
m_MaxSecondaryValue = secondaryMeasure->GetMaxValue();
m_MinSecondaryValue = secondaryMeasure->GetMinValue();
}
if (m_Autoscale)
{
// Go through all values and find the max
double newValue = 0.0;
for (int i = 0; i < maxSize; ++i)
{
newValue = max(newValue, m_PrimaryValues[i]);
}
// Scale the value up to nearest power of 2
if (newValue > DBL_MAX / 2.0)
{
m_MaxPrimaryValue = DBL_MAX;
}
else
{
m_MaxPrimaryValue = 2.0;
while (m_MaxPrimaryValue < newValue)
{
m_MaxPrimaryValue *= 2.0;
}
}
if (secondaryMeasure && m_SecondaryValues)
{
for (int i = 0; i < maxSize; ++i)
{
newValue = max(newValue, m_SecondaryValues[i]);
}
// Scale the value up to nearest power of 2
if (newValue > DBL_MAX / 2.0)
{
m_MaxSecondaryValue = DBL_MAX;
}
else
{
m_MaxSecondaryValue = 2.0;
while (m_MaxSecondaryValue < newValue)
{
m_MaxSecondaryValue *= 2.0;
}
}
}
}
}
return true;
}
return false;
}
/*
** Draws the meter on the double buffer
**
*/
bool MeterHistogram::Draw(Gfx::Canvas& canvas)
{
if (!Meter::Draw(canvas) ||
(m_Measures.size() >= 1 && !m_PrimaryValues) ||
(m_Measures.size() >= 2 && !m_SecondaryValues)) return false;
Measure* secondaryMeasure = (m_Measures.size() >= 2) ? m_Measures[1] : nullptr;
Gfx::D2DBitmap* primaryBitmap = m_PrimaryImage.GetImage();
Gfx::D2DBitmap* secondaryBitmap = m_SecondaryImage.GetImage();
Gfx::D2DBitmap* bothBitmap = m_OverlapImage.GetImage();
D2D1_RECT_F meterRect = GetMeterRectPadding();
int displayW = (int)(meterRect.right - meterRect.left);
int displayH = (int)(meterRect.bottom - meterRect.top);
// Default values (GraphStart=Right, GraphOrientation=Vertical)
int i = 0;
int startValue = 0;
int* endValueLHS = &i;
int* endValueRHS = &displayW;
int step = 1;
int endValue = -1; //(should be 0, but need to simulate <=)
// GraphStart=Left, GraphOrientation=Vertical
if (!m_GraphHorizontalOrientation)
{
if (m_GraphStartLeft)
{
startValue = displayW - 1;
endValueLHS = &endValue;
endValueRHS = &i;
step = -1;
}
}
else
{
if (!m_Flip)
{
endValueRHS = &displayH;
}
else
{
startValue = displayH - 1;
endValueLHS = &endValue;
endValueRHS = &i;
step = -1;
}
}
auto draw = [&](Gfx::D2DBitmap* bitmap, const D2D1_COLOR_F& color, const D2D1_RECT_F& dst) -> void
{
if (!bitmap)
{
canvas.FillRectangle(dst, color);
return;
}
const D2D1_RECT_F src = [&]() -> D2D1_RECT_F
{
return D2D1::RectF(
dst.left - meterRect.left,
dst.top - meterRect.top,
dst.right - meterRect.left,
dst.bottom - meterRect.top);
} ();
canvas.DrawBitmap(bitmap, dst, src);
};
// Horizontal or Vertical graph
if (m_GraphHorizontalOrientation)
{
for (i = startValue; *endValueLHS < *endValueRHS; i += step)
{
const FLOAT startStep = (FLOAT)(startValue + (step * i));
double range = m_MaxPrimaryValue - m_MinPrimaryValue;
double value = (range < 0.0) ? 0.0 : (range == 0.0) ? 1.0 :
(m_PrimaryValues[(i + m_MeterPos) % displayH] - m_MinPrimaryValue) / range;
int primaryBarHeight = (int)(displayW * value);
primaryBarHeight = min(displayW, primaryBarHeight);
primaryBarHeight = max(0, primaryBarHeight);
const FLOAT primaryBarHeightF = (FLOAT)primaryBarHeight;
if (secondaryMeasure)
{
range = m_MaxSecondaryValue - m_MinSecondaryValue;
value = (range < 0.0) ? 0.0 : (range == 0.0) ? 1.0 :
(m_SecondaryValues[(i + m_MeterPos) % displayH] - m_MinSecondaryValue) / range;
int secondaryBarHeight = (int)(displayW * value);
secondaryBarHeight = min(displayW, secondaryBarHeight);
secondaryBarHeight = max(0, secondaryBarHeight);
// Check which measured value is higher
const FLOAT bothBarHeight = (FLOAT)min(primaryBarHeight, secondaryBarHeight);
const FLOAT secondaryBarHeightF = (FLOAT)secondaryBarHeight;
// Draw image/color rectangle for both lines
{
const D2D1_RECT_F& dst = m_GraphStartLeft ?
Gfx::Util::ToRectF(meterRect.left, meterRect.top + startStep, bothBarHeight, 1.0f) :
Gfx::Util::ToRectF(meterRect.right - bothBarHeight, meterRect.top + startStep, bothBarHeight, 1.0f);
draw(bothBitmap, m_OverlapColor, dst);
}
// Draw image/color rectangle for the rest
if (secondaryBarHeight > primaryBarHeight)
{
const D2D1_RECT_F& dst = m_GraphStartLeft ?
Gfx::Util::ToRectF(meterRect.left + bothBarHeight, meterRect.top + startStep, secondaryBarHeightF - bothBarHeight, 1.0f) :
Gfx::Util::ToRectF(meterRect.right - secondaryBarHeightF, meterRect.top + startStep, secondaryBarHeightF - bothBarHeight, 1.0f);
draw(secondaryBitmap, m_SecondaryColor, dst);
}
else
{
const D2D1_RECT_F& dst = m_GraphStartLeft ?
Gfx::Util::ToRectF(meterRect.left + bothBarHeight, meterRect.top + startStep, primaryBarHeightF - bothBarHeight, 1.0f) :
Gfx::Util::ToRectF(meterRect.right - primaryBarHeightF, meterRect.top + startStep, primaryBarHeightF - bothBarHeight, 1.0f);
draw(primaryBitmap, m_PrimaryColor, dst);
}
}
else
{
const D2D1_RECT_F& dst = m_GraphStartLeft ?
Gfx::Util::ToRectF(meterRect.left, meterRect.top + startStep, primaryBarHeightF, 1.0f) :
Gfx::Util::ToRectF(meterRect.right - primaryBarHeightF, meterRect.top + startStep, primaryBarHeightF, 1.0f);
draw(primaryBitmap, m_PrimaryColor, dst);
}
}
}
else // GraphOrientation=Vertical
{
for (i = startValue; *endValueLHS < *endValueRHS; i += step)
{
const FLOAT startStep = (FLOAT)(startValue + (step * i));
double range = m_MaxPrimaryValue - m_MinPrimaryValue;
double value = (range < 0.0) ? 0.0 : (range == 0.0) ? 1.0 :
(m_PrimaryValues[((i + m_MeterPos) % displayW)] - m_MinPrimaryValue) / range;
int primaryBarHeight = (int)(displayH * value);
primaryBarHeight = min(displayH, primaryBarHeight);
primaryBarHeight = max(0, primaryBarHeight);
const FLOAT primaryBarHeightF = (FLOAT)primaryBarHeight;
if (secondaryMeasure)
{
range = m_MaxSecondaryValue - m_MinSecondaryValue;
value = (range < 0.0) ? 0.0 : (range == 0.0) ? 1.0 :
(m_SecondaryValues[(i + m_MeterPos) % displayW] - m_MinSecondaryValue) / range;
int secondaryBarHeight = (int)(displayH * value);
secondaryBarHeight = min(displayH, secondaryBarHeight);
secondaryBarHeight = max(0, secondaryBarHeight);
// Check which measured value is higher
const FLOAT bothBarHeight = (FLOAT)min(primaryBarHeight, secondaryBarHeight);
const FLOAT secondaryBarHeightF = (FLOAT)secondaryBarHeight;
// Draw image/color rectangle for both lines
{
const D2D1_RECT_F& dst = m_Flip ?
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.top, 1.0f, bothBarHeight) :
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.bottom - bothBarHeight, 1.0f, bothBarHeight);
draw(bothBitmap, m_OverlapColor, dst);
}
// Draw image/color rectangle for the rest
if (secondaryBarHeight > primaryBarHeight)
{
const D2D1_RECT_F& dst = m_Flip ?
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.top + bothBarHeight, 1.0f, secondaryBarHeightF - bothBarHeight) :
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.bottom - secondaryBarHeightF, 1.0f, secondaryBarHeightF - bothBarHeight);
draw(secondaryBitmap, m_SecondaryColor, dst);
}
else
{
const D2D1_RECT_F& dst = m_Flip ?
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.top + bothBarHeight, 1.0f, primaryBarHeightF - bothBarHeight) :
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.bottom - primaryBarHeightF, 1.0f, primaryBarHeightF - bothBarHeight);
draw(primaryBitmap, m_PrimaryColor, dst);
}
}
else
{
const D2D1_RECT_F& dst = m_Flip ?
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.top, 1.0f, primaryBarHeightF) :
Gfx::Util::ToRectF(meterRect.left + startStep, meterRect.bottom - primaryBarHeightF, 1.0f, primaryBarHeightF);
draw(primaryBitmap, m_PrimaryColor, dst);
}
}
}
return true;
}
/*
** Overwritten method to handle the secondary measure binding.
**
*/
void MeterHistogram::BindMeasures(ConfigParser& parser, const WCHAR* section)
{
if (BindPrimaryMeasure(parser, section, false))
{
const std::wstring* secondaryMeasure = &parser.ReadString(section, L"MeasureName2", L"");
if (secondaryMeasure->empty())
{
// For backwards compatibility.
secondaryMeasure = &parser.ReadString(section, L"SecondaryMeasureName", L"");
}
Measure* measure = parser.GetMeasure(*secondaryMeasure);
if (measure)
{
m_Measures.push_back(measure);
}
}
}