Class: UI::Widget
- Inherits:
-
Object
show all
- Defined in:
- lib/ektoplayer/ui/widgets.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(parent: nil, size: nil, pos: nil, visible: true) ⇒ Widget
Returns a new instance of Widget.
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/ektoplayer/ui/widgets.rb', line 11
def initialize(parent: nil, size: nil, pos: nil, visible: true)
if !parent and (!size or !pos)
fail ArgumentError, "must provide 'size:' and 'pos:' if 'parent:' is nil"
end
@parent, @visible, = parent, visible
@size = (size or @parent.size.dup)
@pos = (pos or @parent.pos.dup)
@want, @lock = WANT_LAYOUT, Monitor.new
end
|
Instance Attribute Details
#pos ⇒ Object
Returns the value of attribute pos.
5
6
7
|
# File 'lib/ektoplayer/ui/widgets.rb', line 5
def pos
@pos
end
|
#size ⇒ Object
Returns the value of attribute size.
5
6
7
|
# File 'lib/ektoplayer/ui/widgets.rb', line 5
def size
@size
end
|
Instance Method Details
#display(force_refresh = false, force_redraw = false, force_layout = false) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/ektoplayer/ui/widgets.rb', line 64
def display(force_refresh=false, force_redraw=false, force_layout=false)
if @want & WANT_LAYOUT > 0 or force_layout
layout;
@want ^= WANT_LAYOUT
end
return if not visible?
if @want & WANT_REDRAW > 0 or force_redraw
draw
@want ^= WANT_REDRAW
end
if @want > 0 or force_refresh
refresh
@want ^= WANT_REFRESH
end
end
|
#draw ⇒ Object
127
|
# File 'lib/ektoplayer/ui/widgets.rb', line 127
def draw; fail NotImplementedError end
|
#events ⇒ Object
6
|
# File 'lib/ektoplayer/ui/widgets.rb', line 6
def events; @events ||= Events.new.no_auto_create end
|
#invisible! ⇒ Object
88
|
# File 'lib/ektoplayer/ui/widgets.rb', line 88
def invisible!; self.visible=(false) end
|
#invisible? ⇒ Boolean
86
|
# File 'lib/ektoplayer/ui/widgets.rb', line 86
def invisible?; !visible? end
|
#key_press(key) ⇒ Object
116
|
# File 'lib/ektoplayer/ui/widgets.rb', line 116
def key_press(key) on_key_press(key) end
|
#keys ⇒ Object
7
|
# File 'lib/ektoplayer/ui/widgets.rb', line 7
def keys; @keys ||= Events.new end
|
#layout ⇒ Object
129
|
# File 'lib/ektoplayer/ui/widgets.rb', line 129
def layout; fail NotImplementedError end
|
#lock ⇒ Object
40
41
42
|
# File 'lib/ektoplayer/ui/widgets.rb', line 40
def lock
@lock.enter
end
|
#mouse ⇒ Object
8
|
# File 'lib/ektoplayer/ui/widgets.rb', line 8
def mouse; @mouse ||= MouseEvents.new end
|
#mouse_click(mevent) ⇒ Object
120
121
122
123
124
125
|
# File 'lib/ektoplayer/ui/widgets.rb', line 120
def mouse_click(mevent)
if new_event = mouse_event_transform(mevent)
trigger(@mouse, new_event)
trigger(@mouse_section, new_event)
end
end
|
106
107
108
109
110
111
112
113
114
|
# File 'lib/ektoplayer/ui/widgets.rb', line 106
def mouse_event_transform(mevent)
if mevent.y >= @pos.y and mevent.x >= @pos.x and
mevent.y < (@pos.y + @size.height) and
mevent.x < (@pos.x + @size.width)
new_mouse = mevent.to_fake
new_mouse.update!(y: mevent.y - @pos.y, x: mevent.x - @pos.x)
new_mouse
end
end
|
#mouse_section ⇒ Object
9
|
# File 'lib/ektoplayer/ui/widgets.rb', line 9
def mouse_section; @mouse_section ||= MouseSectionEvents.new end
|
#on_key_press(key) ⇒ Object
118
|
# File 'lib/ektoplayer/ui/widgets.rb', line 118
def on_key_press(key) trigger(@keys, key) end
|
135
136
137
138
|
# File 'lib/ektoplayer/ui/widgets.rb', line 135
def on_widget_raise(widget)
fail 'unhandled widget raise' unless @parent
@parent.raise_widget(widget)
end
|
117
|
# File 'lib/ektoplayer/ui/widgets.rb', line 117
def raise_widget(widget) on_widget_raise(widget) end
|
#refresh ⇒ Object
128
|
# File 'lib/ektoplayer/ui/widgets.rb', line 128
def refresh; fail NotImplementedError end
|
#sub(class_type, **opts) ⇒ Object
Proxy method for creating a new widget object with the current object as parent.
24
25
26
|
# File 'lib/ektoplayer/ui/widgets.rb', line 24
def sub(class_type, **opts)
class_type.new(parent: self, **opts)
end
|
#unlock ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/ektoplayer/ui/widgets.rb', line 44
def unlock
return unless (@lock.exit rescue nil)
if @want & WANT_LAYOUT > 0
layout;
@want ^= WANT_LAYOUT
end
return if not visible?
if @want & WANT_REDRAW > 0
draw
@want ^= WANT_REDRAW
end
if @want > 0
Canvas.update_screen
@want ^= WANT_REFRESH
end
end
|
#visible! ⇒ Object
87
|
# File 'lib/ektoplayer/ui/widgets.rb', line 87
def visible!; self.visible=(true) end
|
#visible=(new) ⇒ Object
91
92
93
94
|
# File 'lib/ektoplayer/ui/widgets.rb', line 91
def visible=(new)
return if @visible == new
with_lock { @visible = new; want_refresh }
end
|
#visible? ⇒ Boolean
89
|
# File 'lib/ektoplayer/ui/widgets.rb', line 89
def visible?; @visible and (!@parent or @parent.visible?) end
|
#want_layout ⇒ Object
83
|
# File 'lib/ektoplayer/ui/widgets.rb', line 83
def want_layout; @want = 7 end
|
#want_redraw ⇒ Object
82
|
# File 'lib/ektoplayer/ui/widgets.rb', line 82
def want_redraw; @want |= 3 end
|
#want_refresh ⇒ Object
84
|
# File 'lib/ektoplayer/ui/widgets.rb', line 84
def want_refresh; @want |= WANT_REFRESH end
|
#with_lock ⇒ Object
This method should be used each time a widget may modify its window contents.
It ensures that operations that modify the window (such as draw, layout and refresh) are executed once and only once at the end of this function.
34
35
36
37
38
|
# File 'lib/ektoplayer/ui/widgets.rb', line 34
def with_lock
lock; yield
ensure
unlock
end
|