1+ #include < algorithm>
12#include " meteor/ui/ui_window.h"
23
34extern std::string OnButtonOpenFile ();
@@ -29,6 +30,15 @@ ImageViewerInfo ImageViewer(const ImageViewerArgs &args) {
2930 ImGui::SameLine ();
3031 ImGui::LabelText (" Path" , " %s" , ret_info.path .c_str ());
3132 ImGui::PopItemWidth ();
33+ ImGui::SameLine ();
34+ ret_info.toggle_reset = ImGui::Button (" Reset" );
35+ if (ret_info.toggle_reset ) {
36+ ret_info.scale = 1 .f ;
37+ ret_info.image_pos_uv [0 ] = 0 .f ;
38+ ret_info.image_pos_uv [1 ] = 0 .f ;
39+ ret_info.image_pos_uv [2 ] = 1 .f ;
40+ ret_info.image_pos_uv [3 ] = 1 .f ;
41+ }
3242 float w3 = ret_info.size .x * 0 .113f ;
3343 ImGui::PushItemWidth (w3);
3444 ret_info.toggle_format_change = false ;
@@ -46,7 +56,7 @@ ImageViewerInfo ImageViewer(const ImageViewerArgs &args) {
4656 ImGui::SameLine ();
4757 ret_info.toggle_refresh = ImGui::InputInt2 (" Image Size" , ret_info.image_size );
4858 ImGui::SameLine ();
49- ret_info.toggle_run = ImGui::Button (" R" , ImVec2{20 , 20 });
59+ ret_info.toggle_run ^ = ImGui::Button (" R" , ImVec2{20 , 20 });
5060 if (ImGui::IsItemHovered ()) {
5161 ImGui::SetTooltip (" Run" );
5262 }
@@ -64,6 +74,9 @@ ImageViewerInfo ImageViewer(const ImageViewerArgs &args) {
6474 if (ret_info.frame_num > 0 ) ret_info.frame_num --;
6575 } else if (ret_info.toggle_next ) {
6676 if (ret_info.frame_num < args.max_frame - 1 ) ret_info.frame_num ++;
77+ } else if (ret_info.toggle_run ) {
78+ ret_info.frame_num ++;
79+ if (ret_info.frame_num >= args.max_frame - 1 ) ret_info.frame_num = 0 ;
6780 }
6881 ImGui::SameLine ();
6982 ret_info.toggle_jump =
@@ -89,16 +102,18 @@ ImageViewerInfo ImageViewer(const ImageViewerArgs &args) {
89102 ImGui::BeginTooltip ();
90103 float region_sz = 32 .0f ;
91104 float region_x = io.MousePos .x - pos.x - region_sz * 0 .5f ;
105+ float region_y = io.MousePos .y - pos.y - region_sz * 0 .5f ;
92106 if (region_x < 0 .0f )
93107 region_x = 0 .0f ;
94108 else if (region_x > tex_sz.x - region_sz)
95109 region_x = tex_sz.x - region_sz;
96- float region_y = io.MousePos .y - pos.y - region_sz * 0 .5f ;
97110 if (region_y < 0 .0f )
98111 region_y = 0 .0f ;
99112 else if (region_y > tex_sz.y - region_sz)
100113 region_y = tex_sz.y - region_sz;
101114 float zoom = 4 .0f ;
115+ region_x += uv0.x * tex_sz.x ;
116+ region_y += uv0.y * tex_sz.y ;
102117 ImGui::Text (" Min: (%.2f, %.2f)" , region_x, region_y);
103118 ImVec2 xy0 = ImVec2 ((region_x) / tex_sz.x , (region_y) / tex_sz.y );
104119 ImVec2 xy1 = ImVec2 ((region_x + region_sz) / tex_sz.x ,
@@ -109,21 +124,25 @@ ImageViewerInfo ImageViewer(const ImageViewerArgs &args) {
109124 ImGui::EndTooltip ();
110125 // use mouse wheel to zoom image
111126 ret_info.scale += 0 .2f * io.MouseWheel ;
127+ static float dx, dy;
112128 if (io.MouseDown [0 ]) {
113129 // Drag to move
114130 static ImVec2 prev_uv;
115131 if (io.MouseClicked [0 ]) {
116132 prev_uv = uv0;
117133 }
118- auto dx = io.MouseClickedPos [0 ].x - io.MousePos .x ;
119- auto dy = io.MouseClickedPos [0 ].y - io.MousePos .y ;
134+ dx = io.MouseClickedPos [0 ].x - io.MousePos .x ;
135+ dy = io.MouseClickedPos [0 ].y - io.MousePos .y ;
120136 dx += prev_uv.x * tex_sz.x ;
121137 dy += prev_uv.y * tex_sz.y ;
122- ret_info.image_pos_uv [0 ] = dx / tex_sz.x ;
123- ret_info.image_pos_uv [1 ] = dy / tex_sz.y ;
124- ret_info.image_pos_uv [2 ] = 1 + dx / tex_sz.x ;
125- ret_info.image_pos_uv [3 ] = 1 + dy / tex_sz.y ;
126138 }
139+ auto boarder = ImGui::GetWindowSize ();
140+ dx = std::min (std::max (0 .f , dx), std::max (0 .f , tex_sz.x - boarder.x ));
141+ dy = std::min (std::max (0 .f , dy), std::max (0 .f , tex_sz.y - boarder.y ));
142+ ret_info.image_pos_uv [0 ] = dx / tex_sz.x ;
143+ ret_info.image_pos_uv [1 ] = dy / tex_sz.y ;
144+ ret_info.image_pos_uv [2 ] = 1 + dx / tex_sz.x ;
145+ ret_info.image_pos_uv [3 ] = 1 + dy / tex_sz.y ;
127146 }
128147 }
129148 ImGui::EndChild ();
0 commit comments