forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeterBitmap.cpp
484 lines (417 loc) · 10.9 KB
/
MeterBitmap.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
/* 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 "MeterBitmap.h"
#include "Measure.h"
#include "Rainmeter.h"
#include "System.h"
#include "../Common/Gfx/Canvas.h"
MeterBitmap::MeterBitmap(Skin* skin, const WCHAR* name) : Meter(skin, name),
m_Image(L"BitmapImage", nullptr, true, skin),
m_ZeroFrame(false),
m_FrameCount(1),
m_TransitionFrameCount(0),
m_Align(ALIGN_LEFT),
m_Extend(false),
m_Separation(0),
m_Digits(0),
m_Value(0.0),
m_TransitionStartTicks(0),
m_TransitionStartValue(0.0)
{
}
MeterBitmap::~MeterBitmap()
{
}
/*
** Load the image and get the dimensions of the meter from it.
**
*/
void MeterBitmap::Initialize()
{
Meter::Initialize();
// Load the bitmaps if defined
if (!m_ImageName.empty())
{
m_Image.LoadImage(m_ImageName);
if (m_Image.IsLoaded())
{
Gfx::D2DBitmap* bitmap = m_Image.GetImage();
m_W = bitmap->GetWidth();
m_H = bitmap->GetHeight();
int extraSpace = (m_Digits - 1) * m_Separation;
extraSpace = max(0, extraSpace);
int digits = max(1, m_Digits);
if (m_H > m_W)
{
m_H = (m_H / m_FrameCount);
if (m_Extend) // Increase meter height to account for BitmapDigigts and BitmapSeparation options
{
m_H *= digits;
m_H += extraSpace;
}
}
else
{
m_W = (m_W / m_FrameCount);
if (m_Extend) // Increase meter width to account for BitmapDigigts and BitmapSeparation options
{
m_W *= digits;
m_W += extraSpace;
}
}
m_W += GetWidthPadding();
m_H += GetHeightPadding();
}
}
else if (m_Image.IsLoaded())
{
m_Image.DisposeImage();
}
}
/*
** Checks if the given point is inside the meter.
**
*/
bool MeterBitmap::HitTest(int x, int y)
{
if (m_Extend)
{
if (!Meter::HitTestContainer(x, y))
{
return false;
}
// Calc the number of numbers
int numOfNums = 0;
if (m_Digits > 0)
{
numOfNums = m_Digits;
}
else
{
int tmpValue = (int)m_Value;
tmpValue = max(0, tmpValue); // Only positive integers are supported
int realFrames = (m_FrameCount / (m_TransitionFrameCount + 1));
do
{
++numOfNums;
if (realFrames == 1)
{
tmpValue /= 2;
}
else
{
tmpValue /= realFrames;
}
}
while (tmpValue > 0);
}
const FLOAT xF = (FLOAT)GetX();
const FLOAT yF = (FLOAT)GetY();
D2D1_RECT_F rect = D2D1::RectF(
xF,
yF,
xF + (FLOAT)(m_W * numOfNums + (numOfNums - 1) * m_Separation),
yF + (FLOAT)m_H);
if (m_Align == ALIGN_CENTER)
{
FLOAT offset = -(rect.right - rect.left) / 2.0f;
rect.left += offset;
rect.right += offset;
}
else if (m_Align == ALIGN_RIGHT)
{
FLOAT offset = -(rect.right - rect.left) / 2.0f;
rect.left += offset;
rect.right += offset;
}
if (Gfx::Util::RectContains(rect, D2D1::Point2F((FLOAT)x, (FLOAT)y)))
{
return true;
}
return false;
}
else
{
return Meter::HitTest(x, y);
}
}
/*
** Read the options specified in the ini file.
**
*/
void MeterBitmap::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
// Store the current values so we know if the image needs to be updated
std::wstring oldImageName = m_ImageName;
Meter::ReadOptions(parser, section);
m_ImageName = parser.ReadString(section, L"BitmapImage", L"");
if (!m_ImageName.empty())
{
// Read tinting options
m_Image.ReadOptions(parser, section);
}
m_FrameCount = parser.ReadInt(section, L"BitmapFrames", 1);
m_ZeroFrame = parser.ReadBool(section, L"BitmapZeroFrame", false);
m_Separation = parser.ReadInt(section, L"BitmapSeparation", 0);
m_Extend = parser.ReadBool(section, L"BitmapExtend", false);
m_Digits = parser.ReadInt(section, L"BitmapDigits", 0);
m_TransitionFrameCount = parser.ReadInt(section, L"BitmapTransitionFrames", 0);
const WCHAR* align = parser.ReadString(section, L"BitmapAlign", L"LEFT").c_str();
if (_wcsicmp(align, L"LEFT") == 0)
{
m_Align = ALIGN_LEFT;
}
else if (_wcsicmp(align, L"RIGHT") == 0)
{
m_Align = ALIGN_RIGHT;
}
else if (_wcsicmp(align, L"CENTER") == 0)
{
m_Align = ALIGN_CENTER;
}
else
{
LogErrorF(this, L"BitmapAlign=%s is not valid", align);
}
if (m_Initialized)
{
Initialize(); // Reload the image
}
}
/*
** Updates the value(s) from the measures.
**
*/
bool MeterBitmap::Update()
{
if (Meter::Update() && !m_Measures.empty())
{
Measure* measure = m_Measures[0];
double value = (m_Extend) ? measure->GetValue() : measure->GetRelativeValue();
if (m_TransitionFrameCount > 0)
{
int realFrames = m_FrameCount / (m_TransitionFrameCount + 1);
if ((int)(value * realFrames) != (int)(m_Value * realFrames))
{
m_TransitionStartValue = m_Value;
m_TransitionStartTicks = GetTickCount64();
}
else
{
m_TransitionStartTicks = 0;
}
}
m_Value = value;
return true;
}
return false;
}
/*
** Returns true if the meter has active transition animation.
**
*/
bool MeterBitmap::HasActiveTransition()
{
if (m_TransitionStartTicks > 0)
{
return true;
}
return false;
}
/*
** Draws the meter on the double buffer
**
*/
bool MeterBitmap::Draw(Gfx::Canvas& canvas)
{
if (!Meter::Draw(canvas)) return false;
int newX = 0;
int newY = 0;
if (m_FrameCount == 0 || !m_Image.IsLoaded()) return false; // Unable to continue
Gfx::D2DBitmap* bitmap = m_Image.GetImage();
D2D1_RECT_F meterRect = GetMeterRectPadding();
FLOAT drawW = meterRect.right - meterRect.left;
FLOAT drawH = meterRect.bottom - meterRect.top;
if (m_Extend)
{
// The 'BitmapExtend' option expands the meters width and/or height by the amount
// of digits and separation. We need to subtract that amount here so that the meter
// will draw in the correct place.
int extraSpace = (m_Digits - 1) * m_Separation;
extraSpace = max(0, extraSpace);
int digits = max(1, m_Digits);
if (bitmap->GetHeight() > bitmap->GetWidth())
{
FLOAT height = drawH;
height -= extraSpace;
height /= digits;
meterRect.bottom = meterRect.top + height;
}
else
{
FLOAT width = drawW;
width -= extraSpace;
width /= digits;
meterRect.right = meterRect.left + width;
}
FLOAT width = meterRect.right - meterRect.left;
FLOAT height = meterRect.bottom - meterRect.top;
__int64 value = (__int64)m_Value;
value = max(0LL, value); // Only positive integers are supported
__int64 transitionValue = (__int64)m_TransitionStartValue;
transitionValue = max(0LL, transitionValue); // Only positive integers are supported
// Calc the number of numbers
int numOfNums = 0;
if (m_Digits > 0)
{
numOfNums = m_Digits;
}
else
{
__int64 tmpValue = value;
do
{
++numOfNums;
if (m_FrameCount == 1)
{
tmpValue /= 2LL;
}
else
{
tmpValue /= m_FrameCount;
}
}
while (tmpValue > 0LL);
}
// Blit the images
int offset;
if (m_Align == ALIGN_RIGHT)
{
offset = 0;
}
else if (m_Align == ALIGN_CENTER)
{
offset = numOfNums * ((int)width + m_Separation) / 2;
}
else
{
offset = numOfNums * ((int)width + m_Separation);
}
do
{
offset = offset - ((int)width + m_Separation);
int realFrames = (m_FrameCount / (m_TransitionFrameCount + 1));
int frame = (value % realFrames) * (m_TransitionFrameCount + 1);
// If transition is ongoing the pick the correct frame
if (m_TransitionStartTicks > 0)
{
int diffTicks = (int)(GetTickCount64() - m_TransitionStartTicks);
int range = ((value % realFrames) - (transitionValue % realFrames)) * (m_TransitionFrameCount + 1);
if (range < 0)
{
range += m_FrameCount;
}
int frameAdjustment = range * diffTicks / ((m_TransitionFrameCount + 1) * m_Skin->GetTransitionUpdate());
if (frameAdjustment > range)
{
m_TransitionStartTicks = 0; // The transition is over. Draw with the real value.
}
else
{
frame = (transitionValue % realFrames) * (m_TransitionFrameCount + 1);
frame += frameAdjustment;
frame %= m_FrameCount;
}
}
//LogDebugF(L"[%llu] Value: %f Frame: %i (Transition = %s)", GetTickCount64(), m_Value, frame, m_TransitionStartTicks > 0ULL ? L"true" : L"false");
if (bitmap->GetHeight() > bitmap->GetWidth())
{
newX = 0;
newY = (int)height * frame;
}
else
{
newX = (int)width * frame;
newY = 0;
}
canvas.DrawBitmap(
bitmap,
D2D1::RectF(
meterRect.left + (FLOAT)offset,
meterRect.top,
meterRect.right + (FLOAT)offset,
meterRect.bottom),
D2D1::RectF((FLOAT)newX, (FLOAT)newY, (FLOAT)newX + width, (FLOAT)newY + height));
if (m_FrameCount == 1)
{
value /= 2LL;
transitionValue /= 2LL;
}
else
{
value /= realFrames;
transitionValue /= realFrames;
}
--numOfNums;
}
while (numOfNums > 0);
}
else
{
int frame = 0;
int realFrames = (m_FrameCount / (m_TransitionFrameCount + 1));
if (m_ZeroFrame)
{
// Use the first frame only if the value is zero
if (m_Value > 0.0)
{
frame = (int)(m_Value * (realFrames - 1)) * (m_TransitionFrameCount + 1);
}
}
else
{
// Select the correct frame linearly
frame = (int)(m_Value * realFrames) * (m_TransitionFrameCount + 1);
}
// If transition is ongoing the pick the correct frame
if (m_TransitionStartTicks > 0)
{
int diffTicks = (int)(GetTickCount64() - m_TransitionStartTicks);
if (diffTicks > ((m_TransitionFrameCount + 1) * m_Skin->GetTransitionUpdate()))
{
m_TransitionStartTicks = 0; // The transition is over. Draw with the real value.
}
else
{
double range = (m_Value - m_TransitionStartValue);
double adjustment = range * diffTicks / ((m_TransitionFrameCount + 1) * m_Skin->GetTransitionUpdate());
double frameAdjustment = adjustment * m_FrameCount;
frame = (int)(m_TransitionStartValue * realFrames) * (m_TransitionFrameCount + 1);
frame += (int)frameAdjustment;
frame %= m_FrameCount;
frame = max(0, frame);
}
}
//LogDebugF(L"[%llu] Value: %f Frame: %i (Transition = %s)", GetTickCount64(), m_Value, frame, m_TransitionStartTicks > 0ULL ? L"true" : L"false");
if (bitmap->GetHeight() > bitmap->GetWidth())
{
newX = 0;
newY = frame * (int)drawH;
}
else
{
newX = frame * (int)drawW;
newY = 0;
}
canvas.DrawBitmap(
bitmap,
meterRect,
D2D1::RectF((FLOAT)newX, (FLOAT)newY, (FLOAT)newX + drawW, (FLOAT)newY + drawH));
}
return true;
}