rainmeter-studio/Library/MeterBitmap.cpp

443 lines
9.9 KiB
C++
Raw Normal View History

2009-02-10 18:37:48 +00:00
/*
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "MeterBitmap.h"
#include "Measure.h"
#include "Error.h"
#include "Rainmeter.h"
using namespace Gdiplus;
extern CRainmeter* Rainmeter;
/*
** CMeterBitmap
**
** The constructor
**
*/
CMeterBitmap::CMeterBitmap(CMeterWindow* meterWindow) : CMeter(meterWindow)
{
m_Bitmap = NULL;
m_FrameCount = 1;
m_ZeroFrame = false;
m_Align = ALIGN_LEFT;
m_Extend = false;
m_Separation = 0;
m_Digits = 0;
m_Value = 0;
m_TransitionFrameCount = 0;
m_TransitionStartTicks = 0;
2009-02-10 18:37:48 +00:00
}
/*
** ~CMeterBitmap
**
** The destructor
**
*/
CMeterBitmap::~CMeterBitmap()
{
if(m_Bitmap != NULL) delete m_Bitmap;
}
/*
** Initialize
**
** Load the image and get the dimensions of the meter from it.
**
*/
void CMeterBitmap::Initialize()
{
CMeter::Initialize();
// Load the bitmaps if defined
if(!m_ImageName.empty())
{
m_Bitmap = new Bitmap(m_ImageName.c_str());
Status status = m_Bitmap->GetLastStatus();
if(Ok != status)
{
throw CError(std::wstring(L"Bitmap image not found: ") + m_ImageName, __LINE__, __FILE__);
}
m_W = m_Bitmap->GetWidth();
m_H = m_Bitmap->GetHeight();
if(m_H > m_W)
{
m_H = m_H / m_FrameCount;
}
else
{
m_W = m_W / m_FrameCount;
}
}
}
/*
** HitTest
**
** Checks if the given point is inside the meter.
**
*/
bool CMeterBitmap::HitTest(int x, int y)
{
if (m_Extend)
{
int value = (int)m_Value;
value = max(0, value); // Only positive integers are supported
int tmpValue = value;
// Calc the number of numbers
int numOfNums = 0;
if (m_Digits > 0)
{
numOfNums = m_Digits;
}
else
{
int realFrames = (m_FrameCount / (m_TransitionFrameCount + 1));
2009-02-10 18:37:48 +00:00
do
{
numOfNums ++;
if (realFrames == 1)
2009-02-10 18:37:48 +00:00
{
tmpValue /= 2;
}
else
{
tmpValue /= realFrames;
2009-02-10 18:37:48 +00:00
}
} while (tmpValue > 0);
}
Rect rect(GetX(), GetY(), m_W * numOfNums + (numOfNums - 1) * m_Separation, m_H);
if (m_Align == ALIGN_CENTER)
{
rect.Offset(-rect.Width / 2, 0);
}
else if (m_Align == ALIGN_RIGHT)
{
rect.Offset(-rect.Width, 0);
}
if (rect.Contains(x, y))
{
return true;
}
return false;
}
else
{
return CMeter::HitTest(x, y);
}
}
/*
** ReadConfig
**
** Read the meter-specific configs from the ini-file.
**
*/
void CMeterBitmap::ReadConfig(const WCHAR* section)
{
// Read common configs
CMeter::ReadConfig(section);
CConfigParser& parser = m_MeterWindow->GetParser();
Added MeterStyle functionality: Rainy, given the "issues" listed at the bottom of this comment, I leave it to you whether to create a build using this revision or use r208 for the build. I would like to start testing MeterStyle, but there are a few more things it needs work on. What is MeterStyle? MeterStyle This will allow users to create CSS-like "Styles" for meters. This means that all the parameters of a meter can be defined in the style, and then many meters can use the style to eliminate copy / pasting the same parameters over and over on multiple meters. (Examples: FontColor=, FontSize= etc.) How do I use it? You will create a new [Section] (as many as you want) in the .ini. The section(s) can have any name. [MyStringStyle] Then you will tell Rainmeter that this is a "MeterStyle" and not a measure or meter Style=Meter Note: The "value" of the key "Style" can be anything. It can be used to add a description of the style if you like. Style=This style is for the AccuWeather part of this skin It is however required, both to tell Rainmeter it is not a meter or measure and to have the MeterStyle routines parse it. Then you define parameters you want to use in the style FontColor=#FontColor# FontFace=TheSansBold-Caps FontSize=11 StringEffect=SHADOW StringStyle=BOLD StringAlign=LEFT AntiAlias=1 Then in any or all meters, you just use [MeterName] Meter=STRING (or any other meter type) MeterStyle=MyStringStyle None of the parameters in the style are then required to be actually in the meter(s). They are "inherited" from the MeterStyle. Note: This works and has had preliminary testing with dynamic variables like FontColor=[MeasureName] and regular variables like FontColor=#FontColor#. It doesn't matter if the [Variables] section or the [MeasureName] measure is before or after the [StyleName] in the .ini file. What if I want to override a MeterStyle parameter on a meter? Sure. Just put in any parameter with a value different from the one defined in the MeterStyle and the one in the meter will take presidence. All non-defined parameters will still use the MeterStyle value. [MeterName] Meter=STRING MeterStyle=MyStringStyle FontColor=100,100,100,50 What are these "known issues" you are on about? This is still a bit of a work in progress. Right now you cannot define X or Y in a style. You can define W and H, but NOT for a STRING meter. You cannot define a "Transformation Matrix" in a style. MattKing will be looking into these tomorrow. W and H in a string meter is our top priority. We will also look at X and Y and hope for an easy solution. Transformation Matrix may have to come later.
2009-09-04 14:48:28 +00:00
m_ImageName = parser.ReadString(section, L"BitmapImage", parser.ReadString(m_StyleName.c_str(), L"BitmapImage", L"").c_str(),true,true);
m_ImageName = m_MeterWindow->MakePathAbsolute(m_ImageName);
2009-02-10 18:37:48 +00:00
Added MeterStyle functionality: Rainy, given the "issues" listed at the bottom of this comment, I leave it to you whether to create a build using this revision or use r208 for the build. I would like to start testing MeterStyle, but there are a few more things it needs work on. What is MeterStyle? MeterStyle This will allow users to create CSS-like "Styles" for meters. This means that all the parameters of a meter can be defined in the style, and then many meters can use the style to eliminate copy / pasting the same parameters over and over on multiple meters. (Examples: FontColor=, FontSize= etc.) How do I use it? You will create a new [Section] (as many as you want) in the .ini. The section(s) can have any name. [MyStringStyle] Then you will tell Rainmeter that this is a "MeterStyle" and not a measure or meter Style=Meter Note: The "value" of the key "Style" can be anything. It can be used to add a description of the style if you like. Style=This style is for the AccuWeather part of this skin It is however required, both to tell Rainmeter it is not a meter or measure and to have the MeterStyle routines parse it. Then you define parameters you want to use in the style FontColor=#FontColor# FontFace=TheSansBold-Caps FontSize=11 StringEffect=SHADOW StringStyle=BOLD StringAlign=LEFT AntiAlias=1 Then in any or all meters, you just use [MeterName] Meter=STRING (or any other meter type) MeterStyle=MyStringStyle None of the parameters in the style are then required to be actually in the meter(s). They are "inherited" from the MeterStyle. Note: This works and has had preliminary testing with dynamic variables like FontColor=[MeasureName] and regular variables like FontColor=#FontColor#. It doesn't matter if the [Variables] section or the [MeasureName] measure is before or after the [StyleName] in the .ini file. What if I want to override a MeterStyle parameter on a meter? Sure. Just put in any parameter with a value different from the one defined in the MeterStyle and the one in the meter will take presidence. All non-defined parameters will still use the MeterStyle value. [MeterName] Meter=STRING MeterStyle=MyStringStyle FontColor=100,100,100,50 What are these "known issues" you are on about? This is still a bit of a work in progress. Right now you cannot define X or Y in a style. You can define W and H, but NOT for a STRING meter. You cannot define a "Transformation Matrix" in a style. MattKing will be looking into these tomorrow. W and H in a string meter is our top priority. We will also look at X and Y and hope for an easy solution. Transformation Matrix may have to come later.
2009-09-04 14:48:28 +00:00
m_FrameCount = parser.ReadInt(section, L"BitmapFrames", parser.ReadInt(m_StyleName.c_str(), L"BitmapFrames", 1));
m_ZeroFrame = 0!=parser.ReadInt(section, L"BitmapZeroFrame", 0!=parser.ReadInt(m_StyleName.c_str(), L"BitmapZeroFrame", 0));
2009-02-10 18:37:48 +00:00
Added MeterStyle functionality: Rainy, given the "issues" listed at the bottom of this comment, I leave it to you whether to create a build using this revision or use r208 for the build. I would like to start testing MeterStyle, but there are a few more things it needs work on. What is MeterStyle? MeterStyle This will allow users to create CSS-like "Styles" for meters. This means that all the parameters of a meter can be defined in the style, and then many meters can use the style to eliminate copy / pasting the same parameters over and over on multiple meters. (Examples: FontColor=, FontSize= etc.) How do I use it? You will create a new [Section] (as many as you want) in the .ini. The section(s) can have any name. [MyStringStyle] Then you will tell Rainmeter that this is a "MeterStyle" and not a measure or meter Style=Meter Note: The "value" of the key "Style" can be anything. It can be used to add a description of the style if you like. Style=This style is for the AccuWeather part of this skin It is however required, both to tell Rainmeter it is not a meter or measure and to have the MeterStyle routines parse it. Then you define parameters you want to use in the style FontColor=#FontColor# FontFace=TheSansBold-Caps FontSize=11 StringEffect=SHADOW StringStyle=BOLD StringAlign=LEFT AntiAlias=1 Then in any or all meters, you just use [MeterName] Meter=STRING (or any other meter type) MeterStyle=MyStringStyle None of the parameters in the style are then required to be actually in the meter(s). They are "inherited" from the MeterStyle. Note: This works and has had preliminary testing with dynamic variables like FontColor=[MeasureName] and regular variables like FontColor=#FontColor#. It doesn't matter if the [Variables] section or the [MeasureName] measure is before or after the [StyleName] in the .ini file. What if I want to override a MeterStyle parameter on a meter? Sure. Just put in any parameter with a value different from the one defined in the MeterStyle and the one in the meter will take presidence. All non-defined parameters will still use the MeterStyle value. [MeterName] Meter=STRING MeterStyle=MyStringStyle FontColor=100,100,100,50 What are these "known issues" you are on about? This is still a bit of a work in progress. Right now you cannot define X or Y in a style. You can define W and H, but NOT for a STRING meter. You cannot define a "Transformation Matrix" in a style. MattKing will be looking into these tomorrow. W and H in a string meter is our top priority. We will also look at X and Y and hope for an easy solution. Transformation Matrix may have to come later.
2009-09-04 14:48:28 +00:00
m_Separation = parser.ReadInt(section, L"BitmapSeparation", parser.ReadInt(m_StyleName.c_str(), L"BitmapSeparation", 0));
m_Extend = 0!=parser.ReadInt(section, L"BitmapExtend", 0!=parser.ReadInt(m_StyleName.c_str(), L"BitmapExtend", 0));
m_Digits = parser.ReadInt(section, L"BitmapDigits", parser.ReadInt(m_StyleName.c_str(), L"BitmapDigits", 0));
2009-02-10 18:37:48 +00:00
Added MeterStyle functionality: Rainy, given the "issues" listed at the bottom of this comment, I leave it to you whether to create a build using this revision or use r208 for the build. I would like to start testing MeterStyle, but there are a few more things it needs work on. What is MeterStyle? MeterStyle This will allow users to create CSS-like "Styles" for meters. This means that all the parameters of a meter can be defined in the style, and then many meters can use the style to eliminate copy / pasting the same parameters over and over on multiple meters. (Examples: FontColor=, FontSize= etc.) How do I use it? You will create a new [Section] (as many as you want) in the .ini. The section(s) can have any name. [MyStringStyle] Then you will tell Rainmeter that this is a "MeterStyle" and not a measure or meter Style=Meter Note: The "value" of the key "Style" can be anything. It can be used to add a description of the style if you like. Style=This style is for the AccuWeather part of this skin It is however required, both to tell Rainmeter it is not a meter or measure and to have the MeterStyle routines parse it. Then you define parameters you want to use in the style FontColor=#FontColor# FontFace=TheSansBold-Caps FontSize=11 StringEffect=SHADOW StringStyle=BOLD StringAlign=LEFT AntiAlias=1 Then in any or all meters, you just use [MeterName] Meter=STRING (or any other meter type) MeterStyle=MyStringStyle None of the parameters in the style are then required to be actually in the meter(s). They are "inherited" from the MeterStyle. Note: This works and has had preliminary testing with dynamic variables like FontColor=[MeasureName] and regular variables like FontColor=#FontColor#. It doesn't matter if the [Variables] section or the [MeasureName] measure is before or after the [StyleName] in the .ini file. What if I want to override a MeterStyle parameter on a meter? Sure. Just put in any parameter with a value different from the one defined in the MeterStyle and the one in the meter will take presidence. All non-defined parameters will still use the MeterStyle value. [MeterName] Meter=STRING MeterStyle=MyStringStyle FontColor=100,100,100,50 What are these "known issues" you are on about? This is still a bit of a work in progress. Right now you cannot define X or Y in a style. You can define W and H, but NOT for a STRING meter. You cannot define a "Transformation Matrix" in a style. MattKing will be looking into these tomorrow. W and H in a string meter is our top priority. We will also look at X and Y and hope for an easy solution. Transformation Matrix may have to come later.
2009-09-04 14:48:28 +00:00
m_TransitionFrameCount = parser.ReadInt(section, L"BitmapTransitionFrames", parser.ReadInt(m_StyleName.c_str(), L"BitmapTransitionFrames", 0));
2009-02-10 18:37:48 +00:00
std::wstring align;
Added MeterStyle functionality: Rainy, given the "issues" listed at the bottom of this comment, I leave it to you whether to create a build using this revision or use r208 for the build. I would like to start testing MeterStyle, but there are a few more things it needs work on. What is MeterStyle? MeterStyle This will allow users to create CSS-like "Styles" for meters. This means that all the parameters of a meter can be defined in the style, and then many meters can use the style to eliminate copy / pasting the same parameters over and over on multiple meters. (Examples: FontColor=, FontSize= etc.) How do I use it? You will create a new [Section] (as many as you want) in the .ini. The section(s) can have any name. [MyStringStyle] Then you will tell Rainmeter that this is a "MeterStyle" and not a measure or meter Style=Meter Note: The "value" of the key "Style" can be anything. It can be used to add a description of the style if you like. Style=This style is for the AccuWeather part of this skin It is however required, both to tell Rainmeter it is not a meter or measure and to have the MeterStyle routines parse it. Then you define parameters you want to use in the style FontColor=#FontColor# FontFace=TheSansBold-Caps FontSize=11 StringEffect=SHADOW StringStyle=BOLD StringAlign=LEFT AntiAlias=1 Then in any or all meters, you just use [MeterName] Meter=STRING (or any other meter type) MeterStyle=MyStringStyle None of the parameters in the style are then required to be actually in the meter(s). They are "inherited" from the MeterStyle. Note: This works and has had preliminary testing with dynamic variables like FontColor=[MeasureName] and regular variables like FontColor=#FontColor#. It doesn't matter if the [Variables] section or the [MeasureName] measure is before or after the [StyleName] in the .ini file. What if I want to override a MeterStyle parameter on a meter? Sure. Just put in any parameter with a value different from the one defined in the MeterStyle and the one in the meter will take presidence. All non-defined parameters will still use the MeterStyle value. [MeterName] Meter=STRING MeterStyle=MyStringStyle FontColor=100,100,100,50 What are these "known issues" you are on about? This is still a bit of a work in progress. Right now you cannot define X or Y in a style. You can define W and H, but NOT for a STRING meter. You cannot define a "Transformation Matrix" in a style. MattKing will be looking into these tomorrow. W and H in a string meter is our top priority. We will also look at X and Y and hope for an easy solution. Transformation Matrix may have to come later.
2009-09-04 14:48:28 +00:00
align = parser.ReadString(section, L"BitmapAlign", parser.ReadString(m_StyleName.c_str(), L"BitmapAlign", L"LEFT").c_str(),true,true);
2009-02-10 18:37:48 +00:00
if(_wcsicmp(align.c_str(), L"LEFT") == 0)
{
m_Align = ALIGN_LEFT;
}
else if(_wcsicmp(align.c_str(), L"RIGHT") == 0)
{
m_Align = ALIGN_RIGHT;
}
else if(_wcsicmp(align.c_str(), L"CENTER") == 0)
{
m_Align = ALIGN_CENTER;
}
else
{
throw CError(std::wstring(L"No such BitmapAlign: ") + align, __LINE__, __FILE__);
}
}
/*
** Update
**
** Updates the value(s) from the measures.
**
*/
bool CMeterBitmap::Update()
{
if (CMeter::Update() && m_Measure)
{
double value = 0.0;
2009-02-10 18:37:48 +00:00
if (m_Extend)
{
value = m_Measure->GetValue();
2009-02-10 18:37:48 +00:00
}
else
{
value = m_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 = GetTickCount();
}
else
{
m_TransitionStartTicks = 0;
}
2009-02-10 18:37:48 +00:00
}
m_Value = value;
return true;
}
return false;
}
/*
** HasActiveTransition
**
** Returns true if the meter has active transition animation.
**
*/
bool CMeterBitmap::HasActiveTransition()
{
if (m_TransitionStartTicks > 0)
{
2009-02-10 18:37:48 +00:00
return true;
}
return false;
}
/*
** Draw
**
** Draws the meter on the double buffer
**
*/
bool CMeterBitmap::Draw(Graphics& graphics)
2009-02-10 18:37:48 +00:00
{
if(!CMeter::Draw(graphics)) return false;
2009-02-10 18:37:48 +00:00
int newY, newX;
if(m_FrameCount == 0 || m_Bitmap == NULL) return false; // Unable to continue
int x = GetX();
int y = GetY();
DWORD diffTicks = GetTickCount() - m_TransitionStartTicks;
2009-02-10 18:37:48 +00:00
if (m_Extend)
{
int value = (int)m_Value;
value = max(0, value); // Only positive integers are supported
int transitionValue = (int)m_TransitionStartValue;
transitionValue = max(0, transitionValue); // Only positive integers are supported
2009-02-10 18:37:48 +00:00
int tmpValue = value;
// Calc the number of numbers
int numOfNums = 0;
if (m_Digits > 0)
{
numOfNums = m_Digits;
}
else
{
do
{
numOfNums ++;
if (m_FrameCount == 1)
{
tmpValue /= 2;
}
else
{
tmpValue /= m_FrameCount;
}
} while (tmpValue > 0);
}
// Blit the images
int offset;
if (m_Align == ALIGN_RIGHT)
{
offset = 0;
}
else if (m_Align == ALIGN_CENTER)
{
offset = numOfNums * (m_W + m_Separation) / 2;
}
else
{
offset = numOfNums * (m_W + m_Separation);
}
do
{
offset = offset - (m_W + m_Separation);
Rect r(x + offset, y, m_W, m_H);
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 range = ((value % realFrames) - ((int)transitionValue % realFrames)) * (m_TransitionFrameCount + 1);
if (range < 0)
{
range += m_FrameCount;
}
int frameAdjustment = 0;
frameAdjustment = range * diffTicks / ((m_TransitionFrameCount + 1) * m_MeterWindow->GetTransitionUpdate());
if (frameAdjustment > range)
{
m_TransitionStartTicks = 0; // The transition is over. Draw with the real value.
}
else
{
frame = ((int)transitionValue % realFrames) * (m_TransitionFrameCount + 1);
frame += frameAdjustment;
frame %= m_FrameCount;
}
}
// DebugLog(L"[%i] Value: %f Frame: %i (Transition = %s)", GetTickCount(), m_Value, frame, m_TransitionStartTicks > 0 ? L"true" : L"false");
2009-02-10 18:37:48 +00:00
if(m_Bitmap->GetHeight() > m_Bitmap->GetWidth())
{
newX = 0;
newY = m_H * frame;
2009-02-10 18:37:48 +00:00
}
else
{
newX = m_W * frame;
2009-02-10 18:37:48 +00:00
newY = 0;
}
graphics.DrawImage(m_Bitmap, r, newX, newY, m_W, m_H, UnitPixel);
if (m_FrameCount == 1)
{
value /= 2;
transitionValue /= 2;
2009-02-10 18:37:48 +00:00
}
else
{
value /= realFrames;
transitionValue /= realFrames;
2009-02-10 18:37:48 +00:00
}
numOfNums--;
} while (numOfNums > 0);
}
else
{
int frame = 0;
int realFrames = (m_FrameCount / (m_TransitionFrameCount + 1));
2009-02-10 18:37:48 +00:00
if (m_ZeroFrame)
{
// Use the first frame only if the value is zero
if (m_Value > 0)
{
frame = (int)(m_Value * (realFrames - 1)) * (m_TransitionFrameCount + 1);
2009-02-10 18:37:48 +00:00
}
}
else
{
// Select the correct frame linearly
frame = (int)(m_Value * realFrames) * (m_TransitionFrameCount + 1);
2009-02-10 18:37:48 +00:00
}
// If transition is ongoing the pick the correct frame
if (m_TransitionStartTicks > 0)
{
if ((int)diffTicks > ((m_TransitionFrameCount + 1) * m_MeterWindow->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_MeterWindow->GetTransitionUpdate());
double frameAdjustment = adjustment * m_FrameCount;
frame = (int)(m_TransitionStartValue * realFrames) * (m_TransitionFrameCount + 1);
frame += (int)frameAdjustment;
frame %= m_FrameCount;
frame = max(0, frame);
}
}
// DebugLog(L"[%i] Value: %f Frame: %i (Transition = %s)", GetTickCount(), m_Value, frame, m_TransitionStartTicks > 0 ? L"true" : L"false");
2009-02-10 18:37:48 +00:00
if(m_Bitmap->GetHeight() > m_Bitmap->GetWidth())
{
newX = 0;
newY = frame * m_H;
}
else
{
newX = frame * m_W;
newY = 0;
}
// Blit the image
Rect r(x, y, m_W, m_H);
graphics.DrawImage(m_Bitmap, r, newX, newY, m_W, m_H, UnitPixel);
}
return true;
}