music_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD 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, version 2.
00006  * OpenTTD 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.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "base_media_base.h"
00015 #include "music/music_driver.hpp"
00016 #include "window_gui.h"
00017 #include "strings_func.h"
00018 #include "window_func.h"
00019 #include "sound_func.h"
00020 #include "gfx_func.h"
00021 #include "core/random_func.hpp"
00022 #include "gui.h"
00023 #include "core/geometry_func.hpp"
00024 #include "string_func.h"
00025 #include "settings_type.h"
00026 
00027 #include "table/strings.h"
00028 #include "table/sprites.h"
00029 
00035 static const char *GetSongName(int index)
00036 {
00037   return BaseMusic::GetUsedSet()->song_name[index];
00038 }
00039 
00045 static int GetTrackNumber(int index)
00046 {
00047   return BaseMusic::GetUsedSet()->track_nr[index];
00048 }
00049 
00051 static byte _music_wnd_cursong = 1;
00053 static bool _song_is_active = false;
00054 
00056 static byte _cur_playlist[NUM_SONGS_PLAYLIST + 1];
00057 
00059 static byte _playlist_all[NUM_SONGS_AVAILABLE + 1];
00061 static byte _playlist_old_style[NUM_SONGS_CLASS + 1];
00063 static byte _playlist_new_style[NUM_SONGS_CLASS + 1];
00065 static byte _playlist_ezy_street[NUM_SONGS_CLASS + 1];
00066 
00067 assert_compile(lengthof(_settings_client.music.custom_1) == NUM_SONGS_PLAYLIST + 1);
00068 assert_compile(lengthof(_settings_client.music.custom_2) == NUM_SONGS_PLAYLIST + 1);
00069 
00071 static byte * const _playlists[] = {
00072   _playlist_all,
00073   _playlist_old_style,
00074   _playlist_new_style,
00075   _playlist_ezy_street,
00076   _settings_client.music.custom_1,
00077   _settings_client.music.custom_2,
00078 };
00079 
00084 void ValidatePlaylist(byte *playlist)
00085 {
00086   while (*playlist != 0) {
00087     if (*playlist <= BaseMusic::GetUsedSet()->num_available) {
00088       playlist++;
00089       continue;
00090     }
00091     for (byte *p = playlist; *p != 0; p++) {
00092       p[0] = p[1];
00093     }
00094   }
00095 }
00096 
00098 void InitializeMusic()
00099 {
00100   uint j = 0;
00101   for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00102     if (StrEmpty(GetSongName(i))) continue;
00103     _playlist_all[j++] = i + 1;
00104   }
00105   /* Terminate the list */
00106   _playlist_all[j] = 0;
00107 
00108   /* Now make the 'styled' playlists */
00109   for (uint k = 0; k < NUM_SONG_CLASSES; k++) {
00110     j = 0;
00111     for (uint i = 0; i < NUM_SONGS_CLASS; i++) {
00112       int id = k * NUM_SONGS_CLASS + i + 1;
00113       if (StrEmpty(GetSongName(id))) continue;
00114       _playlists[k + 1][j++] = id + 1;
00115     }
00116     /* Terminate the list */
00117     _playlists[k + 1][j] = 0;
00118   }
00119 
00120   ValidatePlaylist(_settings_client.music.custom_1);
00121   ValidatePlaylist(_settings_client.music.custom_2);
00122 
00123   if (BaseMusic::GetUsedSet()->num_available < _music_wnd_cursong) {
00124     /* If there are less songs than the currently played song,
00125      * just pause and reset to no song. */
00126     _music_wnd_cursong = 0;
00127     _song_is_active = false;
00128   }
00129 }
00130 
00131 static void SkipToPrevSong()
00132 {
00133   byte *b = _cur_playlist;
00134   byte *p = b;
00135   byte t;
00136 
00137   if (b[0] == 0) return; // empty playlist
00138 
00139   do p++; while (p[0] != 0); // find the end
00140 
00141   t = *--p; // and copy the bytes
00142   while (p != b) {
00143     p--;
00144     p[1] = p[0];
00145   }
00146   *b = t;
00147 
00148   _song_is_active = false;
00149 }
00150 
00151 static void SkipToNextSong()
00152 {
00153   byte *b = _cur_playlist;
00154   byte t;
00155 
00156   t = b[0];
00157   if (t != 0) {
00158     while (b[1] != 0) {
00159       b[0] = b[1];
00160       b++;
00161     }
00162     b[0] = t;
00163   }
00164 
00165   _song_is_active = false;
00166 }
00167 
00168 static void MusicVolumeChanged(byte new_vol)
00169 {
00170   _music_driver->SetVolume(new_vol);
00171 }
00172 
00173 static void DoPlaySong()
00174 {
00175   char filename[MAX_PATH];
00176   FioFindFullPath(filename, lengthof(filename), GM_DIR,
00177       BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename);
00178   _music_driver->PlaySong(filename);
00179   SetWindowDirty(WC_MUSIC_WINDOW, 0);
00180 }
00181 
00182 static void DoStopMusic()
00183 {
00184   _music_driver->StopSong();
00185   SetWindowDirty(WC_MUSIC_WINDOW, 0);
00186 }
00187 
00188 static void SelectSongToPlay()
00189 {
00190   uint i = 0;
00191   uint j = 0;
00192 
00193   memset(_cur_playlist, 0, sizeof(_cur_playlist));
00194   do {
00195     const char *filename = BaseMusic::GetUsedSet()->files[_playlists[_settings_client.music.playlist][i] - 1].filename;
00196     /* We are now checking for the existence of that file prior
00197      * to add it to the list of available songs */
00198     if (!StrEmpty(filename) && FioCheckFileExists(filename, GM_DIR)) {
00199       _cur_playlist[j] = _playlists[_settings_client.music.playlist][i];
00200       j++;
00201     }
00202   } while (_playlists[_settings_client.music.playlist][++i] != 0 && j < lengthof(_cur_playlist) - 1);
00203 
00204   /* Do not shuffle when on the intro-start window, as the song to play has to be the original TTD Theme*/
00205   if (_settings_client.music.shuffle && _game_mode != GM_MENU) {
00206     i = 500;
00207     do {
00208       uint32 r = InteractiveRandom();
00209       byte *a = &_cur_playlist[GB(r, 0, 5)];
00210       byte *b = &_cur_playlist[GB(r, 8, 5)];
00211 
00212       if (*a != 0 && *b != 0) {
00213         byte t = *a;
00214         *a = *b;
00215         *b = t;
00216       }
00217     } while (--i);
00218   }
00219 }
00220 
00221 static void StopMusic()
00222 {
00223   _music_wnd_cursong = 0;
00224   DoStopMusic();
00225   _song_is_active = false;
00226   SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
00227 }
00228 
00229 static void PlayPlaylistSong()
00230 {
00231   if (_cur_playlist[0] == 0) {
00232     SelectSongToPlay();
00233     /* if there is not songs in the playlist, it may indicate
00234      * no file on the gm folder, or even no gm folder.
00235      * Stop the playback, then */
00236     if (_cur_playlist[0] == 0) {
00237       _song_is_active = false;
00238       _music_wnd_cursong = 0;
00239       _settings_client.music.playing = false;
00240       return;
00241     }
00242   }
00243   _music_wnd_cursong = _cur_playlist[0];
00244   DoPlaySong();
00245   _song_is_active = true;
00246 
00247   SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
00248 }
00249 
00250 void ResetMusic()
00251 {
00252   _music_wnd_cursong = 1;
00253   DoPlaySong();
00254 }
00255 
00256 void MusicLoop()
00257 {
00258   if (!_settings_client.music.playing && _song_is_active) {
00259     StopMusic();
00260   } else if (_settings_client.music.playing && !_song_is_active) {
00261     PlayPlaylistSong();
00262   }
00263 
00264   if (!_song_is_active) return;
00265 
00266   if (!_music_driver->IsSongPlaying()) {
00267     if (_game_mode != GM_MENU) {
00268       StopMusic();
00269       SkipToNextSong();
00270       PlayPlaylistSong();
00271     } else {
00272       ResetMusic();
00273     }
00274   }
00275 }
00276 
00277 static void SelectPlaylist(byte list)
00278 {
00279   _settings_client.music.playlist = list;
00280   InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
00281   InvalidateWindowData(WC_MUSIC_WINDOW, 0);
00282 }
00283 
00284 enum MusicTrackSelectionWidgets {
00285   MTSW_LIST_LEFT,
00286   MTSW_PLAYLIST,
00287   MTSW_LIST_RIGHT,
00288   MTSW_ALL,
00289   MTSW_OLD,
00290   MTSW_NEW,
00291   MTSW_EZY,
00292   MTSW_CUSTOM1,
00293   MTSW_CUSTOM2,
00294   MTSW_CLEAR,
00295 };
00296 
00297 struct MusicTrackSelectionWindow : public Window {
00298   MusicTrackSelectionWindow(const WindowDesc *desc, WindowNumber number) : Window()
00299   {
00300     this->InitNested(desc, number);
00301     this->LowerWidget(MTSW_LIST_LEFT);
00302     this->LowerWidget(MTSW_LIST_RIGHT);
00303     this->SetWidgetDisabledState(MTSW_CLEAR, _settings_client.music.playlist <= 3);
00304     this->LowerWidget(MTSW_ALL + _settings_client.music.playlist);
00305   }
00306 
00307   virtual void SetStringParameters(int widget) const
00308   {
00309     switch (widget) {
00310       case MTSW_PLAYLIST:
00311         SetDParam(0, STR_MUSIC_PLAYLIST_ALL + _settings_client.music.playlist);
00312         break;
00313     }
00314   }
00315 
00321   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00322   {
00323     if (!gui_scope) return;
00324     for (int i = 0; i < 6; i++) {
00325       this->SetWidgetLoweredState(MTSW_ALL + i, i == _settings_client.music.playlist);
00326     }
00327     this->SetWidgetDisabledState(MTSW_CLEAR, _settings_client.music.playlist <= 3);
00328     this->SetDirty();
00329   }
00330 
00331   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00332   {
00333     switch (widget) {
00334       case MTSW_PLAYLIST: {
00335         Dimension d = {0, 0};
00336 
00337         for (int i = 0; i < 6; i++) {
00338           SetDParam(0, STR_MUSIC_PLAYLIST_ALL + i);
00339           d = maxdim(d, GetStringBoundingBox(STR_PLAYLIST_PROGRAM));
00340         }
00341         d.width += padding.width;
00342         d.height += padding.height;
00343         *size = maxdim(*size, d);
00344         break;
00345       }
00346 
00347       case MTSW_LIST_LEFT: case MTSW_LIST_RIGHT: {
00348         Dimension d = {0, 0};
00349 
00350         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00351           const char *song_name = GetSongName(i);
00352           if (StrEmpty(song_name)) continue;
00353 
00354           SetDParam(0, GetTrackNumber(i));
00355           SetDParam(1, 2);
00356           SetDParamStr(2, GetSongName(i));
00357           Dimension d2 = GetStringBoundingBox(STR_PLAYLIST_TRACK_NAME);
00358           d.width = max(d.width, d2.width);
00359           d.height += d2.height;
00360         }
00361         d.width += padding.width;
00362         d.height += padding.height;
00363         *size = maxdim(*size, d);
00364         break;
00365       }
00366     }
00367   }
00368 
00369   virtual void DrawWidget(const Rect &r, int widget) const
00370   {
00371     switch (widget) {
00372       case MTSW_LIST_LEFT: {
00373         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
00374 
00375         int y = r.top + WD_FRAMERECT_TOP;
00376         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00377           const char *song_name = GetSongName(i);
00378           if (StrEmpty(song_name)) continue;
00379 
00380           SetDParam(0, GetTrackNumber(i));
00381           SetDParam(1, 2);
00382           SetDParamStr(2, song_name);
00383           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
00384           y += FONT_HEIGHT_SMALL;
00385         }
00386         break;
00387       }
00388 
00389       case MTSW_LIST_RIGHT: {
00390         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
00391 
00392         int y = r.top + WD_FRAMERECT_TOP;
00393         for (const byte *p = _playlists[_settings_client.music.playlist]; *p != 0; p++) {
00394           uint i = *p - 1;
00395           SetDParam(0, GetTrackNumber(i));
00396           SetDParam(1, 2);
00397           SetDParamStr(2, GetSongName(i));
00398           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
00399           y += FONT_HEIGHT_SMALL;
00400         }
00401         break;
00402       }
00403     }
00404   }
00405 
00406   virtual void OnClick(Point pt, int widget, int click_count)
00407   {
00408     switch (widget) {
00409       case MTSW_LIST_LEFT: { // add to playlist
00410         int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
00411 
00412         if (_settings_client.music.playlist < 4) return;
00413         if (!IsInsideMM(y, 0, BaseMusic::GetUsedSet()->num_available)) return;
00414 
00415         byte *p = _playlists[_settings_client.music.playlist];
00416         for (uint i = 0; i != NUM_SONGS_PLAYLIST - 1; i++) {
00417           if (p[i] == 0) {
00418             /* Find the actual song number */
00419             for (uint j = 0; j < NUM_SONGS_AVAILABLE; j++) {
00420               if (GetTrackNumber(j) == y + 1) {
00421                 p[i] = j + 1;
00422                 break;
00423               }
00424             }
00425             p[i + 1] = 0;
00426             this->SetDirty();
00427             SelectSongToPlay();
00428             break;
00429           }
00430         }
00431         break;
00432       }
00433 
00434       case MTSW_LIST_RIGHT: { // remove from playlist
00435         int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
00436 
00437         if (_settings_client.music.playlist < 4) return;
00438         if (!IsInsideMM(y, 0, NUM_SONGS_PLAYLIST)) return;
00439 
00440         byte *p = _playlists[_settings_client.music.playlist];
00441         for (uint i = y; i != NUM_SONGS_PLAYLIST - 1; i++) {
00442           p[i] = p[i + 1];
00443         }
00444 
00445         this->SetDirty();
00446         SelectSongToPlay();
00447         break;
00448       }
00449 
00450       case MTSW_CLEAR: // clear
00451         for (uint i = 0; _playlists[_settings_client.music.playlist][i] != 0; i++) _playlists[_settings_client.music.playlist][i] = 0;
00452         this->SetDirty();
00453         StopMusic();
00454         SelectSongToPlay();
00455         break;
00456 
00457       case MTSW_ALL: case MTSW_OLD: case MTSW_NEW:
00458       case MTSW_EZY: case MTSW_CUSTOM1: case MTSW_CUSTOM2: // set playlist
00459         SelectPlaylist(widget - MTSW_ALL);
00460         StopMusic();
00461         SelectSongToPlay();
00462         break;
00463     }
00464   }
00465 };
00466 
00467 static const NWidgetPart _nested_music_track_selection_widgets[] = {
00468   NWidget(NWID_HORIZONTAL),
00469     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00470     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PLAYLIST_MUSIC_PROGRAM_SELECTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00471   EndContainer(),
00472   NWidget(WWT_PANEL, COLOUR_GREY),
00473     NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
00474       /* Left panel. */
00475       NWidget(NWID_VERTICAL),
00476         NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_PLAYLIST_TRACK_INDEX, STR_NULL),
00477         NWidget(WWT_PANEL, COLOUR_GREY, MTSW_LIST_LEFT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK), EndContainer(),
00478         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00479       EndContainer(),
00480       /* Middle buttons. */
00481       NWidget(NWID_VERTICAL),
00482         NWidget(NWID_SPACER), SetMinimalSize(60, 30), // Space above the first button from the title bar.
00483         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
00484         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
00485         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
00486         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
00487         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
00488         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
00489         NWidget(NWID_SPACER), SetMinimalSize(0, 16), // Space above 'clear' button
00490         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, MTSW_CLEAR), SetFill(1, 0), SetDataTip(STR_PLAYLIST_CLEAR, STR_PLAYLIST_TOOLTIP_CLEAR_CURRENT_PROGRAM_CUSTOM1),
00491         NWidget(NWID_SPACER), SetFill(0, 1),
00492       EndContainer(),
00493       /* Right panel. */
00494       NWidget(NWID_VERTICAL),
00495         NWidget(WWT_LABEL, COLOUR_GREY, MTSW_PLAYLIST), SetDataTip(STR_PLAYLIST_PROGRAM, STR_NULL),
00496         NWidget(WWT_PANEL, COLOUR_GREY, MTSW_LIST_RIGHT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK), EndContainer(),
00497         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00498       EndContainer(),
00499     EndContainer(),
00500   EndContainer(),
00501 };
00502 
00503 static const WindowDesc _music_track_selection_desc(
00504   WDP_AUTO, 0, 0,
00505   WC_MUSIC_TRACK_SELECTION, WC_NONE,
00506   WDF_UNCLICK_BUTTONS,
00507   _nested_music_track_selection_widgets, lengthof(_nested_music_track_selection_widgets)
00508 );
00509 
00510 static void ShowMusicTrackSelection()
00511 {
00512   AllocateWindowDescFront<MusicTrackSelectionWindow>(&_music_track_selection_desc, 0);
00513 }
00514 
00515 enum MusicWidgets {
00516   MW_PREV,
00517   MW_NEXT,
00518   MW_STOP,
00519   MW_PLAY,
00520   MW_SLIDERS,
00521   MW_MUSIC_VOL,
00522   MW_GAUGE,
00523   MW_EFFECT_VOL,
00524   MW_BACKGROUND,
00525   MW_TRACK,
00526   MW_TRACK_NR,
00527   MW_TRACK_TITLE,
00528   MW_TRACK_NAME,
00529   MW_SHUFFLE,
00530   MW_PROGRAMME,
00531   MW_ALL,
00532   MW_OLD,
00533   MW_NEW,
00534   MW_EZY,
00535   MW_CUSTOM1,
00536   MW_CUSTOM2,
00537 };
00538 
00539 struct MusicWindow : public Window {
00540   static const int slider_width = 3;
00541 
00542   MusicWindow(const WindowDesc *desc, WindowNumber number) : Window()
00543   {
00544     this->InitNested(desc, number);
00545     this->LowerWidget(_settings_client.music.playlist + MW_ALL);
00546     this->SetWidgetLoweredState(MW_SHUFFLE, _settings_client.music.shuffle);
00547   }
00548 
00549   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00550   {
00551     switch (widget) {
00552       /* Make sure that MW_SHUFFLE and MW_PROGRAMME have the same size.
00553        * This can't be done by using NC_EQUALSIZE as the MW_INFO is
00554        * between those widgets and of different size. */
00555       case MW_SHUFFLE: case MW_PROGRAMME: {
00556         Dimension d = maxdim(GetStringBoundingBox(STR_MUSIC_PROGRAM), GetStringBoundingBox(STR_MUSIC_SHUFFLE));
00557         d.width += padding.width;
00558         d.height += padding.height;
00559         *size = maxdim(*size, d);
00560         break;
00561       }
00562 
00563       case MW_TRACK_NR: {
00564         Dimension d = GetStringBoundingBox(STR_MUSIC_TRACK_NONE);
00565         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00566         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00567         *size = maxdim(*size, d);
00568         break;
00569       }
00570 
00571       case MW_TRACK_NAME: {
00572         Dimension d = GetStringBoundingBox(STR_MUSIC_TITLE_NONE);
00573         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00574           SetDParamStr(0, GetSongName(i));
00575           d = maxdim(d, GetStringBoundingBox(STR_MUSIC_TITLE_NAME));
00576         }
00577         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00578         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00579         *size = maxdim(*size, d);
00580         break;
00581       }
00582 
00583       /* Hack-ish: set the proper widget data; only needs to be done once
00584        * per (Re)Init as that's the only time the language changes. */
00585       case MW_PREV: this->GetWidget<NWidgetCore>(MW_PREV)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_NEXT : SPR_IMG_SKIP_TO_PREV; break;
00586       case MW_NEXT: this->GetWidget<NWidgetCore>(MW_NEXT)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_PREV : SPR_IMG_SKIP_TO_NEXT; break;
00587       case MW_PLAY: this->GetWidget<NWidgetCore>(MW_PLAY)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_PLAY_MUSIC_RTL : SPR_IMG_PLAY_MUSIC; break;
00588     }
00589   }
00590 
00591   virtual void DrawWidget(const Rect &r, int widget) const
00592   {
00593     switch (widget) {
00594       case MW_GAUGE:
00595         GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
00596 
00597         for (uint i = 0; i != 8; i++) {
00598           int colour = PC_GREEN;
00599           if (i > 4) {
00600             colour = PC_YELLOW;
00601             if (i > 6) {
00602               colour = PC_RED;
00603             }
00604           }
00605           GfxFillRect(r.left, r.bottom - i * 2, r.right, r.bottom - i * 2, colour);
00606         }
00607         break;
00608 
00609       case MW_TRACK_NR: {
00610         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
00611         StringID str = STR_MUSIC_TRACK_NONE;
00612         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
00613           SetDParam(0, GetTrackNumber(_music_wnd_cursong - 1));
00614           SetDParam(1, 2);
00615           str = STR_MUSIC_TRACK_DIGIT;
00616         }
00617         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str);
00618         break;
00619       }
00620 
00621       case MW_TRACK_NAME: {
00622         GfxFillRect(r.left, r.top + 1, r.right - 1, r.bottom, PC_BLACK);
00623         StringID str = STR_MUSIC_TITLE_NONE;
00624         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
00625           str = STR_MUSIC_TITLE_NAME;
00626           SetDParamStr(0, GetSongName(_music_wnd_cursong - 1));
00627         }
00628         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
00629         break;
00630       }
00631 
00632       case MW_MUSIC_VOL: case MW_EFFECT_VOL: {
00633         DrawFrameRect(r.left, r.top + 2, r.right, r.bottom - 2, COLOUR_GREY, FR_LOWERED);
00634         byte volume = (widget == MW_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
00635         int x = (volume * (r.right - r.left) / 127);
00636         if (_current_text_dir == TD_RTL) {
00637           x = r.right - x;
00638         } else {
00639           x += r.left;
00640         }
00641         DrawFrameRect(x, r.top, x + slider_width, r.bottom, COLOUR_GREY, FR_NONE);
00642         break;
00643       }
00644     }
00645   }
00646 
00652   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00653   {
00654     if (!gui_scope) return;
00655     for (int i = 0; i < 6; i++) {
00656       this->SetWidgetLoweredState(MW_ALL + i, i == _settings_client.music.playlist);
00657     }
00658     this->SetDirty();
00659   }
00660 
00661   virtual void OnClick(Point pt, int widget, int click_count)
00662   {
00663     switch (widget) {
00664       case MW_PREV: // skip to prev
00665         if (!_song_is_active) return;
00666         SkipToPrevSong();
00667         this->SetDirty();
00668         break;
00669 
00670       case MW_NEXT: // skip to next
00671         if (!_song_is_active) return;
00672         SkipToNextSong();
00673         this->SetDirty();
00674         break;
00675 
00676       case MW_STOP: // stop playing
00677         _settings_client.music.playing = false;
00678         break;
00679 
00680       case MW_PLAY: // start playing
00681         _settings_client.music.playing = true;
00682         break;
00683 
00684       case MW_MUSIC_VOL: case MW_EFFECT_VOL: { // volume sliders
00685         int x = pt.x - this->GetWidget<NWidgetBase>(widget)->pos_x;
00686 
00687         byte *vol = (widget == MW_MUSIC_VOL) ? &_settings_client.music.music_vol : &_settings_client.music.effect_vol;
00688 
00689         byte new_vol = x * 127 / this->GetWidget<NWidgetBase>(widget)->current_x;
00690         if (_current_text_dir == TD_RTL) new_vol = 127 - new_vol;
00691         if (new_vol != *vol) {
00692           *vol = new_vol;
00693           if (widget == MW_MUSIC_VOL) MusicVolumeChanged(new_vol);
00694           this->SetDirty();
00695         }
00696 
00697         _left_button_clicked = false;
00698         break;
00699       }
00700 
00701       case MW_SHUFFLE: // toggle shuffle
00702         _settings_client.music.shuffle ^= 1;
00703         this->SetWidgetLoweredState(MW_SHUFFLE, _settings_client.music.shuffle);
00704         this->SetWidgetDirty(MW_SHUFFLE);
00705         StopMusic();
00706         SelectSongToPlay();
00707         this->SetDirty();
00708         break;
00709 
00710       case MW_PROGRAMME: // show track selection
00711         ShowMusicTrackSelection();
00712         break;
00713 
00714       case MW_ALL: case MW_OLD: case MW_NEW:
00715       case MW_EZY: case MW_CUSTOM1: case MW_CUSTOM2: // playlist
00716         SelectPlaylist(widget - MW_ALL);
00717         StopMusic();
00718         SelectSongToPlay();
00719         this->SetDirty();
00720         break;
00721     }
00722   }
00723 };
00724 
00725 static const NWidgetPart _nested_music_window_widgets[] = {
00726   NWidget(NWID_HORIZONTAL),
00727     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00728     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MUSIC_JAZZ_JUKEBOX_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00729   EndContainer(),
00730 
00731   NWidget(NWID_HORIZONTAL),
00732     NWidget(NWID_VERTICAL),
00733       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
00734       NWidget(NWID_HORIZONTAL),
00735         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_PREV), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_PREV, STR_MUSIC_TOOLTIP_SKIP_TO_PREVIOUS_TRACK),
00736         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_NEXT), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_NEXT, STR_MUSIC_TOOLTIP_SKIP_TO_NEXT_TRACK_IN_SELECTION),
00737         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_STOP), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_STOP_MUSIC, STR_MUSIC_TOOLTIP_STOP_PLAYING_MUSIC),
00738         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_PLAY), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_PLAY_MUSIC, STR_MUSIC_TOOLTIP_START_PLAYING_MUSIC),
00739       EndContainer(),
00740       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
00741     EndContainer(),
00742     NWidget(WWT_PANEL, COLOUR_GREY, MW_SLIDERS),
00743       NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
00744         NWidget(NWID_VERTICAL),
00745           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_MUSIC_VOLUME, STR_NULL),
00746           NWidget(WWT_EMPTY, COLOUR_GREY, MW_MUSIC_VOL), SetMinimalSize(67, 0), SetMinimalTextLines(1, 0), SetFill(1, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
00747           NWidget(NWID_HORIZONTAL),
00748             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
00749             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00750             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00751             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00752             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00753             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00754             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
00755           EndContainer(),
00756         EndContainer(),
00757         NWidget(WWT_PANEL, COLOUR_GREY, MW_GAUGE), SetMinimalSize(16, 20), SetPadding(1, 11, 1, 11), SetFill(0, 0), EndContainer(),
00758         NWidget(NWID_VERTICAL),
00759           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_EFFECTS_VOLUME, STR_NULL),
00760           NWidget(WWT_EMPTY, COLOUR_GREY, MW_EFFECT_VOL), SetMinimalSize(67, 0), SetMinimalTextLines(1, 0), SetFill(1, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
00761           NWidget(NWID_HORIZONTAL),
00762             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
00763             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00764             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00765             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00766             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00767             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00768             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
00769           EndContainer(),
00770         EndContainer(),
00771       EndContainer(),
00772     EndContainer(),
00773   EndContainer(),
00774   NWidget(WWT_PANEL, COLOUR_GREY, MW_BACKGROUND),
00775     NWidget(NWID_HORIZONTAL), SetPIP(6, 0, 6),
00776       NWidget(NWID_VERTICAL),
00777         NWidget(NWID_SPACER), SetFill(0, 1),
00778         NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_SHUFFLE), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_SHUFFLE, STR_MUSIC_TOOLTIP_TOGGLE_PROGRAM_SHUFFLE),
00779         NWidget(NWID_SPACER), SetFill(0, 1),
00780       EndContainer(),
00781       NWidget(NWID_VERTICAL), SetPadding(0, 0, 3, 3),
00782         NWidget(WWT_LABEL, COLOUR_GREY, MW_TRACK), SetFill(0, 0), SetDataTip(STR_MUSIC_TRACK, STR_NULL),
00783         NWidget(WWT_PANEL, COLOUR_GREY, MW_TRACK_NR), EndContainer(),
00784       EndContainer(),
00785       NWidget(NWID_VERTICAL), SetPadding(0, 3, 3, 0),
00786         NWidget(WWT_LABEL, COLOUR_GREY, MW_TRACK_TITLE), SetFill(1, 0), SetDataTip(STR_MUSIC_XTITLE, STR_NULL),
00787         NWidget(WWT_PANEL, COLOUR_GREY, MW_TRACK_NAME), SetFill(1, 0), EndContainer(),
00788       EndContainer(),
00789       NWidget(NWID_VERTICAL),
00790         NWidget(NWID_SPACER), SetFill(0, 1),
00791         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, MW_PROGRAMME), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_PROGRAM, STR_MUSIC_TOOLTIP_SHOW_MUSIC_TRACK_SELECTION),
00792         NWidget(NWID_SPACER), SetFill(0, 1),
00793       EndContainer(),
00794     EndContainer(),
00795   EndContainer(),
00796   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00797     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
00798     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
00799     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
00800     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
00801     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
00802     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
00803   EndContainer(),
00804 };
00805 
00806 static const WindowDesc _music_window_desc(
00807   WDP_AUTO, 0, 0,
00808   WC_MUSIC_WINDOW, WC_NONE,
00809   WDF_UNCLICK_BUTTONS,
00810   _nested_music_window_widgets, lengthof(_nested_music_window_widgets)
00811 );
00812 
00813 void ShowMusicWindow()
00814 {
00815   if (BaseMusic::GetUsedSet()->num_available == 0) ShowErrorMessage(STR_ERROR_NO_SONGS, INVALID_STRING_ID, WL_WARNING);
00816   AllocateWindowDescFront<MusicWindow>(&_music_window_desc, 0);
00817 }

Generated on Fri Jun 3 05:18:52 2011 for OpenTTD by  doxygen 1.6.1