Reset main window width/height ratio automatically when width/height is changed

This commit is contained in:
dijunkun
2024-06-03 11:30:55 +08:00
parent f29b2ee09d
commit d150c374b5
3 changed files with 42 additions and 25 deletions

View File

@@ -26,7 +26,7 @@ class ConfigCenter {
private: private:
VIDEO_QUALITY video_quality_ = VIDEO_QUALITY::MEDIUM; VIDEO_QUALITY video_quality_ = VIDEO_QUALITY::MEDIUM;
LANGUAGE language_ = LANGUAGE::ENGLISH; LANGUAGE language_ = LANGUAGE::CHINESE;
}; };
#endif #endif

View File

@@ -3,24 +3,20 @@
* @Date: 2024-05-29 * @Date: 2024-05-29
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved. * Copyright (c) 2024 by DI JUNKUN, All Rights Reserved.
*/ */
#ifndef _LOCALIZATION_H_ #ifndef _LOCALIZATION_H_
#define _LOCALIZATION_H_ #define _LOCALIZATION_H_
#include <string> #include <string>
#include <vector> #include <vector>
namespace localization { namespace localization {
std::vector<std::string> menu = {u8"菜单", "Menu"}; std::vector<std::string> menu = {u8"菜单:", "Menu"};
std::vector<std::string> local_id = {u8"本机ID:", "Local ID:"}; std::vector<std::string> local_id = {u8"本机ID:", "Local ID:"};
std::vector<std::string> password = {u8"密码:", "Password:"}; std::vector<std::string> password = {u8"密码:", "Password:"};
std::vector<std::string> max_password_len = {u8"最大6个租房字符", std::vector<std::string> max_password_len = {u8"最大6个字符", "Max 6 chars"};
"Max 6 chars"};
std::vector<std::string> remote_id = {u8"远端ID:", "Remote ID:"}; std::vector<std::string> remote_id = {u8"远端ID:", "Remote ID:"};
std::vector<std::string> connect = {u8"连接", "Connect"}; std::vector<std::string> connect = {u8"连接", "Connect"};
std::vector<std::string> disconnect = {u8"断开连接", "Disconnect"}; std::vector<std::string> disconnect = {u8"断开连接", "Disconnect"};
std::vector<std::string> reset_ratio = {u8"重置窗口", "Reset ratio"};
std::vector<std::string> fullscreen = {u8"全屏", "Fullscreen"}; std::vector<std::string> fullscreen = {u8"全屏", "Fullscreen"};
std::vector<std::string> exit_fullscreen = {u8"退出全屏", "Exit fullscreen"}; std::vector<std::string> exit_fullscreen = {u8"退出全屏", "Exit fullscreen"};

View File

@@ -291,24 +291,6 @@ int MainWindow::Run() {
ImGui::Spacing(); ImGui::Spacing();
{
if (ImGui::Button(
localization::reset_ratio[localization_language_index_]
.c_str())) {
SDL_GetWindowSize(main_window_, &main_window_width_,
&main_window_height_);
if (main_window_height_ != main_window_width_ * 9 / 16) {
main_window_width_ = main_window_height_ * 16 / 9;
}
SDL_SetWindowSize(main_window_, main_window_width_,
main_window_height_);
}
}
ImGui::SameLine();
if (ImGui::Button(fullscreen_button_label_.c_str())) { if (ImGui::Button(fullscreen_button_label_.c_str())) {
if (fullscreen_button_label_ == if (fullscreen_button_label_ ==
localization::fullscreen[localization_language_index_]) { localization::fullscreen[localization_language_index_]) {
@@ -332,6 +314,45 @@ int MainWindow::Run() {
SDL_RenderSetScale(sdl_renderer_, io.DisplayFramebufferScale.x, SDL_RenderSetScale(sdl_renderer_, io.DisplayFramebufferScale.x,
io.DisplayFramebufferScale.y); io.DisplayFramebufferScale.y);
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT) {
exit_ = true;
} else if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_RESIZED) {
int window_w_last = main_window_width_;
int window_h_last = main_window_height_;
SDL_GetWindowSize(main_window_, &main_window_width_,
&main_window_height_);
int w_change_ratio = abs(main_window_width_ - window_w_last) / 16;
int h_change_ratio = abs(main_window_height_ - window_h_last) / 9;
if (w_change_ratio > h_change_ratio) {
main_window_height_ = main_window_width_ * 9 / 16;
} else {
main_window_width_ = main_window_height_ * 16 / 9;
}
SDL_SetWindowSize(main_window_, main_window_width_,
main_window_height_);
} else if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(main_window_)) {
exit_ = true;
} else if (event.type == REFRESH_EVENT) {
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = main_window_width_;
sdlRect.h = main_window_height_;
// SDL_UpdateTexture(sdl_texture_, NULL, dst_buffer, pixel_w);
} else {
}
}
SDL_RenderClear(sdl_renderer_); SDL_RenderClear(sdl_renderer_);
SDL_RenderCopy(sdl_renderer_, sdl_texture_, NULL, &sdlRect); SDL_RenderCopy(sdl_renderer_, sdl_texture_, NULL, &sdlRect);