z_mirror/BlueprintTools

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

Caleb HeuerGet control scripting working00b37f1

trunk
4.7 KiB157 linesraw
1local gui = require("gui")
2
3local set_tiles = require("scripts.processors.set-tiles")
4
5local set_tiles_gui = {}
6
7function set_tiles_gui.build(player, player_table)
8  local settings = player_table.set_tiles_settings
9  local refs = gui.build(player.gui.screen, {
10    {
11      type = "frame",
12      direction = "vertical",
13      caption = { "controls.bpt-set-tiles" },
14      ref = { "window" },
15      actions = {
16        on_closed = { gui = "set_tiles", action = "close" },
17      },
18      children = {
19        {
20          type = "frame",
21          style = "inside_shallow_frame_with_padding",
22          children = {
23            {
24              type = "choose-elem-button",
25              style = "slot_button_in_shallow_frame",
26              elem_type = "tile",
27              tile = settings.tile,
28              elem_filters = { { filter = "blueprintable" } },
29              actions = {
30                on_elem_changed = { gui = "set_tiles", action = "update_tile" },
31              },
32              ref = { "tile_button" },
33            },
34            {
35              type = "line",
36              style_mods = { left_margin = 8, vertically_stretchable = true },
37              direction = "vertical",
38            },
39            {
40              type = "flow",
41              style_mods = { left_margin = 8 },
42              direction = "vertical",
43              children = {
44                {
45                  type = "checkbox",
46                  caption = { "gui.bpt-fill-gaps" },
47                  tooltip = { "gui.bpt-fill-gaps-description" },
48                  state = settings.fill_gaps,
49                  ref = { "fill_gaps_checkbox" },
50                },
51                {
52                  type = "flow",
53                  style_mods = { vertical_align = "center" },
54                  ref = { "margin_flow" },
55                  children = {
56                    {
57                      type = "label",
58                      style_mods = { right_margin = 8 },
59                      caption = { "gui.bpt-margin" },
60                      tooltip = { "gui.bpt-margin-description" },
61                    },
62                    {
63                      type = "textfield",
64                      style_mods = { width = 50, horizontal_align = "center" },
65                      text = tostring(settings.margin),
66                      numeric = true,
67                      ref = { "margin_textfield" },
68                    },
69                  },
70                },
71              },
72            },
73          },
74        },
75        {
76          type = "flow",
77          style = "dialog_buttons_horizontal_flow",
78          children = {
79            {
80              type = "button",
81              style = "back_button",
82              caption = { "gui.cancel" },
83              actions = {
84                on_click = { gui = "set_tiles", action = "close" },
85              },
86            },
87            {
88              type = "empty-widget",
89              style = "flib_dialog_footer_drag_handle",
90              style_mods = { minimal_width = 24 },
91              ref = { "footer_drag_handle" },
92            },
93            {
94              type = "button",
95              style = "confirm_button",
96              caption = { "gui.confirm" },
97              ref = { "confirm_button" },
98              actions = {
99                on_click = { gui = "set_tiles", action = "confirm" },
100              },
101            },
102          },
103        },
104      },
105    },
106  })
107
108  refs.footer_drag_handle.drag_target = refs.window
109  refs.window.force_auto_center()
110
111  player.opened = refs.window
112
113  player_table.guis.set_tiles = {
114    refs = refs,
115  }
116end
117
118function set_tiles_gui.destroy(player_table)
119  local gui_data = player_table.guis.set_tiles
120  gui_data.refs.window.destroy()
121  player_table.guis.set_tiles = nil
122end
123
124function set_tiles_gui.handle_action(e, msg)
125  local player = game.get_player(e.player_index)
126  local player_table = storage.players[e.player_index]
127  local gui_data = player_table.guis.set_tiles
128  local refs = gui_data.refs
129
130  if msg.action == "close" then
131    set_tiles_gui.destroy(player_table)
132  elseif msg.action == "confirm" then
133    local tile_name = refs.tile_button.elem_value
134    if tile_name then
135      -- Save settings
136      local settings = player_table.set_tiles_settings
137      settings.tile = tile_name
138      settings.fill_gaps = refs.fill_gaps_checkbox.state
139      settings.margin = tonumber(refs.margin_textfield.text) or 0
140
141      -- Modify the blueprint
142      set_tiles(player, tile_name, settings.fill_gaps, settings.margin)
143
144      -- Destroy the GUI
145      set_tiles_gui.destroy(player_table)
146    end
147  elseif msg.action == "update_tile" then
148    local tile_name = refs.tile_button.elem_value
149    if tile_name then
150      refs.confirm_button.enabled = true
151    else
152      refs.confirm_button.enabled = false
153    end
154  end
155end
156
157return set_tiles_gui