CODE
# Music Selector
# Written by: Prexus
# Special Thanks: Axe Man Deke (came up with code)
# http://prexus.rmxponline.com
# This only finds files in Audio/BGM, so if you are using RTP make sure to export
# the files you want to be able to select into that directory.
class Game_System
def playing_bgm=(playing_bgm)
@playing_bgm = playing_bgm
end
def pick_song(array_index, volume = 100)
array = Dir.entries("Audio/BGM")
array.delete("..")
array.delete(".")
string = array[array_index]
loop do
if string.include?(".")
string.chop!
else
break
end
end
Audio.bgm_play("Audio/BGM/" + string, volume, 0)
$game_system.playing_bgm.name = string
Graphics.frame_reset
end
end
class Window_SelectVolume < Window_Base
def initialize
@number = 100
dummy_bitmap = Bitmap.new(32, 32)
@cursor_width = dummy_bitmap.text_size("0").width + 8
dummy_bitmap.dispose
super(0, 0, 160, 96)
self.contents = Bitmap.new(width - 32, height - 32)
self.contents.font.name = $fontface
self.contents.font.size = $fontsize
self.z += 9999
@index = 0
refresh
update_cursor_rect
end
def number
return @number
end
def number=(number)
@number = [[number, 0].max, 100].min
refresh
end
def index=(index)
@index = index
refresh
end
def update_cursor_rect
case @index
when 0
self.cursor_rect.set(self.width - 32 - 3 * @cursor_width, 32, @cursor_width, 32)
when 1
self.cursor_rect.set(self.width - 32 - 2 * @cursor_width, 32, @cursor_width, 32)
when 2
self.cursor_rect.set(self.width - 32 - 1 * @cursor_width, 32, @cursor_width, 32)
when 3
self.cursor_rect.set(self.width - 32 - 0 * @cursor_width, 32, @cursor_width, 32)
end
end
def update
super
if Input.repeat?(Input::UP) or Input.repeat?(Input::DOWN)
$game_system.se_play($data_system.cursor_se)
place = 10 ** (3 - 1 - @index)
n = @number / place % 10
@number -= n * place
n = (n + 1) % 10 if Input.repeat?(Input::UP)
n = (n + 9) % 10 if Input.repeat?(Input::DOWN)
@number += n * place
if @number > 100
@number = 0
end
refresh
end
if Input.repeat?(Input::RIGHT)
$game_system.se_play($data_system.cursor_se)
@index = (@index + 1) % 3
end
if Input.repeat?(Input::LEFT)
$game_system.se_play($data_system.cursor_se)
@index = (@index + 3 - 1) % 3
end
update_cursor_rect
end
def refresh
self.contents.clear
self.contents.font.color = normal_color
s = sprintf("%0*d", 3, @number)
for i in 0...3
case i
when 0
self.contents.draw_text(self.width-32 - 3 * @cursor_width+4, 32, 32, 32, s[0,1])
when 1
self.contents.draw_text(self.width-32 - 2 * @cursor_width+4, 32, 32, 32, s[1,1])
when 2
self.contents.draw_text(self.width-32 - 1 * @cursor_width+4, 32, 32, 32, s[2,1])
when 3
self.contents.draw_text(self.width-32 - 0 * @cursor_width+4, 32, 32, 32, s[3,1])
end
end
self.contents.draw_text(4, 0, self.width-32, 32, "Volume")
end
end
class Window_MusicList < Window_Selectable
def initialize
super(160, 0, 480, 480)
refresh
self.index = 0
end
def filename
return @data[self.index]
end
def refresh
if self.contents != nil
self.contents.dispose
self.contents = nil
end
@data = []
@data = Dir.entries("Audio/BGM")
@data.delete("..")
@data.delete(".")
@item_max = @data.size
if @item_max > 0
self.contents = Bitmap.new(width - 32, row_max * 32)
self.contents.font.name = $fontface
self.contents.font.size = $fontsize
for i in 0...@item_max
draw_item(i)
end
end
end
def draw_item(index)
item = @data[index]
loop do
if item.include?(".")
item.chop!
else
break
end
end
x = 4
y = index * 32
self.contents.draw_text(x, y, 608, 32, item)
end
end
class Scene_MusicSelector
def main
@musictype = Window_Command.new(160, ["Battle BGM", "Current"])
@musictype.x = 0
@musictype.y = 0
@musictype.active = true
@musiclist = Window_MusicList.new
@musiclist.index = -1
@musiclist.active = false
@volume = Window_SelectVolume.new
@volume.active = false
@volume.visible = false
@volume.x = 0
@volume.y = @musictype.y + @musictype.height
Graphics.transition
loop do
Graphics.update
Input.update
update
if $scene != self
break
end
end
Graphics.freeze
@volume.dispose
@musiclist.dispose
@musictype.dispose
end
def update
@volume.update
@musiclist.update
@musictype.update
if @musictype.active
update_type
return
end
if @musiclist.active
update_list
return
end
if @volume.active
update_volume
return
end
end
def update_type
if Input.trigger?(Input::B
$scene = Scene_Map.new
return
end
if Input.trigger?(Input::C)
@musiclist.active = true
@musictype.active = false
@musiclist.index = 0
return
end
end
def update_list
if Input.trigger?(Input::B
@musictype.active = true
@musictype.index = 0
@musiclist.active = false
@musiclist.index = -1
return
end
if Input.trigger?(Input::C)
@volume.number = 100
@volume.index = 0
@volume.visible = true
@volume.active = true
@musiclist.active = false
return
end
end
def update_volume
if Input.trigger?(Input::B
@volume.visible = false
@volume.active = false
@musiclist.active = true
return
end
if Input.trigger?(Input::C)
case @musictype.index
when 0
$data_system.battle_bgm.name = @musiclist.filename
$data_system.battle_bgm.volume = @volume.number
when 1
$game_system.pick_song(@musiclist.index, @volume.number)
end
@volume.visible = false
@volume.active = false
@musictype.active = true
@musictype.index = 0
@musiclist.index = -1
return
end
end
end