Skip to content

Commit

Permalink
File dialog tweaks (#929)
Browse files Browse the repository at this point in the history
* Move clear filter button to be on top of scrollbar.
Move file sort button under file list.
Right-align "Proceed?" text.

* Code tweaks per Crudelios.
  • Loading branch information
PrettyFlower authored Aug 5, 2023
1 parent 6eeaa81 commit 562725d
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/graphics/generic_button.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "generic_button.h"

#include "graphics/lang_text.h"

static int get_button(const mouse *m, int x, int y, generic_button *buttons, int num_buttons)
{
for (int i = 0; i < num_buttons; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/graphics/generic_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GRAPHICS_CUSTOM_BUTTON_H

#include "graphics/button.h"
#include "graphics/font.h"
#include "input/mouse.h"

typedef struct {
Expand Down
6 changes: 6 additions & 0 deletions src/graphics/lang_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ void lang_text_draw_centered(int group, int number, int x_offset, int y_offset,
text_draw_centered(str, x_offset, y_offset, box_width, font, 0);
}

void lang_text_draw_right_aligned(int group, int number, int x_offset, int y_offset, int box_width, font_t font)
{
const uint8_t *str = lang_get_string(group, number);
text_draw_right_aligned(str, x_offset, y_offset, box_width, font, 0);
}

void lang_text_draw_centered_colored(
int group, int number, int x_offset, int y_offset, int box_width, font_t font, color_t color)
{
Expand Down
1 change: 1 addition & 0 deletions src/graphics/lang_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int lang_text_draw(int group, int number, int x_offset, int y_offset, font_t fon
int lang_text_draw_colored(int group, int number, int x_offset, int y_offset, font_t font, color_t color);

void lang_text_draw_centered(int group, int number, int x_offset, int y_offset, int box_width, font_t font);
void lang_text_draw_right_aligned(int group, int number, int x_offset, int y_offset, int box_width, font_t font);
void lang_text_draw_centered_colored(
int group, int number, int x_offset, int y_offset, int box_width, font_t font, color_t color);

Expand Down
9 changes: 9 additions & 0 deletions src/graphics/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ void text_draw_centered(const uint8_t *str, int x, int y, int box_width, font_t
text_draw(str, offset + x, y, font, color);
}

void text_draw_right_aligned(const uint8_t *str, int x, int y, int box_width, font_t font, color_t color)
{
int offset = box_width - text_get_width(str, font);
if (offset < 0) {
offset = 0;
}
text_draw(str, offset + x, y, font, color);
}

int text_draw_ellipsized(const uint8_t *str, int x, int y, int box_width, font_t font, color_t color)
{
static uint8_t buffer[1000];
Expand Down
1 change: 1 addition & 0 deletions src/graphics/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ int text_draw(const uint8_t *str, int x, int y, font_t font, color_t color);
int text_draw_scaled(const uint8_t *str, int x, int y, font_t font, color_t color, float scale);

void text_draw_centered(const uint8_t *str, int x, int y, int box_width, font_t font, color_t color);
void text_draw_right_aligned(const uint8_t *str, int x, int y, int box_width, font_t font, color_t color);
int text_draw_ellipsized(const uint8_t *str, int x, int y, int box_width, font_t font, color_t color);

int text_draw_number(int value, char prefix, const char *postfix, int x, int y, font_t font, color_t color);
Expand Down
7 changes: 6 additions & 1 deletion src/widget/input_box.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <stdlib.h>

#define CLEAR_BUTTON_WIDTH 43

static void button_clear_text(int param1, int param2);

static uint8_t *text_to_clear;
Expand All @@ -22,7 +24,10 @@ void input_box_start(input_box *box)
{
int text_width = (box->width_blocks - 2) * BLOCK_SIZE - 35;
keyboard_start_capture(box->text, box->text_length, box->allow_punctuation, text_width, box->font);
clear_text_button.x_offset = box->x + box->width_blocks * 16 - 43;
clear_text_button.x_offset = box->x + box->width_blocks * 16;
if (!box->put_clear_button_outside_box) {
clear_text_button.x_offset -= CLEAR_BUTTON_WIDTH;
}
clear_text_button.y_offset = box->y + 3;
system_keyboard_set_input_rect(box->x, box->y,
box->width_blocks * BLOCK_SIZE - 35, box->height_blocks * BLOCK_SIZE);
Expand Down
1 change: 1 addition & 0 deletions src/widget/input_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef struct {
int allow_punctuation;
uint8_t *text;
int text_length;
int put_clear_button_outside_box;
const uint8_t *placeholder;
void (*on_change)(int is_addition_at_end);
uint8_t *old_text;
Expand Down
35 changes: 29 additions & 6 deletions src/window/file_dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static generic_button file_buttons[] = {
};

static generic_button sort_by_button[] = {
{32, 439, 288, 26, button_toggle_sort_type, button_none, 0, 0}
{16, 437, 288, 26, button_toggle_sort_type, button_none, 0, 0}
};

static scrollbar_type scrollbar = { 304, 80, 350, 256, NUM_FILES_IN_VIEW, on_scroll, 1 };
Expand Down Expand Up @@ -128,7 +128,15 @@ static struct {
} data;

static input_box main_input = {
16, 48, 18, 2, FONT_NORMAL_WHITE, 0, data.filter_text, FILTER_TEXT_SIZE, 0, input_box_changed
.x = 16,
.y = 48,
.width_blocks = 18,
.height_blocks = 2,
.font = FONT_NORMAL_WHITE,
.text = data.filter_text,
.text_length = FILTER_TEXT_SIZE,
.put_clear_button_outside_box = 1,
.on_change = input_box_changed
};

static const int MISSION_ID_TO_CITY_ID[] = {
Expand Down Expand Up @@ -357,11 +365,20 @@ static void draw_foreground(void)
int text_id = data.dialog_type + (data.type == FILE_TYPE_SCENARIO ? 3 : 0);
lang_text_draw_centered(43, text_id, 32, 14, 554, FONT_LARGE_BLACK);
}
lang_text_draw_centered(43, 5, 362, 447, 164, FONT_NORMAL_BLACK);
// Proceed? text
lang_text_draw_right_aligned(43, 5, 362, 447, 164, FONT_NORMAL_BLACK);

// Sorting text
lang_text_draw_centered(CUSTOM_TRANSLATION, TR_SAVE_DIALOG_SORTING_BY_NAME + data.sort_type,
32, 447, 288, FONT_NORMAL_BLACK);
int sort_translation = TR_SAVE_DIALOG_SORTING_BY_NAME + data.sort_type;
int sort_button_text_y = sort_by_button[0].y + sort_by_button[0].height / 2 - 5;
lang_text_draw_centered(
CUSTOM_TRANSLATION,
sort_translation,
sort_by_button[0].x,
sort_button_text_y,
sort_by_button[0].width,
FONT_NORMAL_BLACK
);

// Saved game info
if (*data.selected_file && data.type != FILE_TYPE_EMPIRE && data.type != FILE_TYPE_SCENARIO_EVENTS
Expand Down Expand Up @@ -419,7 +436,13 @@ static void draw_foreground(void)
}

input_box_draw(&main_input);
button_border_draw(32, 439, 288, 26, data.sort_by_button_focused);
button_border_draw(
sort_by_button[0].x,
sort_by_button[0].y,
sort_by_button[0].width,
sort_by_button[0].height,
data.sort_by_button_focused
);
image_buttons_draw(0, 0, image_buttons, 2);
scrollbar_draw(&scrollbar);
graphics_reset_dialog();
Expand Down

0 comments on commit 562725d

Please sign in to comment.