VisualTheme.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, 5, 1
  116. MUL: lumaImage, lumaImage, patternImage, 0.003921569
  117. CALL: generate_rounded_rectangle, visImage<ImageU8>, width, height, 5, 0
  118. MUL: redImage<ImageU8>, lumaImage, normRed
  119. MUL: greenImage<ImageU8>, lumaImage, normGreen
  120. MUL: blueImage<ImageU8>, lumaImage, normBlue
  121. PACK_RGBA: colorImage, redImage, greenImage, blueImage, visImage
  122. END:
  123. BEGIN: VerticalScrollBar
  124. INPUT: FixedPoint, width
  125. INPUT: FixedPoint, height
  126. INPUT: FixedPoint, red
  127. INPUT: FixedPoint, green
  128. INPUT: FixedPoint, blue
  129. OUTPUT: ImageRgbaU8, colorImage
  130. CREATE: visImage<ImageU8>, width, height
  131. CREATE: lumaImage<ImageU8>, width, height
  132. FADE_LINEAR: visImage, 0, 0, 128, width, 0, 0
  133. MUL: fadeLength<FixedPoint>, width, 2.0
  134. MUL: maxFadeLength<FixedPoint>, height, 0.5
  135. MIN: fadeLength, fadeLength, maxFadeLength
  136. FADE_REGION_LINEAR: lumaImage, 0, 0, width, fadeLength, 0, 0, 128, 0, fadeLength, 0
  137. SUB: lowerFadeTop<FixedPoint>, height, fadeLength
  138. FADE_REGION_LINEAR: lumaImage, 0, lowerFadeTop, width, fadeLength, 0, 0, 0, 0, fadeLength, 128
  139. # Scale by 2 / 255 so that 127.5 represents full intensity in patternImage
  140. MUL: normRed<FixedPoint>, red, 0.007843138
  141. MUL: normGreen<FixedPoint>, green, 0.007843138
  142. MUL: normBlue<FixedPoint>, blue, 0.007843138
  143. MUL: redImage<ImageU8>, lumaImage, normRed
  144. MUL: greenImage<ImageU8>, lumaImage, normGreen
  145. MUL: blueImage<ImageU8>, lumaImage, normBlue
  146. PACK_RGBA: colorImage, redImage, greenImage, blueImage, visImage
  147. END:
  148. BEGIN: Panel
  149. INPUT: FixedPoint, width
  150. INPUT: FixedPoint, height
  151. INPUT: FixedPoint, red
  152. INPUT: FixedPoint, green
  153. INPUT: FixedPoint, blue
  154. OUTPUT: ImageRgbaU8, colorImage
  155. CREATE: colorImage, width, height
  156. SUB: w2<FixedPoint>, width, 2
  157. SUB: h2<FixedPoint>, height, 2
  158. RECTANGLE: colorImage, 1, 1, w2, h2, red, green, blue, 255
  159. END:
  160. BEGIN: ListBox
  161. INPUT: FixedPoint, width
  162. INPUT: FixedPoint, height
  163. INPUT: FixedPoint, red
  164. INPUT: FixedPoint, green
  165. INPUT: FixedPoint, blue
  166. OUTPUT: ImageRgbaU8, colorImage
  167. CREATE: colorImage, width, height
  168. SUB: w2<FixedPoint>, width, 2
  169. SUB: h2<FixedPoint>, height, 2
  170. RECTANGLE: colorImage, 1, 1, w2, h2, red, green, blue, 255
  171. END:
  172. )QUOTE";
  173. class VisualThemeImpl {
  174. public:
  175. MediaMachine machine;
  176. // Constructor
  177. explicit VisualThemeImpl(const ReadableString& mediaCode) : machine(machine_create(mediaCode)) {}
  178. // Destructor
  179. virtual ~VisualThemeImpl() {}
  180. };
  181. static VisualTheme defaultTheme;
  182. VisualTheme theme_getDefault() {
  183. if (!(defaultTheme.get())) {
  184. defaultTheme = theme_create(defaultThemeCode);
  185. }
  186. return defaultTheme;
  187. }
  188. VisualTheme theme_create(const ReadableString& mediaCode) {
  189. return std::make_shared<VisualThemeImpl>(mediaCode);
  190. }
  191. MediaMethod theme_getScalableImage(const VisualTheme& theme, const ReadableString &name) {
  192. return machine_getMethod(theme->machine, name);
  193. }
  194. }