38
38
#include " gui/item_textfield.hpp"
39
39
#include " gui/item_toggle.hpp"
40
40
#include " gui/item_string_array.hpp"
41
+ #include " gui/item_images.hpp"
41
42
#include " gui/menu_item.hpp"
42
43
#include " gui/menu_manager.hpp"
43
44
#include " gui/mousecursor.hpp"
@@ -62,6 +63,7 @@ Menu::Menu() :
62
63
m_mn_input_char(' \0 ' ),
63
64
m_menu_repeat_time(),
64
65
m_menu_width(),
66
+ m_menu_height(),
65
67
m_items(),
66
68
m_arrange_left(0 ),
67
69
m_active_item(-1 )
@@ -97,6 +99,7 @@ Menu::add_item(std::unique_ptr<MenuItem> new_item)
97
99
}
98
100
99
101
calculate_width ();
102
+ calculate_height ();
100
103
101
104
return item;
102
105
}
@@ -116,6 +119,7 @@ Menu::add_item(std::unique_ptr<MenuItem> new_item, int pos_)
116
119
}
117
120
118
121
calculate_width ();
122
+ calculate_height ();
119
123
120
124
return item;
121
125
}
@@ -353,6 +357,24 @@ Menu::add_string_array(const std::string& text, std::vector<std::string>& items,
353
357
return *item_ptr;
354
358
}
355
359
360
+ ItemImages&
361
+ Menu::add_images (const std::string& image_path, int max_image_width, int max_image_height, int id)
362
+ {
363
+ auto item = std::make_unique<ItemImages>(image_path, max_image_width, max_image_height, id);
364
+ auto item_ptr = item.get ();
365
+ add_item (std::move (item));
366
+ return *item_ptr;
367
+ }
368
+
369
+ ItemImages&
370
+ Menu::add_images (const std::vector<std::string>& image_paths, int max_image_width, int max_image_height, int id)
371
+ {
372
+ auto item = std::make_unique<ItemImages>(image_paths, max_image_width, max_image_height, id);
373
+ auto item_ptr = item.get ();
374
+ add_item (std::move (item));
375
+ return *item_ptr;
376
+ }
377
+
356
378
void
357
379
Menu::clear ()
358
380
{
@@ -547,28 +569,26 @@ Menu::process_action(const MenuAction& menuaction)
547
569
}
548
570
549
571
void
550
- Menu::draw_item (DrawingContext& context, int index)
572
+ Menu::draw_item (DrawingContext& context, int index, float y_pos )
551
573
{
552
- const float menu_height = get_height ();
553
574
const float menu_width = get_width ();
554
575
555
576
MenuItem* pitem = m_items[index].get ();
556
577
557
578
const float x_pos = m_pos.x - menu_width / 2 .0f ;
558
- const float y_pos = m_pos.y + 24 .0f * static_cast <float >(index) - menu_height / 2 .0f + 12 .0f ;
559
579
560
580
pitem->draw (context, Vector (x_pos, y_pos), static_cast <int >(menu_width), m_active_item == index);
561
581
562
582
if (m_active_item == index)
563
583
{
564
584
float blink = (sinf (g_real_time * math::PI * 1 .0f )/2 .0f + 0 .5f ) * 0 .5f + 0 .25f ;
565
- context.color ().draw_filled_rect (Rectf (Vector (m_pos.x - menu_width/2 + 10 - 2 , y_pos - 12 - 2 ),
566
- Vector (m_pos.x + menu_width/2 - 10 + 2 , y_pos + 12 + 2 )),
585
+ context.color ().draw_filled_rect (Rectf (Vector (m_pos.x - menu_width/2 + 10 - 2 , y_pos - static_cast < float >(pitem-> get_height ())/ 2 - 2 ),
586
+ Vector (m_pos.x + menu_width/2 - 10 + 2 , y_pos + static_cast < float >(pitem-> get_height ())/ 2 + 2 )),
567
587
Color (1 .0f , 1 .0f , 1 .0f , blink),
568
588
std::max (0 .f , g_config->menuroundness - 2 .f ),
569
589
LAYER_GUI-10 );
570
- context.color ().draw_filled_rect (Rectf (Vector (m_pos.x - menu_width/2 + 10 , y_pos - 12 ),
571
- Vector (m_pos.x + menu_width/2 - 10 , y_pos + 12 )),
590
+ context.color ().draw_filled_rect (Rectf (Vector (m_pos.x - menu_width/2 + 10 , y_pos - static_cast < float >(pitem-> get_height ())/ 2 ),
591
+ Vector (m_pos.x + menu_width/2 - 10 , y_pos + static_cast < float >(pitem-> get_height ())/ 2 )),
572
592
Color (1 .0f , 1 .0f , 1 .0f , 0 .5f ),
573
593
std::max (0 .f , g_config->menuroundness - 4 .f ),
574
594
LAYER_GUI-10 );
@@ -590,6 +610,17 @@ Menu::calculate_width()
590
610
m_menu_width = max_width;
591
611
}
592
612
613
+ void
614
+ Menu::calculate_height ()
615
+ {
616
+ float height = 0 ;
617
+ for (unsigned i = 0 ; i < m_items.size (); i++)
618
+ {
619
+ height += static_cast <float >(m_items[i]->get_height ());
620
+ }
621
+ m_menu_height = height;
622
+ }
623
+
593
624
float
594
625
Menu::get_width () const
595
626
{
@@ -599,7 +630,7 @@ Menu::get_width() const
599
630
float
600
631
Menu::get_height () const
601
632
{
602
- return static_cast < float >(m_items. size () * 24 ) ;
633
+ return m_menu_height ;
603
634
}
604
635
605
636
void
@@ -612,9 +643,12 @@ Menu::on_window_resize()
612
643
void
613
644
Menu::draw (DrawingContext& context)
614
645
{
646
+ const float menu_height = get_height ();
647
+ float y_pos = m_pos.y - menu_height / 2 .0f ;
615
648
for (unsigned int i = 0 ; i < m_items.size (); ++i)
616
649
{
617
- draw_item (context, i);
650
+ draw_item (context, i, y_pos + static_cast <float >(m_items[i]->get_height ())/2 );
651
+ y_pos += static_cast <float >(m_items[i]->get_height ());
618
652
}
619
653
620
654
if (!m_items[m_active_item]->get_help ().empty ())
@@ -719,8 +753,19 @@ Menu::event(const SDL_Event& ev)
719
753
y > m_pos.y - get_height ()/2 &&
720
754
y < m_pos.y + get_height ()/2 )
721
755
{
722
- int new_active_item
723
- = static_cast <int > ((y - (m_pos.y - get_height ()/2 )) / 24 );
756
+ int new_active_item = 0 ;
757
+ // This is probably not the most efficient way of finding active item
758
+ // but I can't think of something better right now ~ mrkubax10
759
+ float item_y = m_pos.y - get_height ()/2 ;
760
+ for (unsigned i = 0 ; i < m_items.size (); i++)
761
+ {
762
+ if (y >= item_y && y <= item_y + static_cast <float >(m_items[i]->get_height ()))
763
+ {
764
+ new_active_item = i;
765
+ break ;
766
+ }
767
+ item_y += static_cast <float >(m_items[i]->get_height ());
768
+ }
724
769
725
770
/* only change the mouse focus to a selectable item */
726
771
if (!m_items[new_active_item]->skippable () &&
0 commit comments