foot_bar.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package UI
  2. import (
  3. "fmt"
  4. // "io/ioutil"
  5. "log"
  6. "github.com/veandco/go-sdl2/sdl"
  7. "github.com/veandco/go-sdl2/ttf"
  8. "github.com/cuu/gogame/draw"
  9. "github.com/cuu/gogame/font"
  10. "github.com/cuu/gogame/image"
  11. "github.com/cuu/gogame/rect"
  12. "github.com/cuu/gogame/surface"
  13. )
  14. var FootBar_BarHeight = 20
  15. type FootBarIconItem struct {
  16. MultiIconItem
  17. Parent *FootBar
  18. }
  19. func NewFootBarIconItem() *FootBarIconItem {
  20. m := &FootBarIconItem{}
  21. m.IconIndex = 0
  22. m.IconWidth = 18
  23. m.IconHeight = 18
  24. m.Align = ALIGN["VCenter"]
  25. return m
  26. }
  27. func (self *FootBarIconItem) Adjust(x, y, w, h, at int) {
  28. self.PosX = x
  29. self.PosY = y
  30. self.Width = w
  31. self.Height = h
  32. self.AnimationTime = at
  33. if self.Label != nil {
  34. self.Label.SetCanvasHWND(self.Parent.CanvasHWND)
  35. }
  36. self.CreateImgSurf()
  37. // self.AdjustLinkPage()
  38. }
  39. func (self *FootBarIconItem) TotalWidth() int {
  40. lab_w, _ := self.Label.Size()
  41. return self.Width + lab_w
  42. }
  43. func (self *FootBarIconItem) Draw() {
  44. if self.Label != nil {
  45. lab_w, lab_h := self.Label.Size()
  46. if self.Align == ALIGN["VCenter"] {
  47. self.Label.NewCoord(self.PosX-lab_w/2, self.PosY+self.Height/2+12)
  48. } else if self.Align == ALIGN["HLeft"] {
  49. self.Label.NewCoord(self.PosX+self.Width/2+3, self.PosY-lab_h/2)
  50. }
  51. self.Label.Draw()
  52. }
  53. if self.ImgSurf != nil {
  54. portion := rect.Rect(0, self.IconIndex*self.IconHeight, self.IconWidth, self.IconHeight)
  55. surface.Blit(self.Parent.CanvasHWND, self.ImgSurf, draw.MidRect(self.PosX, self.PosY, self.Width, self.Height, Width, Height), &portion)
  56. } else {
  57. fmt.Println("self.ImgSurf is nil ")
  58. }
  59. }
  60. type FootBar struct {
  61. Widget
  62. BarHeight int
  63. BorderWidth int
  64. CanvasHWND *sdl.Surface
  65. HWND *sdl.Surface
  66. Icons map[string]IconItemInterface
  67. IconWidth int
  68. IconHeight int
  69. LabelFont *ttf.Font
  70. State string
  71. SkinManager *SkinManager
  72. icon_base_path string
  73. }
  74. func NewFootBar() *FootBar {
  75. f := &FootBar{}
  76. f.Width = Width
  77. f.BorderWidth = 1
  78. f.BarHeight = FootBar_BarHeight
  79. f.Height = 20
  80. f.IconWidth = 18
  81. f.IconHeight = 18
  82. f.LabelFont = Fonts["veramono10"]
  83. f.State = "normal"
  84. f.icon_base_path = SkinMap("sysgo/gameshell/footbar_icons/")
  85. f.Icons = make(map[string]IconItemInterface)
  86. return f
  87. }
  88. func (self *FootBar) ReadFootBarIcons(icondir string) {
  89. if FileExists(icondir) == false && IsDirectory(icondir) == false {
  90. return
  91. }
  92. keynames := [5]string{"nav", "x", "y", "a", "b"}
  93. share_surf := image.Load(self.icon_base_path + "footbar.png")
  94. for i, v := range keynames { // share_surf contains same number of image pieces of keynames
  95. it := NewFootBarIconItem()
  96. it.MyType = ICON_TYPES["NAV"]
  97. it.Parent = self
  98. it.ImgSurf = share_surf
  99. it.Align = ALIGN["HLeft"] // (X)Text
  100. it.IconWidth = self.IconWidth
  101. it.IconHeight = self.IconHeight
  102. it.AddLabel("game", self.LabelFont)
  103. it.Adjust(self.IconWidth/2+i*self.IconWidth, self.IconHeight/2+2, self.IconWidth, self.IconHeight, 0)
  104. it.IconIndex = i
  105. if val, ok := self.Icons[v]; ok {
  106. if val.(*FootBarIconItem).ImgSurf != nil {
  107. val.(*FootBarIconItem).ImgSurf.Free()
  108. }
  109. }
  110. self.Icons[v] = it
  111. }
  112. }
  113. func (self *FootBar) Init(main_screen *MainScreen) {
  114. self.CanvasHWND = surface.Surface(self.Width, self.Height)
  115. self.HWND = main_screen.HWND
  116. self.SkinManager = main_screen.SkinManager
  117. self.ReadFootBarIcons(self.icon_base_path)
  118. round_corners := NewFootBarIconItem()
  119. round_corners.IconWidth = 10
  120. round_corners.IconHeight = 10
  121. round_corners.MyType = ICON_TYPES["STAT"]
  122. round_corners.Parent = self
  123. round_corners.ImgSurf = MyIconPool.GetImgSurf("roundcorners")
  124. round_corners.Adjust(0, 0, 10, 10, 0)
  125. self.Icons["round_corners"] = round_corners
  126. }
  127. func (self *FootBar) ResetNavText() {
  128. self.Icons["nav"].SetLabelText("Nav.")
  129. self.State = "normal"
  130. self.Draw()
  131. }
  132. func (self *FootBar) UpdateNavText(texts string) {
  133. self.State = "tips"
  134. // my_text := font.Render(self.LabelFont, texts, true,self.SkinManager.GiveColor("Text"),nil)
  135. left_width := self.Width - 18
  136. final_piece := ""
  137. for i, _ := range texts {
  138. text_ := texts[:i+1]
  139. my_text := font.Render(self.LabelFont, text_, true, self.SkinManager.GiveColor("Text"), nil)
  140. final_piece = text_
  141. if int(my_text.W) >= left_width {
  142. my_text.Free()
  143. break
  144. }
  145. my_text.Free()
  146. }
  147. fmt.Printf("finalpiece %s\n", final_piece)
  148. self.Icons["nav"].SetLabelText(final_piece)
  149. self.Draw()
  150. }
  151. func (self *FootBar) SetLabelTexts(texts [5]string) {
  152. keynames := [5]string{"nav", "x", "y", "a", "b"}
  153. if len(texts) < 5 {
  154. log.Println("SetLabelTexts texts length error")
  155. return
  156. }
  157. for idx, x := range keynames {
  158. self.Icons[x].SetLabelText(texts[idx])
  159. }
  160. }
  161. func (self *FootBar) ClearCanvas() {
  162. surface.Fill(self.CanvasHWND, self.SkinManager.GiveColor("White"))
  163. self.Icons["round_corners"].NewCoord(5, self.Height-5)
  164. self.Icons["round_corners"].SetIconIndex(2)
  165. self.Icons["round_corners"].Draw()
  166. self.Icons["round_corners"].NewCoord(self.Width-5, self.Height-5)
  167. self.Icons["round_corners"].SetIconIndex(3)
  168. self.Icons["round_corners"].Draw()
  169. }
  170. func (self *FootBar) Draw() {
  171. self.ClearCanvas()
  172. self.Icons["nav"].NewCoord(self.IconWidth/2+3, self.IconHeight/2+2)
  173. self.Icons["nav"].Draw()
  174. if self.State == "normal" {
  175. _w := 0
  176. for i, x := range []string{"b", "a", "y", "x"} {
  177. if self.Icons[x].GetLabelText() != "" {
  178. if i == 0 {
  179. _w += self.Icons[x].TotalWidth()
  180. } else {
  181. _w += self.Icons[x].TotalWidth() + 5
  182. }
  183. start_x := self.Width - _w
  184. start_y := self.IconHeight/2 + 2
  185. self.Icons[x].NewCoord(start_x, start_y)
  186. self.Icons[x].Draw()
  187. }
  188. }
  189. }
  190. draw.Line(self.CanvasHWND, self.SkinManager.GiveColor("Line"), 0, 0, Width, 0, self.BorderWidth)
  191. if self.HWND != nil {
  192. rect_ := rect.Rect(self.PosX, Height-self.Height, Width, self.BarHeight)
  193. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  194. }
  195. }