Class: Smagacor::UndoManager

Inherits:
Object
  • Object
show all
Includes:
Listener
Defined in:
lib/smagacor/util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Listener

#add_msg_listener, #del_msg_listener

Constructor Details

#initializeUndoManager

Returns a new instance of UndoManager.



122
123
124
125
126
127
128
# File 'lib/smagacor/util.rb', line 122

def initialize
  @history = []
  @index = -1
  add_msg_name( :possible_actions )
  add_msg_name( :undo )
  add_msg_name( :redo )
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



120
121
122
# File 'lib/smagacor/util.rb', line 120

def history
  @history
end

Instance Method Details

#add_undo_info(info) ⇒ Object



130
131
132
133
134
135
# File 'lib/smagacor/util.rb', line 130

def add_undo_info( info )
  @history[@index+1..-1] = []
  @history.push( info )
  @index += 1
  dispatch_msg( :possible_actions, possible_actions )
end

#clearObject



137
138
139
140
# File 'lib/smagacor/util.rb', line 137

def clear
  @history = []
  @index = -1
end

#redoObject



156
157
158
159
160
161
162
163
164
# File 'lib/smagacor/util.rb', line 156

def redo
  if @index < @history.length - 1
    @index += 1
    info = @history[@index]
    dispatch_msg( :possible_actions, possible_actions )
    dispatch_msg( :redo, info )
    info
  end
end

#redo_possible?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/smagacor/util.rb', line 166

def redo_possible?
  @history.length > 0 && @index < @history.length - 1
end

#undoObject



142
143
144
145
146
147
148
149
150
# File 'lib/smagacor/util.rb', line 142

def undo
  if @index > -1
    info = @history[@index]
    @index -= 1
    dispatch_msg( :possible_actions, possible_actions )
    dispatch_msg( :undo, info )
    info
  end
end

#undo_possible?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/smagacor/util.rb', line 152

def undo_possible?
  @history.length > 0 && @index > -1
end