Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Implementing a decent GUI  (Read 4656 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2012-06-25 05:58:43 »

Hey there!

I have been trying to implement a GUI for my game, but it isn't working out as nice as I had hoped. I took a look at some different
helpers for this, but it seems like TWL (for instance) offers so much more than what I need, and I'm not prepared to use HTML and CCS to layout my GUI.

So, I thought I could implement my own with the goals being
 [1] Easy setup
 [2] Extendable
 [3] Single-threaded, and updated through a simple gui.update(input, delta) call.

However, this is not working out in my favor. I like swing (sue me), so I thought I could do something of the sorts. Swing is really hard to get an overview of though, so that I had to quit.

I wanted to make objects as interactive as the user intends, through interfaces and only send the updates they need. This seems impossible though.
I really don't want to have to update everything (entire gui). I could update just whatever the mouse is hovering over, but I don't know if all that checking would be worth it (a gain in execution speed), and that also eliminates the possibility for animation to take place on a component that does not have focus.

So, JGO, I ask you for advice on this. What is your opinion? 

Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #1 - Posted 2012-06-25 06:03:39 »

I've only been using TWL, and I agree that it's not a perfect solution. It took me weeks/months just getting the basics working. It IS powerful, but also really hard to get into since you pretty much have to look in the source code every time you want to, well, do anything. I wouldn't recommend it, but I found no real alternative to it so it's what I've been using lately. Sadly, all I can do is +1 your question...

Myomyomyo.
Offline 65K
« Reply #2 - Posted 2012-06-25 06:50:21 »

First, it depends whether your game runs with Swing/Java2D or OpenGL. Then, on the kind of widgets you need.
In any case I would definitely discourage writing your own complete UI system. That is very complex fulltime task for several people and you won't get into game programming any longer.
I ended up with a Swing based lobby and an OpenGL game view, where the latter uses some crappy self programmed buttons plus progress bar. I also started using LibGdx buttons for one view, but already lost motivation when I didn't understand the class hierarchy.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #3 - Posted 2012-06-25 06:58:01 »

First, it depends whether your game runs with Swing/Java2D or OpenGL. Then, on the kind of widgets you need.
In any case I would definitely discourage writing your own complete UI system. That is very complex fulltime task for several people and you won't get into game programming any longer.
I ended up with a Swing based lobby and an OpenGL game view, where the latter uses some crappy self programmed buttons plus progress bar. I also started using LibGdx buttons for one view, but already lost motivation when I didn't understand the class hierarchy.


I am using OpenGL. What else would you suggest? It seems that learning any of the bigger systems for GUI is just as complex a task, as writing my own components for the few things I need.

I need only a few windows with labels, and a textbox that has a vertical scrollbar.

Offline 65K
« Reply #4 - Posted 2012-06-25 07:21:06 »

I would first look for a suitable ready-to-use solution. Programmers notoriously underestimate efforts (me included).

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #5 - Posted 2012-06-25 07:27:47 »

I would first look for a suitable ready-to-use solution. Programmers notoriously underestimate efforts (me included).

This is find to be very true, usually. In all other cases, I have been able to find some ready-to-use solution. This one however, I couldn't hence the original problem. The only choice seems to be TWL, Nifty and SUI. Of those, SUI is the only one that doesn't require voodoo to work, but it's so horribly half and incomplete.

Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #6 - Posted 2012-06-25 07:48:13 »

I've got my entire UI boiled down to... two classes. Pretty much. Configured with XML (yuk) but it doesn't have to be. The classes are Area and Screen. What happens inside Areas is kinda up to further code but for doing buttons and panels etc. such as found in Revenge of the Titans - everything - it works.

Cas Smiley

Offline Z-Man
« Reply #7 - Posted 2012-06-25 07:51:42 »

The most common GUI elements you'll need in a game are Buttons and Labels and both of those are fairly easy to do yourself. I've written my own for Java2D just because I wanted custom images and didn't want to deal with a java.awt.Button. Blah blah blah reinventing the weal blah blah blah, it works Tongue
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #8 - Posted 2012-06-25 07:55:25 »

I've got my entire UI boiled down to... two classes. Pretty much. Configured with XML (yuk) but it doesn't have to be. The classes are Area and Screen. What happens inside Areas is kinda up to further code but for doing buttons and panels etc. such as found in Revenge of the Titans - everything - it works.

Cas Smiley

Is that using TWL?

The most common GUI elements you'll need in a game are Buttons and Labels and both of those are fairly easy to do yourself. I've written my own for Java2D just because I wanted custom images and didn't want to deal with a java.awt.Button. Blah blah blah reinventing the weal blah blah blah, it works Tongue

I need some draggable elements too. How did you update your elements?

Offline Z-Man
« Reply #9 - Posted 2012-06-25 08:00:40 »

I need some draggable elements too. How did you update your elements?
I had the screen rendering at a constant framerate then just checked input from the mouse. This is a bit easier in Java2D since you have MouseListener, using OpenGL (LWJGL?) you'd have to get input at a constant rate as well. Draggable wouldn't be to hard (I assume):
1  
2  
3  
// Psuedo-code basic idea
if(mouseDown && mouseMoved)
   button.setPos(button.getX() + moveX, button.getY() + moveY)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2012-06-25 08:35:40 »

No TWL, just literally, two classes: Screen and Area. I extend Screen and override a couple of methods like onClicked and onHovered etc. to deal with things. Screen (and all its Areas) is ticked once per frame, but I use LWJGL input events to actually capture multiple things per tick should they be occurring to quickly.

Cas Smiley

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #11 - Posted 2012-06-25 11:00:02 »

I need some draggable elements too. How did you update your elements?
I had the screen rendering at a constant framerate then just checked input from the mouse. This is a bit easier in Java2D since you have MouseListener, using OpenGL (LWJGL?) you'd have to get input at a constant rate as well. Draggable wouldn't be to hard (I assume):
1  
2  
3  
// Psuedo-code basic idea
if(mouseDown && mouseMoved)
   button.setPos(button.getX() + moveX, button.getY() + moveY)


The thing is: How do I know how much it moved? I only have static input, every frame. It could be handles though, with a few extra variables.

No TWL, just literally, two classes: Screen and Area. I extend Screen and override a couple of methods like onClicked and onHovered etc. to deal with things. Screen (and all its Areas) is ticked once per frame, but I use LWJGL input events to actually capture multiple things per tick should they be occurring to quickly.

Cas Smiley

Do you have children, or do you add everything directly on to the Area/Screen?

Offline davedes
« Reply #12 - Posted 2012-06-25 11:30:25 »

I've made several GUI libs in the past, none of them very complex.

What I've learned:
- If your game just needs buttons and labels, it's easier to do it yourself. A basic text field is also not very difficult.
- If your game needs draggable windows/panels that correctly clip their children, things will start to get a bit more complicated. Still, home-brew might be the way to go.
- If your game requires a proper focusing system, multi-line text editing, theme editor, table widgets, or anything else of that sort, then you are way better off spending the time to learn TWL.

TWL is very powerful but I agree it's not the ideal solution if all you want is a very quick and dirty UI.

Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2012-06-25 12:05:56 »

Do you have children, or do you add everything directly on to the Area/Screen?
No children, just a flat bunch of rectangles. I'll be the first to admit it's not ideal but they even lay themselves out correctly and dynamically resize Smiley It's a fiddly, complicated bit of monolithic code but 90% of the time it's all I need.

The actual behaviour is dealt with in overridden methods in derived Screen classes. I've built a few extra widgets that specialise the behaviour in certain areas - like volume control selectors - but it's painfully simplistic otherwise. It's fairly trivial to do stuff like dragging I suppose.

Anyways, that's how far you can get with two big classes that simply deal in rectangles and clicking.

Cas Smiley

Offline Cero
« Reply #14 - Posted 2012-06-25 12:29:19 »

The thing is: How do I know how much it moved? I only have static input, every frame. It could be handles though, with a few extra variables.

System.out.println(Mouse.getDX() + Mouse.getDY());

I wrote a GUI for our new Map Editor.



Did it similarly to what Cas does.

I have like one button class which has all the basic functionalities and I extend when I need special stuff.
Basically, when you think about it, every interact-able element is a button... even a list item or menu option.

Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #15 - Posted 2012-06-25 21:54:20 »

Pimping my own stuff, but I can't help it. Wink libgdx has a 2D scene graph called scene2d. There is a package of classes called scene2d.ui that are for building UIs. It is quite lightweight for the level of power you get. Some large apps have been built with it, so it is reasonably mature. However, it is currently not well documented. Also, I've been spending the past week refuctoring it (full time instead of working on my projects Sad). I'll be done in a few days (I've been saying this for a few days Sad). At that point it will have nice javadocs, and articles/tutorials will be in the works.

Real high level, the Skin class stores resources (texture regions, colors, button styles, etc) that can be looked up by name. Each widget takes a style from the skin (or created by code). The style is a simple POJO specific to that widget which defines texture regions, etc. Both simple and very complex layouts are done via TableLayout, which (surprise) makes it easy to do table based layouts.

Offline Cero
« Reply #16 - Posted 2012-06-25 22:26:13 »

Pimping my own stuff, but I can't help it. Wink libgdx has a 2D scene graph called scene2d. There is a package of classes called scene2d.ui that are for building UIs. It is quite lightweight for the level of power you get. Some large apps have been built with it, so it is reasonably mature. However, it is currently not well documented. Also, I've been spending the past week refuctoring it (full time instead of working on my projects Sad). I'll be done in a few days (I've been saying this for a few days Sad). At that point it will have nice javadocs, and articles/tutorials will be in the works.

Real high level, the Skin class stores resources (texture regions, colors, button styles, etc) that can be looked up by name. Each widget takes a style from the skin (or created by code). The style is a simple POJO specific to that widget which defines texture regions, etc. Both simple and very complex layouts are done via TableLayout, which (surprise) makes it easy to do table based layouts.

Sometimes I think you plan to address each and every need a java game developer could have and write some great libs to help. It's mind blowing.

Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #17 - Posted 2012-06-25 22:51:14 »

I guess I look at what I would need to build games, and go and do that. When I'm done, I panic and find something else I might need if I were to actually build games. Wink Tool projects are smaller in scope, easy to get excited about and be done. A full game project has lots of bits that aren't fun to build. Sad

Offline Cero
« Reply #18 - Posted 2012-06-25 23:24:35 »

I guess I look at what I would need to build games, and go and do that. When I'm done, I panic and find something else I might need if I were to actually build games. Wink Tool projects are smaller in scope, easy to get excited about and be done. A full game project has lots of bits that aren't fun to build. Sad

Write video playback tools... =DD

Offline R.D.

Senior Devvie


Medals: 2
Projects: 1


"For the last time, Hats ARE Awesome"


« Reply #19 - Posted 2012-06-26 00:03:44 »

Video Playback sucks. This is why I hate playing the old Final Fantasies with their stupid videos. I like to jumped or be able to fast forward the scene (Last Story). Real Time is always the best way (Of course it's a bit work to make the run, but it would make more work for me to do a video which is also super static).

@Topic
TWL for complex guis is the way to got imho. The lack of Wiki (a real one :p) and Tutorials is really sad, since you need to look into the code (as theagentd said). But MatthiasM is quite helpful with all my problems so far (Aaaaand there are many xD) so it works for me.
I even do a lot of my own widgets and override. Something I hated on Swing... a lot.
Offline Cero
« Reply #20 - Posted 2012-06-26 15:36:29 »

Video Playback sucks.

Hats suck. There I said it.

Offline nsigma
« Reply #21 - Posted 2012-06-26 15:47:02 »

I guess I look at what I would need to build games, and go and do that. When I'm done, I panic and find something else I might need if I were to actually build games. Wink Tool projects are smaller in scope, easy to get excited about and be done. A full game project has lots of bits that aren't fun to build. Sad

Write video playback tools... =DD

What exactly are you looking for that hasn't been posted here before, or isn't easily achievable by taking that and adapting it?  That's a serious question, btw.  Having just got GStreamer bundling working in Praxis, I'm happy to help with (though not write!) something that's more easily transferable to other code.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #22 - Posted 2012-06-26 15:48:11 »

Fight! Fight! Fight! Fight! Fight!

Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #23 - Posted 2012-10-17 13:46:26 »

No TWL, just literally, two classes: Screen and Area. I extend Screen and override a couple of methods like onClicked and onHovered etc. to deal with things. Screen (and all its Areas) is ticked once per frame, but I use LWJGL input events to actually capture multiple things per tick should they be occurring to quickly.

Cas Smiley
So are you using the org.lwjgl.opengl.XRandR or com.badlogic.gdx Screen-class? Sorry if it's implied in one of the posts, I just want to be sure.

I'm standing in front of an abyss, and on the other side I can see my new menu-framework dancing. I just did one framework in Java2D, which wasn't too hard and ended up having everything I needed. I know I could just use that as a sort of fullscreen launcher-GUI, which could then start the libgdx-app, but I like the idea of an opengl GUI too. Though it seems very hard and clunky.

I've looked at libgdx and their scene2d stuff, and I'm not too happy about how it's set up. It's like they're trying to copy Swing. My 2D framework only requires one line with 10 parameters set, to have a button with text, textcolors it changes between (if you want that when it's hovered), actions on left-/right-click, font, etc., all of it automatically resizing depending on the resolution. The rest is generalized in a MenuController holding the reins.

If I wanted to do the same type of thing in OpenGL, it seems (at a glance) that I'd have to do some insane juggling of power-of-2-textures which I draw on, drawing my text from a sprite-sheet instead of just using a drawText method (again on a texture?)...or is it a Pixmap?

I liked the fact that I could tell a ui-class how to draw itself when using Java2D. This helped me generalize my MenuController. It seems nigh impossible to have this kind of freedom with any openGL library.

Please tell me I'm just overwhelmed, and that it is not as hard as I'm describing here...I've already done a couple of tutorials on this.

- Jonas
Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #24 - Posted 2012-10-17 14:01:23 »

No, I've got literally two large and complicated classes, Screen, and Area (nothing to do with any other libraries).

Screen amounts to a sprite engine and a collection of Areas. Screens are ticked every frame, which in turn tick all the Areas, and the sprite engine.
Areas are just rectangles which are ticked every frame, and basically just do what they do by looking at the mouse and keyboard input that tick. It's "simple" and does 95% of all game UI requirements, and the rests, I hack on top.

Cas Smiley

Offline sproingie

JGO Kernel


Medals: 202



« Reply #25 - Posted 2012-10-17 16:26:09 »

Sounds a lot like scene2d in gdx, where Screen=Stage and Area=Actor.

As for scene2d being too much like Swing ... in what respect, Charlie?  I mean yes, it deals with events and has parent/child relations with widgets, but that's going to be common to any modern GUI.  Hell, it's more similar to Morphic than it is to Swing.
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #26 - Posted 2012-10-17 16:39:53 »

@Cas: Thanks for the elaboration. Much appreciated. I think I have an idea about what I have to do now.

@sproingie: It's not that I'm mocking scene2d. It just seems to be rather elaborate and much more split up than what I want to work with. I like simplicity. I only made 4 sections in my Java2D GUI: the objects themselves (Actors), a container (basically a Scene), the resourceholder, and the controller. If a command was not known by the scene itself (on/off switches etc.), it was sent to the controller and handled there.

Hmm, I guess I could frankenstein my way into porting my old system to scene2d, by making my own extensions on Scene and Actor.

Thanks for your inputs. I have much fiddling to do before I try this Smiley

- Jonas
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #27 - Posted 2012-10-17 19:02:53 »

I've looked at libgdx and their scene2d stuff, and I'm not too happy about how it's set up. It's like they're trying to copy Swing.
*gasp* *choke* Shocked
scene2d has no MVC, so isn't really like Swing. Each widget is one class, though there is some inheritance. I guess you feel scene2d is daunting at first in a similar way that Swing is? I can assure you it is quite straightforward and simple once you get over the initial hump of "what the hell is all this crap?!". Smiley

Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #28 - Posted 2012-10-17 19:38:35 »

For what it's worth:
Screen.java
Area.java

Cas Smiley


Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #29 - Posted 2012-10-18 01:48:53 »

Thanks, Cas. Very comprehensive and "bendy" Smiley A very good read indeed. Sort of what I had in my Java2D project, only mine was much simpler (less features) and with a lot of subclassing to ease the creative process. You've spurred my confidence that I can make this work.
Thanks

- Jonas
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!