Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/lua_console.lua

raw
1 --[[ Copyright (c) 2010 Manuel "Roujin" Wolf
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 -- luacheck: globals _
22 strict_declare_global "_"
23 _ = nil
24
25 --! Interactive Lua Console for ingame debugging.
26 class "UILuaConsole" (UIResizable)
27
28 ---@type UILuaConsole
29 local UILuaConsole = _G["UILuaConsole"]
30
31 local col_bg = {
32 red = 46,
33 green = 186,
34 blue = 60,
35 }
36
37 local col_textbox = {
38 red = 0,
39 green = 0,
40 blue = 0,
41 }
42
43 local col_highlight = {
44 red = 174,
45 green = 166,
46 blue = 218,
47 }
48
49 local col_shadow = {
50 red = 134,
51 green = 126,
52 blue = 178,
53 }
54
55 function UILuaConsole:UILuaConsole(ui)
56 self:UIResizable(ui, 320, 240, col_bg)
57
58 local app = ui.app
59 self.modal_class = "console"
60 self.esc_closes = true
61 self.resizable = true
62 self.min_width = 200
63 self.min_height = 150
64 self:setDefaultPosition(0.1, 0.1)
65
66 -- Window parts definition
67 self.default_button_sound = "selectx.wav"
68
69 -- Textbox for entering code
70 self.textbox = self:addBevelPanel(20, 20, 280, 140, col_textbox, col_highlight, col_shadow)
71 :setLabel("", app.gfx:loadBuiltinFont(), "left"):setTooltip(_S.tooltip.lua_console.textbox):setAutoClip(true)
72 :makeTextbox():allowedInput("all"):setText({""})
73
74 self.textbox:setActive(true) -- activated by default
75
76 -- "Execute" button
77 self.execute_button = self:addBevelPanel(20, self.height - 60, 130, 40, col_bg):setLabel(_S.lua_console.execute_code)
78 :makeButton(0, 0, 130, 40, nil, self.buttonExecute):setTooltip(_S.tooltip.lua_console.execute_code)
79 -- "Close" button
80 self.close_button = self:addBevelPanel(170, self.height - 60, 130, 40, col_bg):setLabel(_S.lua_console.close)
81 :makeButton(0, 0, 130, 40, nil, self.buttonClose):setTooltip(_S.tooltip.lua_console.close)
82 end
83
84 function UILuaConsole:setSize(width, height)
85 UIResizable.setSize(self, width, height)
86
87 self.textbox:setSize(self.width - 40, self.height - 100)
88
89 local button_width = math.floor((self.width - 60) / 2)
90
91 self.execute_button:setPosition(20, self.height - 60)
92 self.execute_button:setSize(button_width, 40)
93
94 self.close_button:setPosition(self.width - 20 - button_width, self.height - 60)
95 self.close_button:setSize(button_width, 40)
96 end
97
98 function UILuaConsole:buttonExecute()
99 print("Loading UserFunction...")
100 local func, err, s
101
102 _ = TheApp.ui and TheApp.ui.debug_cursor_entity
103
104 local i = 0
105 func, err = load(function()
106 i = i + 1
107 if type(self.textbox.text) == "table" then
108 return self.textbox.text[i] and self.textbox.text[i] .. "\n"
109 else
110 return i < 2 and self.textbox.text
111 end
112 end, "=UserFunction")
113
114 if not func then
115 print("Error while loading UserFunction:")
116 print(err)
117 return
118 end
119
120 print("Executing UserFunction...")
121 s, err = pcall(func)
122 if not s then
123 print("Error while executing UserFunction:")
124 print(err)
125 end
126 _ = nil -- Clean up the global after use or it'll corrupt saves
127 end
128
129 function UILuaConsole:buttonClose()
130 self:close()
131 end
132
133 function UILuaConsole:afterLoad(old, new)
134 UIResizable.afterLoad(self, old, new)
135 if old < 65 then
136 -- added min_width and min_height
137 self.min_width = 200
138 self.min_height = 150
139 -- added execute_button, close_button, changed callback
140 self.execute_button = self.buttons[2]
141 self.close_button = self.buttons[3]
142 self.close_button.on_click = self.buttonClose
143 end
144 end