aboutsummaryrefslogtreecommitdiff
path: root/advtrains_interlocking/tool.lua
blob: 5d38b3a1511b81612e4aa40909fa7819a1e9e5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- tool.lua
-- Interlocking tool

local ilrs = advtrains.interlocking.route

minetest.register_craftitem("advtrains_interlocking:tool",{
	description = "Interlocking tool\nright-click turnouts to inspect route locks",
	groups = {cracky=1}, -- key=name, value=rating; rating=1..3.
	inventory_image = "at_il_tool.png",
	wield_image = "at_il_tool.png",
	stack_max = 1,
	on_place = function(itemstack, placer, pointed_thing)
		local pname = placer:get_player_name()
		if not pname then
			return
		end
		if not minetest.check_player_privs(pname, {interlocking=true}) then
			minetest.chat_send_player(pname, "Insufficient privileges to use this!")
			return
		end
		if pointed_thing.type=="node" then
			local pos=pointed_thing.under
			if advtrains.is_passive(pos) then
				local form = "size[7,5]label[0.5,0.5;Route lock inspector]"
				local pts = minetest.pos_to_string(pos)
				
				local rtl = ilrs.has_route_lock(pts)
				
				if rtl then
					form = form.."label[0.5,1;Route locks currently put:\n"..rtl.."]"
					form = form.."button_exit[0.5,3.5;  5,1;clear;Clear]"
				else
					form = form.."label[0.5,1;No route locks set]"
					form = form.."button_exit[0.5,3.5;  5,1;emplace;Emplace manual lock]"
				end
				
				minetest.show_formspec(pname, "at_il_rtool_"..pts, form)
			else
				minetest.chat_send_player(pname, "Cannot use this here.")
				return
			end
		end
	end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
	local pname = player:get_player_name()
	if not minetest.check_player_privs(pname, "interlocking") then
		return
	end
	local pos
	local pts = string.match(formname, "^at_il_rtool_(.+)$")
	if pts then
		pos = minetest.string_to_pos(pts)
	end
	if pos then
		if advtrains.is_passive(pos) then
			if fields.clear then
				ilrs.remove_route_locks(pts)
			end
			if fields.emplace then
				ilrs.add_manual_route_lock(pts, "Manual lock ("..pname..")")
			end
		end
	end
end)
class="hl opt">(gui::IGUIElement *e) { return (e && (e == this || isMyChild(e))) || m_allow_focus_removal; } void draw() { if(!IsVisible) return; video::IVideoDriver* driver = Environment->getVideoDriver(); v2u32 screensize = driver->getScreenSize(); if(screensize != m_screensize_old /*|| m_force_regenerate_gui*/) { m_screensize_old = screensize; regenerateGui(screensize); //m_force_regenerate_gui = false; } drawMenu(); } /* This should be called when the menu wants to quit. WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return immediately if you call this from the menu itself. (More precisely, this decrements the reference count.) */ void quitMenu() { allowFocusRemoval(true); // This removes Environment's grab on us Environment->removeFocus(this); m_menumgr->deletingMenu(this); this->remove(); #ifdef HAVE_TOUCHSCREENGUI if (g_touchscreengui) g_touchscreengui->Show(); #endif } void removeChildren() { const core::list<gui::IGUIElement*> &children = getChildren(); core::list<gui::IGUIElement*> children_copy; for(core::list<gui::IGUIElement*>::ConstIterator i = children.begin(); i != children.end(); i++) { children_copy.push_back(*i); } for(core::list<gui::IGUIElement*>::Iterator i = children_copy.begin(); i != children_copy.end(); i++) { (*i)->remove(); } } virtual void regenerateGui(v2u32 screensize) = 0; virtual void drawMenu() = 0; virtual bool preprocessEvent(const SEvent& event) { return false; }; virtual bool OnEvent(const SEvent& event) { return false; }; virtual bool pausesGame(){ return false; } // Used for pause menu protected: //bool m_force_regenerate_gui; v2u32 m_screensize_old; private: IMenuManager *m_menumgr; // This might be necessary to expose to the implementation if it // wants to launch other menus bool m_allow_focus_removal; }; #endif