2013-02-15 16:10:47 -07:00
|
|
|
#include <algorithm>
|
2014-05-03 03:19:46 -06:00
|
|
|
#include <cctype>
|
|
|
|
#include <functional>
|
|
|
|
#include <locale>
|
2013-02-15 16:10:47 -07:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "Core.h"
|
2013-03-01 01:29:16 -07:00
|
|
|
#include "MiscUtils.h"
|
2013-02-15 16:10:47 -07:00
|
|
|
#include <Console.h>
|
|
|
|
#include <Export.h>
|
|
|
|
#include <PluginManager.h>
|
|
|
|
#include <VTableInterpose.h>
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
#include "modules/Items.h"
|
2013-02-15 16:10:47 -07:00
|
|
|
#include "modules/Screen.h"
|
2014-05-03 03:19:46 -06:00
|
|
|
#include "modules/World.h"
|
2013-02-15 16:10:47 -07:00
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
#include "df/building_stockpilest.h"
|
|
|
|
#include "df/caravan_state.h"
|
|
|
|
#include "df/dfhack_material_category.h"
|
2013-02-15 16:10:47 -07:00
|
|
|
#include "df/enabler.h"
|
2014-05-03 03:19:46 -06:00
|
|
|
#include "df/item_quality.h"
|
|
|
|
#include "df/ui.h"
|
|
|
|
#include "df/world.h"
|
2013-02-15 16:10:47 -07:00
|
|
|
|
2013-10-08 07:41:49 -06:00
|
|
|
using namespace std;
|
2013-02-15 16:10:47 -07:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::map;
|
|
|
|
using std::ostringstream;
|
|
|
|
using std::set;
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
|
|
|
using df::global::enabler;
|
2013-03-01 01:29:16 -07:00
|
|
|
using df::global::gps;
|
2013-02-15 16:10:47 -07:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_NULLPTR
|
|
|
|
#define nullptr 0L
|
|
|
|
#endif
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
#define COLOR_TITLE COLOR_BROWN
|
2013-02-15 16:10:47 -07:00
|
|
|
#define COLOR_UNSELECTED COLOR_GREY
|
|
|
|
#define COLOR_SELECTED COLOR_WHITE
|
|
|
|
#define COLOR_HIGHLIGHTED COLOR_GREEN
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
struct coord32_t
|
|
|
|
{
|
|
|
|
int32_t x, y, z;
|
|
|
|
|
|
|
|
coord32_t()
|
|
|
|
{
|
|
|
|
x = -30000;
|
|
|
|
y = -30000;
|
|
|
|
z = -30000;
|
|
|
|
}
|
|
|
|
|
|
|
|
coord32_t(df::coord& other)
|
|
|
|
{
|
|
|
|
x = other.x;
|
|
|
|
y = other.y;
|
|
|
|
z = other.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
df::coord get_coord16() const
|
|
|
|
{
|
|
|
|
return df::coord(x, y, z);
|
|
|
|
}
|
|
|
|
};
|
2013-02-15 16:10:47 -07:00
|
|
|
|
|
|
|
template <class T, typename Fn>
|
|
|
|
static void for_each_(vector<T> &v, Fn func)
|
|
|
|
{
|
|
|
|
for_each(v.begin(), v.end(), func);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class V, typename Fn>
|
|
|
|
static void for_each_(map<T, V> &v, Fn func)
|
|
|
|
{
|
|
|
|
for_each(v.begin(), v.end(), func);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class V, typename Fn>
|
|
|
|
static void transform_(vector<T> &src, vector<V> &dst, Fn func)
|
|
|
|
{
|
|
|
|
transform(src.begin(), src.end(), back_inserter(dst), func);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef int8_t UIColor;
|
|
|
|
|
|
|
|
void OutputString(UIColor color, int &x, int &y, const std::string &text,
|
|
|
|
bool newline = false, int left_margin = 0, const UIColor bg_color = 0)
|
|
|
|
{
|
|
|
|
Screen::paintString(Screen::Pen(' ', color, bg_color), x, y, text);
|
|
|
|
if (newline)
|
|
|
|
{
|
|
|
|
++y;
|
|
|
|
x = left_margin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
x += text.length();
|
|
|
|
}
|
|
|
|
|
2013-04-12 19:26:39 -06:00
|
|
|
void OutputHotkeyString(int &x, int &y, const char *text, const char *hotkey, bool newline = false,
|
|
|
|
int left_margin = 0, int8_t text_color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
2013-04-12 19:26:39 -06:00
|
|
|
OutputString(hotkey_color, x, y, hotkey);
|
2013-02-15 16:10:47 -07:00
|
|
|
string display(": ");
|
|
|
|
display.append(text);
|
2013-04-12 19:26:39 -06:00
|
|
|
OutputString(text_color, x, y, display, newline, left_margin);
|
|
|
|
}
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
void OutputLabelString(int &x, int &y, const char *text, const char *hotkey, const string &label, bool newline = false,
|
|
|
|
int left_margin = 0, int8_t text_color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN)
|
|
|
|
{
|
|
|
|
OutputString(hotkey_color, x, y, hotkey);
|
|
|
|
string display(": ");
|
|
|
|
display.append(text);
|
|
|
|
display.append(": ");
|
|
|
|
OutputString(text_color, x, y, display);
|
|
|
|
OutputString(hotkey_color, x, y, label, newline, left_margin);
|
|
|
|
}
|
|
|
|
|
2013-04-12 19:26:39 -06:00
|
|
|
void OutputFilterString(int &x, int &y, const char *text, const char *hotkey, bool state, bool newline = false,
|
|
|
|
int left_margin = 0, int8_t hotkey_color = COLOR_LIGHTGREEN)
|
|
|
|
{
|
|
|
|
OutputString(hotkey_color, x, y, hotkey);
|
|
|
|
OutputString(COLOR_WHITE, x, y, ": ");
|
|
|
|
OutputString((state) ? COLOR_WHITE : COLOR_GREY, x, y, text, newline, left_margin);
|
2013-02-15 16:10:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OutputToggleString(int &x, int &y, const char *text, const char *hotkey, bool state, bool newline = true, int left_margin = 0, int8_t color = COLOR_WHITE)
|
|
|
|
{
|
|
|
|
OutputHotkeyString(x, y, text, hotkey);
|
|
|
|
OutputString(COLOR_WHITE, x, y, ": ");
|
|
|
|
if (state)
|
2014-05-03 03:19:46 -06:00
|
|
|
OutputString(COLOR_GREEN, x, y, "On", newline, left_margin);
|
2013-02-15 16:10:47 -07:00
|
|
|
else
|
2014-05-03 03:19:46 -06:00
|
|
|
OutputString(COLOR_GREY, x, y, "Off", newline, left_margin);
|
2013-02-15 16:10:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline string int_to_string(const int n)
|
|
|
|
{
|
|
|
|
return static_cast<ostringstream*>( &(ostringstream() << n) )->str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_to_limit(int &value, const int maximum, const int min = 0)
|
|
|
|
{
|
|
|
|
if (value < min)
|
|
|
|
value = min;
|
|
|
|
else if (value > maximum)
|
|
|
|
value = maximum;
|
|
|
|
}
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
// trim from start
|
|
|
|
static inline std::string <rim(std::string &s) {
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim from end
|
|
|
|
static inline std::string &rtrim(std::string &s) {
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim from both ends
|
|
|
|
static inline std::string &trim(std::string &s) {
|
|
|
|
return ltrim(rtrim(s));
|
|
|
|
}
|
2013-02-15 16:10:47 -07:00
|
|
|
|
|
|
|
inline void paint_text(const UIColor color, const int &x, const int &y, const std::string &text, const UIColor background = 0)
|
|
|
|
{
|
|
|
|
Screen::paintString(Screen::Pen(' ', color, background), x, y, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static string pad_string(string text, const int size, const bool front = true, const bool trim = false)
|
|
|
|
{
|
|
|
|
if (text.length() > size)
|
|
|
|
{
|
|
|
|
if (trim && size > 10)
|
|
|
|
{
|
|
|
|
text = text.substr(0, size-3);
|
|
|
|
text.append("...");
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
string aligned(size - text.length(), ' ');
|
|
|
|
if (front)
|
|
|
|
{
|
|
|
|
aligned.append(text);
|
|
|
|
return aligned;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
text.append(aligned);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
/*
|
|
|
|
* Utility Functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
static df::building_stockpilest *get_selected_stockpile()
|
|
|
|
{
|
|
|
|
if (!Gui::dwarfmode_hotkey(Core::getTopViewscreen()) ||
|
|
|
|
df::global::ui->main.mode != ui_sidebar_mode::QueryBuilding)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virtual_cast<df::building_stockpilest>(df::global::world->selected_building);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool can_trade()
|
|
|
|
{
|
|
|
|
if (df::global::ui->caravans.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto it = df::global::ui->caravans.begin(); it != df::global::ui->caravans.end(); it++)
|
|
|
|
{
|
|
|
|
auto caravan = *it;
|
|
|
|
auto trade_state = caravan->trade_state;
|
|
|
|
auto time_remaining = caravan->time_remaining;
|
|
|
|
if ((trade_state != 1 && trade_state != 2) || time_remaining == 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_metal_item(df::item *item)
|
|
|
|
{
|
|
|
|
MaterialInfo mat(item);
|
|
|
|
return (mat.getCraftClass() == craft_material_class::Metal);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_set_to_melt(df::item* item)
|
|
|
|
{
|
|
|
|
return item->flags.bits.melt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copied from Kelly Martin's code
|
|
|
|
bool can_melt(df::item* item)
|
|
|
|
{
|
|
|
|
|
|
|
|
df::item_flags bad_flags;
|
|
|
|
bad_flags.whole = 0;
|
|
|
|
|
|
|
|
#define F(x) bad_flags.bits.x = true;
|
|
|
|
F(dump); F(forbid); F(garbage_collect); F(in_job);
|
|
|
|
F(hostile); F(on_fire); F(rotten); F(trader);
|
|
|
|
F(in_building); F(construction); F(artifact); F(melt);
|
|
|
|
#undef F
|
|
|
|
|
|
|
|
if (item->flags.whole & bad_flags.whole)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
df::item_type t = item->getType();
|
|
|
|
|
|
|
|
if (t == df::enums::item_type::BOX || t == df::enums::item_type::BAR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!is_metal_item(item)) return false;
|
|
|
|
|
|
|
|
for (auto g = item->general_refs.begin(); g != item->general_refs.end(); g++)
|
|
|
|
{
|
|
|
|
switch ((*g)->getType())
|
|
|
|
{
|
|
|
|
case general_ref_type::CONTAINS_ITEM:
|
|
|
|
case general_ref_type::UNIT_HOLDER:
|
|
|
|
case general_ref_type::CONTAINS_UNIT:
|
|
|
|
return false;
|
|
|
|
case general_ref_type::CONTAINED_IN_ITEM:
|
|
|
|
{
|
|
|
|
df::item* c = (*g)->getItem();
|
|
|
|
for (auto gg = c->general_refs.begin(); gg != c->general_refs.end(); gg++)
|
|
|
|
{
|
|
|
|
if ((*gg)->getType() == general_ref_type::UNIT_HOLDER)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->getQuality() >= item_quality::Masterful)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stockpile Access
|
|
|
|
*/
|
|
|
|
|
|
|
|
class StockpileInfo {
|
|
|
|
public:
|
|
|
|
StockpileInfo() : id(0), sp(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StockpileInfo(df::building_stockpilest *sp_) : sp(sp_)
|
|
|
|
{
|
|
|
|
readBuilding();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool inStockpile(df::item *i)
|
|
|
|
{
|
|
|
|
df::item *container = Items::getContainer(i);
|
|
|
|
if (container)
|
|
|
|
return inStockpile(container);
|
|
|
|
|
|
|
|
if (i->pos.z != z) return false;
|
|
|
|
if (i->pos.x < x1 || i->pos.x >= x2 ||
|
|
|
|
i->pos.y < y1 || i->pos.y >= y2) return false;
|
|
|
|
int e = (i->pos.x - x1) + (i->pos.y - y1) * sp->room.width;
|
|
|
|
return sp->room.extents[e] == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid()
|
|
|
|
{
|
|
|
|
if (!id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto found = df::building::find(id);
|
|
|
|
return found && found == sp && found->getType() == building_type::Stockpile;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t getId()
|
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool matches(df::building_stockpilest* sp)
|
|
|
|
{
|
|
|
|
return this->sp == sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int32_t id;
|
|
|
|
df::building_stockpilest* sp;
|
|
|
|
|
|
|
|
void readBuilding()
|
|
|
|
{
|
|
|
|
if (!sp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
id = sp->id;
|
|
|
|
z = sp->z;
|
|
|
|
x1 = sp->room.x;
|
|
|
|
x2 = sp->room.x + sp->room.width;
|
|
|
|
y1 = sp->room.y;
|
|
|
|
y2 = sp->room.y + sp->room.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int x1, x2, y1, y2, z;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PersistentStockpileInfo : public StockpileInfo {
|
|
|
|
public:
|
|
|
|
PersistentStockpileInfo(df::building_stockpilest *sp, string persistence_key) :
|
|
|
|
StockpileInfo(sp), persistence_key(persistence_key)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentStockpileInfo(PersistentDataItem &config, string persistence_key) :
|
|
|
|
config(config), persistence_key(persistence_key)
|
|
|
|
{
|
|
|
|
id = config.ival(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool load()
|
|
|
|
{
|
|
|
|
auto found = df::building::find(id);
|
|
|
|
if (!found || found->getType() != building_type::Stockpile)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sp = virtual_cast<df::building_stockpilest>(found);
|
|
|
|
if (!sp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
readBuilding();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void save()
|
|
|
|
{
|
|
|
|
config = DFHack::World::AddPersistentData(persistence_key);
|
|
|
|
config.ival(1) = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove()
|
|
|
|
{
|
|
|
|
DFHack::World::DeletePersistentData(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PersistentDataItem config;
|
|
|
|
string persistence_key;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
/*
|
|
|
|
* List classes
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class ListEntry
|
|
|
|
{
|
|
|
|
public:
|
2013-03-02 01:58:36 -07:00
|
|
|
T elem;
|
2013-04-19 21:04:44 -06:00
|
|
|
string text, keywords;
|
2013-02-15 16:10:47 -07:00
|
|
|
bool selected;
|
2014-05-03 03:19:46 -06:00
|
|
|
UIColor color;
|
2013-02-15 16:10:47 -07:00
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
ListEntry(const string text, const T elem, const string keywords = "", const UIColor color = COLOR_UNSELECTED) :
|
|
|
|
elem(elem), text(text), selected(false), keywords(keywords), color(color)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ListColumn
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int highlighted_index;
|
|
|
|
int display_start_offset;
|
2013-03-02 01:58:36 -07:00
|
|
|
unsigned short text_clip_at;
|
2013-02-15 16:10:47 -07:00
|
|
|
int32_t bottom_margin, search_margin, left_margin;
|
|
|
|
bool multiselect;
|
|
|
|
bool allow_null;
|
|
|
|
bool auto_select;
|
|
|
|
bool allow_search;
|
|
|
|
bool feed_changed_highlight;
|
|
|
|
|
|
|
|
ListColumn()
|
|
|
|
{
|
|
|
|
bottom_margin = 3;
|
|
|
|
clear();
|
|
|
|
left_margin = 2;
|
|
|
|
search_margin = 63;
|
|
|
|
highlighted_index = 0;
|
2013-03-02 01:58:36 -07:00
|
|
|
text_clip_at = 0;
|
2013-02-15 16:10:47 -07:00
|
|
|
multiselect = false;
|
|
|
|
allow_null = true;
|
|
|
|
auto_select = false;
|
|
|
|
allow_search = true;
|
|
|
|
feed_changed_highlight = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
list.clear();
|
|
|
|
display_list.clear();
|
|
|
|
display_start_offset = 0;
|
2014-05-03 03:19:46 -06:00
|
|
|
if (highlighted_index != -1)
|
|
|
|
highlighted_index = 0;
|
2013-03-01 01:29:16 -07:00
|
|
|
max_item_width = title.length();
|
2013-02-15 16:10:47 -07:00
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize()
|
|
|
|
{
|
|
|
|
display_max_rows = gps->dimy - 4 - bottom_margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(ListEntry<T> &entry)
|
|
|
|
{
|
|
|
|
list.push_back(entry);
|
|
|
|
if (entry.text.length() > max_item_width)
|
|
|
|
max_item_width = entry.text.length();
|
|
|
|
}
|
|
|
|
|
2013-03-02 01:58:36 -07:00
|
|
|
void add(const string &text, const T &elem)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
|
|
|
list.push_back(ListEntry<T>(text, elem));
|
|
|
|
if (text.length() > max_item_width)
|
|
|
|
max_item_width = text.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
int fixWidth()
|
|
|
|
{
|
2013-03-02 01:58:36 -07:00
|
|
|
if (text_clip_at > 0 && max_item_width > text_clip_at)
|
|
|
|
max_item_width = text_clip_at;
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
for (auto it = list.begin(); it != list.end(); it++)
|
|
|
|
{
|
|
|
|
it->text = pad_string(it->text, max_item_width, false);
|
|
|
|
}
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
return getMaxItemWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
int getMaxItemWidth()
|
|
|
|
{
|
2013-02-15 16:10:47 -07:00
|
|
|
return left_margin + max_item_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void display_extras(const T &elem, int32_t &x, int32_t &y) const {}
|
|
|
|
|
|
|
|
void display(const bool is_selected_column) const
|
|
|
|
{
|
|
|
|
int32_t y = 2;
|
|
|
|
paint_text(COLOR_TITLE, left_margin, y, title);
|
|
|
|
|
|
|
|
int last_index_able_to_display = display_start_offset + display_max_rows;
|
|
|
|
for (int i = display_start_offset; i < display_list.size() && i < last_index_able_to_display; i++)
|
|
|
|
{
|
|
|
|
++y;
|
2014-05-03 03:19:46 -06:00
|
|
|
UIColor fg_color = (display_list[i]->selected) ? COLOR_SELECTED : display_list[i]->color;
|
2013-02-15 16:10:47 -07:00
|
|
|
UIColor bg_color = (is_selected_column && i == highlighted_index) ? COLOR_HIGHLIGHTED : COLOR_BLACK;
|
2013-03-02 01:58:36 -07:00
|
|
|
|
|
|
|
string item_label = display_list[i]->text;
|
|
|
|
if (text_clip_at > 0 && item_label.length() > text_clip_at)
|
|
|
|
item_label.resize(text_clip_at);
|
|
|
|
|
|
|
|
paint_text(fg_color, left_margin, y, item_label, bg_color);
|
2013-02-15 16:10:47 -07:00
|
|
|
int x = left_margin + display_list[i]->text.length() + 1;
|
2013-03-02 01:58:36 -07:00
|
|
|
display_extras(display_list[i]->elem, x, y);
|
2013-02-15 16:10:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_selected_column && allow_search)
|
|
|
|
{
|
2013-04-12 19:26:39 -06:00
|
|
|
y = gps->dimy - 3;
|
2013-02-15 16:10:47 -07:00
|
|
|
int32_t x = search_margin;
|
|
|
|
OutputHotkeyString(x, y, "Search" ,"S");
|
|
|
|
OutputString(COLOR_WHITE, x, y, ": ");
|
|
|
|
OutputString(COLOR_WHITE, x, y, search_string);
|
|
|
|
OutputString(COLOR_LIGHTGREEN, x, y, "_");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void filterDisplay()
|
|
|
|
{
|
|
|
|
ListEntry<T> *prev_selected = (getDisplayListSize() > 0) ? display_list[highlighted_index] : NULL;
|
|
|
|
display_list.clear();
|
2013-04-19 21:04:44 -06:00
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
search_string = toLower(search_string);
|
2013-04-19 21:04:44 -06:00
|
|
|
vector<string> search_tokens;
|
|
|
|
if (!search_string.empty())
|
|
|
|
split_string(&search_tokens, search_string, " ");
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
for (size_t i = 0; i < list.size(); i++)
|
|
|
|
{
|
2013-04-12 19:26:39 -06:00
|
|
|
ListEntry<T> *entry = &list[i];
|
2013-04-19 21:04:44 -06:00
|
|
|
|
|
|
|
bool include_item = true;
|
|
|
|
if (!search_string.empty())
|
|
|
|
{
|
|
|
|
string item_string = toLower(list[i].text);
|
|
|
|
for (auto si = search_tokens.begin(); si != search_tokens.end(); si++)
|
|
|
|
{
|
2013-04-24 05:01:35 -06:00
|
|
|
if (!si->empty() && item_string.find(*si) == string::npos &&
|
|
|
|
list[i].keywords.find(*si) == string::npos)
|
2013-04-19 21:04:44 -06:00
|
|
|
{
|
|
|
|
include_item = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (include_item)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
|
|
|
display_list.push_back(entry);
|
|
|
|
if (entry == prev_selected)
|
|
|
|
highlighted_index = display_list.size() - 1;
|
|
|
|
}
|
2013-04-12 19:26:39 -06:00
|
|
|
else if (auto_select)
|
|
|
|
{
|
|
|
|
entry->selected = false;
|
|
|
|
}
|
2013-02-15 16:10:47 -07:00
|
|
|
}
|
|
|
|
changeHighlight(0);
|
|
|
|
feed_changed_highlight = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectDefaultEntry()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < display_list.size(); i++)
|
|
|
|
{
|
|
|
|
if (display_list[i]->selected)
|
|
|
|
{
|
|
|
|
highlighted_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
void centerSelection()
|
|
|
|
{
|
|
|
|
if (display_list.size() == 0)
|
|
|
|
return;
|
|
|
|
display_start_offset = highlighted_index - (display_max_rows / 2);
|
|
|
|
validateDisplayOffset();
|
|
|
|
validateHighlight();
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
void validateHighlight()
|
|
|
|
{
|
|
|
|
set_to_limit(highlighted_index, display_list.size() - 1);
|
|
|
|
|
|
|
|
if (highlighted_index < display_start_offset)
|
|
|
|
display_start_offset = highlighted_index;
|
|
|
|
else if (highlighted_index >= display_start_offset + display_max_rows)
|
|
|
|
display_start_offset = highlighted_index - display_max_rows + 1;
|
|
|
|
|
|
|
|
if (auto_select || (!allow_null && list.size() == 1))
|
|
|
|
display_list[highlighted_index]->selected = true;
|
|
|
|
|
|
|
|
feed_changed_highlight = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void changeHighlight(const int highlight_change, const int offset_shift = 0)
|
|
|
|
{
|
|
|
|
if (!initHighlightChange())
|
|
|
|
return;
|
|
|
|
|
|
|
|
highlighted_index += highlight_change + offset_shift * display_max_rows;
|
|
|
|
|
|
|
|
display_start_offset += offset_shift * display_max_rows;
|
2014-05-03 03:19:46 -06:00
|
|
|
validateDisplayOffset();
|
2013-02-15 16:10:47 -07:00
|
|
|
validateHighlight();
|
|
|
|
}
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
void validateDisplayOffset()
|
|
|
|
{
|
|
|
|
set_to_limit(display_start_offset, max(0, (int)(display_list.size())-display_max_rows));
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
void setHighlight(const int index)
|
|
|
|
{
|
|
|
|
if (!initHighlightChange())
|
|
|
|
return;
|
|
|
|
|
|
|
|
highlighted_index = index;
|
|
|
|
validateHighlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initHighlightChange()
|
|
|
|
{
|
|
|
|
if (display_list.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (auto_select && !multiselect)
|
|
|
|
{
|
|
|
|
for (auto it = list.begin(); it != list.end(); it++)
|
|
|
|
{
|
|
|
|
it->selected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggleHighlighted()
|
|
|
|
{
|
2014-05-03 03:19:46 -06:00
|
|
|
if (display_list.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
if (auto_select)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ListEntry<T> *entry = display_list[highlighted_index];
|
|
|
|
if (!multiselect || !allow_null)
|
|
|
|
{
|
|
|
|
int selected_count = 0;
|
|
|
|
for (size_t i = 0; i < list.size(); i++)
|
|
|
|
{
|
|
|
|
if (!multiselect && !entry->selected)
|
|
|
|
list[i].selected = false;
|
|
|
|
if (!allow_null && list[i].selected)
|
|
|
|
selected_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_null && entry->selected && selected_count == 1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->selected = !entry->selected;
|
|
|
|
}
|
|
|
|
|
2013-03-02 01:58:36 -07:00
|
|
|
vector<T> getSelectedElems(bool only_one = false)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
2013-03-02 01:58:36 -07:00
|
|
|
vector<T> results;
|
2013-02-15 16:10:47 -07:00
|
|
|
for (auto it = list.begin(); it != list.end(); it++)
|
|
|
|
{
|
|
|
|
if ((*it).selected)
|
|
|
|
{
|
|
|
|
results.push_back(it->elem);
|
|
|
|
if (only_one)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2013-03-02 01:58:36 -07:00
|
|
|
T getFirstSelectedElem()
|
|
|
|
{
|
|
|
|
vector<T> results = getSelectedElems(true);
|
|
|
|
if (results.size() == 0)
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
return results[0];
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
void clearSelection()
|
|
|
|
{
|
2013-09-30 12:51:29 -06:00
|
|
|
for_each_(list, clear_fn);
|
2013-02-15 16:10:47 -07:00
|
|
|
}
|
|
|
|
|
2013-03-02 01:58:36 -07:00
|
|
|
void selectItem(const T elem)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (; i < display_list.size(); i++)
|
|
|
|
{
|
|
|
|
if (display_list[i]->elem == elem)
|
|
|
|
{
|
|
|
|
setHighlight(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearSearch()
|
|
|
|
{
|
|
|
|
search_string.clear();
|
|
|
|
filterDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getDisplayListSize()
|
|
|
|
{
|
|
|
|
return display_list.size();
|
|
|
|
}
|
|
|
|
|
2013-04-13 18:23:47 -06:00
|
|
|
vector<ListEntry<T>*> &getDisplayList()
|
|
|
|
{
|
|
|
|
return display_list;
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
size_t getBaseListSize()
|
|
|
|
{
|
|
|
|
return list.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool feed(set<df::interface_key> *input)
|
|
|
|
{
|
|
|
|
feed_changed_highlight = false;
|
|
|
|
if (input->count(interface_key::CURSOR_UP))
|
|
|
|
{
|
|
|
|
changeHighlight(-1);
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CURSOR_DOWN))
|
|
|
|
{
|
|
|
|
changeHighlight(1);
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::STANDARDSCROLL_PAGEUP))
|
|
|
|
{
|
|
|
|
changeHighlight(0, -1);
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::STANDARDSCROLL_PAGEDOWN))
|
|
|
|
{
|
|
|
|
changeHighlight(0, 1);
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::SELECT) && !auto_select)
|
|
|
|
{
|
|
|
|
toggleHighlighted();
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CUSTOM_SHIFT_S))
|
|
|
|
{
|
|
|
|
clearSearch();
|
|
|
|
}
|
|
|
|
else if (enabler->tracking_on && gps->mouse_x != -1 && gps->mouse_y != -1 && enabler->mouse_lbut)
|
|
|
|
{
|
|
|
|
return setHighlightByMouse();
|
|
|
|
}
|
|
|
|
else if (allow_search)
|
|
|
|
{
|
|
|
|
// Search query typing mode always on
|
|
|
|
df::interface_key last_token = *input->rbegin();
|
2014-08-11 04:23:19 -06:00
|
|
|
int charcode = Screen::keyToChar(last_token);
|
|
|
|
if ((charcode >= 96 && charcode <= 123) || charcode == 32)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
|
|
|
// Standard character
|
2014-08-11 04:23:19 -06:00
|
|
|
search_string += char(charcode);
|
2013-02-15 16:10:47 -07:00
|
|
|
filterDisplay();
|
2014-05-03 03:19:46 -06:00
|
|
|
centerSelection();
|
2013-02-15 16:10:47 -07:00
|
|
|
}
|
|
|
|
else if (last_token == interface_key::STRING_A000)
|
|
|
|
{
|
|
|
|
// Backspace
|
|
|
|
if (search_string.length() > 0)
|
|
|
|
{
|
|
|
|
search_string.erase(search_string.length()-1);
|
|
|
|
filterDisplay();
|
2014-05-03 03:19:46 -06:00
|
|
|
centerSelection();
|
2013-02-15 16:10:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setHighlightByMouse()
|
|
|
|
{
|
|
|
|
if (gps->mouse_y >= 3 && gps->mouse_y < display_max_rows + 3 &&
|
|
|
|
gps->mouse_x >= left_margin && gps->mouse_x < left_margin + max_item_width)
|
|
|
|
{
|
|
|
|
int new_index = display_start_offset + gps->mouse_y - 3;
|
|
|
|
if (new_index < display_list.size())
|
|
|
|
setHighlight(new_index);
|
|
|
|
|
|
|
|
enabler->mouse_lbut = enabler->mouse_rbut = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-03 03:19:46 -06:00
|
|
|
void sort(bool force_sort = false)
|
2013-02-15 16:10:47 -07:00
|
|
|
{
|
|
|
|
if (force_sort || list.size() < 100)
|
2013-09-30 12:51:29 -06:00
|
|
|
std::sort(list.begin(), list.end(), sort_fn);
|
2013-02-15 16:10:47 -07:00
|
|
|
|
|
|
|
filterDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTitle(const string t)
|
|
|
|
{
|
|
|
|
title = t;
|
|
|
|
if (title.length() > max_item_width)
|
|
|
|
max_item_width = title.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getDisplayedListSize()
|
|
|
|
{
|
|
|
|
return display_list.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-09-30 12:51:29 -06:00
|
|
|
static void clear_fn(ListEntry<T> &e) { e.selected = false; }
|
|
|
|
static bool sort_fn(ListEntry<T> const& a, ListEntry<T> const& b) { return a.text.compare(b.text) < 0; }
|
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
vector<ListEntry<T>> list;
|
|
|
|
vector<ListEntry<T>*> display_list;
|
|
|
|
string search_string;
|
|
|
|
string title;
|
|
|
|
int display_max_rows;
|
|
|
|
int max_item_width;
|
|
|
|
};
|
|
|
|
|
|
|
|
|