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
|
#include "Slider.h"
#include "Graphics.h"
#include "Image.h"
#include "SliderListener.h"
#include "WidgetManager.h"
#include "SexyAppBase.h"
using namespace Sexy;
Slider::Slider(Image* theTrackImage, Image* theThumbImage, int theId, SliderListener* theListener) :
mTrackImage(theTrackImage),
mThumbImage(theThumbImage),
mId(theId),
mListener(theListener),
mVal(0.0)
{
mDragging = false;
mHorizontal = true;
mRelX = mRelY = 0;
}
void Slider::SetValue(double theValue)
{
mVal = theValue;
if (mVal < 0.0)
mVal = 0.0;
else if (mVal > 1.0)
mVal = 1.0;
MarkDirtyFull();
}
bool Slider::HasTransparencies()
{
return true;
}
void Slider::Draw(Graphics* g)
{
if (mTrackImage != NULL)
{
int cw = mHorizontal ? mTrackImage->GetWidth()/3 : mTrackImage->GetWidth();
int ch = mHorizontal ? mTrackImage->GetHeight() : mTrackImage->GetHeight()/3;
if (mHorizontal)
{
int ty = (mHeight - ch) / 2;
g->DrawImage(mTrackImage, 0, ty, Rect(0, 0, cw, ch));
Graphics aClipG(*g);
aClipG.ClipRect(cw, ty, mWidth - cw*2, ch);
for (int i = 0; i < (mWidth-cw*2+cw-1)/cw; i++)
aClipG.DrawImage(mTrackImage, cw + i*cw, ty, Rect(cw, 0, cw, ch));
g->DrawImage(mTrackImage, mWidth-cw, ty, Rect(cw*2, 0, cw, ch));
}
else
{
g->DrawImage(mTrackImage, 0, 0, Rect(0, 0, cw, ch));
Graphics aClipG(*g);
aClipG.ClipRect(0, ch, cw, mHeight - ch * 2);
for (int i = 0; i < (mHeight-ch*2+ch-1)/ch; i++)
aClipG.DrawImage(mTrackImage, 0, ch + i*ch, Rect(0, ch, cw, ch));
g->DrawImage(mTrackImage, 0, mHeight-ch, Rect(0, ch*2, cw, ch));
}
}
if (mHorizontal && (mThumbImage != NULL))
g->DrawImage(mThumbImage, (int) (mVal * (mWidth - mThumbImage->GetWidth())), (mHeight - mThumbImage->GetHeight()) / 2);
else if (!mHorizontal && (mThumbImage != NULL))
g->DrawImage(mThumbImage, (mWidth - mThumbImage->GetWidth()) / 2, (int) (mVal * (mHeight - mThumbImage->GetHeight())));
//g->SetColor(Color(255, 255, 0));
//g->FillRect(0, 0, mWidth, mHeight);
}
void Slider::MouseDown(int x, int y, int theClickCount)
{
if (mHorizontal)
{
int aThumbX = (int) (mVal * (mWidth - mThumbImage->GetWidth()));
if ((x >= aThumbX) && (x < aThumbX + mThumbImage->GetWidth()))
{
mWidgetManager->mApp->SetCursor(CURSOR_DRAGGING);
mDragging = true;
mRelX = x - aThumbX;
}
else
{
// clicked on the bar, set position to mouse click
double pos = (double)x / mWidth;
SetValue(pos);
}
}
else
{
int aThumbY = (int) (mVal * (mHeight - mThumbImage->GetHeight()));
if ((y >= aThumbY) && (y < aThumbY + mThumbImage->GetHeight()))
{
mWidgetManager->mApp->SetCursor(CURSOR_DRAGGING);
mDragging = true;
mRelY = y - aThumbY;
}
else
{
// clicked on the bar, set position to mouse click
double pos = (double)y / mHeight;
SetValue(pos);
}
}
}
void Slider::MouseMove(int x, int y)
{
if (mHorizontal)
{
int aThumbX = (int) (mVal * (mWidth - mThumbImage->GetWidth()));
if ((x >= aThumbX) && (x < aThumbX + mThumbImage->GetWidth()))
mWidgetManager->mApp->SetCursor(CURSOR_DRAGGING);
else
mWidgetManager->mApp->SetCursor(CURSOR_POINTER);
}
else
{
int aThumbY = (int) (mVal * (mHeight - mThumbImage->GetHeight()));
if ((y >= aThumbY) && (y < aThumbY + mThumbImage->GetHeight()))
mWidgetManager->mApp->SetCursor(CURSOR_DRAGGING);
else
mWidgetManager->mApp->SetCursor(CURSOR_POINTER);
}
}
void Slider::MouseDrag(int x, int y)
{
if (mDragging)
{
double anOldVal = mVal;
if (mHorizontal)
mVal = (x - mRelX) / (double) (mWidth - mThumbImage->GetWidth());
else
mVal = (y - mRelY) / (double) (mHeight - mThumbImage->GetHeight());
if (mVal < 0.0)
mVal = 0.0;
if (mVal > 1.0)
mVal = 1.0;
if (mVal != anOldVal)
{
mListener->SliderVal(mId, mVal);
MarkDirtyFull();
}
}
}
void Slider::MouseUp(int x, int y)
{
mDragging = false;
mWidgetManager->mApp->SetCursor(CURSOR_POINTER);
mListener->SliderVal(mId, mVal);
}
void Slider::MouseLeave()
{
if (!mDragging)
mWidgetManager->mApp->SetCursor(CURSOR_POINTER);
}
|