From cc087746bd375deda39a7a0bc93bbe45250950da Mon Sep 17 00:00:00 2001 From: omigamedev Date: Sun, 4 Aug 2019 12:00:49 +0200 Subject: [PATCH] add ui and viewport scale option, fix combobox items height from parent --- android/src/cpp/main.cpp | 7 ++++- data/layout.xml | 8 ++++++ src/app.h | 9 ++----- src/app_layout.cpp | 33 +++++++++++++++++++++++ src/main.cpp | 11 +++++--- src/node_canvas.cpp | 58 +++++++++++++++++++++++----------------- src/node_canvas.h | 3 +++ src/node_combobox.cpp | 11 +++++--- src/node_combobox.h | 1 + src/settings.h | 5 ++++ 10 files changed, 107 insertions(+), 39 deletions(-) diff --git a/android/src/cpp/main.cpp b/android/src/cpp/main.cpp index c2003fd..ec4341c 100644 --- a/android/src/cpp/main.cpp +++ b/android/src/cpp/main.cpp @@ -34,6 +34,7 @@ #include "asset.h" #include "keymap.h" #include "main.h" +#include "settings.h" #include "com_omixlab_panopainter_MainActivity.h" #ifdef __QUEST__ @@ -692,6 +693,9 @@ static int engine_init_display(struct engine* engine) { #ifdef __QUEST__ App::I->zoom = 1.f; + if (Settings::has("ui-scale")) + App::I->zoom = Settings::value("ui-scale"); + App::I->width = 1024; App::I->height = 1024; App::I->redraw = true; @@ -714,8 +718,9 @@ static int engine_init_display(struct engine* engine) { engine_start_vr_thread(); #else float density = get_display_density(); + App::I->zoom = Settings::value_or("ui-scale", density / 1.5f); + App::I->display_density = density; LOG("density %f", density); - App::I->zoom = density / 1.5; App::I->width = w; App::I->height = h; App::I->redraw = true; diff --git a/data/layout.xml b/data/layout.xml index 38744fd..78c5a14 100644 --- a/data/layout.xml +++ b/data/layout.xml @@ -1501,6 +1501,14 @@ Here's a list of what's available in this release. + + + + + + + + diff --git a/src/app.h b/src/app.h index 9154eaa..22db1dd 100644 --- a/src/app.h +++ b/src/app.h @@ -139,13 +139,8 @@ public: glm::vec2 cursor{ 0, 0 }; glm::vec2 gesture_p0; glm::vec2 gesture_p1; -#ifdef __ANDROID__ - float zoom = 3.0; -#elif __IOS__ - float zoom = 2.0; -#else - float zoom = 1.0; -#endif // __ANDROID__ + float display_density = 1.f; + float zoom = 1.f; #if defined(__IOS__) && defined(__OBJC__) GameViewController* ios_view; diff --git a/src/app_layout.cpp b/src/app_layout.cpp index c623420..24c7bac 100644 --- a/src/app_layout.cpp +++ b/src/app_layout.cpp @@ -7,6 +7,7 @@ #include "node_dialog_picker.h" #include "node_panel_floating.h" #include "settings.h" +#include "serializer.h" void App::title_update() { @@ -925,6 +926,38 @@ void App::init_menu_tools() } }; + if (auto ui_scale = popup_exp->find("tools-ui-scale")) + { + // set index to current zoom level (or at least the closest in list) + for (int i = 0; i < ui_scale->m_data.size(); i++) + if (App::I->zoom >= ui_scale->get_float(i)) + ui_scale->set_index(i); + + ui_scale->on_select = [ui_scale](Node* target, int index) + { + App::I->zoom = ui_scale->get_float(index); + Settings::set("ui-scale", Serializer::Float(App::I->zoom)); + Settings::save(); + App::I->title_update(); + }; + } + + if (auto vp_scale = popup_exp->find("tools-vp-scale")) + { + // set index to current zoom level (or at least the closest in list) + for (int i = 0; i < vp_scale->m_data.size(); i++) + if (App::I->canvas->m_density >= vp_scale->get_float(i)) + vp_scale->set_index(i); + + vp_scale->on_select = [vp_scale](Node* target, int index) + { + float d = vp_scale->get_float(index); + App::I->canvas->set_density(d); + Settings::set("vp-scale", Serializer::Float(d)); + Settings::save(); + }; + } + if (auto rtl_btn = popup_exp->find("tools-rtl")) { NodeCheckBox* cb = rtl_btn->find("tools-rtl-check"); diff --git a/src/main.cpp b/src/main.cpp index c4cd223..c892619 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -839,11 +839,16 @@ int main(int argc, char** argv) RegisterClass(&wc); auto monitor = MonitorFromWindow(0, MONITOR_DEFAULTTOPRIMARY); - auto x = unsigned{96}; - auto y = unsigned{96}; + auto x = unsigned{ 96 }; + auto y = unsigned{ 96 }; if (GetDpiForMonitor_fn) GetDpiForMonitor_fn(monitor, MDT_EFFECTIVE_DPI, &x, &y); - App::I->zoom *= (float)x / 96.f; + App::I->display_density = (float)x / 96.f; + + if (Settings::has("ui-scale")) + App::I->zoom = Settings::value("ui-scale"); + else + App::I->zoom = (float)x / 96.f; int show_cmd = SW_NORMAL; Settings::value("window-show-cmd", show_cmd); diff --git a/src/node_canvas.cpp b/src/node_canvas.cpp index d7434d0..0f431b0 100644 --- a/src/node_canvas.cpp +++ b/src/node_canvas.cpp @@ -3,6 +3,7 @@ #include "log.h" #include "node_canvas.h" #include "node_image_texture.h" +#include "settings.h" Node* NodeCanvas::clone_instantiate() const { @@ -11,6 +12,8 @@ Node* NodeCanvas::clone_instantiate() const void NodeCanvas::init() { + m_density = Settings::value_or("vp-scale", 1.f); + m_mouse_ignore = false; m_canvas = std::make_unique(); m_canvas->create(CANVAS_RES, CANVAS_RES); @@ -64,6 +67,11 @@ void NodeCanvas::draw() GLfloat cc[4]; glGetIntegerv(GL_VIEWPORT, vp); glGetFloatv(GL_COLOR_CLEAR_VALUE, cc); + auto blend = glIsEnabled(GL_BLEND); + auto depth = glIsEnabled(GL_DEPTH_TEST); + auto scissor = glIsEnabled(GL_SCISSOR_TEST); + + glDisable(GL_SCISSOR_TEST); float zoom = root()->m_zoom; auto box = m_clip * zoom; @@ -80,10 +88,6 @@ void NodeCanvas::draw() m_canvas->m_box = box; m_canvas->m_vp = c; - auto blend = glIsEnabled(GL_BLEND); - auto depth = glIsEnabled(GL_DEPTH_TEST); - - float pitch = 0; if (auto slider = root()->find("pitch-slider")) pitch = (slider->get_value() - 0.5) * glm::half_pi(); @@ -105,10 +109,10 @@ void NodeCanvas::draw() } - if (zoom > 1.f) + if (m_density != 1.f) { m_rtt.bindFramebuffer(); - glClearColor(1, 1, 1, 0); + glClearColor(1, 1, 0, 0); glClear(GL_COLOR_BUFFER_BIT); glViewport(0, 0, m_rtt.getWidth(), m_rtt.getHeight()); } @@ -466,7 +470,7 @@ void NodeCanvas::draw() for (auto& mode : *m_canvas->m_mode) mode->on_Draw(ortho_proj, proj, camera); - if (zoom > 1.f) + if (m_density != 1.f) { m_rtt.unbindFramebuffer(); @@ -485,9 +489,9 @@ void NodeCanvas::draw() m_rtt.unbindTexture(); } + scissor ? glEnable(GL_SCISSOR_TEST) : glDisable(GL_SCISSOR_TEST); blend ? glEnable(GL_BLEND) : glDisable(GL_BLEND); depth ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST); - m_sampler.unbind(); glViewport(vp[0], vp[1], vp[2], vp[3]); glClearColor(cc[0], cc[1], cc[2], cc[3]); } @@ -496,23 +500,8 @@ void NodeCanvas::handle_resize(glm::vec2 old_size, glm::vec2 new_size) { if (new_size.x != m_canvas->m_width || new_size.y != m_canvas->m_height) { - // actual screen size - //new_size = new_size * root()->m_zoom; -//#if defined(__IOS__) || defined(__ANDROID__) -// m_canvas->m_mixer.create((int)new_size.x * m_canvas->m_mixer_scale, -// (int)new_size.y * m_canvas->m_mixer_scale, -1, GL_RGBA16F); -//#else - m_canvas->m_mixer.create((int)new_size.x * m_canvas->m_mixer_scale, - (int)new_size.y * m_canvas->m_mixer_scale, -1, GL_RGBA8); -//#endif - m_blender_rtt.create((int)new_size.x, (int)new_size.y, -1, GL_RGBA8); - m_cache_rtt.create((int)new_size.x, (int)new_size.y, -1, GL_RGBA8); - m_rtt.create((int)new_size.x, (int)new_size.y, -1, GL_RGBA8, true); - m_blender_bg.create((int)new_size.x, (int)new_size.y, GL_RGBA8); - if (auto img = root()->find("tex-debug")) - img->tex.assign(m_canvas->m_mixer.getTextureID()); - // m_canvas->resize((int)new_size.x, (int)new_size.y); - // m_canvas->clear(); + new_size = new_size * m_density; + create_buffers(); } } @@ -629,6 +618,25 @@ void NodeCanvas::reset_camera() m_canvas->m_pan = {0, 0}; } +void NodeCanvas::create_buffers() +{ + auto new_size = GetSize() * m_density; + m_canvas->m_mixer.create((int)new_size.x * m_canvas->m_mixer_scale, + (int)new_size.y * m_canvas->m_mixer_scale, -1, GL_RGBA8); + m_blender_rtt.create((int)new_size.x, (int)new_size.y, -1, GL_RGBA8); + m_cache_rtt.create((int)new_size.x, (int)new_size.y, -1, GL_RGBA8); + m_rtt.create((int)new_size.x, (int)new_size.y, -1, GL_RGBA8, true); + m_blender_bg.create((int)new_size.x, (int)new_size.y, GL_RGBA8); + if (auto img = root()->find("tex-debug")) + img->tex.assign(m_canvas->m_mixer.getTextureID()); +} + +void NodeCanvas::set_density(float d) +{ + m_density = d; + create_buffers(); +} + void NodeCanvas::on_tick(float dt) { m_outline_pan = glm::fract(m_outline_pan + dt * 0.01f); diff --git a/src/node_canvas.h b/src/node_canvas.h index b1bd679..d1ed6d3 100644 --- a/src/node_canvas.h +++ b/src/node_canvas.h @@ -19,6 +19,7 @@ public: Plane m_grid; glm::vec2 m_outline_pan; int m_grid_divs = 30; + float m_density = 1.f; virtual Node* clone_instantiate() const override; virtual void init() override; virtual void restore_context() override; @@ -29,4 +30,6 @@ public: virtual void destroy_immediate() override; virtual void on_tick(float dt) override; void reset_camera(); + void create_buffers(); + void set_density(float d); }; diff --git a/src/node_combobox.cpp b/src/node_combobox.cpp index 57165dd..2240f27 100644 --- a/src/node_combobox.cpp +++ b/src/node_combobox.cpp @@ -44,7 +44,7 @@ void NodeComboBox::loaded() auto btn = popup->add_child(); btn->m_text->set_text(m_data[i].c_str()); btn->m_border->SetWidthP(100.f); - btn->m_border->SetHeight(30.f); + btn->m_border->SetHeight(GetHeight()); int index = (int)m_items.size(); if (index == m_current_index) m_selected_child_index = popup->get_child_index(btn); @@ -62,8 +62,8 @@ void NodeComboBox::loaded() } float offset = 0; for (int i = 0; i <= m_selected_child_index; i++) - offset += (m_data[i] == "-") ? 5.f : 30.f; - float height = m_items.size() * 30.f + (m_data.size() - m_items.size()) * 5.f; // add items and separators + offset += (m_data[i] == "-") ? 5.f : GetHeight(); + float height = m_items.size() * GetHeight() + (m_data.size() - m_items.size()) * 5.f; // add items and separators glm::vec2 pos = m_pos + glm::vec2(0, m_size.y - offset); auto screen = root()->m_size; if ((pos.y + height) > screen.y) pos.y = screen.y - height; @@ -109,3 +109,8 @@ void NodeComboBox::set_index(int index) //if (on_select) // on_select(this, index); } + +float NodeComboBox::get_float(int index) const noexcept +{ + return std::stof(m_data[index]); +} diff --git a/src/node_combobox.h b/src/node_combobox.h index 72230b4..731228e 100644 --- a/src/node_combobox.h +++ b/src/node_combobox.h @@ -14,4 +14,5 @@ public: virtual void loaded() override; virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override; void set_index(int index); + float get_float(int index) const noexcept; }; diff --git a/src/settings.h b/src/settings.h index f865ed1..b2a5599 100644 --- a/src/settings.h +++ b/src/settings.h @@ -32,4 +32,9 @@ public: { return I.Descriptor::value(key); } + template + static D value_or(const std::string& key, D def) + { + return I.Descriptor::value_or(key, def); + } };