z_mirror/BlueprintTools

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

Caleb HeuerGet control scripting working00b37f1

trunk
2.7 KiB94 linesraw
1local gui = require("gui")
2
3local util = require("scripts.util")
4
5local import_string_gui = {}
6
7function import_string_gui.build(player)
8  import_string_gui.destroy(player)
9
10  local frame = gui.add(player.gui.screen, {
11    type = "frame",
12    name = "bpt_import_string_frame",
13    direction = "vertical",
14    caption = { "gui-blueprint-library.import-string" },
15    actions = {
16      on_closed = { gui = "import_string", action = "close" },
17    },
18    ref = { "window" },
19    {
20      type = "text-box",
21      name = "textbox",
22      style_mods = { width = 400, height = 250 },
23      elem_mods = { word_wrap = true },
24      ref = { "textbox" },
25    },
26    {
27      type = "flow",
28      name = "footer_flow",
29      style = "dialog_buttons_horizontal_flow",
30      ref = { "footer_flow" },
31      {
32        type = "button",
33        style = "back_button",
34        caption = { "gui.cancel" },
35        actions = {
36          on_click = { gui = "import_string", action = "close" },
37        },
38      },
39      { type = "empty-widget", style = "flib_dialog_footer_drag_handle", ignored_by_interaction = true },
40      {
41        type = "button",
42        style = "confirm_button",
43        caption = { "gui-blueprint-library.import" },
44        actions = {
45          on_click = { gui = "import_string", action = "import" },
46        },
47      },
48    },
49  })
50
51  frame.force_auto_center()
52  frame.footer_flow.drag_target = frame
53  frame.textbox.focus()
54  player.opened = frame
55end
56
57function import_string_gui.destroy(player)
58  local frame = player.gui.screen.bpt_string_import_frame
59  if frame and frame.valid then
60    if player.opened == frame then
61      player.opened = nil
62    end
63    frame.destroy()
64  end
65end
66
67function import_string_gui.handle_action(player, action)
68  local frame = player.gui.screen.bpt_import_string_frame
69  if not frame or not frame.valid then
70    return
71  end
72  if action.action == "close" then
73    frame.destroy()
74  elseif action.action == "import" then
75    if player.clear_cursor() then
76      local result = player.cursor_stack.import_stack(frame.textbox.text)
77      if result == 0 then
78        local cursor_stack = player.cursor_stack
79        if cursor_stack.valid_for_read then
80          util.cursor_notification(player, { "string-import-successful", cursor_stack.prototype.localised_name })
81        else
82          util.cursor_notification(player, { "string-import-produced-no-item" }, "utility/cannot_build")
83        end
84        frame.destroy()
85      else
86        util.cursor_notification(player, { "failed-to-import-string", "" }, "utility/cannot_build")
87      end
88    else
89      util.cursor_notification(player, { "message.bpt-unable-to-clear-cursor" }, "utility/cannot_build")
90    end
91  end
92end
93
94return import_string_gui