#include #include #include "gfx/types.h" #include "ui/ui_renderer.h" #include "ui/login_screen.h" LoginScreen::LoginScreen(int screen_width, int screen_height) : _screen_width(screen_width), _screen_height(screen_height) {} LoginScreen::~LoginScreen(void) {} void LoginScreen::update(float dt) { /* TODO: */ } void LoginScreen::render(const RenderContext& context) const { UIRenderer* ui_renderer = context.ui_renderer; /* Colours. */ const Color text_color = { 0.8f, 0.8f, 0.8f }; const Color inactive_box_color = { 0.1f, 0.1f, 0.15f }; const Color active_box_color = { 0.15f, 0.15f, 0.2f }; const Color warning_color = { 0.7f, 0.3f, 0.3f }; /* Layout. */ const int box_width = 300; const int box_height = 30; const int center_x = (_screen_width - box_width) / 2; const int start_y = _screen_height / 2 - 100; const Rect username_rect = { center_x, start_y+40, box_width, box_height }; const Rect password_rect = { center_x, start_y+100, box_width, box_height }; const Rect hostname_rect = { center_x, start_y+160, box_width, box_height }; ui_renderer->begin_shapes(); ui_renderer->begin_text(); /* Draw title. */ const char* title = _is_new_account ? "Create Account" : "Login"; float title_width = ui_renderer->get_text_renderer()->get_text_width(title, 1.0f); ui_renderer->render_text(title, (_screen_width-title_width)/2, start_y, text_color); /* Draw input boxes and labels. */ /* Username */ ui_renderer->render_text("Username", username_rect.x, username_rect.y-5, text_color); ui_renderer->draw_rect(username_rect.x, username_rect.y, username_rect.w, username_rect.h, _active_field == 0 ? active_box_color : inactive_box_color); ui_renderer->render_text(_username_input.c_str(), username_rect.x+10, username_rect.y+20, text_color); if(_active_field == 0 && context.show_cursor) { float cursor_x = username_rect.x + 10 + ui_renderer->get_text_renderer()->get_text_width(_username_input.c_str(), 1.0f); ui_renderer->draw_rect((int)cursor_x, username_rect.y+5, 2, 20, text_color); } /* Password. */ ui_renderer->render_text("Password", password_rect.x, password_rect.y-5, text_color); ui_renderer->draw_rect(password_rect.x, password_rect.y, password_rect.w, password_rect.h, _active_field == 1 ? active_box_color : inactive_box_color); ui_renderer->render_text(_password_input.c_str(), password_rect.x+10, password_rect.y+20, text_color); if(_active_field == 1 && context.show_cursor) { float cursor_x = password_rect.x + 10 + ui_renderer->get_text_renderer()->get_text_width(_password_input.c_str(), 1.0f); ui_renderer->draw_rect((int)cursor_x, password_rect.y+5, 2, 20, text_color); } /* Hostname (only for new account). */ if(_is_new_account) { ui_renderer->render_text("Hostname", hostname_rect.x, hostname_rect.y-5, text_color); ui_renderer->draw_rect(hostname_rect.x, hostname_rect.y, hostname_rect.w, hostname_rect.h, _active_field == 2 ? active_box_color : inactive_box_color); ui_renderer->render_text(_hostname_input.c_str(), hostname_rect.x+10, hostname_rect.y+20, text_color); if(_active_field == 2 && context.show_cursor) { float cursor_x = hostname_rect.x + 10 + ui_renderer->get_text_renderer()->get_text_width(_hostname_input.c_str(), 1.0f); ui_renderer->draw_rect((int)cursor_x, hostname_rect.y+5, 2, 20, text_color); } } /* Draw the security warning. */ const char* warning_title = "Security Warning"; float warning_title_width = ui_renderer->get_text_renderer()->get_text_width(warning_title, 1.0f); ui_renderer->render_text(warning_title, (_screen_width-warning_title_width)/2, _screen_height-120, warning_color); const std::vector warning_lines = { "Passwords in Bettola are stored in plain text by design.", "Players, can, and will attempt to obtain your password.", "DO NOT use a password you have used for any other game or service.", "Use a unique, throwaway password for this game only!" }; for(size_t i = 0; i < warning_lines.size(); ++i) { const std::string& line = warning_lines[i]; float line_width = ui_renderer->get_text_renderer()->get_text_width(line.c_str(), 1.0f); ui_renderer->render_text(line.c_str(), (_screen_width-line_width)/2, _screen_height-100+(i*20), warning_color); } ui_renderer->flush_shapes(); ui_renderer->flush_text(); } void LoginScreen::handle_event(const SDL_Event* event) { if(event->type == SDL_EVENT_TEXT_INPUT) { /* Append character to active input string. */ switch(_active_field) { case 0: _username_input += event->text.text; break; case 1: _password_input += event->text.text; break; case 2: if(_is_new_account) { _hostname_input += event->text.text; } break; } } else if(event->type == SDL_EVENT_KEY_DOWN) { if(event->key.key == SDLK_BACKSPACE) { /* Handle backspace. */ switch(_active_field) { case 0: if(!_username_input.empty()) { _username_input.pop_back(); } break; case 1: if(!_password_input.empty()) { _password_input.pop_back(); } break; case 2: if(_is_new_account && !_hostname_input.empty()) { _hostname_input.pop_back(); } break; } } else if(event->key.key == SDLK_TAB) { /* Tab to switch fields. */ int num_fields = _is_new_account ? 3 : 2; _active_field = (_active_field+1) % num_fields; } else if(event->key.key == SDLK_RETURN) { _login_attempted = true; } } else if(event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) { /* Handle mouse clicks. */ int mouse_x = event->button.x; int mouse_y = event->button.y; /* Recalculate rects to check for clicks. */ const int box_width = 300; const int start_y = _screen_height / 2 - 100; const int center_x = (_screen_width - box_width) / 2; const Rect username_rect = { center_x, start_y+40, box_width, 30 }; const Rect password_rect = { center_x, start_y+100, box_width, 30 }; const Rect hostname_rect = { center_x, start_y+160, box_width, 30 }; if(mouse_y >= username_rect.y && mouse_y <= username_rect.y + username_rect.h) _active_field = 0; else if(mouse_y >= password_rect.y && mouse_y <= password_rect.y + password_rect.h) _active_field = 1; else if(_is_new_account && (mouse_y >= hostname_rect.y && mouse_y <= hostname_rect.y + hostname_rect.h)) _active_field = 2; } }