Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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 1213 times)
0 Members and 1 Guest are viewing this topic.
Offline novasharp

Junior Member


Medals: 1
Projects: 1


Gamemaker


« 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.

novasharp
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 Member


Medals: 1
Projects: 1


Gamemaker


« 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.
1  
2  
3  
4  
5  
6  
public abstract class GameContext {
   public abstract void init();
   public abstract void render(Graphics2D gr);
   public abstract void tick();
   public abstract boolean isTransparent();
}

novasharp
Offline novasharp

Junior Member


Medals: 1
Projects: 1


Gamemaker


« 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="http://www.youtube.com/v/qcLP1Ptr4TQ?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/qcLP1Ptr4TQ?version=3&amp;hl=en_US&amp;start=</a>

novasharp
Offline Best Username Ever

Junior Member





« 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.

 

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

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (46 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (36 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (51 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!