|
|
|
@ -1,23 +1,25 @@
|
|
|
|
|
//command-prompt a one line command entry at the top of the screen for quick commands
|
|
|
|
|
// command-prompt: A one-line command entry at the top of the screen for quick commands
|
|
|
|
|
|
|
|
|
|
#include "Core.h"
|
|
|
|
|
#include <ColorText.h>
|
|
|
|
|
#include <Console.h>
|
|
|
|
|
#include <Export.h>
|
|
|
|
|
#include <MiscUtils.h>
|
|
|
|
|
#include <PluginManager.h>
|
|
|
|
|
#include <ColorText.h>
|
|
|
|
|
|
|
|
|
|
#include <modules/Screen.h>
|
|
|
|
|
#include <modules/Gui.h>
|
|
|
|
|
#include <modules/Screen.h>
|
|
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <list>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "df/enabler.h"
|
|
|
|
|
#include "df/graphic.h"
|
|
|
|
|
#include "df/interface_key.h"
|
|
|
|
|
#include "df/ui.h"
|
|
|
|
|
#include "df/graphic.h"
|
|
|
|
|
#include "df/enabler.h"
|
|
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
using namespace df::enums;
|
|
|
|
@ -36,8 +38,10 @@ class prompt_ostream:public buffered_color_ostream
|
|
|
|
|
protected:
|
|
|
|
|
void flush_proxy();
|
|
|
|
|
public:
|
|
|
|
|
prompt_ostream(viewscreen_commandpromptst* parent):parent_(parent){}
|
|
|
|
|
bool empty(){return buffer.empty();}
|
|
|
|
|
prompt_ostream(viewscreen_commandpromptst* parent)
|
|
|
|
|
: parent_(parent)
|
|
|
|
|
{}
|
|
|
|
|
bool empty() { return buffer.empty(); }
|
|
|
|
|
};
|
|
|
|
|
class viewscreen_commandpromptst : public dfhack_viewscreen {
|
|
|
|
|
public:
|
|
|
|
@ -48,7 +52,7 @@ public:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void render();
|
|
|
|
|
void help() { }
|
|
|
|
|
void help() {}
|
|
|
|
|
int8_t movies_okay() { return 0; }
|
|
|
|
|
|
|
|
|
|
df::unit* getSelectedUnit() { return Gui::getAnyUnit(parent); }
|
|
|
|
@ -57,10 +61,11 @@ public:
|
|
|
|
|
df::plant* getSelectedPlant() { return Gui::getAnyPlant(parent); }
|
|
|
|
|
|
|
|
|
|
std::string getFocusString() { return "commandprompt"; }
|
|
|
|
|
viewscreen_commandpromptst(std::string entry):submitted(false), is_response(false)
|
|
|
|
|
viewscreen_commandpromptst(std::string entry)
|
|
|
|
|
: submitted(false), is_response(false)
|
|
|
|
|
{
|
|
|
|
|
show_fps=gps->display_frames;
|
|
|
|
|
gps->display_frames=0;
|
|
|
|
|
show_fps = gps->display_frames;
|
|
|
|
|
gps->display_frames = 0;
|
|
|
|
|
cursor_pos = entry.size();
|
|
|
|
|
frame = 0;
|
|
|
|
|
history_idx = command_history.size();
|
|
|
|
@ -76,7 +81,7 @@ public:
|
|
|
|
|
}
|
|
|
|
|
~viewscreen_commandpromptst()
|
|
|
|
|
{
|
|
|
|
|
gps->display_frames=show_fps;
|
|
|
|
|
gps->display_frames = show_fps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void add_response(color_value v, std::string s)
|
|
|
|
@ -125,7 +130,7 @@ public:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::list<std::pair<color_value,std::string> > responses;
|
|
|
|
|
std::list<std::pair<color_value, std::string> > responses;
|
|
|
|
|
int cursor_pos;
|
|
|
|
|
int history_idx;
|
|
|
|
|
bool submitted;
|
|
|
|
@ -138,8 +143,8 @@ void prompt_ostream::flush_proxy()
|
|
|
|
|
{
|
|
|
|
|
if (buffer.empty())
|
|
|
|
|
return;
|
|
|
|
|
for(auto it=buffer.begin();it!=buffer.end();it++)
|
|
|
|
|
parent_->add_response(it->first,it->second);
|
|
|
|
|
for(auto it = buffer.begin(); it != buffer.end(); it++)
|
|
|
|
|
parent_->add_response(it->first, it->second);
|
|
|
|
|
buffer.clear();
|
|
|
|
|
}
|
|
|
|
|
void viewscreen_commandpromptst::render()
|
|
|
|
@ -154,25 +159,31 @@ void viewscreen_commandpromptst::render()
|
|
|
|
|
|
|
|
|
|
auto dim = Screen::getWindowSize();
|
|
|
|
|
parent->render();
|
|
|
|
|
if(is_response)
|
|
|
|
|
if (is_response)
|
|
|
|
|
{
|
|
|
|
|
auto it=responses.begin();
|
|
|
|
|
for(int i=0;i<dim.y && it!=responses.end();i++,it++)
|
|
|
|
|
int y = 0;
|
|
|
|
|
for (auto &response : responses)
|
|
|
|
|
{
|
|
|
|
|
Screen::fillRect(Screen::Pen(' ', 7, 0),0,i,dim.x,i);
|
|
|
|
|
std::string cur_line=it->second;
|
|
|
|
|
Screen::paintString(Screen::Pen(' ',it->first,0),0,i,cur_line.substr(0,cur_line.size()-1));
|
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
|
word_wrap(&lines, response.second, dim.x);
|
|
|
|
|
for (auto &line : lines)
|
|
|
|
|
{
|
|
|
|
|
Screen::fillRect(Screen::Pen(' ', 7, 0), 0, y, dim.x, y);
|
|
|
|
|
Screen::paintString(Screen::Pen(' ', response.first, 0), 0, y, line);
|
|
|
|
|
if (++y >= dim.y)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::string entry = get_entry();
|
|
|
|
|
Screen::fillRect(Screen::Pen(' ', 7, 0),0,0,dim.x,0);
|
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0,"[DFHack]#");
|
|
|
|
|
Screen::fillRect(Screen::Pen(' ', 7, 0), 0, 0, dim.x, 0);
|
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0, "[DFHack]#");
|
|
|
|
|
std::string cursor = (frame < enabler->gfps / 2) ? "_" : " ";
|
|
|
|
|
if(cursor_pos < (dim.x - 10))
|
|
|
|
|
if (cursor_pos < dim.x - 10)
|
|
|
|
|
{
|
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 10,0 , entry);
|
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 10, 0, entry);
|
|
|
|
|
if (int16_t(entry.size()) > dim.x - 10)
|
|
|
|
|
Screen::paintTile(Screen::Pen('\032', 7, 0), dim.x - 1, 0);
|
|
|
|
|
if (cursor != " ")
|
|
|
|
@ -191,12 +202,12 @@ void viewscreen_commandpromptst::render()
|
|
|
|
|
void viewscreen_commandpromptst::submit()
|
|
|
|
|
{
|
|
|
|
|
CoreSuspendClaimer suspend;
|
|
|
|
|
if(is_response)
|
|
|
|
|
if (is_response)
|
|
|
|
|
{
|
|
|
|
|
Screen::dismiss(this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(submitted)
|
|
|
|
|
if (submitted)
|
|
|
|
|
return;
|
|
|
|
|
submitted = true;
|
|
|
|
|
prompt_ostream out(this);
|
|
|
|
@ -204,11 +215,11 @@ void viewscreen_commandpromptst::submit()
|
|
|
|
|
Screen::Hide hide_guard(this, Screen::Hide::RESTORE_AT_TOP);
|
|
|
|
|
Core::getInstance().runCommand(out, get_entry());
|
|
|
|
|
}
|
|
|
|
|
if(out.empty() && responses.empty())
|
|
|
|
|
if (out.empty() && responses.empty())
|
|
|
|
|
Screen::dismiss(this);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
is_response=true;
|
|
|
|
|
is_response = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
|
|
|
|
@ -240,14 +251,14 @@ void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
|
|
|
|
|
for (auto it = events->begin(); it != events->end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
auto key = *it;
|
|
|
|
|
if (key==interface_key::STRING_A000) //delete?
|
|
|
|
|
if (key == interface_key::STRING_A000) //delete?
|
|
|
|
|
{
|
|
|
|
|
if(entry.size() && cursor_pos > 0)
|
|
|
|
|
if (entry.size() && cursor_pos > 0)
|
|
|
|
|
{
|
|
|
|
|
entry.erase(cursor_pos - 1, 1);
|
|
|
|
|
cursor_pos--;
|
|
|
|
|
}
|
|
|
|
|
if(size_t(cursor_pos) > entry.size())
|
|
|
|
|
if (size_t(cursor_pos) > entry.size())
|
|
|
|
|
cursor_pos = entry.size();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
@ -261,34 +272,34 @@ void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Prevent number keys from moving cursor
|
|
|
|
|
if(events->count(interface_key::CURSOR_RIGHT))
|
|
|
|
|
if (events->count(interface_key::CURSOR_RIGHT))
|
|
|
|
|
{
|
|
|
|
|
cursor_pos++;
|
|
|
|
|
if (size_t(cursor_pos) > entry.size())
|
|
|
|
|
cursor_pos = entry.size();
|
|
|
|
|
}
|
|
|
|
|
else if(events->count(interface_key::CURSOR_LEFT))
|
|
|
|
|
else if (events->count(interface_key::CURSOR_LEFT))
|
|
|
|
|
{
|
|
|
|
|
cursor_pos--;
|
|
|
|
|
if (cursor_pos < 0) cursor_pos = 0;
|
|
|
|
|
}
|
|
|
|
|
else if(events->count(interface_key::CURSOR_RIGHT_FAST))
|
|
|
|
|
else if (events->count(interface_key::CURSOR_RIGHT_FAST))
|
|
|
|
|
{
|
|
|
|
|
forward_word();
|
|
|
|
|
}
|
|
|
|
|
else if(events->count(interface_key::CURSOR_LEFT_FAST))
|
|
|
|
|
else if (events->count(interface_key::CURSOR_LEFT_FAST))
|
|
|
|
|
{
|
|
|
|
|
back_word();
|
|
|
|
|
}
|
|
|
|
|
else if(events->count(interface_key::CUSTOM_CTRL_A))
|
|
|
|
|
else if (events->count(interface_key::CUSTOM_CTRL_A))
|
|
|
|
|
{
|
|
|
|
|
cursor_pos = 0;
|
|
|
|
|
}
|
|
|
|
|
else if(events->count(interface_key::CUSTOM_CTRL_E))
|
|
|
|
|
else if (events->count(interface_key::CUSTOM_CTRL_E))
|
|
|
|
|
{
|
|
|
|
|
cursor_pos = entry.size();
|
|
|
|
|
}
|
|
|
|
|
else if(events->count(interface_key::CURSOR_UP))
|
|
|
|
|
else if (events->count(interface_key::CURSOR_UP))
|
|
|
|
|
{
|
|
|
|
|
history_idx--;
|
|
|
|
|
if (history_idx < 0)
|
|
|
|
@ -296,7 +307,7 @@ void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
|
|
|
|
|
entry = get_entry();
|
|
|
|
|
cursor_pos = entry.size();
|
|
|
|
|
}
|
|
|
|
|
else if(events->count(interface_key::CURSOR_DOWN))
|
|
|
|
|
else if (events->count(interface_key::CURSOR_DOWN))
|
|
|
|
|
{
|
|
|
|
|
if (size_t(history_idx) < command_history.size() - 1)
|
|
|
|
|
{
|
|
|
|
@ -321,8 +332,8 @@ command_result show_prompt(color_ostream &out, std::vector <std::string> & param
|
|
|
|
|
return CR_OK;
|
|
|
|
|
}
|
|
|
|
|
std::string params;
|
|
|
|
|
for(size_t i=0;i<parameters.size();i++)
|
|
|
|
|
params+=parameters[i]+" ";
|
|
|
|
|
for(size_t i = 0; i < parameters.size(); i++)
|
|
|
|
|
params += parameters[i] + " ";
|
|
|
|
|
Screen::show(dts::make_unique<viewscreen_commandpromptst>(params), plugin_self);
|
|
|
|
|
return CR_OK;
|
|
|
|
|
}
|
|
|
|
@ -330,21 +341,23 @@ bool hotkey_allow_all(df::viewscreen *top)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
|
{
|
|
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
|
"command-prompt","Shows a command prompt on window.",show_prompt,hotkey_allow_all,
|
|
|
|
|
"command-prompt [entry] - shows a cmd prompt in df window. Entry is used for default prefix (e.g. ':lua')"
|
|
|
|
|
"command-prompt", "Shows a command prompt on window.",
|
|
|
|
|
show_prompt, hotkey_allow_all,
|
|
|
|
|
"command-prompt [entry] - shows a cmd prompt in df window."
|
|
|
|
|
" Entry is used for default prefix (e.g. ':lua')"
|
|
|
|
|
));
|
|
|
|
|
return CR_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange (color_ostream &out, state_change_event e)
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event e)
|
|
|
|
|
{
|
|
|
|
|
return CR_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
|
DFhackCExport command_result plugin_shutdown(color_ostream &out)
|
|
|
|
|
{
|
|
|
|
|
return CR_OK;
|
|
|
|
|
}
|
|
|
|
|