From abfb596bc9212c5094dc7ec3645ace4e7d35856b Mon Sep 17 00:00:00 2001 From: lstrzebinczyk Date: Sat, 29 Apr 2017 20:28:52 +0200 Subject: introduce lines --- lib/ruby2d.rb | 1 + lib/ruby2d/image.rb | 2 +- lib/ruby2d/line.rb | 41 +++++++++++++++++++++++++++++++++++++++++ lib/ruby2d/sprite.rb | 2 +- lib/ruby2d/text.rb | 2 +- 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 lib/ruby2d/line.rb (limited to 'lib') diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb index 4c13dd9..9642aa1 100644 --- a/lib/ruby2d.rb +++ b/lib/ruby2d.rb @@ -7,6 +7,7 @@ require 'ruby2d/window' require 'ruby2d/application' require 'ruby2d/dsl' require 'ruby2d/quad' +require 'ruby2d/line' require 'ruby2d/rectangle' require 'ruby2d/square' require 'ruby2d/triangle' diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb index 85ee076..91bb3f3 100644 --- a/lib/ruby2d/image.rb +++ b/lib/ruby2d/image.rb @@ -15,7 +15,7 @@ module Ruby2D end end - @type_id = 3 + @type_id = 4 @x, @y, @path = x, y, path @z = z @color = Color.new([1, 1, 1, 1]) diff --git a/lib/ruby2d/line.rb b/lib/ruby2d/line.rb new file mode 100644 index 0000000..5053854 --- /dev/null +++ b/lib/ruby2d/line.rb @@ -0,0 +1,41 @@ +# line.rb + +module Ruby2D + class Line + include Renderable + attr_accessor :x1, :x2, :y1, :y2, :color, :width + + def initialize(x1, y1, x2, y2, width=2, c='white') + @type_id = 3 + @x1, @y1, @x2, @y2 = x1, y1, x2, y2 + @width = width + self.color = c + add + end + + def color=(c) + @color = Color.from(c) + update_color(@color) + end + + private + + def update_color(c) + if c.is_a? Color::Set + if c.length == 4 + @c1 = c[0] + @c2 = c[1] + @c3 = c[2] + @c4 = c[3] + else + raise ArgumentError, "Lines require 4 colors, one for each vertex. #{c.length} were given." + end + else + @c1 = c + @c2 = c + @c3 = c + @c4 = c + end + end + end +end diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb index ad8dc17..93f4f58 100644 --- a/lib/ruby2d/sprite.rb +++ b/lib/ruby2d/sprite.rb @@ -12,7 +12,7 @@ module Ruby2D # raise Error, "Cannot find image file `#{path}`" # end - @type_id = 4 + @type_id = 5 @x, @y, @path = x, y, path @clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0 @default = nil diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index b3b4529..e326029 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -15,7 +15,7 @@ module Ruby2D # @font = resolve_path(font) # end - @type_id = 5 + @type_id = 6 @x, @y, @size = x, y, size @z = z @text = text.to_s -- cgit v1.2.3