cura 0.0.1 → 0.0.2

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 (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -21
  3. data/cura.gemspec +1 -1
  4. data/examples/hello_world/lib/hello_world.rb +10 -10
  5. data/examples/mruby-examples/mrbgem.rake +5 -6
  6. data/examples/todo_list/data.db +0 -0
  7. data/examples/todo_list/lib/todo_list/application.rb +24 -18
  8. data/lib/cura/adapter.rb +13 -20
  9. data/lib/cura/application.rb +47 -51
  10. data/lib/cura/attributes/has_ancestry.rb +4 -8
  11. data/lib/cura/attributes/has_application.rb +3 -7
  12. data/lib/cura/attributes/has_attributes.rb +1 -9
  13. data/lib/cura/attributes/has_children.rb +14 -20
  14. data/lib/cura/attributes/has_colors.rb +14 -18
  15. data/lib/cura/attributes/has_coordinates.rb +9 -15
  16. data/lib/cura/attributes/has_dimensions.rb +12 -18
  17. data/lib/cura/attributes/has_events.rb +10 -18
  18. data/lib/cura/attributes/has_focusability.rb +5 -11
  19. data/lib/cura/attributes/has_initialize.rb +1 -5
  20. data/lib/cura/attributes/has_offsets.rb +16 -20
  21. data/lib/cura/attributes/has_orientation.rb +12 -18
  22. data/lib/cura/attributes/has_relative_coordinates.rb +4 -8
  23. data/lib/cura/attributes/has_root.rb +18 -22
  24. data/lib/cura/attributes/has_side_attributes.rb +18 -24
  25. data/lib/cura/attributes/has_windows.rb +13 -19
  26. data/lib/cura/borders.rb +0 -4
  27. data/lib/cura/color.rb +84 -91
  28. data/lib/cura/component/base.rb +29 -33
  29. data/lib/cura/component/button.rb +10 -16
  30. data/lib/cura/component/group.rb +14 -18
  31. data/lib/cura/component/label.rb +44 -48
  32. data/lib/cura/component/listbox.rb +24 -28
  33. data/lib/cura/component/pack.rb +14 -18
  34. data/lib/cura/component/scrollbar.rb +41 -45
  35. data/lib/cura/component/textbox.rb +21 -25
  36. data/lib/cura/cursor.rb +15 -23
  37. data/lib/cura/error/base.rb +0 -3
  38. data/lib/cura/error/invalid_adapter.rb +1 -7
  39. data/lib/cura/error/invalid_application.rb +1 -7
  40. data/lib/cura/error/invalid_color.rb +1 -7
  41. data/lib/cura/error/invalid_component.rb +1 -7
  42. data/lib/cura/error/invalid_middleware.rb +1 -7
  43. data/lib/cura/event.rb +4 -8
  44. data/lib/cura/event/base.rb +17 -24
  45. data/lib/cura/event/click.rb +1 -6
  46. data/lib/cura/event/dispatcher.rb +20 -26
  47. data/lib/cura/event/focus.rb +1 -6
  48. data/lib/cura/event/handler.rb +16 -24
  49. data/lib/cura/event/key_down.rb +11 -17
  50. data/lib/cura/event/middleware/aimer/base.rb +4 -10
  51. data/lib/cura/event/middleware/aimer/dispatcher_target.rb +2 -8
  52. data/lib/cura/event/middleware/aimer/mouse_focus.rb +6 -11
  53. data/lib/cura/event/middleware/aimer/target_option.rb +4 -10
  54. data/lib/cura/event/middleware/base.rb +0 -4
  55. data/lib/cura/event/middleware/dispatch.rb +0 -4
  56. data/lib/cura/event/middleware/translator/base.rb +4 -10
  57. data/lib/cura/event/middleware/translator/mouse_click.rb +4 -8
  58. data/lib/cura/event/mouse.rb +5 -11
  59. data/lib/cura/event/mouse_button.rb +21 -27
  60. data/lib/cura/event/mouse_wheel_down.rb +1 -6
  61. data/lib/cura/event/mouse_wheel_up.rb +1 -6
  62. data/lib/cura/event/resize.rb +0 -4
  63. data/lib/cura/event/selected.rb +1 -6
  64. data/lib/cura/event/unfocus.rb +1 -6
  65. data/lib/cura/focus_controller.rb +19 -23
  66. data/lib/cura/key.rb +277 -283
  67. data/lib/cura/margins.rb +0 -4
  68. data/lib/cura/offsets.rb +14 -18
  69. data/lib/cura/padding.rb +0 -4
  70. data/lib/cura/pencil.rb +3 -7
  71. data/lib/cura/version.rb +1 -3
  72. data/lib/cura/window.rb +11 -16
  73. data/spec/cura/attributes/has_ancestry_spec.rb +39 -39
  74. data/spec/cura/attributes/has_application_spec.rb +20 -20
  75. data/spec/cura/attributes/has_attributes_spec.rb +26 -26
  76. data/spec/cura/attributes/has_children_spec.rb +54 -54
  77. data/spec/cura/attributes/has_colors_spec.rb +4 -4
  78. data/spec/cura/attributes/has_coordinates_spec.rb +4 -4
  79. data/spec/cura/attributes/has_dimensions_spec.rb +4 -4
  80. data/spec/cura/attributes/has_events_spec.rb +4 -4
  81. data/spec/cura/attributes/has_focusability_spec.rb +18 -18
  82. data/spec/cura/attributes/has_offsets_spec.rb +4 -4
  83. data/spec/cura/attributes/has_orientation_spec.rb +38 -38
  84. data/spec/cura/attributes/has_relative_coordinates_spec.rb +4 -4
  85. data/spec/cura/attributes/has_side_attributes_spec.rb +4 -4
  86. data/spec/spec_helper.rb +1 -1
  87. data/spec/support/shared_examples_for_attributes.rb +41 -41
  88. metadata +1 -1
@@ -5,18 +5,16 @@ end
5
5
 
6
6
  module Cura
7
7
  module Component
8
-
9
8
  # A component containing editable text.
10
9
  class Textbox < Label
11
-
12
10
  on_event(:focus) do
13
11
  set_cursor_position
14
-
12
+
15
13
  cursor.show
16
14
  end
17
-
15
+
18
16
  on_event(:unfocus) { cursor.hide }
19
-
17
+
20
18
  on_event(:key_down) do |event|
21
19
  if event.target == self
22
20
  if event.name == :backspace
@@ -32,35 +30,35 @@ module Cura
32
30
  set_cursor_position
33
31
  end
34
32
  end
35
-
33
+
36
34
  def initialize(attributes={})
37
35
  @focusable = true
38
36
  @foreground = Cura::Color.black
39
37
  @background = Cura::Color.white
40
-
38
+
41
39
  super
42
-
40
+
43
41
  # TODO
44
42
  # @width = 1 if @width != :auto && @width < 1
45
43
  # @height = 1 if @height != :auto && @height < 1
46
44
  end
47
-
45
+
48
46
  # Clear all characters within this textbox.
49
47
  #
50
48
  # @return [Textbox]
51
49
  def clear
52
50
  @text = ""
53
-
51
+
54
52
  set_cursor_position
55
-
53
+
56
54
  self
57
55
  end
58
-
56
+
59
57
  # Get the mask character for this textbox.
60
58
  #
61
59
  # @return [String]
62
60
  attr_reader :mask_character
63
-
61
+
64
62
  # Set the mask character for this textbox.
65
63
  # Set to anything for a "password" textbox. Only the first character of whatever is given is used.
66
64
  #
@@ -69,50 +67,48 @@ module Cura
69
67
  def mask_character=(value)
70
68
  value = value.to_s.strip[0]
71
69
  value = nil if value.empty?
72
-
70
+
73
71
  @mask_character = value
74
72
  end
75
-
73
+
76
74
  # Set the width of this textbox.
77
75
  #
78
76
  # @param [#to_i] value
79
77
  # @return [Integer]
80
78
  def width=(value)
81
79
  super
82
-
80
+
83
81
  @width = 1 if @width < 1
84
82
  end
85
-
83
+
86
84
  # Set the height of this textbox.
87
85
  #
88
86
  # @param [#to_i] value
89
87
  # @return [Integer]
90
88
  def height=(value)
91
89
  super
92
-
90
+
93
91
  @height = 1 if @height < 1
94
92
  end
95
-
93
+
96
94
  protected
97
-
95
+
98
96
  def text_to_draw
99
97
  return text if text.length <= width
100
98
 
101
99
  focused? ? text[-width..-1] : text[0...width]
102
100
  end
103
-
101
+
104
102
  def character_to_draw(character)
105
103
  @mask_character.nil? ? character : @mask_character
106
104
  end
107
-
105
+
108
106
  def set_cursor_position
109
107
  last_line_length = lines.last.nil? ? 0 : lines.last.length
110
-
108
+
111
109
  cursor.x = absolute_x + offsets.left + (text.length < width ? last_line_length : width - 1)
112
110
  cursor.y = absolute_y + offsets.top + text_height - 1
113
111
  end
114
-
115
112
  end
116
-
117
113
  end
118
114
  end
@@ -5,60 +5,54 @@ if Kernel.respond_to?(:require)
5
5
  end
6
6
 
7
7
  module Cura
8
-
9
8
  # The text cursor controller.
10
9
  #
11
10
  # Should only ever have one single Cursor instance at one time.
12
11
  # TODO: Rename Cursor::Text, need Cursor::Mouse
13
12
  class Cursor
14
-
15
13
  include Attributes::HasInitialize
14
+ include Attributes::HasAttributes
16
15
  include Attributes::HasApplication
17
16
  include Attributes::HasCoordinates
18
-
17
+
19
18
  def initialize(attributes={})
20
19
  @hidden = true
21
-
20
+
22
21
  super
23
-
22
+
24
23
  raise ArgumentError, "application must be set" if application.nil?
25
24
  end
26
-
25
+
26
+ # @method hidden?
27
27
  # Check if the cursor is hidden.
28
28
  #
29
29
  # @return [Boolean]
30
- def hidden?
31
- !!@hidden
32
- end
33
-
30
+
31
+ # @method hidden=
34
32
  # Set if the cursor is hidden.
35
33
  #
36
34
  # @param [Boolean] value
37
35
  # @return [Boolean]
38
- def hidden=(value)
39
- value = !!value
40
-
41
- @hidden = value
42
- end
43
-
36
+ attribute(:hidden, query: true)
37
+
44
38
  # Show the cursor.
45
39
  #
46
40
  # @return [Cursor]
47
41
  def show
48
42
  @hidden = false
49
-
43
+
50
44
  self
51
45
  end
52
-
46
+
53
47
  # Hide the cursor.
54
48
  #
55
49
  # @return [Cursor]
56
50
  def hide
57
51
  @hidden = true
58
-
52
+
59
53
  self
60
54
  end
61
-
55
+
62
56
  # Draw (set) the cursor.
63
57
  #
64
58
  # @return [Cursor]
@@ -68,10 +62,8 @@ module Cura
68
62
  else
69
63
  application.adapter.set_cursor(@x, @y)
70
64
  end
71
-
65
+
72
66
  self
73
67
  end
74
-
75
68
  end
76
-
77
69
  end
@@ -1,10 +1,7 @@
1
1
  module Cura
2
2
  module Error
3
-
4
3
  # The base class for errors.
5
4
  class Base < StandardError
6
-
7
5
  end
8
-
9
6
  end
10
7
  end
@@ -1,18 +1,12 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/error/base"
3
- end
1
+ require "cura/error/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Error
7
-
8
5
  # Raised when an adapter is invalid.
9
6
  class InvalidAdapter < Base
10
-
11
7
  def to_s
12
8
  "The adapter is invalid." # TODO: Better message
13
9
  end
14
-
15
10
  end
16
-
17
11
  end
18
12
  end
@@ -1,18 +1,12 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/error/base"
3
- end
1
+ require "cura/error/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Error
7
-
8
5
  # Raised when an application is invalid.
9
6
  class InvalidApplication < Base
10
-
11
7
  def to_s
12
8
  "must nil or be a Cura::Application"
13
9
  end
14
-
15
10
  end
16
-
17
11
  end
18
12
  end
@@ -1,18 +1,12 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/error/base"
3
- end
1
+ require "cura/error/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Error
7
-
8
5
  # Raised when a color is invalid.
9
6
  class InvalidColor < Base
10
-
11
7
  def to_s
12
8
  "must be a Cura::Color or :inherit"
13
9
  end
14
-
15
10
  end
16
-
17
11
  end
18
12
  end
@@ -1,18 +1,12 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/error/base"
3
- end
1
+ require "cura/error/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Error
7
-
8
5
  # Raised when a component is invalid.
9
6
  class InvalidComponent < Base
10
-
11
7
  def to_s
12
8
  "must be a Cura::Component::Base"
13
9
  end
14
-
15
10
  end
16
-
17
11
  end
18
12
  end
@@ -1,18 +1,12 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/error/base"
3
- end
1
+ require "cura/error/base" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Error
7
-
8
5
  # Raised when middleware is invalid.
9
6
  class InvalidMiddleware < Base
10
-
11
7
  def to_s
12
8
  "must respond to #call."
13
9
  end
14
-
15
10
  end
16
-
17
11
  end
18
12
  end
@@ -1,26 +1,24 @@
1
1
  module Cura
2
-
3
2
  # The container for Event::Base and it's subclasses.
4
3
  module Event
5
4
  class << self
6
-
7
5
  # Get all Event::Base subclasses.
8
6
  #
9
7
  # @return [Array<Class>]
10
8
  def all
11
9
  @all ||= []
12
10
  end
13
-
11
+
14
12
  # Find an Event::Base subclass by it's name.
15
13
  #
16
14
  # @param [#to_sym] name The name of the event. For example, `SomeAction` would be `:some_action`.
17
15
  # @return [nil, Class] The Event::Base subclass.
18
16
  def find_by_name(name)
19
17
  name = name.to_sym
20
-
18
+
21
19
  all.find { |event_class| event_class.name == name }
22
20
  end
23
-
21
+
24
22
  # Initialize an Event::Base subclass by it's name.
25
23
  #
26
24
  # @param [#to_sym] name The name of the event class.
@@ -28,11 +26,9 @@ module Cura
28
26
  # TODO: name should be a string formatted like so 'mouse:button:down' which would correspond to Cura::Event::Mouse::Button::Down
29
27
  event_class = find_by_name(name)
30
28
  raise ArgumentError, "Unknown event name '#{name}'" if event_class.nil?
31
-
29
+
32
30
  event_class.new(attributes)
33
31
  end
34
-
35
32
  end # << self
36
33
  end
37
-
38
34
  end
@@ -2,18 +2,14 @@ if Kernel.respond_to?(:require)
2
2
  require "cura/attributes/has_initialize"
3
3
  require "cura/attributes/has_attributes"
4
4
  require "cura/attributes/has_events"
5
-
6
5
  require "cura/event"
7
6
  end
8
7
 
9
8
  module Cura
10
9
  module Event
11
-
12
10
  # The base class for all events.
13
11
  class Base
14
-
15
12
  class << self
16
-
17
13
  # Get the name of this class as a symbol.
18
14
  # For example, `SomeAction` would be `:some_action`.
19
15
  #
@@ -22,38 +18,37 @@ module Cura
22
18
  # Note: 1.3 times faster but relys on Regexp and is the only usage of regexp throughout cura.
23
19
  # Using non regexp version until multiple usages of regexp occur within cura.
24
20
  # to_s.split(/::/).last.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym
25
-
21
+
26
22
  # Note: MRuby does not have a #chars method so this is now 2.6 times slower instead of 1.3
27
23
  caps = ("A".."Z")
28
24
  index = 0
29
- to_s.split("::").last.split("").each_with_object("") { |char, memo|
25
+ to_s.split("::").last.split("").each_with_object("") do |char, memo|
30
26
  memo << "_" if index > 0 && caps.include?(char)
31
27
  memo << char
32
28
  index += 1
33
- }.downcase.to_sym
29
+ end.downcase.to_sym
34
30
  end
35
-
31
+
36
32
  # Add the subclass to `Event.all`, when inherited.
37
33
  def inherited(subclass)
38
34
  Event.all << subclass
39
35
  end
40
-
41
36
  end # << self
42
-
37
+
43
38
  include Attributes::HasInitialize
44
39
  include Attributes::HasAttributes
45
-
40
+
46
41
  def initialize(attributes={})
47
42
  @created_at = Time.now
48
-
43
+
49
44
  super
50
45
  end
51
-
46
+
52
47
  # Get the time this event was created.
53
48
  #
54
49
  # @return [Time]
55
50
  attr_reader :created_at
56
-
51
+
57
52
  # Get the target this event was dispatched to.
58
53
  # TODO: Rename to source.
59
54
  # The source is the component the event was originally sent to.
@@ -61,24 +56,24 @@ module Cura
61
56
  #
62
57
  # @return [Cura::Attributes::HasEvents]
63
58
  attr_reader :target
64
-
59
+
65
60
  # Set the target this event was dispatched to.
66
61
  #
67
62
  # @param [Cura::Attributes::HasEvents] value
68
63
  # @return [Cura::Attributes::HasEvents]
69
64
  def target=(value)
70
65
  raise TypeError, "target must be a Cura::Attributes::HasEvents" unless value.is_a?(Cura::Attributes::HasEvents) # TODO: Error::Event::InvalidTarget
71
-
66
+
72
67
  @target = value
73
68
  end
74
-
69
+
75
70
  # Get this event as a Hash.
76
71
  #
77
72
  # @return [Hash]
78
73
  def to_h
79
74
  { name: self.class.name }
80
75
  end
81
-
76
+
82
77
  # Check if something is equivalent to this event.
83
78
  #
84
79
  # @param [Object] other The object to check equivalence with.
@@ -87,22 +82,20 @@ module Cura
87
82
  # TODO: Below is needed?
88
83
  # object_equivalence = super
89
84
  # return true if object_equivalence
90
-
85
+
91
86
  other = other.to_h if other.respond_to?(:to_h)
92
-
87
+
93
88
  other == to_h
94
89
  end
95
-
90
+
96
91
  # Dispatches this event directly to it's target.
97
92
  #
98
93
  # @return [Event::Base] This event.
99
94
  def dispatch
100
95
  target.event_handler.handle(self)
101
-
96
+
102
97
  self
103
98
  end
104
-
105
99
  end
106
-
107
100
  end
108
101
  end