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,32 @@
1
+ require_relative "../lib/fidgit"
2
+
3
+ # Normally, you'd load this from the gem using:
4
+ # require 'fidgit'
5
+
6
+ class MyGame < Chingu::Window
7
+ def initialize
8
+ super(640, 480, false)
9
+
10
+ # To use the Fidgit features, a Fidgit::GuiState must be active.
11
+ push_game_state MyGuiState
12
+ end
13
+ end
14
+
15
+ class MyGuiState < Fidgit::GuiState
16
+ def initialize
17
+ super
18
+
19
+ # Create a vertically packed section, centred on the window.
20
+ vertical align: :center do
21
+ # Create a label with a dark green background.
22
+ my_label = label "Hello world!", background_color: Gosu::Color.rgb(0, 100, 0)
23
+
24
+ # Create a button that, when clicked, changes the label text.
25
+ button("Goodbye", align_h: :center, tip: "Press me and be done with it!") do
26
+ my_label.text = "Goodbye cruel world!"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ MyGame.new.show
@@ -0,0 +1,49 @@
1
+ require_relative 'helpers/example_window'
2
+
3
+ # Example for Button and ToggleButton
4
+ class ExampleState < Fidgit::GuiState
5
+ HEIGHT = 225
6
+ WIDTH = 140
7
+ def initialize
8
+ super
9
+
10
+ vertical do
11
+ horizontal do
12
+ [
13
+ [20, "All cheer the ascendancy of number "], # Should have both scrollers
14
+ [20, "#"], # Only has v-scroller.
15
+ [4, "#"], # No scrollers.
16
+ [4, "All cheer the ascendancy of number "], # Only has h-scroller
17
+ ].each do |num_labels, text|
18
+ vertical do
19
+ scroll_window(width: WIDTH, height: HEIGHT, background_color: Gosu::Color.rgb(0, 100, 0)) do
20
+ vertical do
21
+ (1..num_labels).each do |i|
22
+ label "#{text}#{i}!"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ horizontal padding: 0 do
31
+ vertical do
32
+ scroll_window(width: 300, height: 150) do
33
+ text_area(text: "Hello world! " * 19, width: 284)
34
+ end
35
+ end
36
+
37
+ vertical do
38
+ scroll_window(width: 300, height: 150) do
39
+ %w[One Two Three Four Five Six].each do |name|
40
+ toggle_button(name)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ ExampleWindow.new.show
@@ -0,0 +1,34 @@
1
+ require_relative 'helpers/example_window'
2
+
3
+ class ExampleState < Fidgit::GuiState
4
+ def initialize
5
+ super
6
+
7
+ vertical do
8
+ horizontal do
9
+ # Discrete values (0..100)
10
+ slider = slider(width: 100, range: 0..5, value: 3, tip: "Discrete value is") do |sender, value|
11
+ @discrete_label.text = "Discrete slider is at #{value}"
12
+ end
13
+
14
+ @discrete_label = label "Discrete slider is at #{slider.value}"
15
+ end
16
+
17
+ horizontal do
18
+ # Continuous values (0.0..1.0)
19
+
20
+ slider = slider(width: 100, range: 0.0..100.0, value: 77.2, tip: "Continuous value is") do |sender, value|
21
+ @continuous_label.text = "Continuous slider is at #{"%.03f" % value}%"
22
+ end
23
+
24
+ @continuous_label = label "Continuous slider is at #{"%.03f" % slider.value}%"
25
+ end
26
+
27
+ horizontal do
28
+ slider(width: 100, range: 0.0..100.0, value: 77.2, tip: "Disabled slider value is", enabled: false)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ ExampleWindow.new.show
@@ -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
+ vertical do
8
+ 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 < Fidgit::GuiState
31
+ def initialize
32
+ super
33
+
34
+ on_input(:esc, :exit)
35
+
36
+ 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,33 @@
1
+ require_relative 'helpers/example_window'
2
+
3
+ class ExampleState < Fidgit::GuiState
4
+ def initialize
5
+ super
6
+
7
+ Gosu.register_entity(:entity, Gosu::Image["head_icon.png"])
8
+
9
+ string = "<c=3333ff>Hello, my name</c> is <c=ff0000>Brian</c> the&entity;&entity;snail!"
10
+ horizontal do
11
+ vertical do
12
+ label 'disabled'
13
+ text_area(text: "Can't even select this text", width: 200, enabled: false)
14
+ end
15
+
16
+ vertical do
17
+ label 'mirrors to right'
18
+ text_area(width: 200, text: string) do |_, text|
19
+ @mirror.text = text
20
+ end
21
+ end
22
+
23
+ vertical do
24
+ my_label = label 'not editable'
25
+ font = Gosu::Font.new($window, "", my_label.font.height)
26
+ font["a"] = Gosu::Image["head_icon.png"]
27
+ @mirror = text_area(text: string, width: 200, editable: false, font: font)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ ExampleWindow.new.show
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
+ require 'fidgit/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'colstrom-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 = "colstrom's fork of Spooner/fidgit"
13
+ s.description = '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
+ s.license = 'MIT'
24
+
25
+ s.add_runtime_dependency('gosu', '~> 0.7', '>= 0.7.41')
26
+ s.add_runtime_dependency('chingu', '~> 0.9rc9')
27
+ s.add_runtime_dependency('clipboard', '~> 0.9', '>= 0.9.9')
28
+ s.add_runtime_dependency('ffi', '~> 1.0', '>= 1.0.11')
29
+
30
+ s.add_development_dependency('rspec', '~> 2.8', '>= 2.8.0')
31
+ s.add_development_dependency('texplay', '~> 0.4', '>= 0.4.3')
32
+ s.add_development_dependency('rake', '~> 10.3', '>= 10.3.2')
33
+ s.add_development_dependency('yard', '~> 0.8.7', '>= 0.8.7.4')
34
+ s.add_development_dependency('RedCloth', '~> 4.2', '>= 4.2.9')
35
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ require 'chingu'
3
+ require 'clipboard'
4
+
5
+ require_relative "fidgit/cursor"
6
+ require_relative "fidgit/event"
7
+ require_relative "fidgit/history"
8
+ require_relative "fidgit/redirector"
9
+ require_relative "fidgit/schema"
10
+ require_relative "fidgit/selection"
11
+ require_relative "fidgit/version"
12
+ require_relative "fidgit/window"
13
+
14
+ require_relative "fidgit/chingu_ext/window"
15
+ require_relative "fidgit/gosu_ext/color"
16
+ require_relative "fidgit/gosu_ext/gosu_module"
17
+ require_relative "fidgit/standard_ext/hash"
18
+
19
+ require_relative "fidgit/elements/element"
20
+ require_relative "fidgit/elements/container"
21
+ require_relative "fidgit/elements/packer"
22
+ require_relative "fidgit/elements/composite"
23
+ require_relative "fidgit/elements/grid"
24
+ require_relative "fidgit/elements/group"
25
+ require_relative "fidgit/elements/label"
26
+ require_relative "fidgit/elements/button"
27
+ require_relative "fidgit/elements/radio_button"
28
+
29
+ require_relative "fidgit/elements/color_picker"
30
+ require_relative "fidgit/elements/color_well"
31
+ require_relative "fidgit/elements/combo_box"
32
+ require_relative "fidgit/elements/file_browser"
33
+ require_relative "fidgit/elements/horizontal"
34
+ require_relative "fidgit/elements/image_frame"
35
+ require_relative "fidgit/elements/list"
36
+ require_relative "fidgit/elements/main_packer"
37
+ require_relative "fidgit/elements/menu_pane"
38
+ require_relative "fidgit/elements/scroll_area"
39
+ require_relative "fidgit/elements/scroll_bar"
40
+ require_relative "fidgit/elements/scroll_window"
41
+ require_relative "fidgit/elements/slider"
42
+ require_relative "fidgit/elements/text_area"
43
+ require_relative "fidgit/elements/text_line"
44
+ require_relative "fidgit/elements/toggle_button"
45
+ require_relative "fidgit/elements/tool_tip"
46
+ require_relative "fidgit/elements/vertical"
47
+
48
+ require_relative "fidgit/states/gui_state"
49
+ require_relative "fidgit/states/dialog_state"
50
+ require_relative "fidgit/states/file_dialog"
51
+ require_relative "fidgit/states/message_dialog"
@@ -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,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,113 @@
1
+ # encoding: utf-8
2
+
3
+ module Fidgit
4
+ class Button < Label
5
+ # @param (see Label#initialize)
6
+ # @option (see Label#initialize)
7
+ # @option options [Symbol] :shortcut (nil) Adds a shortcut key for this element, that activates it. :auto takes the first letter of the text.
8
+ def initialize(text, options = {}, &block)
9
+ options = {
10
+ color: default(:color),
11
+ background_color: default(:background_color),
12
+ border_color: default(:border_color),
13
+ shortcut_color: default(:shortcut_color),
14
+ shortcut: nil,
15
+ }.merge! options
16
+
17
+ @shortcut_color = options[:shortcut_color].dup
18
+
19
+ @shortcut = if options[:shortcut] == :auto
20
+ raise ArgumentError.new("Can't use :auto for :shortcut without text") if text.empty?
21
+ text[0].downcase.to_sym
22
+ else
23
+ options[:shortcut]
24
+ end
25
+
26
+ raise ArgumentError.new(":shortcut must be a symbol") unless @shortcut.nil? or @shortcut.is_a? Symbol
27
+
28
+ super(text, options)
29
+
30
+ self.text = text # Force shortcut to be written out properly.
31
+
32
+ update_colors
33
+ end
34
+
35
+ def text=(value)
36
+ if @shortcut
37
+ super value.sub(/#{Regexp.escape @shortcut}/i) {|char| "<c=#{@shortcut_color.to_hex}>#{char}</c>" }
38
+ else
39
+ super value
40
+ end
41
+ end
42
+
43
+ def parent=(value)
44
+ if @shortcut
45
+ state = $window.game_state_manager.inside_state || $window.current_game_state
46
+ if parent
47
+ raise ArgumentError.new("Repeat of shortcut #{@shortcut.inspect}") if state.input.has_key? @shortcut
48
+ state.on_input(@shortcut) { activate unless state.focus }
49
+ else
50
+ state.input.delete @shortcut
51
+ end
52
+ end
53
+
54
+ super(value)
55
+ end
56
+
57
+ def clicked_left_mouse_button(sender, x, y)
58
+ # TODO: Play click sound?
59
+ nil
60
+ end
61
+
62
+ def enabled=(value)
63
+ super(value)
64
+ update_colors
65
+
66
+ value
67
+ end
68
+
69
+ def enter(sender)
70
+ @mouse_over = true
71
+ update_colors
72
+
73
+ nil
74
+ end
75
+
76
+ def leave(sender)
77
+ @mouse_over = false
78
+ update_colors
79
+
80
+ nil
81
+ end
82
+
83
+ protected
84
+ def update_colors
85
+ [:color, :background_color].each do |attribute|
86
+ send :"#{attribute.to_s}=", enabled? ? default(attribute) : default(:disabled, attribute)
87
+ end
88
+
89
+ self.background_color = if @mouse_over and enabled?
90
+ default(:hover, :background_color)
91
+ else
92
+ default(:background_color)
93
+ end
94
+
95
+ @icon.enabled = enabled? if @icon
96
+
97
+ nil
98
+ end
99
+
100
+ protected
101
+ # A block added to any button subscribes to LMB click.
102
+ def post_init_block(&block)
103
+ subscribe :clicked_left_mouse_button, &block
104
+ end
105
+
106
+ public
107
+ # Activate the button, as though it had been clicked on.
108
+ # Does not do anything if the button is disabled.
109
+ def activate
110
+ publish(:clicked_left_mouse_button, x + width / 2, y + height / 2) if enabled?
111
+ end
112
+ end
113
+ 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
+ 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