FixedWindowView.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package me.hammerle.snuviengine.api;
  2. public class FixedWindowView implements WindowView {
  3. private final int width;
  4. private final int height;
  5. private final int fontScale;
  6. public FixedWindowView(int width, int height, int fontScale) {
  7. this.width = width;
  8. this.height = height;
  9. this.fontScale = fontScale;
  10. }
  11. @Override
  12. public int getScale(int width, int height) {
  13. return Math.max(width / this.width, 1);
  14. }
  15. @Override
  16. public int getFontScale(int width, int height) {
  17. return fontScale;
  18. }
  19. @Override
  20. public int getWidth(int newWidth, int newHeight) {
  21. return Math.max(getMaxScale(newWidth, newHeight) * width, width);
  22. }
  23. @Override
  24. public int getHeight(int newWidth, int newHeight) {
  25. return Math.max(getMaxScale(newWidth, newHeight) * height, height);
  26. }
  27. private int getMaxScale(int newWidth, int newHeight) {
  28. return Math.max(getNearestScale(newWidth, width), getNearestScale(newHeight, height));
  29. }
  30. private int getNearestScale(int value, int multiple) {
  31. float scale = (float) value / multiple;
  32. int roundedScale = (int) scale;
  33. if(scale == roundedScale) {
  34. return roundedScale;
  35. }
  36. float diff = scale - roundedScale;
  37. roundedScale += diff < 0.5f ? 1 : 0;
  38. return roundedScale;
  39. }
  40. }