1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

blueprint helper updates

This commit is contained in:
grilledham 2019-06-20 11:25:24 +01:00
parent 4481e86940
commit 5ef00fe40f
2 changed files with 246 additions and 200 deletions

View File

@ -7,11 +7,9 @@ local Gui = require 'utils.gui'
local player_filters = {} local player_filters = {}
Global.register( Global.register(
{ player_filters,
player_filters = player_filters
},
function(tbl) function(tbl)
player_filters = tbl.player_filters player_filters = tbl
end end
) )
@ -27,124 +25,166 @@ local function getBlueprintCursorStack(player)
end end
local function flip_v(cursor) local function flip_v(cursor)
local ents = cursor.get_blueprint_entities() local entities = cursor.get_blueprint_entities()
if ents then if entities ~= nil then
for i = 1, #ents do for i = 1, #entities do
local dir = ents[i].direction or 0 local entity = entities[i]
if ents[i].name == 'curved-rail' then local dir = entity.direction or 0
ents[i].direction = (13 - dir) % 8
elseif ents[i].name == 'storage-tank' then if entity.name == 'curved-rail' then
if ents[i].direction == 2 or ents[i].direction == 6 then entity.direction = (13 - dir) % 8
ents[i].direction = 4 elseif entity.name == 'storage-tank' then
if entity.direction == 2 or entity.direction == 6 then
entity.direction = 4
else else
ents[i].direction = 2 entity.direction = 2
end end
elseif ents[i].name == 'rail-signal' or ents[i].name == 'rail-chain-signal' then elseif entity.name == 'rail-signal' or entity.name == 'rail-chain-signal' then
if dir == 1 then if dir == 1 then
ents[i].direction = 7 entity.direction = 7
elseif dir == 2 then elseif dir == 2 then
ents[i].direction = 6 entity.direction = 6
elseif dir == 3 then elseif dir == 3 then
ents[i].direction = 5 entity.direction = 5
elseif dir == 5 then elseif dir == 5 then
ents[i].direction = 3 entity.direction = 3
elseif dir == 6 then elseif dir == 6 then
ents[i].direction = 2 entity.direction = 2
elseif dir == 7 then elseif dir == 7 then
ents[i].direction = 1 entity.direction = 1
end end
elseif ents[i].name == 'train-stop' then elseif entity.name == 'train-stop' then
if dir == 2 then if dir == 2 then
ents[i].direction = 6 entity.direction = 6
elseif dir == 6 then elseif dir == 6 then
ents[i].direction = 2 entity.direction = 2
end end
else else
ents[i].direction = (12 - dir) % 8 entity.direction = (12 - dir) % 8
end end
ents[i].position.y = -ents[i].position.y
if ents[i].drop_position then entity.position.y = -entity.position.y
ents[i].drop_position.y = -ents[i].drop_position.y
if entity.drop_position then
entity.drop_position.y = -entity.drop_position.y
end end
if ents[i].pickup_position then if entity.pickup_position then
ents[i].pickup_position.y = -ents[i].pickup_position.y entity.pickup_position.y = -entity.pickup_position.y
end
local input_priority = entity.input_priority
if input_priority == 'left' then
entity.input_priority = 'right'
elseif input_priority == 'right' then
entity.input_priority = 'left'
end
local output_priority = entity.output_priority
if output_priority == 'left' then
entity.output_priority = 'right'
elseif output_priority == 'right' then
entity.output_priority = 'left'
end end
end end
cursor.set_blueprint_entities(ents)
cursor.set_blueprint_entities(entities)
end end
if cursor.get_blueprint_tiles() ~= nil then
ents = cursor.get_blueprint_tiles() local tiles = cursor.get_blueprint_tiles()
for i = 1, #ents do if tiles ~= nil then
local dir = ents[i].direction or 0 for i = 1, #tiles do
ents[i].direction = (12 - dir) % 8 local tile = tiles[i]
ents[i].position.y = -ents[i].position.y local dir = tile.direction or 0
tile.direction = (12 - dir) % 8
tile.position.y = -tile.position.y
end end
cursor.set_blueprint_tiles(ents)
cursor.set_blueprint_tiles(entities)
end end
end end
local function flip_h(cursor) local function flip_h(cursor)
local ents = cursor.get_blueprint_entities() local entities = cursor.get_blueprint_entities()
if ents then if entities ~= nil then
for i = 1, #ents do for i = 1, #entities do
local dir = ents[i].direction or 0 local entity = entities[i]
if ents[i].name == 'curved-rail' then local dir = entity.direction or 0
ents[i].direction = (9 - dir) % 8
elseif ents[i].name == 'storage-tank' then if entity.name == 'curved-rail' then
if ents[i].direction == 2 or ents[i].direction == 6 then entity.direction = (9 - dir) % 8
ents[i].direction = 4 elseif entity.name == 'storage-tank' then
if entity.direction == 2 or entity.direction == 6 then
entity.direction = 4
else else
ents[i].direction = 2 entity.direction = 2
end end
elseif ents[i].name == 'rail-signal' or ents[i].name == 'rail-chain-signal' then elseif entity.name == 'rail-signal' or entity.name == 'rail-chain-signal' then
if dir == 0 then if dir == 0 then
ents[i].direction = 4 entity.direction = 4
elseif dir == 1 then elseif dir == 1 then
ents[i].direction = 3 entity.direction = 3
elseif dir == 3 then elseif dir == 3 then
ents[i].direction = 1 entity.direction = 1
elseif dir == 4 then elseif dir == 4 then
ents[i].direction = 0 entity.direction = 0
elseif dir == 5 then elseif dir == 5 then
ents[i].direction = 7 entity.direction = 7
elseif dir == 7 then elseif dir == 7 then
ents[i].direction = 5 entity.direction = 5
end end
elseif ents[i].name == 'train-stop' then elseif entity.name == 'train-stop' then
if dir == 0 then if dir == 0 then
ents[i].direction = 4 entity.direction = 4
elseif dir == 4 then elseif dir == 4 then
ents[i].direction = 0 entity.direction = 0
end end
else else
ents[i].direction = (16 - dir) % 8 entity.direction = (16 - dir) % 8
end end
ents[i].position.x = -ents[i].position.x
if ents[i].drop_position then entity.position.x = -entity.position.x
ents[i].drop_position.x = -ents[i].drop_position.x
if entity.drop_position then
entity.drop_position.x = -entity.drop_position.x
end end
if ents[i].pickup_position then if entity.pickup_position then
ents[i].pickup_position.x = -ents[i].pickup_position.x entity.pickup_position.x = -entity.pickup_position.x
end
local input_priority = entity.input_priority
if input_priority == 'left' then
entity.input_priority = 'right'
elseif input_priority == 'right' then
entity.input_priority = 'left'
end
local output_priority = entity.output_priority
if output_priority == 'left' then
entity.output_priority = 'right'
elseif output_priority == 'right' then
entity.output_priority = 'left'
end end
end end
cursor.set_blueprint_entities(ents)
cursor.set_blueprint_entities(entities)
end end
if cursor.get_blueprint_tiles() ~= nil then
ents = cursor.get_blueprint_tiles() local tiles = cursor.get_blueprint_tiles()
for i = 1, #ents do if tiles ~= nil then
local dir = ents[i].direction or 0 for i = 1, #tiles do
ents[i].direction = (16 - dir) % 8 local tile = tiles[i]
ents[i].position.x = -ents[i].position.x local dir = tile.direction or 0
tile.direction = (16 - dir) % 8
tile.position.x = -tile.position.x
end end
cursor.set_blueprint_tiles(ents)
cursor.set_blueprint_tiles(tiles)
end end
end end
local function build_filters(data) local function build_filters(data)
local filters = {} local filters = {}
for _, filter in pairs(data) do for _, filter in pairs(data) do
local from = filter.from.tooltip local from = filter.from.parent.caption
local to = filter.to.tooltip local to = filter.to.parent.caption
if from ~= '' and to ~= '' then if from ~= '' and to ~= '' then
filters[from] = to filters[from] = to
@ -170,88 +210,51 @@ local function convert(cursor, filters)
cursor.set_blueprint_entities(entities) cursor.set_blueprint_entities(entities)
end end
local valid_filters = { local filter_blacklist = {
'wooden-chest', ['escape-pod-assembler'] = true,
'iron-chest', ['escape-pod-lab'] = true,
'steel-chest', ['infinity-chest'] = true,
'storage-tank', ['simple-entity-with-force'] = true,
'transport-belt', ['simple-entity-with-owner'] = true,
'fast-transport-belt', ['electric-energy-interface'] = true,
'express-transport-belt', ['heat-interface'] = true,
'underground-belt', ['infinity-pipe'] = true,
'fast-underground-belt', ['player-port'] = true,
'express-underground-belt', ['escape-pod-power'] = true,
'splitter', ['bait-chest'] = true,
'fast-splitter', ['cutscene-gun-turret'] = true,
'express-splitter', ['blue-chest'] = true,
'loader', ['market'] = true,
'fast-loader', ['red-chest'] = true
'express-loader',
'burner-inserter',
'inserter',
'long-handed-inserter',
'fast-inserter',
'filter-inserter',
'stack-inserter',
'stack-filter-inserter',
'small-electric-pole',
'medium-electric-pole',
'big-electric-pole',
'substation',
'pipe',
'pipe-to-ground',
'pump',
'curved-rail',
'straight-rail',
'train-stop',
'rail-signal',
'rail-chain-signal',
'logistic-chest-active-provider',
'logistic-chest-passive-provider',
'logistic-chest-storage',
'logistic-chest-buffer',
'logistic-chest-requester',
'roboport',
'small-lamp',
'arithmetic-combinator',
'decider-combinator',
'constant-combinator',
'power-switch',
'programmable-speaker',
'boiler',
'steam-engine',
'steam-turbine',
'solar-panel',
'accumulator',
'nuclear-reactor',
'heat-exchanger',
'heat-pipe',
'burner-mining-drill',
'electric-mining-drill',
'offshore-pump',
'pumpjack',
'stone-furnace',
'steel-furnace',
'electric-furnace',
'assembling-machine-1',
'assembling-machine-2',
'assembling-machine-3',
'oil-refinery',
'chemical-plant',
'centrifuge',
'lab',
'beacon',
'stone-wall',
'gate',
'gun-turret',
'laser-turret',
'flamethrower-turret',
'artillery-turret',
'radar',
'rocket-silo'
} }
local cached_valid_filters = nil
local function build_valid_filters()
local filters = {}
local count = 0
for name, data in pairs(game.entity_prototypes) do
local has_flag = data.has_flag
if has_flag('player-creation') and not has_flag('placeable-off-grid') and not filter_blacklist[name] then
count = count + 1
filters[count] = name
end
end
return filters
end
local function get_valid_filters()
if cached_valid_filters == nil then
cached_valid_filters = build_valid_filters()
end
return cached_valid_filters
end
-- Gui implementation. -- Gui implementation.
local minimal_width = 400
local main_button_name = Gui.uid_name() local main_button_name = Gui.uid_name()
local main_frame_name = Gui.uid_name() local main_frame_name = Gui.uid_name()
@ -276,12 +279,14 @@ local function player_joined(event)
return return
end end
player.gui.top.add({ player.gui.top.add(
name = main_button_name, {
type = 'sprite-button', name = main_button_name,
sprite = 'item/blueprint', type = 'sprite-button',
tooltip = {'blueprint_helper.tooltip'} sprite = 'item/blueprint',
}) tooltip = {'blueprint_helper.tooltip'}
}
)
end end
local function draw_filters_table(event) local function draw_filters_table(event)
@ -297,19 +302,28 @@ local function draw_filters_table(event)
t.style.horizontal_spacing = 0 t.style.horizontal_spacing = 0
t.style.vertical_spacing = 0 t.style.vertical_spacing = 0
for _, v in ipairs(valid_filters) do local prototypes = game.entity_prototypes
local flow = t.add {type = 'flow'}
local b = flow.add {type = 'sprite-button', name = filter_element_name, sprite = 'entity/' .. v, tooltip = v} for _, v in ipairs(get_valid_filters()) do
local flow = t.add {type = 'flow', caption = v}
local b =
flow.add {
type = 'sprite-button',
name = filter_element_name,
sprite = 'entity/' .. v,
tooltip = prototypes[v].localised_name or v
}
Gui.set_data(b, frame) Gui.set_data(b, frame)
b.style = 'slot_button' b.style = 'slot_button'
end end
local flow = frame.add {type = 'flow'} local flow = frame.add {type = 'flow'}
local close = flow.add {type = 'button', name = filter_table_close_button_name, caption = 'Close'} local close = flow.add {type = 'button', name = filter_table_close_button_name, caption = {'common.close_button'}}
Gui.set_data(close, frame) Gui.set_data(close, frame)
local clear = flow.add {type = 'button', name = filter_table_clear_name, caption = 'Clear Filter'} local clear =
flow.add {type = 'button', name = filter_table_clear_name, caption = {'blueprint_helper.clear_filters'}}
Gui.set_data(clear, frame) Gui.set_data(clear, frame)
event.player.opened = frame event.player.opened = frame
@ -335,12 +349,11 @@ local function toggle(event)
local filters = Gui.get_data(main_frame) local filters = Gui.get_data(main_frame)
for i, f in pairs(filters) do for i, f in pairs(filters) do
p_filters[i].from = f.from.tooltip p_filters[i].from = f.from.parent.caption
p_filters[i].to = f.to.tooltip p_filters[i].to = f.to.parent.caption
end end
Gui.remove_data_recursively(main_frame) Gui.destroy(main_frame)
main_frame.destroy()
if player.opened_gui_type == defines.gui_type.custom then if player.opened_gui_type == defines.gui_type.custom then
local opened = player.opened local opened = player.opened
@ -355,7 +368,7 @@ local function toggle(event)
type = 'frame', type = 'frame',
name = main_frame_name, name = main_frame_name,
direction = 'vertical', direction = 'vertical',
caption = 'Blueprint Helper' caption = {'blueprint_helper.tooltip'}
} }
local scroll_pane = local scroll_pane =
main_frame.add {type = 'scroll-pane', direction = 'vertical', vertical_scroll_policy = 'auto'} main_frame.add {type = 'scroll-pane', direction = 'vertical', vertical_scroll_policy = 'auto'}
@ -363,14 +376,14 @@ local function toggle(event)
-- Flipper. -- Flipper.
local flipper_frame = scroll_pane.add {type = 'frame', caption = 'Flipper', direction = 'vertical'} local flipper_frame =
scroll_pane.add {type = 'frame', caption = {'blueprint_helper.flipper_caption'}, direction = 'vertical'}
flipper_frame.style.minimal_width = minimal_width
local label = local label =
flipper_frame.add { flipper_frame.add {
type = 'label', type = 'label',
caption = [[ caption = {'blueprint_helper.flipper_label'}
Place blueprint on buttons below to flip blueprint.
Obviously this wont work correctly with refineries or chemical plants.]]
} }
label.style.single_line = false label.style.single_line = false
@ -379,52 +392,67 @@ Obviously this wont work correctly with refineries or chemical plants.]]
flow.add { flow.add {
type = 'button', type = 'button',
name = flip_h_button_name, name = flip_h_button_name,
caption = 'Flip Horizontal ⇄' caption = {'blueprint_helper.flip_horizontal'}
} }
flow.add { flow.add {
type = 'button', type = 'button',
name = flip_v_button_name, name = flip_v_button_name,
caption = 'Flip Vertical ⇵' caption = {'blueprint_helper.flip_vertical'}
} }
-- Converter. -- Converter.
local filter_frame = scroll_pane.add {type = 'frame', caption = 'Entity Converter', direction = 'vertical'} local filter_frame =
scroll_pane.add {
type = 'frame',
caption = {'blueprint_helper.entity_converter_caption'},
direction = 'vertical'
}
filter_frame.style.minimal_width = minimal_width
filter_frame.add { filter_frame.add {
type = 'label', type = 'label',
-- The empty space is a hacky way to line this frame up with the above frame. caption = {'blueprint_helper.entity_converter_label'}
caption = 'Set filters then place blueprint on convert button to apply filters. '
} }
local filter_table = filter_frame.add {type = 'table', column_count = 12} local filter_table = filter_frame.add {type = 'table', column_count = 12}
local filters = {} local filters = {}
local prototypes = game.entity_prototypes
for i = 1, 9 do for i = 1, 9 do
local filler = filter_table.add {type = 'label'} local filler = filter_table.add {type = 'label'}
filler.style.minimal_width = 16 filler.style.minimal_width = 16
local from_tooltip = p_filters[i].from local from = p_filters[i].from
local to_tooltip = p_filters[i].to local to = p_filters[i].to
local from_tooltip, to_tooltip
if from ~= '' then
from_tooltip = prototypes[from].localised_name or from
end
if to ~= '' then
to_tooltip = prototypes[to].localised_name or to
end
local from_filter = local from_filter =
filter_table.add({type = 'flow'}).add { filter_table.add({type = 'flow', caption = from}).add {
type = 'sprite-button', type = 'sprite-button',
name = filter_button_name, name = filter_button_name,
tooltip = from_tooltip, tooltip = from_tooltip,
sprite = from_tooltip ~= '' and 'entity/' .. from_tooltip or nil sprite = from ~= '' and 'entity/' .. from or nil
} }
from_filter.style = 'slot_button' from_filter.style = 'slot_button'
filter_table.add {type = 'label', caption = ''} filter_table.add {type = 'label', caption = ''}
local to_filter = local to_filter =
filter_table.add({type = 'flow'}).add { filter_table.add({type = 'flow', caption = to}).add {
type = 'sprite-button', type = 'sprite-button',
name = filter_button_name, name = filter_button_name,
tooltip = to_tooltip, tooltip = to_tooltip,
sprite = to_tooltip ~= '' and 'entity/' .. to_tooltip or nil sprite = to ~= '' and 'entity/' .. to or nil
} }
to_filter.style = 'slot_button' to_filter.style = 'slot_button'
@ -434,14 +462,22 @@ Obviously this wont work correctly with refineries or chemical plants.]]
local converter_buttons_flow = filter_frame.add {type = 'flow'} local converter_buttons_flow = filter_frame.add {type = 'flow'}
local clear_button = local clear_button =
converter_buttons_flow.add {type = 'button', name = clear_all_filters_name, caption = 'Clear Filters'} converter_buttons_flow.add {
type = 'button',
name = clear_all_filters_name,
caption = {'blueprint_helper.clear_filters'}
}
Gui.set_data(clear_button, filters) Gui.set_data(clear_button, filters)
local filter_button = local filter_button =
converter_buttons_flow.add {type = 'button', name = convert_button_name, caption = 'Convert'} converter_buttons_flow.add {
type = 'button',
name = convert_button_name,
caption = {'blueprint_helper.convert'}
}
Gui.set_data(filter_button, filters) Gui.set_data(filter_button, filters)
main_frame.add {type = 'button', name = main_button_name, caption = 'Close'} main_frame.add {type = 'button', name = main_button_name, caption = {'common.close_button'}}
Gui.set_data(main_frame, filters) Gui.set_data(main_frame, filters)
end end
end end
@ -457,7 +493,7 @@ Gui.on_click(
if cursor then if cursor then
flip_h(cursor) flip_h(cursor)
else else
player.print('Click the button with a blueprint or blueprint book.') player.print({'blueprint_helper.empty_cursor_error_message'})
end end
end end
) )
@ -471,7 +507,7 @@ Gui.on_click(
if cursor then if cursor then
flip_v(cursor) flip_v(cursor)
else else
player.print('Click the button with a blueprint or blueprint book.') player.print({'blueprint_helper.empty_cursor_error_message'})
end end
end end
) )
@ -483,6 +519,7 @@ Gui.on_click(
local element = event.element local element = event.element
element.sprite = 'utility/pump_cannot_connect_icon' element.sprite = 'utility/pump_cannot_connect_icon'
element.tooltip = '' element.tooltip = ''
element.parent.caption = ''
else else
draw_filters_table(event) draw_filters_table(event)
end end
@ -499,10 +536,10 @@ Gui.on_click(
if filter_button and filter_button.valid then if filter_button and filter_button.valid then
filter_button.sprite = element.sprite filter_button.sprite = element.sprite
filter_button.tooltip = element.tooltip filter_button.tooltip = element.tooltip
filter_button.parent.caption = element.parent.caption
end end
Gui.remove_data_recursively(frame) Gui.destroy(frame)
frame.destroy()
end end
) )
@ -510,9 +547,7 @@ Gui.on_click(
filter_table_close_button_name, filter_table_close_button_name,
function(event) function(event)
local frame = Gui.get_data(event.element) local frame = Gui.get_data(event.element)
Gui.destroy(frame)
Gui.remove_data_recursively(frame)
frame.destroy()
end end
) )
@ -524,9 +559,9 @@ Gui.on_click(
filter_button.sprite = 'utility/pump_cannot_connect_icon' filter_button.sprite = 'utility/pump_cannot_connect_icon'
filter_button.tooltip = '' filter_button.tooltip = ''
filter_button.parent.caption = ''
Gui.remove_data_recursively(frame) Gui.destroy(frame)
frame.destroy()
end end
) )
@ -541,8 +576,10 @@ Gui.on_click(
from.sprite = 'utility/pump_cannot_connect_icon' from.sprite = 'utility/pump_cannot_connect_icon'
from.tooltip = '' from.tooltip = ''
from.parent.caption = ''
to.sprite = 'utility/pump_cannot_connect_icon' to.sprite = 'utility/pump_cannot_connect_icon'
to.tooltip = '' to.tooltip = ''
to.parent.caption = ''
end end
end end
) )
@ -554,7 +591,7 @@ Gui.on_click(
local cursor = getBlueprintCursorStack(player) local cursor = getBlueprintCursorStack(player)
if not cursor then if not cursor then
player.print('Click the button with a blueprint or blueprint book.') player.print({'blueprint_helper.empty_cursor_error_message'})
return return
end end
@ -562,7 +599,7 @@ Gui.on_click(
local filters = build_filters(data) local filters = build_filters(data)
if next(filters) == nil then if next(filters) == nil then
player.print('No filters have been set') player.print({'blueprint_helper.no_filters_error_message'})
end end
convert(cursor, filters) convert(cursor, filters)
@ -573,8 +610,7 @@ Gui.on_custom_close(
filters_table_name, filters_table_name,
function(event) function(event)
local element = event.element local element = event.element
Gui.remove_data_recursively(element) Gui.destroy(element)
element.destroy()
end end
) )

View File

@ -79,6 +79,16 @@ tooltip=Alien evolution progress
[blueprint_helper] [blueprint_helper]
tooltip=Blueprint helper tooltip=Blueprint helper
flipper_caption=Flipper
flipper_label=Place blueprint on buttons below to flip blueprint.\nObviously this wont work correctly with refineries or chemical plants.
flip_horizontal=Flip horizontal ⇄
flip_vertical=Flip Vertical ⇵
entity_converter_caption=Entity Converter
entity_converter_label=Set filters then place blueprint on convert button to apply filters.
clear_filters=Clear Filters
convert=Convert
empty_cursor_error_message=Click the button with a blueprint or blueprint book.
no_filters_error_message=No filters have been set.
[paint] [paint]
tooltip=Landscape painting tool tooltip=Landscape painting tool