forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeterRotator.cpp
149 lines (125 loc) · 3.62 KB
/
MeterRotator.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
/* 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 "MeterRotator.h"
#include "Measure.h"
#include "Util.h"
#include "Rainmeter.h"
#include "../Common/Gfx/Canvas.h"
#define PI (3.14159265358979323846)
#define CONVERT_TO_DEGREES(X) ((X) * (180.0 / PI))
MeterRotator::MeterRotator(Skin* skin, const WCHAR* name) : Meter(skin, name),
m_Image(L"ImageName", nullptr, false, skin),
m_OffsetX(),
m_OffsetY(),
m_StartAngle(),
m_RotationAngle(PI * 2.0),
m_ValueRemainder(),
m_Value()
{
}
MeterRotator::~MeterRotator()
{
}
/*
** Load the image.
**
*/
void MeterRotator::Initialize()
{
Meter::Initialize();
// Load the bitmaps if defined
if (!m_ImageName.empty())
{
m_Image.LoadImage(m_ImageName);
}
else if (m_Image.IsLoaded())
{
m_Image.DisposeImage();
}
}
/*
** Read the options specified in the ini file.
**
*/
void MeterRotator::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"ImageName", L"");
if (!m_ImageName.empty())
{
// Read tinting options
m_Image.ReadOptions(parser, section);
}
m_OffsetX = parser.ReadFloat(section, L"OffsetX", 0.0);
m_OffsetY = parser.ReadFloat(section, L"OffsetY", 0.0);
m_StartAngle = parser.ReadFloat(section, L"StartAngle", 0.0);
m_RotationAngle = parser.ReadFloat(section, L"RotationAngle", PI * 2.0);
m_ValueRemainder = parser.ReadInt(section, L"ValueReminder", 0); // Typo
m_ValueRemainder = parser.ReadInt(section, L"ValueRemainder", m_ValueRemainder);
if (m_Initialized)
{
Initialize(); // Reload the image
}
}
/*
** Updates the value(s) from the measures.
**
*/
bool MeterRotator::Update()
{
if (Meter::Update() && !m_Measures.empty())
{
Measure* measure = m_Measures[0];
if (m_ValueRemainder > 0)
{
LONGLONG time = (LONGLONG)measure->GetValue();
m_Value = (double)(time % m_ValueRemainder);
m_Value /= (double)m_ValueRemainder;
}
else
{
m_Value = measure->GetRelativeValue();
}
return true;
}
return false;
}
/*
** Draws the meter on the double buffer
**
*/
bool MeterRotator::Draw(Gfx::Canvas& canvas)
{
if (!Meter::Draw(canvas)) return false;
if (m_Image.IsLoaded())
{
Gfx::D2DBitmap* drawBitmap = m_Image.GetImage();
const FLOAT width = (FLOAT)drawBitmap->GetWidth();
const FLOAT height = (FLOAT)drawBitmap->GetHeight();
D2D1_RECT_F meterRect = GetMeterRectPadding();
FLOAT cx = meterRect.left + m_W / 2.0f;
FLOAT cy = meterRect.top + m_H / 2.0f;
D2D1_POINT_2F center = D2D1::Point2F(cx, cy);
// Calculate the rotation
FLOAT angle = (FLOAT)(CONVERT_TO_DEGREES(m_RotationAngle * m_Value + m_StartAngle));
// Get current transform
D2D1_MATRIX_3X2_F matrix = D2D1::Matrix3x2F::Identity();
canvas.GetTransform(&matrix);
canvas.SetTransform(
D2D1::Matrix3x2F::Translation((FLOAT)(-m_OffsetX), (FLOAT)(-m_OffsetY)) *
D2D1::Matrix3x2F::Rotation(angle) *
D2D1::Matrix3x2F::Translation(cx, cy) *
matrix);
const D2D1_RECT_F rect = D2D1::RectF(0.0f, 0.0f, width, height);
canvas.DrawBitmap(drawBitmap, rect, rect);
canvas.ResetTransform();
}
return true;
}