OSHGUI - Ogre Simple Hud and Graphical User Interface

A place to show off your latest screenshots and for people to comment on them. Only start a new thread here if you have some nice images to show off!
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

OSHGUI - Ogre Simple Hud and Graphical User Interface

Post by mkultra333 »

Needed a gui/hud. Hate all the guis out there. Wrote my own.

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 ;
(Edit: Just noticed, the ".Parent" in the menu definition is no longer useful. There is no heirarchy structure to the menus.)

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.

Image

Here you can see a list box opened up on the simple menu.

Image

Selecting "Bee" has caused a third menu to open.

Image

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) ;
			}
So, there's not a lot of flash, no bells or whistles, but it's easy to set up and easy to modify and expand on. There's still room on the TGA to add hud elements like weapons, health, etc. You can always sex it up by changing the graphics on the TGA and writing additional code for new functions. Or render it to some other texture and do some post-processing before final output.

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
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: OSHGUI - Ogre Simple Hud and Graphical User Interface

Post by mkultra333 »

Due to overwhelming response and interest expressed here in the thread, I thought I should do an update. :)

Started to use my gui, found a few bugs and some areas that needed improvement. So here's an update. Fixed a bug in the listbox drawing. Got rid of Ogre2D's ability to use multiple textures, now it's strictly a one texture system. From that, got rid of some code I'd added to reduce batches when multiple textures were used. Added another type of button, the BUTTONACTION_STATICTEXTBACK, just static text but highlighted by a background. Added some colour to list boxes to make them more recognizable.

Made setting up the menus and buttons better, so you call a function instead of setting the raw data. So now the setup for a small menu with some buttons might look like this:

Code: Select all

	CreateMenuB(MENU_TEXTURE, 910, 330, 320, 147, true, true) ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_STATIC_TEXUREEDIT,760,380,20,12, BUTTONACTION_STATICTEXT, "", "Texture Edit") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_COMBO_TEXTURE, 715,428,126,12, BUTTONACTION_LIST, "Image", "Image 1~Image 2") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_STATIC_TEXTURE,702,396,34,12, BUTTONACTION_STATICTEXTBACK, "", "Tex: 333") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_COMBO_TEXTURE_MODE, 715,444,56,12, BUTTONACTION_LIST, "Mode", "Mode 1~Mode 2") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_EDIT_TEXTURE_ROTATION,779+EdShift,444,32-EdShift,12, BUTTONACTION_EDIT_INTEGER, "Rot", "0") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_EDIT_TEXTURE_SCALEX,732+EdShift,460,32-EdShift,12, BUTTONACTION_EDIT_INTEGER, "Scale 1/64 U", "0") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_EDIT_TEXTURE_SCALEY,779+EdShift,460,32-EdShift,12, BUTTONACTION_EDIT_INTEGER, "V", "0") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_EDIT_TEXTURE_OFFSETX,732+EdShift,476,32-EdShift,12, BUTTONACTION_EDIT_INTEGER, "Offset 1/64 U", "0") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_EDIT_TEXTURE_OFFSETY,779+EdShift,476,32-EdShift,12, BUTTONACTION_EDIT_INTEGER, "V", "0") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_COMBO_TEXTURESET, 750,396,50,12, BUTTONACTION_LIST, "", "Texture Set A~Texture Set B~Texture Set C") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_EDIT_TEXTURE_NUMBER,691+EdShift,412,32-EdShift,12, BUTTONACTION_EDIT_INTEGER, "???", "0") ;
	CreateButtonTexEd(MENU_TEXTURE, MENU_TEXTURE_EDIT_TEXTURE_NAME,713+EdShift,412,128-EdShift,12, BUTTONACTION_EDIT_TEXT, "", "Texture Name") ;
This project is pragmatically inspired, I'm using it to update an old VC6 DX7 MFC editor application I wrote years ago. The updated project I'm including here is my first steps, converting the old MFC gui to the OSHGUI. In the code you'll notice a bunch of specialized button creation functions that are used to convert the old MFC resource button positions into the virtual OSHGUI coordinates. You'd ditch those in your own project, as well as all the buttons created, they're just to demonstrate.

Here's a pic of the actual map editor WIP. I'm not intending to release the editor, it's just here to demonstrate the gui.

Image

Here's the project. Still based on the Ogre Tutorial Framework. (Link broken, fixing now) New Link: http://www11.zippyshare.com/v/90187556/file.html
Last edited by mkultra333 on Wed May 02, 2012 9:10 am, edited 2 times in total.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: OSHGUI - Ogre Simple Hud and Graphical User Interface

Post by jacmoe »

mkultra333 wrote:Due to overwhelming response and interest expressed here in the thread, I thought I should do an update. :)
:lol:

I guess we haven't recovered from the last GUI wave. :)

But do keep it up.
You know: scratching an itch is often the best motivation.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: OSHGUI - Ogre Simple Hud and Graphical User Interface

Post by bstone »

Sorry, but I couldn't resist! :lol:

Image
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: OSHGUI - Ogre Simple Hud and Graphical User Interface

Post by mkultra333 »

Jacmoe: Thanks.

bstone: Ha! Did you just make that or has it been around before and I missed it? So true, so tragically true... In my defence, I've set out to make a gui that has less features than any one of those other guis on it's own, let alone all of them combined.

Speaking of which... not a feature, but a bit of design fix. I realized that having to define the menus inside the class itself kinda sux, and is unusable if you want more than one instance of Oshgui doing different things. So I fixed it, you now define all menus and buttons outside of the class, for instance inside baseApplication.ccp. Also I checked, the licenses on the old Ogre2D are actually zlib, not MIT, not that it makes any difference really.

Fixed up TutorialApp: http://www65.zippyshare.com/v/37480151/file.html
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: OSHGUI - Ogre Simple Hud and Graphical User Interface

Post by bstone »

Yeah, that's definitely not your case specifically. It just struck me that I could squeeze some fun into this thread. And no, you couldn't see it somewhere else - you host a premiere here, lol.

As for your design decisions - no size fits all. The only problem is it's hard to know which fits unless you try most of them.