/
freewaffle
/
darkbox
Обзор
Документация
Войти
/
freewaffle
/
darkbox
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
editor/editor.gui_script
256 строк
7 KB
freewaffle
intermediate commit
17 ноя 2025, 22:21
17 ноя 2025, 22:21
771999e
Код
Авторство
О чём код?
--[[ Этот файл — часть Darkbox. Darkbox — свободная программа: вы можете перераспространять ее и/или изменять ее на условиях Стандартной общественной лицензии GNU в том виде, в каком она была опубликована Фондом свободного программного обеспечения; либо версии 3 лицензии, либо (по вашему выбору) любой более поздней версии. Darkbox распространяется в надежде, что она будет полезной, но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Подробнее см. в Стандартной общественной лицензии GNU. Вы должны были получить копию Стандартной общественной лицензии GNU вместе с этой программой. Если это не так, см. <https://www.gnu.org/licenses/>. ]] local ACT = { MOUSE_LEFT = hash "mouse_left", BACKSPACE = hash "backspace", TEXT = hash "text", LEFT = hash "left", RIGHT = hash "right", ENTER = hash "enter" } local AUTO_INIT_FIELDS = true local nodes -- см. init() local game = { name = nil } function game.set_name(new_name) game.name = new_name gui.set_text(nodes.editor.title.text, game.name) end local wm = {} wm.type = { number = 0, string = 1 } wm.input = nil wm.input_cursor = 1 wm.input_type = nil wm.input_callback = nil wm.busyness = 0 function wm.busy(enable) if enable then wm.busyness = wm.busyness + 1 else wm.busyness = wm.busyness - 1 end gui.set_enabled(nodes.busy, wm.busyness > 0) end function wm.request_input(enable, input_type, title, callback) if enable then local alt_title = "???" if not title then local type = ( -- (тип 1) and ("имя типа 1") or ( -- (тип 2) and ("имя типа 2") or ( -- (тип 2) and ("имя типа 2") or ( -- ("альт") -- ))) -- --> (тип 1) and ("имя типа 1") or ((тип 2) and ("имя типа 2") or ((тип 3) and ("имя типа 3") or (...))) (input_type == wm.type.number) and ("число") or ( (input_type == wm.type.string) and ("текст") or ( error("invalid type", 2) )) ) alt_title = "Введите " .. type .. "..." end gui.set_text(nodes.input.title, title or alt_title) end wm.input = (enable == true) and "" or nil wm.input_cursor = 0 wm.input_type = input_type wm.input_callback = assert(callback, "add input callback!") gui.set_enabled(nodes.input.window, enable == true) end function wm.alert_input() gui.set_color(nodes.input.field, vmath.vector3(0.4, 0, 0)) gui.animate(nodes.input.field, "color", vmath.vector3(0.2), gui.EASING_INOUTSINE, 0.25, 0, nil, gui.PLAYBACK_ONCE_FORWARD) end local new_game = {} new_game.game_name = nil -- 2 function new_game.step_1_callback(game_name) game.name = game_name gui.set_text(nodes.editor.title.text, game.name) wm.busy(false) end -- 1 function new_game.require_game_name() gui.set_enabled(nodes.welcome.root, false) wm.request_input(true, wm.type.string, "Введите название игры:", new_game.step_1_callback) end ---------------------------------------------------------------- -- Создаёт диалог создания новой игры. local function dialog_new_game() new_game.require_game_name() end local function quit() -- TODO: вопрос "сохранить игру?" msg.post("main:/system#script", "close_editor") end function init(self) msg.post("#", "acquire_input_focus") do local node = gui.get_node nodes = { editor = { title = { text = node "editor_title" } }, lpanel = { root = node "editor_Lpanel", head = { root = node "editor_Lpanel_head" } }, busy = node "busy", input = { window = node "input", title = node "input_title", data = node "input_field_data", cursor = node "input_cursor", field = node "input_field" }, welcome = { root = node "welcome", quit = node "welcome_quit/root", new = node "welcome_new/root" } } end do -- многоразовый (DRY) аниматор "мягкого мигания" local function soft_blink(node) gui.animate(node, "color", vmath.vector3(1), gui.EASING_INOUTSINE, 2, 0, nil, gui.PLAYBACK_LOOP_PINGPONG) end soft_blink(nodes.input.window) -- self.cursor_blinker = timer.delay(0.5, true, function () -- local cursor = nodes.input.cursor -- gui.set_enabled(cursor, not gui.is_enabled(cursor, false)) -- end) end if AUTO_INIT_FIELDS then game.set_name("test game") end end function on_input(self, action_id, action) local ax, ay = action.x, action.y local update_input = false if action_id == ACT.MOUSE_LEFT then -- if action.pressed then -- end if action.released then if gui.is_enabled(nodes.welcome.root) then if gui.pick_node(nodes.welcome.quit, ax, ay) then quit() elseif gui.pick_node(nodes.welcome.new, ax, ay) then dialog_new_game() end end end elseif (action_id == ACT.LEFT or action_id == ACT.RIGHT) and action.repeated then local move = action_id == ACT.LEFT and -1 or 1 wm.input_cursor = vmath.clamp(wm.input_cursor + move, 0, string.len(wm.input)) update_input = true elseif action_id == ACT.BACKSPACE and action.repeated then if wm.input ~= nil then --local is_non_ascii = (string.byte(wm.input, -1) or 0) >= 128 wm.input = string.sub(wm.input, 1, math.max(0, wm.input_cursor - 1)) .. string.sub(wm.input, wm.input_cursor + 1, string.len(wm.input)) wm.input_cursor = math.max(0, wm.input_cursor - (1)) update_input = true end elseif action_id == ACT.TEXT then if wm.input ~= nil then local character_pattern = "[a-z A-Z %s %p]+" local is_character = (wm.input_type == wm.type.string) and (string.match(action.text, character_pattern) ~= nil) or false local is_number = string.match(action.text, "%d") ~= nil if is_character or is_number then wm.input_cursor = wm.input_cursor + 1 wm.input = string.sub(wm.input, 1, wm.input_cursor-1) .. action.text .. string.sub(wm.input, wm.input_cursor, string.len(wm.input)) update_input = true else wm.alert_input() end end elseif action_id == ACT.ENTER then if wm.input ~= nil then gui.set_enabled(nodes.input.window, false) wm.input_callback(wm.input) end -- elseif not action_id then end if update_input then do local cursor = nodes.input.cursor gui.set_position(cursor, vmath.vector3(-260 + (14 * wm.input_cursor), 0, 0)) local xsize = 14 if wm.input_cursor < string.len(wm.input) then xsize = 2 end gui.set_size(cursor, vmath.vector3(xsize, 24, 0)) end gui.set_text(nodes.input.data, wm.input) end end