u8g2_box.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. u8g2_box.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8g2.h"
  28. /*
  29. draw a filled box
  30. restriction: does not work for w = 0 or h = 0
  31. */
  32. void u8g2_DrawBox(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h)
  33. {
  34. #ifdef U8G2_WITH_INTERSECTION
  35. if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 )
  36. return;
  37. #endif /* U8G2_WITH_INTERSECTION */
  38. while( h != 0 )
  39. {
  40. u8g2_DrawHVLine(u8g2, x, y, w, 0);
  41. y++;
  42. h--;
  43. }
  44. }
  45. /*
  46. draw a frame (empty box)
  47. restriction: does not work for w = 0 or h = 0
  48. */
  49. void u8g2_DrawFrame(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h)
  50. {
  51. u8g2_uint_t xtmp = x;
  52. #ifdef U8G2_WITH_INTERSECTION
  53. if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 )
  54. return;
  55. #endif /* U8G2_WITH_INTERSECTION */
  56. u8g2_DrawHVLine(u8g2, x, y, w, 0);
  57. if (h >= 2) {
  58. h-=2;
  59. y++;
  60. if (h > 0) {
  61. u8g2_DrawHVLine(u8g2, x, y, h, 1);
  62. x+=w;
  63. x--;
  64. u8g2_DrawHVLine(u8g2, x, y, h, 1);
  65. y+=h;
  66. }
  67. u8g2_DrawHVLine(u8g2, xtmp, y, w, 0);
  68. }
  69. }
  70. void u8g2_DrawRBox(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h, u8g2_uint_t r)
  71. {
  72. u8g2_uint_t xl, yu;
  73. u8g2_uint_t yl, xr;
  74. #ifdef U8G2_WITH_INTERSECTION
  75. if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 )
  76. return;
  77. #endif /* U8G2_WITH_INTERSECTION */
  78. xl = x;
  79. xl += r;
  80. yu = y;
  81. yu += r;
  82. xr = x;
  83. xr += w;
  84. xr -= r;
  85. xr -= 1;
  86. yl = y;
  87. yl += h;
  88. yl -= r;
  89. yl -= 1;
  90. u8g2_DrawDisc(u8g2, xl, yu, r, U8G2_DRAW_UPPER_LEFT);
  91. u8g2_DrawDisc(u8g2, xr, yu, r, U8G2_DRAW_UPPER_RIGHT);
  92. u8g2_DrawDisc(u8g2, xl, yl, r, U8G2_DRAW_LOWER_LEFT);
  93. u8g2_DrawDisc(u8g2, xr, yl, r, U8G2_DRAW_LOWER_RIGHT);
  94. {
  95. u8g2_uint_t ww, hh;
  96. ww = w;
  97. ww -= r;
  98. ww -= r;
  99. xl++;
  100. yu++;
  101. if ( ww >= 3 )
  102. {
  103. ww -= 2;
  104. u8g2_DrawBox(u8g2, xl, y, ww, r+1);
  105. u8g2_DrawBox(u8g2, xl, yl, ww, r+1);
  106. }
  107. hh = h;
  108. hh -= r;
  109. hh -= r;
  110. //h--;
  111. if ( hh >= 3 )
  112. {
  113. hh -= 2;
  114. u8g2_DrawBox(u8g2, x, yu, w, hh);
  115. }
  116. }
  117. }
  118. void u8g2_DrawRFrame(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h, u8g2_uint_t r)
  119. {
  120. u8g2_uint_t xl, yu;
  121. #ifdef U8G2_WITH_INTERSECTION
  122. if ( u8g2_IsIntersection(u8g2, x, y, x+w, y+h) == 0 )
  123. return;
  124. #endif /* U8G2_WITH_INTERSECTION */
  125. xl = x;
  126. xl += r;
  127. yu = y;
  128. yu += r;
  129. {
  130. u8g2_uint_t yl, xr;
  131. xr = x;
  132. xr += w;
  133. xr -= r;
  134. xr -= 1;
  135. yl = y;
  136. yl += h;
  137. yl -= r;
  138. yl -= 1;
  139. u8g2_DrawCircle(u8g2, xl, yu, r, U8G2_DRAW_UPPER_LEFT);
  140. u8g2_DrawCircle(u8g2, xr, yu, r, U8G2_DRAW_UPPER_RIGHT);
  141. u8g2_DrawCircle(u8g2, xl, yl, r, U8G2_DRAW_LOWER_LEFT);
  142. u8g2_DrawCircle(u8g2, xr, yl, r, U8G2_DRAW_LOWER_RIGHT);
  143. }
  144. {
  145. u8g2_uint_t ww, hh;
  146. ww = w;
  147. ww -= r;
  148. ww -= r;
  149. hh = h;
  150. hh -= r;
  151. hh -= r;
  152. xl++;
  153. yu++;
  154. if ( ww >= 3 )
  155. {
  156. ww -= 2;
  157. h--;
  158. u8g2_DrawHLine(u8g2, xl, y, ww);
  159. u8g2_DrawHLine(u8g2, xl, y+h, ww);
  160. }
  161. if ( hh >= 3 )
  162. {
  163. hh -= 2;
  164. w--;
  165. u8g2_DrawVLine(u8g2, x, yu, hh);
  166. u8g2_DrawVLine(u8g2, x+w, yu, hh);
  167. }
  168. }
  169. }