00001
00002
00003
00004 #ifndef Impala_Application_SDash_TimeSlider_h
00005 #define Impala_Application_SDash_TimeSlider_h
00006
00007 #ifndef Impala_Application_SDash_TimeSliderListener_h
00008 #include "Application/sdash/TimeSliderListener.h"
00009 #endif
00010
00011 namespace Impala {
00012 namespace Application {
00013 namespace SDash {
00014
00015 using namespace OglGui;
00016
00017 class TimeSlider: public Window
00018 {
00019
00020 public:
00021 TimeSlider(Window *parent, int w, int h,
00022 long startSecs, long timeSpan) :
00023 Window(parent, w, h)
00024 {
00025 Init(w,h,startSecs,timeSpan);
00026 }
00027
00028 TimeSlider(Window *parent, int x, int y, int w, int h,
00029 long startSecs, long timeSpan) :
00030 Window(parent, x, y, w, h)
00031 {
00032 Init(w,h,startSecs,timeSpan);
00033 }
00034
00035 void SetTimeSliderListener(TimeSliderListener* li, void* userData=0)
00036 {
00037 mListener = li;
00038 mListenerData = userData;
00039 }
00040
00041 static void
00042 GetTimeUnits(long timeInSeconds, int& hours, int& minutes, int& seconds)
00043 {
00044 hours = timeInSeconds / 3600;
00045 minutes = (timeInSeconds % 3600) / 60;
00046 seconds = (timeInSeconds % 3600) % 60;
00047 }
00048
00049 static long UnitsToSeconds(int h, int m, int s)
00050 {
00051 return h * 3600 + m * 60 + s;
00052 }
00053
00054 void SetTime(long secs)
00055 {
00056 if (secs < mStartTime)
00057 secs = mStartTime;
00058 if (secs > mStartTime + mTimeSpan)
00059 secs = mStartTime + mTimeSpan;
00060
00061 if (mCurrentTime == secs)
00062 return;
00063
00064 mCurrentTime = secs;
00065
00066 if (mListener)
00067 mListener->TimeSliderEvent(this, secs, mListenerData);
00068 }
00069
00070 void TimeSpan(long timeSpan) { mTimeSpan = timeSpan; }
00071 long TimeSpan() { return mTimeSpan; }
00072
00073 void StartTime(long startTime) { mStartTime = startTime; }
00074 long StartTime() { return mStartTime; }
00075
00076 void CurrentTime(long curTime) { mCurrentTime = curTime; }
00077 long CurrentTime() { return mCurrentTime; }
00078
00079 int ComputeX(long secs)
00080 {
00081 float lW = W()-2*mMargin;
00082 float x = mMargin + ((secs-mStartTime)/(float)mTimeSpan) * lW;
00083 return (int) x;
00084 }
00085
00086 virtual void DisplayFunc()
00087 {
00088 OGC oldOGC;
00089 OGCSave(&oldOGC);
00090
00091 int h = H(), w = W();
00092 SetSolidLineColor(oglBLACK);
00093 DrawLine(mMargin, mLineY, w-mMargin, mLineY);
00094 SetSolidLineColor(oglWHITE);
00095 DrawLine(mMargin, mLineY-1, w-mMargin, mLineY-1);
00096
00097 DrawTicks();
00098 DrawCurrentTime();
00099 OGCRestore(&oldOGC);
00100 Window::DisplayFunc();
00101 }
00102
00103 virtual void MouseFunc(int msg, int btn, int state, int x, int y)
00104 {
00105 int w = W();
00106 if (msg == oglMouseDown || msg == oglMouseMove)
00107 {
00108 if (x<mMargin)
00109 x = mMargin;
00110 if (x>w-mMargin)
00111 x = w-mMargin;
00112 float lW = w-2*mMargin;
00113 float fact = (x-mMargin)/lW;
00114 SetTime(mStartTime + (int) (fact * mTimeSpan+1));
00115 }
00116 if (msg == oglMouseWheelUp)
00117 SetTime(mCurrentTime-1);
00118 if (msg == oglMouseWheelDown)
00119 SetTime(mCurrentTime+2);
00120 }
00121
00122 protected:
00123 void DrawTicks()
00124 {
00125 int h, m, s;
00126 long tickT = mStartTime + mTimeSpan;
00127
00128 while (tickT >= mStartTime)
00129 {
00130 GetTimeUnits(tickT, h, m, s);
00131 int x = ComputeX(tickT);
00132 if ( !(m%5) && !(s%60)){
00133 DrawLine(x, mLineY, x, mLineY - 5);
00134 oglSys.PosColPrintf(mOglWnd, x-15, mLineY-14, 0xff606060,
00135 "%02d:%02d", h, m);
00136 }
00137 else if (!(s%60))
00138 {
00139 DrawLine(x, mLineY, x, mLineY - 3);
00140 if (W()>280 && mTimeSpan < 14*60 && m%5)
00141 oglSys.PosColPrintf(mOglWnd,x-6,mLineY-14,0xffc0c0c0,
00142 "%02d",m);
00143 }
00144 tickT--;
00145 }
00146 }
00147
00148 void DrawCurrentTime()
00149 {
00150 int h, m, s;
00151
00152 int x = ComputeX(mCurrentTime);
00153 SetSolidLineColor(oglBLACK);
00154 DrawLine(x-1,mLineY+5,x-1,mLineY-5);
00155 DrawLine(x+1,mLineY+5,x+1,mLineY-5);
00156 SetSolidLineColor(oglWHITE);
00157 DrawLine(x,mLineY+5,x,mLineY-5);
00158 GetTimeUnits(mCurrentTime, h, m, s);
00159 oglSys.ShadowPrintf(mOglWnd,W()/2-25,mLineY+3,oglBLACK,oglWHITE,
00160 "%02d:%02d:%02d",h,m,s);
00161 }
00162
00163
00164 private:
00165
00166 void Init(int w, int h, long startSecs, long timeSpan)
00167 {
00168 mListener = 0;
00169
00170 mStartTime = startSecs;
00171 mTimeSpan = timeSpan;
00172 mCurrentTime = -1;
00173
00174 mMargin = 16;
00175 mLineY = 16;
00176
00177 SetTime(startSecs);
00178 }
00179
00180 TimeSliderListener* mListener;
00181 void* mListenerData;
00182
00183 int mLineY;
00184 int mMargin;
00185 long mStartTime;
00186 long mTimeSpan;
00187 long mCurrentTime;
00188 };
00189
00190 }
00191 }
00192 }
00193 #endif