z_mirror/BlueprintTools

git clone https://git.raiguard.me/z_mirror/BlueprintTools

raiguardClipboards experiment2d9f200

clipboards
5.7 KiB183 linesraw
1local event = require("__flib__.event")
2local gui = require("__flib__.gui-beta")
3local migration = require("__flib__.migration")
4
5local constants = require("constants")
6
7local buttons_gui = require("scripts.gui.buttons")
8local global_data = require("scripts.global-data")
9local migrations = require("scripts.migrations")
10local player_data = require("scripts.player-data")
11local util = require("scripts.util")
12
13local quick_grid = require("scripts.processors.quick-grid")
14local set_tiles_gui = require("scripts.gui.set-tiles")
15local swap_wire_colors = require("scripts.processors.swap-wire-colors")
16
17-- -----------------------------------------------------------------------------
18-- EVENT HANDLERS
19
20-- BOOTSTRAP
21
22event.on_init(function()
23  global_data.init()
24
25  for i, player in pairs(game.players) do
26    player_data.init(i)
27    player_data.refresh(player, global.players[i])
28  end
29end)
30
31event.on_load(function()
32
33end)
34
35event.on_configuration_changed(function(e)
36  if migration.on_config_changed(e, migrations) then
37    for i, player in pairs(game.players) do
38      local player_table = global.players[i]
39      player_data.refresh(player, player_table)
40    end
41  end
42end)
43
44-- CUSTOM INPUTS
45
46event.register("bpt-swap-wire-colors", function(e)
47  swap_wire_colors(game.get_player(e.player_index))
48end)
49
50event.register("bpt-set-tiles", function(e)
51  local player = game.get_player(e.player_index)
52  local player_table = global.players[e.player_index]
53  local cursor_stack = player.cursor_stack
54  if cursor_stack and cursor_stack.valid_for_read then
55    local blueprint = util.get_blueprint(cursor_stack)
56    if blueprint and blueprint.is_blueprint_setup() then
57      set_tiles_gui.build(player, player_table)
58    end
59  end
60end)
61
62event.register("bpt-quick-grid", function(e)
63  quick_grid(game.get_player(e.player_index))
64end)
65
66event.register("bpt-configure", function(e)
67  local player = game.get_player(e.player_index)
68  local cursor_stack = player.cursor_stack
69  if cursor_stack and cursor_stack.valid_for_read then
70    local blueprint = util.get_blueprint(cursor_stack)
71    if
72      (blueprint and blueprint.is_blueprint_setup())
73      or (cursor_stack.is_upgrade_item or cursor_stack.is_deconstruction_item)
74    then
75      player.opened = cursor_stack
76    end
77  end
78end)
79
80event.register({"bpt-activate-deconstruction-clipboard", "bpt-activate-upgrade-clipboard"}, function(e)
81  local _, _, type = string.find(e.input_name, "bpt%-activate%-(.*)%-clipboard")
82
83  local player = game.get_player(e.player_index)
84  local player_table = global.players[e.player_index]
85
86  local cursor_stack = player.cursor_stack
87  if cursor_stack then
88    local clipboard_book = player_table.clipboards[constants.clipboard_indices[type]]
89    local clipboard_inventory = clipboard_book.get_inventory(defines.inventory.item_main)
90    if not clipboard_inventory.is_empty() and player.clear_cursor() then
91      cursor_stack.set_stack(clipboard_book)
92    end
93  end
94end)
95
96event.register("bpt-linked-confirm-gui", function(e)
97  local player = game.get_player(e.player_index)
98  local player_table = global.players[e.player_index]
99  local gui_data = player_table.guis.set_tiles
100  if gui_data and gui_data.refs.confirm_button.enabled then
101    set_tiles_gui.handle_action(e, {action = "confirm"})
102    player.play_sound{path = "utility/confirm"}
103  end
104end)
105
106-- event.register({"bpt-linked-cycle-blueprint-backwards", "bpt-linked-cycle-blueprint-forwards"}, function(e)
107--   local direction = e.input_name == "bpt-linked-cycle-blueprint-backwards" and -1 or 1
108-- end)
109
110event.register("bpt-linked-clear-cursor", function(e)
111end)
112
113-- GUI
114
115gui.hook_events(function(e)
116  local player = game.get_player(e.player_index)
117  local player_table = global.players[e.player_index]
118  local action = gui.read_action(e)
119
120  if action then
121    if action.gui == "buttons" then
122      if action.action == "swap_wire_colors" then
123        swap_wire_colors(player)
124      elseif action.action == "set_tiles" then
125        set_tiles_gui.build(player, player_table)
126      elseif action.action == "quick_grid" then
127        quick_grid(player)
128      elseif action.action == "configure" then
129        player.opened = player.cursor_stack
130      end
131    elseif action.gui == "set_tiles" then
132      set_tiles_gui.handle_action(e, action)
133    end
134  end
135end)
136
137-- PLAYER
138
139event.on_player_created(function(e)
140  player_data.init(e.player_index)
141  player_data.refresh(game.get_player(e.player_index), global.players[e.player_index])
142end)
143
144event.on_player_removed(function(e)
145  global.players[e.player_index] = nil
146end)
147
148event.on_player_cursor_stack_changed(function(e)
149  local player = game.get_player(e.player_index)
150  local player_table = global.players[e.player_index]
151  local cursor_stack = player.cursor_stack
152
153  local blueprint = util.get_blueprint(cursor_stack)
154  local blueprint_buttons_shown = player_table.flags.blueprint_buttons_shown
155
156  if blueprint then
157    local is_setup = blueprint.is_blueprint_setup()
158    if is_setup and not blueprint_buttons_shown then
159      buttons_gui.show(player_table, "blueprint")
160    elseif not is_setup and blueprint_buttons_shown then
161      buttons_gui.hide(player_table, "blueprint")
162    end
163  elseif blueprint_buttons_shown then
164    buttons_gui.hide(player_table, "blueprint")
165  end
166
167  if cursor_stack.is_upgrade_item then
168    buttons_gui.show(player_table, "upgrade_planner")
169  elseif player_table.flags.upgrade_planner_buttons_shown then
170    buttons_gui.hide(player_table, "upgrade_planner")
171  end
172
173  if cursor_stack.is_deconstruction_item then
174    buttons_gui.show(player_table, "deconstruction_planner")
175  elseif player_table.flags.deconstruction_planner_buttons_shown then
176    buttons_gui.hide(player_table, "deconstruction_planner")
177  end
178
179  -- if the set tiles GUI is open and the cursor stack changes in any way, close it
180  if player_table.guis.set_tiles then
181    set_tiles_gui.destroy(player_table)
182  end
183end)