16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-22 03:29:19 +02:00
This commit is contained in:
milek7
2019-08-09 00:45:38 +02:00
parent 540b21113e
commit 1626864bfe
14 changed files with 493 additions and 475 deletions

View File

@@ -2,40 +2,8 @@
#include "launcher/vehicle_picker.h"
#include "renderer.h"
GLuint ui::deferred_image::get() const
{
if (!path.empty()) {
image = GfxRenderer.Fetch_Texture(path, true);
path.clear();
}
if (image != null_handle) {
opengl_texture &tex = GfxRenderer.Texture(image);
tex.create();
if (tex.is_ready)
return tex.id;
}
return -1;
}
ui::deferred_image::operator bool() const
{
return image != null_handle || !path.empty();
}
glm::ivec2 ui::deferred_image::size() const
{
if (image != null_handle) {
opengl_texture &tex = GfxRenderer.Texture(image);
return glm::ivec2(tex.width(), tex.height());
}
return glm::ivec2();
}
ui::vehiclepicker_panel::vehiclepicker_panel()
: ui_panel(STR("Select vehicle"), true)
: ui_panel(STR("Select vehicle"), false)
{
bank.scan_textures();
}
@@ -64,7 +32,7 @@ void ui::vehiclepicker_panel::render()
{ vehicle_type::unknown, STRN("Unknown") }
};
if (ImGui::Begin(m_name.c_str())) {
if (ImGui::Begin(m_name.c_str(), &is_open)) {
ImGui::Columns(3);
ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.0f, 0.5f));
@@ -83,7 +51,7 @@ void ui::vehiclepicker_panel::render()
ImGui::EndChild();
ImGui::NextColumn();
ImGui::TextUnformatted("Group by: ");
ImGui::TextUnformatted(STR_C("Group by: "));
ImGui::SameLine();
if (ImGui::RadioButton(STR_C("type"), !display_by_groups))
display_by_groups = false;
@@ -193,7 +161,7 @@ void ui::vehiclepicker_panel::render()
auto skin = skinset_list[i];
std::string label = skin->skins[0].stem().string();
if (selectable_image(label.c_str(), skin == selected_skinset, &skin->mini))
if (selectable_image(label.c_str(), skin == selected_skinset, &skin->mini, true))
selected_skinset = skin;
}
}
@@ -202,7 +170,7 @@ void ui::vehiclepicker_panel::render()
ImGui::PopStyleVar();
}
ImGui::End();
/*
if (!lastdef)
return;
@@ -244,12 +212,14 @@ void ui::vehiclepicker_panel::render()
}
ImGui::End();
*/
}
bool ui::vehiclepicker_panel::selectable_image(const char *desc, bool selected, const deferred_image* image)
bool ui::vehiclepicker_panel::selectable_image(const char *desc, bool selected, const deferred_image* image, bool pickable)
{
bool ret = ImGui::Selectable(desc, selected, 0, ImVec2(0, 30));
ImGui::SetItemAllowOverlap();
if (pickable)
ImGui::SetItemAllowOverlap();
if (!image)
return ret;
@@ -260,211 +230,22 @@ bool ui::vehiclepicker_panel::selectable_image(const char *desc, bool selected,
float width = 30.0f / size.y * size.x;
ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);
ImGui::Image(reinterpret_cast<void*>(tex), ImVec2(width, 30), ImVec2(0, 1), ImVec2(1, 0));
lastdef = image;
ImGui::PushID((void*)image);
ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);
ImGui::InvisibleButton(desc, ImVec2(width, 30));
if (pickable) {
ImGui::PushID((void*)image);
ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);
ImGui::InvisibleButton(desc, ImVec2(width, 30));
if (ImGui::BeginDragDropSource()) {
ImGui::Image(reinterpret_cast<void*>(tex), ImVec2(width, 30), ImVec2(0, 1), ImVec2(1, 0));
if (ImGui::BeginDragDropSource()) {
ImGui::Image(reinterpret_cast<void*>(tex), ImVec2(width, 30), ImVec2(0, 1), ImVec2(1, 0));
ImGui::SetDragDropPayload("skin", "aaaa", 5);
ImGui::EndDragDropSource();
ImGui::SetDragDropPayload("skin", "aaaa", 5);
ImGui::EndDragDropSource();
}
ImGui::PopID();
}
ImGui::PopID();
}
return ret;
}
void ui::vehicles_bank::scan_textures()
{
for (auto &f : std::filesystem::recursive_directory_iterator("dynamic")) {
if (f.path().filename() != "textures.txt")
continue;
std::fstream stream(f.path(), std::ios_base::binary | std::ios_base::in);
ctx_path = f.path().parent_path();
std::string line;
while (std::getline(stream, line))
{
if (line.size() < 3)
continue;
if (line.back() == '\r')
line.pop_back();
parse_entry(line);
}
}
}
void ui::vehicles_bank::parse_entry(const std::string &line)
{
std::string content;
std::string comments;
size_t pos = line.find("//");
if (pos != -1)
comments = line.substr(pos);
content = line.substr(0, pos);
std::istringstream stream(content);
std::string target;
std::getline(stream, target, '=');
std::string param;
while (std::getline(stream, param, '=')) {
if (line[0] == '!')
parse_category_entry(param);
else if (line[0] == '*' && line[1] == '*')
parse_texture_rule(target.substr(2), param);
else if (line[0] == '*')
parse_coupling_rule(target.substr(1), param);
else if (line[0] != '@')
parse_texture_info(target, param, comments);
}
}
void ui::vehicles_bank::parse_category_entry(const std::string &param)
{
static std::unordered_map<char, vehicle_type> type_map = {
{ 'e', vehicle_type::electric_loco },
{ 's', vehicle_type::diesel_loco },
{ 'p', vehicle_type::steam_loco },
{ 'a', vehicle_type::railcar },
{ 'z', vehicle_type::emu },
{ 'r', vehicle_type::utility },
{ 'd', vehicle_type::draisine },
{ 't', vehicle_type::tram },
{ 'c', vehicle_type::truck },
{ 'b', vehicle_type::bus },
{ 'o', vehicle_type::car },
{ 'h', vehicle_type::man },
{ 'f', vehicle_type::animal },
};
ctx_type = vehicle_type::unknown;
std::istringstream stream(param);
std::string tok;
std::getline(stream, tok, ',');
if (tok.size() < 1)
return;
auto it = type_map.find(tok[0]);
if (it != type_map.end())
ctx_type = it->second;
else if (tok[0] >= 'A' && tok[0] <= 'Z')
ctx_type = vehicle_type::carriage;
std::string mini;
std::getline(stream, mini, ',');
category_icons.emplace(ctx_type, "textures/mini/" + ToLower(mini));
}
void ui::vehicles_bank::parse_texture_info(const std::string &target, const std::string &param, const std::string &comment)
{
std::istringstream stream(param);
std::string model, mini, miniplus;
std::getline(stream, model, ',');
std::getline(stream, mini, ',');
std::getline(stream, miniplus, ',');
skin_set set;
set.group = mini;
if (!mini.empty())
group_icons.emplace(mini, std::move(deferred_image("textures/mini/" + ToLower(mini))));
if (!miniplus.empty())
set.mini = std::move(deferred_image("textures/mini/" + ToLower(miniplus)));
std::istringstream tex_stream(target);
std::string texture;
while (std::getline(tex_stream, texture, '|')) {
auto path = ctx_path;
path.append(texture);
set.skins.push_back(path);
}
auto vehicle = get_vehicle(model);
group_map[set.group].insert(vehicle);
vehicle->matching_skinsets.push_back(std::move(set));
}
void ui::vehicles_bank::parse_coupling_rule(const std::string &target, const std::string &param)
{
if (param == "?")
return;
std::istringstream stream(param);
int coupling_flag;
stream >> coupling_flag;
stream.ignore(1000, ',');
std::string connected;
std::getline(stream, connected, ',');
std::string param1, param2;
std::getline(stream, param1, ',');
std::getline(stream, param2, ',');
coupling_rule rule;
rule.coupled_vehicle = get_vehicle(target);
rule.coupling_flag = coupling_flag;
get_vehicle(connected)->coupling_rules.push_back(rule);
}
void ui::vehicles_bank::parse_texture_rule(const std::string &target, const std::string &param)
{
std::istringstream stream(param);
std::string prev;
std::getline(stream, prev, ',');
std::string replace_rule;
while (std::getline(stream, replace_rule, ',')) {
if (replace_rule == "-")
break;
std::istringstream rule_stream(replace_rule);
std::string src, dst;
std::getline(rule_stream, src, '-');
std::getline(rule_stream, dst, '-');
texture_rule rule;
rule.previous_vehicle = get_vehicle(prev);
rule.replace_rules.emplace_back(src, dst);
get_vehicle(target)->texture_rules.push_back(rule);
}
}
std::shared_ptr<ui::vehicle_desc> ui::vehicles_bank::get_vehicle(const std::string &name)
{
auto path = ctx_path;
path.append(name);
auto it = vehicles.find(path);
if (it != vehicles.end()) {
return it->second;
}
else {
auto desc = std::make_shared<vehicle_desc>();
desc->type = ctx_type;
desc->path = path;
vehicles.emplace(path, desc);
return desc;
}
}