icon_item.go 5.3 KB

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