Skip to content

Commit

Permalink
Scale login window.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbroad committed Oct 1, 2017
1 parent ae1b0d5 commit 31ca763
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 64 deletions.
156 changes: 93 additions & 63 deletions loginwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,42 @@ static int login_screen_menus;

static char log_in_error_str[520] = {0};

int username_text_x;
int username_text_y;

int password_text_x;
int password_text_y;

int username_bar_x;
int username_bar_y;
int username_bar_x_len = 174;
int username_bar_y_len = 28;

int password_bar_x;
int password_bar_y;
int password_bar_x_len = 174;
int password_bar_y_len = 28;

int log_in_x;
int log_in_y;
int log_in_x_len = 87;
int log_in_y_len = 35;

int new_char_x;
int new_char_y;
int new_char_x_len = 138;
int new_char_y_len = 35;

int settings_x;
int settings_y;
int settings_x_len = 87;
int settings_y_len = 35;

char log_in_button_selected = 0;
char new_char_button_selected = 0;
char settings_button_selected = 0;

void init_login_screen ()
static int username_text_x;
static int username_text_y;

static int password_text_x;
static int password_text_y;

static int username_bar_x;
static int username_bar_y;
static int username_bar_x_len = 0;
static int username_bar_y_len = 0;

static int password_bar_x;
static int password_bar_y;
static int password_bar_x_len = 0;
static int password_bar_y_len = 0;

static int log_in_x;
static int log_in_y;
static int log_in_x_len = 0;
static int log_in_y_len = 0;

static int new_char_x;
static int new_char_y;
static int new_char_x_len = 0;
static int new_char_y_len = 0;

static int settings_x;
static int settings_y;
static int settings_x_len = 0;
static int settings_y_len = 0;

static char log_in_button_selected = 0;
static char new_char_button_selected = 0;
static char settings_button_selected = 0;

void init_login_screen (void)
{
CHECK_GL_ERRORS();
login_screen_menus = load_texture_cached("textures/login_menu.dds", tt_image);
Expand All @@ -92,43 +92,59 @@ void set_login_error (const char *msg, int len, int print_err)
{
safe_strncpy2 (log_in_error_str, msg, sizeof (log_in_error_str), len);
}
reset_soft_breaks (log_in_error_str, strlen (log_in_error_str), sizeof (log_in_error_str), 1.0, window_width, NULL, NULL);

#ifdef NEW_SOUND
if ((snd = get_index_for_sound_type_name("Login Error")) > -1)
add_sound_object(snd, 0, 0, 1);
#endif // NEW_SOUND
}

int resize_login_handler (window_info *win, Uint32 w, Uint32 h)
static int resize_login_handler (window_info *win, Uint32 w, Uint32 h)
{
int s130 = (int)(0.5 + win->current_scale * 130);
int s100 = (int)(0.5 + win->current_scale * 100);
int s50 = (int)(0.5 + win->current_scale * 50);
int s20 = (int)(0.5 + win->current_scale * 20);
int s16 = (int)(0.5 + win->current_scale * 16);
int s10 = (int)(0.5 + win->current_scale * 10);
int s7 = (int)(0.5 + win->current_scale * 7);
int half_screen_x = w / 2;
int half_screen_y = h / 2;
int len1 = strlen (login_username_str);
int len2 = strlen (login_password_str);
int offset = 20 + (len1 > len2 ? (len1+1) * 16 : (len2+1) * 16);
int offset = s20 + (len1 > len2 ? (len1+1) * s16 : (len2+1) * s16);

username_text_x = half_screen_x - offset;
username_text_y = half_screen_y - 130;
username_text_y = half_screen_y - s130;

password_text_x = half_screen_x - offset;
password_text_y = half_screen_y - 100;
password_text_y = half_screen_y - s100;

username_bar_x_len = MAX_USERNAME_LENGTH * win->default_font_len_x;
username_bar_y_len = win->default_font_len_y + s10;
username_bar_x = half_screen_x;
username_bar_y = username_text_y - 7;
username_bar_y = username_text_y - s7;

password_bar_x_len = MAX_USERNAME_LENGTH * win->default_font_len_x;;
password_bar_y_len = win->default_font_len_y + s10;
password_bar_x = half_screen_x;
password_bar_y = password_text_y - 7;
password_bar_y = password_text_y - s7;

log_in_x = username_text_x;
log_in_y = half_screen_y - 50;
log_in_y = half_screen_y - s50;
log_in_x_len = (int)(0.5 + win->current_scale * 87);
log_in_y_len = (int)(0.5 + win->current_scale * 35);

settings_x_len = (int)(0.5 + win->current_scale * 87);
settings_y_len = (int)(0.5 + win->current_scale * 35);
settings_x = username_bar_x + username_bar_x_len - settings_x_len;
settings_y = half_screen_y - 50;

settings_y = half_screen_y - s50;

new_char_x_len = (int)(0.5 + win->current_scale * 138);
new_char_y_len = (int)(0.5 + win->current_scale * 35);
new_char_x = log_in_x + ((settings_x + settings_x_len) - log_in_x)/2 - new_char_x_len/2;
new_char_y = half_screen_y - 50;
new_char_y = half_screen_y - s50;

return 1;
}

Expand All @@ -137,7 +153,7 @@ int resize_login_handler (window_info *win, Uint32 w, Uint32 h)
// need to know the positions of the buttons and input fields. The other option
// was to pass (a struct of) 24 integers to draw_login_screen, which seemed a
// bit excessive.
int display_login_handler (window_info *win)
static int display_login_handler (window_info *win)
{
int num_lines;
float selected_bar_u_start = (float)0/256;
Expand Down Expand Up @@ -191,11 +207,11 @@ int display_login_handler (window_info *win)
draw_console_pic(login_text);

// ok, start drawing the interface...
draw_string (username_text_x, username_text_y, (unsigned char*)login_username_str, 1);
draw_string (password_text_x, password_text_y, (unsigned char*)login_password_str, 1);
draw_string_zoomed (username_text_x, username_text_y, (unsigned char*)login_username_str, 1, win->current_scale);
draw_string_zoomed (password_text_x, password_text_y, (unsigned char*)login_password_str, 1, win->current_scale);

num_lines = reset_soft_breaks(login_rules_str, strlen(login_rules_str), sizeof(login_rules_str), 1, settings_x + settings_x_len - username_text_x, NULL, NULL);
draw_string_zoomed(username_text_x, log_in_y + 60, (unsigned char*)login_rules_str, num_lines, 1);
num_lines = reset_soft_breaks(login_rules_str, strlen(login_rules_str), sizeof(login_rules_str), win->current_scale, settings_x + settings_x_len - username_text_x, NULL, NULL);
draw_string_zoomed(username_text_x, log_in_y + log_in_y_len + win->default_font_len_y + 2, (unsigned char*)login_rules_str, num_lines, win->current_scale);

// start drawing the actual interface pieces
bind_texture(login_screen_menus);
Expand Down Expand Up @@ -235,18 +251,23 @@ int display_login_handler (window_info *win)
glEnd();

glColor3f (0.0f, 0.9f, 1.0f);
draw_string (username_bar_x + 4, username_text_y, (unsigned char*)username_str, 1);
draw_string (password_bar_x + 4, password_text_y, (unsigned char*)display_password_str, 1);
glColor3f (1.0f, 0.0f, 0.0f);
draw_string_zoomed (username_bar_x + win->default_font_len_x / 2, username_text_y, (unsigned char*)username_str, 1, win->current_scale);
draw_string_zoomed (password_bar_x + win->default_font_len_x / 2, password_text_y, (unsigned char*)display_password_str, 1, win->current_scale);

// print the current error, if any
draw_string (0, log_in_y + 40, (unsigned char*)log_in_error_str, 5);
if (strlen (log_in_error_str))
{
glColor3f (1.0f, 0.0f, 0.0f);
num_lines = reset_soft_breaks (log_in_error_str, strlen (log_in_error_str), sizeof (log_in_error_str), win->current_scale, window_width - 2 * win->default_font_len_x, NULL, NULL);
draw_string_zoomed (win->default_font_len_x, log_in_y + log_in_y_len + 2, (unsigned char*)log_in_error_str, num_lines, win->current_scale);
}

CHECK_GL_ERRORS ();
draw_delay = 20;
return 1;
}

int mouseover_login_handler (window_info *win, int mx, int my)
static int mouseover_login_handler (window_info *win, int mx, int my)
{
// check to see if the log in button is active, or not
if (mx >= log_in_x && mx <= log_in_x + log_in_x_len && my >= log_in_y && my <= log_in_y + log_in_y_len && username_str[0] && password_str[0])
Expand All @@ -269,7 +290,7 @@ int mouseover_login_handler (window_info *win, int mx, int my)
return 1;
}

int click_login_handler (window_info *win, int mx, int my, Uint32 flags)
static int click_login_handler (window_info *win, int mx, int my, Uint32 flags)
{
int left_click = flags & ELW_LEFT_MOUSE;
extern int force_elconfig_win_ontop;
Expand Down Expand Up @@ -321,7 +342,7 @@ int click_login_handler (window_info *win, int mx, int my, Uint32 flags)
return 1;
}

int keypress_login_handler (window_info *win, int mx, int my, Uint32 key, Uint32 unikey)
static int keypress_login_handler (window_info *win, int mx, int my, Uint32 key, Uint32 unikey)
{
Uint8 ch = key_to_char (unikey);

Expand All @@ -344,16 +365,18 @@ int keypress_login_handler (window_info *win, int mx, int my, Uint32 key, Uint32
else if (username_box_selected)
{
add_char_to_username (ch);
log_in_error_str[0] = '\0';
}
else
{
add_char_to_password (ch);
log_in_error_str[0] = '\0';
}

return 1;
}

int show_login_handler(window_info * win)
static int show_login_handler(window_info * win)
{
hide_window(book_win);
hide_window(paper_win);
Expand All @@ -362,18 +385,25 @@ int show_login_handler(window_info * win)
return 1;
}

static int ui_scale_login_handler(window_info *win)
{
resize_window(win->window_id, win->len_x, win->len_y);
return 1;
}

void create_login_root_window (int width, int height)
{
if (login_root_win < 0)
{
login_root_win = create_window ("Login", -1, -1, 0, 0, width, height, ELW_TITLE_NONE|ELW_SHOW_LAST);
login_root_win = create_window ("Login", -1, -1, 0, 0, width, height, ELW_USE_UISCALE|ELW_TITLE_NONE|ELW_SHOW_LAST);

set_window_handler (login_root_win, ELW_HANDLER_DISPLAY, &display_login_handler);
set_window_handler (login_root_win, ELW_HANDLER_MOUSEOVER, &mouseover_login_handler);
set_window_handler (login_root_win, ELW_HANDLER_CLICK, &click_login_handler);
set_window_handler (login_root_win, ELW_HANDLER_KEYPRESS, &keypress_login_handler);
set_window_handler (login_root_win, ELW_HANDLER_RESIZE, &resize_login_handler);
set_window_handler (login_root_win, ELW_HANDLER_SHOW, &show_login_handler);
set_window_handler (login_root_win, ELW_HANDLER_UI_SCALE, &ui_scale_login_handler);

resize_window (login_root_win, width, height);
}
Expand Down
2 changes: 1 addition & 1 deletion loginwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern char password_box_selected; /*!< true, if the cursor is currently in the
*
* \callgraph
*/
void init_login_screen ();
void init_login_screen (void);

/*!
* \ingroup interface_login
Expand Down

0 comments on commit 31ca763

Please sign in to comment.