|
| 1 | +require 'toxiclibs' |
| 2 | +load_library :control_panel |
| 3 | +attr_reader :gfx, :bool, :panel, :type, :hide, :polies |
| 4 | +include Toxi |
| 5 | + |
| 6 | +TYPE = [BooleanShapeBuilder::Type::UNION, BooleanShapeBuilder::Type::XOR].freeze |
| 7 | +KEY = %w(union xor).freeze |
| 8 | + |
| 9 | +def setup |
| 10 | + sketch_title 'Boolean Shapes' |
| 11 | + @gfx = Gfx::ToxiclibsSupport.new(self) |
| 12 | + @bool = KEY.zip(TYPE).to_h |
| 13 | + control_panel do |c| |
| 14 | + c.title = 'Control Panel' |
| 15 | + c.menu :type, KEY, 'union' |
| 16 | + @panel = c |
| 17 | + end |
| 18 | + @hide = false |
| 19 | +end |
| 20 | + |
| 21 | +def draw |
| 22 | + unless hide |
| 23 | + @hide = true |
| 24 | + panel.set_visible(hide) |
| 25 | + end |
| 26 | + background(160) |
| 27 | + builder = BooleanShapeBuilder.new(bool[type]) |
| 28 | + phi = frame_count * 0.01 |
| 29 | + builder.add_shape(Circle.new(mouse_x, mouse_y, 50)) |
| 30 | + builder.add_shape(Ellipse.new(150, 130 + sin(phi) * 50, 120, 60)) |
| 31 | + builder.add_shape(Rect.new(200 + sin(phi * 13 / 8) * 50, 180, 100, 100)) |
| 32 | + builder.add_shape( |
| 33 | + Triangle2D.create_equilateral_from( |
| 34 | + TVec2D.new(50 + sin(phi * 15 / 13) * 50, 200), TVec2D.new(300, 200) |
| 35 | + ) |
| 36 | + ) |
| 37 | + builder.add_shape( |
| 38 | + Circle.new(100, 300, 50 + 30 * sin(phi * 21 / 15)).toPolygon2D(6) |
| 39 | + ) |
| 40 | + fill(125) |
| 41 | + stroke(255, 0, 0) |
| 42 | + @polies = builder.compute_shapes |
| 43 | + polies.each { |p| gfx.polygon2D(p) } |
| 44 | +end |
| 45 | + |
| 46 | +def settings |
| 47 | + size(400, 400) |
| 48 | +end |
0 commit comments