brightness_page.go 6.3 KB

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