1 ////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // Nestopia - NES/Famicom emulator written in C++
4 //
5 // Copyright (C) 2003-2008 Martin Freij
6 //
7 // This file is part of Nestopia.
8 //
9 // Nestopia is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Nestopia is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Nestopia; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 //
23 ////////////////////////////////////////////////////////////////////////////////////////
24 
25 #include "NstManager.hpp"
26 #include "NstManagerNsf.hpp"
27 #include "../core/api/NstApiNsf.hpp"
28 
29 namespace Nestopia
30 {
31 	namespace Managers
32 	{
Nsf(Emulator & e,const Configuration & cfg,Window::Menu & m)33 		Nsf::Nsf(Emulator& e,const Configuration& cfg,Window::Menu& m)
34 		: Manager(e,m,this,&Nsf::OnEmuEvent)
35 		{
36 			static const Window::Menu::CmdHandler::Entry<Nsf> commands[] =
37 			{
38 				{ IDM_MACHINE_NSF_PLAY,                     &Nsf::OnCmd          },
39 				{ IDM_MACHINE_NSF_STOP,                     &Nsf::OnCmd          },
40 				{ IDM_MACHINE_NSF_NEXT,                     &Nsf::OnCmd          },
41 				{ IDM_MACHINE_NSF_PREV,                     &Nsf::OnCmd          },
42 				{ IDM_MACHINE_NSF_OPTIONS_PLAYINBACKGROUND, &Nsf::OnCmdPlayInBkg }
43 			};
44 
45 			menu.Commands().Add( this, commands );
46 
47 			static const Window::Menu::PopupHandler::Entry<Nsf> popups[] =
48 			{
49 				{ Window::Menu::PopupHandler::Pos<IDM_POS_MACHINE,IDM_POS_MACHINE_NSF>::ID, &Nsf::OnMenu }
50 			};
51 
52 			menu.Popups().Add( this, popups );
53 
54 			menu[IDM_MACHINE_NSF_OPTIONS_PLAYINBACKGROUND].Check( !cfg["nsf"]["play-in-background"].No() );
55 		}
56 
Save(Configuration & cfg) const57 		void Nsf::Save(Configuration& cfg) const
58 		{
59 			cfg["nsf"]["play-in-background"].YesNo() = menu[IDM_MACHINE_NSF_OPTIONS_PLAYINBACKGROUND].Checked();
60 		}
61 
OnMenu(const Window::Menu::PopupHandler::Param & param)62 		void Nsf::OnMenu(const Window::Menu::PopupHandler::Param& param)
63 		{
64 			const bool on = emulator.IsNsfOn();
65 			const Nes::Nsf nsf( emulator );
66 
67 			menu[IDM_MACHINE_NSF_PLAY].Enable( !param.show || (on && !nsf.IsPlaying()) );
68 			menu[IDM_MACHINE_NSF_STOP].Enable( !param.show || (on && nsf.IsPlaying()) );
69 			menu[IDM_MACHINE_NSF_NEXT].Enable( !param.show || (on && nsf.GetCurrentSong() + 1 < nsf.GetNumSongs()) );
70 			menu[IDM_MACHINE_NSF_PREV].Enable( !param.show || (on && nsf.GetCurrentSong() > 0) );
71 		}
72 
OnEmuEvent(const Emulator::Event event,const Emulator::Data data)73 		void Nsf::OnEmuEvent(const Emulator::Event event,const Emulator::Data data)
74 		{
75 			switch (event)
76 			{
77 				case Emulator::EVENT_POWER_ON:
78 
79 					if (emulator.IsNsf())
80 						Nes::Nsf(emulator).PlaySong();
81 
82 					break;
83 
84 				case Emulator::EVENT_NETPLAY_MODE:
85 
86 					menu[IDM_MACHINE_NSF_OPTIONS_PLAYINBACKGROUND].Enable( !data );
87 					menu[IDM_POS_MACHINE][IDM_POS_MACHINE_NSF].Enable( !data );
88 					break;
89 			}
90 		}
91 
OnCmd(uint cmd)92 		void Nsf::OnCmd(uint cmd)
93 		{
94 			Nes::Nsf nsf(emulator);
95 
96 			switch (cmd)
97 			{
98 				case IDM_MACHINE_NSF_PLAY: nsf.PlaySong(); break;
99 				case IDM_MACHINE_NSF_STOP: nsf.StopSong(); break;
100 				case IDM_MACHINE_NSF_NEXT: nsf.SelectNextSong(); break;
101 				case IDM_MACHINE_NSF_PREV: nsf.SelectPrevSong(); break;
102 			}
103 
104 			Resume();
105 		}
106 
OnCmdPlayInBkg(uint)107 		void Nsf::OnCmdPlayInBkg(uint)
108 		{
109 			menu[IDM_MACHINE_NSF_OPTIONS_PLAYINBACKGROUND].ToggleCheck();
110 		}
111 	}
112 }
113