diff --git a/PHI-CL/control.lua b/PHI-CL/control.lua index 44958a8..c4d3567 100644 --- a/PHI-CL/control.lua +++ b/PHI-CL/control.lua @@ -78,7 +78,7 @@ if settings.startup['PHI-CT'].value then end local frame = player.gui.relative.add({type = 'frame', name = 'inserter_config', anchor = {gui = defines.relative_gui_type.inserter_gui, position = defines.relative_gui_position.right}}) - -- frame.add({type = 'label', name = 'test', caption = '1', style = 'heading_2_label'}) + frame.add({type = 'label', name = 'label_info', caption = 'Inserter direction', style = 'heading_2_label'}) frame.add({type = 'drop-down', name = 'direction', items = {'virtual-signal/down-arrow', 'virtual-signal/left-arrow', 'virtual-signal/up-arrow', 'virtual-signal/right-arrow'}, selected_index = 1}) frame.add({type = 'drop-down', name = 'sub-direction', items = {'virtual-signal/signal-0', 'virtual-signal/signal-1', 'virtual-signal/signal-2', 'virtual-signal/signal-3'}, selected_index = 1}) end @@ -111,7 +111,6 @@ if settings.startup['PHI-CT'].value then end end) - --[[ script.on_configuration_changed(function(_) for _, player in pairs(game.players) do gui_create(player) @@ -142,9 +141,13 @@ if settings.startup['PHI-CT'].value then end end) - script.on_event(defines.events.on_gui_click, function(e) + script.on_event(defines.events.on_gui_selection_state_changed, function(e) local player = game.players[e.player_index] local gui = player.gui.relative.inserter_config + + if gui[e.element.name] and e.element.entity and (e.element.entity.type == 'inserter' or (e.element.entity.type == 'entity-ghost' and e.element.entity.ghost_type == 'inserter')) then + e.element.entity.direction = (e.element.parent.direction.selected_index - 1) * 4 + (e.element.parent.sub_direction.selected_index - 1) + end end) script.on_event(defines.events.on_player_rotated_entity, function(e) @@ -180,186 +183,4 @@ if settings.startup['PHI-CT'].value then end end end) - - local gui = {} - local inserter_utils = {} - math2d.direction = {vectors = {{x = 0, y = -1}, {x = 1, y = -1}, {x = 1, y = 0}, {x = 1, y = 1}, {x = 0, y = 1}, {x = -1, y = 1}, {x = -1, y = 0}, {x = -1, y = -1}}} - - function inserter_utils.get_arm_positions(inserter) - local x_int, x_frac, y_int, y_frac = math.modf((inserter.position.x and inserter.position.x) or inserter.position[1]), math.modf((inserter.position.y and inserter.position.y) or inserter.position[2]) - local base_tile = {x = x_frac < 0 and (x_int - 1) or x_int, y = y_frac < 0 and (y_int - 1) or y_int} - local pos2 = (inserter.drop_position.x and {x = inserter.drop_position.x, y = inserter.drop_position.y}) or {x = inserter.drop_position[1], y = inserter.drop_position[2]} - local x_int2, x_frac2, y_int2, y_frac2 = math.modf(pos2.x), math.modf(pos2.y) - local pos3 = (inserter.pickup_position.x and {x = inserter.pickup_position.x, y = inserter.pickup_position.y}) or {x = inserter.pickup_position[1], y = inserter.pickup_position[2]} - local x_int3, x_frac3, y_int3, y_frac3 = math.modf(pos3.x), math.modf(pos3.y) - - return { - base = base_tile, - base_offset = {x = x_frac < 0 and (x_frac + 1) or x_frac, y = y_frac < 0 and (y_frac + 1) or y_frac}, - drop = math2d.position.subtract({x = x_frac2 < 0 and (x_int2 - 1) or x_int2, y = y_frac2 < 0 and (y_int2 - 1) or y_int2}, base_tile), - drop_offset = {x = (((x_frac2 < 0 and (x_frac2 + 1) or x_frac2) > 0.5 and 1) or ((x_frac2 < 0 and (x_frac2 + 1) or x_frac2) < 0.5 and -1)) or 0, y = (((y_frac2 < 0 and (y_frac2 + 1) or y_frac2) > 0.5 and 1) or ((y_frac2 < 0 and (y_frac2 + 1) or y_frac2) < 0.5 and -1)) or 0}, - pickup = math2d.position.subtract({x = x_frac3 < 0 and (x_int3 - 1) or x_int3, y = y_frac3 < 0 and (y_int3 - 1) or y_int3}, base_tile) - } - end - - function inserter_utils.set_arm_positions(inserter, positions) - inserter.pickup_position = (positions.pickup and math2d.position.add(inserter.position, positions.pickup)) or nil - - if positions.drop or positions.drop_offset then - local x_int, x_frac, y_int, y_frac = math.modf((inserter.position.x and inserter.position.x) or inserter.position[1]), math.modf((inserter.position.y and inserter.position.y) or inserter.position[2]) - local x_int2, x_frac2, y_int2, y_frac2 = math.modf((inserter.drop_position.x and inserter.drop_position.x) or inserter.drop_position[1]), math.modf((inserter.drop_position.y and inserter.drop_position.y) or inserter.drop_position[2]) - local old_drop_tile, old_drop_offset = {x = x_frac2 < 0 and (x_int2 - 1) or x_int2, y = y_frac2 < 0 and (y_int2 - 1) or y_int2}, {x = x_frac2 < 0 and (x_frac2 + 1) or x_frac2, y = y_frac2 < 0 and (y_frac2 + 1) or y_frac2} - inserter.drop_position = math2d.position.add((positions.drop and math2d.position.add({x = x_frac < 0 and (x_int - 1) or x_int, y = y_frac < 0 and (y_int - 1) or y_int}, positions.drop)) or old_drop_tile, (positions.drop_offset and math2d.position.add(math2d.position.multiply_scalar(positions.drop_offset, 0.2), {0.5, 0.5})) or old_drop_offset) - end - end - - function gui.create(player) - if player.gui.relative.inserter_config then - player.gui.relative.inserter_config.destroy() - end - - local frame_main = player.gui.relative.add({type = 'frame', name = 'inserter_config', anchor = {gui = defines.relative_gui_type.inserter_gui, position = defines.relative_gui_position.right}}) - local frame_content = frame_main.add({type = 'frame', name = 'frame_content', style = 'entity_frame'}) - local flow_content = frame_content.add({type = 'flow', name = 'flow_content', direction = 'vertical'}) - local table_range = 1 - - for _, inserter in pairs(prototypes.get_entity_filtered({{filter = 'type', type = 'inserter'}})) do - local pickup_pos = math2d.position.ensure_xy(math2d.position.add(inserter.inserter_pickup_position, {0.5, 0.5})) - local drop_pos = math2d.position.ensure_xy(math2d.position.add(inserter.inserter_drop_position, {0.5, 0.5})) - table_range = math.max(table_range, math.abs(math.floor(pickup_pos.x)), math.abs(math.floor(pickup_pos.y)), math.abs(math.floor(drop_pos.x)), math.abs(math.floor(drop_pos.y))) - end - - local table_position = flow_content.add({type = 'table', name = 'table_position', column_count = 1 + table_range * 2}) - table_position.style.horizontal_spacing = 1 - table_position.style.vertical_spacing = 1 - - for y = -table_range, table_range, 1 do - for x = -table_range, table_range, 1 do - local sprite = ((x == 0 and y == 0) and table_position.add({type = 'sprite', name = 'sprite_inserter', sprite = 'item/bulk-inserter'})) or table_position.add({type = 'sprite-button', name = 'button_position_' .. tostring(x + table_range + 1) .. '_' .. tostring(y + table_range + 1), style = 'slot_sized_button'}) - - if (x == 0 and y == 0) then - sprite.style.stretch_image_to_widget_size = true - end - - sprite.style.size = {32, 32} - end - end - - local line = flow_content.add({type = 'line', name = 'line', style = 'inside_shallow_frame_with_padding_line'}) - line.style.top_margin = 8 - table_position = flow_content.add({type = 'table', name = 'table_offset', column_count = 3}) - table_position.style.horizontal_spacing = 1 - table_position.style.vertical_spacing = 1 - - for y = 1, 3, 1 do - for x = 1, 3, 1 do - local sprite = table_position.add({type = 'sprite-button', name = 'button_offset_' .. tostring(x + table_range + 1) .. '_' .. tostring(y + table_range + 1), style = 'slot_sized_button'}) - sprite.style.size = {32, 32} - end - end - end - - function gui.update(player, inserter) - local gui_instance = player.gui.relative.inserter_config.frame_content.flow_content - local table_range = (gui_instance.table_position.column_count - 1) / 2 - local pickup_pos = math2d.position.ensure_xy(math2d.position.add((inserter.prototype or inserter.ghost_prototype).inserter_pickup_position, {0.5, 0.5})) - local drop_pos = math2d.position.ensure_xy(math2d.position.add((inserter.prototype or inserter.ghost_prototype).inserter_drop_position, {0.5, 0.5})) - local inserter_range = math.max(math.abs(math.floor(pickup_pos.x)), math.abs(math.floor(pickup_pos.y)), math.abs(math.floor(drop_pos.x)), math.abs(math.floor(drop_pos.y))) - local arm_positions = inserter_utils.get_arm_positions(inserter) - local idx = 0 - - for y = -table_range, table_range, 1 do - for x = -table_range, table_range, 1 do - idx = idx + 1 - - if gui_instance.table_position.children[idx].type == 'sprite-button' then - p1, p2, p3, p4 = (arm_positions.drop.x and {x = arm_positions.drop.x, y = arm_positions.drop.y}) or {x = arm_positions.drop[1], y = arm_positions.drop[2]}, {x = x, y = y}, (arm_positions.pickup.x and {x = arm_positions.pickup.x, y = arm_positions.pickup.y}) or {x = arm_positions.pickup[1], y = arm_positions.pickup[2]}, {x = x, y = y} - gui_instance.table_position.children[idx].sprite = (((p1.x == p2.x and p1.y == p2.y) and 'virtual-signal/down-arrow') or ((p3.x == p4.x and p3.y == p4.y) and 'virtual-signal/up-arrow')) or ((x ~= 0 or y ~= 0) and nil) - gui_instance.table_position.children[idx].enabled = math.abs(x) < inserter_range or math.abs(y) < inserter_range - end - end - end - - local prototype = (inserter.object_name == 'LuaEntity' and ((inserter.type == 'entity-ghost' and inserter.ghost_prototype) or inserter.prototype)) or ((inserter.object_name == 'LuaEntityPrototype' and inserter) or nil) - gui_instance.table_position.sprite_inserter.sprite = ((prototype and prototype.items_to_place_this) and 'item/' .. prototype.items_to_place_this[1].name) or 'item/inserter' - idx = 0 - - for y = -1, 1, 1 do - for x = -1, 1, 1 do - idx = idx + 1 - p1, p2 = (arm_positions.drop_offset.x and {x = arm_positions.drop_offset.x, y = arm_positions.drop_offset.y}) or {x = arm_positions.drop_offset[1], y = arm_positions.drop_offset[2]}, {x = x, y = y} - gui_instance.table_offset.children[idx].sprite = ((p1.x == p2.x and p1.y == p2.y) and 'virtual-signal/down-arrow') or nil - end - end - end - - script.on_event(defines.events.on_gui_click, function(e) - local player = game.players[e.player_index] - local gui_instance = player.gui.relative.inserter_config.frame_content.flow_content - - if e.element.parent == gui_instance.table_position and e.element ~= gui_instance.table_position.sprite_inserter then - local new_pos = math2d.position.add({(e.element.get_index_in_parent() - 1) % e.element.parent.column_count, math.floor((e.element.get_index_in_parent() - 1) / e.element.parent.column_count)}, {(e.element.parent.column_count - 1) * -0.5, (e.element.parent.column_count - 1) * -0.5}) - - if (e.button == defines.mouse_button_type.left and (not e.control or e.shift)) and e.element.sprite == 'virtual-signal/up-arrow' then - local new_positions = {drop = new_pos, pickup = (e.element.sprite == 'virtual-signal/up-arrow' and inserter_utils.get_arm_positions(player.opened).drop) or nil} - local old_positions = inserter_utils.get_arm_positions(player.opened) - local vec = (new_positions.drop.x and {x = new_positions.drop.x, y = new_positions.drop.y}) or {x = new_positions.drop[1], y = new_positions.drop[2]} - local new_drop_dir = math.floor(math.atan2(vec.x, -vec.y) * (4 / math.pi) + 0.5) % 8 - vec = (old_positions.drop.x and {x = old_positions.drop.x, y = old_positions.drop.y}) or {x = old_positions.drop[1], y = old_positions.drop[2]} - local delta = (new_drop_dir - math.floor(math.atan2(vec.x, -vec.y) * (4 / math.pi) + 0.5) % 8) % 8 - new_positions.drop_offset = (not new_positions.drop and old_positions.drop_offset) or (((delta % 2 == 0) and {x = ((delta == 0 or delta == 6) and old_positions.drop_offset.y) or -old_positions.drop_offset.y, y = ((delta == 0 or delta == 2) and old_positions.drop_offset.x) or -old_positions.drop_offset.x}) or math2d.direction.vectors[(new_drop_dir + (new_drop_dir % 2) * 4 % 8) + 1]) - inserter_utils.set_arm_positions(player.opened, new_positions) - - elseif (e.button == defines.mouse_button_type.right or (e.button == defines.mouse_button_type.left and (e.control or e.shift))) and e.element.sprite == 'virtual-signal/down-arrow' then - inserter_utils.set_arm_positions(player.opened, {pickup = new_pos, drop = (e.element.sprite == 'virtual-signal/down-arrow' and inserter_utils.get_arm_positions(player.opened).pickup) or nil}) - end - - for _, p in pairs(game.players) do - if p.opened and ((p.opened == player.opened) or (p.opened.object_name == 'LuaEntity' and (p.opened.type == 'inserter' or (p.opened.type == 'entity-ghost' and p.opened.ghost_type == 'inserter')))) then - gui.update(p, p.opened) - end - end - - elseif e.element.parent == gui_instance.table_offset then - inserter_utils.set_arm_positions(player.opened, {drop_offset = math2d.position.add({(e.element.get_index_in_parent() - 1) % e.element.parent.column_count, math.floor((e.element.get_index_in_parent() - 1) / e.element.parent.column_count)}, {(e.element.parent.column_count - 1) * -0.5, (e.element.parent.column_count - 1) * -0.5})}) - gui.update(player, player.opened) - end - end) - - script.on_event(defines.events.on_player_rotated_entity, function(e) - if e.entity and (e.entity.type == 'inserter' or (e.entity.type == 'entity-ghost' and e.entity.ghost_type == 'inserter')) then - for _, player in pairs(game.players) do - if player.opened == e.entity then - gui.update(player, player.opened) - end - end - end - end) - - script.on_event(defines.events.on_entity_settings_pasted, function(e) - if e.destination.type == 'inserter' or (e.destination.type == 'entity-ghost' and e.destination.ghost_type == 'inserter') then - e.destination.direction = e.source.direction - local arm_positions = inserter_utils.get_arm_positions(e.destination) - local pickup_pos = math2d.position.ensure_xy(math2d.position.add((e.destination.prototype or e.destination.ghost_prototype).inserter_pickup_position, {0.5, 0.5})) - local drop_pos = math2d.position.ensure_xy(math2d.position.add((e.destination.prototype or e.destination.ghost_prototype).inserter_drop_position, {0.5, 0.5})) - local max_range = math.max(math.abs(math.floor(pickup_pos.x)), math.abs(math.floor(pickup_pos.y)), math.abs(math.floor(drop_pos.x)), math.abs(math.floor(drop_pos.y))) - - for _, v in pairs({'drop', 'pickup'}) do - if math.max(math.abs(arm_positions[v].x), math.abs(arm_positions[v].y)) > max_range then - local vec = (arm_positions[v].x and {x = arm_positions[v].x, y = arm_positions[v].y}) or {x = arm_positions[v][1], y = arm_positions[v][2]} - arm_positions[v] = math2d.position.multiply_scalar(math2d.direction.vectors[(math.floor(math.atan2(vec.x, -vec.y) * (4 / math.pi) + 0.5) % 8) + 1], max_range) - end - end - - arm_positions.pickup = ((arm_positions.pickup.x == arm_positions.drop.x and arm_positions.pickup.y == arm_positions.drop.y) and {x = -arm_positions.drop.x , y = -arm_positions.drop.y}) or arm_positions.pickup - inserter_utils.set_arm_positions(e.destination, arm_positions) - - for _, player in pairs(game.players) do - if player.opened == e.destination then - gui.update(player, player.opened) - end - end - end - end) - ]] end