Jump to content

Afke's Blog

  • entries
    3
  • comments
    29
  • views
    6,775

My GUI


Afke

3,969 views

 Share

A couple of days I've worked on a GUI solution, and I made good progress. I tried to make as simple solution that completely fits the style of the engine.

Of course I have not finished everything I planned but I already have a version that works.

 

The basic idea is to be modular and variable depending on size of screen, it must look the same on all screen resolutions, and everything happens automatically in the background.

 

This is how everything works:

 

CreateGui();

addFont("fonts/ArialBlack10",0.8);
addFont("fonts/ArialBlack12",1);
addFont("fonts/ArialBlack14",1.1);
addFont("fonts/ArialBlack16",1.2);

 

First we create a GUI screen that is 800x600 by default, of course it can be changed and add Fonts which we will use in the gui .

 

addFont(str name ,ftl scaling factor)

 

we adding fonts sizes as much as we need and scaling factor that is, in fact, Screensize (1024/768) / GuiSize (800/600) = 1.28 so engine will calculate size of fonts for this situation which will be "fonts/ArialBlack16". because 1.28 > 1.2 if we set 640x 480 factor will be 0.8 engine will chose "fonts/ArialBlack10".

 

 int id=CreateButton("btn1",Vec2(200,50),Vec4(1,0,0,0.5),Vec4(1,1,0,0.5),Vec4(1,1,1,0.9));
AddText(id,"Create Box",Vec4(1,1,1,1));
SetGuiPosition(id,Vec2(200,50));

int id1=CreateButton("btn2","data/ui/btn.dds","data/ui/btn1.dds","data/ui/btn2.dds",Vec4(1,1,1,0.9));
AddText(id1,"Rotate Box",Vec4(1,1,1,1));
SetGuiSize(id1,Vec2(200,50));
SetGuiPosition(id1,Vec2(200,150));

 

CreateButton(str name,TVec2 size,TVec4 color1,TVec4 color2,TVec4 color3)

 

this will add button with "name" ,size, defoult color , Mouse Over color ,click color.

 

names we will use later on for events

 

CreateButton(str name,img1,img2,img3,color)

 

this is image button with name default img ,Mouse over img and Click img

 

 

CreateButton function returns id of element which we use for adding text on or for setting new size or position

 

AddText(id,"Create Box",Vec4(1,1,1,1));

 

simple add text to element with "id" ,text and color

 

SetGuiPosition(id,Vec2(200,50));

 

seting position

 

CreateImage("img","data/ui/splash1.dds",Vec4(1,1,1,1));
CreateBorderRect("rect1",Vec2(10,10),Vec2(780,580),Vec4(1,0,0,0.5));

 

creating static img gui element etc.

 

Every GUI element has a name that is used as the event name.

 

 

void onUpdateGuiEvent(str eventName)
{
if(eventName=="btn1")
{
	if(!mesh)mesh=CreateCube();
	PositionEntity(mesh,Vec3(0,0,0));

}else if(guiElement=="btn2"){

	if(mesh)RotateEntity(mesh,Vec3(0,45,0));
}
else if(guiElement=="btn3"){

	if(mesh)RotateEntity(mesh,Vec3(0,0,0));
}

} 

 

onUpdateGuiEvent() function will be called every time we click something.

 

Function receive eventName which is the name of the Gui element and then do something :)

 

Later on I will upload video

 

I forgot in main loop we have to add updateGui () and renderGui () functions

 

I have plans for adding save load functionality and editor for visual editing and tons of GUI elements :)

 Share

18 Comments


Recommended Comments

You lost me on how you handle events. Making the user poll for events is just not the way to do a GUI.

 

Otherwise it seems ok, although I'm not a fan of just getting ID's for things but if you were going to match how LE does things then that seems valid. But seriously polling for events is so 1990's :D

Link to comment

Otherwise it seems ok, although I'm not a fan of just getting ID's for things but if you were going to match how LE does things then that seems valid. But seriously polling for events is so 1990's :D

You're correct I just wanted to make it simply, this is not the final version. What would you do in my place?

Link to comment

I would use C++, but if you really want to keep it C, I would use function callbacks for the event handlers. Inside CreateX() functions accept a function pointer that will be called when the event is fired.

Link to comment

Actually I did everything in a layers below the surface is typical C ++ design I wanted to keep LE look :D for now

Link to comment

I did this simple gui sometime ago.

I has event handling. The source can be downloaded from that page.

Maybe you could get some help from there.

 

 

Edit: the provided link does not work any more. Sorry about that.

Link to comment

Honestly I really feel like I have a great back-end structure for a GUI, but I'm not a fan of the drawing part. I used a 3x3 image system, but messing around with text positioning and image positioning is just kind of a pain. That's generally where I lose interest. Then you have to worry about text clipping (which seems like we have to do it manually really) and all of that. The basic controls are easy enough, but when you get into more advanced controls it gets to just be a pain.

Link to comment

Honestly I really feel like I have a great back-end structure for a GUI, but I'm not a fan of the drawing part. I used a 3x3 image system, but messing around with text positioning and image positioning is just kind of a pain. That's generally where I lose interest. Then you have to worry about text clipping (which seems like we have to do it manually really) and all of that. The basic controls are easy enough, but when you get into more advanced controls it gets to just be a pain.

 

If you are talking about my little sample, I can agree.

That is aimed to be a real full GUI. Its just some tests I did for my needs.

My meaning was to give some idea of the event handling, not on how to write a GUI

Link to comment

Text is most critical part of the gui . We have to do something about .For most game we don't need some advanced controls . like RPG games did ;)

Link to comment

Yeah... and my little GUI sample has event handling also ;)

 

Roland your "little" GUI sample is really nice piece of code. We can make something really usefull by the way ;)

Link to comment

Roland your "little" GUI sample is really nice piece of code. We can make something really usefull by the way ;)

That one is made very simple as my need was just some buttons and simple things.

 

Most simple games does not need more. Maybe (as you say) the text could be made better.

But for most cases the in-game GUI's are very simple. If we are talking about making a tool

or something like that something more complicated is need of course. In that case I would

have a look at some third-party GUI (like some of the guys have).

 

But I'm open to all suggestions although I'm working with some music recording right now.

 

Cheers

Link to comment

I didn't mean yours Roland, just meant in general, creating a skinable gui library is a pain with all the little positioning of all the little pieces. Coding that sucks, and it's very boring.

Link to comment

I didn't mean yours Roland, just meant in general, creating a skinable gui library is a pain with all the little positioning of all the little pieces. Coding that sucks, and it's very boring.

 

Oh. Yeah. No problem ;)

Link to comment

The 3x3 system is probably the most versatile, and you just have to code that once, then the user can change the images. I prefer a system where none of the sizes are hard-coded, but rather the dimensions of the images dictate the layout.

Link to comment

but rather the dimensions of the images dictate the layout.

 

The most versatile would be to allow the "skinner" to define offsets and such and to not rely on the image size. Artists can get very creative and funky that way with transparency and such. Having it hardcoded to use the image size could end up restricting what they want to do. But yeah, the 3x3 system is the best.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...