OXYGENHUB
◆ main openmsx / src / imgui / ImGuiAdjust.hh
1.9 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef IMGUI_ADJUST_HH
2
#define IMGUI_ADJUST_HH
3
4
#include "ImGuiPartInterface.hh"
5
6
#include "gl_vec.hh"
7
8
namespace openmsx {
9
10
// Keep the imgui window in the 'same' position in the main view port (= the
11
// main openMSX window) when that view port changes size. This happens when
12
// changing scale_factor, but also when switching to/from fullscreen.
13
//
14
// Without this adjustment, after the resize, the window can e.g. move from the
15
// border to somewhere in the middle of the view port, or even outside the view
16
// port. This is not desired for stuff like the OSD icons or the reverse-bar.
17
//
18
// The heuristic for keeping the window in the 'same' position is:
19
// * Only make adjustment when the window is shown fully inside the main view port.
20
// * Separately for the horizontal and vertical direction:
21
// * Check if the window is closer to the left or the right (or top or bottom)
22
//   border of the viewport.
23
// * Keep this closet distance the same after rescaling. In other words, if the
24
//   window was e.g. close to the bottom, it will remain near the bottom.
25
// * And some stuff for boundary conditions, see comments in the code for details.
26
class AdjustWindowInMainViewPort
27
{
28
public:
29
	// should be called right before the call to im::Window()
30
	void pre();
31
32
	// should be called right after the call to im::Window()
33
	bool post();
34
35
	void save(ImGuiTextBuffer& buf);
36
	bool loadLine(std::string_view name, zstring_view value);
37
38
private:
39
	gl::vec2 oldViewPortSize;
40
	gl::vec2 oldRelWindowPos;
41
	bool oldIsOnMainViewPort = false;
42
	bool setMainViewPort = false;
43
44
	static constexpr auto persistentElements = std::tuple{
45
		PersistentElement{"viewPortSize",   &AdjustWindowInMainViewPort::oldViewPortSize},
46
		PersistentElement{"windowPos",      &AdjustWindowInMainViewPort::oldRelWindowPos},
47
		PersistentElement{"onMainViewPort", &AdjustWindowInMainViewPort::oldIsOnMainViewPort}
48
	};
49
};
50
51
} // namespace openmsx
52
53
#endif