Blame view

control.lua 2.89 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
  -- 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
  
  -- ----------------------------------------------------------------
3ef575ee   Stefan Wichmann   Initial checkin
30
31
32
33
34
35
36
37
38
39
40
41
42
  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
43
  	local player = game.players[event.player_index]
2de48216   Stefan Wichmann   Version 0.18.4 pu...
44
45
  
  	local calcui_prefix = "calcui"
3ef575ee   Stefan Wichmann   Initial checkin
46
47
48
49
50
  	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
51
52
53
54
55
56
57
58
  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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  	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...
78
79
  local function on_hotkey_main(event)
  	local player = game.players[event.player_index]
3371d413   Stefan Wichmann   New setting for c...
80
  	toggle_calculator(player, true)
c6cb1d22   Stefan Wichmann   Bugfix 0.18.1 - b...
81
82
83
84
  end
  
  -- ----------------------------------------------------------------
  script.on_event( "calcui_hotkey", on_hotkey_main )
3ef575ee   Stefan Wichmann   Initial checkin
85
86
87
88
89
  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...
90
  commands.add_command( "calcui", "Calculator UI [ debug | nodebug ] ", on_calcui_command )