/
MeterRotator.cpp
180 lines (149 loc) · 4.12 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
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
/*
Copyright (C) 2001 Kimmo Pekkola
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "StdAfx.h"
#include "MeterRotator.h"
#include "Measure.h"
#include "Error.h"
#include "Litestep.h"
#include "Rainmeter.h"
using namespace Gdiplus;
#define PI (3.14159265358979323846)
#define CONVERT_TO_DEGREES(X) ((X) * (180.0 / PI))
extern CRainmeter* Rainmeter;
/*
** The constructor
**
*/
CMeterRotator::CMeterRotator(CMeterWindow* meterWindow, const WCHAR* name) : CMeter(meterWindow, name),
m_NeedsReload(false),
m_OffsetX(),
m_OffsetY(),
m_StartAngle(),
m_RotationAngle(6.2832),
m_ValueRemainder(),
m_Value()
{
}
/*
** The destructor
**
*/
CMeterRotator::~CMeterRotator()
{
}
/*
** Load the image.
**
*/
void CMeterRotator::Initialize()
{
CMeter::Initialize();
// Load the bitmaps if defined
if (!m_ImageName.empty())
{
m_Image.LoadImage(m_ImageName, m_NeedsReload);
}
else if (m_Image.IsLoaded())
{
m_Image.DisposeImage();
}
}
/*
** Read the options specified in the ini file.
**
*/
void CMeterRotator::ReadOptions(CConfigParser& parser, const WCHAR* section)
{
// Store the current values so we know if the image needs to be updated
std::wstring oldImageName = m_ImageName;
CMeter::ReadOptions(parser, section);
m_ImageName = parser.ReadString(section, L"ImageName", L"");
if (!m_ImageName.empty())
{
m_MeterWindow->MakePathAbsolute(m_ImageName);
// Read tinting options
m_Image.ReadOptions(parser, section);
}
else
{
m_Image.ClearOptionFlags();
}
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", 6.2832);
m_ValueRemainder = parser.ReadInt(section, L"ValueReminder", 0); // Typo
m_ValueRemainder = parser.ReadInt(section, L"ValueRemainder", m_ValueRemainder);
if (m_Initialized)
{
m_NeedsReload = (wcscmp(oldImageName.c_str(), m_ImageName.c_str()) != 0);
if (m_NeedsReload ||
m_Image.IsOptionsChanged())
{
Initialize(); // Reload the image
}
}
}
/*
** Updates the value(s) from the measures.
**
*/
bool CMeterRotator::Update()
{
if (CMeter::Update() && !m_Measures.empty())
{
CMeasure* 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 CMeterRotator::Draw(Graphics& graphics)
{
if (!CMeter::Draw(graphics)) return false;
if (m_Image.IsLoaded())
{
// Calculate the center for rotation
int x = GetX();
int y = GetY();
REAL cx = (REAL)(x + m_W / 2.0);
REAL cy = (REAL)(y + m_H / 2.0);
// Calculate the rotation
REAL angle = (REAL)(CONVERT_TO_DEGREES(m_RotationAngle * m_Value + m_StartAngle));
graphics.TranslateTransform(cx, cy);
graphics.RotateTransform(angle);
graphics.TranslateTransform((REAL)-m_OffsetX, (REAL)-m_OffsetY);
Bitmap* drawBitmap = m_Image.GetImage();
UINT width = drawBitmap->GetWidth();
UINT height = drawBitmap->GetHeight();
// Blit the image
graphics.DrawImage(drawBitmap, 0, 0, width, height);
graphics.ResetTransform();
}
return true;
}