forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeterButton.cpp
296 lines (260 loc) · 5.78 KB
/
MeterButton.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
/* Copyright (C) 2005 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 "MeterButton.h"
#include "Measure.h"
#include "Rainmeter.h"
#include "../Common/Gfx/Canvas.h"
enum BUTTON_STATE
{
BUTTON_STATE_NORMAL,
BUTTON_STATE_DOWN,
BUTTON_STATE_HOVER
};
MeterButton::MeterButton(Skin* skin, const WCHAR* name) : Meter(skin, name),
m_Image(L"ButtonImage", nullptr, true, skin),
m_BitmapsRects(),
m_State(BUTTON_STATE_NORMAL),
m_Clicked(false),
m_Focus(false)
{
}
MeterButton::~MeterButton()
{
}
/*
** Load the image and get the dimensions of the meter from it.
**
*/
void MeterButton::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();
int bitmapW = bitmap->GetWidth();
int bitmapH = bitmap->GetHeight();
m_W = bitmapW;
m_H = bitmapH;
if (m_H > m_W)
{
m_H /= BUTTON_FRAMES;
}
else
{
m_W /= BUTTON_FRAMES;
}
// Separate the frames
for (int i = 0; i < BUTTON_FRAMES; ++i)
{
if (bitmapH > bitmapW)
{
m_BitmapsRects[i] = D2D1::RectF(0.0f, (FLOAT)(m_H * i), (FLOAT)m_W, (FLOAT)(m_H * (i + 1)));
}
else
{
m_BitmapsRects[i] = D2D1::RectF((FLOAT)(m_W * i), 0.0f, (FLOAT)(m_W * (i + 1)), (FLOAT)m_H);
}
}
m_W += GetWidthPadding();
m_H += GetHeightPadding();
}
}
else if (m_Image.IsLoaded())
{
m_Image.DisposeImage();
}
}
/*
** Read the options specified in the ini file.
**
*/
void MeterButton::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"ButtonImage", L"");
if (!m_ImageName.empty())
{
// Read tinting options
m_Image.ReadOptions(parser, section);
}
m_Command = parser.ReadString(section, L"ButtonCommand", L"", false);
if (m_Initialized)
{
Initialize(); // Reload the image
}
}
/*
** Updates the value(s) from the measures.
**
*/
bool MeterButton::Update()
{
return Meter::Update();
}
/*
** Draws the meter on the double buffer
**
*/
bool MeterButton::Draw(Gfx::Canvas& canvas)
{
if (!Meter::Draw(canvas)) return false;
const auto image = m_Image.GetImage();
D2D1_RECT_F meterRect = GetMeterRectPadding();
if (image)
{
canvas.DrawBitmap(
image,
D2D1::RectF(
meterRect.left,
meterRect.top,
meterRect.left + (FLOAT)m_W,
meterRect.top + (FLOAT)m_H),
m_BitmapsRects[m_State]);
}
return true;
}
/*
** Overridden method. The meters need not to be bound on anything
**
*/
void MeterButton::BindMeasures(ConfigParser& parser, const WCHAR* section)
{
BindPrimaryMeasure(parser, section, true);
}
/*
** Checks if the given point is inside the button.
**
*/
bool MeterButton::HitTest2(int px, int py)
{
if (!Meter::HitTestContainer(px, py))
{
return false;
}
int x = GetX();
int y = GetY();
if (m_MouseOver &&
px >= x && px < (x + m_W) &&
py >= y && py < (y + m_H))
{
if (m_SolidColor.a != 0.0f || m_SolidColor2.a != 0.0f)
{
return true;
}
// Check transparent pixels
if (m_Image.IsLoaded())
{
const D2D1_RECT_F meterRect = GetMeterRectPadding();
if (Gfx::Util::RectContains(meterRect, D2D1::Point2F((FLOAT)px, (FLOAT)py)))
{
const FLOAT drawW = meterRect.right - meterRect.left;
const FLOAT drawH = meterRect.bottom - meterRect.top;
px = px - (int)meterRect.left;
py = py - (int)meterRect.top;
auto bitmap = m_Image.GetImage();
if (!bitmap) return false;
const int bitmapW = bitmap->GetWidth();
const int bitmapH = bitmap->GetHeight();
if (bitmapW > bitmapH)
{
px -= (int)drawW * m_State;
}
else
{
py -= (int)drawH * m_State;
}
D2D1_COLOR_F color;
const bool valid = bitmap->GetPixel(m_Skin->GetCanvas(), px, py, color);
return !valid || (color.a != 0.0f);
}
}
else
{
return true;
}
}
return false;
}
bool MeterButton::MouseUp(POINT pos, bool execute)
{
if (m_State == BUTTON_STATE_DOWN)
{
if (execute && m_Clicked && m_Focus && HitTest2(pos.x, pos.y))
{
GetRainmeter().ExecuteActionCommand(m_Command.c_str(), this);
}
m_State = BUTTON_STATE_NORMAL;
m_Clicked = false;
return true;
}
m_Clicked = false;
return false;
}
bool MeterButton::MouseDown(POINT pos)
{
if (m_Focus && HitTest2(pos.x, pos.y))
{
m_State = BUTTON_STATE_DOWN;
m_Clicked = true;
return true;
}
return false;
}
bool MeterButton::MouseMove(POINT pos)
{
if (m_Clicked)
{
if (HitTest2(pos.x, pos.y))
{
if (m_State == BUTTON_STATE_NORMAL)
{
m_State = BUTTON_STATE_DOWN;
return true;
}
}
else
{
// If the left button is not down anymore the clicked state needs to be set false
if (!IsLButtonDown())
{
m_Clicked = false;
}
if (m_State == BUTTON_STATE_DOWN)
{
m_State = BUTTON_STATE_NORMAL;
return true;
}
}
}
else
{
if (HitTest2(pos.x, pos.y))
{
if (m_State == BUTTON_STATE_NORMAL)
{
m_State = BUTTON_STATE_HOVER;
return true;
}
}
else
{
if (m_State == BUTTON_STATE_HOVER)
{
m_State = BUTTON_STATE_NORMAL;
return true;
}
}
}
return false;
}