Merge pull request #473 from Montellese/onplaybackspeedchanged
[vuplus_xbmc] / xbmc / music / windows / GUIWindowVisualisation.cpp
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "GUIWindowVisualisation.h"
23 #include "Application.h"
24 #include "music/dialogs/GUIDialogMusicOSD.h"
25 #include "GUIUserMessages.h"
26 #include "GUIInfoManager.h"
27 #include "music/dialogs/GUIDialogVisualisationPresetList.h"
28 #include "guilib/GUIWindowManager.h"
29 #include "settings/Settings.h"
30 #include "settings/AdvancedSettings.h"
31
32 using namespace MUSIC_INFO;
33
34 #define START_FADE_LENGTH  2.0f // 2 seconds on startup
35
36 #define CONTROL_VIS          2
37
38 CGUIWindowVisualisation::CGUIWindowVisualisation(void)
39     : CGUIWindow(WINDOW_VISUALISATION, "MusicVisualisation.xml"),
40       m_initTimer(true), m_lockedTimer(true)
41 {
42   m_bShowPreset = false;
43 }
44
45 bool CGUIWindowVisualisation::OnAction(const CAction &action)
46 {
47   bool passToVis = false;
48   switch (action.GetID())
49   {
50   case ACTION_VIS_PRESET_NEXT:
51   case ACTION_VIS_PRESET_PREV:
52   case ACTION_VIS_PRESET_RANDOM:
53   case ACTION_VIS_RATE_PRESET_PLUS:
54   case ACTION_VIS_RATE_PRESET_MINUS:
55     passToVis = true;
56     break;
57
58   case ACTION_SHOW_INFO:
59     {
60       m_initTimer.Stop();
61       g_settings.m_bMyMusicSongThumbInVis = g_infoManager.ToggleShowInfo();
62       return true;
63     }
64     break;
65
66   case ACTION_SHOW_GUI:
67     // save the settings
68     g_settings.Save();
69     g_windowManager.PreviousWindow();
70     return true;
71     break;
72
73   case ACTION_VIS_PRESET_LOCK:
74     { // show the locked icon + fall through so that the vis handles the locking
75       if (!m_bShowPreset)
76       {
77         m_lockedTimer.StartZero();
78         g_infoManager.SetShowCodec(true);
79       }
80       passToVis = true;
81     }
82     break;
83   case ACTION_VIS_PRESET_SHOW:
84     {
85       if (!m_lockedTimer.IsRunning() || m_bShowPreset)
86         m_bShowPreset = !m_bShowPreset;
87       g_infoManager.SetShowCodec(m_bShowPreset);
88       return true;
89     }
90     break;
91
92   case ACTION_DECREASE_RATING:
93   case ACTION_INCREASE_RATING:
94     {
95       // actual action is taken care of in CApplication::OnAction()
96       m_initTimer.StartZero();
97       g_infoManager.SetShowInfo(true);
98     }
99     break;
100     // TODO: These should be mapped to it's own function - at the moment it's overriding
101     // the global action of fastforward/rewind and OSD.
102 /*  case KEY_BUTTON_Y:
103     g_application.m_CdgParser.Pause();
104     return true;
105     break;
106
107     case ACTION_ANALOG_FORWARD:
108     // calculate the speed based on the amount the button is held down
109     if (action.GetAmount())
110     {
111       float AVDelay = g_application.m_CdgParser.GetAVDelay();
112       g_application.m_CdgParser.SetAVDelay(AVDelay - action.GetAmount() / 4.0f);
113       return true;
114     }
115     break;*/
116   }
117
118   if (passToVis)
119   {
120     CGUIControl *control = (CGUIControl *)GetControl(CONTROL_VIS);
121     if (control)
122       return control->OnAction(action);
123   }
124
125   return CGUIWindow::OnAction(action);
126 }
127
128 bool CGUIWindowVisualisation::OnMessage(CGUIMessage& message)
129 {
130   switch ( message.GetMessage() )
131   {
132   case GUI_MSG_GET_VISUALISATION:
133   case GUI_MSG_VISUALISATION_RELOAD:
134   case GUI_MSG_PLAYBACK_STARTED:
135     {
136       CGUIControl *control = (CGUIControl *)GetControl(CONTROL_VIS);
137       if (control)
138         return control->OnMessage(message);
139     }
140     break;
141   case GUI_MSG_VISUALISATION_ACTION:
142   {
143     CAction action(message.GetParam1());
144     return OnAction(action);
145   }
146   case GUI_MSG_WINDOW_DEINIT:
147     {
148       if (IsActive()) // save any changed settings from the OSD
149         g_settings.Save();
150       // check and close any OSD windows
151       CGUIDialogMusicOSD *pOSD = (CGUIDialogMusicOSD *)g_windowManager.GetWindow(WINDOW_DIALOG_MUSIC_OSD);
152       if (pOSD && pOSD->IsDialogRunning()) pOSD->Close(true);
153       CGUIDialogVisualisationPresetList *pList = (CGUIDialogVisualisationPresetList *)g_windowManager.GetWindow(WINDOW_DIALOG_VIS_PRESET_LIST);
154       if (pList && pList->IsDialogRunning()) pList->Close(true);
155     }
156     break;
157   case GUI_MSG_WINDOW_INIT:
158     {
159       // check whether we've come back here from a window during which time we've actually
160       // stopped playing music
161       if (message.GetParam1() == WINDOW_INVALID && !g_application.IsPlayingAudio())
162       { // why are we here if nothing is playing???
163         g_windowManager.PreviousWindow();
164         return true;
165       }
166
167       // hide or show the preset button(s)
168       g_infoManager.SetShowCodec(m_bShowPreset);
169       g_infoManager.SetShowInfo(true);  // always show the info initially.
170       CGUIWindow::OnMessage(message);
171       if (g_infoManager.GetCurrentSongTag())
172         m_tag = *g_infoManager.GetCurrentSongTag();
173
174       if (g_settings.m_bMyMusicSongThumbInVis)
175       { // always on
176         m_initTimer.Stop();
177       }
178       else
179       {
180         // start display init timer (fade out after 3 secs...)
181         m_initTimer.StartZero();
182       }
183       return true;
184     }
185   }
186   return CGUIWindow::OnMessage(message);
187 }
188
189 EVENT_RESULT CGUIWindowVisualisation::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
190 {
191   if (event.m_id == ACTION_MOUSE_RIGHT_CLICK)
192   { // no control found to absorb this click - go back to GUI
193     OnAction(CAction(ACTION_SHOW_GUI));
194     return EVENT_RESULT_HANDLED;
195   }
196   if (event.m_id != ACTION_MOUSE_MOVE || event.m_offsetX || event.m_offsetY)
197   { // some other mouse action has occurred - bring up the OSD
198     CGUIDialog *pOSD = (CGUIDialog *)g_windowManager.GetWindow(WINDOW_DIALOG_MUSIC_OSD);
199     if (pOSD)
200     {
201       pOSD->SetAutoClose(3000);
202       pOSD->DoModal();
203     }
204     return EVENT_RESULT_HANDLED;
205   }
206   return EVENT_RESULT_UNHANDLED;
207 }
208
209 void CGUIWindowVisualisation::FrameMove()
210 {
211   // check for a tag change
212   const CMusicInfoTag* tag = g_infoManager.GetCurrentSongTag();
213   if (tag && *tag != m_tag)
214   { // need to fade in then out again
215     m_tag = *tag;
216     // fade in
217     m_initTimer.StartZero();
218     g_infoManager.SetShowInfo(true);
219   }
220   if (m_initTimer.IsRunning() && m_initTimer.GetElapsedSeconds() > (float)g_advancedSettings.m_songInfoDuration)
221   {
222     m_initTimer.Stop();
223     if (!g_settings.m_bMyMusicSongThumbInVis)
224     { // reached end of fade in, fade out again
225       g_infoManager.SetShowInfo(false);
226     }
227   }
228   // show or hide the locked texture
229   if (m_lockedTimer.IsRunning() && m_lockedTimer.GetElapsedSeconds() > START_FADE_LENGTH)
230   {
231     m_lockedTimer.Stop();
232     if (!m_bShowPreset)
233     {
234       g_infoManager.SetShowCodec(false);
235     }
236   }
237   CGUIWindow::FrameMove();
238 }