This is OSHGUI, Ogre Simple Hud and Graphical User Interface. It is quickly written and barely tested. It's probably full of bugs, and definitely full of code you'll hate. Design goals were:
1. Avoid CMake, premake, complex compiling setups, ten tonnes of dependencies.
2. Avoid any text or html scripting.
3. Avoid excessive OO so that it's easy to make any part change any other part.
4. Provide bare minimum of controls typically essential for a user interface.
To that end, it requires three files: ogre2d-main.cpp, ogre2d-main.h and BZN_newchars9.tga. If Ogre2d-main looks familiar, that's because it's someone else's code from the wiki, a small, fast, sprite class. I've simply tacked the gui onto it, as well as modifying it slightly so that you can change sprite colours or render to viewports other than the main one. Licenses are in the code, it's MIT or similar, and the tga is public domain (I made it).
Provided you stick to using a single texture, it'll all be rendered in one batch. The original Ogre2d has abilities to use multiple textures, I've not removed that but if you try then at best you'll get more batches, at worst, who knows, maybe it'll break.
The hud/menu is defined and drawn using "virtual" screen coordinates, on a virtual screen defined as being 1280 x 720. This just means that when you define the gui or hud, you don't have to worry about what resolution the user has, it'll look the same. It is rendered at the real screen resolution, not the virtual one, but all the coordinates are worked out relative to a screen of 1280 by 720. This might cause problems with people who use those super-wide multi-monitor setups, so additional code to fix that might be added later.
There are eight different kinds of controls, some are just variations.
1. Button. You press it.
2. Switch. This is a button that has up to 32 different states. Every time you press it, the state increments, until it loops around. States might be "Off", "Half Power", "Full Power" for example.
3. Static Text. Just a message.
4. Edit Box. Put in any text you want.
5. Edit Box Integer. Only integers within a defined range allowed. Can use cursor up/down or mousewheel to increase/decrease value.
6. Edit Box Float. Only floating point numbers withing a defined range allowed.
7. Check Box. On or off button.
8. List Box. Like a switch, 32 possible entries, but you can instantly select from any one instead of having to step through them.
There basically two types of things, "Menus" and "Buttons." A menu is a small collection of buttons grouped together. You can set menus as visible or not. Buttons are the controls on the menu, the 8 listed above.
Since there are no scripts, menus and buttons are created at runtime, and defined right inside of Ogre2d. For instance, this sets up a menu with one button:
Code: Select all
enum{
MENU_NONE=-1,
// user defined menus go here
MENU_MAIN=0, // our only menu
// reserved menus
MENU_LISTBOX, // dynamically created menu that functions as a general purpose list box
MENU_MAX
};
enum{
MENU_MAIN_NEWGAME, // the only button on our main menu
MENU_MAIN_MAXBUTTON
};
Code: Select all
BZNBUTTON NullButton ;
ZeroMemory( (void*)&NullButton, sizeof(BZNBUTTON) ) ;
///////////////////////////////////////////////////////////////////////////////////////////////////
// main menu
Menu[MENU_MAIN].Name[0]='\0' ;
nMaxButton=MENU_MAIN_MAXBUTTON ;
Menu[MENU_MAIN].Parent=MENU_NONE ;
Menu[MENU_MAIN].Back=true ;
Menu[MENU_MAIN].Visible=true ;
Menu[MENU_MAIN].L=VSCREEN_W * 1.0f/8.0f ;
Menu[MENU_MAIN].R=VSCREEN_W * 3.0f/8.0f ;
Menu[MENU_MAIN].U=VSCREEN_H * 2.0f/8.0f ;
Menu[MENU_MAIN].D=VSCREEN_H * 5.0f/8.0f ;
flMenuWidth=Menu[MENU_MAIN].R-Menu[MENU_MAIN].L ;
Menu[MENU_MAIN].Button.push_back(NullButton) ;
strcpy_s(Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].Name[0], MAX_BUTTON_TEXT, "Static Text") ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].MaxSwitch=0 ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].Switch=0 ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].PositionX=flMenuWidth/2-BUTTONFRAMEX/2 ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].SizeX=BUTTONSIZEX ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].SizeY=BUTTONSIZEY ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].FrameSizeX=BUTTONFRAMEX ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].FrameSizeY=BUTTONFRAMEY ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].Action=BUTTONACTION_STATICTEXT ;
Menu[MENU_MAIN].Button[MENU_MAIN_NEWGAME].Style=BUTTONSTYLE_FANCY ;
Heres a pic of a test setup. Some text, a few hud images, and two menus. One is in the "Fancy" style, which uses a bigger font and puts scrolling patterns on the background of buttons. The other uses the "Simple" style, which is more for small controls on an editor, for instance. The grey backgrounds on the menus are optional, you can have no background in you prefer.
Here you can see a list box opened up on the simple menu.
Selecting "Bee" has caused a third menu to open.
If you want to change the font, you need two create two fonts, a small and a large, and put them on the tga. Then you change the hard-wired texture coordinates of the letters in the code. If you look at the code, you'll see. It's not difficult assuming you've used a font generating program that gives you some kind of text file of the letter coordinates, like FontStudio for instance. You'll have to tweak the coords a little, to compensate for the fact that we are on a larger tga and there's more than one font there, but if you follow the example in the program you'll see that's fairly easy.
So during a frame you'd call the functions needed to run and render the gui/hud, and you'd get a return result that tells you if any controls have issued a message. For instance, here's a line of code from the demo program that processes a particular message.
Code: Select all
// here, turn on or off the menu depending on whether "Bee" is selected from the list.
// Listboxes are really just switches with a differnet way of changing the switch, and Bee is switch number 1
if((MResult.Menu==MENU_NEWGAME) && (MResult.Button==MENU_NEWGAME_LIST) && (MResult.Result==MENURESULT_BUTTONACTIVATED))
{
if(MResult.Switch==1)
Ogre2D->SetMenuVisibility(MENU_SYSTEMOPTIONS, true) ;
else
Ogre2D->SetMenuVisibility(MENU_SYSTEMOPTIONS, false) ;
}
I've included a demo project that just shows the screens above. It is the Tutorial Framework (http://www.ogre3d.org/tikiwiki/Ogre+Wik ... +Framework) with just the Gui added. If you search the project for "Ogre2D" you should find all the spots of BaseApplication that have been changed to run the gui, so you'll see how to set things up, how to print text and icons to the screen, how get and set input. Other than that, just experiment if you want a particular effect. DrawButtonSimple and DrawButtonFancy are probably the main two functions to control how buttons look.
Here's the project, including the TGA.
http://www7.zippyshare.com/v/45853391/file.html


