table.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package stats
  2. import (
  3. "github.com/g3n/engine/gls"
  4. "github.com/g3n/engine/gui"
  5. )
  6. // StatsTable is a gui.Table panel with statistics
  7. type StatsTable struct {
  8. *gui.Table // embedded table panel
  9. fields []*field // array of fields to show
  10. }
  11. type field struct {
  12. id string
  13. row int
  14. }
  15. // NewStatsTable creates and returns a pointer to a new statistics table panel
  16. func NewStatsTable(width, height float32, gs *gls.GLS) *StatsTable {
  17. // Creates table panel
  18. st := new(StatsTable)
  19. t, err := gui.NewTable(width, height, []gui.TableColumn{
  20. {Id: "f", Header: "Stat", Width: 50, Minwidth: 32, Align: gui.AlignRight, Format: "%s", Resize: true, Expand: 2},
  21. {Id: "v", Header: "Value", Width: 50, Minwidth: 32, Align: gui.AlignRight, Format: "%d", Resize: false, Expand: 1},
  22. })
  23. if err != nil {
  24. panic(err)
  25. }
  26. st.Table = t
  27. st.ShowHeader(false)
  28. // Add rows
  29. st.addRow("shaders", "Shaders:")
  30. st.addRow("vaos", "Vaos:")
  31. st.addRow("buffers", "Buffers:")
  32. st.addRow("textures", "Textures:")
  33. st.addRow("unisets", "Uniforms/frame:")
  34. st.addRow("drawcalls", "Draw calls/frame:")
  35. st.addRow("cgocalls", "CGO calls/frame:")
  36. return st
  37. }
  38. // Update updates the table values from the specified stats table
  39. func (st *StatsTable) Update(s *Stats) {
  40. for i := 0; i < len(st.fields); i++ {
  41. f := st.fields[i]
  42. switch f.id {
  43. case "shaders":
  44. st.Table.SetCell(f.row, "v", s.Glstats.Shaders)
  45. case "vaos":
  46. st.Table.SetCell(f.row, "v", s.Glstats.Vaos)
  47. case "buffers":
  48. st.Table.SetCell(f.row, "v", s.Glstats.Buffers)
  49. case "textures":
  50. st.Table.SetCell(f.row, "v", s.Glstats.Textures)
  51. case "unisets":
  52. st.Table.SetCell(f.row, "v", s.Unisets)
  53. case "drawcalls":
  54. st.Table.SetCell(f.row, "v", s.Drawcalls)
  55. case "cgocalls":
  56. st.Table.SetCell(f.row, "v", s.Cgocalls)
  57. }
  58. }
  59. }
  60. func (st *StatsTable) addRow(id, label string) {
  61. f := new(field)
  62. f.id = id
  63. f.row = st.Table.RowCount()
  64. st.Table.AddRow(map[string]interface{}{"f": label, "v": 0})
  65. st.fields = append(st.fields, f)
  66. }