icon_item.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/color"
  7. "github.com/cuu/gogame/draw"
  8. "github.com/cuu/gogame/image"
  9. "github.com/cuu/gogame/rect"
  10. "github.com/cuu/gogame/surface"
  11. "github.com/cuu/gogame/transform"
  12. "github.com/cuu/gogame/utils"
  13. )
  14. type IconItemInterface interface {
  15. Init(x, y, w, h, at int)
  16. Adjust(x, y, w, h, at int)
  17. GetCmdPath() string
  18. SetCmdPath(path string)
  19. SetMyType(thetype int)
  20. GetMyType() int
  21. GetIconIndex() int
  22. SetIconIndex(idx int)
  23. GetIndex() int
  24. SetIndex(i int)
  25. SetParent(p interface{})
  26. SetLabelColor(col *color.Color)
  27. SetLabelText(text string)
  28. GetLabelText() string
  29. Coord() (int, int)
  30. NewCoord(x, y int)
  31. Size() (int, int)
  32. NewSize(w, h int)
  33. TotalWidth() int
  34. AddLabel(text string, fontobj *ttf.Font)
  35. GetLinkPage() PageInterface
  36. AdjustLinkPage()
  37. GetImgSurf() *sdl.Surface
  38. SetImgSurf(newsurf *sdl.Surface)
  39. CreateImgSurf()
  40. ChangeImgSurfColor(col *color.Color)
  41. Clear()
  42. GetCmdInvoke() PluginInterface
  43. GetFileName() string
  44. Draw()
  45. DrawTopLeft()
  46. }
  47. type IconItem struct {
  48. Widget
  49. ImageName string
  50. ImgSurf *sdl.Surface
  51. Parent PageInterface
  52. Index int
  53. IconIndex int
  54. MyType int
  55. CmdPath string
  56. CmdInvoke PluginInterface
  57. LinkPage PageInterface
  58. Label LabelInterface
  59. Align int
  60. AnimationTime int
  61. FileName string
  62. }
  63. func NewIconItem() *IconItem {
  64. i := &IconItem{}
  65. i.MyType = ICON_TYPES["EXE"]
  66. i.Align = ALIGN["VCenter"]
  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) GetFileName() string {
  189. return self.FileName
  190. }
  191. func (self *IconItem) DrawTopLeft() {
  192. if self.Parent == nil {
  193. fmt.Println("Error: IconItem Draw Parent nil")
  194. return
  195. }
  196. parent_x, parent_y := self.Parent.Coord()
  197. if self.Label != nil {
  198. // lab_x,lab_y := self.Label.Coord()
  199. lab_w, lab_h := self.Label.Size()
  200. if self.Align == ALIGN["VCenter"] {
  201. // fmt.Println("IconItem Draw VCenter:",lab_w,lab_h,self.Label.GetText())
  202. self.Label.NewCoord(self.PosX-lab_w/2+parent_x, self.PosY+self.Height/2+6+parent_y)
  203. } else if self.Align == ALIGN["HLeft"] {
  204. self.Label.NewCoord(self.PosX+self.Width/2+3+parent_x, self.PosY-lab_h/2+parent_y)
  205. }
  206. self.Label.Draw()
  207. }
  208. if self.ImgSurf != nil {
  209. rect_ := rect.Rect(self.PosX+parent_x, self.PosY+parent_y, self.Width, self.Height) // DIFF
  210. surface.Blit(self.Parent.GetCanvasHWND(), self.ImgSurf, &rect_, nil)
  211. }
  212. }
  213. func (self *IconItem) Draw() {
  214. if self.Parent == nil {
  215. fmt.Println("Error: IconItem Draw Parent nil")
  216. return
  217. }
  218. parent_x, parent_y := self.Parent.Coord()
  219. if self.Label != nil {
  220. // lab_x,lab_y := self.Label.Coord()
  221. lab_w, lab_h := self.Label.Size()
  222. if self.Align == ALIGN["VCenter"] {
  223. // fmt.Println("IconItem Draw VCenter:",lab_w,lab_h,self.Label.GetText())
  224. self.Label.NewCoord(self.PosX-lab_w/2+parent_x, self.PosY+self.Height/2+6+parent_y)
  225. } else if self.Align == ALIGN["HLeft"] {
  226. self.Label.NewCoord(self.PosX+self.Width/2+3+parent_x, self.PosY-lab_h/2+parent_y)
  227. }
  228. self.Label.Draw()
  229. }
  230. if self.ImgSurf != nil {
  231. surface.Blit(self.Parent.GetCanvasHWND(), self.ImgSurf, draw.MidRect(self.PosX+parent_x, self.PosY+parent_y,
  232. self.Width, self.Height, Width, Height), nil)
  233. }
  234. }