2
0

VisualTheme.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include <stdint.h>
  24. #include "VisualTheme.h"
  25. #include "../api/imageAPI.h"
  26. #include "../api/drawAPI.h"
  27. namespace dsr {
  28. // The default theme
  29. // Copy and modify and compile with theme_create to get a custom theme
  30. static const ReadableString defaultThemeCode =
  31. UR"QUOTE(
  32. # Helper methods
  33. BEGIN: generate_rounded_rectangle
  34. # Dimensions of the result image
  35. INPUT: FixedPoint, width
  36. INPUT: FixedPoint, height
  37. # The whole pixel radius from center points to the end of the image
  38. INPUT: FixedPoint, corner
  39. # The subtracted offset from the radius to create a border on certain channels
  40. INPUT: FixedPoint, border
  41. # Create the result image
  42. OUTPUT: ImageU8, resultImage
  43. CREATE: resultImage, width, height
  44. # Limit outer radius to half of the image's minimum dimension
  45. MIN: radius<FixedPoint>, width, height
  46. MUL: radius, radius, 0.5
  47. MIN: radius, radius, corner
  48. ROUND: radius, radius
  49. # Place the inner radius for drawing
  50. SUB: innerRadius<FixedPoint>, corner, border
  51. # Use +- 0.5 pixel offsets for fake anti-aliasing
  52. ADD: radiusOut<FixedPoint>, innerRadius, 0.5
  53. ADD: radiusIn<FixedPoint>, innerRadius, -0.5
  54. # Calculate dimensions for drawing
  55. SUB: w2<FixedPoint>, width, radius
  56. SUB: w3<FixedPoint>, w2, radius
  57. SUB: w4<FixedPoint>, width, border
  58. SUB: w4, w4, border
  59. SUB: h2<FixedPoint>, height, radius
  60. SUB: h3<FixedPoint>, h2, radius
  61. SUB: r2<FixedPoint>, radius, border
  62. # Draw
  63. FADE_REGION_RADIAL: resultImage, 0, 0, radius, radius, radius, radius, radiusIn, 255, radiusOut, 0
  64. FADE_REGION_RADIAL: resultImage, w2, 0, radius, radius, 0, radius, radiusIn, 255, radiusOut, 0
  65. FADE_REGION_RADIAL: resultImage, 0, h2, radius, radius, radius, 0, radiusIn, 255, radiusOut, 0
  66. FADE_REGION_RADIAL: resultImage, w2, h2, radius, radius, 0, 0, radiusIn, 255, radiusOut, 0
  67. RECTANGLE: resultImage, radius, border, w3, r2, 255
  68. RECTANGLE: resultImage, radius, h2, w3, r2, 255
  69. RECTANGLE: resultImage, border, radius, w4, h3, 255
  70. END:
  71. BEGIN: Button
  72. INPUT: FixedPoint, width
  73. INPUT: FixedPoint, height
  74. INPUT: FixedPoint, pressed
  75. INPUT: FixedPoint, red
  76. INPUT: FixedPoint, green
  77. INPUT: FixedPoint, blue
  78. OUTPUT: ImageRgbaU8, colorImage
  79. # Scale by 2 / 255 so that 127.5 represents full intensity in patternImage
  80. MUL: normRed<FixedPoint>, red, 0.007843138
  81. MUL: normGreen<FixedPoint>, green, 0.007843138
  82. MUL: normBlue<FixedPoint>, blue, 0.007843138
  83. CREATE: patternImage<ImageU8>, width, height
  84. MUL: pressDarknessHigh<FixedPoint>, pressed, 80
  85. MUL: pressDarknessLow<FixedPoint>, pressed, 10
  86. SUB: highLuma<FixedPoint>, 150, pressDarknessHigh
  87. SUB: lowLuma<FixedPoint>, 100, pressDarknessLow
  88. FADE_LINEAR: patternImage, 0, 0, highLuma, 0, height, lowLuma
  89. CALL: generate_rounded_rectangle, lumaImage<ImageU8>, width, height, 12, 2
  90. MUL: lumaImage, lumaImage, patternImage, 0.003921569
  91. CALL: generate_rounded_rectangle, visImage<ImageU8>, width, height, 12, 0
  92. MUL: redImage<ImageU8>, lumaImage, normRed
  93. MUL: greenImage<ImageU8>, lumaImage, normGreen
  94. MUL: blueImage<ImageU8>, lumaImage, normBlue
  95. PACK_RGBA: colorImage, redImage, greenImage, blueImage, visImage
  96. END:
  97. BEGIN: ScrollButton
  98. INPUT: FixedPoint, width
  99. INPUT: FixedPoint, height
  100. INPUT: FixedPoint, pressed
  101. INPUT: FixedPoint, red
  102. INPUT: FixedPoint, green
  103. INPUT: FixedPoint, blue
  104. OUTPUT: ImageRgbaU8, colorImage
  105. # Scale by 2 / 255 so that 127.5 represents full intensity in patternImage
  106. MUL: normRed<FixedPoint>, red, 0.007843138
  107. MUL: normGreen<FixedPoint>, green, 0.007843138
  108. MUL: normBlue<FixedPoint>, blue, 0.007843138
  109. CREATE: patternImage<ImageU8>, width, height
  110. MUL: pressDarknessHigh<FixedPoint>, pressed, 80
  111. MUL: pressDarknessLow<FixedPoint>, pressed, 10
  112. SUB: highLuma<FixedPoint>, 150, pressDarknessHigh
  113. SUB: lowLuma<FixedPoint>, 100, pressDarknessLow
  114. FADE_LINEAR: patternImage, 0, 0, highLuma, 0, height, lowLuma
  115. CALL: generate_rounded_rectangle, lumaImage<ImageU8>, width, height, 3, 1
  116. MUL: lumaImage, lumaImage, patternImage, 0.003921569
  117. MUL: redImage<ImageU8>, lumaImage, normRed
  118. MUL: greenImage<ImageU8>, lumaImage, normGreen
  119. MUL: blueImage<ImageU8>, lumaImage, normBlue
  120. PACK_RGBA: colorImage, redImage, greenImage, blueImage, 255
  121. END:
  122. BEGIN: Panel
  123. INPUT: FixedPoint, width
  124. INPUT: FixedPoint, height
  125. INPUT: FixedPoint, red
  126. INPUT: FixedPoint, green
  127. INPUT: FixedPoint, blue
  128. OUTPUT: ImageRgbaU8, colorImage
  129. CREATE: colorImage, width, height
  130. SUB: w2<FixedPoint>, width, 2
  131. SUB: h2<FixedPoint>, height, 2
  132. RECTANGLE: colorImage, 1, 1, w2, h2, red, green, blue, 255
  133. END:
  134. BEGIN: ListBox
  135. INPUT: FixedPoint, width
  136. INPUT: FixedPoint, height
  137. INPUT: FixedPoint, red
  138. INPUT: FixedPoint, green
  139. INPUT: FixedPoint, blue
  140. OUTPUT: ImageRgbaU8, colorImage
  141. CREATE: colorImage, width, height
  142. SUB: w2<FixedPoint>, width, 2
  143. SUB: h2<FixedPoint>, height, 2
  144. RECTANGLE: colorImage, 1, 1, w2, h2, red, green, blue, 255
  145. END:
  146. )QUOTE";
  147. class VisualThemeImpl {
  148. public:
  149. MediaMachine machine;
  150. // Constructor
  151. VisualThemeImpl(const ReadableString& mediaCode) : machine(machine_create(mediaCode)) {}
  152. // Destructor
  153. virtual ~VisualThemeImpl() {}
  154. };
  155. static VisualTheme defaultTheme;
  156. VisualTheme theme_getDefault() {
  157. if (!(defaultTheme.get())) {
  158. defaultTheme = theme_create(defaultThemeCode);
  159. }
  160. return defaultTheme;
  161. }
  162. VisualTheme theme_create(const ReadableString& mediaCode) {
  163. return std::make_shared<VisualThemeImpl>(mediaCode);
  164. }
  165. MediaMethod theme_getScalableImage(const VisualTheme& theme, const ReadableString &name) {
  166. return machine_getMethod(theme->machine, name);
  167. }
  168. }