Blame view

control.lua 3.21 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
  -- 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)
3ef575ee   Stefan Wichmann   Initial checkin
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  	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)
3ef575ee   Stefan Wichmann   Initial checkin
54
  	local player = game.players[event.player_index]
3ef575ee   Stefan Wichmann   Initial checkin
55
56
57
58
59
60
61
  	
  	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
3ef575ee   Stefan Wichmann   Initial checkin
62
63
64
65
66
67
68
69
  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)
3ef575ee   Stefan Wichmann   Initial checkin
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  	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
  
  -- ----------------------------------------------------------------
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
89
90
91
92
93
94
95
96
  local function on_hotkey_main(event)
  	local player = game.players[event.player_index]
  	show_calculator(player)
  	focus_on_input(player)
  end
  
  -- ----------------------------------------------------------------
  script.on_event( "calcui_hotkey", on_hotkey_main )
3ef575ee   Stefan Wichmann   Initial checkin
97
98
99
100
101
  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 )
4078977a   Stefan Wichmann   Prepare for first...
102
  commands.add_command( "calcui", "Calculator UI [ debug | nodebug ] ", on_calcui_command )