Class: UI::Storage::SelectionHandler

Inherits:
Object
  • Object
show all
Defined in:
scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb

Overview

Class responsive of handling the selection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode_handler) ⇒ SelectionHandler

Create a new selection handler

Parameters:

  • mode_handler (ModeHandler)

    object responsive of handling the mode



16
17
18
19
20
21
22
23
24
25
26
27
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 16

def initialize(mode_handler)
  @mode_handler = mode_handler
  @box_selections = {}
  @battle_selections = {}
  @party_selection = []
  # @type [CursorHandler]
  @cursor = nil
  # @type [PFM::Storage]
  @storage = nil
  # @type [Array<PFM::Pokemon>]
  @party = nil
end

Instance Attribute Details

#cursor=(value) ⇒ CursorHandler (writeonly)

Set the current cursor handler

Returns:



10
11
12
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 10

def cursor=(value)
  @cursor = value
end

#party=(value) ⇒ Array<PFM::Pokemon> (writeonly)

Set the current party

Returns:



13
14
15
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 13

def party=(value)
  @party = value
end

#storage=(value) ⇒ PFM::Storage (writeonly)

Set the current storage object

Returns:



7
8
9
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 7

def storage=(value)
  @storage = value
end

Instance Method Details

#all_selected_pokemonArray<PFM::Pokemon>

Get all selected Pokemon

Returns:



151
152
153
154
155
156
157
158
159
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 151

def all_selected_pokemon
  selection = []
  process = proc { |box, i| selection << box[i] if box[i] }
  each_box_selection(&process)
  each_battle_selection(&process)
  each_party_selection(&process)

  return selection
end

#all_selected_pokemon_in_partyArray<PFM::Pokemon>

Get all selected Pokemon in party

Returns:



163
164
165
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 163

def all_selected_pokemon_in_party
  return @party_selection.map { |i| @party[i] }.compact
end

#box_selection_display=(box)

Define the box selection display

Parameters:



139
140
141
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 139

def box_selection_display=(box)
  @box = box
end

#clear

Clear the selection



131
132
133
134
135
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 131

def clear
  @box_selections.clear
  @battle_selections.clear
  @party_selection.clear
end

#empty?Boolean

Tell if all selection are empty

Returns:

  • (Boolean)


39
40
41
42
43
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 39

def empty?
  @box_selections.all? { |_, v| v.empty? } &&
    @battle_selections.all? { |_, v| v.empty? } &&
    @party_selection.empty?
end

#move_items_to_cursorBoolean

Move the current selection of items to the current cursor

Returns:

  • (Boolean)

    if the operation was a success



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 90

def move_items_to_cursor
  size = current_object_content_size
  return false if selection_size > size

  get_pokemon = current_object_getter
  index = @cursor.index

  process = proc do |box, i|
    next unless box[i]

    pokemon = get_pokemon.call(index)
    index = (index + 1) % size
    redo unless pokemon
    box[i].item_holding, pokemon.item_holding = pokemon.item_holding, box[i].item_holding
    box[i].form_calibrate
    pokemon.form_calibrate
  end

  each_box_selection(&process)
  each_battle_selection(&process)
  each_party_selection(&process)

  clear
  return true
end

#move_pokemon_to_cursorBoolean

Move the current selection of pokemon to the current cursor

Returns:

  • (Boolean)

    if the operation was a success



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 63

def move_pokemon_to_cursor
  size = current_object_size
  return false if selection_size > size

  set_pokemon = current_object_setter
  get_pokemon = current_object_getter
  index = @cursor.index

  process = proc do |box, i|
    pokemon = get_pokemon.call(index)
    pokemon_stored = set_pokemon.call(index, box[i])
    reset_form(pokemon_stored)
    index = (index + 1) % size
    box[i] = pokemon
  end

  each_box_selection(&process)
  each_battle_selection(&process)
  each_party_selection(&process)

  clear
  @party.compact!
  return true
end

#party_selection_display=(party_display)

Define the party selection display

Parameters:



145
146
147
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 145

def party_selection_display=(party_display)
  @party_display = party_display
end

#release_selected_pokemon

Release all selected Pokemon



117
118
119
120
121
122
123
124
125
126
127
128
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 117

def release_selected_pokemon
  process = proc do |box, i|
    box[i] = nil
  end

  each_box_selection(&process)
  each_battle_selection(&process)
  each_party_selection(&process)

  clear
  @party.compact!
end

#select

Select or deselect the current index



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 46

def select
  if @cursor.mode == :box
    arr = (@box_selections[@storage.current_box] ||= [])
  elsif @mode_handler.mode == :battle
    arr = (@battle_selections[@storage.current_battle_box] ||= [])
  else
    arr = @party_selection
  end
  if arr.include?(index = @cursor.index)
    arr.delete(index)
  else
    arr << index
  end
end

#update_selection

Update the current selection



30
31
32
33
34
35
# File 'scripts/01450 Systems/00200 Storage/00002 UI_Storage/00012 SelectionHandler.rb', line 30

def update_selection
  @box&.update_selection(@box_selections[@storage.current_box] || [])
  @party_display&.update_selection(
    @mode_handler.mode == :battle ? @battle_selections[@storage.current_battle_box] || [] : @party_selection
  )
end