diff --git a/control.lua b/control.lua index 37e75ab..6743229 100644 --- a/control.lua +++ b/control.lua @@ -1,56 +1,101 @@ local DEBUG = false + script.on_event("picker-select", function(event) - local player = game.players[event.player_index] - local creative = player.cheat_mode - if player.selected then - - - local ip - local locname = player.selected.localised_name -- use entity name as locname in case we can't find an item. - local ep = player.selected.prototype - - - if ep and ep.mineable_properties and ep.mineable_properties.minable and ep.mineable_properties.products - and ep.mineable_properties.products[1].type == "item" then -- If the entity has mineable products. - - ip = game.item_prototypes[ep.mineable_properties.products[1].name] -- Retrieve first available item prototype - - - if ip and ip.place_result then -- If the entity has an item with a placeable prototype, - locname=ip.localised_name or locname - local n = math.min(player.get_item_count(ip.name), ip.stack_size) - - if n > 0 then -- Player has some of this item in their inventory. - local held_stack = nil - if player.cursor_stack.valid_for_read then - -- Player is already holding something, remember what it was: - held_stack = {name = player.cursor_stack.name, count = player.cursor_stack.count} - player.cursor_stack.clear() - end - -- Put items in the cursor stack: - player.cursor_stack.set_stack({name = ip.name, count = n}) - -- Remove an equal number from the main inventory: - player.remove_item({name = ip.name, count = n}) - -- Put whatever was originally being held back into player's inventory: - if held_stack then - player.insert(held_stack) - end - elseif creative then -- player has cheat_mode enabled just give him 1 if he doesn't have any! - player.cursor_stack.clear() - player.cursor_stack.set_stack({name = ip.name, count = 1}) - else -- None in inventory - if DEBUG then locname = ip.name end - player.print({"msg-no-item-inv", locname}) - end - - else -- No prototype found. - if DEBUG then player.print("No placeable entity prototype found for item "..player.selected.name..".") end - end - else - if DEBUG then player.print("Not a minable entity with item products") end - end - - else -- No entity under cursor; maybe the player doesn't know how to use the picker. - --if DEBUG then player.print("Place cursor over an object before activating picker tool.") end - end -end) + local player = game.players[event.player_index] + local creative = player.cheat_mode + if player.selected then + + local ip + local locname = player.selected.localised_name -- use entity name as locname in case we can't find an item. + local ep = player.selected.prototype + + if ep and ep.mineable_properties and ep.mineable_properties.minable and ep.mineable_properties.products + and ep.mineable_properties.products[1].type == "item" then -- If the entity has mineable products. + + ip = game.item_prototypes[ep.mineable_properties.products[1].name] -- Retrieve first available item prototype + + if ip and ip.place_result then -- If the entity has an item with a placeable prototype, + locname=ip.localised_name or locname + local n = math.min(player.get_item_count(ip.name), ip.stack_size) + + if n > 0 then -- Player has some of this item in their inventory. + local held_stack = nil + if player.cursor_stack.valid_for_read then + -- Player is already holding something, remember what it was: + held_stack = {name = player.cursor_stack.name, count = player.cursor_stack.count} + player.cursor_stack.clear() + end + -- Put items in the cursor stack: + player.cursor_stack.set_stack({name = ip.name, count = n}) + -- Remove an equal number from the main inventory: + player.remove_item({name = ip.name, count = n}) + -- Put whatever was originally being held back into player's inventory: + if held_stack then + player.insert(held_stack) + end + elseif creative then -- player has cheat_mode enabled just give him 1 if he doesn't have any! + player.cursor_stack.clear() + player.cursor_stack.set_stack({name = ip.name, count = 1}) + else -- None in inventory + if DEBUG then locname = ip.name end + if remote.interfaces.handyhands and remote.interfaces.handyhands.paused and not remote.call("handyhands", "paused", player.index) then + --player.print("HandyHands not paused") + if player.get_craftable_count(ip.name) > 0 and player.force.recipes[ip.name].enabled then + player.begin_crafting{count=1,recipe=ip.name,silent=true} + end + else + player.print({"msg-no-item-inv", locname}) + end + end + + else -- No prototype found. + if DEBUG then player.print("No placeable entity prototype found for item "..player.selected.name..".") end + end + else + if DEBUG then player.print("Not a minable entity with item products") end + end + + else -- No entity under cursor; maybe the player doesn't know how to use the picker. + if DEBUG then player.print("Place cursor over an object before activating picker tool.") end + end + end) + +local function SpawnGUI(player) + -- if not player.gui.center.renameFrame then + local frame = player.gui.center.add{type="frame", name="picker_renameFrame"} + frame.add{type = "button",name = "picker_renamerX",caption = " X ",style = "picker-renamer-button-style"} + frame.add{type="textfield",name="picker_renameTextfield"} + player.gui.center.picker_renameFrame.picker_renameTextfield.text = + global.renamer[player.index].backer_name + frame.add{type = "button",name = "picker_renamerButton",caption = "OK",style = "picker-renamer-button-style"} + -- end +end + +script.on_event(defines.events.on_gui_click, function(event) + if event.element.name == "picker_renamerX" then + game.players[event.player_index].gui.center.picker_renameFrame.destroy() + elseif event.element.name == "picker_renamerButton" then + local player = game.players[event.player_index] + if global.renamer[event.player_index].valid then + global.renamer[event.player_index].backer_name = + player.gui.center.picker_renameFrame.picker_renameTextfield.text + end + player.gui.center.picker_renameFrame.destroy() + end + end) + +script.on_event("picker-rename", function(event) + if game.players[event.player_index].gui.center.picker_renameFrame then + game.players[event.player_index].gui.center.picker_renameFrame.destroy() + end + local selection = game.players[event.player_index].selected + if selection then + if selection.supports_backer_name() then + if not global.renamer then global.renamer = {} end + global.renamer[event.player_index] = selection + SpawnGUI(game.players[event.player_index]) + else + game.players[event.player_index].print({"selection-not-renamable"}) + end + end + end) diff --git a/data.lua b/data.lua index 17aeee9..a5d6ba7 100644 --- a/data.lua +++ b/data.lua @@ -5,4 +5,38 @@ data:extend({ key_sequence = "Q", consuming = "all" }, -}) \ No newline at end of file +}) + +if data.raw["custom-input"]["rename"] then -- Gotlags renamer is installed unassign hotkey? + log("PickerExtended: Renamer installed") +end + +data:extend({ + { + type = "custom-input", + name = "picker-rename", + key_sequence = "CONTROL + R", + consuming = "all" + }, + { + type = "font", + name = "picker-renamer-button", + from = "default-bold", + size = 14 + } +}) + +data.raw["gui-style"].default["picker-renamer-button-style"] = +{ + type = "button_style", + parent = "button_style", + font = "picker-renamer-button", + align = "center", + top_padding = 2, + right_padding = 2, + bottom_padding = 2, + left_padding = 2, + default_font_color = {r = 1, g = 0.707, b = 0.12}, + hovered_font_color = {r = 1, g = 1, b = 1}, + clicked_font_color = {r = 1, g = 0.707, b = 0.12} +} \ No newline at end of file diff --git a/info.json b/info.json index 783a130..15f6995 100644 --- a/info.json +++ b/info.json @@ -1,10 +1,11 @@ { - "name": "picker-old", + "name": "PickerExtended", "version": "1.0.1", "factorio_version": "0.14", - "title": "Picker old version", - "author": "Tinyboss", + "title": "Picker Extended Version", + "author": "Tinyboss, nexela", "contact": "ericdb@gmail.com", + "dependencies": ["? Renamer"], "homepage": "https://forums.factorio.com/viewtopic.php?f=93&t=14695", - "description": "Adds a hotkey to select the entity under the mouse cursor from player inventory, if present." + "description": "Adds a hotkey to select the entity under the mouse cursor from player inventory, if present. extended with renamer and debugger" } diff --git a/locale/en/locale.cfg b/locale/en/locale.cfg index 11c4bb9..9d82369 100644 --- a/locale/en/locale.cfg +++ b/locale/en/locale.cfg @@ -1,3 +1,5 @@ msg-no-item-inv=No __1__ in inventory. +selection-not-renamable=Selection does not support renaming. [controls] -picker-select=Select entity +picker-select=Picker Select entity +picker-rename=Picker Rename entity