Class: Clogs::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/clogs/canvas.rb

Overview

Owns a uiArea and turns libui's callbacks into ordinary Ruby ones.

Everything Clogs draws lives in a single area per window. That is not a stylistic choice: libui has no container that positions native controls at arbitrary coordinates, and Shoes' layout model requires exactly that. So Clogs paints the whole Shoes document itself and synthesises the widgets Shoes needs. See docs/libui_shoes_coverage.md.

Defined Under Namespace

Classes: KeyEvent, MouseEvent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scrolling: false, scroll_width: 0, scroll_height: 0) ⇒ Canvas

Returns a new instance of Canvas.



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
49
50
51
52
53
# File 'lib/clogs/canvas.rb', line 18

def initialize(scrolling: false, scroll_width: 0, scroll_height: 0)
  @handler = UI.malloc(UI::L::FFI::AreaHandler)

  @handler.Draw = UI.callback(0, [1, 1, 1]) do |_h, _area, params|
    p = UI::L::FFI::AreaDrawParams.new(params)
    painter = Painter.new(p.Context, p.AreaWidth, p.AreaHeight)
    @on_draw&.call(painter, p)
    0
  end

  @handler.MouseEvent = UI.callback(0, [1, 1, 1]) do |_h, _area, evt|
    e = UI::L::FFI::AreaMouseEvent.new(evt)
    @on_mouse&.call(mouse_event(e))
    0
  end

  @handler.MouseCrossed = UI.callback(0, [1, 1, 0]) do |_h, _area, left|
    @on_crossed&.call(left != 0)
    0
  end

  @handler.DragBroken = UI.callback(0, [1, 1]) { 0 }

  @handler.KeyEvent = UI.callback(1, [1, 1, 1]) do |_h, _area, evt|
    e = UI::L::FFI::AreaKeyEvent.new(evt)
    handled = @on_key&.call(key_event(e))
    handled ? 1 : 0
  end

  @area = if scrolling
    UI::L.new_scrolling_area(@handler, scroll_width, scroll_height)
  else
    UI::L.new_area(@handler)
  end
  @scrolling = scrolling
end

Instance Attribute Details

#areaObject (readonly)

Returns the value of attribute area.



15
16
17
# File 'lib/clogs/canvas.rb', line 15

def area
  @area
end

#on_crossedObject

Returns the value of attribute on_crossed.



16
17
18
# File 'lib/clogs/canvas.rb', line 16

def on_crossed
  @on_crossed
end

#on_drawObject

Returns the value of attribute on_draw.



16
17
18
# File 'lib/clogs/canvas.rb', line 16

def on_draw
  @on_draw
end

#on_keyObject

Returns the value of attribute on_key.



16
17
18
# File 'lib/clogs/canvas.rb', line 16

def on_key
  @on_key
end

#on_mouseObject

Returns the value of attribute on_mouse.



16
17
18
# File 'lib/clogs/canvas.rb', line 16

def on_mouse
  @on_mouse
end

Instance Method Details

#redrawObject



55
56
57
# File 'lib/clogs/canvas.rb', line 55

def redraw
  UI::L.area_queue_redraw_all(@area)
end

#scroll_to(x, y, w, h) ⇒ Object



65
66
67
68
69
# File 'lib/clogs/canvas.rb', line 65

def scroll_to(x, y, w, h)
  return unless @scrolling

  UI::L.area_scroll_to(@area, x, y, w, h)
end

#set_scroll_size(w, h) ⇒ Object



59
60
61
62
63
# File 'lib/clogs/canvas.rb', line 59

def set_scroll_size(w, h)
  return unless @scrolling

  UI::L.area_set_size(@area, w.to_i, h.to_i)
end