#pragma once #include #include #include #include #include #include #include #include namespace settlement::viewer { constexpr int title_continue_id = 1100; constexpr int title_new_id = 1101; constexpr int title_browse_id = 1102; constexpr int title_coop_id = 1103; constexpr int title_exit_id = 1104; constexpr int title_saves_id = 1105; constexpr int title_open_id = 1106; inline std::filesystem::path saves_directory() { const DWORD length = GetEnvironmentVariableW(L"LOCALAPPDATA", nullptr, 0); if (length == 0) return {}; std::wstring base(length, L'\0'); const DWORD copied = GetEnvironmentVariableW(L"LOCALAPPDATA", base.data(), length); if (copied == 0 || copied >= length) return {}; base.resize(copied); return std::filesystem::path(base) / L"Settlement" / L"Saves"; } class TitleScreen { public: ~TitleScreen() { if (list_background_) DeleteObject(list_background_); if (font_) DeleteObject(font_); } void create(HWND owner) { constexpr const wchar_t* names[] = { L"Продолжить", L"Новая колония", L"Открыть файл…", L"Кооператив…", L"Выход" }; const auto instance = GetModuleHandleW(nullptr); for (std::size_t i = 0; i < buttons_.size(); ++i) { buttons_[i] = CreateWindowExW(0, L"BUTTON", names[i], WS_CHILD | WS_TABSTOP | BS_OWNERDRAW, 0, 0, 0, 0, owner, id(title_continue_id + static_cast(i)), instance, nullptr); } saves_ = CreateWindowExW(WS_EX_CLIENTEDGE, L"LISTBOX", L"", WS_CHILD | WS_TABSTOP | WS_VSCROLL | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT, 0, 0, 0, 0, owner, id(title_saves_id), instance, nullptr); open_ = CreateWindowExW(0, L"BUTTON", L"Загрузить выбранное", WS_CHILD | WS_TABSTOP | BS_OWNERDRAW, 0, 0, 0, 0, owner, id(title_open_id), instance, nullptr); list_background_ = CreateSolidBrush(RGB(44, 63, 53)); font_ = CreateFontW(-16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI"); const auto font = reinterpret_cast(font_ ? font_ : GetStockObject(DEFAULT_GUI_FONT)); for (const auto button : buttons_) SendMessageW(button, WM_SETFONT, font, FALSE); SendMessageW(saves_, WM_SETFONT, font, FALSE); SendMessageW(open_, WM_SETFONT, font, FALSE); } void reload_saves() { const auto folder = saves_directory(); saves.clear(); SendMessageW(saves_, LB_RESETCONTENT, 0, 0); if (folder.empty()) return; std::error_code error; for (auto entry = std::filesystem::directory_iterator(folder, error); !error && entry != std::filesystem::directory_iterator{}; entry.increment(error)) { std::error_code file_error; if (!entry->is_regular_file(file_error)) continue; if (_wcsicmp(entry->path().extension().c_str(), L".settlement") != 0) continue; const auto written = entry->last_write_time(file_error); if (!file_error) saves.push_back({entry->path(), written}); } std::sort(saves.begin(), saves.end(), [](const auto& a, const auto& b) { if (a.written != b.written) return a.written > b.written; return a.path < b.path; }); for (const auto& entry : saves) { const auto name = entry.path.stem().wstring(); SendMessageW(saves_, LB_ADDSTRING, 0, reinterpret_cast(name.c_str())); } if (!saves.empty()) SendMessageW(saves_, LB_SETCURSEL, 0, 0); } void show(bool visible) const { const int flags = visible ? SW_SHOWNA : SW_HIDE; for (const auto button : buttons_) ShowWindow(button, flags); ShowWindow(saves_, flags); ShowWindow(open_, flags); } void update(bool can_resume, bool offline) const { EnableWindow(buttons_[0], can_resume || (offline && !saves.empty())); EnableWindow(buttons_[title_new_id - title_continue_id], offline); EnableWindow(buttons_[title_browse_id - title_continue_id], offline); EnableWindow(open_, offline && selected_index() < saves.size()); EnableWindow(saves_, offline); } HBRUSH paint_list_background(HWND control, HDC dc) const { if (control != saves_ || !list_background_) return nullptr; SetTextColor(dc, RGB(246, 237, 210)); SetBkColor(dc, RGB(44, 63, 53)); return list_background_; } [[nodiscard]] std::optional selected_save() const { const auto selected = selected_index(); return selected < saves.size() ? std::optional{saves[selected].path} : std::nullopt; } [[nodiscard]] std::optional latest_save() const { return saves.empty() ? std::nullopt : std::optional{saves.front().path}; } void layout(int width, int height) const { const RECT panel = bounds(width, height); for (std::size_t i = 0; i < buttons_.size(); ++i) { position(buttons_[i], panel.left + 28, panel.top + 118 + static_cast(i) * 40, 252, 33); } position(saves_, panel.left + 318, panel.top + 146, panel.right - panel.left - 346, 118); position(open_, panel.left + 318, panel.top + 274, panel.right - panel.left - 346, 33); } void draw(HDC dc, int width, int height, const std::wstring& notice) const { shade(dc, {0, 0, width, height}, RGB(24, 38, 34)); const auto old_pen = SelectObject(dc, GetStockObject(NULL_PEN)); const auto distant = CreateSolidBrush(RGB(32, 53, 44)); const auto near_hills = CreateSolidBrush(RGB(41, 66, 50)); const auto old_brush = SelectObject(dc, distant); const POINT hills[] = {{0, height}, {0, height * 5 / 8}, {width / 4, height / 4}, {width * 5 / 9, height * 5 / 8}, {width * 5 / 6, height / 4}, {width, height / 2}, {width, height}}; Polygon(dc, hills, static_cast(std::size(hills))); SelectObject(dc, near_hills); const POINT foreground[] = {{0, height}, {0, height * 7 / 8}, {width / 3, height / 2}, {width * 3 / 4, height * 7 / 8}, {width, height * 3 / 5}, {width, height}}; Polygon(dc, foreground, static_cast(std::size(foreground))); SelectObject(dc, old_brush); SelectObject(dc, old_pen); DeleteObject(near_hills); DeleteObject(distant); const RECT panel = bounds(width, height); shade(dc, panel, RGB(35, 50, 43)); shade(dc, {panel.left, panel.top, panel.right, panel.top + 2}, RGB(179, 150, 92)); const auto face = CreateFontW(-46, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_ROMAN, L"Georgia"); const auto font = SelectObject(dc, face ? face : GetStockObject(DEFAULT_GUI_FONT)); const auto mode = SetBkMode(dc, TRANSPARENT); const auto ink = SetTextColor(dc, RGB(248, 235, 204)); RECT heading{panel.left + 27, panel.top + 19, panel.right - 27, panel.top + 76}; DrawTextW(dc, L"Settlement", -1, &heading, DT_SINGLELINE | DT_VCENTER); SelectObject(dc, font_ ? font_ : GetStockObject(DEFAULT_GUI_FONT)); RECT name{panel.left + 30, panel.top + 78, panel.right - 28, panel.top + 99}; DrawTextW(dc, L"Первое поселение", -1, &name, DT_SINGLELINE | DT_VCENTER); RECT saves_label{panel.left + 318, panel.top + 117, panel.right - 27, panel.top + 140}; DrawTextW(dc, saves.empty() ? L"Сохранения · список пуст" : L"Сохранения", -1, &saves_label, DT_SINGLELINE | DT_VCENTER); if (!notice.empty()) { RECT information{panel.left + 318, panel.top + 311, panel.right - 27, panel.bottom - 11}; DrawTextW(dc, notice.c_str(), -1, &information, DT_WORDBREAK | DT_NOPREFIX); } SetTextColor(dc, ink); SetBkMode(dc, mode); SelectObject(dc, font); if (face) DeleteObject(face); } static bool draw_button(const DRAWITEMSTRUCT& item) { if (item.CtlID < title_continue_id || item.CtlID > title_open_id || item.CtlID == title_saves_id) return false; const bool disabled = (item.itemState & ODS_DISABLED) != 0; const bool accented = (item.itemState & (ODS_SELECTED | ODS_FOCUS)) != 0; const bool primary = item.CtlID == title_new_id && !disabled; shade(item.hDC, item.rcItem, accented ? RGB(90, 105, 70) : (primary ? RGB(69, 89, 60) : RGB(47, 65, 55))); const auto edge = CreateSolidBrush(accented || primary ? RGB(186, 157, 100) : RGB(91, 110, 91)); FrameRect(item.hDC, &item.rcItem, edge); DeleteObject(edge); RECT label = item.rcItem; InflateRect(&label, -12, 0); wchar_t text[96]{}; GetWindowTextW(item.hwndItem, text, static_cast(std::size(text))); const auto face = reinterpret_cast(SendMessageW(item.hwndItem, WM_GETFONT, 0, 0)); const auto font = SelectObject(item.hDC, face ? face : GetStockObject(DEFAULT_GUI_FONT)); const auto mode = SetBkMode(item.hDC, TRANSPARENT); const auto ink = SetTextColor(item.hDC, disabled ? RGB(162, 173, 157) : RGB(248, 240, 217)); DrawTextW(item.hDC, text, -1, &label, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS); SetTextColor(item.hDC, ink); SetBkMode(item.hDC, mode); SelectObject(item.hDC, font); if ((item.itemState & ODS_FOCUS) != 0) { RECT focus = item.rcItem; InflateRect(&focus, -4, -4); DrawFocusRect(item.hDC, &focus); } return true; } private: struct SaveFile { std::filesystem::path path; std::filesystem::file_time_type written; }; static HMENU id(int value) { return reinterpret_cast(static_cast(value)); } static RECT bounds(int width, int height) { constexpr int panel_width = 704; constexpr int panel_height = 365; const int left = (width - panel_width) / 2; const int top = (height - panel_height) / 2; return {left, top, left + panel_width, top + panel_height}; } static void position(HWND control, int x, int y, int width, int height) { SetWindowPos(control, nullptr, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER); } static void shade(HDC dc, RECT area, COLORREF color) { const auto brush = CreateSolidBrush(color); FillRect(dc, &area, brush); DeleteObject(brush); } [[nodiscard]] std::size_t selected_index() const { const auto selection = SendMessageW(saves_, LB_GETCURSEL, 0, 0); return selection == LB_ERR ? saves.size() : static_cast(selection); } std::array buttons_{}; HWND saves_ = nullptr; HWND open_ = nullptr; HBRUSH list_background_ = nullptr; HFONT font_ = nullptr; std::vector saves; }; } // namespace settlement::viewer