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,55 +5,53 @@ end
5
5
 
6
6
  module Cura
7
7
  module Component
8
-
9
8
  # A component containing a selectable list of components.
10
9
  class Listbox < Pack
11
-
12
10
  on_event(:focus) do |event|
13
11
  if event.target == self
14
12
  set_cursor_position
15
13
  cursor.show
16
14
  end
17
15
  end
18
-
16
+
19
17
  on_event(:unfocus) do |event|
20
18
  cursor.hide if event.target == self
21
19
  end
22
-
20
+
23
21
  on_event(:key_down) do |event|
24
22
  if event.target == self
25
23
  self.selected_index -= 1 if event.name == :up
26
24
  self.selected_index += 1 if event.name == :down
27
25
  end
28
26
  end
29
-
27
+
30
28
  on_event(:mouse_button) do |event|
31
29
  if event.target == self && event.down?
32
30
  child = @children.find { |child| child.contains_coordinates?(x: event.x, y: event.y) }
33
-
31
+
34
32
  self.selected_index = @children.index(child) unless child.nil?
35
33
  end
36
34
  end
37
-
35
+
38
36
  on_event(:selected) do |event|
39
37
  set_cursor_position if event.target == self && focused?
40
38
  end
41
-
39
+
42
40
  def initialize(attributes={})
43
41
  @focusable = true
44
42
  @loopable = true
45
43
  @selected_index = 0
46
44
  @objects = []
47
-
45
+
48
46
  super
49
47
  end
50
-
48
+
51
49
  # Get the currently selected item's index.
52
50
  # Returns `nil` is nothing is selected.
53
51
  #
54
52
  # @return [nil, Integer]
55
53
  attr_reader :selected_index
56
-
54
+
57
55
  # Set the currently selected item's index.
58
56
  # Set to `nil` if nothing is selected.
59
57
  #
@@ -61,28 +59,28 @@ module Cura
61
59
  # @return [nil, Integer]
62
60
  def selected_index=(value)
63
61
  value = value.to_i
64
-
62
+
65
63
  if @loopable
66
64
  value = count == 0 ? 0 : value % count # Avoids value = value % 0 (divide by zero error)
67
65
  else
68
66
  value = 0 if value <= 0
69
67
  value = count - 1 if value >= count - 1
70
68
  end
71
-
69
+
72
70
  @selected_index = value
73
-
71
+
74
72
  application.dispatch_event(:selected, target: self)
75
-
73
+
76
74
  @selected_index
77
75
  end
78
-
76
+
79
77
  # Get the child at the selected index.
80
78
  #
81
79
  # @return [Component]
82
80
  def selected_child
83
81
  @children[@selected_index]
84
82
  end
85
-
83
+
86
84
  # Add a child to this group.
87
85
  #
88
86
  # @param [Component] component
@@ -95,20 +93,20 @@ module Cura
95
93
 
96
94
  child
97
95
  end
98
-
96
+
99
97
  # Remove a child from this listbox's children at the given index.
100
98
  #
101
99
  # @param [#to_i] index
102
100
  # @return [Component]
103
101
  def delete_child_at(index)
104
102
  deleted_child = super
105
-
103
+
106
104
  @objects.delete_at(index)
107
105
  self.selected_index = @children.length - 1 if @selected_index >= @children.length
108
-
106
+
109
107
  deleted_child
110
108
  end
111
-
109
+
112
110
  # Get the associated object with the child at the given index.
113
111
  #
114
112
  # @param [#to_i] index
@@ -116,21 +114,21 @@ module Cura
116
114
  def object_at(index)
117
115
  @objects[index]
118
116
  end
119
-
117
+
120
118
  # Get the object associated with the child at the selected index.
121
119
  #
122
120
  # @return [Component]
123
121
  def selected_object
124
122
  @objects[@selected_index]
125
123
  end
126
-
124
+
127
125
  # Get whether this listbox is loopable or not.
128
126
  #
129
127
  # @return [Boolean]
130
128
  def loopable?
131
129
  @loopable
132
130
  end
133
-
131
+
134
132
  # Set whether this listbox is loopable or not.
135
133
  #
136
134
  # @param [Object] value
@@ -138,15 +136,13 @@ module Cura
138
136
  def loopable=(value)
139
137
  @loopable = !!value
140
138
  end
141
-
139
+
142
140
  protected
143
-
141
+
144
142
  def set_cursor_position
145
143
  cursor.x = @children.empty? ? absolute_x : selected_child.absolute_x
146
144
  cursor.y = @children.empty? ? absolute_y : selected_child.absolute_y
147
145
  end
148
-
149
146
  end
150
-
151
147
  end
152
148
  end
@@ -5,22 +5,20 @@ end
5
5
 
6
6
  module Cura
7
7
  module Component
8
-
9
8
  # A component with children which moves and optionally resizes them.
10
9
  # TODO: Expand attribute - See: http://www.pygtk.org/pygtk2tutorial/sec-DetailsOfBoxes.html
11
10
  # TODO: I think the only time it needs to pack_children is right before drawing? Would that get messy?
12
11
  class Pack < Group
13
-
14
12
  include Attributes::HasOrientation
15
-
13
+
16
14
  def initialize(attributes={})
17
15
  @fill = false
18
16
  @spacing = 0
19
-
17
+
20
18
  # @child_modifiers
21
19
  super
22
20
  end
23
-
21
+
24
22
  # Set the width dimension of this pack.
25
23
  def width=(value)
26
24
  result = super
@@ -68,7 +66,7 @@ module Cura
68
66
 
69
67
  child
70
68
  end
71
-
69
+
72
70
  # Get whether children will be filled.
73
71
  # If this pack's orientation is set to :vertical, then the children's width will be set to this pack's width.
74
72
  # If this pack's orientation is set to :horizontal, then the children's height will be set to this pack's width.
@@ -77,7 +75,7 @@ module Cura
77
75
  def fill?
78
76
  @fill
79
77
  end
80
-
78
+
81
79
  # Set whether children will be filled.
82
80
  # Must be truthy or falsey.
83
81
  #
@@ -90,12 +88,12 @@ module Cura
90
88
  def fill=(value)
91
89
  @fill = !!value
92
90
  end
93
-
91
+
94
92
  # Get the spacing between children.
95
93
  #
96
94
  # @return [Integer]
97
95
  attr_reader :spacing
98
-
96
+
99
97
  # Set the spacing between children.
100
98
  #
101
99
  # @param [#to_i] value
@@ -103,10 +101,10 @@ module Cura
103
101
  def spacing=(value)
104
102
  value = value.to_i
105
103
  value = 0 if value < 0
106
-
104
+
107
105
  @spacing = value
108
106
  end
109
-
107
+
110
108
  # Draw this pack.
111
109
  #
112
110
  # @return [Pack]
@@ -115,30 +113,28 @@ module Cura
115
113
 
116
114
  super
117
115
  end
118
-
116
+
119
117
  protected
120
-
118
+
121
119
  # Position and resize this pack's children based on it's attributes.
122
120
  def pack_children
123
121
  child_x = 0
124
122
  child_y = 0
125
-
123
+
126
124
  children.each do |child|
127
125
  if horizontal?
128
126
  child.x = child_x
129
127
  child_x += child.width + child.offsets.width + spacing
130
-
128
+
131
129
  child.height = height if fill?
132
130
  elsif vertical?
133
131
  child.y = child_y if vertical?
134
132
  child_y += child.height + child.offsets.height + spacing
135
-
133
+
136
134
  child.width = width if fill?
137
135
  end
138
136
  end
139
137
  end
140
-
141
138
  end
142
-
143
139
  end
144
140
  end
@@ -6,140 +6,138 @@ end
6
6
 
7
7
  module Cura
8
8
  module Component
9
-
10
9
  # A component for scrolling.
11
10
  # TODO: Option to have buttons or not
12
11
  class Scrollbar < Group
13
-
14
12
  include Attributes::HasOrientation
15
-
13
+
16
14
  def initialize(attributes={})
17
15
  @value = 0
18
16
  @min = 0
19
17
  @max = 100
20
18
  @orientation = :vertical
21
19
  @buttons = {}
22
-
20
+
23
21
  @buttons[:decrement] = Button.new(width: 1, height: 1) { parent.decrement }
24
22
  @buttons[:increment] = Button.new(width: 1, height: 1) { parent.increment }
25
23
  @handle = Component.new(width: 1, height: 1)
26
-
24
+
27
25
  super
28
-
26
+
29
27
  setup_value
30
28
  setup_buttons
31
29
  setup_handle
32
-
30
+
33
31
  set_button_labels_based_on_orientation
34
32
  set_button_coordinates_based_on_orientation
35
33
  set_percentage
36
34
  set_handle_position
37
35
  end
38
-
36
+
39
37
  # Get the buttons for this scrollbar.
40
38
  attr_reader :buttons
41
-
39
+
42
40
  # Get the value of this scrollbar.
43
41
  attr_reader :value
44
-
42
+
45
43
  # Set the value of this scrollbar.
46
44
  def value=(value)
47
45
  raise ArgumentError, "value must respond to :to_i" unless value.respond_to?(:to_i)
48
-
46
+
49
47
  value = max if value > max
50
48
  value = min if value < min
51
-
49
+
52
50
  @value = value.to_i
53
-
51
+
54
52
  set_percentage
55
53
  set_handle_position
56
54
  end
57
-
55
+
58
56
  # Get the percentage of this scrollbar.
59
57
  attr_reader :percent
60
-
58
+
61
59
  # Set the width of this scrollbar.
62
60
  def width=(value)
63
61
  super
64
-
62
+
65
63
  # @width = 2 if @width == 0 # TODO: Depends on if buttons are shown or not AND orientation
66
-
64
+
67
65
  set_button_coordinates_based_on_orientation
68
66
  end
69
-
67
+
70
68
  # Set the height of this scrollbar.
71
69
  def height=(value)
72
70
  super
73
-
71
+
74
72
  # @height = 2 if @height == 0 # TODO: Depends on if buttons are shown or not AND orientation
75
-
73
+
76
74
  set_button_coordinates_based_on_orientation
77
75
  end
78
-
76
+
79
77
  # Get the minimum value of this scrollbar.
80
78
  attr_reader :min
81
-
79
+
82
80
  # Set the minimum value of this scrollbar.
83
81
  def min=(value)
84
82
  raise ArgumentError, "min must respond to :to_i" unless value.respond_to?(:to_i)
85
-
83
+
86
84
  @min = value.to_i
87
85
  end
88
-
86
+
89
87
  # Get the maximum value of this scrollbar.
90
88
  attr_reader :max
91
-
89
+
92
90
  # Set the maximum value of this scrollbar.
93
91
  def max=(value)
94
92
  raise ArgumentError, "max must respond to :to_i" unless value.respond_to?(:to_i)
95
-
93
+
96
94
  @max = value.to_i
97
95
  end
98
-
96
+
99
97
  # Set the orientation of this scrollbar.
100
98
  def orientation=(value)
101
99
  super
102
-
100
+
103
101
  set_button_labels_based_on_orientation
104
102
  end
105
-
103
+
106
104
  # Increment the value of this scrollbar by the given number.
107
105
  def increment(value=1)
108
106
  raise ArgumentError, "value must respond to :to_i" unless value.respond_to?(:to_i)
109
-
107
+
110
108
  self.value += value.to_i
111
109
  end
112
-
110
+
113
111
  # Decrement the value of this scrollbar by the given number.
114
112
  def decrement(value=1)
115
113
  raise ArgumentError, "value must respond to :to_i" unless value.respond_to?(:to_i)
116
-
114
+
117
115
  self.value -= value.to_i
118
116
  end
119
-
117
+
120
118
  protected
121
-
119
+
122
120
  def setup_value
123
121
  @value = min if value < min
124
122
  @value = max if value > max
125
123
  end
126
-
124
+
127
125
  def setup_buttons
128
126
  @buttons.each do |_, button|
129
127
  button.foreground = foreground
130
128
  button.background = background
131
-
129
+
132
130
  add(button)
133
131
  end
134
132
  end
135
-
133
+
136
134
  def setup_handle
137
135
  @handle.foreground = background
138
136
  @handle.background = foreground
139
-
137
+
140
138
  add(@handle)
141
139
  end
142
-
140
+
143
141
  def set_button_labels_based_on_orientation
144
142
  if vertical?
145
143
  @buttons[:decrement].text = "˄"
@@ -149,7 +147,7 @@ module Cura
149
147
  @buttons[:increment].text = "˃"
150
148
  end
151
149
  end
152
-
150
+
153
151
  def set_button_coordinates_based_on_orientation
154
152
  if vertical?
155
153
  @buttons[:increment].x = 0
@@ -159,16 +157,16 @@ module Cura
159
157
  @buttons[:increment].y = 0
160
158
  end
161
159
  end
162
-
160
+
163
161
  def set_percentage
164
162
  @percent = ((value.to_f - min.to_f) / (max.to_f - min.to_f) * 100.0).to_i
165
163
  end
166
-
164
+
167
165
  def set_handle_position
168
166
  # TODO: Only +/- padding when buttons are shown
169
167
  dimension = (vertical? ? height : width) - 3
170
168
  position = (dimension.to_f * percent.to_f / 100.0).to_i + 1
171
-
169
+
172
170
  if vertical?
173
171
  @handle.x = 0
174
172
  @handle.y = position
@@ -177,8 +175,6 @@ module Cura
177
175
  @handle.y = 0
178
176
  end
179
177
  end
180
-
181
178
  end
182
-
183
179
  end
184
180
  end