2014-02-13 14:55:13 -07:00
|
|
|
//command-prompt a one line command entry at the top of the screen for quick commands
|
|
|
|
|
|
|
|
#include "Core.h"
|
|
|
|
#include <Console.h>
|
|
|
|
#include <Export.h>
|
|
|
|
#include <PluginManager.h>
|
2014-03-07 09:52:06 -07:00
|
|
|
#include <ColorText.h>
|
2014-02-13 14:55:13 -07:00
|
|
|
|
|
|
|
#include <modules/Screen.h>
|
|
|
|
|
|
|
|
#include <set>
|
2014-03-07 09:52:06 -07:00
|
|
|
#include <list>
|
|
|
|
#include <utility>
|
2014-02-13 14:55:13 -07:00
|
|
|
|
|
|
|
#include "df/interface_key.h"
|
|
|
|
#include "df/ui.h"
|
|
|
|
#include "df/graphic.h"
|
|
|
|
#include "df/enabler.h"
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
|
|
|
using df::global::ui;
|
|
|
|
using df::global::gps;
|
|
|
|
using df::global::enabler;
|
|
|
|
|
2014-03-07 09:52:06 -07:00
|
|
|
class viewscreen_commandpromptst;
|
|
|
|
class prompt_ostream:public buffered_color_ostream
|
|
|
|
{
|
|
|
|
viewscreen_commandpromptst *parent_;
|
|
|
|
protected:
|
|
|
|
void flush_proxy();
|
|
|
|
public:
|
|
|
|
prompt_ostream(viewscreen_commandpromptst* parent):parent_(parent){}
|
|
|
|
bool empty(){return buffer.empty();}
|
|
|
|
};
|
2014-02-13 14:55:13 -07:00
|
|
|
class viewscreen_commandpromptst : public dfhack_viewscreen {
|
|
|
|
public:
|
|
|
|
void feed(std::set<df::interface_key> *events);
|
|
|
|
|
|
|
|
void logic() {
|
|
|
|
dfhack_viewscreen::logic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void render();
|
|
|
|
void help() { }
|
|
|
|
|
|
|
|
std::string getFocusString() { return "commandprompt"; }
|
2014-03-07 09:52:06 -07:00
|
|
|
viewscreen_commandpromptst(std::string entry):is_response(false),entry(entry)
|
2014-02-13 14:55:13 -07:00
|
|
|
{
|
|
|
|
show_fps=df::global::gps->display_frames;
|
|
|
|
df::global::gps->display_frames=0;
|
|
|
|
}
|
|
|
|
~viewscreen_commandpromptst()
|
|
|
|
{
|
|
|
|
df::global::gps->display_frames=show_fps;
|
|
|
|
}
|
2014-03-07 09:52:06 -07:00
|
|
|
|
|
|
|
void add_response(color_value v,std::string s)
|
|
|
|
{
|
|
|
|
responses.push_back(std::make_pair(v,s));
|
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
protected:
|
2014-03-07 09:52:06 -07:00
|
|
|
std::list<std::pair<color_value,std::string> > responses;
|
|
|
|
bool is_response;
|
2014-02-13 14:55:13 -07:00
|
|
|
bool show_fps;
|
|
|
|
void submit();
|
|
|
|
std::string entry;
|
|
|
|
};
|
2014-03-07 09:52:06 -07:00
|
|
|
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);
|
|
|
|
buffer.clear();
|
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
void viewscreen_commandpromptst::render()
|
|
|
|
{
|
|
|
|
if (Screen::isDismissed(this))
|
|
|
|
return;
|
|
|
|
|
|
|
|
dfhack_viewscreen::render();
|
|
|
|
|
|
|
|
auto dim = Screen::getWindowSize();
|
|
|
|
parent->render();
|
2014-03-07 09:52:06 -07:00
|
|
|
if(is_response)
|
|
|
|
{
|
|
|
|
auto it=responses.begin();
|
|
|
|
for(int i=0;i<dim.y && it!=responses.end();i++,it++)
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
else
|
|
|
|
{
|
2014-03-07 09:52:06 -07:00
|
|
|
Screen::fillRect(Screen::Pen(' ', 7, 0),0,0,dim.x,0);
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 0, 0,"[DFHack]#");
|
|
|
|
if(entry.size()<dim.x)
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 10,0 , entry);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Screen::paintTile(Screen::Pen('>', 7, 0), 9, 0);
|
|
|
|
Screen::paintString(Screen::Pen(' ', 7, 0), 10, 0, entry.substr(entry.size()-dim.x));
|
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void viewscreen_commandpromptst::submit()
|
|
|
|
{
|
|
|
|
CoreSuspendClaimer suspend;
|
2014-03-07 09:52:06 -07:00
|
|
|
if(is_response)
|
|
|
|
{
|
|
|
|
Screen::dismiss(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//color_ostream_proxy out(Core::getInstance().getConsole());
|
|
|
|
prompt_ostream out(this);
|
2014-02-13 14:55:13 -07:00
|
|
|
Core::getInstance().runCommand(out, entry);
|
2014-03-07 09:52:06 -07:00
|
|
|
if(out.empty() && responses.empty())
|
|
|
|
Screen::dismiss(this);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_response=true;
|
|
|
|
}
|
2014-02-13 14:55:13 -07:00
|
|
|
}
|
|
|
|
void viewscreen_commandpromptst::feed(std::set<df::interface_key> *events)
|
|
|
|
{
|
|
|
|
|
|
|
|
bool leave_all = events->count(interface_key::LEAVESCREEN_ALL);
|
|
|
|
if (leave_all || events->count(interface_key::LEAVESCREEN))
|
|
|
|
{
|
|
|
|
events->clear();
|
|
|
|
Screen::dismiss(this);
|
|
|
|
if (leave_all)
|
|
|
|
{
|
|
|
|
events->insert(interface_key::LEAVESCREEN);
|
|
|
|
parent->feed(events);
|
|
|
|
events->clear();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(events->count(interface_key::SELECT))
|
|
|
|
{
|
|
|
|
submit();
|
|
|
|
return;
|
|
|
|
}
|
2014-03-07 09:52:06 -07:00
|
|
|
if(is_response)
|
|
|
|
return;
|
2014-02-13 14:55:13 -07:00
|
|
|
for (auto it = events->begin(); it != events->end(); ++it)
|
|
|
|
{
|
|
|
|
auto key = *it;
|
|
|
|
if (key==interface_key::STRING_A000) //delete?
|
|
|
|
{
|
|
|
|
if(entry.size())
|
2014-04-13 09:18:31 -06:00
|
|
|
entry.resize(entry.size()-1);
|
2014-02-13 14:55:13 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key >= interface_key::STRING_A000 &&
|
|
|
|
key <= interface_key::STRING_A255)
|
|
|
|
{
|
|
|
|
entry.push_back(char(key - interface_key::STRING_A000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DFHACK_PLUGIN("command-prompt");
|
|
|
|
command_result show_prompt(color_ostream &out, std::vector <std::string> & parameters)
|
|
|
|
{
|
2014-03-07 09:52:06 -07:00
|
|
|
std::string params;
|
|
|
|
for(size_t i=0;i<parameters.size();i++)
|
|
|
|
params+=parameters[i]+" ";
|
|
|
|
Screen::show(new viewscreen_commandpromptst(params));
|
2014-02-13 14:55:13 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
|
|
|
commands.push_back(PluginCommand(
|
2014-03-07 09:52:06 -07:00
|
|
|
"command-prompt","Shows a command prompt on window.",show_prompt,false,
|
|
|
|
"command-prompt [entry] - shows a cmd prompt in df window. Entry is used for default prefix (e.g. ':lua')"
|
2014-02-13 14:55:13 -07:00
|
|
|
));
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|