Skip to content

Commit 285b7ea

Browse files
committed
Some weird bug. offset not working properly
1 parent 595bdcc commit 285b7ea

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

Examples/GameMenu_sample/tests/sample_menu.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void main() {
1818
sf::Font font;
1919
font.loadFromFile( "sansation.ttf" );
2020
gmenu::Style style( font, font );
21+
style.layout = gmenu::Layout::TitleCentre;
2122
for ( int i = 0; i < 3; ++i ) {
2223
item.title = text[i];
2324
item.action = new test::testAction();

include/GameMenu/GameMenu.h

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,41 @@ namespace gmenu {
3535
/* BitFlags for Different possible Layouts */
3636
enum Layout {
3737

38-
TitleCentre = 1<<0,
39-
TitleRight = 1<<1,
40-
TitleLeft = 1<<2,
41-
42-
ItemCentre = 1<<3,
43-
ItemRight = 1<<4,
44-
ItemLeft = 1<<5,
45-
46-
TitleFar = 1<<6,
47-
TitleNear = 1<<7,
48-
TitleNormal = 1<<8,
38+
TitleCentre = 1 << 0,
39+
TitleRight = 1 << 1,
40+
TitleLeft = 1 << 2,
4941

50-
ItemSpaceCompact = 1<<9,
51-
ItemSpaceNormal = 1<<10,
52-
ItemSpaceFar = 1<<11,
42+
ItemCentre = 1 << 3,
43+
ItemRight = 1 << 4,
44+
ItemLeft = 1 << 5,
5345

46+
Default = TitleCentre | ItemCentre
47+
5448
};
5549

5650
/* Defines the style of the menu */
5751
struct Style {
5852
sf::Font &TitleFont;
5953
sf::Font &ItemFont;
60-
sf::Color TitleColor;
61-
sf::Color ItemColor;
62-
sf::Color Selected;
63-
unsigned int TitleFontSize;
64-
unsigned int ItemFontSize;
65-
Layout layout;
54+
55+
sf::Color TitleColor = sf::Color::Green;;
56+
sf::Color ItemColor = sf::Color::Red ;
57+
sf::Color Selected = sf::Color::Blue;
58+
59+
unsigned int TitleFontSize = 50;
60+
unsigned int ItemFontSize = 20;
61+
62+
float MenuTitleScaleFactor = 0.125;
63+
float MenuItemScaleFactor = 0.25;
64+
65+
struct {
66+
unsigned int top, left;
67+
} Padding;
68+
69+
int layout = Layout::Default;
6670
Style(sf::Font &mf, sf::Font &itmf):
67-
TitleFont(mf),ItemFont(itmf){
68-
TitleColor = sf::Color::Green;
69-
ItemColor = sf::Color::Red;
70-
Selected = sf::Color::Blue;
71-
ItemFontSize = 20;
72-
TitleFontSize = 50;
71+
TitleFont( mf ), ItemFont( itmf ), Padding {10,0}
72+
{
7373
}
7474
};
7575

@@ -146,8 +146,7 @@ namespace gmenu {
146146
std::string menu_title;
147147

148148
// TODO: create an interface to set these
149-
float MenuTitleScaleFactor = 0.125;
150-
float MenuItemScaleFactor = 0.25;
149+
151150

152151
}; // Menu
153152

lib/libGameMenu.a

175 KB
Binary file not shown.

src/GameMenu/GameMenu.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
#include <GameMenu/GameMenu.h>
8-
8+
#include <iostream>
99
namespace gmenu {
1010

1111

@@ -72,31 +72,50 @@ namespace gmenu {
7272
text.setCharacterSize(size);
7373
sf::FloatRect textRect = text.getLocalBounds();
7474
text.setOrigin(textRect.width / 2.0f,0);
75+
if ( x - textRect.width / 2.0f < 0 ) {
76+
//std::cout << x << " " << textRect.width / 2.0f;
77+
x = textRect.width / 2 + style.Padding.left;
78+
}
7579
text.setPosition(sf::Vector2f(x,y));
7680
window->draw(text);
7781
} //writeText(...)
7882

7983
void Menu::setMenu() {
8084

85+
std::cout << "screen size:" << window->getSize().x << " " << window->getSize().y << std::endl;
86+
8187
/* Setting title of menu */
8288
{
8389
/* Small scope just to be able to freely use the variable names */
84-
float x = (float) window->getSize().x / 2, y = 0;
85-
title_location.x = x;
90+
int offset_coefficient = 0.5;
91+
if ( style.layout & Layout::TitleCentre == Layout::TitleCentre ) offset_coefficient = 0.5;
92+
else if ( style.layout & Layout::TitleLeft == Layout::TitleLeft ) offset_coefficient = 0.25;
93+
else if ( style.layout & Layout::TitleRight == Layout::TitleRight ) offset_coefficient = 0.75;
94+
float x = (float) window->getSize().x * offset_coefficient, y = style.Padding.top;
95+
title_location.x = (x + style.Padding.left);
8696
title_location.y = y;
97+
std::cout << "title_location:" << title_location.x << " "<<title_location.y<<offset_coefficient<<std::endl;
8798
}
8899

89-
unsigned int menu_screen_height =(int) window->getSize().y * (1 - MenuTitleScaleFactor);
90-
unsigned int block_height = (int) menu_screen_height / menu_items.size * MenuItemScaleFactor;
91-
float x = (float)window->getSize().x / 2;
92-
float y = (float)window->getSize().y - 0.75 * menu_screen_height + block_height * 1 / 8;
100+
unsigned int menu_screen_height =(int) window->getSize().y * (1 - style.MenuTitleScaleFactor);
101+
unsigned int block_height = (int) menu_screen_height / menu_items.size * style.MenuItemScaleFactor;
102+
103+
float offset_coefficient = 0.5;
104+
if ( style.layout & Layout::ItemCentre == Layout::ItemCentre ) offset_coefficient = 0.5;
105+
else if ( style.layout & Layout::ItemLeft == Layout::ItemLeft ) offset_coefficient = 0.25;
106+
else if ( style.layout & Layout::ItemRight == Layout::ItemRight) offset_coefficient = 0.75;
107+
108+
float x = (float)window->getSize().x * offset_coefficient + style.Padding.left;
109+
float y = ((float)window->getSize().y) - 0.75 * menu_screen_height + block_height * 1 / 8;
93110
/* Calculating Menu item locations */
94111
for (int8_t i = 0; i < menu_items.size; ++i) {
95112
coordinates crd ;
96113
crd.x = x;
97114
crd.y = y;
98115
item_location.push_back( crd );
116+
std::cout << "menu location:" << x << " " << y <<offset_coefficient<< std::endl;
99117
y += block_height;
118+
100119
}
101120

102121
} //setMenu()
@@ -116,4 +135,5 @@ namespace gmenu {
116135
} //drawMenu()
117136

118137

138+
119139
} // namespace sui

0 commit comments

Comments
 (0)