Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Canvas-based GUI  (Read 1653 times)
0 Members and 1 Guest are viewing this topic.
Offline novasharp

Junior Devvie

Medals: 1
Projects: 1


« Posted 2013-02-09 02:10:03 »

Hi, I am trying to implement a GUI for my game entirely from scratch (no Swing except for the Canvas). So far I have implemented the rendering of 4 basic elements (Label, button, text field, and a selection element). I can't figure out how to properly handle inputs though. I know I can use keyboard listeners, etc..., but I can't find a good way to do it.

Offline ReBirth
« Reply #1 - Posted 2013-02-09 03:15:11 »

Check if the textfield is holding the focus/selected.

Offline ClickerMonkey

JGO Coder

Medals: 20

Game Engineer

« Reply #2 - Posted 2013-02-09 03:16:57 »

Basically you should have a "container", that container has one focused element.... when you select an element the last focused element is blurred and the new one is focused. you only send key input to the focused element.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2013-02-09 03:21:30 »

Hold reference to focused element. Add listener on container. When receive input, container will pass it to that reference. You may need make an interface to make it easier.

Offline novasharp

Junior Devvie

Medals: 1
Projects: 1


« Reply #4 - Posted 2013-02-09 12:31:47 »

How well would holding a queue of received input events work? Because my game is a bit separated, so I have contexts that are pushed onto the context stack and each context is updated by a call to a method with no arguments. This is the interface each context implements.
public abstract class GameContext {
   public abstract void init();
   public abstract void render(Graphics2D gr);
   public abstract void tick();
   public abstract boolean isTransparent();

Offline novasharp

Junior Devvie

Medals: 1
Projects: 1


« Reply #5 - Posted 2013-02-09 22:06:16 »

For reference, here is a demo of my interface. I got input working based off of a suggestion from reddit. Comments, suggestions?
Now my problem is that I need to determine how to handle button events (and other events). Should I try to emulate the java callback system?
<a href=";hl=en_US&amp;start=" target="_blank">;hl=en_US&amp;start=</a>

Offline Best Username Ever

Junior Devvie

« Reply #6 - Posted 2013-02-09 22:58:20 »

On screen buttons is just a matter of testing to see if a mouse click event occurred inside a rectangle. If buttons can overlap, you need to define a z-order of each button and activate the closest one only. Keyboard input may as well be done exactly how AWT/Swing does it. Make sure you understand the difference between keyPressed/keyReleased events and keyTyped events. They are defined in the same interface, but have very different purposes.

If you use event listeners do not confuse the callback method for the action it is supposed to trigger. Mixing game logic with UI logic creates a mess and means you have to rewrite lots of code if you want to keep the same functionality but map it to a different input or sequence of inputs.

It's absolutely a good idea to split up a basic listener interface with individual "events". [But] a lot of people make the mistake of doing things like creating a public save() method of their component class instead of a protected saveButtonPressed() and a public saveProject() method in another class.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Coldstream24 (16 views)
2015-09-03 00:41:28

Andrew_3ds (26 views)
2015-09-01 19:08:10

afikri (17 views)
2015-08-31 09:30:22

afikri (25 views)
2015-08-31 09:30:07

afikri (13 views)
2015-08-31 09:27:24

afikri (16 views)
2015-08-31 09:26:40

Roquen (30 views)
2015-08-29 11:30:54

GamerC4 (36 views)
2015-08-22 20:38:50

GamerC4 (33 views)
2015-08-22 20:37:18

GamerC4 (40 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 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‑
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!