Debian Codegraph @

CorsixTH/Lua/dialogs/jukebox.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 local ipairs
22 = ipairs
23
24 class "UIJukebox" (Window)
25
26 ---@type UIJukebox
27 local UIJukebox = _G["UIJukebox"]
28
29 function UIJukebox:UIJukebox(app)
30 self:Window()
31 self.modal_class = "jukebox"
32 self.esc_closes = true
33 self.audio = app.audio
34 self.app = app
35 self.ui = app.ui
36 self.width = 259
37 self.height = 74 + 30 * #self.audio.background_playlist + 18
38 self:setDefaultPosition(26, 26)
39 self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req13V", true)
40 self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
41 self.blue_font = app.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, { apply_ui_scale = true })
42
43 -- Dialog head (current track title & exit button)
44 self:addPanel(389, 0, 0)
45 for x = 30, self.width - 61, 24 do
46 self:addPanel(390, x, 0)
47 end
48 self:addPanel(391, self.width - 61, 0)
49 self:addPanel(409, self.width - 42, 19):makeButton(0, 0, 24, 24, 410, self.close):setTooltip(_S.tooltip.jukebox.close)
50
51 self.play_btn =
52 self:addPanel(392, 0, 49):makeToggleButton(19, 2, 50, 24, 393, self.togglePlayPause):setSound("selectx.wav"):setTooltip(_S.tooltip.jukebox.play)
53 self:updatePlayButton()
54 self:addPanel(394, 87, 49):makeButton(0, 2, 24, 24, 395, self.audio.playPreviousBackgroundTrack, self.audio):setSound("selectx.wav"):setTooltip(_S.tooltip.jukebox.rewind)
55 self:addPanel(396, 115, 49):makeButton(0, 2, 24, 24, 397, self.audio.playNextBackgroundTrack, self.audio):setSound("selectx.wav"):setTooltip(_S.tooltip.jukebox.fast_forward)
56 self:addPanel(398, 157, 49):makeButton(0, 2, 24, 24, 399, self.stopBackgroundTrack):setSound("selectx.wav"):setTooltip(_S.tooltip.jukebox.stop)
57 self:addPanel(400, 185, 49):makeButton(0, 2, 24, 24, 401, self.loopTrack):setSound("selectx.wav"):setTooltip(_S.tooltip.jukebox.loop)
58
59 -- Track list
60 self.track_buttons = {}
61 for i, info in ipairs(self.audio.background_playlist) do
62 local y = 47 + i * 30
63 self:addPanel(402, 0, y)
64 for x = 30, self.width - 61, 24 do
65 self:addPanel(403, x, y)
66 end
67 self.track_buttons[i] = self:addPanel(404, self.width - 61, y):makeToggleButton(19, 4, 24, 24, 405):setSound("selectx.wav")
68 if not info.enabled then
69 self.track_buttons[i]:setToggleState(true)
70 end
71 self.track_buttons[i].on_click = --[[persistable:jukebox_toggle_track]] function(window)
72 window:toggleTrack(i)
73 end
74 end
75
76 -- Dialog footer
77 local y = 74 + 30 * #self.audio.background_playlist
78 self:addPanel(406, 0, y)
79 for x = 30, self.width - 61, 24 do
80 self:addPanel(407, x, y)
81 end
82 self:addPanel(408, self.width - 61, y)
83
84 self:makeTooltip(_S.tooltip.jukebox.current_title, 17, 17, 212, 46)
85 end
86
87 -- makes the play button consistent with the current status of the background music
88 -- running -> toggled
89 -- stopped -> not toggled
90 -- paused -> not toggled
91 function UIJukebox:updatePlayButton()
92 local status = not not self.audio.background_music and not self.audio.background_paused
93 if status ~= self.play_btn.toggled then
94 self.play_btn:toggle()
95 end
96 end
97
98 function UIJukebox:togglePlayPause()
99 if not self.audio.background_music then
100 -- Update configuration that we want music now.
101 self.app.config.play_music = true
102 self.app:saveConfig()
103 self.audio:playRandomBackgroundTrack()
104 else
105 self.audio:pauseBackgroundTrack()
106 -- Just pausing does not imply that the player will want to have music turned off
107 -- the next time he or she starts the game.
108 end
109 end
110
111 function UIJukebox:stopBackgroundTrack()
112 -- Pressing this button means that the player doesn't want any
113 -- background music in the future, so set that config flag.
114 self.app.config.play_music = false
115 self.app:saveConfig()
116 self.audio:stopBackgroundTrack()
117 end
118
119 function UIJukebox:toggleTrack(index)
120 local info = self.audio.background_playlist[index]
121 self.track_buttons[index]:setToggleState(info.enabled)
122 info.enabled = not info.enabled
123 if not info.enabled and self.audio.background_music == info.music then
124 self.audio:stopBackgroundTrack()
125 self.audio:playRandomBackgroundTrack()
126 end
127 end
128
129 function UIJukebox:loopTrack()
130 local index = self.audio:findIndexOfCurrentTrack()
131 local playlist = self.audio.background_playlist
132 if not playlist[index] then return end
133
134 if playlist[index].loop then
135 playlist[index].loop = false
136
137 for i, list_entry in ipairs(playlist) do
138 if list_entry.enabled_before_loop and index ~= i then
139 list_entry.enabled_before_loop = nil
140 self:toggleTrack(i)
141 end
142 end
143 else
144 playlist[index].loop = true
145
146 for i, list_entry in ipairs(playlist) do
147 if list_entry.enabled and index ~= i then
148 list_entry.enabled_before_loop = true
149 self:toggleTrack(i)
150 end
151 end
152 end
153 end
154
155 function UIJukebox:draw(canvas, x, y)
156 for i, info in ipairs(TheApp.audio.background_playlist) do
157 self.track_buttons[i]:setToggleState(not info.enabled)
158 end
159 local s = TheApp.config.ui_scale
160 Window.draw(self, canvas, x, y)
161 x, y = self.x * s + x, self.y * s + y
162
163 local playing = self.audio.background_music or ""
164 for i, info in ipairs(self.audio.background_playlist) do
165 local ypos = y + 47 * s + i * 30 * s
166 local font = self.white_font
167 if info.music == playing then
168 font = self.blue_font
169 end
170 local str = info.title
171 while font:sizeOf(str) > 185 * s do
172 str = string.sub(str, 1, string.len(str) - 5) .. "..."
173 end
174 font:draw(canvas, str, x + 24 * s, ypos + 11 * s)
175 if info.music == playing then
176 font:draw(canvas, str, x + 24 * s, self.y * s + 27 * s)
177 end
178 end
179 end
180
181 function UIJukebox:afterLoad(old, new)
182 if not self.app.config.audio then
183 self:close()
184 end
185 Window.afterLoad(self, old, new)
186 if old < 236 then
187 self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
188 self.blue_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, { apply_ui_scale = true })
189 end
190 end