Android: escala da interface pela densidade e as duas orientacoes
Num celular o editor ficava com tudo minusculo: os 12 px da fonte e os blocos de 36 px sao pensados para uma janela de desktop. Agora o backend SDL2 tem uma escala (TINYBLOCKS_UI_SCALE ou, no Android, dpi/160 limitado a 3): desenha as coordenadas multiplicadas, divide a entrada e mede o texto no tamanho real (sem serrilhar). No desktop o padrao continua 1. A activity passa a aceitar as duas orientacoes (fullSensor) e sai o hint SDL_IOS_ORIENTATIONS que forcava paisagem; no retrato o editor entra no modo compacto. O limiar do compacto sobe de 720 para 1000 (unidades logicas): num telefone a janela logica e ~914 de largura e tres colunas nao cabem. }
This commit is contained in:
1 parent
1d8368036e
commit
a6f834f88e
7 files changed
+80
-24
No files matched your search
@@ -27,6 +27,35 @@
|
||||
#include <tb_graphics.h>
|
||||
#include "../include/tb_sdl2.h"
|
||||
|
||||
/* ===
|
||||
* Escala da interface
|
||||
* === */
|
||||
/* O editor desenha tudo em pixels logicos. Num celular (muita densidade) os
|
||||
* 12 px da fonte e os blocos de 36 px ficam minusculos. A escala vem da
|
||||
* densidade do Android (mdpi = 160 dpi) ou de TINYBLOCKS_UI_SCALE; o backend
|
||||
* multiplica as coordenadas ao desenhar e divide a entrada, e mede o texto no
|
||||
* tamanho escalado (sem serrilhar). */
|
||||
static float s_ui_scale = 1.f;
|
||||
|
||||
static void ui_scale_begin(void) {
|
||||
float s = 1.f;
|
||||
const char* e = getenv("TINYBLOCKS_UI_SCALE");
|
||||
if (e && *e) {
|
||||
float v = (float)atof(e);
|
||||
if (v > 0.f) s = v;
|
||||
}
|
||||
#ifdef __ANDROID__
|
||||
else {
|
||||
float ddpi = 0.f;
|
||||
if (SDL_GetDisplayDPI(0, &ddpi, NULL, NULL) == 0 && ddpi > 0.f)
|
||||
s = ddpi / 160.f; /* densidade: mdpi = 1.0 */
|
||||
}
|
||||
#endif
|
||||
if (s < 1.f) s = 1.f;
|
||||
if (s > 3.f) s = 3.f;
|
||||
s_ui_scale = s;
|
||||
}
|
||||
|
||||
/* ===
|
||||
* Handles opacos de Window/Renderer (blocos wrapper)
|
||||
* === */
|
||||
@@ -287,6 +316,14 @@ static void sdl2_execute(SDL_Renderer* ren,
|
||||
|
||||
for (uint32_t i = 0; i < rb->count; i++) {
|
||||
const TinyBlocks_DrawCmd* cmd = &rb->cmds[i];
|
||||
TinyBlocks_DrawCmd scaled;
|
||||
if (s_ui_scale != 1.f) { /* coordenadas logicas -> pixels */
|
||||
scaled = *cmd;
|
||||
scaled.x *= s_ui_scale; scaled.y *= s_ui_scale;
|
||||
scaled.w *= s_ui_scale; scaled.h *= s_ui_scale;
|
||||
scaled.param *= s_ui_scale; scaled.font_size *= s_ui_scale;
|
||||
cmd = &scaled;
|
||||
}
|
||||
switch (cmd->kind) {
|
||||
|
||||
case TB_DRAW_CLEAR:
|
||||
@@ -383,7 +420,7 @@ static void sdl2_execute(SDL_Renderer* ren,
|
||||
|
||||
case TB_DRAW_TEXT:
|
||||
if (cmd->text[0]) {
|
||||
TTF_Font* font = get_font((int)cmd->font_size);
|
||||
TTF_Font* font = get_font((int)(cmd->font_size + 0.5f));
|
||||
if (font) {
|
||||
SDL_Color col = {
|
||||
(Uint8)(cmd->r*255.f), (Uint8)(cmd->g*255.f),
|
||||
@@ -491,6 +528,7 @@ static bool be_init(void) {
|
||||
}
|
||||
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
|
||||
SDL_StartTextInput();
|
||||
ui_scale_begin();
|
||||
sensors_open();
|
||||
s_be_inited = true;
|
||||
return true;
|
||||
@@ -552,6 +590,7 @@ static bool s_has_pending = false;
|
||||
/* Aplica um evento a entrada da sua janela. false = nao cabe neste quadro. */
|
||||
static bool be_apply(SDL_Event* ev) {
|
||||
SdlWin* w = NULL;
|
||||
const float S = s_ui_scale > 0.f ? s_ui_scale : 1.f;
|
||||
switch (ev->type) {
|
||||
case SDL_QUIT: /* SIGTERM/Ctrl+C ou o SO pediu para sair */
|
||||
for (w = s_wins; w; w = w->next) w->in.close = 1;
|
||||
@@ -562,8 +601,8 @@ static bool be_apply(SDL_Event* ev) {
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
if ((w = win_by_id(ev->motion.windowID))) {
|
||||
w->in.mouse_x = (float)ev->motion.x;
|
||||
w->in.mouse_y = (float)ev->motion.y;
|
||||
w->in.mouse_x = (float)ev->motion.x / S;
|
||||
w->in.mouse_y = (float)ev->motion.y / S;
|
||||
w->pos_fresh = 1;
|
||||
}
|
||||
break;
|
||||
@@ -571,8 +610,8 @@ static bool be_apply(SDL_Event* ev) {
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if ((w = win_by_id(ev->button.windowID))) {
|
||||
bool down = ev->type == SDL_MOUSEBUTTONDOWN;
|
||||
w->in.mouse_x = (float)ev->button.x;
|
||||
w->in.mouse_y = (float)ev->button.y;
|
||||
w->in.mouse_x = (float)ev->button.x / S;
|
||||
w->in.mouse_y = (float)ev->button.y / S;
|
||||
w->pos_fresh = 1;
|
||||
/* clique repetido no mesmo quadro: espera o proximo */
|
||||
if (ev->button.button == SDL_BUTTON_LEFT && (down ? w->in.l_down : w->in.l_up)) return false;
|
||||
@@ -677,11 +716,16 @@ static void be_poll(void) {
|
||||
bool f = w->win == focus;
|
||||
/* a posicao do evento vale mais: o estado global fica velho quando a
|
||||
janela acabou de ganhar o foco com aquele mesmo clique */
|
||||
if (f && !w->pos_fresh) { w->in.mouse_x = (float)mx; w->in.mouse_y = (float)my; }
|
||||
if (f && !w->pos_fresh) { w->in.mouse_x = (float)mx / s_ui_scale; w->in.mouse_y = (float)my / s_ui_scale; }
|
||||
w->in.l_held = f && (btns & SDL_BUTTON_LMASK) ? 1 : 0;
|
||||
w->in.r_held = f && (btns & SDL_BUTTON_RMASK) ? 1 : 0;
|
||||
w->in.mod_state = mods;
|
||||
SDL_GetWindowSize(w->win, &w->in.win_w, &w->in.win_h);
|
||||
{
|
||||
int pw = 0, ph = 0;
|
||||
SDL_GetWindowSize(w->win, &pw, &ph);
|
||||
w->in.win_w = (int)((float)pw / s_ui_scale + 0.5f);
|
||||
w->in.win_h = (int)((float)ph / s_ui_scale + 0.5f);
|
||||
}
|
||||
SDL_GetWindowPosition(w->win, &w->in.win_x, &w->in.win_y);
|
||||
w->in.gmouse_x = (float)gmx;
|
||||
w->in.gmouse_y = (float)gmy;
|
||||
@@ -726,10 +770,11 @@ static void be_window_present(void* handle, const TinyBlocks_RenderableBlock* rb
|
||||
|
||||
static float be_measure_text(const char* text, float font_size) {
|
||||
if (!be_init()) return 0.f;
|
||||
TTF_Font* font = get_font((int)(font_size + 0.5f));
|
||||
const float S = s_ui_scale > 0.f ? s_ui_scale : 1.f;
|
||||
TTF_Font* font = get_font((int)(font_size * S + 0.5f));
|
||||
int w = 0, h = 0;
|
||||
if (!font || TTF_SizeUTF8(font, text, &w, &h) != 0) return 0.f;
|
||||
return (float)w;
|
||||
return (float)w / S; /* largura em unidades logicas */
|
||||
}
|
||||
|
||||
/* Completa o quadro em ~16,7 ms (60 fps) contando desde o anterior. Com
|
||||
@@ -967,14 +1012,19 @@ static void eval_sdl2_app(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
{
|
||||
int mx, my;
|
||||
Uint32 btns = SDL_GetMouseState(&mx, &my);
|
||||
fe.mouse_x = (float)mx;
|
||||
fe.mouse_y = (float)my;
|
||||
fe.mouse_x = (float)mx / s_ui_scale;
|
||||
fe.mouse_y = (float)my / s_ui_scale;
|
||||
fe.l_held = (btns & SDL_BUTTON_LMASK) ? 1 : 0;
|
||||
fe.r_held = (btns & SDL_BUTTON_RMASK) ? 1 : 0;
|
||||
}
|
||||
|
||||
/* Tamanho atual da janela e estado dos modificadores */
|
||||
SDL_GetWindowSize(s->win, &fe.win_w, &fe.win_h);
|
||||
{
|
||||
int pw = 0, ph = 0;
|
||||
SDL_GetWindowSize(s->win, &pw, &ph);
|
||||
fe.win_w = (int)((float)pw / s_ui_scale + 0.5f);
|
||||
fe.win_h = (int)((float)ph / s_ui_scale + 0.5f);
|
||||
}
|
||||
fe.mod_state = (int)SDL_GetModState();
|
||||
|
||||
/* Preenche o bloco SDL2.Events */
|
||||
|
||||
Reference in new issue
Block a user