Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/rttrConfig/src/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace folders {
constexpr auto mbob = "<RTTR_GAME>/DATA/MBOB"; // nation graphics
constexpr auto music = "<RTTR_RTTR>/MUSIC";
constexpr auto playlists = "<RTTR_USERDATA>/playlists";
constexpr auto addonPresets = "<RTTR_USERDATA>/PRESETS";
constexpr auto replays = "<RTTR_USERDATA>/REPLAYS";
constexpr auto save = "<RTTR_USERDATA>/SAVE";
constexpr auto screenshots = "<RTTR_USERDATA>/screenshots";
Expand Down
9 changes: 5 additions & 4 deletions libs/s25main/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ IngameWindow& WindowManager::DoShow(std::unique_ptr<IngameWindow> window, bool m

SetToolTip(nullptr, "");

// All windows are inserted before the first modal window (shown behind)
auto itModal = helpers::find_if(windows, [](const auto& curWnd) { return curWnd->IsModal(); });
// Note that if there is no other modal window it will be put at the back which is what we want
auto& result = **windows.emplace(itModal, std::move(window));
// New modal window goes on top, non-modal window goes behind all modals
const auto itInsert = window->IsModal() ?
windows.end() :
helpers::find_if(windows, [](const auto& curWnd) { return curWnd->IsModal(); });
auto& result = **windows.emplace(itInsert, std::move(window));

// Make the new window active (special cases handled in the function)
SetActiveWindow(result);
Expand Down
36 changes: 28 additions & 8 deletions libs/s25main/controls/ctrlEdit.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later

#include "ctrlEdit.h"
#include "CollisionDetection.h"
#include "RTTR_Assert.h"
#include "ctrlTextDeepening.h"
#include "driver/MouseCoords.h"
#include "drivers/VideoDriverWrapper.h"
#include "helpers/containerUtils.h"
#include "ogl/FontStyle.h"
#include "ogl/glFont.h"
#include "s25util/StringConversion.h"
#include "s25util/fileFuncs.h"
#include <s25util/utf8.h>
#include <boost/algorithm/string/trim.hpp>
#include <boost/nowide/detail/utf.hpp>
#include <numeric>

Expand All @@ -33,8 +36,10 @@ ctrlEdit::ctrlEdit(Window* parent, unsigned id, const DrawPoint& pos, const Exte
void ctrlEdit::SetText(const std::string& text)
{
text_ = s25util::utf8to32(text);
if(numberOnly_)
if(editType_ == EditType::Number)
helpers::erase_if(text_, [](char32_t c) { return c < '0' || c > '9'; });
if(editType_ == EditType::Filename)
helpers::erase_if(text_, [](char32_t c) { return !isValidFileNameChar(c); });
if(maxLength_ > 0 && text_.size() > maxLength_)
text_.resize(maxLength_);

Expand All @@ -54,6 +59,24 @@ std::string ctrlEdit::GetText() const
return s25util::utf32to8(text_);
}

GetFileNameResult ctrlEdit::GetFileName(const std::string& ext) const
{
RTTR_Assert(editType_ == EditType::Filename);

std::string name = GetText();
const auto isSpace = [](char c) { return c == ' '; };
boost::algorithm::trim_left_if(name, isSpace);
if(ext.empty())
boost::algorithm::trim_right_if(name, isSpace);
if(name.empty())
return {FileNameStatus::Empty, {}};
if(!ext.empty())
name += ext;
if(!isValidFileName(name))
return {FileNameStatus::Invalid, {}};
return {FileNameStatus::Valid, std::move(name)};
}

void ctrlEdit::SetFocus(bool focus)
{
if(focus_ != focus)
Expand Down Expand Up @@ -167,8 +190,9 @@ void ctrlEdit::Draw_()
*/
void ctrlEdit::AddChar(char32_t c)
{
// Number-only text fields accept numbers only ;)
if(numberOnly_ && (c < '0' || c > '9'))
if(editType_ == EditType::Number && (c < '0' || c > '9'))
return;
if(editType_ == EditType::Filename && !isValidFileNameChar(c))
return;

if(maxLength_ > 0 && text_.size() >= maxLength_)
Expand Down Expand Up @@ -244,10 +268,6 @@ bool ctrlEdit::Msg_KeyDown(const KeyEvent& ke)
switch(ke.kt)
{
default: return false;
// Wird bereits über Char geliefert !!
case KeyType::Space: // Leertaste
AddChar(0x20);
break;

case KeyType::Left: // Cursor nach Links
// Blockweise nach links, falls Strg gedrückt
Expand Down
27 changes: 25 additions & 2 deletions libs/s25main/controls/ctrlEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@
#pragma once

#include "Window.h"
#include <string>

struct MouseCoords;
class glFont;
class ctrlTextDeepening;
struct KeyEvent;

enum class EditType
{
Text,
Number,
Filename
};

enum class FileNameStatus
{
Empty,
Invalid,
Valid
};
struct GetFileNameResult
{
FileNameStatus status;
std::string name; // only set when status == Valid
};

class ctrlEdit : public Window
{
public:
Expand All @@ -21,11 +41,14 @@ class ctrlEdit : public Window
void SetText(unsigned text);

std::string GetText() const;
/// Trims leading whitespace (and trailing whitespace only if ext is empty), appends a non-empty ext,
/// validates; returns Empty/Invalid/Valid with filename. Requires EditType::Filename.
GetFileNameResult GetFileName(const std::string& ext = "") const;
void SetFocus(bool focus = true);
bool HasFocus() const { return focus_; }
void SetDisabled(bool disabled = true) { this->isDisabled_ = disabled; }
void SetNotify(bool notify = true) { this->notify_ = notify; }
void SetNumberOnly(const bool activated) { this->numberOnly_ = activated; }
void SetType(EditType type) { this->editType_ = type; }

void Resize(const Extent& newSize) override;

Expand Down Expand Up @@ -58,5 +81,5 @@ class ctrlEdit : public Window
unsigned cursorOffsetX_ = 0;
unsigned viewStart_ = 0;

bool numberOnly_ = false;
EditType editType_ = EditType::Text;
};
4 changes: 2 additions & 2 deletions libs/s25main/desktops/dskOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ dskOptions::dskOptions() : Desktop(LOADER.GetImageN("setup013", 0))
groupCommon->AddText(ID_txtPort, curPos, _("Local Port:"), COLOR_YELLOW, FontStyle{}, NormalFont);
ctrlEdit* edtPort =
groupCommon->AddEdit(ID_edtPort, curPos + ctrlOffset, ctrlSize, TextureColor::Grey, NormalFont, 15);
edtPort->SetNumberOnly(true);
edtPort->SetType(EditType::Number);
edtPort->SetText(SETTINGS.server.localPort);
curPos.y += rowHeight;

Expand All @@ -266,7 +266,7 @@ dskOptions::dskOptions() : Desktop(LOADER.GetImageN("setup013", 0))
proxy->SetText(SETTINGS.proxy.hostname);
proxy =
groupCommon->AddEdit(ID_edtProxyPort, curPos + ctrlOffset2, Extent(50, 22), TextureColor::Grey, NormalFont, 5);
proxy->SetNumberOnly(true);
proxy->SetType(EditType::Number);
proxy->SetText(SETTINGS.proxy.port);
curPos.y += rowHeight;

Expand Down
1 change: 1 addition & 0 deletions libs/s25main/gameData/const_gui_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum GUI_ID : unsigned
{
CGI_ACTION,
CGI_ADDONS,
CGI_ADDON_PRESETS,
CGI_AI_DEBUG,
CGI_BUILDINGS,
CGI_BUILDINGSPRODUCTIVITY,
Expand Down
Loading