music_gui.cpp

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

Generated on Thu Jan 20 22:57:35 2011 for OpenTTD by  doxygen 1.6.1