MenuRenderer.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package pathgame.rendering;
  2. import me.hammerle.snuviengine.api.KeyBinding;
  3. import me.hammerle.snuviengine.api.Renderer;
  4. import pathgame.gameplay.Keys;
  5. import pathgame.gameplay.menu.Menu;
  6. import pathgame.gameplay.menu.MenuButton;
  7. /**
  8. * A container for holding everything about the renderer for the menus
  9. *
  10. * @author julia
  11. */
  12. public class MenuRenderer
  13. {
  14. private final float keyWidths[] = new float[Keys.KEYS.length];
  15. /**
  16. * Recalculates the rendering positions and settings of the menu-elements on
  17. * the screen every rendertick based on the gamelogic in the gametick
  18. *
  19. * @param r the renderer
  20. * @param lag the current lag
  21. * @param menu the current menu
  22. */
  23. public void renderTick(Renderer r, float lag, Menu menu)
  24. {
  25. r.translateTo(0.0f, 0.0f);
  26. float scale = 2.0f;
  27. r.scale(scale, scale);
  28. r.updateMatrix();
  29. scale = 1.0f / scale;
  30. MenuButton[] options = menu.getOptions();
  31. float lastGap = 10.0f;
  32. float baseBoxHeight = 300.0f * scale;
  33. float textBoxPaddingY = (100.0f - (20.0f / 3.0f) * options.length) * scale;
  34. float windowHeight = r.getViewHeight() * scale;
  35. float windowWidth = r.getViewWidth() * scale;
  36. float y = (windowHeight - baseBoxHeight) * 0.5f + textBoxPaddingY;
  37. float textBoxHeight = baseBoxHeight - textBoxPaddingY * 2;
  38. float step = (textBoxHeight - r.getFontRenderer().getHeight()) / (options.length - 1) - lastGap / (options.length - 2);
  39. r.setMixColorEnabled(false);
  40. r.setColorEnabled(true);
  41. r.setTextureEnabled(false);
  42. r.setBlendingEnabled(true);
  43. r.getColorRenderer().drawRectangle(windowWidth * 0.10f, y - textBoxPaddingY * 0.5f, windowWidth * 0.90f, y + textBoxHeight + textBoxPaddingY * 0.5f, 0x50000000);
  44. r.setTextureEnabled(true);
  45. if(!menu.isOptionMenu())
  46. {
  47. for(int i = 0; i < options.length - 1; i++)
  48. {
  49. renderText(options[i].getName(), menu.getActiveIndex() == i, r, windowWidth, y, false);
  50. y += step;
  51. }
  52. y += lastGap;
  53. renderText(options[options.length - 1].getName(), menu.getActiveIndex() == options.length - 1, r, windowWidth, y, false);
  54. }
  55. else
  56. {
  57. float max = Float.MIN_VALUE;
  58. for(int i = 0; i < Keys.KEYS.length; i++)
  59. {
  60. keyWidths[i] = r.getFontRenderer().getSize(getKeyName(Keys.KEYS[i])).getWidth();
  61. if(keyWidths[i] > max)
  62. {
  63. max = keyWidths[i];
  64. }
  65. }
  66. for(int i = 0; i < options.length - 1; i++)
  67. {
  68. boolean active = menu.getActiveIndex() == i;
  69. renderText(options[i].getName(), active, r, windowWidth, y, true);
  70. r.getFontRenderer().drawString(windowWidth * 0.85f - max * 0.5f - keyWidths[i] * 0.5f, y, addColor(getKeyName(Keys.KEYS[i]), active));
  71. y += step;
  72. }
  73. y += lastGap;
  74. renderText(options[options.length - 1].getName(), menu.getActiveIndex() == options.length - 1, r, windowWidth, y, true);
  75. }
  76. }
  77. private void renderText(String s, boolean active, Renderer r, float wWidth, float y, boolean left)
  78. {
  79. if(left)
  80. {
  81. r.getFontRenderer().drawString(wWidth * 0.15f, y, addColor(s, active));
  82. }
  83. else
  84. {
  85. r.getFontRenderer().drawString(wWidth * 0.5f - (r.getFontRenderer().getSize(s).getWidth() * 0.5f), y, addColor(s, active));
  86. }
  87. }
  88. private String addColor(String s, boolean active)
  89. {
  90. return (active ? "&f" : "&7") + s;
  91. }
  92. private String getKeyName(KeyBinding key)
  93. {
  94. if(key.isRebinding())
  95. {
  96. return "[...]";
  97. }
  98. else
  99. {
  100. return key.getName();
  101. }
  102. }
  103. }