fidgit 0.0.2alpha

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 (87) hide show
  1. data/.gitignore +8 -0
  2. data/.rspec +2 -0
  3. data/COPYING.txt +674 -0
  4. data/Gemfile +4 -0
  5. data/README.textile +138 -0
  6. data/Rakefile +38 -0
  7. data/config/default_schema.yml +180 -0
  8. data/examples/_all_examples.rb +9 -0
  9. data/examples/align_example.rb +56 -0
  10. data/examples/button_and_toggle_button_example.rb +27 -0
  11. data/examples/color_picker_example.rb +17 -0
  12. data/examples/color_well_example.rb +25 -0
  13. data/examples/combo_box_example.rb +24 -0
  14. data/examples/file_dialog_example.rb +42 -0
  15. data/examples/grid_packer_example.rb +29 -0
  16. data/examples/helpers/example_window.rb +17 -0
  17. data/examples/label_example.rb +17 -0
  18. data/examples/list_example.rb +23 -0
  19. data/examples/media/images/head_icon.png +0 -0
  20. data/examples/menu_pane_example.rb +27 -0
  21. data/examples/message_dialog_example.rb +65 -0
  22. data/examples/radio_button_example.rb +37 -0
  23. data/examples/readme_example.rb +32 -0
  24. data/examples/scroll_window_example.rb +49 -0
  25. data/examples/slider_example.rb +30 -0
  26. data/examples/splash_example.rb +42 -0
  27. data/examples/text_area_example.rb +28 -0
  28. data/fidgit.gemspec +28 -0
  29. data/lib/fidgit.rb +4 -0
  30. data/lib/fidgit/chingu_ext/window.rb +6 -0
  31. data/lib/fidgit/clipboard.rb +23 -0
  32. data/lib/fidgit/cursor.rb +38 -0
  33. data/lib/fidgit/elements/button.rb +68 -0
  34. data/lib/fidgit/elements/color_picker.rb +63 -0
  35. data/lib/fidgit/elements/color_well.rb +39 -0
  36. data/lib/fidgit/elements/combo_box.rb +85 -0
  37. data/lib/fidgit/elements/composite.rb +17 -0
  38. data/lib/fidgit/elements/container.rb +187 -0
  39. data/lib/fidgit/elements/element.rb +252 -0
  40. data/lib/fidgit/elements/file_browser.rb +152 -0
  41. data/lib/fidgit/elements/grid_packer.rb +219 -0
  42. data/lib/fidgit/elements/group.rb +66 -0
  43. data/lib/fidgit/elements/horizontal_packer.rb +12 -0
  44. data/lib/fidgit/elements/label.rb +77 -0
  45. data/lib/fidgit/elements/list.rb +47 -0
  46. data/lib/fidgit/elements/menu_pane.rb +149 -0
  47. data/lib/fidgit/elements/packer.rb +42 -0
  48. data/lib/fidgit/elements/radio_button.rb +86 -0
  49. data/lib/fidgit/elements/scroll_area.rb +75 -0
  50. data/lib/fidgit/elements/scroll_bar.rb +114 -0
  51. data/lib/fidgit/elements/scroll_window.rb +92 -0
  52. data/lib/fidgit/elements/slider.rb +119 -0
  53. data/lib/fidgit/elements/text_area.rb +351 -0
  54. data/lib/fidgit/elements/toggle_button.rb +67 -0
  55. data/lib/fidgit/elements/tool_tip.rb +35 -0
  56. data/lib/fidgit/elements/vertical_packer.rb +12 -0
  57. data/lib/fidgit/event.rb +99 -0
  58. data/lib/fidgit/gosu_ext/color.rb +123 -0
  59. data/lib/fidgit/history.rb +85 -0
  60. data/lib/fidgit/redirector.rb +83 -0
  61. data/lib/fidgit/schema.rb +123 -0
  62. data/lib/fidgit/selection.rb +106 -0
  63. data/lib/fidgit/standard_ext/hash.rb +21 -0
  64. data/lib/fidgit/states/dialog_state.rb +42 -0
  65. data/lib/fidgit/states/file_dialog.rb +24 -0
  66. data/lib/fidgit/states/gui_state.rb +301 -0
  67. data/lib/fidgit/states/message_dialog.rb +61 -0
  68. data/lib/fidgit/thumbnail.rb +29 -0
  69. data/lib/fidgit/version.rb +5 -0
  70. data/lib/fidgit/window.rb +19 -0
  71. data/media/images/arrow.png +0 -0
  72. data/media/images/file_directory.png +0 -0
  73. data/media/images/file_file.png +0 -0
  74. data/media/images/pixel.png +0 -0
  75. data/spec/fidgit/elements/helpers/helper.rb +3 -0
  76. data/spec/fidgit/elements/label_spec.rb +49 -0
  77. data/spec/fidgit/event_spec.rb +149 -0
  78. data/spec/fidgit/gosu_ext/color_spec.rb +130 -0
  79. data/spec/fidgit/gosu_ext/helpers/helper.rb +3 -0
  80. data/spec/fidgit/helpers/helper.rb +4 -0
  81. data/spec/fidgit/helpers/tex_play_helper.rb +9 -0
  82. data/spec/fidgit/history_spec.rb +144 -0
  83. data/spec/fidgit/redirector_spec.rb +78 -0
  84. data/spec/fidgit/schema_spec.rb +67 -0
  85. data/spec/fidgit/schema_test.yml +32 -0
  86. data/spec/fidgit/thumbnail_spec.rb +50 -0
  87. metadata +177 -0
@@ -0,0 +1,42 @@
1
+ require_relative 'helpers/example_window'
2
+
3
+ # By using a splash screen of some sort, one can switch to another resolution for the main game.
4
+ class ExampleState < Fidgit::GuiState
5
+ def initialize
6
+ super
7
+ pack :vertical do
8
+ pack :horizontal do
9
+ text = label "Width:"
10
+
11
+ @width_combo = combo_box(value: [640, 480]) do
12
+ [[640, 480], [800, 600], [Gosu::screen_width, Gosu::screen_height]].each do |width, height|
13
+ item "#{width}x#{height}", [width, height]
14
+ end
15
+ end
16
+ end
17
+
18
+ @full_screen_button = toggle_button "Fullscreen?"
19
+
20
+ button "Load game", icon: Gosu::Image["head_icon.png"] do
21
+ $window.close
22
+ window = Chingu::Window.new(*@width_combo.value, @full_screen_button.value)
23
+ window.push_game_state ExampleAfterState
24
+ window.show
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ class ExampleAfterState < GuiState
31
+ def initialize
32
+ super
33
+
34
+ on_input(:esc, :exit)
35
+
36
+ pack :vertical do
37
+ label "Game loaded!", icon: Gosu::Image["head_icon.png"], border_color: Gosu::Color.rgb(255, 255, 255)
38
+ end
39
+ end
40
+ end
41
+
42
+ ExampleWindow.new.show
@@ -0,0 +1,28 @@
1
+ require_relative 'helpers/example_window'
2
+
3
+ class ExampleState < Fidgit::GuiState
4
+ def initialize
5
+ super
6
+
7
+ pack :horizontal do
8
+ pack :vertical do
9
+ label 'editable'
10
+ text_area(width: 200)
11
+ end
12
+
13
+ pack :vertical do
14
+ label 'mirrors to right'
15
+ text_area(width: 200, text: "Hello, my name in brian the snail!") do |sender, text|
16
+ @mirror.text = text
17
+ end
18
+ end
19
+
20
+ pack :vertical do
21
+ label 'not editable'
22
+ @mirror = text_area(width: 200, enabled: false)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ ExampleWindow.new.show
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fidgit/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fidgit"
7
+ s.version = Fidgit::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bil Bas (Spooner)"]
10
+ s.email = ["bil.bagpuss@gmail.com"]
11
+ s.homepage = "http://github.com/Spooner/fidgit/"
12
+ s.summary = %q{Fidgit is a GUI library built on Gosu/Chingu}
13
+ s.description = %q{Fidgit is a GUI library built on Gosu/Chingu}
14
+
15
+ s.rubyforge_project = "fidgit"
16
+ s.has_rdoc = true
17
+ s.required_ruby_version = "~> 1.9.2"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_dependency('gosu')
25
+ s.add_dependency('chingu')
26
+ s.add_development_dependency('rspec')
27
+ s.add_development_dependency('texplay')
28
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ require 'chingu'
3
+
4
+ require_all File.join(File.dirname(__FILE__), 'fidgit')
@@ -0,0 +1,6 @@
1
+ module Chingu
2
+ class Window
3
+ # Include fidgits extra functionality in the Chingu window (which is minimal).
4
+ include Fidgit::Window
5
+ end
6
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ class Clipboard
5
+ # Items held in the clipboard.
6
+ attr_reader :items
7
+
8
+ def empty?; @items.empty?; end
9
+
10
+ def initialize
11
+ @items = []
12
+ end
13
+
14
+ # Copy items into the clipboard.
15
+ #
16
+ # @param [Array] items Items to copy
17
+ def copy(items)
18
+ @items = items.to_a.dup
19
+
20
+ nil
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ class Cursor < Chingu::GameObject
5
+ ARROW = 'arrow.png'
6
+ HAND = 'hand.png'
7
+
8
+ def initialize(options = {})
9
+ options = {
10
+ image: Gosu::Image[ARROW],
11
+ rotation_center: :top_left,
12
+ zorder: Float::INFINITY
13
+ }.merge!(options)
14
+
15
+ super(options)
16
+
17
+ nil
18
+ end
19
+
20
+ # Is the mouse pointer position inside the game window pane?
21
+ def inside_window?
22
+ x >= 0 and y >= 0 and x < $window.width and y < $window.height
23
+ end
24
+
25
+ def update
26
+ self.x, self.y = $window.mouse_x, $window.mouse_y
27
+
28
+ super
29
+
30
+ nil
31
+ end
32
+
33
+ def draw
34
+ # Prevent system and game mouse from being shown at the same time.
35
+ super if inside_window? and $window.current_game_state.is_a? GuiState and not $window.needs_cursor?
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ class Button < Label
5
+ # @param (see Label#initialize)
6
+ # @option (see Label#initialize)
7
+ def initialize(text, options = {}, &block)
8
+ options = {
9
+ color: default(:color),
10
+ background_color: default(:background_color),
11
+ border_color: default(:border_color),
12
+ }.merge! options
13
+
14
+ super(text, options)
15
+
16
+ update_colors
17
+ end
18
+
19
+ def clicked_left_mouse_button(sender, x, y)
20
+ # TODO: Play click sound?
21
+ nil
22
+ end
23
+
24
+ def enabled=(value)
25
+ super(value)
26
+ update_colors
27
+
28
+ value
29
+ end
30
+
31
+ def enter(sender)
32
+ @mouse_over = true
33
+ update_colors
34
+
35
+ nil
36
+ end
37
+
38
+ def leave(sender)
39
+ @mouse_over = false
40
+ update_colors
41
+
42
+ nil
43
+ end
44
+
45
+ protected
46
+ def update_colors
47
+ @background_color = if @mouse_over and enabled?
48
+ default(:hover, :background_color)
49
+ else
50
+ default(:background_color)
51
+ end
52
+
53
+ @color = if enabled?
54
+ default(:color)
55
+ else
56
+ default(:disabled, :color)
57
+ end
58
+
59
+ nil
60
+ end
61
+
62
+ protected
63
+ # A block added to any button subscribes to LMB click.
64
+ def post_init_block(&block)
65
+ subscribe :clicked_left_mouse_button, &block
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ class ColorPicker < Composite
5
+ CHANNELS = [:red, :green, :blue]
6
+ DEFAULT_CHANNEL_NAMES = CHANNELS.map {|c| c.to_s.capitalize }
7
+
8
+ INDICATOR_HEIGHT = 25
9
+
10
+ event :changed
11
+
12
+ def color; @color.dup; end
13
+
14
+ def color=(value)
15
+ @color = value.dup
16
+ CHANNELS.each do |channel|
17
+ @sliders[channel].value = @color.send channel
18
+ end
19
+
20
+ publish :changed, @color.dup
21
+
22
+ value
23
+ end
24
+
25
+ # @param (see Composite#initialize)
26
+ # @option (see Composite#initialize)
27
+ def initialize(options = {}, &block)
28
+ options = {
29
+ padding: 0,
30
+ spacing: 0,
31
+ channel_names: DEFAULT_CHANNEL_NAMES,
32
+ color: default(:color),
33
+ indicator_height: default(:indicator_height),
34
+ }.merge! options
35
+
36
+ @color = options[:color].dup
37
+ @indicator_height = options[:indicator_height]
38
+
39
+ super(options)
40
+
41
+ slider_width = width
42
+ pack :vertical do
43
+ @sliders = {}
44
+ CHANNELS.each_with_index do |channel, i|
45
+ @sliders[channel] = slider(value: @color.send(channel), range: 0..255, width: slider_width,
46
+ tip: options[:channel_names][i]) do |sender, value|
47
+ @color.send "#{channel}=", value
48
+ @indicator.background_color = @color
49
+ publish :changed, @color.dup
50
+ end
51
+ end
52
+
53
+ @indicator = label '', background_color: @color, width: slider_width, height: @indicator_height
54
+ end
55
+ end
56
+
57
+ protected
58
+ # Use block as an event handler.
59
+ def post_init_block(&block)
60
+ subscribe :changed, &block
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ class ColorWell < RadioButton
5
+ alias_method :color, :value
6
+
7
+ # @param (see RadioButton#initialize)
8
+ # @option (see RadioButton#initialize)
9
+ def initialize(options = {}, &block)
10
+ options = {
11
+ width: default(:width),
12
+ height: default(:height),
13
+ color: default(:color),
14
+ outline_color: default(:outline_color),
15
+ checked_border_color: default(:checked, :border_color),
16
+ }.merge! options
17
+
18
+ @outline_color = options[:outline_color].dup
19
+
20
+ super('', (options[:color] || options[:value]).dup, options)
21
+ end
22
+
23
+ protected
24
+ def draw_background
25
+ super
26
+
27
+ draw_frame x + 2, y + 2, width - 4, height - 4, 1, z, @outline_color
28
+
29
+ nil
30
+ end
31
+
32
+ protected
33
+ def draw_foreground
34
+ draw_rect x + 3, y + 3, width - 6, height - 6, z, value
35
+
36
+ nil
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ class ComboBox < Button
5
+ event :changed
6
+
7
+ def index; @menu.index(@value) end
8
+ def value; @value; end
9
+
10
+ def value=(value)
11
+ if @value != value
12
+ @value = value
13
+ @text = @menu.find(@value).text
14
+ publish :changed, @value
15
+ end
16
+
17
+ value
18
+ end
19
+
20
+ def index=(index)
21
+ if index.between?(0, @menu.size - 1)
22
+ self.value = @menu[index].value
23
+ end
24
+
25
+ index
26
+ end
27
+
28
+ # @param (see Button#initialize)
29
+ # @option (see Button#initialize)
30
+ # @option options [] :value
31
+ def initialize(options = {}, &block)
32
+ options = {
33
+ background_color: default(:background_color),
34
+ border_color: default(:border_color),
35
+ }.merge! options
36
+
37
+ @value = options[:value]
38
+
39
+ @hover_index = 0
40
+
41
+ @menu = MenuPane.new(show: false) do
42
+ subscribe :selected do |widget, value|
43
+ self.value = value
44
+ end
45
+ end
46
+
47
+ super('', options)
48
+
49
+ rect.height = [height, font_size + padding_top + padding_bottom].max
50
+ rect.width = [width, font_size * 4 + padding_left + padding_right].max
51
+ end
52
+
53
+ def item(text, value, options = {}, &block)
54
+ item = @menu.item(text, value, options, &block)
55
+
56
+ # Force text to be updated if the item added has the same value.
57
+ if item.value == @value
58
+ @text = item.text
59
+ end
60
+
61
+ item
62
+ end
63
+
64
+ def clicked_left_mouse_button(sender, x, y)
65
+ @menu.x = self.x
66
+ @menu.y = self.y + height + border_thickness
67
+ $window.game_state_manager.current_game_state.show_menu @menu
68
+
69
+ nil
70
+ end
71
+
72
+ protected
73
+ # Any combo-box passed a block will allow you access to its methods.
74
+ def post_init_block(&block)
75
+ case block.arity
76
+ when 1
77
+ yield self
78
+ when 0
79
+ instance_methods_eval &block
80
+ else
81
+ raise "block arity must be 0 or 1"
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ # A composite element, made up of other elements (but manages them internally).
5
+ class Composite < Packer
6
+ DEBUG_BORDER_COLOR = Gosu::Color.rgba(0, 255, 0, 100) # Color to draw an outline in when debugging layout.
7
+
8
+ # @param (see Element#initialize)
9
+ #
10
+ # @option (see Element#initialize)
11
+ def initialize(options = {})
12
+ options[:border_color] = DEBUG_BORDER_COLOR if Fidgit.debug_mode?
13
+
14
+ super(options)
15
+ end
16
+ end
17
+ end