z_mirror/BlueprintTools

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

Caleb HeuerGet control scripting working00b37f1

trunk
2.5 KiB80 linesraw
1local gui = require("gui")
2
3local constants = require("constants")
4local import_string_gui = require("scripts.gui.import-string")
5local util = require("scripts.util")
6
7local library_shortcuts_gui = {}
8
9--- @param player LuaPlayer
10--- @param player_table PlayerTable
11function library_shortcuts_gui.refresh(player, player_table)
12  local frame = player.gui.relative.bpt_library_shortcuts_frame
13  if frame and frame.valid then
14    frame.destroy()
15  end
16
17  if not player_table.settings.show_library_shortcuts then
18    return
19  end
20
21  local buttons = {}
22  for _, prototype_name in pairs(constants.library_shortcut_prototypes) do
23    local prototype = prototypes.shortcut[prototype_name]
24    if prototype then
25      local tooltip = prototype.localised_name
26      if prototype.associated_control_input then
27        tooltip = { "", tooltip, " (", { "gui.bpt-shortcut-" .. prototype_name .. "-control" }, ")" }
28      end
29      table.insert(buttons, {
30        type = "sprite-button",
31        style = "bpt_shortcut_button_" .. prototype_name,
32        sprite = "bpt_shortcut_sprite_" .. prototype_name,
33        tooltip = tooltip,
34        actions = {
35          on_click = {
36            gui = "library_shortcuts",
37            action = prototype.action,
38            item_to_spawn = prototype.item_to_spawn and prototype.item_to_spawn.name or nil,
39          },
40        },
41      })
42    end
43  end
44
45  gui.add(player.gui.relative, {
46    type = "frame",
47    name = "bpt_library_shortcuts_frame",
48    style = "slot_window_frame",
49    {
50      type = "frame",
51      style = "inside_deep_frame",
52      direction = "vertical",
53      children = buttons,
54    },
55    --- @type GuiAnchor
56    anchor = {
57      gui = defines.relative_gui_type.blueprint_library_gui,
58      position = defines.relative_gui_position.right,
59    },
60  })
61end
62
63--- @param player LuaPlayer
64function library_shortcuts_gui.handle_action(player, player_table, action)
65  if action.action == "spawn-item" then
66    local cursor_stack = player.cursor_stack
67    if cursor_stack and cursor_stack.valid and player.clear_cursor() then
68      cursor_stack.set_stack({ type = "item", name = action.item_to_spawn, count = 1 })
69      -- The event will not fire until the next tick
70      player_table.flags.setting_temporary_item = true
71      player_table.flags.holding_temporary_item = true
72    else
73      util.cursor_notification(player, { "message.bpt-unable-to-clear-cursor" }, "utility/cannot_build")
74    end
75  elseif action.action == "import-string" then
76    import_string_gui.build(player)
77  end
78end
79
80return library_shortcuts_gui