Skip to content

Commit

Permalink
Merge branch 'feature/SC-allow-disable-blocklist' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ToKiNoBug committed Aug 10, 2024
2 parents 9e22d71 + 145d357 commit a3aed7c
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 87 deletions.
58 changes: 56 additions & 2 deletions SlopeCraft/BlockListDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Created by Joseph on 2024/4/9.
//

#include <QListView>
#include <QFileDialog>
#include <QMessageBox>
#include "BlockListDialog.h"
#include "ui_BlockListDialog.h"
#include <QListView>
#include "SCWind.h"

class BLD_block_list_provider : public QAbstractListModel {
Expand Down Expand Up @@ -210,7 +212,7 @@ class BLD_block_info_provider : public QAbstractTableModel {
};

BlockListDialog::BlockListDialog(SCWind *parent, BlockListManager *blm)
: QDialog{parent}, ui{new Ui::BlockListDialog} {
: QDialog{parent}, ui{new Ui::BlockListDialog}, block_list_manager{blm} {
this->ui->setupUi(this);

{
Expand Down Expand Up @@ -303,4 +305,56 @@ void BlockListDialog::update_info(
this->ui->le_id_old->setText(blk->getIdOld());
this->ui->le_name_cn->setText(QString::fromUtf8(blk->getNameZH()));
this->ui->le_name_en->setText(QString::fromUtf8(blk->getNameEN()));
}

void BlockListDialog::on_pb_add_block_list_clicked() noexcept {
const auto files = QFileDialog::getOpenFileNames(this, tr("选择方块列表"),
this->prev_dir, "*.zip");
if (files.empty()) {
return;
}
this->prev_dir = QFileInfo{files.first()}.absoluteDir().path();

for (auto &file : files) {
this->block_list_manager->add_blocklist(file);
}
this->block_list_provider->dataChanged({}, {});
}

void BlockListDialog::on_pb_remove_block_list_clicked() noexcept {
const auto selected_indices =
this->ui->lv_block_lists->selectionModel()->selectedIndexes();
std::vector<QString> names;
for (auto &qmi : selected_indices) {
if (not qmi.isValid()) {
continue;
}
names.emplace_back(
this->block_list_provider->available_block_lists()[qmi.row()].first);
}

int num_lists = 0;
size_t remove_counter = 0;
for (auto &name : names) {
if (name == "FixedBlocks.zip") {
QMessageBox::warning(this, tr("不能删除基础方块列表"),
tr("FixedBlocks.zip 是基础方块列表,不允许移除。"));
continue;
}
auto res = this->block_list_manager->remove_blocklist(name);
if (not res) {
QMessageBox::warning(this, tr("删除方块列表 %1 失败").arg(name),
res.error());
} else {
remove_counter += res.value();
num_lists++;
}
}
if (num_lists > 0) {
QMessageBox::information(this, tr("删除方块列表成功"),
tr("删除了 %1 个方块列表,移除了 %2 个方块")
.arg(num_lists)
.arg(remove_counter));
}
this->block_list_provider->dataChanged({}, {});
}
7 changes: 7 additions & 0 deletions SlopeCraft/BlockListDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ class BlockListDialog : public QDialog {
BLD_block_list_provider* block_list_provider{nullptr};
BLD_block_provider* block_provider{nullptr};
BLD_block_info_provider* block_info_provider{nullptr};
QString prev_dir;

BlockListManager* const block_list_manager;

void update_info(const SlopeCraft::mc_block_interface*) noexcept;

public:
explicit BlockListDialog(SCWind* parent, BlockListManager* blm);
~BlockListDialog();

private slots:
void on_pb_add_block_list_clicked() noexcept;
void on_pb_remove_block_list_clicked() noexcept;
};

#endif // SLOPECRAFT_BLOCKLISTDIALOG_H
93 changes: 50 additions & 43 deletions SlopeCraft/BlockListDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,49 @@
<property name="modal">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="1,1,2">
<item row="0" column="0" rowspan="2">
<widget class="QListView" name="lv_block_lists">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<layout class="QGridLayout" name="gridLayout_2" rowstretch="1,0,0" columnstretch="1,1,2">
<item row="2" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>0</number>
</property>
</widget>
<item>
<widget class="QPushButton" name="pb_add_block_list">
<property name="text">
<string>添加方块列表</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pb_remove_block_list">
<property name="text">
<string>删除方块列表</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pb_ok">
<property name="text">
<string>确定</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<item row="0" column="1" rowspan="2">
<widget class="QListView" name="lv_blocks">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
Expand All @@ -40,7 +71,7 @@
</property>
</widget>
</item>
<item row="0" column="2">
<item row="0" column="2" rowspan="2">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
Expand Down Expand Up @@ -119,39 +150,15 @@
</item>
</layout>
</item>
<item row="2" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>0</number>
<item row="0" column="0" rowspan="2">
<widget class="QListView" name="lv_block_lists">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>若想增加/修改方块列表,增加/删除 blocks 文件夹中的 zip 文件,然后重新启动 SlopeCraft</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pb_ok">
<property name="text">
<string>确定</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
Expand Down
2 changes: 1 addition & 1 deletion SlopeCraft/SCWind_slots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ void SCWind::on_ac_test_blocklist_triggered() noexcept {
basecolor++) {
const auto bcwp = this->ui->blm->basecolorwidget_at(basecolor);
for (const auto &bwp : bcwp->block_widgets()) {
blks.emplace_back(bwp->attachted_block());
blks.emplace_back(bwp->attached_block());
basecolors.emplace_back(basecolor);
}
}
Expand Down
29 changes: 9 additions & 20 deletions SlopeCraftL/structure_3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,10 @@ bool structure_3D_impl::export_litematica(
info.regionname_utf8 = option.region_name_utf8;

{
errorFlag flag = errorFlag::NO_ERROR_OCCUR;
std::string error_string;
const bool ok =
this->schem.export_litematic(filename, info, &flag, &error_string);
auto res = this->schem.export_litematic(filename, info);

if (!ok) {
option.ui.report_error(flag, error_string.c_str());
if (not res) {
option.ui.report_error(res.error().first, res.error().second.c_str());
return false;
}
}
Expand All @@ -245,13 +242,9 @@ bool structure_3D_impl::export_vanilla_structure(
option.ui.report_working_status(workStatus::writingMetaInfo);
option.progressbar.set_range(0, 100 + schem.size(), 0);

errorFlag flag = errorFlag::NO_ERROR_OCCUR;
std::string error_string;
const bool success = schem.export_structure(
filename, option.is_air_structure_void, &flag, &error_string);

if (!success) {
option.ui.report_error(flag, error_string.c_str());
auto res = schem.export_structure(filename, option.is_air_structure_void);
if (not res) {
option.ui.report_error(res.error().first, res.error().second.c_str());
return false;
}

Expand Down Expand Up @@ -279,13 +272,9 @@ bool structure_3D_impl::export_WE_schem(

option.progressbar.set_range(0, 100, 5);

errorFlag flag = errorFlag::NO_ERROR_OCCUR;
std::string error_string;
const bool success =
schem.export_WESchem(filename, info, &flag, &error_string);

if (!success) {
option.ui.report_error(flag, error_string.c_str());
auto res = schem.export_WESchem(filename, info);
if (not res) {
option.ui.report_error(res.error().first, res.error().second.c_str());
return false;
}

Expand Down
65 changes: 52 additions & 13 deletions utilities/BlockListManager/BaseColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ constexpr int basecolor_cols = 3;
static_assert(basecolor_cols >= 1);

void BaseColorWidget::add_block(SlopeCraft::mc_block_interface* ab) noexcept {
this->place_holders.clear();
BlockWidget* bw = new BlockWidget(this, ab);

const int idx = this->blocks.size();
Expand All @@ -47,27 +48,67 @@ void BaseColorWidget::add_block(SlopeCraft::mc_block_interface* ab) noexcept {

void BaseColorWidget::add_placeholders() noexcept {
if (this->blocks.size() <= 0) {
this->place_holders.clear();
return;
}

if (this->blocks.size() < basecolor_cols) {
this->place_holders.clear();
for (int i = this->blocks.size(); i < basecolor_cols; i++) {
QLabel* lb = new QLabel("", this);
auto lb = new QLabel("", this);
// lb->setFrameShape(QLabel::Shape::Box);
dynamic_cast<QGridLayout*>(this->ui->layout_blocks)->addWidget(lb, 0, i);
this->place_holders.emplace_back(lb);
}
return;
}

if (this->blocks.size() % basecolor_cols == 0) {
return;
this->place_holders.clear();
}
/*
const int max_row = this->blocks.size() / basecolor_cols;
}

dynamic_cast<QGridLayout*>(this->ui->layout_blocks)
->addWidget(new QLabel("", this), max_row, basecolor_cols - 1);
*/
void BaseColorWidget::re_arrange_blocks() noexcept {
this->place_holders.clear();
auto layout = dynamic_cast<QGridLayout*>(this->ui->layout_blocks);
for (auto& bw : this->blocks) {
assert(layout->indexOf(bw) >= 0);
layout->removeWidget(bw);
}

constexpr int cols = basecolor_cols;
for (size_t idx = 0; idx < this->blocks.size(); idx++) {
const int col = idx % cols;
const int row = idx / cols;
layout->addWidget(this->blocks[idx], row, col);
}
this->finish_blocks();
}

tl::expected<size_t, QString> BaseColorWidget::remove_blocks(
const std::function<bool(const SlopeCraft::mc_block_interface*)>&
remove_this_block) noexcept {
size_t remove_counter = 0;
for (auto it = this->blocks.begin(); it not_eq this->blocks.end();) {
const bool remove_current = remove_this_block((*it)->attached_block());
if (not remove_current) {
++it;
continue;
}
if (this->blocks.size() <= 1) {
return tl::make_unexpected(
tr("无法删除方块 %1,基色 %2 只拥有 "
"%3个方块,若继续删除,则该基色将没有方块,SlopeCraft 可能崩溃。")
.arg((*it)->text())
.arg(int(this->basecolor), this->blocks.size()));
}

(*it)->deleteLater();
it = this->blocks.erase(it);
remove_counter++;
}
this->re_arrange_blocks();
return remove_counter;
}

constexpr inline bool should_be_disabled(
Expand Down Expand Up @@ -95,8 +136,7 @@ void BaseColorWidget::finish_blocks() noexcept {

for (int idx = 0; idx < (int)this->blocks.size(); idx++) {
this->blocks[idx]->setDisabled(should_be_disabled(
idx,
(SCL_gameVersion)this->blocks[idx]->attachted_block()->getVersion(),
idx, (SCL_gameVersion)this->blocks[idx]->attached_block()->getVersion(),
this->blocks.size(), v));
}
}
Expand Down Expand Up @@ -129,8 +169,7 @@ void BaseColorWidget::when_version_updated(SCL_gameVersion v) noexcept {

for (int idx = 0; idx < int(this->blocks.size()); idx++) {
this->blocks[idx]->setDisabled(should_be_disabled(
idx,
(SCL_gameVersion)this->blocks[idx]->attachted_block()->getVersion(),
idx, (SCL_gameVersion)this->blocks[idx]->attached_block()->getVersion(),
this->blocks.size(), v));
}
if (this->basecolor == 0) { // basecolor 0 (air) must be selected
Expand Down Expand Up @@ -164,7 +203,7 @@ int BaseColorWidget::prefered_block_idx(int checked_idx,
score = 0;

const SCL_gameVersion blk_ver =
(SCL_gameVersion)this->blocks[idx]->attachted_block()->getVersion();
(SCL_gameVersion)this->blocks[idx]->attached_block()->getVersion();

if (blk_ver <= ver) {
score = 100;
Expand Down Expand Up @@ -200,7 +239,7 @@ void BaseColorWidget::select_by_callback(const select_callback_t& fun) {
std::vector<const SlopeCraft::mc_block_interface*> blks;
blks.reserve(this->blocks.size());
for (auto bw : this->blocks) {
blks.emplace_back(bw->attachted_block());
blks.emplace_back(bw->attached_block());
}

const int output = fun(blks);
Expand Down
Loading

0 comments on commit a3aed7c

Please sign in to comment.