<commit_before>/*
* Polygon-4
* Copyright (C) 2015 lzwdgc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Polygon4.h"
#include "InfoTreeView.h"

#include "Game/P4Engine.h"

#define LOCTEXT_NAMESPACE "InfoTreeView"

TSharedPtr<InfoTreeItem> InfoTreeItem::AddChild(P4InfoTreeItem *P4Item)
{
    TSharedPtr<InfoTreeItem> Item = MakeShareable(new InfoTreeItem());
    Item->P4Item = P4Item;
    Item->Parent = this;
    Item->Build(P4Item);
    Children.Add(Item);
    return Item;
}

void InfoTreeItem::Build(P4InfoTreeItem *Root)
{
    if (!Root)
        return;
    if (Root->children.empty())
        return;
    for (auto &c : Root->children)
        AddChild(c.get());
}

void InfoTreeView::Construct(const FArguments& InArgs)
{
    P4RootItem = InArgs._RootItem;
    RootItem = MakeShareable(new InfoTreeItem());
    RootItem->Build(P4RootItem);

    ParentType::FArguments args;
    args
        .SelectionMode(ESelectionMode::Single)
        .ClearSelectionOnClick(false)
        .OnSelectionChanged(this, &InfoTreeView::OnSelectionChanged)
        .OnGenerateRow(this, &InfoTreeView::OnGenerateRow)
        .OnGetChildren(this, &InfoTreeView::OnGetChildren)
        ;
    if (RootItem.IsValid())
    {
        args
            .TreeItemsSource(&RootItem->Children)
            ;
    }
    ParentType::Construct(args);

    SetExpandedItems(RootItem->Children);
}

void InfoTreeView::Reset(P4InfoTreeItem *P4RootItem)
{
    this->P4RootItem = P4RootItem;
    RootItem = MakeShareable(new InfoTreeItem());
    RootItem->Build(P4RootItem);
    TreeItemsSource = &RootItem->Children;
    SetExpandedItems(RootItem->Children);
    ReGenerateItems(PanelGeometryLastTick);
    RequestTreeRefresh();
}

void InfoTreeView::SetExpandedItems(const TArray<TSharedPtr<InfoTreeItem>> &Items)
{
    for (auto &i : Items)
    {
        SetItemExpansion(i, i->P4Item->expanded);
        SetExpandedItems(i->Children);
    }
}

TSharedRef<ITableRow> InfoTreeView::OnGenerateRow(ListItem InItem, const TSharedRef<STableViewBase>& OwnerTable)
{
    using namespace polygon4;
    using namespace polygon4::detail;

    FText T = InItem->P4Item->text.toFText();
    auto o = InItem->P4Item->object;
    if (o)
    {
        switch (o->getType())
        {
        case EObjectType::JournalRecord:
        {
            JournalRecord *r = (JournalRecord *)o;
            if (r->message)
            {
                polygon4::String s = r->message->title->string;
                T = s.toFText();
            }
        }
        break;
        }
    }

    if (T.IsEmpty())
        T = LOCTEXT("EmptyString", "Empty");

    return SNew(STableRow<ListItem>, OwnerTable)
        [
            SAssignNew(InItem->Widget, STextBlock)
            .Text(T)
            .Font(FSlateFontInfo("Tahoma", 16))
        ];
}

void InfoTreeView::OnGetChildren(ListItem Item, TArray<ListItem>& OutChildren)
{
    OutChildren.Append(Item->Children);
}

void InfoTreeView::OnSelectionChanged(ListItem Item, ESelectInfo::Type SelectInfo)
{
    if (GetVisibility() != EVisibility::Visible)
        return;
    if (!Item.IsValid() || !Item->P4Item)
        return;
    auto bm = GP4Engine()->getBuildingMenu();
    auto p = Item->P4Item;
    if (!p->object)
    {
        bm->getText() = p->text;
        return;
    }
    switch (p->object->getType())
    {
    case polygon4::detail::EObjectType::Message:
        bm->showMessage((polygon4::detail::Message*)p->object);
        break;
    case polygon4::detail::EObjectType::JournalRecord:
        bm->showMessage(((polygon4::detail::JournalRecord*)p->object)->message);
        break;

#define PRINT_DESCRIPTION(t) \
    case polygon4::detail::EObjectType::t: \
        if (((polygon4::detail::t*)p->object)->description) \
            bm->showText(p->object->getName(), ((polygon4::detail::t*)p->object)->description->string); \
        else \
            bm->showText(p->object->getName()); \
        break

        PRINT_DESCRIPTION(Glider);
        PRINT_DESCRIPTION(Good);
        PRINT_DESCRIPTION(Equipment);
        PRINT_DESCRIPTION(Weapon);
        PRINT_DESCRIPTION(Projectile);
        PRINT_DESCRIPTION(Modificator);

#define PRINT_DESCRIPTION_VAR(t, v) \
    case polygon4::detail::EObjectType::t: \
        if (((polygon4::detail::t*)p->object)->v->description) \
            bm->showText(p->object->getName(), ((polygon4::detail::t*)p->object)->v->description->string); \
        else \
            bm->showText(p->object->getName()); \
        break

        PRINT_DESCRIPTION_VAR(ConfigurationEquipment, equipment);
        PRINT_DESCRIPTION_VAR(ConfigurationGood, good);
        PRINT_DESCRIPTION_VAR(ConfigurationProjectile, projectile);
        PRINT_DESCRIPTION_VAR(ConfigurationWeapon, weapon);
        PRINT_DESCRIPTION_VAR(ConfigurationModificator, modificator);

        PRINT_DESCRIPTION_VAR(MapBuildingGlider, glider);<commit_msg>Show quantity of projectiles in glider tree view.<commit_after>/*
* Polygon-4
* Copyright (C) 2015 lzwdgc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Polygon4.h"
#include "InfoTreeView.h"

#include "Game/P4Engine.h"

#define LOCTEXT_NAMESPACE "InfoTreeView"

TSharedPtr<InfoTreeItem> InfoTreeItem::AddChild(P4InfoTreeItem *P4Item)
{
    TSharedPtr<InfoTreeItem> Item = MakeShareable(new InfoTreeItem());
    Item->P4Item = P4Item;
    Item->Parent = this;
    Item->Build(P4Item);
    Children.Add(Item);
    return Item;
}

void InfoTreeItem::Build(P4InfoTreeItem *Root)
{
    if (!Root)
        return;
    if (Root->children.empty())
        return;
    for (auto &c : Root->children)
        AddChild(c.get());
}

void InfoTreeView::Construct(const FArguments& InArgs)
{
    P4RootItem = InArgs._RootItem;
    RootItem = MakeShareable(new InfoTreeItem());
    RootItem->Build(P4RootItem);

    ParentType::FArguments args;
    args
        .SelectionMode(ESelectionMode::Single)
        .ClearSelectionOnClick(false)
        .OnSelectionChanged(this, &InfoTreeView::OnSelectionChanged)
        .OnGenerateRow(this, &InfoTreeView::OnGenerateRow)
        .OnGetChildren(this, &InfoTreeView::OnGetChildren)
        ;
    if (RootItem.IsValid())
    {
        args
            .TreeItemsSource(&RootItem->Children)
            ;
    }
    ParentType::Construct(args);

    SetExpandedItems(RootItem->Children);
}

void InfoTreeView::Reset(P4InfoTreeItem *P4RootItem)
{
    this->P4RootItem = P4RootItem;
    RootItem = MakeShareable(new InfoTreeItem());
    RootItem->Build(P4RootItem);
    TreeItemsSource = &RootItem->Children;
    SetExpandedItems(RootItem->Children);
    ReGenerateItems(PanelGeometryLastTick);
    RequestTreeRefresh();
}

void InfoTreeView::SetExpandedItems(const TArray<TSharedPtr<InfoTreeItem>> &Items)
{
    for (auto &i : Items)
    {
        SetItemExpansion(i, i->P4Item->expanded);
        SetExpandedItems(i->Children);
    }
}

TSharedRef<ITableRow> InfoTreeView::OnGenerateRow(ListItem InItem, const TSharedRef<STableViewBase>& OwnerTable)
{
    using namespace polygon4;
    using namespace polygon4::detail;

    auto T = InItem->P4Item->text;
    auto o = InItem->P4Item->object;
    if (o)
    {
        switch (o->getType())
        {
        case EObjectType::JournalRecord:
        {
            JournalRecord *r = (JournalRecord *)o;
            if (r->message)
            {
                polygon4::String s = r->message->title->string;
                T = s.toFText();
            }
            break;
        }
        case EObjectType::ConfigurationProjectile:
        {
            ConfigurationProjectile *p = (ConfigurationProjectile *)o;
            T += L" (" + std::to_wstring(p->quantity) + L")";
            break;
        }
        default:
            break;
        }
    }

    auto FT = T.toFText();
    if (FT.IsEmpty())
        FT = LOCTEXT("EmptyString", "Empty");

    return SNew(STableRow<ListItem>, OwnerTable)
        [
            SAssignNew(InItem->Widget, STextBlock)
            .Text(FT)
            .Font(FSlateFontInfo("Tahoma", 16))
        ];
}

void InfoTreeView::OnGetChildren(ListItem Item, TArray<ListItem>& OutChildren)
{
    OutChildren.Append(Item->Children);
}

void InfoTreeView::OnSelectionChanged(ListItem Item, ESelectInfo::Type SelectInfo)
{
    if (GetVisibility() != EVisibility::Visible)
        return;
    if (!Item.IsValid() || !Item->P4Item)
        return;
    auto bm = GP4Engine()->getBuildingMenu();
    auto p = Item->P4Item;
    if (!p->object)
    {
        bm->getText() = p->text;
        return;
    }
    switch (p->object->getType())
    {
    case polygon4::detail::EObjectType::Message:
        bm->showMessage((polygon4::detail::Message*)p->object);
        break;
    case polygon4::detail::EObjectType::JournalRecord:
        bm->showMessage(((polygon4::detail::JournalRecord*)p->object)->message);
        break;

#define PRINT_DESCRIPTION(t) \
    case polygon4::detail::EObjectType::t: \
        if (((polygon4::detail::t*)p->object)->description) \
            bm->showText(p->object->getName(), ((polygon4::detail::t*)p->object)->description->string); \
        else \
            bm->showText(p->object->getName()); \
        break

        PRINT_DESCRIPTION(Glider);
        PRINT_DESCRIPTION(Good);
        PRINT_DESCRIPTION(Equipment);
        PRINT_DESCRIPTION(Weapon);
        PRINT_DESCRIPTION(Projectile);
        PRINT_DESCRIPTION(Modificator);

#define PRINT_DESCRIPTION_VAR(t, v) \
    case polygon4::detail::EObjectType::t: \
    do { \
        auto o = (polygon4::detail::t*)p->object; \
        if (o->v->description) \
            bm->showText(o->getName(), o->v->description->string); \
        else \
            bm->showText(o->getName()); \
        break; \
    } while (0)

        PRINT_DESCRIPTION_VAR(ConfigurationEquipment, equipment);
        PRINT_DESCRIPTION_VAR(ConfigurationGood, good);
        PRINT_DESCRIPTION_VAR(ConfigurationProjectile, projectile);
        PRINT_DESCRIPTION_VAR(ConfigurationWeapon, weapon);
        PRINT_DESCRIPTION_VAR(ConfigurationModificator, modificator);

        PRINT_DESCRIPTION_VAR(MapBuildingGlider, glider);
{'fraction_non_alphanumeric': 0.1269883351007423, 'fraction_numerical': 0.007423117709437964, 'mean_word_length': 3.041785714285714, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4884833', 'n_tokens_mistral': 3514, 'n_tokens_neox': 3416, 'n_words': 684}