tomato/src/main_screen.cpp

233 lines
5.9 KiB
C++
Raw Normal View History

2023-07-26 12:24:18 +02:00
#include "./main_screen.hpp"
#include <imgui/imgui.h>
2023-07-26 20:09:57 +02:00
#include <SDL3/SDL.h>
2023-07-26 12:24:18 +02:00
#include <memory>
MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins) :
2023-07-26 20:09:57 +02:00
renderer(renderer_),
rmm(cr),
2023-07-29 20:51:32 +02:00
mts(rmm),
tc(save_path, save_password),
2023-11-13 15:14:30 +01:00
tpi(tc.getTox()),
ad(tc),
2023-07-26 20:09:57 +02:00
tcm(cr, tc, tc),
tmm(rmm, cr, tcm, tc, tc),
ttm(rmm, cr, tcm, tc, tc),
2023-08-02 19:24:51 +02:00
mmil(rmm),
tam(rmm, cr, conf),
2023-07-28 18:03:45 +02:00
sdlrtu(renderer_),
cg(conf, rmm, cr, sdlrtu),
sw(conf),
2023-11-13 16:23:49 +01:00
tuiu(tc, conf),
tdch(tpi)
2023-07-26 20:09:57 +02:00
{
2023-07-26 12:55:50 +02:00
tel.subscribeAll(tc);
conf.set("tox", "save_file_path", save_path);
{ // name stuff
auto name = tc.toxSelfGetName();
if (name.empty()) {
name = "tomato";
}
conf.set("tox", "name", name);
tc.setSelfName(name); // TODO: this is ugly
}
// TODO: remove
std::cout << "own address: " << tc.toxSelfGetAddressStr() << "\n";
{ // setup plugin instances
g_provideInstance<ConfigModelI>("ConfigModelI", "host", &conf);
g_provideInstance<Contact3Registry>("Contact3Registry", "host", &cr);
g_provideInstance<RegistryMessageModel>("RegistryMessageModel", "host", &rmm);
g_provideInstance<ToxI>("ToxI", "host", &tc);
2023-11-13 15:14:30 +01:00
g_provideInstance<ToxPrivateI>("ToxPrivateI", "host", &tpi);
2023-07-26 12:55:50 +02:00
g_provideInstance<ToxEventProviderI>("ToxEventProviderI", "host", &tc);
g_provideInstance<ToxContactModel2>("ToxContactModel2", "host", &tcm);
2023-07-26 12:55:50 +02:00
// TODO: pm?
// graphics
g_provideInstance("ImGuiContext", "host", ImGui::GetCurrentContext());
2023-07-26 20:09:57 +02:00
g_provideInstance<TextureUploaderI>("TextureUploaderI", "host", &sdlrtu);
2023-07-26 12:55:50 +02:00
}
for (const auto& ppath : plugins) {
if (!pm.add(ppath)) {
std::cerr << "MS error: loading plugin '" << ppath << "' failed!\n";
// thow?
assert(false && "failed to load plugin");
}
}
2023-07-26 12:55:50 +02:00
conf.dump();
2023-07-26 12:24:18 +02:00
}
2023-07-26 20:09:57 +02:00
MainScreen::~MainScreen(void) {
}
2023-07-30 15:10:26 +02:00
bool MainScreen::handleEvent(SDL_Event& e) {
2023-10-18 14:23:27 +02:00
if (e.type == SDL_EVENT_DROP_FILE) {
std::cout << "DROP FILE: " << e.drop.file << "\n";
cg.sendFilePath(e.drop.file);
return true; // TODO: forward return succ from sendFilePath()
}
if (
e.type == SDL_EVENT_WINDOW_MINIMIZED ||
e.type == SDL_EVENT_WINDOW_HIDDEN ||
e.type == SDL_EVENT_WINDOW_OCCLUDED // does this trigger on partial occlusion?
) {
auto* window = SDL_GetWindowFromID(e.window.windowID);
auto* event_renderer = SDL_GetRenderer(window);
if (event_renderer != nullptr && event_renderer == renderer) {
// our window is now obstructed
if (_window_hidden_ts < e.window.timestamp) {
_window_hidden_ts = e.window.timestamp;
_window_hidden = true;
//std::cout << "TOMAT: window hidden " << e.window.timestamp << "\n";
}
}
return true; // forward?
}
if (
e.type == SDL_EVENT_WINDOW_SHOWN ||
e.type == SDL_EVENT_WINDOW_RESTORED ||
e.type == SDL_EVENT_WINDOW_EXPOSED
) {
auto* window = SDL_GetWindowFromID(e.window.windowID);
auto* event_renderer = SDL_GetRenderer(window);
if (event_renderer != nullptr && event_renderer == renderer) {
if (_window_hidden_ts <= e.window.timestamp) {
_window_hidden_ts = e.window.timestamp;
2024-01-06 15:13:45 +01:00
if (_window_hidden) {
// if window was previously hidden, we shorten the wait for the next frame
_render_interval = 1.f/60.f;
}
_window_hidden = false;
//std::cout << "TOMAT: window shown " << e.window.timestamp << "\n";
}
}
return true; // forward?
}
2024-01-06 18:23:06 +01:00
if (
_fps_perf_mode <= 1 && (
e.type == SDL_EVENT_MOUSE_MOTION ||
e.type == SDL_EVENT_MOUSE_WHEEL ||
e.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
e.type == SDL_EVENT_MOUSE_BUTTON_UP ||
e.type == SDL_EVENT_TEXT_INPUT ||
e.type == SDL_EVENT_KEY_DOWN ||
e.type == SDL_EVENT_KEY_UP ||
e.type == SDL_EVENT_WINDOW_MOUSE_ENTER ||
e.type == SDL_EVENT_WINDOW_MOUSE_LEAVE ||
e.type == SDL_EVENT_WINDOW_FOCUS_GAINED ||
e.type == SDL_EVENT_WINDOW_FOCUS_LOST
)
) {
_render_interval = 1.f/60.f; // TODO: magic
}
2023-07-30 15:10:26 +02:00
return false;
}
2024-01-05 14:47:08 +01:00
Screen* MainScreen::render(float time_delta, bool& quit) {
2023-07-26 12:55:50 +02:00
quit = !tc.iterate();
tcm.iterate(time_delta);
tam.iterate();
// HACK: render the tomato main window first, with proper flags set.
// flags need to be set the first time begin() is called.
// and plugins are run before the main cg is run.
{
// TODO: maybe render cg earlier? or move the main window out of cg?
constexpr auto bg_window_flags =
ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_MenuBar |
ImGuiWindowFlags_NoBringToFrontOnFocus;
ImGui::Begin("tomato", nullptr, bg_window_flags);
ImGui::End();
}
2023-07-26 12:55:50 +02:00
pm.tick(time_delta);
2023-11-13 16:23:49 +01:00
tdch.tick(time_delta);
2023-07-26 12:55:50 +02:00
2023-07-29 20:51:32 +02:00
mts.iterate();
2023-07-28 18:03:45 +02:00
cg.render();
sw.render();
tuiu.render();
2023-11-13 16:23:49 +01:00
tdch.render();
2023-07-28 18:03:45 +02:00
{ // main window menubar injection
if (ImGui::Begin("tomato")) {
if (ImGui::BeginMenuBar()) {
// ImGui::Separator(); // why do we not need this????
if (ImGui::BeginMenu("Performance")) {
{ // fps
2024-01-06 18:23:06 +01:00
const auto targets = "normal\0reduced\0powersave\0";
ImGui::SetNextItemWidth(ImGui::GetFontSize()*10);
ImGui::Combo("fps mode", &_fps_perf_mode, targets, 4);
}
{ // compute
2024-01-06 18:23:06 +01:00
const auto targets = "normal\0powersave\0";
ImGui::SetNextItemWidth(ImGui::GetFontSize()*10);
ImGui::Combo("compute mode", &_compute_perf_mode, targets, 4);
ImGui::SetItemTooltip("Limiting compute can slow down things filetransfers.");
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
}
ImGui::End();
}
if constexpr (false) {
2023-09-29 18:15:18 +02:00
ImGui::ShowDemoWindow();
2023-07-26 12:55:50 +02:00
}
2023-07-26 12:24:18 +02:00
if (
2024-01-06 18:23:06 +01:00
_fps_perf_mode >= 1 || // TODO: magic
_window_hidden
) {
_render_interval = 1.f/4.f;
} else {
_render_interval = 1.f/60.f;
}
2024-01-05 14:47:08 +01:00
return nullptr;
}
Screen* MainScreen::tick(float time_delta, bool& quit) {
_min_tick_interval = std::max<float>(
std::min<float>(
tc.toxIterationInterval()/1000.f,
0.03f // HACK: 30ms upper bound, should be the same as tox but will change
),
(_compute_perf_mode == 0 ? 0.001f : 0.1f) // in powersave fix the lowerbound to 100ms
2024-01-05 14:47:08 +01:00
);
2023-07-26 20:09:57 +02:00
2023-07-26 12:24:18 +02:00
return nullptr;
}