glimmer-dsl-libui 0.1.1 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -0
  3. data/README.md +1215 -30
  4. data/VERSION +1 -1
  5. data/examples/area_gallery.rb +50 -0
  6. data/examples/area_gallery2.rb +111 -0
  7. data/examples/area_gallery3.rb +52 -0
  8. data/examples/area_gallery4.rb +113 -0
  9. data/examples/basic_area2.rb +1 -1
  10. data/examples/basic_table_progress_bar.rb +13 -3
  11. data/examples/basic_transform.rb +27 -0
  12. data/examples/dynamic_area.rb +1 -1
  13. data/examples/dynamic_area2.rb +97 -0
  14. data/examples/histogram.rb +119 -0
  15. data/glimmer-dsl-libui.gemspec +0 -0
  16. data/lib/glimmer/dsl/libui/control_expression.rb +1 -1
  17. data/lib/glimmer/dsl/libui/dsl.rb +1 -0
  18. data/lib/glimmer/dsl/libui/property_expression.rb +5 -1
  19. data/lib/glimmer/dsl/libui/shape_expression.rb +56 -0
  20. data/lib/glimmer/libui/{rectangle_proxy.rb → arc.rb} +11 -26
  21. data/lib/glimmer/libui/area_proxy.rb +21 -11
  22. data/lib/glimmer/libui/bezier.rb +36 -0
  23. data/lib/glimmer/libui/box.rb +1 -1
  24. data/lib/glimmer/libui/color_button_proxy.rb +67 -15
  25. data/lib/glimmer/libui/control_proxy.rb +10 -14
  26. data/lib/glimmer/libui/figure.rb +52 -0
  27. data/lib/glimmer/libui/form_proxy.rb +1 -1
  28. data/lib/glimmer/libui/grid_proxy.rb +1 -1
  29. data/lib/glimmer/libui/line.rb +36 -0
  30. data/lib/glimmer/libui/matrix_proxy.rb +145 -0
  31. data/lib/glimmer/libui/parent.rb +36 -0
  32. data/lib/glimmer/libui/path_proxy.rb +35 -18
  33. data/lib/glimmer/libui/rectangle.rb +36 -0
  34. data/lib/glimmer/libui/shape.rb +143 -0
  35. data/lib/glimmer/libui/square.rb +36 -0
  36. data/lib/glimmer/libui/transformable.rb +72 -0
  37. data/lib/glimmer/libui/window_proxy.rb +8 -1
  38. data/lib/glimmer/libui.rb +50 -0
  39. data/lib/glimmer-dsl-libui.rb +1 -0
  40. metadata +23 -5
@@ -0,0 +1,56 @@
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/dsl/expression'
23
+ require 'glimmer/dsl/parent_expression'
24
+
25
+ module Glimmer
26
+ module DSL
27
+ module Libui
28
+ class ShapeExpression < Expression
29
+ include ParentExpression
30
+
31
+ def can_interpret?(parent, keyword, *args, &block)
32
+ Glimmer::LibUI::Shape.exists?(keyword) and
33
+ (
34
+ parent.is_a?(Glimmer::LibUI::PathProxy) or
35
+ parent.is_a?(Glimmer::LibUI::Shape)
36
+ )
37
+ end
38
+
39
+ def interpret(parent, keyword, *args, &block)
40
+ Glimmer::LibUI::Shape.create(keyword, parent, args, &block)
41
+ end
42
+
43
+ def add_content(parent, keyword, *args, &block)
44
+ super
45
+ parent.post_add_content
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ # TODO Consider moving all shapes underneath Shape namespace
54
+ require 'glimmer/libui/path_proxy'
55
+ require 'glimmer/libui/shape'
56
+ Dir[File.expand_path('../../libui/*.rb', __dir__)].each {|f| require f}
@@ -19,37 +19,22 @@
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/libui/control_proxy'
22
+ require 'glimmer/libui/shape'
23
23
 
24
24
  module Glimmer
25
25
  module LibUI
26
- # Proxy for LibUI rectangle objects
27
- #
28
- # Follows the Proxy Design Pattern
29
- class RectangleProxy < ControlProxy
30
- def initialize(keyword, parent, args, &block)
31
- @keyword = keyword
32
- @parent_proxy = parent
33
- @args = args
34
- @block = block
35
- @enabled = true
36
- post_add_content if @block.nil?
37
- end
38
-
26
+ class Arc < Shape
27
+ parameters :x_center, :y_center, :radius, :start_angle, :sweep, :is_negative
28
+ parameter_defaults 0, 0, 0, 0, 360, false
29
+
39
30
  def draw(area_draw_params)
40
- ::LibUI.draw_path_add_rectangle(@parent_proxy.libui, *@args)
41
- end
42
-
43
- def destroy
44
- if @parent_proxy
45
- @parent_proxy.children.delete(self)
31
+ @args[5] ||= Glimmer::LibUI.boolean_to_integer(@args[5], allow_nil: false)
32
+ if parent.is_a?(Figure) && parent.x.nil? && parent.y.nil?
33
+ ::LibUI.draw_path_new_figure_with_arc(path_proxy.libui, *@args)
34
+ else
35
+ ::LibUI.draw_path_arc_to(path_proxy.libui, *@args)
46
36
  end
47
- end
48
-
49
- private
50
-
51
- def build_control
52
- # No Op
37
+ super
53
38
  end
54
39
  end
55
40
  end
@@ -21,6 +21,8 @@
21
21
 
22
22
  require 'glimmer/libui/control_proxy'
23
23
  require 'glimmer/fiddle_consumer'
24
+ require 'glimmer/libui/parent'
25
+ require 'glimmer/libui/transformable'
24
26
 
25
27
  module Glimmer
26
28
  module LibUI
@@ -28,24 +30,22 @@ module Glimmer
28
30
  #
29
31
  # Follows the Proxy Design Pattern
30
32
  class AreaProxy < ControlProxy
33
+ class << self
34
+ # this attribute is only populated during on_draw call
35
+ attr_accessor :current_area_draw_params
36
+ end
37
+
31
38
  include Glimmer::FiddleConsumer
39
+ include Parent
40
+ prepend Transformable
32
41
 
33
42
  attr_reader :area_handler
34
43
 
35
- def post_initialize_child(child)
36
- super
37
- children << child
38
- end
39
-
40
44
  def post_add_content
41
45
  super
42
46
  install_listeners
43
47
  end
44
48
 
45
- def children
46
- @children ||= []
47
- end
48
-
49
49
  def on_draw(&block)
50
50
  @on_draw_procs ||= []
51
51
  if block.nil?
@@ -69,6 +69,15 @@ module Glimmer
69
69
  end
70
70
  end
71
71
 
72
+ def draw(area_draw_params)
73
+ children.dup.each {|child| child.draw(area_draw_params)}
74
+ on_draw.each {|listener| listener.call(area_draw_params)}
75
+ end
76
+
77
+ def redraw
78
+ queue_redraw_all
79
+ end
80
+
72
81
  private
73
82
 
74
83
  def build_control
@@ -80,8 +89,9 @@ module Glimmer
80
89
  @area_handler.Draw = fiddle_closure_block_caller(0, [1, 1, 1]) do |_, _, area_draw_params|
81
90
  area_draw_params = ::LibUI::FFI::AreaDrawParams.new(area_draw_params)
82
91
  area_draw_params = area_draw_params_hash(area_draw_params)
83
- children.each {|child| child.draw(area_draw_params)}
84
- on_draw.each {|listener| listener.call(area_draw_params) }
92
+ AreaProxy.current_area_draw_params = area_draw_params
93
+ draw(area_draw_params)
94
+ AreaProxy.current_area_draw_params = nil
85
95
  end
86
96
  @area_handler.MouseEvent = fiddle_closure_block_caller(0, [0]) {}
87
97
  @area_handler.MouseCrossed = fiddle_closure_block_caller(0, [0]) {}
@@ -0,0 +1,36 @@
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 Bezier < Shape
27
+ parameters :c1_x, :c1_y, :c2_x, :c2_y, :end_x, :end_y
28
+ parameter_defaults 0, 0, 0, 0, 0, 0
29
+
30
+ def draw(area_draw_params)
31
+ ::LibUI.draw_path_bezier_to(path_proxy.libui, *@args)
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
@@ -28,7 +28,7 @@ module Glimmer
28
28
 
29
29
  def post_initialize_child(child)
30
30
  child.stretchy = true if child.stretchy.nil?
31
- ::LibUI.box_append(@libui, child.libui, ControlProxy.boolean_to_integer(child.stretchy))
31
+ ::LibUI.box_append(@libui, child.libui, Glimmer::LibUI.boolean_to_integer(child.stretchy))
32
32
  children << child
33
33
  end
34
34
 
@@ -27,30 +27,82 @@ module Glimmer
27
27
  #
28
28
  # Follows the Proxy Design Pattern
29
29
  class ColorButtonProxy < ControlProxy
30
- def color
31
- @red ||= Fiddle::Pointer.malloc(8) # double
32
- @green ||= Fiddle::Pointer.malloc(8) # double
33
- @blue ||= Fiddle::Pointer.malloc(8) # double
34
- @alpha ||= Fiddle::Pointer.malloc(8) # double
35
- ::LibUI.color_button_color(@libui, @red, @green, @blue, @alpha)
36
- [@red[0, 8].unpack1('d') * 255.0, @green[0, 8].unpack1('d') * 255.0, @blue[0, 8].unpack1('d') * 255.0, @alpha[0, 8].unpack1('d')]
30
+ def color(value = nil)
31
+ # TODO support hex color value
32
+ if value.nil?
33
+ @red ||= Fiddle::Pointer.malloc(8) # double
34
+ @green ||= Fiddle::Pointer.malloc(8) # double
35
+ @blue ||= Fiddle::Pointer.malloc(8) # double
36
+ @alpha ||= Fiddle::Pointer.malloc(8) # double
37
+ ::LibUI.color_button_color(@libui, @red, @green, @blue, @alpha)
38
+ {
39
+ r: @red[0, 8].unpack1('d') * 255.0,
40
+ g: @green[0, 8].unpack1('d') * 255.0,
41
+ b: @blue[0, 8].unpack1('d') * 255.0,
42
+ a: @alpha[0, 8].unpack1('d')
43
+ }
44
+ else
45
+ current_color = color
46
+ value = Glimmer::LibUI.hex_to_rgb(value)
47
+ value[:r] ||= current_color[:r]
48
+ value[:g] ||= current_color[:g]
49
+ value[:b] ||= current_color[:b]
50
+ value[:a] ||= current_color[:a]
51
+ ::LibUI.color_button_set_color(@libui, value[:r].to_f / 255.0, value[:g].to_f / 255.0, value[:b].to_f / 255.0, value[:a])
52
+ end
37
53
  end
38
54
 
39
- def red
40
- color[0]
55
+ def red(value = nil)
56
+ if value.nil?
57
+ color[:r]
58
+ else
59
+ self.color = {r: value}
60
+ end
41
61
  end
62
+ alias red= red
63
+ alias set_red red
64
+ alias r red
65
+ alias r= red
66
+ alias set_r red
42
67
 
43
- def green
44
- color[1]
68
+ def green(value = nil)
69
+ if value.nil?
70
+ color[:g]
71
+ else
72
+ self.color = {g: value}
73
+ end
45
74
  end
75
+ alias green= green
76
+ alias set_green green
77
+ alias g green
78
+ alias g= green
79
+ alias set_g green
46
80
 
47
- def blue
48
- color[2]
81
+ def blue(value = nil)
82
+ if value.nil?
83
+ color[:b]
84
+ else
85
+ self.color = {b: value}
86
+ end
49
87
  end
88
+ alias blue= blue
89
+ alias set_blue blue
90
+ alias b blue
91
+ alias b= blue
92
+ alias set_b blue
50
93
 
51
- def alpha
52
- color[3]
94
+ def alpha(value = nil)
95
+ if value.nil?
96
+ color[:a]
97
+ else
98
+ self.color = {a: value}
99
+ end
53
100
  end
101
+ alias alpha= alpha
102
+ alias set_alpha alpha
103
+ alias a alpha
104
+ alias a= alpha
105
+ alias set_a alpha
54
106
 
55
107
  def destroy
56
108
  Fiddle.free @red unless @red.nil?
@@ -26,7 +26,7 @@ module Glimmer
26
26
  # Follows the Proxy Design Pattern
27
27
  class ControlProxy
28
28
  class << self
29
- def control_exists?(keyword)
29
+ def exists?(keyword)
30
30
  ::LibUI.respond_to?("new_#{keyword}") ||
31
31
  ::LibUI.respond_to?(keyword) ||
32
32
  Glimmer::LibUI.constants.include?("#{keyword.camelcase(:upper)}Proxy".to_sym)
@@ -55,14 +55,6 @@ module Glimmer
55
55
  control_proxies.find {|c| c.is_a?(Glimmer::LibUI::WindowProxy)}
56
56
  end
57
57
 
58
- def integer_to_boolean(int)
59
- int.nil? ? nil : int == 1
60
- end
61
-
62
- def boolean_to_integer(bool)
63
- bool.nil? ? nil : (bool ? 1 : 0)
64
- end
65
-
66
58
  def menu_proxies
67
59
  control_proxies.select {|c| c.keyword == 'menu' }
68
60
  end
@@ -92,7 +84,7 @@ module Glimmer
92
84
  ]
93
85
 
94
86
  # libui returns the contained LibUI object
95
- attr_reader :parent_proxy, :libui, :args, :keyword
87
+ attr_reader :parent_proxy, :libui, :args, :keyword, :block
96
88
 
97
89
  def initialize(keyword, parent, args, &block)
98
90
  @keyword = keyword
@@ -173,7 +165,7 @@ module Glimmer
173
165
  handle_string_property(property, handle_boolean_property(property, value))
174
166
  elsif ::LibUI.respond_to?("#{libui_api_keyword}_set_#{method_name.to_s.sub(/=$/, '')}") && !args.empty?
175
167
  property = method_name.to_s.sub(/=$/, '')
176
- args[0] = ControlProxy.boolean_to_integer(args.first) if BOOLEAN_PROPERTIES.include?(property) && (args.first.is_a?(TrueClass) || args.first.is_a?(FalseClass))
168
+ args[0] = Glimmer::LibUI.boolean_to_integer(args.first) if BOOLEAN_PROPERTIES.include?(property) && (args.first.is_a?(TrueClass) || args.first.is_a?(FalseClass))
177
169
  ::LibUI.send("#{libui_api_keyword}_set_#{property}", @libui, *args)
178
170
  elsif ::LibUI.respond_to?("#{libui_api_keyword}_#{method_name}") && !args.empty?
179
171
  ::LibUI.send("#{libui_api_keyword}_#{method_name}", @libui, *args)
@@ -183,7 +175,7 @@ module Glimmer
183
175
  handle_string_property(property, handle_boolean_property(property, value))
184
176
  elsif ::LibUI.respond_to?("control_set_#{method_name.to_s.sub(/=$/, '')}")
185
177
  property = method_name.to_s.sub(/=$/, '')
186
- args[0] = ControlProxy.boolean_to_integer(args.first) if BOOLEAN_PROPERTIES.include?(property) && (args.first.is_a?(TrueClass) || args.first.is_a?(FalseClass))
178
+ args[0] = Glimmer::LibUI.boolean_to_integer(args.first) if BOOLEAN_PROPERTIES.include?(property) && (args.first.is_a?(TrueClass) || args.first.is_a?(FalseClass))
187
179
  ::LibUI.send("control_set_#{method_name.to_s.sub(/=$/, '')}", @libui, *args)
188
180
  elsif ::LibUI.respond_to?("control_#{method_name}") && !args.empty?
189
181
  ::LibUI.send("control_#{method_name}", @libui, *args)
@@ -201,7 +193,7 @@ module Glimmer
201
193
  value = @append_property_hash[property]
202
194
  handle_string_property(property, handle_boolean_property(property, value))
203
195
  else
204
- value = ControlProxy.boolean_to_integer(value) if BOOLEAN_PROPERTIES.include?(property) && (value.is_a?(TrueClass) || value.is_a?(FalseClass))
196
+ value = Glimmer::LibUI.boolean_to_integer(value) if BOOLEAN_PROPERTIES.include?(property) && (value.is_a?(TrueClass) || value.is_a?(FalseClass))
205
197
  @append_property_hash[property] = value
206
198
  end
207
199
  end
@@ -258,6 +250,10 @@ module Glimmer
258
250
  alias set_visible visible
259
251
  alias visible= visible
260
252
 
253
+ def content(&block)
254
+ Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Libui::ControlExpression.new, @keyword, &block)
255
+ end
256
+
261
257
  private
262
258
 
263
259
  def build_control
@@ -270,7 +266,7 @@ module Glimmer
270
266
  end
271
267
 
272
268
  def handle_boolean_property(property, value)
273
- BOOLEAN_PROPERTIES.include?(property) ? ControlProxy.integer_to_boolean(value) : value
269
+ BOOLEAN_PROPERTIES.include?(property) ? Glimmer::LibUI.integer_to_boolean(value) : value
274
270
  end
275
271
 
276
272
  def handle_string_property(property, value)
@@ -0,0 +1,52 @@
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
+ # Represents a figure consisting of shapes (nested under path)
27
+ # Can optionally have `closed true` property (connecting last point to first point automatically)
28
+ class Figure < Shape
29
+ parameters :x, :y
30
+ parameter_defaults nil, nil
31
+
32
+ def draw(area_draw_params)
33
+ ::LibUI.draw_path_new_figure(path_proxy.libui, *@args) unless @args.compact.empty? # TODO if args empty then wait till there is an arc child and it starts the figure
34
+ children.dup.each {|child| child.draw(area_draw_params)}
35
+ ::LibUI.draw_path_close_figure(path_proxy.libui) if closed?
36
+ super
37
+ end
38
+
39
+ def closed(value = nil)
40
+ if value.nil?
41
+ @closed
42
+ else
43
+ @closed = value
44
+ area_proxy&.queue_redraw_all
45
+ end
46
+ end
47
+ alias closed= closed
48
+ alias set_closed closed
49
+ alias closed? closed
50
+ end
51
+ end
52
+ end
@@ -29,7 +29,7 @@ module Glimmer
29
29
  def post_initialize_child(child)
30
30
  child.label = '' if child.label.nil?
31
31
  child.stretchy = true if child.stretchy.nil?
32
- ::LibUI.form_append(@libui, child.label, child.libui, ControlProxy.boolean_to_integer(child.stretchy))
32
+ ::LibUI.form_append(@libui, child.label, child.libui, Glimmer::LibUI.boolean_to_integer(child.stretchy))
33
33
  children << child
34
34
  end
35
35
 
@@ -35,7 +35,7 @@ module Glimmer
35
35
  child.halign = 0 if child.halign.nil?
36
36
  child.vexpand = false if child.vexpand.nil?
37
37
  child.valign = 0 if child.valign.nil?
38
- ::LibUI.grid_append(@libui, child.libui, child.left, child.top, child.xspan, child.yspan, ControlProxy.boolean_to_integer(child.hexpand), child.halign, ControlProxy.boolean_to_integer(child.vexpand), child.valign)
38
+ ::LibUI.grid_append(@libui, child.libui, child.left, child.top, child.xspan, child.yspan, Glimmer::LibUI.boolean_to_integer(child.hexpand), child.halign, Glimmer::LibUI.boolean_to_integer(child.vexpand), child.valign)
39
39
  children << child
40
40
  end
41
41
 
@@ -0,0 +1,36 @@
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 Line < Shape
27
+ parameters :x, :y
28
+ parameter_defaults 0, 0
29
+
30
+ def draw(area_draw_params)
31
+ ::LibUI.draw_path_line_to(path_proxy.libui, *@args)
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,145 @@
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
+
24
+ module Glimmer
25
+ module LibUI
26
+ # Proxy for LibUI matrix objects
27
+ #
28
+ # Follows the Proxy Design Pattern
29
+ class MatrixProxy < ControlProxy
30
+ def libui_api_keyword
31
+ 'draw_matrix'
32
+ end
33
+
34
+ def clone
35
+ MatrixProxy.new('matrix', nil, [@libui.M11, @libui.M12, @libui.M21, @libui.M22, @libui.M31, @libui.M32])
36
+ end
37
+
38
+ def dup
39
+ clone
40
+ end
41
+
42
+ def m11(value = nil)
43
+ if value.nil?
44
+ @libui.M11
45
+ else
46
+ @libui.M11 = value.to_f
47
+ end
48
+ end
49
+ alias m11= m11
50
+ alias set_m11 m11
51
+
52
+ def m12(value = nil)
53
+ if value.nil?
54
+ @libui.M12
55
+ else
56
+ @libui.M12 = value.to_f
57
+ end
58
+ end
59
+ alias m12= m12
60
+ alias set_m12 m12
61
+
62
+ def m21(value = nil)
63
+ if value.nil?
64
+ @libui.M21
65
+ else
66
+ @libui.M21 = value.to_f
67
+ end
68
+ end
69
+ alias m21= m21
70
+ alias set_m21 m21
71
+
72
+ def m22(value = nil)
73
+ if value.nil?
74
+ @libui.M22
75
+ else
76
+ @libui.M22 = value.to_f
77
+ end
78
+ end
79
+ alias m22= m22
80
+ alias set_m22 m22
81
+
82
+ def m31(value = nil)
83
+ if value.nil?
84
+ @libui.M31
85
+ else
86
+ @libui.M31 = value.to_f
87
+ end
88
+ end
89
+ alias m31= m31
90
+ alias set_m31 m31
91
+
92
+ def m32(value = nil)
93
+ if value.nil?
94
+ @libui.M32
95
+ else
96
+ @libui.M32 = value.to_f
97
+ end
98
+ end
99
+ alias m32= m32
100
+ alias set_m32 m32
101
+
102
+ def identity
103
+ set_identity
104
+ end
105
+
106
+ def rotate(x = 0, y = 0, degrees)
107
+ super(x, y, (Math::PI*2.0/360.0)*degrees)
108
+ end
109
+
110
+ def scale(x_center = 0, y_center = 0, x, y)
111
+ super
112
+ end
113
+
114
+ def skew(x = 0, y = 0, x_amount, y_amount)
115
+ super
116
+ end
117
+
118
+ def multiply(matrix)
119
+ super(matrix.respond_to?(:libui) ? matrix.libui : matrix)
120
+ end
121
+
122
+ def invertible
123
+ Glimmer::LibUI.integer_to_boolean(super, allow_nil: false)
124
+ end
125
+ alias invertible? invertible
126
+
127
+ private
128
+
129
+ def build_control
130
+ @libui = ::LibUI::FFI::DrawMatrix.malloc
131
+ if @args.empty?
132
+ set_identity
133
+ else
134
+ @libui.M11 = @args[0].to_f
135
+ @libui.M12 = @args[1].to_f
136
+ @libui.M21 = @args[2].to_f
137
+ @libui.M22 = @args[3].to_f
138
+ @libui.M31 = @args[4].to_f
139
+ @libui.M32 = @args[5].to_f
140
+ end
141
+ end
142
+ end
143
+ TransformProxy = MatrixProxy # alias
144
+ end
145
+ end