gui.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import subprocess
  2. import wx # type: ignore
  3. import wx.adv # type: ignore
  4. from wxasync import AsyncBind, StartCoroutine # type: ignore
  5. import client
  6. import config
  7. class TaskbarIcon(wx.adv.TaskBarIcon):
  8. def __init__(self, frame):
  9. super().__init__()
  10. self.frame = frame
  11. self.SetIcon(wx.Icon("icon.ico"))
  12. self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnLeftClick)
  13. def OnLeftClick(self, evt):
  14. self.frame.Show()
  15. self.frame.Restore()
  16. class MainFrame(wx.Frame):
  17. def __init__(self, *args, **kw):
  18. super().__init__(*args, **kw)
  19. self.panel = wx.Panel(self)
  20. self.SetIcon(wx.Icon("icon.ico"))
  21. self.CreateStatusBar()
  22. self.SetStatusText("Loading sounds...")
  23. self.client = client.Client(self)
  24. vbox = wx.BoxSizer(wx.VERTICAL)
  25. vbox.AddStretchSpacer()
  26. vbox.Add(
  27. self.make_volume_zone(), border=5, flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ALL
  28. )
  29. vbox.Add(
  30. self.make_settings_zone(),
  31. border=5,
  32. flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  33. )
  34. vbox.AddStretchSpacer()
  35. self.panel.SetSizer(vbox)
  36. self.panel.Layout()
  37. self.taskbarIcon = TaskbarIcon(self)
  38. AsyncBind(wx.EVT_ICONIZE, self.OnMinimize, self)
  39. AsyncBind(wx.EVT_SHOW, self.OnUnMinimize, self)
  40. AsyncBind(wx.EVT_CLOSE, self.OnClose, self)
  41. self.Centre()
  42. self.Show()
  43. StartCoroutine(self.UpdateSounds(None), self)
  44. def make_volume_zone(self):
  45. with self.client.sounds.lock:
  46. self.volumeSlider = wx.Slider(
  47. self.panel, value=self.client.sounds.volume, size=(272, 25)
  48. )
  49. AsyncBind(wx.EVT_COMMAND_SCROLL_CHANGED, self.OnVolumeSlider, self.volumeSlider)
  50. volumeZone = wx.StaticBoxSizer(wx.VERTICAL, self.panel, label="Volume")
  51. volumeZone.Add(self.volumeSlider)
  52. return volumeZone
  53. def make_settings_zone(self):
  54. self.preferHeadshotsChk = wx.CheckBox(
  55. self.panel, label="Prefer headshot sounds over killstreak sounds"
  56. )
  57. openSoundDirBtn = wx.Button(self.panel, label="Open sounds directory")
  58. self.updateSoundsBtn = wx.Button(self.panel, label="Update sounds")
  59. AsyncBind(wx.EVT_BUTTON, self.OpenSoundsDir, openSoundDirBtn)
  60. AsyncBind(wx.EVT_BUTTON, self.UpdateSounds, self.updateSoundsBtn)
  61. soundBtns = wx.BoxSizer(wx.HORIZONTAL)
  62. soundBtns.Add(openSoundDirBtn)
  63. soundBtns.Add(self.updateSoundsBtn)
  64. settingsBox = wx.StaticBoxSizer(wx.VERTICAL, self.panel, label="Settings")
  65. settingsBox.Add(self.preferHeadshotsChk, border=5, flag=wx.ALL)
  66. settingsBox.Add(soundBtns, border=5, flag=wx.ALIGN_CENTER | wx.UP | wx.DOWN)
  67. preferHeadshots = config.config["Sounds"].getboolean("PreferHeadshots", False)
  68. self.preferHeadshotsChk.SetValue(preferHeadshots)
  69. self.Bind(
  70. wx.EVT_CHECKBOX,
  71. lambda e: config.set(
  72. "Sounds", "PreferHeadshots", self.preferHeadshotsChk.Value
  73. ),
  74. self.preferHeadshotsChk,
  75. )
  76. return settingsBox
  77. def SetStatusText(self, text):
  78. """Override default SetStatusText to avoid minimizing CS:GO"""
  79. if self.IsIconized():
  80. return
  81. super().SetStatusText(text)
  82. async def OnUnMinimize(self, event):
  83. await self.client.update_status()
  84. async def OnVolumeSlider(self, event):
  85. config.set("Sounds", "Volume", self.volumeSlider.Value)
  86. with self.client.sounds.lock:
  87. # Volume didn't change
  88. if self.client.sounds.volume == self.volumeSlider.Value:
  89. return
  90. self.client.sounds.volume = self.volumeSlider.Value
  91. self.client.sounds.play("Headshot")
  92. async def OpenSoundsDir(self, event):
  93. # TODO linux
  94. subprocess.Popen('explorer "sounds"')
  95. async def UpdateSounds(self, event):
  96. self.updateSoundsBtn.Disable()
  97. await self.client.reload_sounds()
  98. async def OnMinimize(self, event):
  99. if self.IsIconized():
  100. self.Hide()
  101. async def OnClose(self, event):
  102. self.taskbarIcon.Destroy()
  103. self.Destroy()