image.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/texture"
  7. "image"
  8. )
  9. type Image struct {
  10. Panel // Embedded panel
  11. tex *texture.Texture2D // pointer to image texture
  12. }
  13. // NewImage creates and returns an image panel with the image
  14. // from the specified image used as a texture.
  15. // Initially the size of the panel content area is the exact size of the image.
  16. func NewImage(imgfile string) (image *Image, err error) {
  17. tex, err := texture.NewTexture2DFromImage(imgfile)
  18. if err != nil {
  19. return nil, err
  20. }
  21. return NewImageFromTex(tex), nil
  22. }
  23. // NewImageFromRGBA creates and returns an image panel from the
  24. // specified image
  25. func NewImageFromRGBA(rgba *image.RGBA) *Image {
  26. tex := texture.NewTexture2DFromRGBA(rgba)
  27. return NewImageFromTex(tex)
  28. }
  29. // NewImageFromTex creates and returns an image panel from the specified texture2D
  30. func NewImageFromTex(tex *texture.Texture2D) *Image {
  31. i := new(Image)
  32. i.Panel.Initialize(0, 0)
  33. i.tex = tex
  34. i.Panel.SetContentSize(float32(i.tex.Width()), float32(i.tex.Height()))
  35. i.Material().AddTexture(i.tex)
  36. return i
  37. }
  38. // SetTexture changes the image texture to the specified texture2D.
  39. // It returns a pointer to the previous texture.
  40. func (i *Image) SetTexture(tex *texture.Texture2D) *texture.Texture2D {
  41. prevtex := i.tex
  42. i.Material().RemoveTexture(prevtex)
  43. i.tex = tex
  44. i.Panel.SetContentSize(float32(i.tex.Width()), float32(i.tex.Height()))
  45. i.Material().AddTexture(i.tex)
  46. return prevtex
  47. }
  48. //func (i *Image) Clone() *Image {
  49. //
  50. // return NewImageFromTex(i.tex.Clone())
  51. //}