--[[ File: UI_SAVE_DIALOG.SCRIPT Description: Save Dialog for STALKER Created: 27.9.2004 Author: Serhiy Vynnychenko (narrator@gsc-game.kiev.ua) Copyright: 2004 GSC Game World Version: 1.0 Modified: Serhiy Pryshchepa (peacemaker@gsc-game.kiev.ua) Modified by Orleon 2023/5/11 Added profile system --]] local saved_game_extension = ui_load_dialog.saved_game_extension local get_profile_name = ui_load_dialog.get_profile_name local hide_profile_prefix = ui_load_dialog.hide_profile_prefix local get_full_save_name = ui_load_dialog.get_full_save_name local check_save_files = ui_load_dialog.check_save_files ---------------------------------------------------------------------- class "save_item" (CUIListBoxItem) function save_item:__init(height) super(height) self:SetTextColor (GetARGB(255, 170, 170, 170)) self.fn = self:GetTextItem() self.fn:SetFont (GetFontLetterica18Russian()) self.fn:SetEllipsis (true) end ---------------------------------------------------------------------- class "UISaveDialog" (CUIScriptWnd) function UISaveDialog:__init() super() self:InitControls () self:InitCallBacks () self:FillList () end function UISaveDialog:__finalize() end function UISaveDialog:FillList() check_save_files() self.list_box:RemoveAll() local flist = getFS():file_list_open_ex("$game_saves$",FS.FS_ListFiles,"*"..saved_game_extension) local f_cnt = flist:Size() local profile_name = get_profile_name() flist:Sort(FS.FS_sort_by_modif_down) for it=0, f_cnt-1 do local file = flist:GetAt(it) local file_name = string.sub(file:NameFull(), 0, (string.len(file:NameFull()) - string.len(saved_game_extension))) if (file_name:find(profile_name)) then local date_time = "[" .. file:ModifDigitOnly() .. "]" self:AddItemToList(file_name, date_time) end end end function UISaveDialog:InitControls() self:SetWndRect(Frect():set(0,0,1024,768)) self.list_file_font = GetFontMedium() self.list_date_font = GetFontMedium() local xml = CScriptXmlInit() xml:ParseFile("ui_mm_save_dlg.xml") local ctrl xml:InitWindow ("background", 0, self) ctrl = CUIWindow() ctrl:SetAutoDelete(true) xml:InitWindow ("file_item:main", 0, ctrl) self.file_item_main_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) xml:InitWindow ("file_item:fn",0,ctrl) self.file_item_fn_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) xml:InitWindow ("file_item:fd",0,ctrl) self.file_item_fd_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) self.form = xml:InitStatic ("form", self) xml:InitTextWnd ("form:caption", self.form) self.editbox = xml:InitEditBox("form:edit", self.form) self:Register (self.editbox, "edit_filename") xml:InitFrame ("form:list_frame", self.form) self.list_box = xml:InitListBox ("form:list", self.form) self.list_box:ShowSelectedItem (true) self:Register (self.list_box, "list_window") ctrl = xml:Init3tButton ("form:btn_save", self.form) self:Register (ctrl, "button_ok") ctrl = xml:Init3tButton ("form:btn_delete", self.form) self:Register (ctrl, "button_del") ctrl = xml:Init3tButton ("form:btn_cancel", self.form) self:Register (ctrl, "button_cancel") self.message_box = CUIMessageBoxEx() self:Register (self.message_box,"message_box") self.mbox_mode = 0 end function UISaveDialog:InitCallBacks() -- main frame buttons self:AddCallback("button_ok", ui_events.BUTTON_CLICKED, self.OnButton_ok_clicked, self) self:AddCallback("button_cancel", ui_events.BUTTON_CLICKED, self.OnButton_cancel_clicked, self) self:AddCallback("button_del", ui_events.BUTTON_CLICKED, self.OnButton_del_clicked, self) self:AddCallback("message_box", ui_events.MESSAGE_BOX_YES_CLICKED, self.OnMsgYes, self) self:AddCallback("list_window", ui_events.LIST_ITEM_CLICKED, self.OnListItemClicked, self) end function UISaveDialog:OnListItemClicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if item==nil then return end local item_text = item.fn:GetText() self.editbox:SetText (item_text) end function UISaveDialog:OnMsgYes() if self.mbox_mode == 1 then self:SaveFile(self.new_save) self.owner:ShowDialog(true) self:HideDialog() self.owner:Show(true) elseif self.mbox_mode == 2 then self:delete_selected_file() end end function UISaveDialog:OnButton_del_clicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if item == nil then return end self.mbox_mode = 2 self.message_box:InitMessageBox("message_box_delete_file_name") self.message_box:ShowDialog(true) end function UISaveDialog:delete_selected_file() if self.list_box:GetSize()==0 then return end local index = self.list_box:GetSelectedIndex() if index == -1 then return end local item = self.list_box:GetItemByIndex(index) local filename = get_full_save_name(item.fn:GetText()) ui_load_dialog.delete_save_game(filename) self.list_box:RemoveItem(item) self:OnListItemClicked() end function UISaveDialog:OnButton_ok_clicked() -- prepare message box -- Get file name self.new_save = self.editbox:GetText() local illegal = { [[/]],[[%.]],[[\]],[[%%]],[[%?]],[[%*]],[[:]],[[|]],[["]],[[<]],[[>]] } for i=1,#illegal do if (string.find(self.new_save,illegal[i])) then self.mbox_mode = 0 self.message_box:InitMessageBox("message_box_illegal_file_name") self.message_box:ShowDialog(true) return end end -- check for empty name if string.len(self.new_save) == 0 then self.mbox_mode = 0 self.message_box:InitMessageBox("message_box_empty_file_name") self.message_box:ShowDialog(true) return end -- check for match name local f = getFS() --local flist = f:file_list_open("$game_saves$",FS.FS_ListFiles) local file_struct = f:exist("$game_saves$", get_full_save_name(self.new_save) .. saved_game_extension ) if file_struct ~= nil then self.mbox_mode = 1 self.message_box:InitMessageBox("message_box_file_already_exist") self.message_box:ShowDialog(true) --flist:Free() return end --flist:Free() self:SaveFile(self.new_save) self.owner:ShowDialog(true) self:HideDialog() self.owner:Show(true) end function UISaveDialog:OnButton_cancel_clicked() self.owner:ShowDialog(true) self:HideDialog() self.owner:Show(true) end function UISaveDialog:OnKeyboard(dik, keyboard_action) --virtual function CUIScriptWnd.OnKeyboard(self,dik,keyboard_action) local bind = dik_to_bind(dik) if bind == key_bindings.kQUIT then self:OnButton_cancel_clicked() else if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then if (dik == DIK_keys.DIK_RETURN) then self:OnButton_ok_clicked() elseif (dik == DIK_keys.DIK_DELETE) then self:OnButton_del_clicked() end end end return true end function UISaveDialog:AddItemToList(file_name, date_time) file_name = hide_profile_prefix(file_name) local _itm = save_item(self.file_item_main_sz.y) _itm:SetWndSize (self.file_item_main_sz) _itm.fn:SetWndPos (vector2():set(0,0)) _itm.fn:SetWndSize (self.file_item_fn_sz) _itm.fn:SetText (file_name) _itm.fage = _itm:AddTextField(date_time, self.file_item_fd_sz.x) _itm.fage:SetFont (GetFontLetterica16Russian()) _itm.fage:SetWndPos (vector2():set(self.file_item_fn_sz.x+4, 0)) self.list_box:AddExistingItem(_itm) end function UISaveDialog:SaveFile(fileName) if nil~= fileName then -- clear excess corpses everytime player saves --release_body_manager.get_release_body_manager():clear(nil,nil,true) exec_console_cmd("save " .. fileName) end end