Module: Gosu

Defined in:
lib/cyberarm_engine/ui/style.rb,
lib/cyberarm_engine/gosu_ext/draw_arc.rb,
lib/cyberarm_engine/gosu_ext/draw_path.rb,
lib/cyberarm_engine/gosu_ext/draw_circle.rb

Defined Under Namespace

Classes: Color, PathNode

Class Method Summary collapse

Class Method Details

.draw_arc(x, y, radius, percentage = 1.0, segments = 128, thickness = 4, color = Gosu::Color::WHITE, z = 0, mode = :default) ⇒ void

Note:

thickness is subtracted from radius, meaning that the arc will grow towards the origin, not away from it.

This method returns an undefined value.

Draw an arc around the point x and y.

Color accepts the following: Gosu::Color, Array (with 2 colors), or a Hash with keys: from: and to: both colors.

With a Gosu::Color the arc will be painted with color

With an Array the first Gosu::Color with be the innermost color and the last Gosu::Color with be the outermost color

With a Hash the arc will smoothly transition from the start of the arc to the end

Examples:

# Using a Hash
Gosu.draw_arc(100, 100, 50, 0.5, 128, 4, {from: Gosu::Color::BLUE, to: Gosu::Color::GREEN}, 0, :default)

# Using an Array
Gosu.draw_arc(100, 100, 50, 0.5, 128, 4, [Gosu::Color::BLUE, Gosu::Color::GREEN], 0, :default)

# Using a Gosu::Color
Gosu.draw_arc(100, 100, 50, 0.5, 128, 4, Gosu::Color::BLUE, 0, :default)

Parameters:

  • x

    X position.

  • y

    Y position.

  • radius

    radius of arc, in pixels.

  • percentage (defaults to: 1.0)

    how complete the segment is, 0.0 is 0% and 1.0 is 100%.

  • segments (defaults to: 128)

    how many segments for arc, more will appear smoother, less will appear jagged.

  • thickness (defaults to: 4)

    how thick arc will be.

  • color (Gosu::Color, Array<Gosu::Color, Gosu::Color>, Hash{from: start_color, to: end_color}) (defaults to: Gosu::Color::WHITE)

    color or colors to draw the arc with.

  • z (defaults to: 0)

    Z position.

  • mode (defaults to: :default)

    blend mode.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cyberarm_engine/gosu_ext/draw_arc.rb', line 35

def self.draw_arc(x, y, radius, percentage = 1.0, segments = 128, thickness = 4, color = Gosu::Color::WHITE, z = 0, mode = :default)
  segments = 360.0 / segments

  return if percentage == 0.0

  0.step((359 * percentage), percentage > 0 ? segments : -segments) do |angle|
    angle2 = angle + segments

    point_a_left_x = x + Gosu.offset_x(angle, radius - thickness)
    point_a_left_y = y + Gosu.offset_y(angle, radius - thickness)

    point_a_right_x = x + Gosu.offset_x(angle2, radius - thickness)
    point_a_right_y = y + Gosu.offset_y(angle2, radius - thickness)

    point_b_left_x = x + Gosu.offset_x(angle, radius)
    point_b_left_y = y + Gosu.offset_y(angle, radius)

    point_b_right_x = x + Gosu.offset_x(angle2, radius)
    point_b_right_y = y + Gosu.offset_y(angle2, radius)

    if color.is_a?(Array)
      Gosu.draw_quad(
        point_a_left_x, point_a_left_y, color.first,
        point_b_left_x, point_b_left_y, color.last,
        point_a_right_x, point_a_right_y, color.first,
        point_b_right_x, point_b_right_y, color.last,
        z, mode
      )
    elsif color.is_a?(Hash)
      start_color = color[:from]
      end_color = color[:to]

      color_a = Gosu::Color.rgba(
        (end_color.red - start_color.red) * (angle / 360.0) + start_color.red,
        (end_color.green - start_color.green) * (angle / 360.0) + start_color.green,
        (end_color.blue - start_color.blue) * (angle / 360.0) + start_color.blue,
        (end_color.alpha - start_color.alpha) * (angle / 360.0) + start_color.alpha,
      )
      color_b = Gosu::Color.rgba(
        (end_color.red - start_color.red) * (angle2 / 360.0) + start_color.red,
        (end_color.green - start_color.green) * (angle2 / 360.0) + start_color.green,
        (end_color.blue - start_color.blue) * (angle2 / 360.0) + start_color.blue,
        (end_color.alpha - start_color.alpha) * (angle2 / 360.0) + start_color.alpha,
      )

      Gosu.draw_quad(
        point_a_left_x, point_a_left_y, color_a,
        point_b_left_x, point_b_left_y, color_a,
        point_a_right_x, point_a_right_y, color_b,
        point_b_right_x, point_b_right_y, color_b,
        z, mode
      )
    else
      Gosu.draw_quad(
        point_a_left_x, point_a_left_y, color,
        point_b_left_x, point_b_left_y, color,
        point_a_right_x, point_a_right_y, color,
        point_b_right_x, point_b_right_y, color,
        z, mode
      )
    end
  end
end

.draw_circle(x, y, radius, step_size = 36, color = Gosu::Color::WHITE, z = 0, mode = :default) ⇒ void

This method returns an undefined value.

Draw a filled circled around point X and Y.

Parameters:

  • x

    X position.

  • y

    Y position.

  • radius

    radius of circle, in pixels.

  • step_size (defaults to: 36)

    resolution of circle, more steps will apear smoother, less will appear jagged.

  • color (defaults to: Gosu::Color::WHITE)

    color to draw circle with.

  • mode (defaults to: :default)

    blend mode.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cyberarm_engine/gosu_ext/draw_circle.rb', line 13

def self.draw_circle(x, y, radius, step_size = 36, color = Gosu::Color::WHITE, z = 0, mode = :default)
  step_size = (360.0 / step_size).floor

  0.step(359, step_size) do |angle|
    angle2 = angle + step_size

    point_lx = x + Gosu.offset_x(angle, radius)
    point_ly = y + Gosu.offset_y(angle, radius)
    point_rx = x + Gosu.offset_x(angle2, radius)
    point_ry = y + Gosu.offset_y(angle2, radius)

    Gosu.draw_triangle(
      point_lx, point_ly, color,
     point_rx, point_ry, color,
     x, y, color, z, mode
    )
  end
end

.draw_path(nodes, color = Gosu::Color::WHITE, z = 0, mode = :default) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cyberarm_engine/gosu_ext/draw_path.rb', line 4

def self.draw_path(nodes, color = Gosu::Color::WHITE, z = 0, mode = :default)
  last_node = nodes.first

  nodes[1..nodes.size - 1].each do |current_node|
    Gosu.draw_line(
      last_node.x, last_node.y, color,
      current_node.x, current_node.y, color,
      z, mode
    )

    last_node = current_node
  end
end