VisualTheme.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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: generate_rounded_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. INPUT: FixedPoint, radius
  79. INPUT: FixedPoint, border
  80. OUTPUT: ImageRgbaU8, resultImage
  81. # Scale by 2 / 255 so that 127.5 represents full intensity in patternImage
  82. MUL: normRed<FixedPoint>, red, 0.007843138
  83. MUL: normGreen<FixedPoint>, green, 0.007843138
  84. MUL: normBlue<FixedPoint>, blue, 0.007843138
  85. CREATE: patternImage<ImageU8>, width, height
  86. MUL: pressDarknessHigh<FixedPoint>, pressed, 80
  87. MUL: pressDarknessLow<FixedPoint>, pressed, 10
  88. SUB: highLuma<FixedPoint>, 150, pressDarknessHigh
  89. SUB: lowLuma<FixedPoint>, 100, pressDarknessLow
  90. FADE_LINEAR: patternImage, 0, 0, highLuma, 0, height, lowLuma
  91. CALL: generate_rounded_rectangle, lumaImage<ImageU8>, width, height, radius, border
  92. MUL: lumaImage, lumaImage, patternImage, 0.003921569
  93. CALL: generate_rounded_rectangle, visImage<ImageU8>, width, height, radius, 0
  94. MUL: redImage<ImageU8>, lumaImage, normRed
  95. MUL: greenImage<ImageU8>, lumaImage, normGreen
  96. MUL: blueImage<ImageU8>, lumaImage, normBlue
  97. PACK_RGBA: resultImage, redImage, greenImage, blueImage, visImage
  98. END:
  99. BEGIN: Button
  100. INPUT: FixedPoint, width
  101. INPUT: FixedPoint, height
  102. INPUT: FixedPoint, pressed
  103. INPUT: FixedPoint, red
  104. INPUT: FixedPoint, green
  105. INPUT: FixedPoint, blue
  106. OUTPUT: ImageRgbaU8, colorImage
  107. CALL: generate_rounded_button, colorImage, width, height, pressed, red, green, blue, 12, 2
  108. END:
  109. BEGIN: ScrollButton
  110. INPUT: FixedPoint, width
  111. INPUT: FixedPoint, height
  112. INPUT: FixedPoint, pressed
  113. INPUT: FixedPoint, red
  114. INPUT: FixedPoint, green
  115. INPUT: FixedPoint, blue
  116. OUTPUT: ImageRgbaU8, colorImage
  117. CALL: generate_rounded_button, colorImage, width, height, pressed, red, green, blue, 5, 2
  118. END:
  119. BEGIN: VerticalScrollBar
  120. INPUT: FixedPoint, width
  121. INPUT: FixedPoint, height
  122. INPUT: FixedPoint, red
  123. INPUT: FixedPoint, green
  124. INPUT: FixedPoint, blue
  125. OUTPUT: ImageRgbaU8, colorImage
  126. CREATE: visImage<ImageU8>, width, height
  127. CREATE: lumaImage<ImageU8>, width, height
  128. FADE_LINEAR: visImage, 0, 0, 128, width, 0, 0
  129. MUL: fadeLength<FixedPoint>, width, 2.0
  130. MUL: maxFadeLength<FixedPoint>, height, 0.5
  131. MIN: fadeLength, fadeLength, maxFadeLength
  132. FADE_REGION_LINEAR: lumaImage, 0, 0, width, fadeLength, 0, 0, 128, 0, fadeLength, 0
  133. SUB: lowerFadeTop<FixedPoint>, height, fadeLength
  134. FADE_REGION_LINEAR: lumaImage, 0, lowerFadeTop, width, fadeLength, 0, 0, 0, 0, fadeLength, 128
  135. # Scale by 2 / 255 so that 127.5 represents full intensity in patternImage
  136. MUL: normRed<FixedPoint>, red, 0.007843138
  137. MUL: normGreen<FixedPoint>, green, 0.007843138
  138. MUL: normBlue<FixedPoint>, blue, 0.007843138
  139. MUL: redImage<ImageU8>, lumaImage, normRed
  140. MUL: greenImage<ImageU8>, lumaImage, normGreen
  141. MUL: blueImage<ImageU8>, lumaImage, normBlue
  142. PACK_RGBA: colorImage, redImage, greenImage, blueImage, visImage
  143. END:
  144. BEGIN: Panel
  145. INPUT: FixedPoint, width
  146. INPUT: FixedPoint, height
  147. INPUT: FixedPoint, red
  148. INPUT: FixedPoint, green
  149. INPUT: FixedPoint, blue
  150. OUTPUT: ImageRgbaU8, colorImage
  151. CREATE: colorImage, width, height
  152. SUB: w2<FixedPoint>, width, 2
  153. SUB: h2<FixedPoint>, height, 2
  154. RECTANGLE: colorImage, 1, 1, w2, h2, red, green, blue, 255
  155. END:
  156. BEGIN: ListBox
  157. INPUT: FixedPoint, width
  158. INPUT: FixedPoint, height
  159. INPUT: FixedPoint, red
  160. INPUT: FixedPoint, green
  161. INPUT: FixedPoint, blue
  162. OUTPUT: ImageRgbaU8, colorImage
  163. CREATE: colorImage, width, height
  164. SUB: w2<FixedPoint>, width, 4
  165. SUB: h2<FixedPoint>, height, 4
  166. RECTANGLE: colorImage, 2, 2, w2, h2, red, green, blue, 255
  167. END:
  168. )QUOTE";
  169. class VisualThemeImpl {
  170. public:
  171. MediaMachine machine;
  172. // Constructor
  173. explicit VisualThemeImpl(const ReadableString& mediaCode) : machine(machine_create(mediaCode)) {}
  174. // Destructor
  175. virtual ~VisualThemeImpl() {}
  176. };
  177. static VisualTheme defaultTheme;
  178. VisualTheme theme_getDefault() {
  179. if (!(defaultTheme.get())) {
  180. defaultTheme = theme_create(defaultThemeCode);
  181. }
  182. return defaultTheme;
  183. }
  184. VisualTheme theme_create(const ReadableString& mediaCode) {
  185. return std::make_shared<VisualThemeImpl>(mediaCode);
  186. }
  187. MediaMethod theme_getScalableImage(const VisualTheme& theme, const ReadableString &name) {
  188. return machine_getMethod(theme->machine, name);
  189. }
  190. }