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

updates to blueprint_helper

This commit is contained in:
grilledham 2018-05-20 16:29:05 +01:00
parent 594c6195f1
commit 0b0540f8c3

View File

@ -1,6 +1,7 @@
-- Soft mod version of Blueprint Flipper and Turner https://mods.factorio.com/mods/Marthen/Blueprint_Flip_Turn -- Soft mod version of Blueprint Flipper and Turner https://mods.factorio.com/mods/Marthen/Blueprint_Flip_Turn
local Event = require 'utils.event' local Event = require 'utils.event'
local Gui = require 'utils.gui'
local function getBlueprintCursorStack(player) local function getBlueprintCursorStack(player)
local cursor = player.cursor_stack local cursor = player.cursor_stack
@ -16,8 +17,8 @@ end
local function flip_v(player) local function flip_v(player)
local cursor = getBlueprintCursorStack(player) local cursor = getBlueprintCursorStack(player)
if cursor then if cursor then
if cursor.get_blueprint_entities() ~= nil then local ents = cursor.get_blueprint_entities()
local ents = cursor.get_blueprint_entities() if ents then
for i = 1, #ents do for i = 1, #ents do
local dir = ents[i].direction or 0 local dir = ents[i].direction or 0
if ents[i].name == 'curved-rail' then if ents[i].name == 'curved-rail' then
@ -76,8 +77,8 @@ end
local function flip_h(player) local function flip_h(player)
local cursor = getBlueprintCursorStack(player) local cursor = getBlueprintCursorStack(player)
if cursor then if cursor then
if cursor.get_blueprint_entities() ~= nil then local ents = cursor.get_blueprint_entities()
local ents = cursor.get_blueprint_entities() if ents then
for i = 1, #ents do for i = 1, #ents do
local dir = ents[i].direction or 0 local dir = ents[i].direction or 0
if ents[i].name == 'curved-rail' then if ents[i].name == 'curved-rail' then
@ -133,10 +134,59 @@ local function flip_h(player)
end end
end end
local main_button_name = 'blueprint_helper_main_button' local function valid_filter(entity_name)
local main_frame_name = 'blueprint_helper_main_frame' local prototype = game.entity_prototypes[entity_name]
local flip_h_button_name = 'blueprint_helper_flip_h_button'
local flip_v_button_name = 'blueprint_helper_flip_v_button' if not prototype then
return false
end
-- 'not-blueprintable' doesn't seem to work - grilledham 2018.05.20
return prototype.has_flag('player-creation') and not prototype.has_flag('placeable-off-grid')
end
local function convert(player, data)
local cursor = getBlueprintCursorStack(player)
if not cursor then
return
end
local entities = cursor.get_blueprint_entities()
if not entities then
return
end
local filters = {}
for _, filter in pairs(data) do
local from = filter.from.elem_value
local to = filter.to.elem_value
if from and to then
if valid_filter(from) and valid_filter(to) then
filters[from] = to
else
player.print('invalid filter: ' .. from .. ' => ' .. to)
end
end
end
for _, e in ipairs(entities) do
local to_name = filters[e.name]
if to_name then
e.name = to_name
end
end
cursor.set_blueprint_entities(entities)
end
-- Gui implementation.
local main_button_name = Gui.uid_name()
local main_frame_name = Gui.uid_name()
local flip_h_button_name = Gui.uid_name()
local flip_v_button_name = Gui.uid_name()
local convert_button_name = Gui.uid_name()
local function player_joined(event) local function player_joined(event)
local player = game.players[event.player_index] local player = game.players[event.player_index]
@ -148,21 +198,15 @@ local function player_joined(event)
return return
end end
local button = player.gui.top.add {name = main_button_name, type = 'sprite-button', sprite = 'item/blueprint'} player.gui.top.add {name = main_button_name, type = 'sprite-button', sprite = 'item/blueprint'}
button.style.font = 'default-bold'
button.style.minimal_height = 38
button.style.minimal_width = 38
button.style.top_padding = 2
button.style.left_padding = 4
button.style.right_padding = 4
button.style.bottom_padding = 2
end end
local function toggle_main_frame(player) local function toggle(event)
local left = player.gui.left local left = event.player.gui.left
local main_frame = left[main_frame_name] local main_frame = left[main_frame_name]
if main_frame and main_frame.valid then if main_frame and main_frame.valid then
Gui.remove_data_recursivly(main_frame)
main_frame.destroy() main_frame.destroy()
else else
main_frame = main_frame =
@ -172,47 +216,97 @@ local function toggle_main_frame(player)
direction = 'vertical', direction = 'vertical',
caption = 'Blueprint Helper' caption = 'Blueprint Helper'
} }
main_frame.add { local scroll_pane =
main_frame.add {type = 'scroll-pane', direction = 'vertical', vertical_scroll_policy = 'auto'}
scroll_pane.style.maximal_height = 500
-- Flipper.
local flipper_frame = scroll_pane.add {type = 'frame', caption = 'Flipper', direction = 'vertical'}
flipper_frame.add {
type = 'label', type = 'label',
caption = 'With blueprint in cursor click on button below to flip blueprint.' caption = 'With blueprint in cursor click on button below to flip blueprint.'
} }
main_frame.add { flipper_frame.add {
type = 'label', type = 'label',
caption = 'Obviously this wont work correctly with refineries or chemical plants.' caption = 'Obviously this wont work correctly with refineries or chemical plants.'
} }
main_frame.add { flipper_frame.add {
type = 'button', type = 'button',
name = flip_h_button_name, name = flip_h_button_name,
caption = 'Flip Horizontal' caption = 'Flip Horizontal'
} }
main_frame.add { flipper_frame.add {
type = 'button', type = 'button',
name = flip_v_button_name, name = flip_v_button_name,
caption = 'Flip Vertical' caption = 'Flip Vertical'
} }
-- Converter.
local filter_frame = scroll_pane.add {type = 'frame', caption = 'Entity Converter', direction = 'vertical'}
filter_frame.add {type = 'label', caption = 'Set filters then with blueprint in cursor click convert'}
local filter_table = filter_frame.add {type = 'table', column_count = 13}
local filters = {}
for _ = 1, 3 do
for _ = 1, 3 do
local filler = filter_table.add {type = 'label'}
filler.style.minimal_width = 16
local from_filter =
filter_table.add {
type = 'choose-elem-button',
elem_type = 'entity'
}
filter_table.add {type = 'label', caption = '=>'}
local to_filter =
filter_table.add {
type = 'choose-elem-button',
elem_type = 'entity'
}
table.insert(filters, {from = from_filter, to = to_filter})
end
local filler = filter_table.add {type = 'label'}
filler.style.minimal_width = 16
end
local filter_button = filter_frame.add {type = 'button', name = convert_button_name, caption = 'convert'}
Gui.set_data(filter_button, filters)
main_frame.add {type = 'button', name = main_button_name, caption = 'close'}
end end
end end
local function gui_click(event) Gui.on_click(main_button_name, toggle)
local element = event.element
if not element or not element.valid then
return
end
local player = game.players[event.player_index] Gui.on_click(
if not player or not player.valid then flip_h_button_name,
return function(event)
flip_h(event.player)
end end
)
local name = element.name Gui.on_click(
if name == main_button_name then flip_v_button_name,
toggle_main_frame(player) function(event)
elseif name == flip_h_button_name then flip_v(event.player)
flip_h(player)
elseif name == flip_v_button_name then
flip_v(player)
end end
end )
Gui.on_click(
convert_button_name,
function(event)
local data = Gui.get_data(event.element)
convert(event.player, data)
end
)
Event.add(defines.events.on_player_joined_game, player_joined) Event.add(defines.events.on_player_joined_game, player_joined)
Event.add(defines.events.on_gui_click, gui_click)