Module: Contrek::Bitmaps::Rendering::ClassMethods

Defined in:
lib/contrek/bitmaps/rendering.rb

Instance Method Summary collapse

Instance Method Details

#to_window(result, zoom: 100, coords: true, window_w: 2000, window_h: 1200, font_h: 20) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/contrek/bitmaps/rendering.rb', line 11

def to_window(result, zoom: 100, coords: true, window_w: 2000, window_h: 1200, font_h: 20)
  require "ruby2d"
  ubuntu_purple = ChunkyPNG::Color.from_hex("#77216F")
  bm = RawBitmap.new(w: window_w, h: window_h, color: ubuntu_purple)
  # grid
  light_purple = ChunkyPNG::Color.from_hex("#95338B")
  (window_w / zoom).times { |x| bm.draw_line(x * zoom, 0, x * zoom, window_h, light_purple) }
  (window_h / zoom).times { |y| bm.draw_line(0, y * zoom, window_w, y * zoom, light_purple) }
  # polygons
  Painting.direct_draw_polygons(result.points, bm, zoom: zoom)
  # windowing
  Tempfile.create do |temp_file|
    bm.image.save(temp_file.path, color_mode: ChunkyPNG::COLOR_TRUECOLOR_ALPHA)
    Window.clear
    Window.set(
      title: "Rendering #{result[:metadata][:groups]} polygons",
      width: window_w,
      height: window_h,
      resizable: true
    )
    Image.new(
      temp_file.path,
      x: 0, y: 0,
      width: window_w,
      height: window_h
    )
    draw_labels(result.points, zoom: zoom, draw_coords: coords, font_h:)
    Window.on :key_down do |event|
      if event.key == "escape"
        Window.close
      end
      if event.key == "s"
        Window.screenshot("window_content.png")
      end
    end
    Window.show
  end
end