[SOLVED] How to use relative positions?

Buckcherry

24-05-2008 11:45:09

Hi everybody,

I would like to know how to use relative positions to place an object or specify a size.

Any hint, please?

kungfoomasta

26-05-2008 21:31:04

All positions are relative to their parent by default. If you want a relative size you have to use horizontal and vertical anchoring, which maintains the distance between left/right/top/bottom edges of a widgets parent, and may shrink/grow the widget when its parent is resized.

Buckcherry

28-05-2008 18:13:11

The relative positions does not work for me.

I have this code to initialize the QuickGUI:

new QuickGUI::Root();
QuickGUI::SkinSetManager::getSingleton().loadSkin("qgui",QuickGUI::SkinSet::IMAGE_TYPE_PNG);
mGUIManager = QuickGUI::Root::getSingleton().createGUIManager(mCamera->getViewport(),"qgui");
mGUIManager->setSceneManager(mSceneMgr);
mSheet = mGUIManager->getDefaultSheet();


And this is the code I use to create a text label:

mText = mSheet->createLabel("DebugText");
mText->setText((UTFString)"Test");
mText->setPosition(250,550);
mText->setSize(200,40);


Changing the resolution of the screen, the position of the label changes of position.

What is wrong?.

kungfoomasta

28-05-2008 19:28:14

Are you calling

mGUIManager->_notifyViewportDimensionsChanged()

?

The label should remain 250 pixels right of the sheet's left edge and 550 pixels below the sheet's top edge. If you want it to stay relative to the bottom or right side of the sheet, you'll need to change the horizontal/vertical anchors, ie

mText->setHorizontalAnchor(ANCHOR_HORIZONTAL_RIGHT);
mText->setVerticalAnchor(ANCHOR_VERTICAL_BOTTOM);

Buckcherry

28-05-2008 20:48:45

I have added the line you said to my code:

new QuickGUI::Root();
QuickGUI::SkinSetManager::getSingleton().loadSkin("qgui",QuickGUI::SkinSet::IMAGE_TYPE_PNG);
mGUIManager = QuickGUI::Root::getSingleton().createGUIManager(mCamera->getViewport(),"qgui");
mGUIManager->setSceneManager(mSceneMgr);
mGUIManager->_notifyViewportDimensionsChanged();
mSheet = mGUIManager->getDefaultSheet();


But it is still not working...

I do not understand how to set the position of the text relative to the screen...

Do I have to calculate the height and width of the screen manually?.

I am making tests changing from 800x600 to 1024x768 resolutions. In 800x600 mode the text appears at the bottom-middle of the screen, but changing it to 1024x768 it appears to the left, it is not centred.

How do I get the ANCHOR_HORIZONTAL_RIGHT and ANCHOR_VERTICAL_BOTTOM parameters?

kungfoomasta

29-05-2008 01:08:44

You need to call mGUIManager->_notifyViewportDimensionsChanged(); whenever your viewport size changes. So you call this AFTER you've changed resolution.

How do I get the ANCHOR_HORIZONTAL_RIGHT and ANCHOR_VERTICAL_BOTTOM parameters?

They are just simple enumerated types, you can use them to set how a widget is anchored horizontally and vertically. The code in my previous post shows how you set the anchors.

Buckcherry

31-05-2008 19:43:01

So, where do I have to call the mGUIManager->_notifyViewportDimensionsChanged();?

I am basing the application in the ExampleApplication.

kungfoomasta

31-05-2008 20:23:30

If you look at the demo you'll see its in the callback that is called when the window is resized. Are you changing resolutions during execution, or before each time you start your app? Can you show any screenshots of what you're seeing?

Buckcherry

01-06-2008 08:46:43

I am changing the resolution each time I start the application, using the Ogre3D typical dialog, in which you can choose between DirectX or OpenGL...

kungfoomasta

02-06-2008 18:55:30

Ok, so you don't need to call _notifyDimensionsChanged.

Following your code, the TextBox should always be 250 pixels from the left of the sheet, and 550 pixels from the top of the sheet. Is this not the case?

Buckcherry

02-06-2008 21:03:59

Yes, the TextBox is always 250 pixles from the left of the sheet and 550 pixels from the top of the sheet.

But I want the widget appears in the same proportions on the screen. I mean:

- With a 800x600 resolution, the TextBox appears in the bottom middle of the screen
- With a 1024x768 resolution, the TextBox appears displaced up and left respecting to the previous resolution.

Is there any way to mantain the proportions on the screen?. That is, the TextBox appears on the bottom middle of the screen independently of its resolution.

kungfoomasta

02-06-2008 21:06:16

mText->setHorizontalAnchor(ANCHOR_HORIZONTAL_LEFT_RIGHT);
mText->setVerticalAnchor(ANCHOR_VERTICAL_TOP_BOTTOM);


If you play around with these functions you will understand how anchoring works. :)

Buckcherry

02-06-2008 21:30:24

Ok. I will try.

But it seems you can not set the position of the widget, for example, a 75% from the top and 60% from the left, can't you?.

kungfoomasta

02-06-2008 22:12:32

You're right. Anchoring is used mainly to determine a widget's position/size when its parent gets resized. For example, a Button in a window. Some people may always want the button in the center of the window, and some may always want the button on the left hand side. How to support both of these needs?

Until I can come up with an elegant solution to this, you'd probably have to set the widget's position manually, based on the Sheet's size. Just create one function that will position all your widgets according to the Sheet's size, and call this function after you've created all the widgets.

Buckcherry

03-06-2008 21:26:41

I have solved my problem making my own function to place a widget according to a percentage of the screen.

Thank you very much!!! :-)