[Fix] Correct menu bar rendering and event handling.
This commit is contained in:
		
							parent
							
								
									a6e159f0da
								
							
						
					
					
						commit
						601bc86bdc
					
				@ -49,10 +49,10 @@ void Editor::handle_input(SDL_Event* event, int window_x, int window_y, int wind
 | 
				
			|||||||
void Editor::render(const RenderContext& context, int x, int y, int width, int height) {
 | 
					void Editor::render(const RenderContext& context, int x, int y, int width, int height) {
 | 
				
			||||||
  int menu_bar_height = _menu_bar->get_height();
 | 
					  int menu_bar_height = _menu_bar->get_height();
 | 
				
			||||||
  int menu_bar_y_gl = y + height - menu_bar_height;
 | 
					  int menu_bar_y_gl = y + height - menu_bar_height;
 | 
				
			||||||
  _menu_bar->render(context.shape_renderer, context.txt_renderer, x, menu_bar_y_gl, width);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  _view->render(context.txt_renderer, x, y, width, height - menu_bar_height,
 | 
					  _view->render(context.txt_renderer, x, y, width, height - menu_bar_height,
 | 
				
			||||||
                context.show_cursor);
 | 
					                context.show_cursor);
 | 
				
			||||||
 | 
					  _menu_bar->render(context.shape_renderer, context.txt_renderer, x, menu_bar_y_gl, width);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Editor::scroll(int amount, int content_height) {
 | 
					void Editor::scroll(int amount, int content_height) {
 | 
				
			||||||
 | 
				
			|||||||
@ -30,36 +30,41 @@ void MenuBar::handle_event(SDL_Event* event, int window_x, int window_y, int win
 | 
				
			|||||||
  if(event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
 | 
					  if(event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
 | 
				
			||||||
    int mouse_x = event->button.x;
 | 
					    int mouse_x = event->button.x;
 | 
				
			||||||
    int mouse_y = event->button.y;
 | 
					    int mouse_y = event->button.y;
 | 
				
			||||||
 | 
					    const int title_bar_height = 30;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int menu_x = window_x;
 | 
					    int menu_bar_screen_y = window_y + title_bar_height;
 | 
				
			||||||
    for(size_t i = 0; i < _menus.size(); ++i) {
 | 
					    int current_menu_x = window_x;
 | 
				
			||||||
      int menu_width = 60; /* Just hardcode this for now. */
 | 
					    bool click_on_bar = false;
 | 
				
			||||||
      if(mouse_x >= menu_x && mouse_x <= menu_x + menu_width &&
 | 
					 | 
				
			||||||
         mouse_y >= window_y && mouse_y <= window_y + _height) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if(_open_menu_index == (int)i) {
 | 
					    if(mouse_y >= menu_bar_screen_y && mouse_y <= menu_bar_screen_y + _height) {
 | 
				
			||||||
          _open_menu_index = -1; /* Close if open already. */
 | 
					      for(size_t i = 0; i < _menus.size(); ++i) {
 | 
				
			||||||
        } else {
 | 
					        int menu_width = 60;
 | 
				
			||||||
          _open_menu_index = i;
 | 
					        if(mouse_x >= current_menu_x && mouse_x <= current_menu_x + menu_width) {
 | 
				
			||||||
 | 
					          _open_menu_index = (_open_menu_index == (int)i) ? -1: i;
 | 
				
			||||||
 | 
					          click_on_bar = true;
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return;
 | 
					        current_menu_x += menu_width;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      menu_x += menu_width;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if(click_on_bar) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(_open_menu_index != -1) {
 | 
					    if(_open_menu_index != -1) {
 | 
				
			||||||
      Menu& open_menu = _menus[_open_menu_index];
 | 
					      Menu& open_menu = _menus[_open_menu_index];
 | 
				
			||||||
      int item_y = window_gl_y + _height;
 | 
					      int dropdown_x = window_x + (_open_menu_index * 60);
 | 
				
			||||||
 | 
					      int dropdown_y = menu_bar_screen_y + _height;
 | 
				
			||||||
      int item_height = 30;
 | 
					      int item_height = 30;
 | 
				
			||||||
      int dropdown_width = 150;
 | 
					      int dropdown_width = 150;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      for(const auto& item: open_menu.items) {
 | 
					      for(const auto& item: open_menu.items) {
 | 
				
			||||||
        if(mouse_x >= window_x && mouse_x <= window_x  + dropdown_width &&
 | 
					        if(mouse_x >= dropdown_x && mouse_x <= dropdown_x + dropdown_width &&
 | 
				
			||||||
          (window_gl_y - mouse_y) >= item_y && (window_gl_y - mouse_y) <= item_y + item_height) {
 | 
					           mouse_y >= dropdown_y && mouse_y <= dropdown_y + item_height) {
 | 
				
			||||||
          item.action();
 | 
					          item.action();
 | 
				
			||||||
          _open_menu_index = -1; /* Close menu. */
 | 
					          _open_menu_index = -1; /* Close menu. */
 | 
				
			||||||
          return;
 | 
					          return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        item_y += item_height;
 | 
					        dropdown_y += item_height;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    /* Close any open menu when clicked outside. */
 | 
					    /* Close any open menu when clicked outside. */
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user