colstrom-fidgit 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +2 -0
  4. data/CHANGELOG.md +31 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.md +154 -0
  8. data/Rakefile +38 -0
  9. data/config/default_schema.yml +216 -0
  10. data/examples/_all_examples.rb +9 -0
  11. data/examples/align_example.rb +56 -0
  12. data/examples/button_and_toggle_button_example.rb +38 -0
  13. data/examples/color_picker_example.rb +17 -0
  14. data/examples/color_well_example.rb +25 -0
  15. data/examples/combo_box_example.rb +24 -0
  16. data/examples/file_dialog_example.rb +42 -0
  17. data/examples/grid_packer_example.rb +29 -0
  18. data/examples/helpers/example_window.rb +17 -0
  19. data/examples/label_example.rb +23 -0
  20. data/examples/list_example.rb +23 -0
  21. data/examples/media/images/head_icon.png +0 -0
  22. data/examples/menu_pane_example.rb +27 -0
  23. data/examples/message_dialog_example.rb +65 -0
  24. data/examples/radio_button_example.rb +37 -0
  25. data/examples/readme_example.rb +32 -0
  26. data/examples/scroll_window_example.rb +49 -0
  27. data/examples/slider_example.rb +34 -0
  28. data/examples/splash_example.rb +42 -0
  29. data/examples/text_area_example.rb +33 -0
  30. data/fidgit.gemspec +35 -0
  31. data/lib/fidgit.rb +51 -0
  32. data/lib/fidgit/chingu_ext/window.rb +6 -0
  33. data/lib/fidgit/cursor.rb +38 -0
  34. data/lib/fidgit/elements/button.rb +113 -0
  35. data/lib/fidgit/elements/color_picker.rb +63 -0
  36. data/lib/fidgit/elements/color_well.rb +39 -0
  37. data/lib/fidgit/elements/combo_box.rb +115 -0
  38. data/lib/fidgit/elements/composite.rb +17 -0
  39. data/lib/fidgit/elements/container.rb +210 -0
  40. data/lib/fidgit/elements/element.rb +298 -0
  41. data/lib/fidgit/elements/file_browser.rb +152 -0
  42. data/lib/fidgit/elements/grid.rb +227 -0
  43. data/lib/fidgit/elements/group.rb +64 -0
  44. data/lib/fidgit/elements/horizontal.rb +12 -0
  45. data/lib/fidgit/elements/image_frame.rb +65 -0
  46. data/lib/fidgit/elements/label.rb +85 -0
  47. data/lib/fidgit/elements/list.rb +47 -0
  48. data/lib/fidgit/elements/main_packer.rb +25 -0
  49. data/lib/fidgit/elements/menu_pane.rb +163 -0
  50. data/lib/fidgit/elements/packer.rb +42 -0
  51. data/lib/fidgit/elements/radio_button.rb +86 -0
  52. data/lib/fidgit/elements/scroll_area.rb +68 -0
  53. data/lib/fidgit/elements/scroll_bar.rb +128 -0
  54. data/lib/fidgit/elements/scroll_window.rb +83 -0
  55. data/lib/fidgit/elements/slider.rb +125 -0
  56. data/lib/fidgit/elements/text_area.rb +494 -0
  57. data/lib/fidgit/elements/text_line.rb +92 -0
  58. data/lib/fidgit/elements/toggle_button.rb +67 -0
  59. data/lib/fidgit/elements/tool_tip.rb +35 -0
  60. data/lib/fidgit/elements/vertical.rb +12 -0
  61. data/lib/fidgit/event.rb +159 -0
  62. data/lib/fidgit/gosu_ext/color.rb +136 -0
  63. data/lib/fidgit/gosu_ext/gosu_module.rb +25 -0
  64. data/lib/fidgit/history.rb +91 -0
  65. data/lib/fidgit/redirector.rb +83 -0
  66. data/lib/fidgit/schema.rb +123 -0
  67. data/lib/fidgit/selection.rb +106 -0
  68. data/lib/fidgit/standard_ext/hash.rb +21 -0
  69. data/lib/fidgit/states/dialog_state.rb +52 -0
  70. data/lib/fidgit/states/file_dialog.rb +24 -0
  71. data/lib/fidgit/states/gui_state.rb +331 -0
  72. data/lib/fidgit/states/message_dialog.rb +61 -0
  73. data/lib/fidgit/version.rb +5 -0
  74. data/lib/fidgit/window.rb +19 -0
  75. data/media/images/arrow.png +0 -0
  76. data/media/images/combo_arrow.png +0 -0
  77. data/media/images/file_directory.png +0 -0
  78. data/media/images/file_file.png +0 -0
  79. data/media/images/pixel.png +0 -0
  80. data/spec/fidgit/elements/helpers/helper.rb +3 -0
  81. data/spec/fidgit/elements/helpers/tex_play_helper.rb +9 -0
  82. data/spec/fidgit/elements/image_frame_spec.rb +69 -0
  83. data/spec/fidgit/elements/label_spec.rb +37 -0
  84. data/spec/fidgit/event_spec.rb +210 -0
  85. data/spec/fidgit/gosu_ext/color_spec.rb +130 -0
  86. data/spec/fidgit/gosu_ext/helpers/helper.rb +3 -0
  87. data/spec/fidgit/helpers/helper.rb +4 -0
  88. data/spec/fidgit/history_spec.rb +153 -0
  89. data/spec/fidgit/redirector_spec.rb +78 -0
  90. data/spec/fidgit/schema_spec.rb +67 -0
  91. data/spec/fidgit/schema_test.yml +32 -0
  92. metadata +320 -0
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ module Fidgit
4
+ VERSION = '0.2.7'
5
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ module Window
5
+
6
+ def self.included(base)
7
+ base.send :include, Methods
8
+ end
9
+
10
+ module Methods
11
+
12
+ def close
13
+ super
14
+ GuiState.clear
15
+ end
16
+ end
17
+
18
+ end
19
+ end
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ require "rspec"
2
+
3
+ require_relative File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "fidgit")
@@ -0,0 +1,9 @@
1
+ require 'texplay'
2
+
3
+ module Gosu
4
+ class Image
5
+ def self.create(width, height, options = {})
6
+ TexPlay.create_image($window, width, height, options)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,69 @@
1
+ require_relative 'helpers/helper'
2
+ require_relative 'helpers/tex_play_helper'
3
+
4
+ require 'fidgit'
5
+
6
+ def check_thumbnail_is_square
7
+ {square: [10, 10], tall: [5, 12], wide: [6, 5]}.each_pair do |type, dimensions|
8
+ context "with a #{type} image" do
9
+ it "should be square and just large enough to contain the image" do
10
+ subject = described_class.new(Gosu::Image.create(*dimensions), thumbnail: true)
11
+ subject.width.should equal dimensions.max
12
+ subject.height.should equal dimensions.max
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ def check_image_dimensions
19
+ {square: [10, 10], tall: [5, 12], wide: [6, 5]}.each_pair do |type, dimensions|
20
+ context "with a #{type} image" do
21
+ it "should be just large enough to contain the image" do
22
+ subject = described_class.new(Gosu::Image.create(*dimensions))
23
+ subject.width.should equal dimensions.first
24
+ subject.height.should equal dimensions.last
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ module Fidgit
31
+ describe ImageFrame do
32
+ before :all do
33
+ $window = Chingu::Window.new(100, 100, false) unless $window
34
+ end
35
+
36
+ before :each do
37
+ @image = Gosu::Image.create(10, 10)
38
+ end
39
+
40
+ subject { described_class.new(@image) }
41
+
42
+ describe '#image' do
43
+ it "should have the image set" do
44
+ subject.image.should be @image
45
+ end
46
+ end
47
+
48
+ describe '#width and #height' do
49
+ check_image_dimensions
50
+ end
51
+
52
+ describe "Thumbnails" do
53
+ subject { described_class.new(@image, thumbnail: true) }
54
+
55
+ describe '#image=' do
56
+ it "should update the height and width" do
57
+ image = Gosu::Image.create(8, 2)
58
+ subject.image = image
59
+ subject.height.should equal 8
60
+ subject.width.should equal 8
61
+ end
62
+ end
63
+
64
+ describe '#width and #height' do
65
+ check_thumbnail_is_square
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "helpers/helper"
2
+
3
+ include Fidgit
4
+
5
+ describe Label do
6
+ before :all do
7
+ $window = Chingu::Window.new(100, 100, false) unless $window
8
+ end
9
+
10
+ context "with default parameters" do
11
+ subject { Label.new( "Hello world!") }
12
+
13
+ it "should have text value set" do
14
+ subject.text.should eq "Hello world!"
15
+ end
16
+
17
+ it "should have white text" do
18
+ subject.color.should eq Gosu::Color.rgb(255, 255, 255)
19
+ end
20
+
21
+ it "should have a transparent background" do
22
+ subject.background_color.should be_transparent
23
+ end
24
+
25
+ it "should have a transparent border" do
26
+ subject.border_color.should be_transparent
27
+ end
28
+
29
+ it "should be enabled" do
30
+ subject.should be_enabled
31
+ end
32
+
33
+ it "should not have an image" do
34
+ subject.icon.should be_nil
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,210 @@
1
+ require_relative 'helpers/helper'
2
+
3
+ require 'event'
4
+
5
+ module Fidgit
6
+ describe Event do
7
+ before :each do
8
+ class Test
9
+ include Event
10
+ end
11
+ end
12
+
13
+ after :each do
14
+ Fidgit.send :remove_const, :Test
15
+ end
16
+
17
+ subject { Test }
18
+
19
+ describe "events" do
20
+ it "should initially be empty" do
21
+ subject.events.should be_empty
22
+ end
23
+ end
24
+
25
+ describe "event" do
26
+ it "should add the event to the list of events handled" do
27
+ subject.event :frog
28
+ subject.events.should include :frog
29
+ end
30
+
31
+ it "should inherit parent's events and be able to add more" do
32
+ Test.event :frog
33
+ class Test2 < Test; end
34
+ Test2.event :fish
35
+ Test2.events.should include :frog
36
+ Test2.events.should include :fish
37
+ end
38
+ end
39
+
40
+ context "When included into a class that is instanced" do
41
+ subject { Test.event :frog; Test.new }
42
+
43
+ describe "#subscribe" do
44
+ it "should add a handler as a block" do
45
+ subject.subscribe(:frog) { puts "hello" }
46
+ end
47
+
48
+ it "should fail if the event name isn't handled by this object" do
49
+ ->{ subject.subscribe(:unhandled_event) {} }.should raise_error ArgumentError
50
+ end
51
+
52
+ it "should add a handler as a method" do
53
+ subject.stub! :handler
54
+ subject.subscribe(:frog, subject.method(:handler))
55
+ end
56
+
57
+ it "should fail if neither a method or a block is passed" do
58
+ ->{ subject.subscribe(:frog) }.should raise_error ArgumentError
59
+ end
60
+
61
+ it "should fail if both a method and a block is passed" do
62
+ subject.stub! :handler
63
+ ->{ subject.subscribe(:frog, subject.method(:handler)) { } }.should raise_error ArgumentError
64
+ end
65
+
66
+ it "should return a Subscription" do
67
+ handler = proc {}
68
+ result = subject.subscribe(:frog, &handler)
69
+ result.should be_a Fidgit::Event::Subscription
70
+ result.handler.should be handler
71
+ result.publisher.should be subject
72
+ result.event.should equal :frog
73
+ end
74
+
75
+ it "should return a Subscription that can use Subscription#unsubscribe" do
76
+ handler_ran = false
77
+ subscription = subject.subscribe(:frog) { handler_ran = true }
78
+ subscription.unsubscribe
79
+ subject.publish :frog
80
+ handler_ran.should be_false
81
+ end
82
+ end
83
+
84
+ describe "#unsubscribe" do
85
+ it "should accept a Subscription" do
86
+ handler_ran = false
87
+ subscription = subject.subscribe(:frog) { handler_ran = true }
88
+ subject.unsubscribe subscription
89
+ subject.publish :frog
90
+ handler_ran.should be_false
91
+ end
92
+
93
+ it "should accept a handler" do
94
+ handler_ran = false
95
+ handler = proc { handler_ran = true }
96
+ subject.subscribe(:frog, &handler)
97
+ subject.unsubscribe handler
98
+ subject.publish :frog
99
+ handler_ran.should be_false
100
+ end
101
+
102
+ it "should accept an event and handler" do
103
+ handler_ran = false
104
+ handler = proc { handler_ran = true }
105
+ subject.subscribe(:frog, &handler)
106
+ subject.unsubscribe :frog, handler
107
+ subject.publish :frog
108
+ handler_ran.should be_false
109
+ end
110
+
111
+ it "should require 1..2 arguments" do
112
+ ->{ subject.unsubscribe }.should raise_error ArgumentError
113
+ ->{ subject.unsubscribe 1, 2, 3 }.should raise_error ArgumentError
114
+ end
115
+
116
+ it "should fail with bad types" do
117
+ ->{ subject.unsubscribe 1 }.should raise_error TypeError
118
+ ->{ subject.unsubscribe 1, ->{} }.should raise_error TypeError
119
+ ->{ subject.unsubscribe :event, 2 }.should raise_error TypeError
120
+ end
121
+
122
+ it "should fail if passed a Subscription to another object" do
123
+ subscription = Fidgit::Event::Subscription.new(Object.new.extend(Fidgit::Event), :event, proc {})
124
+ ->{ subject.unsubscribe subscription }.should raise_error ArgumentError
125
+ end
126
+ end
127
+
128
+ describe "#publish" do
129
+ it "should return nil if there are no handlers" do
130
+ subject.publish(:frog).should be_nil
131
+ end
132
+
133
+ it "should return nil if there are no handlers that handle the event" do
134
+ subject.should_receive(:frog).with(subject)
135
+ subject.should_receive(:handler).with(subject)
136
+ subject.subscribe(:frog, subject.method(:handler))
137
+ subject.publish(:frog).should be_nil
138
+ end
139
+
140
+ it "should return :handled if a manual handler handled the event and not call other handlers" do
141
+ subject.should_not_receive(:handler1)
142
+ subject.should_receive(:handler2).with(subject).and_return(:handled)
143
+ subject.subscribe(:frog, subject.method(:handler1))
144
+ subject.subscribe(:frog, subject.method(:handler2))
145
+ subject.publish(:frog).should == :handled
146
+ end
147
+
148
+ it "should return :handled if an automatic handler handled the event and not call other handlers" do
149
+ subject.should_receive(:frog).with(subject).and_return(:handled)
150
+ subject.should_not_receive(:handler2)
151
+ subject.subscribe(:frog, subject.method(:handler2))
152
+ subject.publish(:frog).should == :handled
153
+ end
154
+
155
+ it "should pass the object as the first parameter" do
156
+ subject.should_receive(:handler).with(subject)
157
+ subject.subscribe(:frog, subject.method(:handler))
158
+ subject.publish :frog
159
+ end
160
+
161
+ it "should call all the handlers, once each" do
162
+ subject.should_receive(:handler1).with(subject)
163
+ subject.should_receive(:handler2).with(subject)
164
+ subject.subscribe(:frog, subject.method(:handler1))
165
+ subject.subscribe(:frog, subject.method(:handler2))
166
+ subject.publish(:frog).should be_nil
167
+ end
168
+
169
+ it "should pass parameters passed to it" do
170
+ subject.should_receive(:handler).with(subject, 1, 2)
171
+ subject.subscribe(:frog, subject.method(:handler))
172
+ subject.publish(:frog, 1, 2).should be_nil
173
+ end
174
+
175
+ it "should do nothing if the subject is disabled" do
176
+ subject.should_receive(:enabled?).and_return(false)
177
+ subject.should_not_receive(:handler)
178
+ subject.subscribe(:frog, subject.method(:handler))
179
+ subject.publish(:frog, 1, 2).should be_nil
180
+ end
181
+
182
+ it "should act normally if the subject is enabled" do
183
+ subject.should_receive(:enabled?).and_return(true)
184
+ subject.should_receive(:handler)
185
+ subject.subscribe(:frog, subject.method(:handler))
186
+ subject.publish(:frog, 1, 2).should be_nil
187
+ end
188
+
189
+ it "should only call the handlers requested" do
190
+ Test.event :fish
191
+
192
+ subject.should_receive(:handler1).with(subject)
193
+ subject.should_not_receive(:handler2)
194
+ subject.subscribe(:frog, subject.method(:handler1))
195
+ subject.subscribe(:fish, subject.method(:handler2))
196
+ subject.publish(:frog).should be_nil
197
+ end
198
+
199
+ it "should automatically call a method on the publisher if it exists" do
200
+ subject.should_receive(:frog).with(subject)
201
+ subject.publish(:frog).should be_nil
202
+ end
203
+
204
+ it "should fail if the event name isn't handled by this object" do
205
+ ->{ subject.publish(:unhandled_event) }.should raise_error ArgumentError
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,130 @@
1
+ require_relative "helpers/helper"
2
+
3
+ include Gosu
4
+
5
+ describe Color do
6
+ describe "rgb" do
7
+ it "should create a color with the correct values from channel values" do
8
+ color = Color.rgb(1, 2, 3)
9
+ color.red.should equal 1
10
+ color.green.should equal 2
11
+ color.blue.should equal 3
12
+ color.alpha.should equal 255
13
+ end
14
+ end
15
+
16
+ describe "rgba" do
17
+ it "should create a color with the correct value from channel valuess" do
18
+ color = Color.rgba(1, 2, 3, 4)
19
+ color.red.should equal 1
20
+ color.green.should equal 2
21
+ color.blue.should equal 3
22
+ color.alpha.should equal 4
23
+ end
24
+ end
25
+
26
+ describe "argb" do
27
+ it "should create a color with the correct values from channel values" do
28
+ color = Color.argb(1, 2, 3, 4)
29
+ color.red.should equal 2
30
+ color.green.should equal 3
31
+ color.blue.should equal 4
32
+ color.alpha.should equal 1
33
+ end
34
+ end
35
+
36
+ describe "ahsv" do
37
+ it "should create a color with the correct values from channel values" do
38
+ color = Color.ahsv(1, 180.0, 0.5, 0.7)
39
+ color.hue.should be_within(0.001).of(180.0)
40
+ color.saturation.should be_within(0.01).of(0.5)
41
+ color.value.should be_within(0.01).of(0.7)
42
+ color.alpha.should equal 1
43
+ end
44
+ end
45
+
46
+ describe "hsva" do
47
+ it "should create a color with the correct values from channel values" do
48
+ color = Color.hsva(180.0, 0.5, 0.7, 4)
49
+ color.hue.should be_within(0.001).of(180.0)
50
+ color.saturation.should be_within(0.01).of(0.5)
51
+ color.value.should be_within(0.01).of(0.7)
52
+ color.alpha.should equal 4
53
+ end
54
+ end
55
+
56
+ describe "from_tex_play" do
57
+ it "should create a color with the correct values" do
58
+ color = Color.from_tex_play([1 / 255.0, 2 / 255.0, 3 / 255.0, 4 / 255.0])
59
+ color.red.should equal 1
60
+ color.green.should equal 2
61
+ color.blue.should equal 3
62
+ color.alpha.should equal 4
63
+ end
64
+ end
65
+
66
+ describe "#to_tex_play" do
67
+ it "should create an array with the correct values" do
68
+ array = Color.rgba(1, 2, 3, 4).to_tex_play
69
+ array.should eq [1 / 255.0, 2 / 255.0, 3 / 255.0, 4 / 255.0]
70
+ end
71
+ end
72
+
73
+ describe "#==" do
74
+ it "should return true for colours that are identical" do
75
+ (Color.rgb(1, 2, 3) == Color.rgb(1, 2, 3)).should be_true
76
+ end
77
+
78
+ it "should return false for colours that are not the same" do
79
+ (Color.rgb(1, 2, 3) == Color.rgb(4, 2, 3)).should be_false
80
+ end
81
+ end
82
+
83
+ describe "#transparent?" do
84
+ it "should be true if alpha is 0" do
85
+ Color.rgba(1, 1, 1, 0).should be_transparent
86
+ end
87
+
88
+ it "should be false if 0 > alpha > 255" do
89
+ Color.rgba(1, 1, 1, 2).should_not be_transparent
90
+ end
91
+
92
+ it "should be false if alpha == 255" do
93
+ Color.rgba(1, 1, 1, 255).should_not be_transparent
94
+ end
95
+ end
96
+
97
+ describe "#opaque?" do
98
+ it "should be true if alpha is 0" do
99
+ Color.rgba(1, 1, 1, 0).should_not be_opaque
100
+ end
101
+
102
+ it "should be false if 0 > alpha > 255" do
103
+ Color.rgba(1, 1, 1, 2).should_not be_opaque
104
+ end
105
+
106
+ it "should be false if alpha == 255" do
107
+ Color.rgba(1, 1, 1, 255).should be_opaque
108
+ end
109
+ end
110
+
111
+ describe "#+" do
112
+ it "should add two colours together" do
113
+ (Color.rgba(1, 2, 3, 4) + Color.rgba(10, 20, 30, 40)).should == Color.rgba(11, 22, 33, 44)
114
+ end
115
+
116
+ it "should cap values at 255" do
117
+ (Color.rgba(56, 56, 56, 56) + Color.rgba(200, 200, 200, 200)).should == Color.rgba(255, 255, 255, 255)
118
+ end
119
+ end
120
+
121
+ describe "#-" do
122
+ it "should subtract one color from another two colours together" do
123
+ (Color.rgba(10, 20, 30, 40) - Color.rgba(1, 2, 3, 4)).should == Color.rgba(9, 18, 27, 36)
124
+ end
125
+
126
+ it "should cap values at 0" do
127
+ (Color.rgba(56, 56, 56, 56) - Color.rgba(57, 57, 57, 57)).should == Color.rgba(0, 0, 0, 0)
128
+ end
129
+ end
130
+ end