Skip to content

Commit

Permalink
Revert #111 Remodel GUI work and split between threads for now. On re…
Browse files Browse the repository at this point in the history
…quest because of stutter.
  • Loading branch information
JockeJarre committed Jan 8, 2025
1 parent eaad780 commit 8017861
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 120 deletions.
3 changes: 3 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
2.5.0
- #111 Remodel GUI work and split between threads

2.1.3
- **Finally a merge of the source code** for B2SBackglassServer & B2SBackglassServerEXE into one. _Should not be noticed_ while running, but huge for development.
- Added new method B2SSetPos(ImageId, xpos, ypos) to move the images on the backglass. Currently only as DLL!!!
- Added new method B2SBuildVersion() returning float version number: 20500.0295
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

Public Class B2SVersionInfo
Public Const B2S_VERSION_MAJOR = "2"
Public Const B2S_VERSION_MINOR = "5"
Public Const B2S_VERSION_REVISION = "0"
Public Const B2S_VERSION_MINOR = "1"
Public Const B2S_VERSION_REVISION = "3"
Public Const B2S_VERSION_BUILD = "999"
Public Const B2S_VERSION_HASH = "nonset"
' 2.5.0
Expand Down
6 changes: 1 addition & 5 deletions b2sbackglassserver/b2sbackglassserver/Controls/B2SLEDBox.vb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ Public Class B2SLEDBox
Set(ByVal newvalue As Integer)
If _Value <> newvalue OrElse refresh Then
_Value = newvalue
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() Me.Invalidate())
Else
Me.Invalidate()
End If
Me.Invalidate()
End If
End Set
End Property
Expand Down
20 changes: 15 additions & 5 deletions b2sbackglassserver/b2sbackglassserver/Controls/B2SPictureBox.vb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@ Public Class B2SPictureBox


Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)

'e.Graphics.DrawImage(BackgroundImage, e.ClipRectangle)
' rectangle area for painting
Dim rect As Rectangle = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)

' draw dashed frame
Dim pen As Pen = New Pen(Brushes.LightGray)
pen.DashPattern = New Single() {3.0F, 3.0F}
e.Graphics.DrawRectangle(pen, rect)
pen.Dispose()

' draw text
'If Not String.IsNullOrEmpty(Me.Text) Then
' TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, rect, Color.White, TextFormatFlags.WordBreak Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
'End If
End Sub

Public Sub New()

' set some drawing styles
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
'Me.SetStyle(ControlStyles.ResizeRedraw Or ControlStyles.SupportsTransparentBackColor, True)
Me.DoubleBuffered = True
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)
'Me.DoubleBuffered = True
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)

' backcolor needs to be transparent
Me.BackColor = Color.Transparent
Expand Down
31 changes: 6 additions & 25 deletions b2sbackglassserver/b2sbackglassserver/Controls/B2SReelBox.vb
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ Public Class B2SReelBox

If intermediates2go > 0 OrElse intermediates = -1 Then

If Me.InvokeRequired Then
Me.BeginInvoke(Sub() Me.Invalidate())
Else
Me.Invalidate()
End If
Me.Invalidate()
intermediates2go -= 1

Else
Expand All @@ -147,11 +143,8 @@ Public Class B2SReelBox
End If
Catch
End Try
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() Me.Invalidate())
Else
Me.Invalidate()
End If
Me.Invalidate()

intermediates2go -= 1
ElseIf intermediates2go = -1 Then
intermediates2go -= 1
Expand Down Expand Up @@ -208,11 +201,7 @@ Public Class B2SReelBox
If _Illuminated <> value Then
_Illuminated = value
intermediates2go = 0
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() Me.Invalidate())
Else
Me.Invalidate()
End If
Me.Invalidate()
End If
End Set
End Property
Expand All @@ -226,11 +215,7 @@ Public Class B2SReelBox
If _Value <> value OrElse refresh Then
_Value = value
reelindex = ConvertValue(_Value)
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() Me.Invalidate())
Else
Me.Invalidate()
End If
Me.Invalidate()
End If
End Set
End Property
Expand All @@ -251,11 +236,7 @@ Public Class B2SReelBox
timer.Start()
Else
reelindex = ConvertText(_Text)
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() Me.Invalidate())
Else
Me.Invalidate()
End If
Me.Invalidate()
End If
End If
End If
Expand Down
8 changes: 1 addition & 7 deletions b2sbackglassserver/b2sbackglassserver/Forms/formBackglass.vb
Original file line number Diff line number Diff line change
Expand Up @@ -1716,13 +1716,7 @@ Public Class formBackglass
B2SSettings.Save(, True)
#End If
Me.BackgroundImage = DarkImage

If Me.InvokeRequired Then
Me.BeginInvoke(Sub() Me.Invalidate())
Else
Me.Invalidate()
End If

Me.Invalidate()
ShowStartupImages()
B2SAnimation.RestartAnimations()
If formMode IsNot Nothing Then
Expand Down
12 changes: 2 additions & 10 deletions b2sbackglassserver/b2sbackglassserver/Forms/formSettings.vb
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,8 @@ Public Class formSettings
isSettingsScreenDirty = True
B2SSettings.CurrentDualMode = cmbMode.SelectedIndex + 1
If formBackglass IsNot Nothing Then
If Me.InvokeRequired Then
Me.BeginInvoke(Sub()
formBackglass.BackgroundImage = formBackglass.DarkImage
formBackglass.Invalidate()
End Sub
)
Else
formBackglass.BackgroundImage = formBackglass.DarkImage
formBackglass.Invalidate()
End If
formBackglass.BackgroundImage = formBackglass.DarkImage
formBackglass.Invalidate()
End If
B2SAnimation.RestartAnimations()
End Sub
Expand Down
80 changes: 14 additions & 66 deletions b2sbackglassserver/b2sbackglassserver/Server.vb
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
Imports System.Text
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Linq.Expressions
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.Reflection
Imports System.Runtime.InteropServices.WindowsRuntime

<ProgId("B2S.Server"), ComClass(Server.ClassID, Server.InterfaceID, Server.EventsID)>
Public Class Server

Implements IDisposable

Private Shared thread As Thread
Private Shared threadContext As SynchronizationContext = Nothing
Public threadReady As Boolean = False

Private Declare Function IsWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, Msg As UInteger, wParam As Integer, lParam As Integer) As Integer
Public Const WM_SYSCOMMAND As Integer = &H112
Expand Down Expand Up @@ -96,29 +94,7 @@ Public Class Server
#Region "constructor and end timer"

Public Sub New()
' Create new STA thread for this dll to run in, if the thread is already running then don't start another
' However if the thread is running, we need to stop the application from running, and then restart it by running StartThread again
If thread IsNot Nothing Then
' stop the application from running
Application.Exit()
End If

thread = New Thread(AddressOf StartThread)
thread.SetApartmentState(ApartmentState.STA)
thread.Name = "B2SThread"
thread.Start()
' Dont return until the thread is ready, by checking the threadReady variable
Do While Not threadReady
Thread.Sleep(100)
Loop
End Sub


Private Sub StartThread()
' Create a syncronization context for the thread
SynchronizationContext.SetSynchronizationContext(New WindowsFormsSynchronizationContext())
' Store the context for later use
threadContext = SynchronizationContext.Current
' maybe create the base registry key
If Registry.CurrentUser.OpenSubKey("Software\B2S") Is Nothing Then Registry.CurrentUser.CreateSubKey("Software\B2S")
If Registry.CurrentUser.OpenSubKey("Software\B2S\VPinMAME") Is Nothing Then Registry.CurrentUser.CreateSubKey("Software\B2S\VPinMAME")
Expand Down Expand Up @@ -149,10 +125,6 @@ Public Class Server
AddHandler timer.Tick, AddressOf Timer_Tick
timer.Interval = 37

' set the thread ready flag
threadReady = True

Application.Run()
End Sub

Private Sub Timer_Tick()
Expand Down Expand Up @@ -272,7 +244,8 @@ Public Class Server

End If
Catch ex As Exception
errorlog.WriteLogEntry(DateTime.Now & ex.Message & vbNewLine & ex.StackTrace)
Dim st As New StackTrace(ex, True)
errorlog.WriteLogEntry(DateTime.Now & "Line: " & st.GetFrame(0).GetMethod().Name & " : " & st.GetFrame(0).GetFileLineNumber().ToString & " : " & ex.Message)
Throw ex
End Try

Expand Down Expand Up @@ -431,11 +404,6 @@ Public Class Server


Public Sub Run(Optional ByVal handle As Object = 0)
'Make sure this is run on threadContext thread
If SynchronizationContext.Current IsNot threadContext Then
threadContext.Send(New SendOrPostCallback(AddressOf Run), handle)
Return
End If

' startup
tableHandle = CInt(handle)
Expand Down Expand Up @@ -471,17 +439,6 @@ Public Class Server

Public Sub [Stop]()

Try
If SynchronizationContext.Current IsNot threadContext Then
threadContext.Send(New SendOrPostCallback(AddressOf [Stop]), Nothing)
Return
End If

Catch ex As Exception
'If Exception is InvalidAsynchronousStateException then the thread is already stopped, so we can ignore it and return
Return
End Try

Try
Try
timer.Stop()
Expand Down Expand Up @@ -2158,7 +2115,7 @@ Public Class Server

Else

' only do the lighting stuff if the group has a name
' only do the lightning stuff if the group has a name
If Not String.IsNullOrEmpty(groupname) AndAlso B2SData.IlluminationGroups.ContainsKey(groupname) Then
' get all matching picture boxes
For Each picbox As B2SPictureBox In B2SData.IlluminationGroups(groupname)
Expand Down Expand Up @@ -2188,24 +2145,15 @@ Public Class Server
End Using
ElseIf B2SData.UsedRomLampIDs.ContainsKey(id) Then
Dim rescaleBackglass As SizeF
Me.formBackglass.GetScaleFactor(rescaleBackglass)
Me.formBackglass.GetScaleFactor(rescaleBackglass)

For Each picbox As B2SPictureBox In B2SData.UsedRomLampIDs(id)
If picbox IsNot Nothing AndAlso (Not B2SData.UseIlluminationLocks OrElse String.IsNullOrEmpty(picbox.GroupName) OrElse Not B2SData.IlluminationLocks.ContainsKey(picbox.GroupName)) Then
For Each picbox As B2SPictureBox In B2SData.UsedRomLampIDs(id)
If picbox IsNot Nothing AndAlso (Not B2SData.UseIlluminationLocks OrElse String.IsNullOrEmpty(picbox.GroupName) OrElse Not B2SData.IlluminationLocks.ContainsKey(picbox.GroupName)) Then
If picbox.Left <> xpos OrElse picbox.Top <> ypos Then
If (picbox.InvokeRequired) Then
picbox.BeginInvoke(Sub()
picbox.Left = xpos
picbox.Top = ypos
picbox.RectangleF = New RectangleF(CInt(picbox.Left / rescaleBackglass.Width), CInt(picbox.Top / rescaleBackglass.Height), picbox.RectangleF.Width, picbox.RectangleF.Height)
If picbox.Parent IsNot Nothing Then picbox.Parent.Invalidate()
End Sub)
Else
picbox.Left = xpos
picbox.Top = ypos
picbox.Left = xpos
picbox.Top = ypos
picbox.RectangleF = New RectangleF(CInt(picbox.Left / rescaleBackglass.Width), CInt(picbox.Top / rescaleBackglass.Height), picbox.RectangleF.Width, picbox.RectangleF.Height)
If picbox.Parent IsNot Nothing Then picbox.Parent.Invalidate()
End If
If picbox.Parent IsNot Nothing Then picbox.Parent.Invalidate()
End If
End If
Next
Expand Down

0 comments on commit 8017861

Please sign in to comment.