sound_patch.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package UI
  2. import (
  3. "log"
  4. "github.com/cuu/gogame/draw"
  5. "github.com/cuu/gogame/rect"
  6. "github.com/itchyny/volume-go"
  7. //"github.com/cuu/gogame/color"
  8. )
  9. type SoundPatch struct {
  10. AboveAllPatch
  11. snd_segs [][2]int
  12. Needle int
  13. Parent *MainScreen
  14. }
  15. func NewSoundPatch() *SoundPatch {
  16. p := &SoundPatch{}
  17. p.PosX = Width / 2
  18. p.PosY = Height / 2
  19. p.Width = 50
  20. p.Height = 120
  21. p.FontObj = Fonts["veramono20"]
  22. p.Color = MySkinManager.GiveColor("Text")
  23. p.ValColor = MySkinManager.GiveColor("URL")
  24. p.Icons = make(map[string]IconItemInterface)
  25. p.Value = 0
  26. p.snd_segs = [][2]int{[2]int{0, 20}, [2]int{21, 40}, [2]int{41, 50},
  27. [2]int{51, 60}, [2]int{61, 70}, [2]int{71, 85},
  28. [2]int{86, 90}, [2]int{91, 95}, [2]int{96, 100}}
  29. return p
  30. }
  31. func (self *SoundPatch) Init() {
  32. self.SetCanvasHWND(self.Parent.CanvasHWND)
  33. }
  34. func (self *SoundPatch) VolumeUp() int {
  35. vol, err := volume.GetVolume()
  36. if err != nil {
  37. log.Printf("SoundPatch VolumeUp get volume failed: %+v", err)
  38. vol = 0
  39. }
  40. for i, v := range self.snd_segs {
  41. if vol >= v[0] && vol <= v[1] {
  42. self.Needle = i
  43. break
  44. }
  45. }
  46. self.Needle += 1
  47. if self.Needle > len(self.snd_segs)-1 {
  48. self.Needle = len(self.snd_segs) - 1
  49. }
  50. val := self.snd_segs[self.Needle][0] + (self.snd_segs[self.Needle][1]-self.snd_segs[self.Needle][0])/2
  51. volume.SetVolume(val)
  52. self.Value = self.snd_segs[self.Needle][1]
  53. self.Parent.TitleBar.SetSoundVolume(val)
  54. return self.Value
  55. }
  56. func (self *SoundPatch) VolumeDown() int {
  57. vol, err := volume.GetVolume()
  58. if err != nil {
  59. log.Printf("SoundPatch VolumeDown get volume failed: %+v\n", err)
  60. vol = 0
  61. }
  62. for i, v := range self.snd_segs {
  63. if vol >= v[0] && vol <= v[1] {
  64. self.Needle = i
  65. break
  66. }
  67. }
  68. self.Needle -= 1
  69. if self.Needle < 0 {
  70. self.Needle = 0
  71. }
  72. val := self.snd_segs[self.Needle][0]
  73. if val < 0 {
  74. val = 0
  75. }
  76. volume.SetVolume(val)
  77. self.Value = val
  78. self.Parent.TitleBar.SetSoundVolume(val)
  79. return self.Value
  80. }
  81. func (self *SoundPatch) Draw() {
  82. for i := 0; i < (self.Needle + 1); i++ {
  83. vol_rect := rect.Rect(80+i*20, self.Height/2+20, 10, 40)
  84. draw.AARoundRect(self.CanvasHWND, &vol_rect, MySkinManager.GiveColor("Front"), 3, 0, MySkinManager.GiveColor("Front"))
  85. }
  86. }