fidgit 0.2.4 → 0.2.5
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.
- data/.gitignore +7 -7
- data/.rspec +2 -2
- data/CHANGELOG.md +30 -30
- data/Gemfile +3 -3
- data/LICENSE.txt +19 -19
- data/README.textile +139 -139
- data/Rakefile +37 -37
- data/config/default_schema.yml +216 -216
- data/examples/_all_examples.rb +9 -9
- data/examples/align_example.rb +55 -55
- data/examples/button_and_toggle_button_example.rb +37 -37
- data/examples/color_picker_example.rb +16 -16
- data/examples/color_well_example.rb +24 -24
- data/examples/combo_box_example.rb +23 -23
- data/examples/file_dialog_example.rb +41 -41
- data/examples/grid_packer_example.rb +28 -28
- data/examples/helpers/example_window.rb +16 -16
- data/examples/label_example.rb +22 -22
- data/examples/list_example.rb +22 -22
- data/examples/menu_pane_example.rb +26 -26
- data/examples/message_dialog_example.rb +64 -64
- data/examples/radio_button_example.rb +36 -36
- data/examples/readme_example.rb +31 -31
- data/examples/scroll_window_example.rb +48 -48
- data/examples/slider_example.rb +33 -33
- data/examples/splash_example.rb +41 -41
- data/examples/text_area_example.rb +32 -32
- data/fidgit.gemspec +35 -35
- data/lib/fidgit.rb +50 -50
- data/lib/fidgit/chingu_ext/window.rb +5 -5
- data/lib/fidgit/cursor.rb +37 -37
- data/lib/fidgit/elements/button.rb +112 -112
- data/lib/fidgit/elements/color_picker.rb +62 -62
- data/lib/fidgit/elements/color_well.rb +38 -38
- data/lib/fidgit/elements/combo_box.rb +113 -113
- data/lib/fidgit/elements/composite.rb +16 -16
- data/lib/fidgit/elements/container.rb +208 -208
- data/lib/fidgit/elements/element.rb +297 -297
- data/lib/fidgit/elements/file_browser.rb +151 -151
- data/lib/fidgit/elements/grid.rb +226 -226
- data/lib/fidgit/elements/group.rb +64 -64
- data/lib/fidgit/elements/horizontal.rb +11 -11
- data/lib/fidgit/elements/image_frame.rb +64 -64
- data/lib/fidgit/elements/label.rb +84 -84
- data/lib/fidgit/elements/list.rb +46 -46
- data/lib/fidgit/elements/main_packer.rb +24 -24
- data/lib/fidgit/elements/menu_pane.rb +160 -160
- data/lib/fidgit/elements/packer.rb +41 -41
- data/lib/fidgit/elements/radio_button.rb +85 -85
- data/lib/fidgit/elements/scroll_area.rb +67 -67
- data/lib/fidgit/elements/scroll_bar.rb +127 -127
- data/lib/fidgit/elements/scroll_window.rb +82 -82
- data/lib/fidgit/elements/slider.rb +124 -124
- data/lib/fidgit/elements/text_area.rb +493 -493
- data/lib/fidgit/elements/text_line.rb +91 -91
- data/lib/fidgit/elements/toggle_button.rb +66 -66
- data/lib/fidgit/elements/tool_tip.rb +34 -34
- data/lib/fidgit/elements/vertical.rb +11 -11
- data/lib/fidgit/event.rb +158 -158
- data/lib/fidgit/gosu_ext/color.rb +135 -135
- data/lib/fidgit/gosu_ext/gosu_module.rb +24 -24
- data/lib/fidgit/history.rb +90 -90
- data/lib/fidgit/redirector.rb +82 -82
- data/lib/fidgit/schema.rb +123 -123
- data/lib/fidgit/selection.rb +105 -105
- data/lib/fidgit/standard_ext/hash.rb +20 -20
- data/lib/fidgit/states/dialog_state.rb +51 -51
- data/lib/fidgit/states/file_dialog.rb +24 -24
- data/lib/fidgit/states/gui_state.rb +329 -329
- data/lib/fidgit/states/message_dialog.rb +60 -60
- data/lib/fidgit/version.rb +4 -4
- data/lib/fidgit/window.rb +19 -19
- data/spec/fidgit/elements/helpers/helper.rb +2 -2
- data/spec/fidgit/elements/helpers/tex_play_helper.rb +8 -8
- data/spec/fidgit/elements/image_frame_spec.rb +68 -68
- data/spec/fidgit/elements/label_spec.rb +36 -36
- data/spec/fidgit/event_spec.rb +209 -209
- data/spec/fidgit/gosu_ext/color_spec.rb +129 -129
- data/spec/fidgit/gosu_ext/helpers/helper.rb +2 -2
- data/spec/fidgit/helpers/helper.rb +3 -3
- data/spec/fidgit/history_spec.rb +153 -153
- data/spec/fidgit/redirector_spec.rb +77 -77
- data/spec/fidgit/schema_spec.rb +66 -66
- data/spec/fidgit/schema_test.yml +32 -32
- metadata +67 -22
@@ -1,136 +1,136 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Gosu
|
4
|
-
class Color
|
5
|
-
# Is the color completely transparent?
|
6
|
-
def transparent?; alpha == 0; end
|
7
|
-
# Is the color completely opaque?
|
8
|
-
def opaque?; alpha == 255; end
|
9
|
-
|
10
|
-
# RGB in 0..255 format (Alpha assumed 255)
|
11
|
-
#
|
12
|
-
# @param [Integer] red
|
13
|
-
# @param [Integer] green
|
14
|
-
# @param [Integer] blue
|
15
|
-
# @return [Color]
|
16
|
-
def self.rgb(red, green, blue)
|
17
|
-
new(255, red, green, blue)
|
18
|
-
end
|
19
|
-
|
20
|
-
# RGBA in 0..255 format
|
21
|
-
#
|
22
|
-
# @param [Integer] red
|
23
|
-
# @param [Integer] green
|
24
|
-
# @param [Integer] blue
|
25
|
-
# @param [Integer] alpha
|
26
|
-
# @return [Color]
|
27
|
-
def self.rgba(red, green, blue, alpha)
|
28
|
-
new(alpha, red, green, blue)
|
29
|
-
end
|
30
|
-
|
31
|
-
# ARGB in 0..255 format (equivalent to Color.new, but explicit)
|
32
|
-
#
|
33
|
-
# @param [Integer] alpha
|
34
|
-
# @param (see Color.rgb)
|
35
|
-
# @return [Color]
|
36
|
-
def self.argb(alpha, red, green, blue)
|
37
|
-
new(alpha, red, green, blue)
|
38
|
-
end
|
39
|
-
|
40
|
-
# HSV format (alpha assumed to be 255)
|
41
|
-
#
|
42
|
-
# @param [Float] hue 0.0..360.0
|
43
|
-
# @param [Float] saturation 0.0..1.0
|
44
|
-
# @param [Float] value 0.0..1.0
|
45
|
-
# @return [Color]
|
46
|
-
def self.hsv(hue, saturation, value)
|
47
|
-
from_hsv(hue, saturation, value)
|
48
|
-
end
|
49
|
-
|
50
|
-
# HSVA format
|
51
|
-
#
|
52
|
-
# @param [Float] hue 0.0..360.0
|
53
|
-
# @param [Float] saturation 0.0..1.0
|
54
|
-
# @param [Float] value 0.0..1.0
|
55
|
-
# @param [Integer] alpha 1..255
|
56
|
-
# @return [Color]
|
57
|
-
def self.hsva(hue, saturation, value, alpha)
|
58
|
-
from_ahsv(alpha, hue, saturation, value)
|
59
|
-
end
|
60
|
-
|
61
|
-
class << self
|
62
|
-
alias_method :ahsv, :from_ahsv
|
63
|
-
end
|
64
|
-
|
65
|
-
# Convert from an RGBA array, as used by TexPlay.
|
66
|
-
#
|
67
|
-
# @param [Array<Float>] color TexPlay color [r, g, b, a] in range 0.0..1.0
|
68
|
-
# @return [Color]
|
69
|
-
def self.from_tex_play(color)
|
70
|
-
rgba(*color.map {|c| (c * 255).to_i })
|
71
|
-
end
|
72
|
-
|
73
|
-
# Convert to an RGBA array, as used by TexPlay.
|
74
|
-
#
|
75
|
-
# @return [Array<Float>] TexPlay color array [r, g, b, a] in range 0.0..1.0
|
76
|
-
def to_tex_play
|
77
|
-
[red / 255.0, green / 255.0, blue / 255.0, alpha / 255.0]
|
78
|
-
end
|
79
|
-
|
80
|
-
# Convert to a 6-digit hexadecimal value, appropriate for passing to the Gosu +<c>+ tag. The alpha channel is ignored.
|
81
|
-
#
|
82
|
-
# @return [String] RGB hexadecimal string, such as "AABB00"
|
83
|
-
def to_hex
|
84
|
-
"%02x%02x%02x" % [red, green, blue]
|
85
|
-
end
|
86
|
-
|
87
|
-
# Colorize text for in-line rendering by Gosu.
|
88
|
-
# e.g. "frog" => "<c=00ff9a>frog</c>"
|
89
|
-
def colorize(text)
|
90
|
-
"<c=#{to_hex}>#{text}</c>"
|
91
|
-
end
|
92
|
-
|
93
|
-
# Show the Color as <RGBA [0, 0, 0, 0]> or, if opaque, <RGB [0, 0, 0]> (Gosu default is '(ARGB:0/0/0/0)')
|
94
|
-
def to_s
|
95
|
-
if opaque?
|
96
|
-
"<RGB [#{red}, #{green}, #{blue}]>"
|
97
|
-
else
|
98
|
-
"<RGBA [#{red}, #{green}, #{blue}, #{alpha}]>"
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def +(other)
|
103
|
-
raise ArgumentError, "Can only add another #{self.class}" unless other.is_a? Color
|
104
|
-
|
105
|
-
copy = Color.new(0)
|
106
|
-
|
107
|
-
copy.red = [red + other.red, 255].min
|
108
|
-
copy.green = [green + other.green, 255].min
|
109
|
-
copy.blue = [blue + other.blue, 255].min
|
110
|
-
copy.alpha = [alpha + other.alpha, 255].min
|
111
|
-
|
112
|
-
copy
|
113
|
-
end
|
114
|
-
|
115
|
-
def -(other)
|
116
|
-
raise ArgumentError, "Can only take away another #{self.class}" unless other.is_a? Color
|
117
|
-
|
118
|
-
copy = Color.new(0)
|
119
|
-
|
120
|
-
copy.red = [red - other.red, 0].max
|
121
|
-
copy.green = [green - other.green, 0].max
|
122
|
-
copy.blue = [blue - other.blue, 0].max
|
123
|
-
copy.alpha = [alpha - other.alpha, 0].max
|
124
|
-
|
125
|
-
copy
|
126
|
-
end
|
127
|
-
|
128
|
-
def ==(other)
|
129
|
-
if other.is_a? Color
|
130
|
-
red == other.red and green == other.green and blue == other.blue and alpha == other.alpha
|
131
|
-
else
|
132
|
-
false
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Gosu
|
4
|
+
class Color
|
5
|
+
# Is the color completely transparent?
|
6
|
+
def transparent?; alpha == 0; end
|
7
|
+
# Is the color completely opaque?
|
8
|
+
def opaque?; alpha == 255; end
|
9
|
+
|
10
|
+
# RGB in 0..255 format (Alpha assumed 255)
|
11
|
+
#
|
12
|
+
# @param [Integer] red
|
13
|
+
# @param [Integer] green
|
14
|
+
# @param [Integer] blue
|
15
|
+
# @return [Color]
|
16
|
+
def self.rgb(red, green, blue)
|
17
|
+
new(255, red, green, blue)
|
18
|
+
end
|
19
|
+
|
20
|
+
# RGBA in 0..255 format
|
21
|
+
#
|
22
|
+
# @param [Integer] red
|
23
|
+
# @param [Integer] green
|
24
|
+
# @param [Integer] blue
|
25
|
+
# @param [Integer] alpha
|
26
|
+
# @return [Color]
|
27
|
+
def self.rgba(red, green, blue, alpha)
|
28
|
+
new(alpha, red, green, blue)
|
29
|
+
end
|
30
|
+
|
31
|
+
# ARGB in 0..255 format (equivalent to Color.new, but explicit)
|
32
|
+
#
|
33
|
+
# @param [Integer] alpha
|
34
|
+
# @param (see Color.rgb)
|
35
|
+
# @return [Color]
|
36
|
+
def self.argb(alpha, red, green, blue)
|
37
|
+
new(alpha, red, green, blue)
|
38
|
+
end
|
39
|
+
|
40
|
+
# HSV format (alpha assumed to be 255)
|
41
|
+
#
|
42
|
+
# @param [Float] hue 0.0..360.0
|
43
|
+
# @param [Float] saturation 0.0..1.0
|
44
|
+
# @param [Float] value 0.0..1.0
|
45
|
+
# @return [Color]
|
46
|
+
def self.hsv(hue, saturation, value)
|
47
|
+
from_hsv(hue, saturation, value)
|
48
|
+
end
|
49
|
+
|
50
|
+
# HSVA format
|
51
|
+
#
|
52
|
+
# @param [Float] hue 0.0..360.0
|
53
|
+
# @param [Float] saturation 0.0..1.0
|
54
|
+
# @param [Float] value 0.0..1.0
|
55
|
+
# @param [Integer] alpha 1..255
|
56
|
+
# @return [Color]
|
57
|
+
def self.hsva(hue, saturation, value, alpha)
|
58
|
+
from_ahsv(alpha, hue, saturation, value)
|
59
|
+
end
|
60
|
+
|
61
|
+
class << self
|
62
|
+
alias_method :ahsv, :from_ahsv
|
63
|
+
end
|
64
|
+
|
65
|
+
# Convert from an RGBA array, as used by TexPlay.
|
66
|
+
#
|
67
|
+
# @param [Array<Float>] color TexPlay color [r, g, b, a] in range 0.0..1.0
|
68
|
+
# @return [Color]
|
69
|
+
def self.from_tex_play(color)
|
70
|
+
rgba(*color.map {|c| (c * 255).to_i })
|
71
|
+
end
|
72
|
+
|
73
|
+
# Convert to an RGBA array, as used by TexPlay.
|
74
|
+
#
|
75
|
+
# @return [Array<Float>] TexPlay color array [r, g, b, a] in range 0.0..1.0
|
76
|
+
def to_tex_play
|
77
|
+
[red / 255.0, green / 255.0, blue / 255.0, alpha / 255.0]
|
78
|
+
end
|
79
|
+
|
80
|
+
# Convert to a 6-digit hexadecimal value, appropriate for passing to the Gosu +<c>+ tag. The alpha channel is ignored.
|
81
|
+
#
|
82
|
+
# @return [String] RGB hexadecimal string, such as "AABB00"
|
83
|
+
def to_hex
|
84
|
+
"%02x%02x%02x" % [red, green, blue]
|
85
|
+
end
|
86
|
+
|
87
|
+
# Colorize text for in-line rendering by Gosu.
|
88
|
+
# e.g. "frog" => "<c=00ff9a>frog</c>"
|
89
|
+
def colorize(text)
|
90
|
+
"<c=#{to_hex}>#{text}</c>"
|
91
|
+
end
|
92
|
+
|
93
|
+
# Show the Color as <RGBA [0, 0, 0, 0]> or, if opaque, <RGB [0, 0, 0]> (Gosu default is '(ARGB:0/0/0/0)')
|
94
|
+
def to_s
|
95
|
+
if opaque?
|
96
|
+
"<RGB [#{red}, #{green}, #{blue}]>"
|
97
|
+
else
|
98
|
+
"<RGBA [#{red}, #{green}, #{blue}, #{alpha}]>"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def +(other)
|
103
|
+
raise ArgumentError, "Can only add another #{self.class}" unless other.is_a? Color
|
104
|
+
|
105
|
+
copy = Color.new(0)
|
106
|
+
|
107
|
+
copy.red = [red + other.red, 255].min
|
108
|
+
copy.green = [green + other.green, 255].min
|
109
|
+
copy.blue = [blue + other.blue, 255].min
|
110
|
+
copy.alpha = [alpha + other.alpha, 255].min
|
111
|
+
|
112
|
+
copy
|
113
|
+
end
|
114
|
+
|
115
|
+
def -(other)
|
116
|
+
raise ArgumentError, "Can only take away another #{self.class}" unless other.is_a? Color
|
117
|
+
|
118
|
+
copy = Color.new(0)
|
119
|
+
|
120
|
+
copy.red = [red - other.red, 0].max
|
121
|
+
copy.green = [green - other.green, 0].max
|
122
|
+
copy.blue = [blue - other.blue, 0].max
|
123
|
+
copy.alpha = [alpha - other.alpha, 0].max
|
124
|
+
|
125
|
+
copy
|
126
|
+
end
|
127
|
+
|
128
|
+
def ==(other)
|
129
|
+
if other.is_a? Color
|
130
|
+
red == other.red and green == other.green and blue == other.blue and alpha == other.alpha
|
131
|
+
else
|
132
|
+
false
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
136
|
end
|
@@ -1,25 +1,25 @@
|
|
1
|
-
module Gosu
|
2
|
-
class << self
|
3
|
-
alias_method :register_entity_fidgit, :register_entity
|
4
|
-
|
5
|
-
protected
|
6
|
-
def init_entities
|
7
|
-
@entities = {}
|
8
|
-
end
|
9
|
-
|
10
|
-
public
|
11
|
-
def register_entity(name, image)
|
12
|
-
name = name.to_sym
|
13
|
-
register_entity_fidgit(name, image)
|
14
|
-
@entities[name] = image
|
15
|
-
nil
|
16
|
-
end
|
17
|
-
|
18
|
-
public
|
19
|
-
def entity(name)
|
20
|
-
@entities[name.to_sym]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
init_entities
|
1
|
+
module Gosu
|
2
|
+
class << self
|
3
|
+
alias_method :register_entity_fidgit, :register_entity
|
4
|
+
|
5
|
+
protected
|
6
|
+
def init_entities
|
7
|
+
@entities = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
public
|
11
|
+
def register_entity(name, image)
|
12
|
+
name = name.to_sym
|
13
|
+
register_entity_fidgit(name, image)
|
14
|
+
@entities[name] = image
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
|
18
|
+
public
|
19
|
+
def entity(name)
|
20
|
+
@entities[name.to_sym]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
init_entities
|
25
25
|
end
|
data/lib/fidgit/history.rb
CHANGED
@@ -1,91 +1,91 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Fidgit
|
4
|
-
# Manages a history of actions, along with doing, undoing and redoing those actions.
|
5
|
-
class History
|
6
|
-
# Maximum number of actions in the History before Actions are deleted.
|
7
|
-
DEFAULT_MAX_SIZE = 250
|
8
|
-
|
9
|
-
# An action in the History. Inherit actions from this in order to add them to a History.
|
10
|
-
class Action
|
11
|
-
# Perform the action.
|
12
|
-
def do; raise NotImplementedError, "#{self.class} does not have a do method defined"; end
|
13
|
-
|
14
|
-
# Reverse the action.
|
15
|
-
def undo; raise NotImplementedError, "#{self.class} does not have an undo method defined"; end
|
16
|
-
end
|
17
|
-
|
18
|
-
# Is there an action that can be undone?
|
19
|
-
def can_undo?; @last_done >= 0; end
|
20
|
-
|
21
|
-
# Is there an action that has been undone that can now be redone?
|
22
|
-
def can_redo?; @last_done < (@actions.size - 1); end
|
23
|
-
|
24
|
-
def initialize(max_size = DEFAULT_MAX_SIZE)
|
25
|
-
@max_size = max_size
|
26
|
-
@actions = []
|
27
|
-
@last_done = -1 # Last command that was performed.
|
28
|
-
end
|
29
|
-
|
30
|
-
# Perform a History::Action, adding it to the history.
|
31
|
-
# If there are currently any actions that have been undone, they will be permanently lost and cannot be redone.
|
32
|
-
#
|
33
|
-
# @param [History::Action] action Action to be performed
|
34
|
-
def do(action)
|
35
|
-
raise ArgumentError, "Parameter, 'action', expected to be a #{Action}, but received: #{action}" unless action.is_a? Action
|
36
|
-
|
37
|
-
# Remove all undone actions when a new one is performed.
|
38
|
-
if can_redo?
|
39
|
-
if @last_done == -1
|
40
|
-
@actions.clear
|
41
|
-
else
|
42
|
-
@actions = @actions[0..@last_done]
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# If history is too big, remove the oldest action.
|
47
|
-
if @actions.size >= @max_size
|
48
|
-
@actions.shift
|
49
|
-
end
|
50
|
-
|
51
|
-
@last_done = @actions.size
|
52
|
-
@actions << action
|
53
|
-
action.do
|
54
|
-
|
55
|
-
nil
|
56
|
-
end
|
57
|
-
|
58
|
-
# Perform a History::Action, replacing the last action that was performed.
|
59
|
-
#
|
60
|
-
# @param [History::Action] action Action to be performed
|
61
|
-
def replace_last(action)
|
62
|
-
raise ArgumentError, "Parameter, 'action', expected to be a #{Action}, but received: #{action}" unless action.is_a? Action
|
63
|
-
|
64
|
-
@actions[@last_done].undo
|
65
|
-
@actions[@last_done] = action
|
66
|
-
action.do
|
67
|
-
|
68
|
-
nil
|
69
|
-
end
|
70
|
-
|
71
|
-
# Undo the last action that was performed.
|
72
|
-
def undo
|
73
|
-
raise "Can't undo unless there are commands in past" unless can_undo?
|
74
|
-
|
75
|
-
@actions[@last_done].undo
|
76
|
-
@last_done -= 1
|
77
|
-
|
78
|
-
nil
|
79
|
-
end
|
80
|
-
|
81
|
-
# Redo the last action that was undone.
|
82
|
-
def redo
|
83
|
-
raise "Can't redo if there are no commands in the future" unless can_redo?
|
84
|
-
|
85
|
-
@last_done += 1
|
86
|
-
@actions[@last_done].do
|
87
|
-
|
88
|
-
nil
|
89
|
-
end
|
90
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Fidgit
|
4
|
+
# Manages a history of actions, along with doing, undoing and redoing those actions.
|
5
|
+
class History
|
6
|
+
# Maximum number of actions in the History before Actions are deleted.
|
7
|
+
DEFAULT_MAX_SIZE = 250
|
8
|
+
|
9
|
+
# An action in the History. Inherit actions from this in order to add them to a History.
|
10
|
+
class Action
|
11
|
+
# Perform the action.
|
12
|
+
def do; raise NotImplementedError, "#{self.class} does not have a do method defined"; end
|
13
|
+
|
14
|
+
# Reverse the action.
|
15
|
+
def undo; raise NotImplementedError, "#{self.class} does not have an undo method defined"; end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Is there an action that can be undone?
|
19
|
+
def can_undo?; @last_done >= 0; end
|
20
|
+
|
21
|
+
# Is there an action that has been undone that can now be redone?
|
22
|
+
def can_redo?; @last_done < (@actions.size - 1); end
|
23
|
+
|
24
|
+
def initialize(max_size = DEFAULT_MAX_SIZE)
|
25
|
+
@max_size = max_size
|
26
|
+
@actions = []
|
27
|
+
@last_done = -1 # Last command that was performed.
|
28
|
+
end
|
29
|
+
|
30
|
+
# Perform a History::Action, adding it to the history.
|
31
|
+
# If there are currently any actions that have been undone, they will be permanently lost and cannot be redone.
|
32
|
+
#
|
33
|
+
# @param [History::Action] action Action to be performed
|
34
|
+
def do(action)
|
35
|
+
raise ArgumentError, "Parameter, 'action', expected to be a #{Action}, but received: #{action}" unless action.is_a? Action
|
36
|
+
|
37
|
+
# Remove all undone actions when a new one is performed.
|
38
|
+
if can_redo?
|
39
|
+
if @last_done == -1
|
40
|
+
@actions.clear
|
41
|
+
else
|
42
|
+
@actions = @actions[0..@last_done]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# If history is too big, remove the oldest action.
|
47
|
+
if @actions.size >= @max_size
|
48
|
+
@actions.shift
|
49
|
+
end
|
50
|
+
|
51
|
+
@last_done = @actions.size
|
52
|
+
@actions << action
|
53
|
+
action.do
|
54
|
+
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
# Perform a History::Action, replacing the last action that was performed.
|
59
|
+
#
|
60
|
+
# @param [History::Action] action Action to be performed
|
61
|
+
def replace_last(action)
|
62
|
+
raise ArgumentError, "Parameter, 'action', expected to be a #{Action}, but received: #{action}" unless action.is_a? Action
|
63
|
+
|
64
|
+
@actions[@last_done].undo
|
65
|
+
@actions[@last_done] = action
|
66
|
+
action.do
|
67
|
+
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
|
71
|
+
# Undo the last action that was performed.
|
72
|
+
def undo
|
73
|
+
raise "Can't undo unless there are commands in past" unless can_undo?
|
74
|
+
|
75
|
+
@actions[@last_done].undo
|
76
|
+
@last_done -= 1
|
77
|
+
|
78
|
+
nil
|
79
|
+
end
|
80
|
+
|
81
|
+
# Redo the last action that was undone.
|
82
|
+
def redo
|
83
|
+
raise "Can't redo if there are no commands in the future" unless can_redo?
|
84
|
+
|
85
|
+
@last_done += 1
|
86
|
+
@actions[@last_done].do
|
87
|
+
|
88
|
+
nil
|
89
|
+
end
|
90
|
+
end
|
91
91
|
end
|