@@ -155,6 +155,8 @@ static const char *ATTR_NAMES[] = {
155
155
typedef typeof (((inputs_t *)0)->safe_margins) margins_t;
156
156
157
157
typedef struct gui_t {
158
+ bool initialized;
159
+
158
160
gl_shader_t *shader;
159
161
GLuint array_buffer;
160
162
GLuint index_buffer;
@@ -195,10 +197,40 @@ typedef struct gui_t {
195
197
bool deactivated;
196
198
} groups[16 ];
197
199
200
+ float scale;
201
+
198
202
} gui_t ;
199
203
200
204
static gui_t *gui = NULL ;
201
205
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
+
202
234
static void on_click (void ) {
203
235
if (DEFINED (GUI_SOUND))
204
236
sound_play (" click" , 1.0 , 1.0 );
@@ -336,13 +368,14 @@ static void add_font(const char *uri, const ImWchar *ranges, bool mergmode)
336
368
const void *data;
337
369
int data_size;
338
370
ImFontConfig conf;
371
+ float size = 14 * gui->scale * scale;
339
372
340
373
conf.FontDataOwnedByAtlas = false ;
341
374
conf.MergeMode = mergmode;
342
375
data = assets_get (uri, &data_size);
343
376
assert (data);
344
377
io.Fonts ->AddFontFromMemoryTTF (
345
- (void *)data, data_size, 14 * scale , &conf, ranges);
378
+ (void *)data, data_size, size , &conf, ranges);
346
379
}
347
380
348
381
static void load_fonts_texture ()
@@ -361,6 +394,7 @@ static void load_fonts_texture()
361
394
0
362
395
};
363
396
397
+ io.Fonts ->Clear ();
364
398
add_font (" asset://data/fonts/DejaVuSans.ttf" , ranges, false );
365
399
add_font (" asset://data/fonts/goxel-font.ttf" , range_user, true );
366
400
@@ -425,8 +459,8 @@ static void init_ImGui(void)
425
459
426
460
static void gui_init (void )
427
461
{
428
- if (!gui) {
429
- gui = ( gui_t *) calloc ( 1 , sizeof (* gui));
462
+ gui_create ();
463
+ if (! gui-> initialized ) {
430
464
init_ImGui ();
431
465
goxel.gui .panel_width = GUI_PANEL_WIDTH_NORMAL;
432
466
}
@@ -444,6 +478,8 @@ static void gui_init(void)
444
478
445
479
ImGuiIO& io = ImGui::GetIO ();
446
480
if (!io.Fonts ->TexID ) load_fonts_texture ();
481
+
482
+ load_fonts_texture ();
447
483
}
448
484
449
485
void gui_release (void )
@@ -661,7 +697,7 @@ static void gui_iter(const inputs_t *inputs)
661
697
style.ChildBorderSize = 0 ;
662
698
style.SelectableTextAlign = ImVec2 (0.5 , 0.5 );
663
699
style.FramePadding = ImVec2 (4 ,
664
- (GUI_ITEM_HEIGHT - ImGui::GetFontSize ()) / 2 );
700
+ (GUI_ITEM_HEIGHT * gui-> scale - ImGui::GetFontSize ()) / 2 );
665
701
style.Colors [ImGuiCol_WindowBg] = COLOR (WINDOW, BACKGROUND, false );
666
702
style.Colors [ImGuiCol_ChildBg] = COLOR (SECTION, BACKGROUND, false );
667
703
style.Colors [ImGuiCol_Header] = ImVec4 (0 , 0 , 0 , 0 );
@@ -960,7 +996,7 @@ bool gui_input_float(const char *label, float *v, float step,
960
996
ImGuiStorage *storage = ImGui::GetStateStorage ();
961
997
const ImGuiStyle& style = ImGui::GetStyle ();
962
998
const ImVec2 button_size = ImVec2 (
963
- GUI_ITEM_HEIGHT ,
999
+ gui_get_item_height () ,
964
1000
ImGui::GetFontSize () + style.FramePadding .y * 2 .0f );
965
1001
int v_int;
966
1002
char v_label[128 ];
@@ -1042,7 +1078,7 @@ bool gui_input_float(const char *label, float *v, float step,
1042
1078
} else {
1043
1079
ret = slider_float (v, minv, maxv, format);
1044
1080
}
1045
-
1081
+ update_activation_state ();
1046
1082
is_active = ImGui::IsItemActive ();
1047
1083
}
1048
1084
@@ -1149,7 +1185,7 @@ static bool _selectable(const char *label, bool *v, const char *tooltip,
1149
1185
v = v ? v : &default_v;
1150
1186
size = (icon != -1 ) ?
1151
1187
ImVec2 (GUI_ICON_HEIGHT, GUI_ICON_HEIGHT) :
1152
- ImVec2 (w, GUI_ITEM_HEIGHT );
1188
+ ImVec2 (w, gui_get_item_height () );
1153
1189
1154
1190
if (!tooltip && icon != -1 ) {
1155
1191
tooltip = label;
@@ -1375,16 +1411,16 @@ bool gui_button(const char *label, float size, int icon)
1375
1411
int w, isize;
1376
1412
1377
1413
button_size = ImVec2 (size * ImGui::GetContentRegionAvail ().x ,
1378
- GUI_ITEM_HEIGHT );
1414
+ gui_get_item_height () );
1379
1415
if (size == -1 ) button_size.x = ImGui::GetContentRegionAvail ().x ;
1380
1416
if (size == 0 && (label == NULL || label[0 ] == ' #' )) {
1381
1417
button_size.x = GUI_ICON_HEIGHT;
1382
1418
button_size.y = GUI_ICON_HEIGHT;
1383
1419
}
1384
1420
if (size == 0 && label && label[0 ] != ' #' ) {
1385
1421
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 () ;
1388
1424
}
1389
1425
1390
1426
if (gui->item_size ) button_size.x = gui->item_size ;
@@ -1422,7 +1458,7 @@ bool gui_button_right(const char *label, int icon)
1422
1458
const ImGuiStyle& style = ImGui::GetStyle ();
1423
1459
float text_size = ImGui::CalcTextSize (label).x ;
1424
1460
float w = text_size + 2 * style.FramePadding .x ;
1425
- w = max (w, GUI_ITEM_HEIGHT );
1461
+ w = max (w, gui_get_item_height () );
1426
1462
w += style.FramePadding .x ;
1427
1463
ImGui::SameLine ();
1428
1464
ImGui::Dummy (ImVec2 (ImGui::GetContentRegionAvail ().x - w, 0 ));
@@ -1628,7 +1664,7 @@ void gui_on_popup_closed(void (*func)(int))
1628
1664
void gui_popup_bottom_begin (void )
1629
1665
{
1630
1666
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 ;
1632
1668
float w = ImGui::GetContentRegionAvail ().y - bottom_y;
1633
1669
ImGui::Dummy (ImVec2 (0 , w));
1634
1670
gui_row_begin (0 );
@@ -1831,12 +1867,13 @@ static bool panel_header_close_button(void)
1831
1867
ImDrawList* draw_list = ImGui::GetWindowDrawList ();
1832
1868
bool ret;
1833
1869
1834
- w = GUI_ITEM_HEIGHT + style.FramePadding .x ;
1870
+ w = gui_get_item_height () + style.FramePadding .x ;
1835
1871
ImGui::SameLine ();
1836
1872
ImGui::Dummy (ImVec2 (ImGui::GetContentRegionAvail ().x - w, 0 ));
1837
1873
ImGui::SameLine ();
1838
1874
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 ()));
1840
1877
ImGui::PopStyleColor ();
1841
1878
1842
1879
center = ImGui::GetItemRectMin () +
@@ -1855,7 +1892,7 @@ bool gui_panel_header(const char *label)
1855
1892
{
1856
1893
bool ret;
1857
1894
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 () ;
1859
1896
1860
1897
ImGui::PushID (" panel_header" );
1861
1898
ImGui::BeginGroup ();
@@ -1910,7 +1947,7 @@ bool gui_icons_grid(int nb, const gui_icon_info_t *icons, int *current)
1910
1947
size = GUI_ICON_HEIGHT;
1911
1948
clicked = gui_selectable_icon (label, &v, icon->icon );
1912
1949
} else { // Color icon.
1913
- size = GUI_ITEM_HEIGHT ;
1950
+ size = gui_get_item_height () ;
1914
1951
ImGui::PushStyleColor (ImGuiCol_Button, icon->color );
1915
1952
ImGui::PushStyleColor (ImGuiCol_ButtonHovered, icon->color );
1916
1953
clicked = ImGui::Button (" " , ImVec2 (size, size));
0 commit comments