ZenScript main repository
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package org.openzen.drawablegui;
  7. /**
  8. *
  9. * @author Hoofdgebruiker
  10. */
  11. public interface DPath {
  12. public static DPath line(float x1, float y1, float x2, float y2) {
  13. return tracer -> {
  14. tracer.moveTo(x1, y1);
  15. tracer.lineTo(x2, y2);
  16. };
  17. }
  18. public static DPath circle(float x, float y, float radius) {
  19. // see http://spencermortensen.com/articles/bezier-circle/
  20. return tracer -> {
  21. float c = radius * 0.551915024494f;
  22. tracer.moveTo(x, y + radius);
  23. tracer.bezierCubic(
  24. x + c, y + radius,
  25. x + radius, y + c,
  26. x + radius, y);
  27. tracer.bezierCubic(
  28. x + radius, y - c,
  29. x + c, y - radius,
  30. x, y - radius);
  31. tracer.bezierCubic(
  32. x - c, y - radius,
  33. x - radius, y - c,
  34. x - radius, y);
  35. tracer.bezierCubic(
  36. x - radius, y + c,
  37. x - c, y + radius,
  38. x, y + radius);
  39. };
  40. }
  41. public static DPath rectangle(float x, float y, float width, float height) {
  42. return tracer -> {
  43. tracer.moveTo(x, y);
  44. tracer.lineTo(x + width, y);
  45. tracer.lineTo(x + width, y + height);
  46. tracer.lineTo(x, y + height);
  47. tracer.close();
  48. };
  49. }
  50. public static DPath roundedRectangle(float x, float y, float width, float height, float radius) {
  51. if (radius < 0.01f)
  52. return rectangle(x, y, width, height);
  53. return tracer -> {
  54. float c = radius - radius * 0.551915024494f;
  55. tracer.moveTo(x + width - radius, y + height);
  56. tracer.bezierCubic(
  57. x + width - c, y + height,
  58. x + width, y + height - c,
  59. x + width, y + height - radius);
  60. tracer.lineTo(x + width, y + radius);
  61. tracer.bezierCubic(
  62. x + width, y + c,
  63. x + width - c, y,
  64. x + width - radius, y);
  65. tracer.lineTo(x + radius, y);
  66. tracer.bezierCubic(
  67. x + c, y,
  68. x, y + c,
  69. x, y + radius);
  70. tracer.lineTo(x, y + height - radius);
  71. tracer.bezierCubic(
  72. x, y + height - c,
  73. x + c, y + height,
  74. x + radius, y + height);
  75. tracer.close();
  76. };
  77. }
  78. public static DPath roundedRectangle(float x, float y, float width, float height, float radiusTopLeft, float radiusTopRight, float radiusBottomLeft, float radiusBottomRight) {
  79. return tracer -> {
  80. float cTopLeft = radiusTopLeft - radiusTopLeft * 0.551915024494f;
  81. float cTopRight = radiusTopRight - radiusTopRight * 0.551915024494f;
  82. float cBottomLeft = radiusBottomLeft - radiusBottomLeft * 0.551915024494f;
  83. float cBottomRight = radiusBottomRight - radiusBottomRight * 0.551915024494f;
  84. tracer.moveTo(x + width - radiusBottomRight, y + height);
  85. if (radiusBottomRight > 0.01f) {
  86. tracer.bezierCubic(
  87. x + width - cBottomRight, y + height,
  88. x + width, y + height - cBottomRight,
  89. x + width, y + height - radiusBottomRight);
  90. }
  91. tracer.lineTo(x + width, y + radiusTopRight);
  92. if (radiusTopRight > 0.01f) {
  93. tracer.bezierCubic(
  94. x + width, y + cTopRight,
  95. x + width - cTopRight, y,
  96. x + width - radiusTopRight, y);
  97. }
  98. tracer.lineTo(x + radiusTopLeft, y);
  99. if (radiusTopLeft > 0.01f) {
  100. tracer.bezierCubic(
  101. x + cTopLeft, y,
  102. x, y + cTopLeft,
  103. x, y + radiusTopLeft);
  104. }
  105. tracer.lineTo(x, y + height - radiusBottomLeft);
  106. if (radiusBottomLeft > 0.01f) {
  107. tracer.bezierCubic(
  108. x, y + height - cBottomLeft,
  109. x + cBottomLeft, y + height,
  110. x + radiusBottomLeft, y + height);
  111. }
  112. tracer.close();
  113. };
  114. }
  115. void trace(DPathTracer tracer);
  116. }