hier_list_item.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package UI
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. "github.com/veandco/go-sdl2/ttf"
  8. "github.com/cuu/gogame/rect"
  9. "github.com/cuu/gogame/color"
  10. "github.com/cuu/gogame/draw"
  11. "github.com/cuu/gogame/surface"
  12. )
  13. type ListItemIcon struct {
  14. IconItem
  15. }
  16. func NewListItemIcon() *ListItemIcon {
  17. p := &ListItemIcon{}
  18. p.MyType = ICON_TYPES["EXE"]
  19. p.Align = ALIGN["VCenter"]
  20. p.Width = 18
  21. p.Height = 18
  22. return p
  23. }
  24. func (self *ListItemIcon) Draw() {
  25. _, h := self.Parent.Size()
  26. rect_ := rect.Rect(self.PosX, self.PosY+(h-self.Height)/2, self.Width, self.Height)
  27. surface.Blit(self.Parent.GetCanvasHWND(), self.ImgSurf, &rect_, nil)
  28. }
  29. /// [..] [.]
  30. type HierListItem struct {
  31. ListItem
  32. MyType int
  33. Path string
  34. Active bool
  35. Playing bool
  36. }
  37. var HierListItemDefaultHeight = 32
  38. func NewHierListItem() *HierListItem {
  39. p := &HierListItem{}
  40. p.Labels = make(map[string]LabelInterface)
  41. p.Icons = make(map[string]IconItemInterface)
  42. p.Fonts = make(map[string]*ttf.Font)
  43. p.MyType = ICON_TYPES["EXE"]
  44. p.Height = HierListItemDefaultHeight
  45. p.Width = 0
  46. return p
  47. }
  48. func (self *HierListItem) IsFile() bool {
  49. if self.MyType == ICON_TYPES["FILE"] {
  50. return true
  51. }
  52. return false
  53. }
  54. func (self *HierListItem) IsDir() bool {
  55. if self.MyType == ICON_TYPES["DIR"] {
  56. return true
  57. }
  58. return false
  59. }
  60. func (self *HierListItem) Init(text string) {
  61. l := NewLabel()
  62. l.PosX = 20
  63. if self.Parent == nil {
  64. fmt.Println("Parent nil")
  65. return
  66. }
  67. l.SetCanvasHWND(self.Parent.GetCanvasHWND())
  68. if self.IsDir() == true || self.IsFile() == true {
  69. self.Path = text
  70. }
  71. label_text := filepath.Base(text)
  72. ext := filepath.Ext(text)
  73. if ext != "" {
  74. alias_file := strings.Replace(text, ext, "", -1) + ".alias"
  75. if FileExists(alias_file) == true {
  76. b, err := ioutil.ReadFile(alias_file)
  77. if err != nil {
  78. fmt.Print(err)
  79. } else {
  80. label_text = string(b)
  81. }
  82. }
  83. }
  84. if self.IsDir() == true {
  85. l.Init(label_text, self.Fonts["normal"], nil)
  86. } else {
  87. l.Init(label_text, self.Fonts["normal"], nil)
  88. }
  89. self.Labels["Text"] = l
  90. }
  91. func (self *HierListItem) Draw() {
  92. x, y := self.Labels["Text"].Coord()
  93. _, h := self.Labels["Text"].Size()
  94. if self.Path != "[..]" {
  95. self.Labels["Text"].NewCoord(23, y)
  96. } else {
  97. self.Labels["Text"].NewCoord(3, y)
  98. }
  99. x, y = self.Labels["Text"].Coord()
  100. self.Labels["Text"].NewCoord(x, self.PosY+(self.Height-h)/2)
  101. self.Labels["Text"].Draw()
  102. /*
  103. w,h := self.Parent.Icons["sys"].Size()
  104. if self.IsDir() == true && self.Path != "[..]" {
  105. self.Parent.Icons["sys"].IconIndex = 0
  106. self.Parent.Icons["sys"].NewCoord(self.PosX+12,self.PosY+(self.Height-h)/2+h/2)
  107. self.Parent.Icons["sys"].Draw()
  108. }
  109. if self.IsFile() == true {
  110. self.Parent.Icons["sys"].IconIndex = 1
  111. self.Parent.Icons["sys"].NewCoord(self.PosX+12,self.PosY+(self.Height-h)/2+h/2)
  112. self.Parent.Icons["sys"].Draw()
  113. }
  114. */
  115. draw.Line(self.Parent.GetCanvasHWND(), &color.Color{169, 169, 169, 255},
  116. self.PosX, self.PosY+self.Height-1, self.PosX+self.Width, self.PosY+self.Height-1, 1)
  117. }