foot_bar.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/rect"
  9. "github.com/cuu/gogame/surface"
  10. "github.com/cuu/gogame/draw"
  11. "github.com/cuu/gogame/image"
  12. "github.com/cuu/gogame/font"
  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. self.Icons[v] = it
  106. }
  107. }
  108. func (self *FootBar) Init(main_screen *MainScreen) {
  109. self.CanvasHWND = surface.Surface(self.Width,self.Height)
  110. self.HWND = main_screen.HWND
  111. self.SkinManager = main_screen.SkinManager
  112. self.ReadFootBarIcons(self.icon_base_path)
  113. round_corners := NewFootBarIconItem()
  114. round_corners.IconWidth = 10
  115. round_corners.IconHeight = 10
  116. round_corners.MyType = ICON_TYPES["STAT"]
  117. round_corners.Parent = self
  118. round_corners.ImgSurf = MyIconPool.GetImgSurf("roundcorners")
  119. round_corners.Adjust(0,0,10,10,0)
  120. self.Icons["round_corners"] = round_corners
  121. }
  122. func (self *FootBar) ResetNavText() {
  123. self.Icons["nav"].SetLabelText("Nav.")
  124. self.State = "normal"
  125. self.Draw()
  126. }
  127. func (self *FootBar) UpdateNavText(texts string) {
  128. self.State = "tips"
  129. // my_text := font.Render(self.LabelFont, texts, true,self.SkinManager.GiveColor("Text"),nil)
  130. left_width := self.Width - 18
  131. final_piece := ""
  132. for i,_ := range texts {
  133. text_ := texts[:i+1]
  134. my_text := font.Render(self.LabelFont, text_, true, self.SkinManager.GiveColor("Text"),nil)
  135. final_piece = text_
  136. if int(my_text.W) >= left_width {
  137. break
  138. }
  139. }
  140. fmt.Printf("finalpiece %s\n", final_piece)
  141. self.Icons["nav"].SetLabelText(final_piece)
  142. self.Draw()
  143. }
  144. func (self *FootBar) SetLabelTexts( texts [5]string) {
  145. keynames := [5]string{"nav","x","y","a","b"}
  146. if len(texts) < 5 {
  147. log.Fatal("SetLabelTexts texts length error")
  148. return
  149. }
  150. for idx,x := range keynames {
  151. self.Icons[x].SetLabelText(texts[idx])
  152. }
  153. }
  154. func (self *FootBar) ClearCanvas() {
  155. surface.Fill( self.CanvasHWND, self.SkinManager.GiveColor("White"))
  156. self.Icons["round_corners"].NewCoord(5,self.Height-5)
  157. self.Icons["round_corners"].SetIconIndex(2)
  158. self.Icons["round_corners"].Draw()
  159. self.Icons["round_corners"].NewCoord(self.Width - 5,self.Height - 5)
  160. self.Icons["round_corners"].SetIconIndex(3)
  161. self.Icons["round_corners"].Draw()
  162. }
  163. func (self *FootBar) Draw() {
  164. self.ClearCanvas()
  165. self.Icons["nav"].NewCoord(self.IconWidth/2+3, self.IconHeight/2+2)
  166. self.Icons["nav"].Draw()
  167. if self.State == "normal" {
  168. _w := 0
  169. for i,x := range []string{"b","a","y","x"} {
  170. if self.Icons[x].GetLabelText() != "" {
  171. if i== 0 {
  172. _w += self.Icons[x].TotalWidth()
  173. }else {
  174. _w += self.Icons[x].TotalWidth()+5
  175. }
  176. start_x := self.Width - _w
  177. start_y := self.IconHeight/2+2
  178. self.Icons[x].NewCoord(start_x, start_y)
  179. self.Icons[x].Draw()
  180. }
  181. }
  182. }
  183. draw.Line(self.CanvasHWND, self.SkinManager.GiveColor("Line"),0,0,Width,0,self.BorderWidth)
  184. if self.HWND != nil {
  185. rect_ := rect.Rect(self.PosX, Height - self.Height, Width, self.BarHeight)
  186. surface.Blit(self.HWND,self.CanvasHWND, &rect_,nil)
  187. }
  188. }