You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. ScaleComponent.cpp
  4. Created: 27 Aug 2021 1:26:08pm
  5. Author: discoDSP
  6. ==============================================================================
  7. */
  8. #include "../PluginProcessor.h"
  9. #include "ScaleComponent.h"
  10. //==============================================================================
  11. ScalableComponent::ScalableComponent(ObxdAudioProcessor* owner_)
  12. : scaleFactor(0.0f),
  13. isHighResolutionDisplay(false),
  14. processor(owner_)
  15. {
  16. setScaleFactor(1.0f, false);
  17. }
  18. ScalableComponent::~ScalableComponent()
  19. {
  20. }
  21. void ScalableComponent::setScaleFactor(float newScaleFactor, bool newIsHighResolutionDisplay)
  22. {
  23. // until we get a freely scalable editor !!!
  24. jassert(newScaleFactor == 1.0f || newScaleFactor == 1.5f || newScaleFactor == 2.0f);
  25. if (scaleFactor != newScaleFactor || isHighResolutionDisplay != newIsHighResolutionDisplay)
  26. {
  27. scaleFactor = newScaleFactor;
  28. isHighResolutionDisplay = newIsHighResolutionDisplay;
  29. scaleFactorChanged();
  30. }
  31. }
  32. float ScalableComponent::getScaleImage(){
  33. float scale = 1.0;
  34. if (!isHighResolutionDisplay)
  35. {
  36. if (getScaleFactor() == 1.5f)
  37. {
  38. scale= 0.75f;
  39. }
  40. else if (getScaleFactor() == 2.0f)
  41. {
  42. scale= 0.5f;
  43. }
  44. } else {
  45. if (getScaleFactor() == 1.0f) //2x image
  46. {
  47. scale= 0.5f;
  48. }
  49. else if (getScaleFactor() == 1.5f) //4x images =>150%
  50. {
  51. scale= (0.25f + 0.125f);
  52. }
  53. else if (getScaleFactor() == 2.0f) //4x images =>200x
  54. {
  55. scale= 0.5f;
  56. }
  57. }
  58. return scale;
  59. };
  60. float ScalableComponent::getScaleFactor() const
  61. {
  62. return scaleFactor;
  63. }
  64. bool ScalableComponent::getIsHighResolutionDisplay() const
  65. {
  66. return isHighResolutionDisplay;
  67. }
  68. int ScalableComponent::getScaleInt(){
  69. int scaleFactorInt = 1;
  70. if (scaleFactor == 1.5f)
  71. scaleFactorInt = 2;
  72. if (scaleFactor == 2.0f)
  73. scaleFactorInt = 4;
  74. if (isHighResolutionDisplay){
  75. scaleFactorInt *= 2;
  76. }
  77. if (scaleFactorInt> 4){
  78. scaleFactorInt=4;
  79. }
  80. return scaleFactorInt;
  81. }
  82. Image ScalableComponent::getScaledImageFromCache(const String& imageName,
  83. float scaleFactor,
  84. bool isHighResolutionDisplay)
  85. {
  86. jassert(scaleFactor == 1.0f || scaleFactor == 1.5f || scaleFactor == 2.0f);
  87. this->isHighResolutionDisplay = isHighResolutionDisplay;
  88. int scaleFactorInt = getScaleInt();
  89. String resourceName = imageName + "_png";
  90. if (scaleFactorInt != 1){
  91. resourceName = imageName + String::formatted("%dx_png", scaleFactorInt);
  92. }
  93. int size = 0;
  94. File skin;
  95. if (processor){
  96. File f(processor->getCurrentSkinFolder());
  97. if (f.isDirectory()){
  98. skin=f;
  99. }
  100. }
  101. const char* data = nullptr;
  102. String image_file = imageName;
  103. if (scaleFactorInt ==1)
  104. image_file += ".png";
  105. else
  106. image_file += String::formatted("@%dx.png", scaleFactorInt);
  107. DBG(" Loaf image: " << image_file);
  108. File file = skin.getChildFile(image_file);
  109. if (file.exists()){
  110. return ImageCache::getFromFile(file);
  111. } else {
  112. data = BinaryData::getNamedResource((const char*)resourceName.toUTF8(), size);
  113. DBG(" Image: " << resourceName);
  114. return ImageCache::getFromMemory(data, size);
  115. }
  116. }
  117. void ScalableComponent::scaleFactorChanged()
  118. {
  119. }