Skip to content

Commit

Permalink
Add locations UIMinimapEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
GladiatorVS committed Nov 15, 2024
1 parent 2eff4e1 commit afc4e85
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 35 deletions.
213 changes: 180 additions & 33 deletions src/Editors/xrECore/Editor/UIMinimapEditorForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ UIMinimapEditorForm::UIMinimapEditorForm()
isEdited = false;
ActiveFile.clear();

ReloadLevelsList();

string_path fn = {};
FS.update_path(fn, _game_config_, "game_maps_single.ltx");
ReloadMapInfo(fn);
Expand All @@ -18,13 +20,30 @@ UIMinimapEditorForm::UIMinimapEditorForm()
UIMinimapEditorForm::~UIMinimapEditorForm()
{
for (auto& element : elements)
if (element.texture) element.texture->Release();
if (element.Texture) element.Texture->Release();

if (m_BackgroundTexture)m_BackgroundTexture->Release();
selectedElement = nullptr;

elements.clear();
}

void UIMinimapEditorForm::ReloadLevelsList()
{
levels.clear();
FS_FileSet lst;
if (FS.file_list(lst, _game_levels_, FS_ListFolders | FS_RootOnly)) {
FS_FileSetIt it = lst.begin();
FS_FileSetIt _E = lst.end();
for (; it != _E; it++)
{
xr_string level = it->name;
level.erase(std::remove(level.begin(), level.end(), '\\'), level.end());
levels.push_back(level);
}
}
}

void UIMinimapEditorForm::UnselectAllElements()
{
for (auto& other_element : elements) {
Expand Down Expand Up @@ -117,7 +136,7 @@ void UIMinimapEditorForm::RenderCanvas()
element_screen_size *= m_Zoom;
float handle_size = 5.0f * m_Zoom;

ImGui::GetWindowDrawList()->AddImage(element.texture, element_screen_pos, element_screen_pos + element_screen_size, ImVec2(0,0), ImVec2(1, 1), IM_COL32(255, 255, 255, m_ItemsOpacity));
ImGui::GetWindowDrawList()->AddImage(element.Texture, element_screen_pos, element_screen_pos + element_screen_size, ImVec2(0,0), ImVec2(1, 1), IM_COL32(255, 255, 255, m_ItemsOpacity));

if (element.selected || m_AlwaysDrawBorder)
{
Expand Down Expand Up @@ -226,7 +245,101 @@ void UIMinimapEditorForm::ShowPreview()

ImGui::End();
}
//


void UIMinimapEditorForm::CreateElementPopup()
{
if (ImGui::BeginPopup("Add Location"))
{
ImGui::Text(" = Add Location =");

if (ImGui::Checkbox("Show mp_ levels", &showMPLevels))
{
if (!showMPLevels && CreatingData.name.rfind("mp_", 0) == 0)
CreatingData.name.clear();
}

if (ImGui::BeginCombo("Level Name", CreatingData.name.data()))
{
for (auto level : levels)
{
if (!showMPLevels && level.rfind("mp_", 0) == 0)
continue;

bool used = false;
for (auto element : elements)
{
if (element.name == level)
{
used = true;
break;
}
}

if (used)
continue;

if (ImGui::Selectable(level.c_str(), (level == CreatingData.name)))
{
GetTextureFromLevelLtx(level, CreatingData.TexturePath);
CreatingData.name = level;
}
}

ImGui::EndCombo();
}
ImGui::Text("Level texture:");
ImGui::SameLine();
ImGui::Text(CreatingData.TexturePath.c_str());

ImGui::NewLine();

ImGui::BeginDisabled(CreatingData.TexturePath.empty() || CreatingData.name.empty());

if (ImGui::Button("Add"))
{
Element el_new;
el_new.position = ImVec2(0, 0);
el_new.name = CreatingData.name;
el_new.TexturePath = CreatingData.TexturePath;
el_new.Texture = nullptr;

string_path p;
if (!el_new.TexturePath.empty())
sprintf(p, "%s%s.dds", FS.get_path(_game_textures_)->m_Path, CreatingData.TexturePath.c_str());


if (LoadTexture(el_new, p) != 0)
{
u32 mem = 0;
el_new.Texture = RImplementation.texture_load("ed\\ed_nodata", mem);
el_new.FileSize = ImVec2(512,512);
}
el_new.RenderSize = ImVec2(512, 512);
elements.push_back(el_new);

CreatingData.name.clear();
CreatingData.TexturePath.clear();

isEdited = true;
SelectElement(elements[elements.size() - 1]);
}

ImGui::EndDisabled();

if (ImGui::Button("Clear"))
{
CreatingData.TexturePath.clear();
CreatingData.name.clear();
if (CreatingData.Texture)
CreatingData.Texture->Release();
CreatingData.Texture = nullptr;
}

ImGui::EndPopup();
}
}
void UIMinimapEditorForm::ShowMenu()
{
ImGui::Text((isEdited) ? " [Edited] View" : " View");
Expand Down Expand Up @@ -267,29 +380,13 @@ void UIMinimapEditorForm::ShowMenu()
{
LoadBGClick();
}
if (ImGui::MenuItem("Add Location"))
{
Element test;
//test.name = "Test";
test.texture = nullptr;
test.position = ImVec2(0, 0);
test.FileSize = ImVec2(100, 100);

int res = LoadTexture(test);

if (res == 2)
{
u32 mem = 0;
test.texture = RImplementation.texture_load("ed\\ed_nodata", mem);
}
CreateElementPopup();

if (res != 1)
{
test.RenderSize = test.FileSize;
elements.push_back(test);
isEdited = true;
SelectElement(elements[elements.size() - 1]);
}
if (ImGui::MenuItem("Add Location"))
{
ImGui::OpenPopup("Add Location");

}

ImGui::Separator();
Expand Down Expand Up @@ -365,9 +462,9 @@ void UIMinimapEditorForm::ShowMenu()
auto it = elements.begin();
for (; it != elements.end(); ++it) {
if (&(*it) == selectedElement) {
if (it->texture)
it->texture->Release();
it->texture = nullptr;
if (it->Texture)
it->Texture->Release();
it->Texture = nullptr;

elements.erase(it);
selectedElement = nullptr;
Expand Down Expand Up @@ -397,6 +494,27 @@ void UIMinimapEditorForm::OpenFile()
ReloadMapInfo(fn);
}

//vnedrit v ReloadMapInfo
bool UIMinimapEditorForm::GetTextureFromLevelLtx(const xr_string level_name, xr_string& dest )
{
string_path levelLtx;
sprintf(levelLtx, "%s%s\\level.ltx", FS.get_path("$level$")->m_Path, level_name.c_str());

bool find_result = false;
if (!FS.exist(levelLtx))
return false;

CInifile levelLtxFile(levelLtx, TRUE);

if (!levelLtxFile.line_exist("level_map", "texture"))
return false;

auto textureFile = levelLtxFile.r_string("level_map", "texture");
dest = textureFile;

return true;

}
void UIMinimapEditorForm::ReloadMapInfo(const xr_string& fn)
{
elements.clear();
Expand Down Expand Up @@ -444,10 +562,11 @@ void UIMinimapEditorForm::ReloadMapInfo(const xr_string& fn)

Element el;
el.name = levelName.c_str();
el.texture = nullptr;
el.Texture = nullptr;
el.selected = false;
el.position.x = tmp.x;
el.position.y = tmp.y;
el.TexturePath = "";

el.RenderSize.x = tmp.z - tmp.x;
el.RenderSize.y = tmp.w - tmp.y;
Expand All @@ -464,7 +583,7 @@ void UIMinimapEditorForm::ReloadMapInfo(const xr_string& fn)
if (levelLtxFile.line_exist("level_map", "texture"))
{
auto textureFile = levelLtxFile.r_string("level_map", "texture");

el.TexturePath = textureFile;
sprintf(texturePath, "%s%s.dds", FS.get_path("$game_textures$")->m_Path, textureFile);
}
}
Expand Down Expand Up @@ -512,6 +631,28 @@ void UIMinimapEditorForm::SaveFile(bool saveCurrent)
ltxFile->w_fvector4("global_map", "bound_rect", Fvector4(0.f, 0.f, m_BackgroundRenderSize.x, m_BackgroundRenderSize.y));
}

//old levels check
{
CInifile::Sect& S = ltxFile->r_section("level_maps_single");
CInifile::SectCIt it = S.Data.begin();
CInifile::SectCIt it_e = S.Data.end();

for (; it != it_e; ++it)
{
auto& levelName = it->first;

bool res = false;
for (auto& element : elements)
{
if (levelName == element.name.c_str())
res = true;
}

if (!res)
ltxFile->remove_line("level_maps_single", levelName.c_str());
}
}

for (auto& element : elements)
{
ltxFile->remove_line("level_maps_single", element.name.c_str());
Expand Down Expand Up @@ -569,6 +710,7 @@ void UIMinimapEditorForm::Draw()
ImGui::BeginChild("Canvas", ImVec2(0, 0), true);
RenderCanvas();
ImGui::EndChild();

}


Expand Down Expand Up @@ -631,18 +773,23 @@ int UIMinimapEditorForm::LoadTexture(Element& el, const xr_string texture)
return 2;
}

if (el.texture)
el.texture->Release();
el.texture = nullptr;
if (el.Texture)
el.Texture->Release();
el.Texture = nullptr;

if (texture == "")
{
el.TexturePath = FS.fix_path(fn);
}

el.path = fn;
//el.path = fn;
el.FileSize.x = W;
el.FileSize.y = H;

ID3DTexture2D* pTexture = nullptr;
{
R_CHK(REDevice->CreateTexture(W, H, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, 0));
el.texture = pTexture;
el.Texture = pTexture;
{
D3DLOCKED_RECT rect;
R_CHK(pTexture->LockRect(0, &rect, 0, D3DLOCK_DISCARD));
Expand Down
13 changes: 11 additions & 2 deletions src/Editors/xrECore/Editor/UIMinimapEditorForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class UIMinimapEditorForm:
private:
struct Element {
xr_string name;
xr_string path;
ImVec2 position;
ImVec2 RenderSize;
ImVec2 FileSize;
ImTextureID texture;
ImTextureID Texture;
xr_string TexturePath;
bool selected = false;
bool locked = false;
bool hidden = false;
Expand Down Expand Up @@ -46,12 +46,18 @@ class UIMinimapEditorForm:
void UnselectAllElements();
void SelectElement(Element&);

void CreateElementPopup();

void SaveFile(bool);
void OpenFile();

void ReloadMapInfo(const xr_string& fn);
void ReloadLevelsList();

bool GetTextureFromLevelLtx(const xr_string, xr_string&);

xr_vector<Element> elements;
xr_vector<xr_string> levels;

bool m_AlwaysDrawBorder = false;
bool isDragging = false;
Expand All @@ -68,6 +74,9 @@ class UIMinimapEditorForm:
xr_string ActiveFile;
xr_string ActiveFileShort;
bool isEdited;

Element CreatingData;
bool showMPLevels = false;
private:
ImTextureID m_BackgroundTexture;
ImTextureID m_TextureRemove;
Expand Down

0 comments on commit afc4e85

Please sign in to comment.