/// speed (pixels/s) the console closes
static const float FADE_SPEED = 1;
-Console::Console() :
+ConsoleBuffer::ConsoleBuffer() :
+ m_lines()
+{
+}
+
+void
+ConsoleBuffer::addLines(const std::string& s)
+{
+ std::istringstream iss(s);
+ std::string line;
+ while (std::getline(iss, line, '\n'))
+ {
+ addLine(line);
+ }
+}
+
+void
+ConsoleBuffer::addLine(const std::string& s_)
+{
+ std::string s = s_;
+
+ // output line to stderr
+ std::cerr << s << std::endl;
+
+ // wrap long lines
+ std::string overflow;
+ int line_count = 0;
+ do {
+ m_lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
+ line_count += 1;
+ s = overflow;
+ } while (s.length() > 0);
+
+ // trim scrollback buffer
+ while (m_lines.size() >= 1000)
+ {
+ m_lines.pop_back();
+ }
+
+ if (Console::current())
+ {
+ Console::current()->on_buffer_change(line_count);
+ }
+}
+
+void
+ConsoleBuffer::flush(ConsoleStreamBuffer& buffer)
+{
+ if (&buffer == &s_outputBuffer)
+ {
+ std::string s = s_outputBuffer.str();
+ if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')))
+ {
+ while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))
+ {
+ s.erase(s.length()-1);
+ }
+ addLines(s);
+ s_outputBuffer.str(std::string());
+ }
+ }
+}
+
+Console::Console(ConsoleBuffer& buffer) :
+ m_buffer(buffer),
+ m_inputBuffer(),
+ m_inputBufferPosition(0),
m_history(),
m_history_position(m_history.end()),
- m_lines(),
m_background(Surface::create("images/engine/console.png")),
m_background2(Surface::create("images/engine/console2.png")),
m_vm(NULL),
}
void
-Console::flush(ConsoleStreamBuffer* buffer)
+Console::on_buffer_change(int line_count)
{
- if (buffer == &outputBuffer) {
- std::string s = outputBuffer.str();
- if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
- while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
- addLines(s);
- outputBuffer.str(std::string());
+ // increase console height if necessary
+ if (m_stayOpen > 0 && m_height < 64)
+ {
+ if(m_height < 4)
+ {
+ m_height = 4;
}
+ m_height += m_font->get_height() * line_count;
}
+
+ // reset console to full opacity
+ m_alpha = 1.0;
}
void
throw SquirrelError(m_vm, "Problem while executing command");
if(sq_gettype(m_vm, -1) != OT_NULL)
- addLines(squirrel2string(m_vm, -1));
+ m_buffer.addLines(squirrel2string(m_vm, -1));
} catch(std::exception& e) {
- addLines(e.what());
+ m_buffer.addLines(e.what());
}
SQInteger newtop = sq_gettop(m_vm);
if(newtop < oldtop) {
void
Console::input(char c)
{
- inputBuffer.insert(inputBufferPosition, 1, c);
- inputBufferPosition++;
+ m_inputBuffer.insert(m_inputBufferPosition, 1, c);
+ m_inputBufferPosition++;
}
void
Console::backspace()
{
- if ((inputBufferPosition > 0) && (inputBuffer.length() > 0)) {
- inputBuffer.erase(inputBufferPosition-1, 1);
- inputBufferPosition--;
+ if ((m_inputBufferPosition > 0) && (m_inputBuffer.length() > 0)) {
+ m_inputBuffer.erase(m_inputBufferPosition-1, 1);
+ m_inputBufferPosition--;
}
}
void
Console::eraseChar()
{
- if (inputBufferPosition < (int)inputBuffer.length()) {
- inputBuffer.erase(inputBufferPosition, 1);
+ if (m_inputBufferPosition < (int)m_inputBuffer.length()) {
+ m_inputBuffer.erase(m_inputBufferPosition, 1);
}
}
void
Console::enter()
{
- addLines("> "+inputBuffer);
- parse(inputBuffer);
- inputBuffer = "";
- inputBufferPosition = 0;
+ m_buffer.addLines("> " + m_inputBuffer);
+ parse(m_inputBuffer);
+ m_inputBuffer = "";
+ m_inputBufferPosition = 0;
}
void
offset_++;
}
if (m_history_position == m_history.end()) {
- inputBuffer = "";
- inputBufferPosition = 0;
+ m_inputBuffer = "";
+ m_inputBufferPosition = 0;
} else {
- inputBuffer = *m_history_position;
- inputBufferPosition = inputBuffer.length();
+ m_inputBuffer = *m_history_position;
+ m_inputBufferPosition = m_inputBuffer.length();
}
}
void
Console::move_cursor(int offset_)
{
- if (offset_ == -65535) inputBufferPosition = 0;
- if (offset_ == +65535) inputBufferPosition = inputBuffer.length();
- inputBufferPosition+=offset_;
- if (inputBufferPosition < 0) inputBufferPosition = 0;
- if (inputBufferPosition > (int)inputBuffer.length()) inputBufferPosition = inputBuffer.length();
+ if (offset_ == -65535) m_inputBufferPosition = 0;
+ if (offset_ == +65535) m_inputBufferPosition = m_inputBuffer.length();
+ m_inputBufferPosition+=offset_;
+ if (m_inputBufferPosition < 0) m_inputBufferPosition = 0;
+ if (m_inputBufferPosition > (int)m_inputBuffer.length()) m_inputBufferPosition = m_inputBuffer.length();
}
// Helper functions for Console::autocomplete
void
Console::autocomplete()
{
- //int autocompleteFrom = inputBuffer.find_last_of(" ();+", inputBufferPosition);
- int autocompleteFrom = inputBuffer.find_last_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_->.", inputBufferPosition);
+ //int autocompleteFrom = m_inputBuffer.find_last_of(" ();+", m_inputBufferPosition);
+ int autocompleteFrom = m_inputBuffer.find_last_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_->.", m_inputBufferPosition);
if (autocompleteFrom != (int)std::string::npos) {
autocompleteFrom += 1;
} else {
autocompleteFrom = 0;
}
- std::string prefix = inputBuffer.substr(autocompleteFrom, inputBufferPosition - autocompleteFrom);
- addLines("> "+prefix);
+ std::string prefix = m_inputBuffer.substr(autocompleteFrom, m_inputBufferPosition - autocompleteFrom);
+ m_buffer.addLines("> " + prefix);
std::list<std::string> cmds;
sq_pop(m_vm, 1); // remove table
// depending on number of hits, show matches or autocomplete
- if (cmds.empty()) addLines("No known command starts with \""+prefix+"\"");
- if (cmds.size() == 1) {
+ if (cmds.empty())
+ {
+ m_buffer.addLines("No known command starts with \"" + prefix + "\"");
+ }
+
+ if (cmds.size() == 1)
+ {
// one match: just replace input buffer with full command
std::string replaceWith = cmds.front();
- inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
- inputBufferPosition += (replaceWith.length() - prefix.length());
+ m_inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
+ m_inputBufferPosition += (replaceWith.length() - prefix.length());
}
- if (cmds.size() > 1) {
+
+ if (cmds.size() > 1)
+ {
// multiple matches: show all matches and set input buffer to longest common prefix
std::string commonPrefix = cmds.front();
while (cmds.begin() != cmds.end()) {
std::string cmd = cmds.front();
cmds.pop_front();
- addLines(cmd);
+ m_buffer.addLines(cmd);
for (int n = commonPrefix.length(); n >= 1; n--) {
if (cmd.compare(0, n, commonPrefix) != 0) commonPrefix.resize(n-1); else break;
}
}
std::string replaceWith = commonPrefix;
- inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
- inputBufferPosition += (replaceWith.length() - prefix.length());
- }
-}
-
-void
-Console::addLines(std::string s)
-{
- std::istringstream iss(s);
- std::string line;
- while (std::getline(iss, line, '\n')) addLine(line);
-}
-
-void
-Console::addLine(std::string s)
-{
- // output line to stderr
- std::cerr << s << std::endl;
-
- // wrap long lines
- std::string overflow;
- unsigned int line_count = 0;
- do {
- m_lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
- line_count++;
- s = overflow;
- } while (s.length() > 0);
-
- // trim scrollback buffer
- while (m_lines.size() >= 1000)
- m_lines.pop_back();
-
- // increase console height if necessary
- if ((m_stayOpen > 0) && (m_height < 64)) {
- if(m_height < 4)
- m_height = 4;
- m_height += m_font->get_height() * line_count;
+ m_inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
+ m_inputBufferPosition += (replaceWith.length() - prefix.length());
}
-
- // reset console to full opacity
- m_alpha = 1.0;
}
void
try {
execute_script(s);
} catch(std::exception& e) {
- addLines(e.what());
+ m_buffer.addLines(e.what());
}
-
}
bool
m_stayOpen = 0;
// clear input buffer
- inputBuffer = "";
- inputBufferPosition = 0;
+ m_inputBuffer = "";
+ m_inputBufferPosition = 0;
// SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
}
context.push_transform();
context.set_alpha(m_alpha);
- context.draw_surface(m_background2,
- Vector(SCREEN_WIDTH/2 - m_background->get_width()/2 - m_background->get_width() + m_backgroundOffset,
- m_height - m_background->get_height()),
+ context.draw_surface(m_background2,
+ Vector(SCREEN_WIDTH/2 - m_background->get_width()/2 - m_background->get_width() + m_backgroundOffset,
+ m_height - m_background->get_height()),
layer);
- context.draw_surface(m_background2,
+ context.draw_surface(m_background2,
Vector(SCREEN_WIDTH/2 - m_background->get_width()/2 + m_backgroundOffset,
- m_height - m_background->get_height()),
+ m_height - m_background->get_height()),
layer);
- for (int x = (SCREEN_WIDTH/2 - m_background->get_width()/2
+ for (int x = (SCREEN_WIDTH/2 - m_background->get_width()/2
- (static_cast<int>(ceilf((float)SCREEN_WIDTH /
- (float)m_background->get_width()) - 1) * m_background->get_width()));
- x < SCREEN_WIDTH;
+ (float)m_background->get_width()) - 1) * m_background->get_width()));
+ x < SCREEN_WIDTH;
x += m_background->get_width())
{
context.draw_surface(m_background, Vector(x, m_height - m_background->get_height()), layer);
if (m_focused) {
lineNo++;
float py = m_height-4-1 * m_font->get_height();
- context.draw_text(m_font, "> "+inputBuffer, Vector(4, py), ALIGN_LEFT, layer);
+ context.draw_text(m_font, "> "+m_inputBuffer, Vector(4, py), ALIGN_LEFT, layer);
if (SDL_GetTicks() % 1000 < 750) {
- int cursor_px = 2 + inputBufferPosition;
+ int cursor_px = 2 + m_inputBufferPosition;
context.draw_text(m_font, "_", Vector(4 + (cursor_px * m_font->get_text_width("X")), py), ALIGN_LEFT, layer);
}
}
int skipLines = -m_offset;
- for (std::list<std::string>::iterator i = m_lines.begin(); i != m_lines.end(); i++) {
+ for (std::list<std::string>::iterator i = m_buffer.m_lines.begin(); i != m_buffer.m_lines.end(); i++)
+ {
if (skipLines-- > 0) continue;
lineNo++;
float py = m_height - 4 - lineNo * m_font->get_height();
context.pop_transform();
}
-int Console::inputBufferPosition = 0;
-std::string Console::inputBuffer;
-ConsoleStreamBuffer Console::outputBuffer;
-std::ostream Console::output(&Console::outputBuffer);
+ConsoleStreamBuffer ConsoleBuffer::s_outputBuffer;
+std::ostream ConsoleBuffer::output(&ConsoleBuffer::s_outputBuffer);
/* EOF */
class ConsoleCommandReceiver;
class DrawingContext;
+class ConsoleBuffer : public Currenton<ConsoleBuffer>
+{
+public:
+ static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
+ static ConsoleStreamBuffer s_outputBuffer; /**< stream buffer used by output stream */
+
+public:
+ std::list<std::string> m_lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
+
+public:
+ ConsoleBuffer();
+
+ void addLines(const std::string& s); /**< display a string of (potentially) multiple lines in the console */
+ void addLine(const std::string& s); /**< display a line in the console */
+
+ void flush(ConsoleStreamBuffer& buffer); /**< act upon changes in a ConsoleStreamBuffer */
+
+private:
+ ConsoleBuffer(const ConsoleBuffer&) = delete;
+ ConsoleBuffer& operator=(const ConsoleBuffer&) = delete;
+};
+
class Console : public Currenton<Console>
{
public:
- Console();
+ Console(ConsoleBuffer& buffer);
~Console();
- static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
+ void on_buffer_change(int line_count);
void input(char c); /**< add character to inputBuffer */
void backspace(); /**< delete character left of inputBufferPosition */
bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
private:
+ ConsoleBuffer& m_buffer;
+
+ std::string m_inputBuffer; /**< string used for keyboard input */
+ int m_inputBufferPosition; /**< position in inputBuffer before which to append new characters */
+
std::list<std::string> m_history; /**< command history. New lines get added to back. */
std::list<std::string>::iterator m_history_position; /**< item of command history that is currently displayed */
- std::list<std::string> m_lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
SurfacePtr m_background; /**< console background image */
SurfacePtr m_background2; /**< second, moving console background image */
float m_stayOpen;
- static int inputBufferPosition; /**< position in inputBuffer before which to append new characters */
- static std::string inputBuffer; /**< string used for keyboard input */
- static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
-
- void addLines(std::string s); /**< display a string of (potentially) multiple lines in the console */
- void addLine(std::string s); /**< display a line in the console */
void parse(std::string s); /**< react to a given command */
/** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< process internal command; return false if command was unknown, true otherwise */
- friend class ConsoleStreamBuffer;
- void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
-
private:
Console(const Console&);
Console & operator=(const Console&);
int sync()
{
int result = std::stringbuf::sync();
- if(Console::current())
- Console::current()->flush(this);
+ if(ConsoleBuffer::current())
+ ConsoleBuffer::current()->flush(*this);
return result;
}
};