HighScorePanel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //-----------------------------------------------------------------------------
  2. // HighScorePanel.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Content;
  10. using Microsoft.Xna.Framework.Graphics;
  11. namespace UserInterfaceSample.Controls
  12. {
  13. /// <remarks>
  14. /// This class displays a list of high scores, to give an example of presenting
  15. /// a list of data that the player can scroll through.
  16. /// </remarks>
  17. public class HighScorePanel : ScrollingPanelControl
  18. {
  19. Control resultListControl = null;
  20. SpriteFont titleFont;
  21. SpriteFont headerFont;
  22. SpriteFont detailFont;
  23. public HighScorePanel(ContentManager content)
  24. {
  25. titleFont = content.Load<SpriteFont>("Font\\MenuTitle");
  26. headerFont = content.Load<SpriteFont>("Font\\MenuHeader");
  27. detailFont = content.Load<SpriteFont>("Font\\MenuDetail");
  28. AddChild(new TextControl("High score", titleFont));
  29. AddChild(CreateHeaderControl());
  30. PopulateWithFakeData();
  31. }
  32. private void PopulateWithFakeData()
  33. {
  34. PanelControl newList = new PanelControl();
  35. Random rng = new Random();
  36. for (int i = 0; i < 50; i++)
  37. {
  38. long score = 10000 - i * 10;
  39. TimeSpan time = TimeSpan.FromSeconds(rng.Next(60, 3600));
  40. newList.AddChild(CreateLeaderboardEntryControl("player" + i.ToString(), score, time));
  41. }
  42. newList.LayoutColumn(0, 0, 0);
  43. if (resultListControl != null)
  44. {
  45. RemoveChild(resultListControl);
  46. }
  47. resultListControl = newList;
  48. AddChild(resultListControl);
  49. LayoutColumn(0, 0, 0);
  50. }
  51. protected Control CreateHeaderControl()
  52. {
  53. PanelControl panel = new PanelControl();
  54. panel.AddChild(new TextControl("Player", headerFont, Color.Turquoise, new Vector2(0, 0)));
  55. panel.AddChild(new TextControl("Score", headerFont, Color.Turquoise, new Vector2(200, 0)));
  56. return panel;
  57. }
  58. // Create a Control to display one entry in a leaderboard. The content is broken out into a parameter
  59. // list so that we can easily create a control with fake data when running under the emulator.
  60. //
  61. // Note that for time leaderboards, this function interprets the time as a count in seconds. The
  62. // value posted is simply a long, so your leaderboard might actually measure time in ticks, milliseconds,
  63. // or microfortnights. If that is the case, adjust this function to display appropriately.
  64. protected Control CreateLeaderboardEntryControl(string player, long rating, TimeSpan time)
  65. {
  66. Color textColor = Color.White;
  67. var panel = new PanelControl();
  68. // Player name
  69. panel.AddChild(
  70. new TextControl
  71. {
  72. Text = player,
  73. Font = detailFont,
  74. Color = textColor,
  75. Position = new Vector2(0, 0)
  76. });
  77. // Score
  78. panel.AddChild(
  79. new TextControl
  80. {
  81. Text = String.Format("{0}", rating),
  82. Font = detailFont,
  83. Color = textColor,
  84. Position = new Vector2(200, 0)
  85. });
  86. // Time
  87. panel.AddChild(
  88. new TextControl
  89. {
  90. Text = String.Format("Completed in {0:g}", time),
  91. Font = detailFont,
  92. Color = textColor,
  93. Position = new Vector2(400, 0)
  94. });
  95. return panel;
  96. }
  97. }
  98. }