Add status message functionality

This commit is contained in:
Josh Holtrop 2018-03-23 20:13:35 -04:00
parent ec6d0eb8b7
commit 14fd4b96f7
2 changed files with 82 additions and 1 deletions

View File

@ -16,6 +16,8 @@
/* Process multiple queued events for up to this long before stopping to
* redraw the screen. */
#define MAX_EVENT_PROCESS_TIME 50000
/* Amount of time until a status message fades out */
#define STATUS_TIME 4000
/**
* Initialize OpenGL.
@ -137,6 +139,7 @@ bool Window::create(std::shared_ptr<Buffer> buffer)
m_command_buffer_pane->set_command_mode();
m_command_buffer_screen_rows = 1;
m_command_invalid = false;
m_status_message_timer = (size_t)-1;
resize(INITIAL_WIDTH, INITIAL_HEIGHT);
@ -213,6 +216,13 @@ void Window::handle_event(Jtk_Event & event)
m_focused_buffer_pane->scroll_window_down(ScrollMode::WHEEL);
}
break;
case JTK_EVENT_TIMER:
if (event.timer.timer_id == m_status_message_timer)
{
handle_status_message_timer();
}
break;
}
}
@ -265,6 +275,7 @@ void Window::handle_keypress(uint32_t keyval)
}
else
{
clear_status();
m_command_input.push_back(ctrl_keyval);
evaluate_command_input();
}
@ -468,6 +479,10 @@ void Window::redraw()
{
m_command_buffer_pane->draw();
}
else if (m_status_message_timer != (size_t)-1)
{
draw_status_message();
}
else
{
draw_command_input();
@ -476,6 +491,40 @@ void Window::redraw()
Jtk_SwapBuffers(m_window);
}
void Window::draw_status_message()
{
int x = 0;
uint64_t current_time = Jtk_UsTime();
float alpha;
if (((current_time - m_status_message_basetime) / 1000) < (STATUS_TIME / 2))
{
alpha = 1.0;
}
else
{
alpha = 1.0 - (((current_time - m_status_message_basetime) / 1000) - (STATUS_TIME / 2)) / (float)(STATUS_TIME / 2);
if (alpha < 0.0)
{
alpha = 0.0;
}
}
for (size_t i = 0u, length = m_status_message.size(); i < length; i++)
{
m_gl->draw_character(x, 0, m_status_message[i], *m_font, 1.0, 1.0, 1.0, alpha);
x += m_font->get_advance();
}
}
void Window::handle_status_message_timer()
{
if (((Jtk_UsTime() - m_status_message_basetime) / 1000) >= STATUS_TIME)
{
Jtk_RemoveTimer(m_status_message_timer);
m_status_message_timer = (size_t)-1;
}
m_redraw_requested = true;
}
void Window::draw_command_input()
{
int x = 0;
@ -518,6 +567,23 @@ static bool is_number(const EncodedString & s)
return true;
}
void Window::set_status(const std::string & status)
{
clear_status();
m_status_message = status;
m_status_message_basetime = Jtk_UsTime();
m_status_message_timer = Jtk_AddTimer(STATUS_TIME / 2, 50u, nullptr, nullptr);
}
void Window::clear_status()
{
if (m_status_message_timer != (size_t)-1)
{
Jtk_RemoveTimer(m_status_message_timer);
m_status_message_timer = (size_t)-1;
}
}
void Window::handle_command(const EncodedString & command)
{
CommandParser cp;
@ -572,8 +638,16 @@ void Window::command_write_file(const CommandParser & cp)
}
if (path != "")
{
buffer->write_to_file(Path::clean(path).c_str());
if (buffer->write_to_file(Path::clean(path).c_str()))
{
set_status("Wrote file");
}
else
{
set_status("Error writing file");
}
}
m_redraw_requested = true;
}
void Window::command_quit(const CommandParser & cp)

View File

@ -52,10 +52,14 @@ protected:
void evaluate_command_input();
bool check_insert_mode_command(uint32_t insert_mode_command);
void execute_command(const Command & command);
void handle_status_message_timer();
void draw_command_input();
void draw_status_message();
void command_write_file(const CommandParser & cp);
void command_quit(const CommandParser & cp);
void set_status(const std::string & status);
void clear_status();
void * m_window;
bool m_exit_requested;
@ -74,6 +78,9 @@ protected:
std::shared_ptr<Buffer> m_command_buffer;
std::shared_ptr<BufferPane> m_command_buffer_pane;
std::vector<uint32_t> m_command_input;
std::string m_status_message;
size_t m_status_message_timer;
uint64_t m_status_message_basetime;
bool m_command_invalid;
};