icon_item.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package UI
  2. import (
  3. // "fmt"
  4. "github.com/veandco/go-sdl2/sdl"
  5. "github.com/veandco/go-sdl2/ttf"
  6. "github.com/cuu/gogame/surface"
  7. "github.com/cuu/gogame/draw"
  8. "github.com/cuu/gogame/color"
  9. "github.com/cuu/gogame/image"
  10. "github.com/cuu/gogame/transform"
  11. "github.com/cuu/gogame/utils"
  12. )
  13. type IconItemInterface interface {
  14. Init(x,y,w,h,at int)
  15. Adjust(x,y,w,h,at int)
  16. GetCmdPath() string
  17. SetCmdPath( path string)
  18. SetMyType( thetype int )
  19. GetMyType() int
  20. GetIconIndex() int
  21. SetIconIndex(idx int)
  22. GetIndex() int
  23. SetIndex(i int)
  24. SetParent( p interface{} )
  25. SetLabelColor(col *color.Color)
  26. SetLabelText(text string)
  27. GetLabelText() string
  28. Coord() (int,int)
  29. NewCoord(x,y int)
  30. Size() (int,int)
  31. NewSize(w,h int)
  32. TotalWidth() int
  33. AddLabel(text string, fontobj *ttf.Font)
  34. GetLinkPage() PageInterface
  35. AdjustLinkPage()
  36. GetImgSurf() *sdl.Surface
  37. SetImgSurf(newsurf *sdl.Surface)
  38. CreateImgSurf()
  39. ChangeImgSurfColor(col *color.Color)
  40. Clear()
  41. GetCmdInvoke() PluginInterface
  42. Draw()
  43. }
  44. type IconItem struct {
  45. Widget
  46. ImageName string
  47. ImgSurf *sdl.Surface
  48. Parent PageInterface
  49. Index int
  50. IconIndex int
  51. MyType int
  52. CmdPath string
  53. CmdInvoke PluginInterface
  54. LinkPage PageInterface
  55. Label LabelInterface
  56. Align int
  57. AnimationTime int
  58. }
  59. func NewIconItem() *IconItem {
  60. i := &IconItem{}
  61. i.MyType = ICON_TYPES["EXE"]
  62. i.Align = ALIGN["VCenter"]
  63. l := NewLabel()
  64. i.Label = l
  65. return i
  66. }
  67. func (self *IconItem) Init(x,y,w,h,at int) {
  68. self.PosX = x
  69. self.PosY = y
  70. self.Width = w
  71. self.Height = h
  72. self.AnimationTime = at
  73. }
  74. func (self *IconItem) Adjust(x,y,w,h,at int) {
  75. self.PosX = x
  76. self.PosY = y
  77. self.Width = w
  78. self.Height = h
  79. self.AnimationTime = at
  80. if self.Label != nil {
  81. self.Label.SetCanvasHWND(self.Parent.GetCanvasHWND())
  82. }
  83. self.CreateImgSurf()
  84. self.AdjustLinkPage()
  85. }
  86. func (self *IconItem) GetCmdPath() string {
  87. return self.CmdPath
  88. }
  89. func (self *IconItem) SetCmdPath( path string) {
  90. self.CmdPath = path
  91. }
  92. func (self *IconItem) SetMyType( thetype int ) {
  93. self.MyType = thetype
  94. }
  95. func (self *IconItem) GetMyType() int {
  96. return self.MyType
  97. }
  98. func (self *IconItem) GetIconIndex() int {
  99. return self.IconIndex
  100. }
  101. func (self *IconItem) SetIconIndex( idx int) {
  102. self.IconIndex = idx
  103. }
  104. func (self *IconItem) GetIndex() int {
  105. return self.Index
  106. }
  107. func (self *IconItem) SetIndex(i int) {
  108. self.Index = i
  109. }
  110. func (self *IconItem) SetParent(p interface{} ) {
  111. self.Parent = p.(PageInterface)
  112. }
  113. func (self *IconItem) SetLabelColor(col *color.Color) {
  114. self.Label.SetColor(col)
  115. }
  116. func (self *IconItem) GetLabelText() string {
  117. return self.Label.GetText()
  118. }
  119. func (self *IconItem) SetLabelText(text string) {
  120. self.Label.SetText(text)
  121. }
  122. func (self *IconItem) Coord() (int,int) {
  123. return self.PosX,self.PosY
  124. }
  125. func (self *IconItem) NewCoord(x,y int) {
  126. self.PosX = x
  127. self.PosY = y
  128. }
  129. func (self *IconItem) TotalWidth() int {
  130. return 0
  131. }
  132. func (self *IconItem) Size() (int,int) {
  133. return self.Width,self.Height
  134. }
  135. func (self *IconItem) AddLabel(text string, fontobj *ttf.Font) {
  136. if self.Label == nil {
  137. l:= NewLabel()
  138. self.Label = l
  139. }
  140. self.Label.Init(text,fontobj,nil)
  141. }
  142. func (self *IconItem) GetLinkPage() PageInterface {
  143. return self.LinkPage
  144. }
  145. func (self *IconItem) AdjustLinkPage() {
  146. if self.MyType == ICON_TYPES["DIR"] && self.LinkPage != nil {
  147. self.LinkPage.SetIndex(0)
  148. self.LinkPage.SetAlign(ALIGN["SLeft"])
  149. self.LinkPage.UpdateIconNumbers()
  150. self.LinkPage.SetScreen(self.Parent.GetScreen())
  151. self.LinkPage.SetCanvasHWND( (self.Parent.GetScreen()).CanvasHWND )
  152. self.LinkPage.SetFootMsg([5]string{ "Nav.","","","Back","Enter" } )
  153. if self.LinkPage.GetAlign() == ALIGN["HLeft"] {
  154. self.LinkPage.AdjustHLeftAlign()
  155. }else if self.LinkPage.GetAlign() == ALIGN["SLeft"] {
  156. self.LinkPage.AdjustSAutoLeftAlign()
  157. if self.LinkPage.GetIconNumbers() > 1 {
  158. self.LinkPage.SetPsIndex(1)
  159. self.LinkPage.SetIconIndex ( 1 )
  160. }
  161. }
  162. }
  163. }
  164. func (self *IconItem) GetImgSurf() *sdl.Surface {
  165. return self.ImgSurf
  166. }
  167. func (self *IconItem) SetImgSurf(newsurf *sdl.Surface) {
  168. self.ImgSurf = newsurf
  169. }
  170. func (self *IconItem) CreateImgSurf() {
  171. if self.ImgSurf == nil && self.ImageName != "" {
  172. self.ImgSurf = image.Load(self.ImageName)
  173. if int(self.ImgSurf.W) > IconWidth || int(self.ImgSurf.H) > IconHeight {
  174. self.ImgSurf = transform.Scale(self.ImgSurf,IconWidth,IconHeight)
  175. }
  176. }
  177. }
  178. func (self *IconItem) ChangeImgSurfColor(col *color.Color) {
  179. utils.ColorSurface(self.ImgSurf,col)
  180. }
  181. func (self *IconItem) Clear() {
  182. }
  183. func (self *IconItem) GetCmdInvoke() PluginInterface {
  184. return self.CmdInvoke
  185. }
  186. func (self *IconItem) Draw() {
  187. parent_x,parent_y := self.Parent.Coord()
  188. if self.Label != nil {
  189. // lab_x,lab_y := self.Label.Coord()
  190. lab_w,lab_h:= self.Label.Size()
  191. if self.Align == ALIGN["VCenter"] {
  192. // fmt.Println("IconItem Draw VCenter:",lab_w,lab_h,self.Label.GetText())
  193. self.Label.NewCoord( self.PosX - lab_w/2 + parent_x, self.PosY + self.Height/2+6+parent_y)
  194. }else if self.Align == ALIGN["HLeft"] {
  195. self.Label.NewCoord( self.PosX + self.Width/2+3+parent_x, self.PosY - lab_h/2 + parent_y)
  196. }
  197. self.Label.Draw()
  198. }
  199. if self.ImgSurf != nil {
  200. surface.Blit(self.Parent.GetCanvasHWND(), self.ImgSurf,draw.MidRect(self.PosX + parent_x, self.PosY + parent_y,
  201. self.Width,self.Height, Width, Height),nil)
  202. }
  203. }