Blame view

control.lua 3.82 KB
3ef575ee   Stefan Wichmann   Initial checkin
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  -- control.lua
  
  require("calculator")
  
  -- ----------------------------------------------------------------
  function boolstr(bool)
  	if bool then 
  		return "T"
  	else 
  		return "F"
  	end
  end
  
  -- ----------------------------------------------------------------
  function debug_print(str)
  	if global.marc_debug then
  		game.print(str)
  	end
  end
  
  function __FUNC__() return debug.getinfo(2, 'n').name end
  
  function debug_log(f, str)
  	if global.marc_debug then
  		game.print(f .. ": " .. str)
  	end
  end
  
  -- ----------------------------------------------------------------
  local function get_gui_root(player)
  	-- return player.gui.left
  	return player.gui.screen
  end
  
  -- ----------------------------------------------------------------
  function destroy_calcui_gui(player)
  	local root = get_gui_root(player)
  	root.calcui_gui_top.destroy()
  end
  
  -- ----------------------------------------------------------------
  local function shortcut(event)
  	if event.prototype_name == "calcui_4func" then
      	local player = game.players[event.player_index]
      	toggle_calculator(player)
  	end
  end
  
  -- ----------------------------------------------------------------
  -- user has clicked somewhere.  If clicked on any gui item name that starts with "calcui_..."
  -- hide the gui
  local function on_gui_click(event)
  	local event_name = event.element.name
  	debug_print("event_name " .. event_name)
  	-- local calcui_prefix = "calcui_"
  	-- local possible_calcui_prefix = string.sub( event_name, 1, string.len(calcui_prefix) )
  	local player = game.players[event.player_index]
  	-- local root = get_gui_root(player)
  	
  	local calcui_prefix = "calcui_"
  	local possible_marcalc_prefix = string.sub( event_name, 1, string.len(calcui_prefix))
  	if possible_marcalc_prefix == calcui_prefix then
  		handle_calcui_click(event, player)
  		return
  	end
  	
  	-- if possible_calcui_prefix == calcui_prefix then
  		-- if event_name == "calcui_calculator_button" then
  			-- toggle_calculator(player)
  			-- return
  		-- end
  	
  		-- if root.calcui_gui_top then
  		    -- if event_name == "calcui_close_button" then
  				-- destroy_calcui_gui(player)
  				-- hide_calculator(player)
  			-- end
  		-- elseif event_name == "calcui_close_button" then
  			-- if player.gui.left.calcui_gui_top then
  				-- player.gui.left.calcui_gui_top.destroy()
  				-- hide_calculator(player)
  			-- end
  		-- end
  	-- end
  end
  
  -- ----------------------------------------------------------------
  -- user has confirmed the textfield / Called when a LuaGuiElement is confirmed, for example by pressing Enter in a textfield. 
  local function on_gui_confirmed(event)
  	player = game.players[event.player_index];
  	if event.element.name == "calcui_display" then
  		process_equal_key(player)
  		if settings.get_player_settings(player)["calcui-enter-clear"].value then
  			clear_equation(player)
  		end
  	end
  end
  
  -- ----------------------------------------------------------------
  local function on_calcui_command(event)
  	if event.parameter == "debug" then
  		global.calcui_debug = true
  		debug_print("calcui debugging is on")
  	elseif event.parameter == "nodebug" then
  		debug_print("calcui debugging is off")
  		global.calcui_debug = false
  	elseif event.parameter == nil then
  		game.players[event.player_index].print("please add a parameter")
  	else
  		game.players[event.player_index].print("unknown calcui parameter: " .. event.parameter)
  	end
  end
  
  -- ----------------------------------------------------------------
  --script.on_event( "calcui_hotkey", on_hotkey_main )
  script.on_event( defines.events.on_lua_shortcut, shortcut )
  script.on_event( defines.events.on_gui_click, on_gui_click)
  script.on_event( defines.events.on_gui_confirmed, on_gui_confirmed)
  script.on_event( defines.events.on_gui_text_changed, calcui_on_gui_text_changed )
  script.on_event( defines.events.on_gui_location_changed, calcui_on_gui_location_changed )
  commands.add_command( "calcui", "Max Rate Calculator [ debug | nodebug ] ", on_calcui_command )