icon_pool.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package UI
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "strings"
  7. "github.com/veandco/go-sdl2/sdl"
  8. "github.com/cuu/gogame/image"
  9. )
  10. type IconPool struct {
  11. GameShellIconPath string
  12. Icons map[string]*sdl.Surface
  13. }
  14. func NewIconPool() *IconPool {
  15. i := &IconPool{}
  16. i.GameShellIconPath = SkinMap("sysgo/gameshell/icons/")
  17. i.Icons = make(map[string]*sdl.Surface)
  18. return i
  19. }
  20. func (self *IconPool) Init() {
  21. files, err := ioutil.ReadDir(self.GameShellIconPath)
  22. if err != nil {
  23. log.Fatal(err)
  24. return
  25. }
  26. for _, f := range files {
  27. if f.IsDir() {
  28. //pass
  29. } else {
  30. if strings.HasSuffix(f.Name(), ".png") == true {
  31. keyname := strings.Split(f.Name(), ".")
  32. if len(keyname) > 1 {
  33. self.Icons[keyname[0]] = image.Load(self.GameShellIconPath + "/" + f.Name())
  34. }
  35. }
  36. }
  37. }
  38. }
  39. func (self *IconPool) GetImgSurf(keyname string) *sdl.Surface {
  40. if _, ok := self.Icons[keyname]; ok {
  41. return self.Icons[keyname]
  42. } else {
  43. icon_file := self.GameShellIconPath+"/"+keyname+".png"
  44. if IsAFile(icon_file) {
  45. self.Icons[keyname] = image.Load(icon_file)
  46. return self.Icons[keyname]
  47. }else {
  48. fmt.Println("IconPool GetImgSurf ", keyname, " failed")
  49. return nil
  50. }
  51. }
  52. }
  53. func (self *IconPool) Width(keyname string) int {
  54. if _,ok := self.Icons[keyname]; ok {
  55. return int(self.Icons[keyname].W)
  56. }else {
  57. fmt.Println("IconPool lookup ", keyname, " failed")
  58. return 0
  59. }
  60. }
  61. func (self *IconPool) Height(keyname string) int {
  62. if _,ok := self.Icons[keyname]; ok {
  63. return int(self.Icons[keyname].W)
  64. }else {
  65. fmt.Println("IconPool lookup ", keyname, " failed")
  66. return 0
  67. }
  68. }
  69. var MyIconPool *IconPool
  70. // = NewIconPool()