-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.vb
333 lines (240 loc) · 10.2 KB
/
Main.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
Imports System.Runtime.InteropServices
Imports JukeBox.Controllers
Imports JukeBox.Controllers.Controller
Imports System.IO
Imports System.Data.SqlServerCe
Imports System.Drawing.Drawing2D
Imports System.Data.SqlClient
Imports System.Linq
Public Class Form1
'Money related
Dim credit As Integer = 0
Public store As Storage
Private dbConn As SqlCeConnection = Nothing
Private queuedSongs As New Queue(Of Song)
Private failedSongs As HashSet(Of String)
Private songTimeTracker As New Timer
Private Delegate Sub nextSong()
Private nxtSong As nextSong = AddressOf changeMedia
Private ssTimer As Timer = New Timer()
Sub New()
InitializeComponent()
MyBase.SetStyle(ControlStyles.OptimizedDoubleBuffer _
Or ControlStyles.ResizeRedraw _
Or ControlStyles.Selectable _
Or ControlStyles.AllPaintingInWmPaint _
Or ControlStyles.UserPaint _
Or ControlStyles.SupportsTransparentBackColor, True)
lblCredit.BackColor = Color.FromArgb(150, Color.DarkBlue)
lblCurrentAction.BackColor = Color.FromArgb(150, Color.DarkBlue)
lblSongTime.BackColor = Color.FromArgb(150, Color.DarkBlue)
gbOrder.BackColor = Color.FromArgb(150, Color.DarkBlue)
wmp.Visible = False
wmp.uiMode = "invisible"
wmp.windowlessVideo = True
wmp.settings.autoStart = True
wmp.settings.enableErrorDialogs = False
orderedList.Items = New List(Of String)
If Settings.getInst().getVal("screen_saver_enabled") = "true" Then
ssTimer.Interval = Integer.Parse(Settings.getInst().getVal("screen_saver_time"))
ssTimer.Start()
End If
AddHandler ssTimer.Tick, AddressOf showScreenSaver
AddHandler wmp.MediaError, AddressOf WmpMediaError
AddHandler wmp.PlayStateChange, AddressOf WmpPlayStateChanged
AddHandler wmp.MediaChange, AddressOf WmpMediaChanged
AddHandler Me.FormClosing, AddressOf appShutdown
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Form settings
If Settings.getInst().getVal("fullscreen") = "true" Then
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized
End If
' Db connection
dbConn = New SqlCeConnection(String.Format("Data Source = {0}", Path.GetFullPath(Settings.getInst().getVal("db_file"))))
dbConn.Open()
' Load failed songs
failedSongs = getFailedSongs(dbConn)
' LoadSongs
store = Storage.getFromDirectory(Path.GetFullPath(Settings.getInst().getVal("music_library_location")), Function(p As String) Not failedSongs.Contains(p))
' Top List
loadTopList(store)
store.initialize(True)
' Song Selected handler
AddHandler store.songSelected, AddressOf songSelected
changeMedia()
'Initilizing app state
creditChange(0)
'Controller related stuff
Dim ctrlr As Controller
Select Case Settings.getInst().getVal("control_dev").ToUpper()
Case "KEYBOARD"
ctrlr = New KeyboardController(Me)
Case "LPT"
ctrlr = New ParallelPortController()
Case Else
MsgBox("Not valid controller")
Environment.Exit(1)
End Select
AddHandler ctrlr.ControllerEvent, AddressOf CtrlEvt
songTimeTracker.Interval = 500
AddHandler songTimeTracker.Tick, AddressOf songTimeTrack
songTimeTracker.Start()
End Sub
Private Sub showScreenSaver()
ssTimer.Stop()
ScreenSaver.Show()
End Sub
Public Sub closeScreenSaver()
If Application.OpenForms.OfType(Of ScreenSaver).Any Then
ScreenSaver.Close()
End If
ssTimer.Stop()
ssTimer.Start()
End Sub
Private Function getFailedSongs(ByVal dbC As SqlCeConnection) As HashSet(Of String)
Dim fSongs As New HashSet(Of String)
Dim query As New SqlCeCommand("SELECT * FROM failed_songs", dbC)
Using reader As SqlCeDataReader = query.ExecuteReader()
While (reader.Read())
fSongs.Add(reader.Item("path"))
End While
End Using
Return fSongs
End Function
Private Sub creditChange(ByVal i As Integer)
credit += i
lblCredit.Text = "Kredit : " + credit.ToString()
End Sub
Private Sub songTimeTrack()
If (wmp.playState = WMPLib.WMPPlayState.wmppsPlaying) Then
lblSongTime.Text = wmp.Ctlcontrols.currentPositionString + "/" + wmp.currentMedia.durationString
Else
lblSongTime.Text = ""
End If
End Sub
Private Sub loadTopList(ByVal store As Storage)
Dim topList As SecondLevelItem = New SecondLevelItem(store, "Top lista", "", SecondLevelItem.IType.GROUP)
Dim sqlComm1 As New SqlCeCommand("SELECT * FROM stats ORDER BY repetitions DESC", dbConn)
Using res As SqlCeDataReader = sqlComm1.ExecuteReader()
While (res.Read())
Dim p As String = res.Item("path")
Dim fName As String = Path.GetFileNameWithoutExtension(p)
topList.items.Add(fName, New SecondLevelItem(topList, fName, p, SecondLevelItem.IType.SONG))
End While
End Using
store.items.Add("Top lista", topList)
End Sub
Private Sub addFailedSong(ByVal path As String)
Dim failedSongs As New SqlCeCommand("SELECT * FROM failed_songs WHERE path = @path", dbConn)
failedSongs.Parameters.Add("@path", path)
Using results As SqlCeDataReader = failedSongs.ExecuteReader()
While (results.Read())
Return
End While
Dim addToFailedSongs As New SqlCeCommand("INSERT INTO failed_songs (path) VALUES (@path)", dbConn)
addToFailedSongs.Parameters.Add("@path", path)
addToFailedSongs.ExecuteNonQuery()
End Using
End Sub
Private Sub CtrlEvt(ByVal ctrlEvt As ControllerEvt)
If Me.InvokeRequired Then
Invoke(Sub(ctlEvt) Me.CtrlEvt(ctlEvt), ctrlEvt)
Return
End If
closeScreenSaver()
If ctrlEvt = ControllerEvt.A_EXIT Then
appShutdown()
Return
ElseIf ctrlEvt = ControllerEvt.COIN_ACCEPTED Then
creditChange(1 * Integer.Parse(Settings.getInst().getVal("songs_per_coin")))
Return
End If
' This need to be modularized ( i.e maybe some idea of controller contexts )
If (ctrlEvt = ControllerEvt.UP Or ctrlEvt = ControllerEvt.DOWN) Then
If TypeOf Me.ActiveControl Is ListPanel Then
If (ctrlEvt = ControllerEvt.UP) Then
CType(Me.ActiveControl, ListPanel).prevItem()
Else
CType(Me.ActiveControl, ListPanel).nextItem()
End If
End If
Return
End If
If (ctrlEvt = ControllerEvt.OK) Then
store.currentCtx.switchToChildView()
Else
store.currentCtx.switchToParentView()
End If
End Sub
Private Sub appShutdown(Optional ByVal sender As Object = Nothing, Optional ByVal e As FormClosingEventArgs = Nothing)
dbConn.Close()
songTimeTracker.Stop()
If e Is Nothing Then
Me.Close()
End If
End Sub
Private Sub changeMedia(Optional ByVal url As String = Nothing)
If Not url Is Nothing Then
wmp.URL = url
Return
End If
If (queuedSongs.Count > 0) Then
wmp.URL = queuedSongs.Dequeue().getPath()
updateQueuedSongsDisplay()
Return
End If
wmp.URL = store.getRandomSong()
End Sub
Private Sub WmpPlayStateChanged(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent)
If e.newState = WMPLib.WMPPlayState.wmppsMediaEnded Then
Me.BeginInvoke(nxtSong)
End If
End Sub
Public Sub WmpMediaError(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_MediaErrorEvent)
addFailedSong(wmp.URL)
Me.BeginInvoke(nxtSong)
End Sub
Private Sub WmpMediaChanged(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_MediaChangeEvent)
lblCurrentAction.Text = "Trenutna pjesma: " + Path.GetFileNameWithoutExtension(wmp.URL)
End Sub
Private Sub ItemChanged(ByVal idx As Integer, ByVal val As String) Handles catList.ItemSelected, groupsAndSongsList.ItemSelected
If Not store Is Nothing Then
store.currentCtx.setSelected(idx, val)
End If
End Sub
Private Sub updateQueuedSongsDisplay()
orderedList.Items = Array.ConvertAll(queuedSongs.ToArray(), Function(song As Song) song.getDisplay()).ToList()
End Sub
Private Sub updateTopList(ByVal path As String)
Dim failedSongs As New SqlCeCommand("SELECT * FROM stats WHERE path = @path", dbConn)
failedSongs.Parameters.Add("@path", path)
Using results As SqlCeDataReader = failedSongs.ExecuteReader()
While (results.Read())
Dim updateFailedSongs As New SqlCeCommand("UPDATE stats SET repetitions=repetitions+1 WHERE path = @path", dbConn)
updateFailedSongs.Parameters.Add("@path", path)
updateFailedSongs.ExecuteNonQuery()
Return
End While
Dim addToFailedSongs As New SqlCeCommand("INSERT INTO stats (path, repetitions) VALUES (@path, 1)", dbConn)
addToFailedSongs.Parameters.Add("@path", path)
addToFailedSongs.ExecuteNonQuery()
End Using
End Sub
Private Sub songSelected(ByVal path As String)
If credit = 0 Then
Return
End If
If queuedSongs.ToList().FindAll(Function(s) s.getPath = path).Count > 0 Then
Return
End If
updateTopList(path)
queuedSongs.Enqueue(New Song(path))
updateQueuedSongsDisplay()
creditChange(-1)
If credit = 0 Then
store.initialize(True)
End If
End Sub
End Class