Skip to content

Commit 27a2fea

Browse files
authored
Refactor Selection Util (#409)
* Add Selection to ExpUtil * Convert modules to use new lib * Bug Fixes
1 parent 85c7cce commit 27a2fea

File tree

9 files changed

+316
-263
lines changed

9 files changed

+316
-263
lines changed

exp_legacy/module/modules/control/selection.lua

Lines changed: 0 additions & 194 deletions
This file was deleted.

exp_legacy/module/modules/gui/vlayer.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
1010
local format_number = require("util").format_number --- @dep util
1111
local config = require("modules.exp_legacy.config.vlayer") --- @dep config.vlayer
1212
local vlayer = require("modules.exp_legacy.modules.control.vlayer")
13-
local Selection = require("modules.exp_legacy.modules.control.selection") --- @dep modules.control.selection
14-
local SelectionConvertArea = "VlayerConvertChest"
13+
14+
local Selection = require("modules/exp_util/selection")
15+
local SelectArea = Selection.connect("ExpGui_VLayer")
1516

1617
--- Align an aabb to the grid by expanding it
1718
local function aabb_align_expand(aabb)
@@ -72,13 +73,15 @@ local function format_energy(amount, unit)
7273
return formatted .. " " .. suffix .. unit
7374
end
7475

76+
local ExpUtil = require("modules/exp_util")
7577
--- When an area is selected to add protection to the area
76-
Selection.on_selection(SelectionConvertArea, function(event)
78+
SelectArea:on_selection(function(event)
79+
log(ExpUtil.format_any(event))
7780
local area = aabb_align_expand(event.area)
7881
local player = game.players[event.player_index]
7982

8083
if not player then
81-
return nil
84+
return
8285
end
8386

8487
local container = Gui.get_left_element(vlayer_container, player)
@@ -91,22 +94,22 @@ Selection.on_selection(SelectionConvertArea, function(event)
9194
entities = event.surface.find_entities_filtered{ area = area, name = "constant-combinator", force = player.force }
9295
else
9396
player.print{ "vlayer.power-on-space-research", config.power_on_space_research.name, config.power_on_space_research.level }
94-
return nil
97+
return
9598
end
9699
else
97100
entities = event.surface.find_entities_filtered{ area = area, name = "steel-chest", force = player.force }
98101
end
99102

100103
if #entities == 0 then
101104
player.print{ "vlayer.steel-chest-detect" }
102-
return nil
105+
return
103106
elseif #entities > 1 then
104107
player.print{ "vlayer.result-unable", { "vlayer.control-type-" .. target:gsub("_", "-") }, { "vlayer.result-multiple" } }
105-
return nil
108+
return
106109
end
107110

108111
if not entities[1] then
109-
return nil
112+
return
110113
end
111114

112115
local e = entities[1]
@@ -115,20 +118,20 @@ Selection.on_selection(SelectionConvertArea, function(event)
115118

116119
if e.name and e.name == "steel-chest" and (not e.get_inventory(defines.inventory.chest).is_empty()) then
117120
player.print{ "vlayer.steel-chest-empty" }
118-
return nil
121+
return
119122
end
120123

121124
if (vlayer.get_interface_counts()[target] >= config.interface_limit[target]) then
122125
player.print{ "vlayer.result-unable", { "vlayer.control-type-" .. target:gsub("_", "-") }, { "vlayer.result-limit" } }
123-
return nil
126+
return
124127
end
125128

126129
e.destroy()
127130

128131
if target == "energy" then
129132
if not vlayer.create_energy_interface(event.surface, e_pos, player) then
130133
player.print{ "vlayer.result-unable", { "vlayer.control-type-energy" }, { "vlayer.result-space" } }
131-
return nil
134+
return
132135
end
133136
elseif target == "circuit" then
134137
vlayer.create_circuit_interface(event.surface, e_pos, e_circ, player)
@@ -400,11 +403,10 @@ local vlayer_gui_control_build = Gui.define("vlayer_gui_control_build")
400403
}:style{
401404
width = 200,
402405
}:on_click(function(def, player, element)
403-
if Selection.is_selecting(player, SelectionConvertArea) then
404-
Selection.stop(player)
406+
if SelectArea:stop(player) then
405407
player.print{ "vlayer.exit" }
406408
else
407-
Selection.start(player, SelectionConvertArea)
409+
SelectArea:start(player)
408410
player.print{ "vlayer.enter" }
409411
end
410412

exp_scenario/module/commands/artillery.lua

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Adds a command that helps shoot artillery
44

55
local AABB = require("modules/exp_util/aabb")
66
local Commands = require("modules/exp_commands")
7-
local Selection = require("modules.exp_legacy.modules.control.selection") --- @dep modules.control.selection
8-
local SelectionName = "ExpCommand_Artillery"
7+
8+
local Selection = require("modules/exp_util/selection")
9+
local SelectArea = Selection.connect("ExpCommand_Artillery")
910

1011
local floor = math.floor
1112
local abs = math.abs
@@ -37,18 +38,15 @@ end
3738
--- @overload fun(player: LuaPlayer)
3839
commands.artillery = Commands.new("artillery", { "exp-commands_artillery.description" })
3940
:register(function(player)
40-
if Selection.is_selecting(player, SelectionName) then
41-
Selection.stop(player)
41+
if SelectArea:stop(player) then
4242
return Commands.status.success{ "exp-commands_artillery.exit" }
43-
else
44-
Selection.start(player, SelectionName)
45-
return Commands.status.success{ "exp-commands_artillery.enter" }
4643
end
44+
SelectArea:start(player)
45+
return Commands.status.success{ "exp-commands_artillery.enter" }
4746
end) --[[ @as any ]]
4847

4948
--- when an area is selected to add protection to the area
50-
Selection.on_selection(SelectionName, function(event)
51-
--- @cast event EventData.on_player_selected_area
49+
SelectArea:on_selection(function(event)
5250
local area = AABB.expand(event.area)
5351
local player = game.players[event.player_index]
5452
local surface = event.surface

0 commit comments

Comments
 (0)