glimmer-dsl-libui 0.1.11 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -0
  3. data/README.md +573 -23
  4. data/VERSION +1 -1
  5. data/examples/area_gallery.rb +7 -1
  6. data/examples/area_gallery2.rb +14 -4
  7. data/examples/area_gallery3.rb +8 -2
  8. data/examples/area_gallery4.rb +15 -5
  9. data/examples/basic_draw_text.rb +67 -0
  10. data/examples/color_the_circles.rb +220 -0
  11. data/examples/midi_player.rb +2 -2
  12. data/examples/timer.rb +135 -0
  13. data/glimmer-dsl-libui.gemspec +0 -0
  14. data/lib/glimmer/dsl/libui/control_expression.rb +0 -1
  15. data/lib/glimmer/dsl/libui/property_expression.rb +3 -1
  16. data/lib/glimmer/dsl/libui/string_expression.rb +48 -0
  17. data/lib/glimmer/libui/attributed_string.rb +90 -0
  18. data/lib/glimmer/libui/control_proxy/area_proxy.rb +14 -3
  19. data/lib/glimmer/libui/control_proxy/font_button_proxy.rb +3 -3
  20. data/lib/glimmer/libui/control_proxy/menu_item_proxy/radio_menu_item_proxy.rb +69 -0
  21. data/lib/glimmer/libui/control_proxy/menu_proxy.rb +3 -0
  22. data/lib/glimmer/libui/control_proxy/path_proxy.rb +0 -2
  23. data/lib/glimmer/libui/control_proxy/text_proxy.rb +158 -0
  24. data/lib/glimmer/libui/control_proxy/window_proxy.rb +0 -6
  25. data/lib/glimmer/libui/control_proxy.rb +7 -1
  26. data/lib/glimmer/libui/shape/arc.rb +6 -3
  27. data/lib/glimmer/libui/shape/circle.rb +50 -0
  28. data/lib/glimmer/libui/shape/rectangle.rb +4 -0
  29. data/lib/glimmer/libui/shape/square.rb +4 -0
  30. data/lib/glimmer/libui.rb +73 -6
  31. data/sounds/AlanWalker-Faded.mid +0 -0
  32. data/sounds/AlanWalker-SingMeToSleep.mid +0 -0
  33. data/sounds/CalvinHarris-Blame.mid +0 -0
  34. data/sounds/CalvinHarris-MyWay.mid +0 -0
  35. data/sounds/deadmau5-2448.mid +0 -0
  36. data/sounds/deadmau5-SoThereIWas.mid +0 -0
  37. metadata +16 -2
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/libui/control_proxy'
23
+ require 'glimmer/libui/control_proxy/area_proxy'
24
+ require 'glimmer/libui/parent'
25
+ require 'glimmer/libui/control_proxy/transformable'
26
+
27
+ module Glimmer
28
+ module LibUI
29
+ class AttributedString
30
+ attr_accessor :string
31
+ attr_reader :block, :keyword, :parent_proxy, :args
32
+
33
+ def initialize(keyword, parent_proxy, args, &block)
34
+ @keyword = keyword
35
+ @parent_proxy = parent_proxy
36
+ @args = args
37
+ @string = @args.first || ''
38
+ @block = block
39
+ post_add_content if @block.nil?
40
+ end
41
+
42
+ def font(value = nil)
43
+ # UI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, start, start + @string.size)
44
+ end
45
+
46
+ def color(value = nil)
47
+ if value.nil?
48
+ @color
49
+ else
50
+ @color = Glimmer::LibUI.interpret_color(value)
51
+ end
52
+ end
53
+
54
+ def background(value = nil)
55
+ # UI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, start, start + @string.size)
56
+ end
57
+
58
+ def underline(value = nil)
59
+ # UI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, start, start + @string.size)
60
+ end
61
+
62
+ def post_add_content
63
+ block_result = block&.call
64
+ @string = block_result if block_result.is_a?(String)
65
+ @parent_proxy&.post_initialize_child(self)
66
+ end
67
+
68
+ def draw(area_draw_params)
69
+ @start = ::LibUI.attributed_string_len(@parent_proxy.attributed_string)
70
+ ::LibUI.attributed_string_append_unattributed(@parent_proxy.attributed_string, @string)
71
+ unless color.nil?
72
+ color_attribute = ::LibUI.new_color_attribute(@color[:r], @color[:g], @color[:b], @color[:a] || 1.0)
73
+ ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, @start, @start + @string.size)
74
+ end
75
+ end
76
+
77
+ def destroy
78
+ @parent_proxy&.children&.delete(self)
79
+ end
80
+
81
+ def redraw
82
+ area_proxy&.queue_redraw_all
83
+ end
84
+
85
+ def area_proxy
86
+ @parent_proxy.parent_proxy
87
+ end
88
+ end
89
+ end
90
+ end
@@ -60,7 +60,18 @@ module Glimmer
60
60
  end
61
61
 
62
62
  def post_add_content
63
- super
63
+ unless parent_proxy.is_a?(Box)
64
+ original_parent_proxy = @parent_proxy
65
+ @vertical_box_parent_proxy = ControlProxy.create('vertical_box', parent_proxy, []) {} # block prevents calling post add content
66
+ append_properties.each do |property|
67
+ @vertical_box_parent_proxy.append_property(property, append_property(property))
68
+ end
69
+ @vertical_box_parent_proxy.post_add_content
70
+ @parent_proxy = @vertical_box_parent_proxy
71
+ @vertical_box_parent_proxy.post_initialize_child(self)
72
+ else
73
+ super
74
+ end
64
75
  install_listeners
65
76
  end
66
77
 
@@ -72,7 +83,7 @@ module Glimmer
72
83
  def redraw
73
84
  queue_redraw_all
74
85
  end
75
-
86
+
76
87
  private
77
88
 
78
89
  def build_control
@@ -169,7 +180,7 @@ module Glimmer
169
180
  end
170
181
 
171
182
  def ext_key_to_symbol(ext_key_value)
172
- Glimmer::LibUI.enum_symbols(:ext_key)[ext_key_value - 1].to_s.to_sym if ext_key_value > 0
183
+ Glimmer::LibUI.enum_value_to_symbol(:ext_key, ext_key_value)
173
184
  end
174
185
 
175
186
  def modifiers_to_symbols(modifiers_value)
@@ -34,9 +34,9 @@ module Glimmer
34
34
  {
35
35
  family: @font_descriptor.Family.to_s,
36
36
  size: @font_descriptor.Size,
37
- weight: @font_descriptor.Weight,
38
- italic: @font_descriptor.Italic,
39
- stretch: @font_descriptor.Stretch
37
+ weight: Glimmer::LibUI.enum_value_to_symbol(:text_weight, @font_descriptor.Weight),
38
+ italic: Glimmer::LibUI.enum_value_to_symbol(:text_italic, @font_descriptor.Italic),
39
+ stretch: Glimmer::LibUI.enum_value_to_symbol(:text_stretch, @font_descriptor.Stretch),
40
40
  }
41
41
  end
42
42
 
@@ -0,0 +1,69 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/libui/control_proxy/menu_item_proxy'
23
+
24
+ module Glimmer
25
+ module LibUI
26
+ class ControlProxy
27
+ class MenuItemProxy < ControlProxy
28
+ # Proxy for LibUI radio menu item object
29
+ #
30
+ # Follows the Proxy Design Pattern
31
+ class RadioMenuItemProxy < MenuItemProxy
32
+ def checked(value = nil)
33
+ if !value.nil?
34
+ if Glimmer::LibUI.integer_to_boolean(value) != checked?
35
+ super
36
+ if Glimmer::LibUI.integer_to_boolean(value)
37
+ (@parent_proxy.children - [self]).select {|c| c.is_a?(MenuItemProxy)}.each do |menu_item|
38
+ menu_item.checked = false
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ alias checked? checked
45
+ alias set_checked checked
46
+ alias checked= checked
47
+
48
+ def handle_listener(listener_name, &listener)
49
+ if listener_name.to_s == 'on_clicked'
50
+ radio_listener = Proc.new do
51
+ self.checked = true if !checked?
52
+ listener.call(self)
53
+ end
54
+ super(listener_name, &radio_listener)
55
+ else
56
+ super
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def build_control
63
+ @libui = @parent_proxy.append_check_item(*@args)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -20,6 +20,7 @@
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
22
  require 'glimmer/libui/control_proxy'
23
+ require 'glimmer/libui/parent'
23
24
 
24
25
  module Glimmer
25
26
  module LibUI
@@ -28,6 +29,8 @@ module Glimmer
28
29
  #
29
30
  # Follows the Proxy Design Pattern
30
31
  class MenuProxy < ControlProxy
32
+ include Parent
33
+
31
34
  DEFAULT_TEXT = ''
32
35
 
33
36
  private
@@ -34,13 +34,11 @@ module Glimmer
34
34
  include Parent
35
35
  prepend Transformable
36
36
 
37
- # TODO support mode without parent proxy
38
37
  def initialize(keyword, parent, args, &block)
39
38
  @keyword = keyword
40
39
  @parent_proxy = parent
41
40
  @args = args
42
41
  @block = block
43
- @enabled = true
44
42
  post_add_content if @block.nil?
45
43
  end
46
44
 
@@ -0,0 +1,158 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/libui/control_proxy'
23
+ require 'glimmer/libui/control_proxy/area_proxy'
24
+ require 'glimmer/libui/parent'
25
+ require 'glimmer/libui/control_proxy/transformable'
26
+
27
+ module Glimmer
28
+ module LibUI
29
+ class ControlProxy
30
+ # Proxy for LibUI text objects
31
+ #
32
+ # Follows the Proxy Design Pattern
33
+ class TextProxy < ControlProxy
34
+ include Parent
35
+ prepend Transformable
36
+
37
+ def initialize(keyword, parent, args, &block)
38
+ @keyword = keyword
39
+ @parent_proxy = parent
40
+ @args = args
41
+ @block = block
42
+ post_add_content if @block.nil?
43
+ end
44
+
45
+ def post_add_content
46
+ super
47
+ if @parent_proxy.nil? && AreaProxy.current_area_draw_params
48
+ draw(AreaProxy.current_area_draw_params)
49
+ destroy
50
+ end
51
+ end
52
+
53
+ def draw(area_draw_params)
54
+ children.dup.each {|child| child.draw(area_draw_params)}
55
+ build_control
56
+ ::LibUI.draw_text(area_draw_params[:context], @libui, x, y)
57
+ ::LibUI.draw_free_text_layout(@libui)
58
+ end
59
+
60
+ def destroy
61
+ @parent_proxy&.children&.delete(self)
62
+ ControlProxy.control_proxies.delete(self)
63
+ end
64
+
65
+ def redraw
66
+ @parent_proxy&.queue_redraw_all
67
+ end
68
+
69
+ def x(value = nil)
70
+ if value.nil?
71
+ @x ||= args[0] || 0
72
+ else
73
+ @x = value
74
+ end
75
+ end
76
+ alias x= x
77
+ alias set_x x
78
+
79
+ def y(value = nil)
80
+ if value.nil?
81
+ @y ||= args[1] || 0
82
+ else
83
+ @y = value
84
+ end
85
+ end
86
+ alias y= y
87
+ alias set_y y
88
+
89
+ def width(value = nil)
90
+ if value.nil?
91
+ @width ||= args[2] || (AreaProxy.current_area_draw_params && AreaProxy.current_area_draw_params[:width])
92
+ else
93
+ @width = value
94
+ end
95
+ end
96
+ alias width= width
97
+ alias set_width width
98
+
99
+ def attributed_string
100
+ @attributed_string ||= ::LibUI.new_attributed_string('')
101
+ end
102
+
103
+ def default_font(value = nil)
104
+ if value.nil?
105
+ @default_font ||= {}
106
+ else
107
+ @default_font = value
108
+ end
109
+ end
110
+ alias default_font= default_font
111
+ alias set_default_font default_font
112
+
113
+ def default_font_descriptor
114
+ @default_font_descriptor ||= ::LibUI::FFI::FontDescriptor.malloc
115
+ @default_font_descriptor.Family = default_font[:family] || 'Helvetica'
116
+ @default_font_descriptor.Size = default_font[:size] || 12.0
117
+ @default_font_descriptor.Weight = Glimmer::LibUI.enum_symbol_to_value(:text_weight, default_font[:weight], default_symbol: :normal)
118
+ @default_font_descriptor.Italic = Glimmer::LibUI.enum_symbol_to_value(:text_italic, default_font[:italic], default_symbol: :normal)
119
+ @default_font_descriptor.Stretch = Glimmer::LibUI.enum_symbol_to_value(:text_stretch, default_font[:stretch], default_symbol: :normal)
120
+ @default_font_descriptor
121
+ end
122
+
123
+ def align(value = nil)
124
+ if value.nil?
125
+ @align ||= {}
126
+ else
127
+ @align = value
128
+ end
129
+ end
130
+ alias align= align
131
+ alias set_align align
132
+
133
+ def draw_text_layout_params
134
+ @draw_text_layout_params ||= ::LibUI::FFI::DrawTextLayoutParams.malloc
135
+ @draw_text_layout_params.String = attributed_string
136
+ @draw_text_layout_params.DefaultFont = default_font_descriptor
137
+ @draw_text_layout_params.Width = width
138
+ @draw_text_layout_params.Align = Glimmer::LibUI.enum_symbol_to_value(:draw_text_align, align, default_symbol: :center)
139
+ @draw_text_layout_params
140
+ end
141
+
142
+ private
143
+
144
+ def build_control
145
+ @libui = ::LibUI.draw_new_text_layout(draw_text_layout_params)
146
+ end
147
+
148
+ def init_draw_brush(draw_brush, draw_brush_args)
149
+ draw_brush.Type = Glimmer::LibUI.enum_symbol_to_value(:draw_brush_type, draw_brush_args[:type])
150
+ draw_brush.R = (draw_brush_args[:r] || draw_brush_args[:red]).to_f / 255.0
151
+ draw_brush.G = (draw_brush_args[:g] || draw_brush_args[:green]).to_f / 255.0
152
+ draw_brush.B = (draw_brush_args[:b] || draw_brush_args[:blue]).to_f / 255.0
153
+ draw_brush.A = (draw_brush_args[:a] || draw_brush_args[:alpha])
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -35,12 +35,6 @@ module Glimmer
35
35
  DEFAULT_HAS_MENUBAR = 1
36
36
 
37
37
  def post_initialize_child(child)
38
- if child.is_a?(AreaProxy)
39
- vertical_box_parent = ControlProxy.create('vertical_box', self, [])
40
- child.instance_variable_set(:@parent_proxy, vertical_box_parent)
41
- vertical_box_parent.post_initialize_child(child)
42
- child = vertical_box_parent
43
- end
44
38
  ::LibUI.window_set_child(@libui, child.libui)
45
39
  end
46
40
 
@@ -223,7 +223,13 @@ module Glimmer
223
223
  elsif ::LibUI.respond_to?("#{libui_api_keyword}_set_#{method_name.to_s.sub(/=$/, '')}") && !args.empty?
224
224
  property = method_name.to_s.sub(/=$/, '')
225
225
  args[0] = Glimmer::LibUI.boolean_to_integer(args.first) if BOOLEAN_PROPERTIES.include?(property) && (args.first.is_a?(TrueClass) || args.first.is_a?(FalseClass))
226
- ::LibUI.send("#{libui_api_keyword}_set_#{property}", @libui, *args)
226
+ if property.to_s == 'checked'
227
+ current_value = Glimmer::LibUI.integer_to_boolean(::LibUI.send("#{libui_api_keyword}_checked", @libui))
228
+ new_value = Glimmer::LibUI.integer_to_boolean(args[0])
229
+ ::LibUI.send("#{libui_api_keyword}_set_#{property}", @libui, *args) if new_value != current_value
230
+ else
231
+ ::LibUI.send("#{libui_api_keyword}_set_#{property}", @libui, *args)
232
+ end
227
233
  elsif ::LibUI.respond_to?("#{libui_api_keyword}_#{method_name}") && !args.empty?
228
234
  ::LibUI.send("#{libui_api_keyword}_#{method_name}", @libui, *args)
229
235
  elsif ::LibUI.respond_to?("control_#{method_name.to_s.sub(/\?$/, '')}") && args.empty?
@@ -29,11 +29,14 @@ module Glimmer
29
29
  parameter_defaults 0, 0, 0, 0, 360, false
30
30
 
31
31
  def draw(area_draw_params)
32
- @args[5] ||= Glimmer::LibUI.boolean_to_integer(@args[5], allow_nil: false)
32
+ arc_args = @args.dup
33
+ arc_args[3] = Glimmer::LibUI.degrees_to_radians(arc_args[3])
34
+ arc_args[4] = Glimmer::LibUI.degrees_to_radians(arc_args[4])
35
+ arc_args[5] = Glimmer::LibUI.boolean_to_integer(arc_args[5], allow_nil: false)
33
36
  if parent.is_a?(Figure) && parent.x.nil? && parent.y.nil?
34
- ::LibUI.draw_path_new_figure_with_arc(path_proxy.libui, *@args)
37
+ ::LibUI.draw_path_new_figure_with_arc(path_proxy.libui, *arc_args)
35
38
  else
36
- ::LibUI.draw_path_arc_to(path_proxy.libui, *@args)
39
+ ::LibUI.draw_path_arc_to(path_proxy.libui, *arc_args)
37
40
  end
38
41
  super
39
42
  end
@@ -0,0 +1,50 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/libui/shape'
23
+
24
+ module Glimmer
25
+ module LibUI
26
+ class Shape
27
+ class Circle < Shape
28
+ parameters :x_center, :y_center, :radius
29
+ parameter_defaults 0, 0, 0
30
+
31
+ def draw(area_draw_params)
32
+ arc_args = @args.dup
33
+ arc_args[3] = 0
34
+ arc_args[4] = Math::PI * 2.0
35
+ arc_args[5] = 0
36
+ if parent.is_a?(Figure) && parent.x.nil? && parent.y.nil?
37
+ ::LibUI.draw_path_new_figure_with_arc(path_proxy.libui, *arc_args)
38
+ else
39
+ ::LibUI.draw_path_arc_to(path_proxy.libui, *arc_args)
40
+ end
41
+ super
42
+ end
43
+
44
+ def include?(x, y)
45
+ (x - x_center)**2 + (y - y_center)**2 < radius**2
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -32,6 +32,10 @@ module Glimmer
32
32
  ::LibUI.draw_path_add_rectangle(path_proxy.libui, *@args)
33
33
  super
34
34
  end
35
+
36
+ def include?(x, y)
37
+ x.between?(self.x, self.x + width) && y.between?(self.y, self.y + height)
38
+ end
35
39
  end
36
40
  end
37
41
  end
@@ -32,6 +32,10 @@ module Glimmer
32
32
  ::LibUI.draw_path_add_rectangle(path_proxy.libui, *@args, length)
33
33
  super
34
34
  end
35
+
36
+ def include?(x, y)
37
+ x.between?(self.x, self.x + length) && y.between?(self.y, self.y + length)
38
+ end
35
39
  end
36
40
  end
37
41
  end
data/lib/glimmer/libui.rb CHANGED
@@ -19,15 +19,23 @@
19
19
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
+ require 'glimmer/fiddle_consumer'
23
+
22
24
  module Glimmer
23
25
  module LibUI
24
26
  class << self
27
+ include Glimmer::FiddleConsumer
28
+
25
29
  def integer_to_boolean(int, allow_nil: true)
26
- int.nil? ? (allow_nil ? nil : false) : int == 1
30
+ int.nil? ? (allow_nil ? nil : false) : ((int.is_a?(TrueClass) || int.is_a?(FalseClass)) ? int : int == 1)
27
31
  end
28
32
 
29
33
  def boolean_to_integer(bool, allow_nil: true)
30
- bool.nil? ? (allow_nil ? nil : 0) : (bool ? 1 : 0)
34
+ bool.nil? ? (allow_nil ? nil : 0) : (bool.is_a?(Integer) ? bool : (bool == true ? 1 : 0))
35
+ end
36
+
37
+ def degrees_to_radians(degrees)
38
+ ((Math::PI * 2.0) / 360.00) * degrees.to_f
31
39
  end
32
40
 
33
41
  def interpret_color(value)
@@ -88,22 +96,81 @@ module Glimmer
88
96
  value
89
97
  end
90
98
 
91
- # Returns ruby underscored symbols for enum values starting with enum name (camelcase, e.g. 'ext_key')
92
99
  def enum_symbols(enum_name)
100
+ enum_symbol_values(enum_name).keys
101
+ end
102
+
103
+ # Returns ruby underscored symbols for enum values starting with enum name (camelcase, e.g. 'ext_key')
104
+ def enum_symbol_values(enum_name)
93
105
  enum_name = enum_name.to_s.underscore.to_sym
94
106
  @enum_symbols ||= {}
95
- @enum_symbols[enum_name] ||= ::LibUI.constants.select { |c| c.to_s.start_with?(enum_name.to_s.camelcase(:upper)) }.map { |c| c.to_s.underscore.sub("#{enum_name}_", '').to_sym }
107
+ @enum_symbols[enum_name] ||= ::LibUI.constants.select { |c| c.to_s.start_with?(enum_name.to_s.camelcase(:upper)) }.map { |c| [c.to_s.underscore.sub("#{enum_name}_", '').to_sym, ::LibUI.const_get(c)] }.to_h
108
+ end
109
+
110
+ def enum_value_to_symbol(enum_name, enum_value)
111
+ enum_symbol_values(enum_name).invert[enum_value]
96
112
  end
97
113
 
98
- def enum_symbol_to_value(enum_name, enum_symbol, default_index: 0)
114
+ def enum_symbol_to_value(enum_name, enum_symbol, default_symbol: nil, default_index: 0)
99
115
  if enum_symbol.is_a?(Integer)
100
116
  enum_symbol
101
117
  elsif enum_symbols(enum_name).include?(enum_symbol.to_s.to_sym)
102
- ::LibUI.const_get("#{enum_name}_#{enum_symbol}".camelcase(:upper))
118
+ enum_symbol_values(enum_name)[enum_symbol]
119
+ elsif default_symbol
120
+ enum_symbol_to_value(enum_name, default_symbol)
103
121
  else
104
122
  enum_symbol_to_value(enum_name, enum_symbols(enum_name)[default_index])
105
123
  end
106
124
  end
125
+
126
+ def x11_colors
127
+ Color::RGB.constants.reject {|c| c.to_s.upcase == c.to_s}.map(&:to_s).map(&:underscore).map(&:to_sym)
128
+ end
129
+
130
+ # Queues block to execute at the nearest opportunity possible on the main GUI event loop
131
+ def queue_main(&block)
132
+ closure = fiddle_closure_block_caller(4, [0]) do
133
+ result = boolean_to_integer(block.call)
134
+ result = 1 if result.nil?
135
+ result
136
+ end
137
+ ::LibUI.queue_main(closure)
138
+ closure
139
+ end
140
+
141
+ # Calls block on the main GUI event loop after time_in_seconds delay, repeating indefinitely by default
142
+ # If `repeat:` keyword arg is passed with an Integer value, it repeats for that number of times
143
+ # If `repeat:` keyword arg is passed with false or 0, then the block is only called once
144
+ # If block returns false at any point, the timer is stopped from further repetitions regardless of `repeat:` keyword arg value
145
+ # If block returns true at any point, the timer continues for another repetition regardless of `repeat:` keyword arg value
146
+ def timer(time_in_seconds = 0.1, repeat: true, &block)
147
+ closure = fiddle_closure_block_caller(4, [0]) do
148
+ result = boolean_to_integer(block.call)
149
+ repeat -= 1 if repeat.is_a?(Integer)
150
+ if result.nil?
151
+ if (repeat == true || (repeat.is_a?(Integer) && repeat > 0))
152
+ result = 1
153
+ else
154
+ result = 0
155
+ end
156
+ end
157
+ result
158
+ end
159
+ ::LibUI.timer(time_in_seconds * 1000.0, closure)
160
+ closure
161
+ end
162
+
163
+ def respond_to?(method_name, *args)
164
+ super || ::LibUI.respond_to?(method_name, *args)
165
+ end
166
+
167
+ def method_missing(method_name, *args, &block)
168
+ if ::LibUI.respond_to?(method_name, true)
169
+ ::LibUI.send(method_name, *args, &block)
170
+ else
171
+ super
172
+ end
173
+ end
107
174
  end
108
175
  end
109
176
  end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file