00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "fileio.h"
00008 #include "variables.h"
00009 #include "music.h"
00010 #include "music/music_driver.hpp"
00011 #include "window_gui.h"
00012 #include "strings_func.h"
00013 #include "window_func.h"
00014 #include "sound_func.h"
00015 #include "gfx_func.h"
00016 #include "core/math_func.hpp"
00017 #include "core/random_func.hpp"
00018
00019 #include "table/strings.h"
00020 #include "table/sprites.h"
00021
00022 static byte _music_wnd_cursong;
00023 static bool _song_is_active;
00024 static byte _cur_playlist[NUM_SONGS_PLAYLIST];
00025
00026
00027
00028 static byte _playlist_all[] = {
00029 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 0
00030 };
00031
00032 static byte _playlist_old_style[] = {
00033 1, 8, 2, 9, 14, 15, 19, 13, 0
00034 };
00035
00036 static byte _playlist_new_style[] = {
00037 6, 11, 10, 17, 21, 18, 5, 0
00038 };
00039
00040 static byte _playlist_ezy_street[] = {
00041 12, 7, 16, 3, 20, 4, 0
00042 };
00043
00044 static byte * const _playlists[] = {
00045 _playlist_all,
00046 _playlist_old_style,
00047 _playlist_new_style,
00048 _playlist_ezy_street,
00049 msf.custom_1,
00050 msf.custom_2,
00051 };
00052
00053 static void SkipToPrevSong()
00054 {
00055 byte *b = _cur_playlist;
00056 byte *p = b;
00057 byte t;
00058
00059 if (b[0] == 0) return;
00060
00061 do p++; while (p[0] != 0);
00062
00063 t = *--p;
00064 while (p != b) {
00065 p--;
00066 p[1] = p[0];
00067 }
00068 *b = t;
00069
00070 _song_is_active = false;
00071 }
00072
00073 static void SkipToNextSong()
00074 {
00075 byte* b = _cur_playlist;
00076 byte t;
00077
00078 t = b[0];
00079 if (t != 0) {
00080 while (b[1] != 0) {
00081 b[0] = b[1];
00082 b++;
00083 }
00084 b[0] = t;
00085 }
00086
00087 _song_is_active = false;
00088 }
00089
00090 static void MusicVolumeChanged(byte new_vol)
00091 {
00092 _music_driver->SetVolume(new_vol);
00093 }
00094
00095 static void DoPlaySong()
00096 {
00097 char filename[MAX_PATH];
00098 FioFindFullPath(filename, lengthof(filename), GM_DIR,
00099 origin_songs_specs[_music_wnd_cursong - 1].filename);
00100 _music_driver->PlaySong(filename);
00101 }
00102
00103 static void DoStopMusic()
00104 {
00105 _music_driver->StopSong();
00106 }
00107
00108 static void SelectSongToPlay()
00109 {
00110 uint i = 0;
00111 uint j = 0;
00112
00113 memset(_cur_playlist, 0, sizeof(_cur_playlist));
00114 do {
00115
00116
00117 if (FioCheckFileExists(origin_songs_specs[_playlists[msf.playlist][i]].filename, GM_DIR)) {
00118 _cur_playlist[j] = _playlists[msf.playlist][i];
00119 j++;
00120 }
00121 } while (_playlists[msf.playlist][i++] != 0 && i < lengthof(_cur_playlist) - 1);
00122
00123
00124 if (msf.shuffle && _game_mode != GM_MENU) {
00125 i = 500;
00126 do {
00127 uint32 r = InteractiveRandom();
00128 byte *a = &_cur_playlist[GB(r, 0, 5)];
00129 byte *b = &_cur_playlist[GB(r, 8, 5)];
00130
00131 if (*a != 0 && *b != 0) {
00132 byte t = *a;
00133 *a = *b;
00134 *b = t;
00135 }
00136 } while (--i);
00137 }
00138 }
00139
00140 static void StopMusic()
00141 {
00142 _music_wnd_cursong = 0;
00143 DoStopMusic();
00144 _song_is_active = false;
00145 InvalidateWindowWidget(WC_MUSIC_WINDOW, 0, 9);
00146 }
00147
00148 static void PlayPlaylistSong()
00149 {
00150 if (_cur_playlist[0] == 0) {
00151 SelectSongToPlay();
00152
00153
00154
00155 if (_cur_playlist[0] == 0) {
00156 _song_is_active = false;
00157 _music_wnd_cursong = 0;
00158 msf.playing = false;
00159 return;
00160 }
00161 }
00162 _music_wnd_cursong = _cur_playlist[0];
00163 DoPlaySong();
00164 _song_is_active = true;
00165
00166 InvalidateWindowWidget(WC_MUSIC_WINDOW, 0, 9);
00167 }
00168
00169 void ResetMusic()
00170 {
00171 _music_wnd_cursong = 1;
00172 DoPlaySong();
00173 }
00174
00175 void MusicLoop()
00176 {
00177 if (!msf.playing && _song_is_active) {
00178 StopMusic();
00179 } else if (msf.playing && !_song_is_active) {
00180 PlayPlaylistSong();
00181 }
00182
00183 if (!_song_is_active) return;
00184
00185 if (!_music_driver->IsSongPlaying()) {
00186 if (_game_mode != GM_MENU) {
00187 StopMusic();
00188 SkipToNextSong();
00189 PlayPlaylistSong();
00190 } else {
00191 ResetMusic();
00192 }
00193 }
00194 }
00195
00196 static void MusicTrackSelectionWndProc(Window *w, WindowEvent *e)
00197 {
00198 switch (e->event) {
00199 case WE_PAINT: {
00200 const byte* p;
00201 uint i;
00202 int y;
00203
00204 w->SetWidgetDisabledState(11, msf.playlist <= 3);
00205 w->LowerWidget(3);
00206 w->LowerWidget(4);
00207 DrawWindowWidgets(w);
00208
00209 GfxFillRect(3, 23, 3 + 177, 23 + 191, 0);
00210 GfxFillRect(251, 23, 251 + 177, 23 + 191, 0);
00211
00212 DrawStringCentered(92, 15, STR_01EE_TRACK_INDEX, TC_FROMSTRING);
00213
00214 SetDParam(0, STR_01D5_ALL + msf.playlist);
00215 DrawStringCentered(340, 15, STR_01EF_PROGRAM, TC_FROMSTRING);
00216
00217 for (i = 1; i <= NUM_SONGS_AVAILABLE; i++) {
00218 SetDParam(0, i);
00219 SetDParam(2, i);
00220 SetDParam(1, SPECSTR_SONGNAME);
00221 DrawString(4, 23 + (i - 1) * 6, (i < 10) ? STR_01EC_0 : STR_01ED, TC_FROMSTRING);
00222 }
00223
00224 for (i = 0; i != 6; i++) {
00225 DrawStringCentered(216, 45 + i * 8, STR_01D5_ALL + i, (i == msf.playlist) ? TC_WHITE : TC_BLACK);
00226 }
00227
00228 DrawStringCentered(216, 45 + 8 * 6 + 16, STR_01F0_CLEAR, TC_FROMSTRING);
00229 #if 0
00230 DrawStringCentered(216, 45 + 8 * 6 + 16 * 2, STR_01F1_SAVE, TC_FROMSTRING);
00231 #endif
00232
00233 y = 23;
00234 for (p = _playlists[msf.playlist], i = 0; (i = *p) != 0; p++) {
00235 SetDParam(0, i);
00236 SetDParam(1, SPECSTR_SONGNAME);
00237 SetDParam(2, i);
00238 DrawString(252, y, (i < 10) ? STR_01EC_0 : STR_01ED, TC_FROMSTRING);
00239 y += 6;
00240 }
00241 break;
00242 }
00243
00244 case WE_CLICK:
00245 switch (e->we.click.widget) {
00246 case 3: {
00247 int y = (e->we.click.pt.y - 23) / 6;
00248 uint i;
00249 byte *p;
00250
00251 if (msf.playlist < 4) return;
00252 if (!IsInsideMM(y, 0, NUM_SONGS_AVAILABLE)) return;
00253
00254 p = _playlists[msf.playlist];
00255 for (i = 0; i != NUM_SONGS_PLAYLIST - 1; i++) {
00256 if (p[i] == 0) {
00257 p[i] = y + 1;
00258 p[i + 1] = 0;
00259 SetWindowDirty(w);
00260 SelectSongToPlay();
00261 break;
00262 }
00263 }
00264 } break;
00265
00266 case 4: {
00267 int y = (e->we.click.pt.y - 23) / 6;
00268 uint i;
00269 byte *p;
00270
00271 if (msf.playlist < 4) return;
00272 if (!IsInsideMM(y, 0, NUM_SONGS_AVAILABLE)) return;
00273
00274 p = _playlists[msf.playlist];
00275 for (i = y; i != NUM_SONGS_PLAYLIST - 1; i++) {
00276 p[i] = p[i + 1];
00277 }
00278
00279 SetWindowDirty(w);
00280 SelectSongToPlay();
00281 } break;
00282
00283 case 11:
00284 _playlists[msf.playlist][0] = 0;
00285 SetWindowDirty(w);
00286 StopMusic();
00287 SelectSongToPlay();
00288 break;
00289
00290 #if 0
00291 case 12:
00292 ShowInfo("MusicTrackSelectionWndProc:save not implemented");
00293 break;
00294 #endif
00295
00296 case 5: case 6: case 7: case 8: case 9: case 10:
00297 msf.playlist = e->we.click.widget - 5;
00298 SetWindowDirty(w);
00299 InvalidateWindow(WC_MUSIC_WINDOW, 0);
00300 StopMusic();
00301 SelectSongToPlay();
00302 break;
00303 }
00304 break;
00305 }
00306 }
00307
00308 static const Widget _music_track_selection_widgets[] = {
00309 { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00310 { WWT_CAPTION, RESIZE_NONE, 14, 11, 431, 0, 13, STR_01EB_MUSIC_PROGRAM_SELECTION, STR_018C_WINDOW_TITLE_DRAG_THIS},
00311 { WWT_PANEL, RESIZE_NONE, 14, 0, 431, 14, 217, 0x0, STR_NULL},
00312 { WWT_PANEL, RESIZE_NONE, 14, 2, 181, 22, 215, 0x0, STR_01FA_CLICK_ON_MUSIC_TRACK_TO},
00313 { WWT_PANEL, RESIZE_NONE, 14, 250, 429, 22, 215, 0x0, STR_CLICK_ON_TRACK_TO_REMOVE},
00314 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 44, 51, 0x0, STR_01F3_SELECT_ALL_TRACKS_PROGRAM},
00315 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 52, 59, 0x0, STR_01F4_SELECT_OLD_STYLE_MUSIC},
00316 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 60, 67, 0x0, STR_01F5_SELECT_NEW_STYLE_MUSIC},
00317 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 68, 75, 0x0, STR_0330_SELECT_EZY_STREET_STYLE},
00318 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 76, 83, 0x0, STR_01F6_SELECT_CUSTOM_1_USER_DEFINED},
00319 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 84, 91, 0x0, STR_01F7_SELECT_CUSTOM_2_USER_DEFINED},
00320 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 108, 115, 0x0, STR_01F8_CLEAR_CURRENT_PROGRAM_CUSTOM1},
00321 #if 0
00322 { WWT_PUSHBTN, RESIZE_NONE, 14, 186, 245, 124, 131, 0x0, STR_01F9_SAVE_MUSIC_SETTINGS},
00323 #endif
00324 { WIDGETS_END},
00325 };
00326
00327 static const WindowDesc _music_track_selection_desc = {
00328 104, 131, 432, 218, 432, 218,
00329 WC_MUSIC_TRACK_SELECTION, WC_NONE,
00330 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00331 _music_track_selection_widgets,
00332 MusicTrackSelectionWndProc
00333 };
00334
00335 static void ShowMusicTrackSelection()
00336 {
00337 AllocateWindowDescFront(&_music_track_selection_desc, 0);
00338 }
00339
00340 static void MusicWindowWndProc(Window *w, WindowEvent *e)
00341 {
00342 switch (e->event) {
00343 case WE_PAINT: {
00344 uint i;
00345 StringID str;
00346
00347 w->RaiseWidget(7);
00348 w->RaiseWidget(9);
00349 DrawWindowWidgets(w);
00350
00351 GfxFillRect(187, 16, 200, 33, 0);
00352
00353 for (i = 0; i != 8; i++) {
00354 int color = 0xD0;
00355 if (i > 4) {
00356 color = 0xBF;
00357 if (i > 6) {
00358 color = 0xB8;
00359 }
00360 }
00361 GfxFillRect(187, NUM_SONGS_PLAYLIST - i * 2, 200, NUM_SONGS_PLAYLIST - i * 2, color);
00362 }
00363
00364 GfxFillRect(60, 46, 239, 52, 0);
00365
00366 if (_song_is_active == 0 || _music_wnd_cursong == 0) {
00367 str = STR_01E3;
00368 } else {
00369 SetDParam(0, _music_wnd_cursong);
00370 str = (_music_wnd_cursong < 10) ? STR_01E4_0 : STR_01E5;
00371 }
00372 DrawString(62, 46, str, TC_FROMSTRING);
00373
00374 str = STR_01E6;
00375 if (_song_is_active != 0 && _music_wnd_cursong != 0) {
00376 str = STR_01E7;
00377 SetDParam(0, SPECSTR_SONGNAME);
00378 SetDParam(1, _music_wnd_cursong);
00379 }
00380 DrawStringCentered(155, 46, str, TC_FROMSTRING);
00381
00382
00383 DrawString(60, 38, STR_01E8_TRACK_XTITLE, TC_FROMSTRING);
00384
00385 for (i = 0; i != 6; i++) {
00386 DrawStringCentered(25 + i * 50, 59, STR_01D5_ALL + i, msf.playlist == i ? TC_WHITE : TC_BLACK);
00387 }
00388
00389 DrawStringCentered(31, 43, STR_01E9_SHUFFLE, (msf.shuffle ? TC_WHITE : TC_BLACK));
00390 DrawStringCentered(269, 43, STR_01EA_PROGRAM, TC_FROMSTRING);
00391 DrawStringCentered(141, 15, STR_01DB_MUSIC_VOLUME, TC_FROMSTRING);
00392 DrawStringCentered(141, 29, STR_01DD_MIN_MAX, TC_FROMSTRING);
00393 DrawStringCentered(247, 15, STR_01DC_EFFECTS_VOLUME, TC_FROMSTRING);
00394 DrawStringCentered(247, 29, STR_01DD_MIN_MAX, TC_FROMSTRING);
00395
00396 DrawFrameRect(108, 23, 174, 26, 14, FR_LOWERED);
00397 DrawFrameRect(214, 23, 280, 26, 14, FR_LOWERED);
00398
00399 DrawFrameRect(
00400 108 + msf.music_vol / 2, 22, 111 + msf.music_vol / 2, 28, 14, FR_NONE
00401 );
00402
00403 DrawFrameRect(
00404 214 + msf.effect_vol / 2, 22, 217 + msf.effect_vol / 2, 28, 14, FR_NONE
00405 );
00406 } break;
00407
00408 case WE_CLICK:
00409 switch (e->we.click.widget) {
00410 case 2:
00411 if (!_song_is_active)
00412 return;
00413 SkipToPrevSong();
00414 break;
00415 case 3:
00416 if (!_song_is_active)
00417 return;
00418 SkipToNextSong();
00419 break;
00420 case 4:
00421 msf.playing = false;
00422 break;
00423 case 5:
00424 msf.playing = true;
00425 break;
00426 case 6: {
00427 byte *vol, new_vol;
00428 int x = e->we.click.pt.x - 88;
00429
00430 if (x < 0) return;
00431
00432 vol = &msf.music_vol;
00433 if (x >= 106) {
00434 vol = &msf.effect_vol;
00435 x -= 106;
00436 }
00437
00438 new_vol = min(max(x - 21, 0) * 2, 127);
00439 if (new_vol != *vol) {
00440 *vol = new_vol;
00441 if (vol == &msf.music_vol)
00442 MusicVolumeChanged(new_vol);
00443 SetWindowDirty(w);
00444 }
00445
00446 _left_button_clicked = false;
00447 } break;
00448 case 10:
00449 msf.shuffle ^= 1;
00450 StopMusic();
00451 SelectSongToPlay();
00452 break;
00453 case 11:
00454 ShowMusicTrackSelection();
00455 break;
00456 case 12: case 13: case 14: case 15: case 16: case 17:
00457 msf.playlist = e->we.click.widget - 12;
00458 SetWindowDirty(w);
00459 InvalidateWindow(WC_MUSIC_TRACK_SELECTION, 0);
00460 StopMusic();
00461 SelectSongToPlay();
00462 break;
00463 }
00464 break;
00465
00466 case WE_MOUSELOOP:
00467 InvalidateWindowWidget(WC_MUSIC_WINDOW, 0, 7);
00468 break;
00469 }
00470
00471 }
00472
00473 static const Widget _music_window_widgets[] = {
00474 { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00475 { WWT_CAPTION, RESIZE_NONE, 14, 11, 299, 0, 13, STR_01D2_JAZZ_JUKEBOX, STR_018C_WINDOW_TITLE_DRAG_THIS},
00476 { WWT_PUSHIMGBTN, RESIZE_NONE, 14, 0, 21, 14, 35, SPR_IMG_SKIP_TO_PREV, STR_01DE_SKIP_TO_PREVIOUS_TRACK},
00477 { WWT_PUSHIMGBTN, RESIZE_NONE, 14, 22, 43, 14, 35, SPR_IMG_SKIP_TO_NEXT, STR_01DF_SKIP_TO_NEXT_TRACK_IN_SELECTION},
00478 { WWT_PUSHIMGBTN, RESIZE_NONE, 14, 44, 65, 14, 35, SPR_IMG_STOP_MUSIC, STR_01E0_STOP_PLAYING_MUSIC},
00479 { WWT_PUSHIMGBTN, RESIZE_NONE, 14, 66, 87, 14, 35, SPR_IMG_PLAY_MUSIC, STR_01E1_START_PLAYING_MUSIC},
00480 { WWT_PANEL, RESIZE_NONE, 14, 88, 299, 14, 35, 0x0, STR_01E2_DRAG_SLIDERS_TO_SET_MUSIC},
00481 { WWT_PANEL, RESIZE_NONE, 14, 186, 201, 15, 34, 0x0, STR_NULL},
00482 { WWT_PANEL, RESIZE_NONE, 14, 0, 299, 36, 57, 0x0, STR_NULL},
00483 { WWT_PANEL, RESIZE_NONE, 14, 59, 240, 45, 53, 0x0, STR_NULL},
00484 { WWT_PUSHBTN, RESIZE_NONE, 14, 6, 55, 42, 49, 0x0, STR_01FB_TOGGLE_PROGRAM_SHUFFLE},
00485 { WWT_PUSHBTN, RESIZE_NONE, 14, 244, 293, 42, 49, 0x0, STR_01FC_SHOW_MUSIC_TRACK_SELECTION},
00486 { WWT_PUSHBTN, RESIZE_NONE, 14, 0, 49, 58, 65, 0x0, STR_01F3_SELECT_ALL_TRACKS_PROGRAM},
00487 { WWT_PUSHBTN, RESIZE_NONE, 14, 50, 99, 58, 65, 0x0, STR_01F4_SELECT_OLD_STYLE_MUSIC},
00488 { WWT_PUSHBTN, RESIZE_NONE, 14, 100, 149, 58, 65, 0x0, STR_01F5_SELECT_NEW_STYLE_MUSIC},
00489 { WWT_PUSHBTN, RESIZE_NONE, 14, 150, 199, 58, 65, 0x0, STR_0330_SELECT_EZY_STREET_STYLE},
00490 { WWT_PUSHBTN, RESIZE_NONE, 14, 200, 249, 58, 65, 0x0, STR_01F6_SELECT_CUSTOM_1_USER_DEFINED},
00491 { WWT_PUSHBTN, RESIZE_NONE, 14, 250, 299, 58, 65, 0x0, STR_01F7_SELECT_CUSTOM_2_USER_DEFINED},
00492 { WIDGETS_END},
00493 };
00494
00495 static const WindowDesc _music_window_desc = {
00496 0, 22, 300, 66, 300, 66,
00497 WC_MUSIC_WINDOW, WC_NONE,
00498 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00499 _music_window_widgets,
00500 MusicWindowWndProc
00501 };
00502
00503 void ShowMusicWindow()
00504 {
00505 AllocateWindowDescFront(&_music_window_desc, 0);
00506 }