Skip to content

Commit

Permalink
Standardized button sizes in picker dialogs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Espyo committed Feb 3, 2025
1 parent 625a92b commit c2ebb29
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
48 changes: 7 additions & 41 deletions source/source/game_state/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ const float OP_ERROR_CURSOR_THICKNESS = 5.0f;
//Duration of the operation error red flash effect.
const float OP_ERROR_FLASH_DURATION = 1.5f;

//Picker dialog maximum button size.
const float PICKER_IMG_BUTTON_MAX_SIZE = 160.0f;
//Picker dialog button size.
const float PICKER_IMG_BUTTON_SIZE = 168.0f;

//Picker dialog minimum button size.
const float PICKER_IMG_BUTTON_MIN_SIZE = 32.0f;
Expand Down Expand Up @@ -3512,47 +3512,14 @@ void editor::picker_info::process() {

ImGui::BeginGroup();

point bmp_size(
al_get_bitmap_width(i_ptr->bitmap),
al_get_bitmap_height(i_ptr->bitmap)
);
if(bmp_size.x > 0.0f && bmp_size.x > bmp_size.y) {
float ratio = bmp_size.y / bmp_size.x;
button_size =
point(
EDITOR::PICKER_IMG_BUTTON_MAX_SIZE,
EDITOR::PICKER_IMG_BUTTON_MAX_SIZE * ratio
);
} else if(bmp_size.y > 0.0f) {
float ratio = bmp_size.x / bmp_size.y;
button_size =
point(
EDITOR::PICKER_IMG_BUTTON_MAX_SIZE * ratio,
EDITOR::PICKER_IMG_BUTTON_MAX_SIZE
);
}
button_size.x =
std::max(
button_size.x,
EDITOR::PICKER_IMG_BUTTON_MIN_SIZE
);
button_size.y =
std::max(
button_size.y,
EDITOR::PICKER_IMG_BUTTON_MIN_SIZE
);

ImGui::PushStyleVar(
ImGuiStyleVar_FramePadding, ImVec2(4.0f, 4.0f)
);
button_size = point(EDITOR::PICKER_IMG_BUTTON_SIZE);
bool button_pressed =
ImGui::ImageButton(
ImGui::ImageButtonOrganized(
widgetId + "Button",
i_ptr->bitmap,
button_size
button_size - 4.0f, button_size
);
ImGui::PopStyleVar();


if(button_pressed) {
pick_callback(
i_ptr->name, i_ptr->top_category, i_ptr->sec_category, i_ptr->info, false
Expand All @@ -3561,14 +3528,13 @@ void editor::picker_info::process() {
dialog_ptr->is_open = false;
}
}
ImGui::SetNextItemWidth(20.0f);
ImGui::TextWrapped("%s", i_ptr->name.c_str());
ImGui::Dummy(ImVec2(0.0f, 8.0f));
ImGui::EndGroup();

} else {

button_size = point(168.0f, 32.0f);
button_size = point(EDITOR::PICKER_IMG_BUTTON_SIZE, 32.0f);
if(ImGui::Button(i_ptr->name.c_str(), ImVec2(button_size.x, button_size.y))) {
pick_callback(
i_ptr->name, i_ptr->top_category, i_ptr->sec_category, i_ptr->info, false
Expand Down
3 changes: 1 addition & 2 deletions source/source/game_state/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ extern const float OP_ERROR_CURSOR_SHAKE_WIDTH;
extern const float OP_ERROR_CURSOR_SIZE;
extern const float OP_ERROR_CURSOR_THICKNESS;
extern const float OP_ERROR_FLASH_DURATION;
extern const float PICKER_IMG_BUTTON_MAX_SIZE;
extern const float PICKER_IMG_BUTTON_MIN_SIZE;
extern const float PICKER_IMG_BUTTON_SIZE;
extern const float TW_DEF_SIZE;
extern const float TW_HANDLE_RADIUS;
extern const float TW_OUTLINE_THICKNESS;
Expand Down
45 changes: 41 additions & 4 deletions source/source/util/imgui_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "imgui_utils.h"

#include "allegro_utils.h"


/**
* @brief Helps creating an ImGui combo box, using a vector of strings for the
Expand Down Expand Up @@ -221,11 +223,11 @@ void ImGui::Image(
*
* @param str_id Button widget ID.
* @param bitmap Bitmap to show on the button.
* @param bitmap_size Width and height of the bitmap.
* @param bitmap_size Size to display the bitmap at in the GUI.
* @param uv0 UV coordinates of the top-left coordinate.
* @param uv1 UV coordinates of the bottom-right coordinate.
* @param bg_col Background color.
* @param tint_col Tint color.
* @param bg_col Bitmap background color.
* @param tint_col Bitmap tint color.
* @return Whether the button was pressed.
*/
bool ImGui::ImageButton(
Expand All @@ -246,12 +248,47 @@ bool ImGui::ImageButton(
}


/**
* @brief Helps creating an ImGui ImageButton using Allegro bitmaps, and
* keeping the bitmap centered and in proportion, while also allowing the
* button size to be specified.
*
* @param str_id Button widget ID.
* @param bitmap Bitmap to show on the button.
* @param max_bitmap_size Maximum size to display the bitmap at in the GUI.
* @param button_size Size of the button.
* @param bg_col Bitmap background color.
* @param tint_col Bitmap tint color.
* @return Whether the button was pressed.
*/
bool ImGui::ImageButtonOrganized(
const string &str_id, ALLEGRO_BITMAP* bitmap,
const point &max_bitmap_size, const point &button_size,
const ALLEGRO_COLOR &bg_col, const ALLEGRO_COLOR &tint_col
) {
point final_bmp_size =
resize_to_box_keeping_aspect_ratio(
get_bitmap_dimensions(bitmap), max_bitmap_size
);

point padding = (button_size - final_bmp_size) / 2.0f;

PushStyleVar(
ImGuiStyleVar_FramePadding, ImVec2(padding.x, padding.y)
);
bool result = ImageButton(str_id, bitmap, final_bmp_size);
PopStyleVar();

return result;
}


/**
* @brief Helps creating an ImGui ImageButton, followed by a centered Text.
*
* @param id Button widget ID.
* @param icon Icon to show on the button.
* @param icon_size Width and height of the icon.
* @param icon_size Size to display the bitmap at in the GUI.
* @param button_padding Padding between the icon and button edges.
* @param text The button's text.
* @return Whether the button was pressed.
Expand Down
6 changes: 6 additions & 0 deletions source/source/util/imgui_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ bool ImageButton(
const ALLEGRO_COLOR &bg_col = al_map_rgba(0, 0, 0, 0),
const ALLEGRO_COLOR &tint_col = al_map_rgb(255, 255, 255)
);
bool ImageButtonOrganized(
const string &str_id, ALLEGRO_BITMAP* bitmap,
const point &max_bitmap_size, const point &button_size,
const ALLEGRO_COLOR &bg_col = al_map_rgba(0, 0, 0, 0),
const ALLEGRO_COLOR &tint_col = al_map_rgb(255, 255, 255)
);
bool ImageButtonAndText(
const string &id, ALLEGRO_BITMAP* icon, const point &icon_size,
float button_padding, const string &text
Expand Down

0 comments on commit c2ebb29

Please sign in to comment.