Night_Wood

GMLive Analog Clock

Oct 1st, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define main
  2.  
  3. // CONSTANTS
  4. WIDTH   = 720;
  5. HEIGHT  = 720;
  6. xpos    = 32;
  7. ypos    = 32;
  8.  
  9. RADIUS              = 256;
  10. BASE_HUE            = 120;  // blue-ish
  11. HUE_SCALAR          = 0.1;  // how much of the gradient to show? 0 = solid color
  12.  
  13. // Finally make the clock
  14. clock   = make_clock(RADIUS, BASE_HUE, HUE_SCALAR);
  15.  
  16. #define draw
  17.  
  18. var seconds_rad = degtorad((current_second * 6) - 90);
  19. var sx      = xpos + RADIUS + SECOND_HAND_LENGTH * cos(seconds_rad);
  20. var sy      = ypos + RADIUS + SECOND_HAND_LENGTH * sin(seconds_rad);
  21.  
  22. var minutes_rad = degtorad((current_minute * 6) - 90);
  23. var mx      = xpos + RADIUS + MINUTE_HAND_LENGTH * cos(minutes_rad);
  24. var my      = ypos + RADIUS + MINUTE_HAND_LENGTH * sin(minutes_rad);
  25.  
  26. var hours_rad = degtorad((current_hour * 30) - 90);
  27. var hx      = xpos + RADIUS + HOUR_HAND_LENGTH * cos(hours_rad);
  28. var hy      = ypos + RADIUS + HOUR_HAND_LENGTH * sin(hours_rad);
  29.  
  30. // Draw clock background
  31. draw_surface(clock, xpos, ypos);
  32.  
  33. // Draw the hands of the clock
  34. line(xpos + RADIUS, ypos + RADIUS, sx, sy, SECOND_HAND_WIDTH, c_black);
  35. line(xpos + RADIUS, ypos + RADIUS, mx, my, MINUTE_HAND_WIDTH, c_black);
  36. line(xpos + RADIUS, ypos + RADIUS, hx, hy, HOUR_HAND_WIDTH, c_black);
  37.  
  38. /*              FUNCTIONS               */
  39.  
  40. #define circle(x, y, radius, outline, color)
  41. draw_set_color(color);
  42. draw_circle(x, y, radius, outline);
  43.  
  44. #define line(x1, y1, x2, y2, width, color)
  45. draw_set_color(color);
  46. draw_line_width(x1, y1, x2, y2, width);
  47.  
  48. #define distance(x1, y1, x2, y2)
  49. return sqrt(power(x2 - x1, 2) + power(y2 - y1, 2));
  50.  
  51. #define in_circle(x, y, radius, dx, dy)
  52. return distance(x, y, dx, dy) < radius;
  53.  
  54. #define make_clock(radius, hue, hue_scalar)
  55.  
  56. /*      The following are all a function of the arguments above     */
  57.  
  58. OUTER_RING_RADIUS   = radius;
  59. INNER_RING_RADIUS   = radius / 1.05;
  60. CENTER_RING_RADIUS  = radius / 32;
  61.  
  62. SECOND_HAND_LENGTH  = radius / 1.3;
  63. SECOND_HAND_WIDTH   = max(1, radius / 128);
  64.  
  65. MINUTE_HAND_LENGTH  = radius / 1.46;
  66. MINUTE_HAND_WIDTH   = max(2, radius / 64);
  67.  
  68. HOUR_HAND_LENGTH    = radius / 2.32;
  69. HOUR_HAND_WIDTH     = max(3, radius / 51);
  70.  
  71. DOT_OFFSET          = radius / 21;
  72. NUM_OFFSET          = radius / 8;
  73.  
  74. SMALL_DOT_RADIUS    = max(1, radius / 85);
  75. BIG_DOT_RADIUS      = max(2, radius / 43);
  76.  
  77. NUM_SCALE           = radius / 256;
  78.  
  79. COLOR_SCALAR        = (256 / radius) * HUE_SCALAR;
  80.  
  81. var clock = surface_create(radius * 2, radius * 2);
  82. surface_set_target(clock);
  83.  
  84. circle(radius, radius, OUTER_RING_RADIUS, false, c_black);
  85.  
  86. // Draw the gradient :D
  87. for(var dx = -INNER_RING_RADIUS; dx <= INNER_RING_RADIUS; dx++) {
  88.     for(var dy = -INNER_RING_RADIUS; dy <= INNER_RING_RADIUS; dy++) {
  89.         var ax = radius + dx, ay = radius + dy;
  90.  
  91.         var dist  = distance(radius, radius, ax, ay);
  92.         var color = c_teal;
  93.  
  94.         color = make_color_hsv((hue + (dist * COLOR_SCALAR)) % 360, 255, 255);
  95.  
  96.         if(in_circle(radius, radius, INNER_RING_RADIUS, ax, ay))
  97.             draw_point_color(ax, ay, color);
  98.     }
  99. }
  100.  
  101.  
  102. circle(radius, radius, CENTER_RING_RADIUS, false, c_black);
  103.  
  104. // Draw the numbers and the points to the edge of the circle
  105. draw_set_color(c_black);
  106. draw_set_halign(fa_center);
  107. draw_set_valign(fa_middle);
  108.  
  109. for(var i = 0; i < 360; i += 6) {
  110.     var dot_x   = radius + (INNER_RING_RADIUS - DOT_OFFSET) * cos(degtorad(i));
  111.     var dot_y   = radius + (INNER_RING_RADIUS - DOT_OFFSET) * sin(degtorad(i));
  112.  
  113.     var num_x   = radius + (INNER_RING_RADIUS - NUM_OFFSET) * cos(degtorad(i - 60));
  114.     var num_y   = radius + (INNER_RING_RADIUS - NUM_OFFSET) * sin(degtorad(i - 60));
  115.  
  116.     // Draw the point
  117.     if(i % 5 != 0)
  118.         draw_circle(dot_x, dot_y, SMALL_DOT_RADIUS, true);
  119.     else
  120.         draw_circle(dot_x, dot_y, BIG_DOT_RADIUS, false);
  121.  
  122.     // Draw the number
  123.     if(i % 5 == 0)
  124.         draw_text_transformed(
  125.             num_x, num_y,
  126.             string((i / 30) + 1),
  127.             NUM_SCALE, NUM_SCALE, 0
  128.         );
  129. }
  130.  
  131. surface_reset_target();
  132.  
  133. return clock;
Add Comment
Please, Sign In to add comment