sound_page.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package Sound
  2. import (
  3. "fmt"
  4. "github.com/cuu/gogame/event"
  5. "github.com/veandco/go-sdl2/sdl"
  6. "github.com/cuu/gogame/draw"
  7. "github.com/cuu/gogame/rect"
  8. "github.com/cuu/gogame/surface"
  9. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  10. )
  11. type OnChangeCB_T func(int)
  12. type SliderIcon struct {
  13. UI.IconItem
  14. Parent *SoundSlider
  15. }
  16. func NewSliderIcon() *SliderIcon {
  17. p := &SliderIcon{}
  18. p.MyType = UI.ICON_TYPES["EXE"]
  19. p.Align = UI.ALIGN["VCenter"]
  20. return p
  21. }
  22. func (self *SliderIcon) Draw() {
  23. if self.Parent == nil {
  24. fmt.Println("Error: SliderIcon Draw Parent nil")
  25. return
  26. }
  27. parent_x, parent_y := self.Parent.Coord()
  28. if self.Label != nil {
  29. // lab_x,lab_y := self.Label.Coord()
  30. lab_w, lab_h := self.Label.Size()
  31. if self.Align == UI.ALIGN["VCenter"] {
  32. // fmt.Println("IconItem Draw VCenter:",lab_w,lab_h,self.Label.GetText())
  33. self.Label.NewCoord(self.PosX-lab_w/2+parent_x, self.PosY+self.Height/2+6+parent_y)
  34. } else if self.Align == UI.ALIGN["HLeft"] {
  35. self.Label.NewCoord(self.PosX+self.Width/2+3+parent_x, self.PosY-lab_h/2+parent_y)
  36. }
  37. self.Label.Draw()
  38. }
  39. if self.ImgSurf != nil {
  40. surface.Blit(self.Parent.GetCanvasHWND(), self.ImgSurf, draw.MidRect(self.PosX+parent_x, self.PosY+parent_y,
  41. self.Width, self.Height, UI.Width, UI.Height), nil)
  42. }
  43. }
  44. type SliderMultiIcon struct {
  45. UI.MultiIconItem
  46. Parent *SoundSlider
  47. }
  48. func NewSliderMultiIcon() *SliderMultiIcon {
  49. p := &SliderMultiIcon{}
  50. p.MyType = UI.ICON_TYPES["EXE"]
  51. p.Align = UI.ALIGN["VCenter"]
  52. p.IconIndex = 0
  53. p.IconWidth = 18
  54. p.IconHeight = 18
  55. return p
  56. }
  57. func (self *SliderMultiIcon) Draw() {
  58. if self.Parent == nil {
  59. fmt.Println("Error: SliderMultiIcon Draw Parent nil")
  60. return
  61. }
  62. parent_x, parent_y := self.Parent.Coord()
  63. if self.Label != nil {
  64. // lab_x,lab_y := self.Label.Coord()
  65. lab_w, lab_h := self.Label.Size()
  66. if self.Align == UI.ALIGN["VCenter"] {
  67. self.Label.NewCoord(self.PosX-lab_w/2+parent_x, self.PosY+self.Height/2+6+parent_y)
  68. } else if self.Align == UI.ALIGN["HLeft"] {
  69. self.Label.NewCoord(self.PosX+self.Width/2+3+parent_x, self.PosY-lab_h/2+parent_y)
  70. }
  71. self.Label.Draw()
  72. }
  73. if self.ImgSurf != nil {
  74. portion := rect.Rect(0, self.IconIndex*self.IconHeight, self.IconWidth, self.IconHeight)
  75. surface.Blit(self.Parent.GetCanvasHWND(),
  76. self.ImgSurf, draw.MidRect(self.PosX+parent_x, self.PosY+parent_y,
  77. self.Width, self.Height, UI.Width, UI.Height), &portion)
  78. }
  79. }
  80. type SoundSlider struct {
  81. UI.Slider
  82. BGpng *SliderIcon
  83. BGwidth int
  84. BGheight int
  85. //NeedleSurf
  86. Scale *SliderMultiIcon
  87. Parent *SoundPage
  88. OnChangeCB OnChangeCB_T
  89. snd_segs [][2]int
  90. }
  91. func NewSoundSlider() *SoundSlider {
  92. p := &SoundSlider{}
  93. p.Range = [2]int{0, 255}
  94. p.Value = 0
  95. p.BGwidth = 192
  96. p.BGheight = 173
  97. p.snd_segs = [][2]int{[2]int{0, 20}, [2]int{21, 40}, [2]int{41, 50},
  98. [2]int{51, 60}, [2]int{61, 70}, [2]int{71, 85},
  99. [2]int{86, 90}, [2]int{91, 95}, [2]int{96, 100}}
  100. return p
  101. }
  102. func (self *SoundSlider) GetCanvasHWND() *sdl.Surface {
  103. return self.CanvasHWND
  104. }
  105. func (self *SoundSlider) Init() {
  106. self.Width = self.Parent.Width
  107. self.Height = self.Parent.Height
  108. self.BGpng = NewSliderIcon()
  109. self.BGpng.ImgSurf = UI.MyIconPool.GetImgSurf("vol")
  110. self.BGpng.MyType = UI.ICON_TYPES["STAT"]
  111. self.BGpng.Parent = self
  112. self.BGpng.Adjust(0, 0, self.BGwidth, self.BGheight, 0)
  113. self.Scale = NewSliderMultiIcon()
  114. self.Scale.MyType = UI.ICON_TYPES["STAT"]
  115. self.Scale.Parent = self
  116. self.Scale.ImgSurf = UI.MyIconPool.GetImgSurf("scale")
  117. self.Scale.IconWidth = 82
  118. self.Scale.IconHeight = 63
  119. self.Scale.Adjust(0, 0, 82, 63, 0)
  120. }
  121. func (self *SoundSlider) SetValue(vol int) { // pct 0 - 100
  122. for i, v := range self.snd_segs {
  123. if vol >= v[0] && vol <= v[1] {
  124. self.Value = i
  125. break
  126. }
  127. }
  128. }
  129. func (self *SoundSlider) Further() {
  130. self.Value += 1
  131. if self.Value >= len(self.snd_segs)-1 {
  132. self.Value = len(self.snd_segs) - 1
  133. }
  134. vol := self.snd_segs[self.Value][0] + (self.snd_segs[self.Value][1]-self.snd_segs[self.Value][0])/2
  135. if self.OnChangeCB != nil {
  136. self.OnChangeCB(vol)
  137. }
  138. }
  139. func (self *SoundSlider) StepBack() {
  140. self.Value -= 1
  141. if self.Value < 0 {
  142. self.Value = 0
  143. }
  144. vol := self.snd_segs[self.Value][0] + (self.snd_segs[self.Value][1]-self.snd_segs[self.Value][0])/2
  145. if self.OnChangeCB != nil {
  146. self.OnChangeCB(vol)
  147. }
  148. }
  149. func (self *SoundSlider) Draw() {
  150. self.BGpng.NewCoord(self.Width/2, self.Height/2)
  151. //fmt.Printf("%x\n",self.BGpng.Parent)
  152. self.BGpng.Draw()
  153. self.Scale.NewCoord(self.Width/2, self.Height/2)
  154. self.Scale.IconIndex = self.Value
  155. self.Scale.Draw()
  156. }
  157. type SoundPage struct {
  158. UI.Page
  159. MySlider *SoundSlider
  160. }
  161. func NewSoundPage() *SoundPage {
  162. p := &SoundPage{}
  163. p.PageIconMargin = 20
  164. p.SelectedIconTopOffset = 20
  165. p.EasingDur = 10
  166. p.Align = UI.ALIGN["SLeft"]
  167. p.FootMsg = [5]string{"Nav", "", "", "Back", "Enter"}
  168. return p
  169. }
  170. func (self *SoundPage) Init() {
  171. self.CanvasHWND = self.Screen.CanvasHWND
  172. self.Width = self.Screen.Width
  173. self.Height = self.Screen.Height
  174. self.MySlider = NewSoundSlider()
  175. self.MySlider.Parent = self
  176. self.MySlider.SetCanvasHWND(self.CanvasHWND)
  177. self.MySlider.OnChangeCB = self.WhenSliderDrag
  178. self.MySlider.Init()
  179. v, err := GetVolume()
  180. if err == nil {
  181. self.MySlider.SetValue(v)
  182. } else {
  183. fmt.Println(err)
  184. }
  185. }
  186. func (self *SoundPage) OnLoadCb() {
  187. v, err := GetVolume()
  188. if err == nil {
  189. self.MySlider.SetValue(v)
  190. } else {
  191. fmt.Println(err)
  192. }
  193. }
  194. func (self *SoundPage) WhenSliderDrag(val int) { //value 0 - 100
  195. if val < 0 || val > 100 {
  196. return
  197. }
  198. self.Screen.TitleBar.SetSoundVolume(val)
  199. SetVolume(val)
  200. }
  201. func (self *SoundPage) KeyDown(ev *event.Event) {
  202. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  203. self.ReturnToUpLevelPage()
  204. self.Screen.Refresh()
  205. }
  206. if ev.Data["Key"] == UI.CurKeys["Right"] {
  207. self.MySlider.Further()
  208. self.Screen.Refresh()
  209. }
  210. if ev.Data["Key"] == UI.CurKeys["Left"] {
  211. self.MySlider.StepBack()
  212. self.Screen.Refresh()
  213. }
  214. }
  215. func (self *SoundPage) Draw() {
  216. self.ClearCanvas()
  217. self.MySlider.Draw()
  218. }