set-goal/control.lua

Sun, 29 Jun 2025 23:37:19 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 29 Jun 2025 23:37:19 +0300
changeset 5
4418c07556d4
parent 3
b0344856f104
permissions
-rw-r--r--

Add more descriptions mod

local function open_gui(event)
	local player = game.players[event.player_index]
	if not player.gui.goal["set-goal"]
	then
		local frame = player.gui.goal.add{
			type = "frame",
			name = "set-goal",
			caption = {"set-goal-gui.title"},
			direction = "vertical"
		}
		local goal_description_text = player.get_goal_description()
		if type(goal_description_text) == "table"
		then
			local translation_request_id = player.request_translation(goal_description_text)
			goal_description_text = ""
			if not storage.translation_requests
			then
				storage.translation_requests = {}
			end
			storage.translation_requests[translation_request_id] = {player_index=event.player_index, type = "goal-description"}
		end
		frame.add{
			type = "text-box",
			text = goal_description_text,
			name = "goal-description",
			style = "set-goal-gui-textbox",
			icon_selector = true,
		}
		local buttons_flow = frame.add{
			type = "flow",
			name = "buttons-flow",
			direction = "horizontal",
		}
		buttons_flow.add{
			type = "sprite-button",
			tooltip = {"set-goal-gui.close-tooltip"},
			style = "tool_button_red",
			sprite = 'utility.close',
			name = "close-button",
			tags = {["owner-mod"] = "set-goal", action="close"},
		}
		buttons_flow.add{
			type = "sprite-button",
			tooltip = {"set-goal-gui.confirm-tooltip"},
			style = "confirm_button",
			name = "confirm-button",
			sprite = "utility.confirm_slot",	
			tags = {["owner-mod"] = "set-goal", action="confirm"},
		}
	else
		player.gui.goal["set-goal"].destroy()
	end
end

script.on_event(defines.events.on_gui_click,
	function(event)
		local player = game.players[event.player_index]
		if player.gui.goal["set-goal"]
			and event.element.tags
			and event.element.tags["owner-mod"] == "set-goal"
		then
			if event.element.tags.action == "confirm"
			then
				player.set_goal_description(player.gui.goal["set-goal"]["goal-description"].text, player.get_goal_description() ~= "")
				player.gui.goal["set-goal"].destroy()
			elseif event.element.tags.action == "close"
			then
				player.gui.goal["set-goal"].destroy()
			end
		end
	end
)

script.on_event("open-set-goal-gui", open_gui)

script.on_event(defines.events.on_lua_shortcut,
	function(event)
		if event.prototype_name == "open-set-goal-gui"
		then
			open_gui(event)
		end
	end
)

script.on_event(defines.events.on_string_translated,
	function(event)
		local player = game.players[event.player_index]
		if storage.translation_requests
		then
			local request_data = storage.translation_requests[event.id]
			if request_data and request_data.type == "goal-description" and player.gui.goal["set-goal"] and player.gui.goal["set-goal"]["goal-description"]
			then
				player.gui.goal["set-goal"]["goal-description"].text = event.result
			end
			storage.translation_requests[event.id] = nil
		end
	end
)

mercurial