Skip to content

Commit ddcd356

Browse files
Gui: add a setting to scale the UI
To address issue #399 Not tested much.
1 parent e457f00 commit ddcd356

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

src/gui.cpp

+53-16
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ static const char *ATTR_NAMES[] = {
155155
typedef typeof(((inputs_t*)0)->safe_margins) margins_t;
156156

157157
typedef struct gui_t {
158+
bool initialized;
159+
158160
gl_shader_t *shader;
159161
GLuint array_buffer;
160162
GLuint index_buffer;
@@ -195,10 +197,40 @@ typedef struct gui_t {
195197
bool deactivated;
196198
} groups[16];
197199

200+
float scale;
201+
198202
} gui_t;
199203

200204
static gui_t *gui = NULL;
201205

206+
static void gui_create(void)
207+
{
208+
if (gui) return;
209+
gui = (gui_t*)calloc(1, sizeof(*gui));
210+
gui->scale = 1;
211+
}
212+
213+
float gui_get_scale(void)
214+
{
215+
gui_create();
216+
return gui->scale;
217+
}
218+
219+
void gui_set_scale(float s)
220+
{
221+
if (s < 1 || s > 2) {
222+
LOG_W("Invalid scale value: %f", s);
223+
s = 1;
224+
}
225+
gui_create();
226+
gui->scale = s;
227+
228+
if (gui->initialized) {
229+
ImGuiIO& io = ImGui::GetIO();
230+
io.Fonts->TexID = NULL; // Note: this leaks the texture.
231+
}
232+
}
233+
202234
static void on_click(void) {
203235
if (DEFINED(GUI_SOUND))
204236
sound_play("click", 1.0, 1.0);
@@ -336,13 +368,14 @@ static void add_font(const char *uri, const ImWchar *ranges, bool mergmode)
336368
const void *data;
337369
int data_size;
338370
ImFontConfig conf;
371+
float size = 14 * gui->scale * scale;
339372

340373
conf.FontDataOwnedByAtlas = false;
341374
conf.MergeMode = mergmode;
342375
data = assets_get(uri, &data_size);
343376
assert(data);
344377
io.Fonts->AddFontFromMemoryTTF(
345-
(void*)data, data_size, 14 * scale, &conf, ranges);
378+
(void*)data, data_size, size, &conf, ranges);
346379
}
347380

348381
static void load_fonts_texture()
@@ -361,6 +394,7 @@ static void load_fonts_texture()
361394
0
362395
};
363396

397+
io.Fonts->Clear();
364398
add_font("asset://data/fonts/DejaVuSans.ttf", ranges, false);
365399
add_font("asset://data/fonts/goxel-font.ttf", range_user, true);
366400

@@ -425,8 +459,8 @@ static void init_ImGui(void)
425459

426460
static void gui_init(void)
427461
{
428-
if (!gui) {
429-
gui = (gui_t*)calloc(1, sizeof(*gui));
462+
gui_create();
463+
if (!gui->initialized) {
430464
init_ImGui();
431465
goxel.gui.panel_width = GUI_PANEL_WIDTH_NORMAL;
432466
}
@@ -444,6 +478,8 @@ static void gui_init(void)
444478

445479
ImGuiIO& io = ImGui::GetIO();
446480
if (!io.Fonts->TexID) load_fonts_texture();
481+
482+
load_fonts_texture();
447483
}
448484

449485
void gui_release(void)
@@ -661,7 +697,7 @@ static void gui_iter(const inputs_t *inputs)
661697
style.ChildBorderSize = 0;
662698
style.SelectableTextAlign = ImVec2(0.5, 0.5);
663699
style.FramePadding = ImVec2(4,
664-
(GUI_ITEM_HEIGHT - ImGui::GetFontSize()) / 2);
700+
(GUI_ITEM_HEIGHT * gui->scale - ImGui::GetFontSize()) / 2);
665701
style.Colors[ImGuiCol_WindowBg] = COLOR(WINDOW, BACKGROUND, false);
666702
style.Colors[ImGuiCol_ChildBg] = COLOR(SECTION, BACKGROUND, false);
667703
style.Colors[ImGuiCol_Header] = ImVec4(0, 0, 0, 0);
@@ -960,7 +996,7 @@ bool gui_input_float(const char *label, float *v, float step,
960996
ImGuiStorage *storage = ImGui::GetStateStorage();
961997
const ImGuiStyle& style = ImGui::GetStyle();
962998
const ImVec2 button_size = ImVec2(
963-
GUI_ITEM_HEIGHT,
999+
gui_get_item_height(),
9641000
ImGui::GetFontSize() + style.FramePadding.y * 2.0f);
9651001
int v_int;
9661002
char v_label[128];
@@ -1042,7 +1078,7 @@ bool gui_input_float(const char *label, float *v, float step,
10421078
} else {
10431079
ret = slider_float(v, minv, maxv, format);
10441080
}
1045-
1081+
update_activation_state();
10461082
is_active = ImGui::IsItemActive();
10471083
}
10481084

@@ -1149,7 +1185,7 @@ static bool _selectable(const char *label, bool *v, const char *tooltip,
11491185
v = v ? v : &default_v;
11501186
size = (icon != -1) ?
11511187
ImVec2(GUI_ICON_HEIGHT, GUI_ICON_HEIGHT) :
1152-
ImVec2(w, GUI_ITEM_HEIGHT);
1188+
ImVec2(w, gui_get_item_height());
11531189

11541190
if (!tooltip && icon != -1) {
11551191
tooltip = label;
@@ -1375,16 +1411,16 @@ bool gui_button(const char *label, float size, int icon)
13751411
int w, isize;
13761412

13771413
button_size = ImVec2(size * ImGui::GetContentRegionAvail().x,
1378-
GUI_ITEM_HEIGHT);
1414+
gui_get_item_height());
13791415
if (size == -1) button_size.x = ImGui::GetContentRegionAvail().x;
13801416
if (size == 0 && (label == NULL || label[0] == '#')) {
13811417
button_size.x = GUI_ICON_HEIGHT;
13821418
button_size.y = GUI_ICON_HEIGHT;
13831419
}
13841420
if (size == 0 && label && label[0] != '#') {
13851421
w = ImGui::CalcTextSize(label, NULL, true).x + style.FramePadding.x * 2;
1386-
if (w < GUI_ITEM_HEIGHT)
1387-
button_size.x = GUI_ITEM_HEIGHT;
1422+
if (w < gui_get_item_height())
1423+
button_size.x = gui_get_item_height();
13881424
}
13891425

13901426
if (gui->item_size) button_size.x = gui->item_size;
@@ -1422,7 +1458,7 @@ bool gui_button_right(const char *label, int icon)
14221458
const ImGuiStyle& style = ImGui::GetStyle();
14231459
float text_size = ImGui::CalcTextSize(label).x;
14241460
float w = text_size + 2 * style.FramePadding.x;
1425-
w = max(w, GUI_ITEM_HEIGHT);
1461+
w = max(w, gui_get_item_height());
14261462
w += style.FramePadding.x;
14271463
ImGui::SameLine();
14281464
ImGui::Dummy(ImVec2(ImGui::GetContentRegionAvail().x - w, 0));
@@ -1628,7 +1664,7 @@ void gui_on_popup_closed(void (*func)(int))
16281664
void gui_popup_bottom_begin(void)
16291665
{
16301666
const ImGuiStyle& style = ImGui::GetStyle();
1631-
float bottom_y = GUI_ITEM_HEIGHT + style.FramePadding.y * 2;
1667+
float bottom_y = gui_get_item_height() + style.FramePadding.y * 2;
16321668
float w = ImGui::GetContentRegionAvail().y - bottom_y;
16331669
ImGui::Dummy(ImVec2(0, w));
16341670
gui_row_begin(0);
@@ -1831,12 +1867,13 @@ static bool panel_header_close_button(void)
18311867
ImDrawList* draw_list = ImGui::GetWindowDrawList();
18321868
bool ret;
18331869

1834-
w = GUI_ITEM_HEIGHT + style.FramePadding.x;
1870+
w = gui_get_item_height() + style.FramePadding.x;
18351871
ImGui::SameLine();
18361872
ImGui::Dummy(ImVec2(ImGui::GetContentRegionAvail().x - w, 0));
18371873
ImGui::SameLine();
18381874
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
1839-
ret = ImGui::Button("", ImVec2(GUI_ITEM_HEIGHT, GUI_ITEM_HEIGHT));
1875+
ret = ImGui::Button("", ImVec2(gui_get_item_height(),
1876+
gui_get_item_height()));
18401877
ImGui::PopStyleColor();
18411878

18421879
center = ImGui::GetItemRectMin() +
@@ -1855,7 +1892,7 @@ bool gui_panel_header(const char *label)
18551892
{
18561893
bool ret;
18571894
float label_w = ImGui::CalcTextSize(label).x;
1858-
float w = ImGui::GetContentRegionAvail().x - GUI_ITEM_HEIGHT;
1895+
float w = ImGui::GetContentRegionAvail().x - gui_get_item_height();
18591896

18601897
ImGui::PushID("panel_header");
18611898
ImGui::BeginGroup();
@@ -1910,7 +1947,7 @@ bool gui_icons_grid(int nb, const gui_icon_info_t *icons, int *current)
19101947
size = GUI_ICON_HEIGHT;
19111948
clicked = gui_selectable_icon(label, &v, icon->icon);
19121949
} else { // Color icon.
1913-
size = GUI_ITEM_HEIGHT;
1950+
size = gui_get_item_height();
19141951
ImGui::PushStyleColor(ImGuiCol_Button, icon->color);
19151952
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, icon->color);
19161953
clicked = ImGui::Button("", ImVec2(size, size));

src/gui.h

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ typedef struct {
4848
float w;
4949
} gui_window_ret_t;
5050

51+
float gui_get_scale(void);
52+
void gui_set_scale(float s);
53+
5154
int gui_window_begin(const char *label, float x, float y, float w, float h,
5255
int flags);
5356

src/gui/settings.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ int gui_settings_popup(void *data)
125125
const tr_lang_t *language;
126126
const tr_lang_t *languages;
127127
bool val;
128+
float scale;
128129

129130
if (gui_section_begin(_("Language"), GUI_SECTION_COLLAPSABLE)) {
130131
language = tr_get_language();
@@ -143,17 +144,26 @@ int gui_settings_popup(void *data)
143144
}
144145
} gui_section_end();
145146

146-
if (gui_section_begin(_("Theme"), GUI_SECTION_COLLAPSABLE)) {
147+
if (gui_section_begin(_("UI"), GUI_SECTION_COLLAPSABLE)) {
147148
DL_COUNT(themes, theme, nb);
148149
i = 0;
149150
DL_FOREACH(themes, theme) {
150151
if (strcmp(theme->name, theme_get()->name) == 0) current = i;
151152
names[i++] = theme->name;
152153
}
153-
if (gui_combo("##themes", &current, names, nb)) {
154+
gui_text("Theme");
155+
if (gui_combo("#themes", &current, names, nb)) {
154156
theme_set(names[current]);
155157
settings_save();
156158
}
159+
scale = gui_get_scale();
160+
if (gui_input_float("Scale", &scale, 0.1, 1.0, 2.0, "%.1f")) {
161+
gui_set_scale(scale);
162+
}
163+
if (gui_is_item_deactivated()) {
164+
settings_save();
165+
}
166+
157167
} gui_section_end();
158168

159169
if (gui_section_begin("Inputs", GUI_SECTION_COLLAPSABLE_CLOSED)) {
@@ -237,6 +247,9 @@ static int settings_ini_handler(void *user, const char *section,
237247
tr_set_language(value);
238248
goxel.lang = tr_get_language()->id;
239249
}
250+
if (strcmp(name, "scale") == 0) {
251+
gui_set_scale(atof(value));
252+
}
240253
}
241254
if (strcmp(section, "shortcuts") == 0) {
242255
a = action_get_by_name(name);
@@ -333,6 +346,7 @@ void settings_save(void)
333346
fprintf(file, "[ui]\n");
334347
fprintf(file, "theme=%s\n", theme_get()->name);
335348
fprintf(file, "language=%s\n", goxel.lang);
349+
fprintf(file, "scale=%f\n", gui_get_scale());
336350
fprintf(file, "\n");
337351

338352
fprintf(file, "[shortcuts]\n");

0 commit comments

Comments
 (0)