Slick Forums

Discuss the Slick 2D Library
It is currently Fri Apr 18, 2014 10:35 am

All times are UTC




Post new topic Reply to topic  [ 33 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Sat Jul 07, 2007 12:05 pm 
Offline

Joined: Sun Feb 11, 2007 9:25 pm
Posts: 92
Okay, while SUI is excellent, my game needs many GUI components which SUI does not provide. Also, SUI is still in heavy development structure-wise, so I began looking for a solution to my GUI problem.

I used to use FengGUI before I ran into Slick. It is a mature GUI system, which a plethora of features. This morning, I decided to make the two work together by writing a little wrapper.

This one calls up the "Everything" demo from the FengGUI package.
If anyone needs help seeing how to fit this into their game, just ask.

I am too tired at the moment to write that.

July 7th, 2007
Version v0.1
Code:
import org.fenggui.event.mouse.MouseButton;
import org.fenggui.example.Everything;
import org.fenggui.render.lwjgl.EventHelper;
import org.fenggui.render.lwjgl.LWJGLBinding;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.InputListener;
import org.newdawn.slick.opengl.SlickCallable;
/**
 * FengWrapper Example.
 *
 * This class demonstrates a way to use FengGUI inside slick.
 * It uses SlickCallable to handle the rendering, and uses part of FengGUI's LWJGL binding, while converting most of it into a simple
 * InputListener.
 *
 * This was coded fairly quickly and may have bugs. If any are found, post them on the forum please
 * @author Dantarion
 *
 */
public class FengWrapper implements InputListener
{
   org.fenggui.Display desk = null;
   GameContainer container;
   Input input;

   public FengWrapper(GameContainer container)
   {
      this.container = container;
      container.getInput().addPrimaryListener(this);
      container.getInput().enableKeyRepeat(500, 30);
      LWJGLBinding binding = new LWJGLBinding();
      desk = new org.fenggui.Display(binding);
      Everything everything = new Everything();
      everything.buildGUI(desk);
   }

   public void render(GameContainer container, Graphics g)
   {
      SlickCallable.enterSafeBlock();
      desk.display();
      SlickCallable.leaveSafeBlock();
   }

   private MouseButton SlickButtonToFeng(int button)
   {
      MouseButton pressed;
      switch (button)
      {
         case (1):
            pressed = MouseButton.RIGHT;
            break;
         case (2):
            pressed = MouseButton.MIDDLE;
            break;
         default:
            pressed = MouseButton.LEFT;
            break;
      }
      return pressed;
   }

   public void controllerButtonPressed(int controller, int button)
   {

   }

   public void controllerButtonReleased(int controller, int button)
   {

   }

   public void controllerDownPressed(int controller)
   {

   }

   public void controllerDownReleased(int controller)
   {

   }

   public void controllerLeftPressed(int controller)
   {

   }

   public void controllerLeftReleased(int controller)
   {

   }

   public void controllerRightPressed(int controller)
   {

   }

   public void controllerRightReleased(int controller)
   {

   }

   public void controllerUpPressed(int controller)
   {

   }

   public void controllerUpReleased(int controller)
   {

   }

   public void inputEnded()
   {

   }

   public boolean isAcceptingInput()
   {
      return true;
   }

   public void keyPressed(int key, char c)
   {
      desk.fireKeyPressedEvent(EventHelper.mapKeyChar(), EventHelper.mapEventKey());
      desk.fireKeyTypedEvent(EventHelper.mapKeyChar());

   }

   public void keyReleased(int key, char c)
   {
      desk.fireKeyReleasedEvent(EventHelper.mapKeyChar(), EventHelper.mapEventKey());

   }

   public void mouseMoved(int oldx, int oldy, int newx, int newy)
   {
      if (!container.getInput().isMouseButtonDown(0))
         desk.fireMouseMovedEvent(newx, container.getHeight() - newy);
      else
         desk.fireMouseDraggedEvent(newx, container.getHeight() - newy, MouseButton.LEFT);
   }

   public void mousePressed(int button, int x, int y)
   {

      desk.fireMousePressedEvent(x, container.getHeight() - y, SlickButtonToFeng(button), 1);

   }

   public void mouseReleased(int button, int x, int y)
   {
      desk.fireMouseReleasedEvent(x, container.getHeight() - y, SlickButtonToFeng(button), 1);
   }

   public void mouseWheelMoved(int change)
   {
      desk.fireMouseWheel(container.getInput().getMouseX(), container.getHeight()
            - container.getInput().getMouseY(), change > 0, 1);

   }

   public void setInput(Input input)
   {
      this.input = input;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 10:27 pm 
Offline

Joined: Tue Jul 24, 2007 8:43 am
Posts: 22
Thanks Dantarion for posting this.

Needed a GUI today and found that your wrapper made it extremely easy to plug one straight into my game.

A note for anyone using ScalableGame, you will need to scale all of the mouse coordinates:

Code:
wScale = (float)container.getWidth() / realWidth;
hScale = (float)container.getHeight() / realHeight;

private int scaleMouseX( int x )
{
    return (int)(x * wScale);
}
   
private int scaleMouseY( int y )
{
    return (int)(y * hScale);
}



Cheers,
Brett


Top
 Profile  
 
 Post subject: Shift and key repeat
PostPosted: Sun Oct 07, 2007 3:17 pm 
Offline

Joined: Thu May 24, 2007 1:08 am
Posts: 12
I'm not sure what's causing this, but.

If I'm typing in a FengGui component, and I press SHIFT with any other key, say 1 to type an exclamation (!) mark. If I release the 1 key, but continue to hold the SHIFT key, the component will continuously have '?' appended to the current text.

Anyone have any ideas about this?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 07, 2007 3:48 pm 
Offline

Joined: Sun Feb 11, 2007 9:25 pm
Posts: 92
That is the one bug that I am really unsure about. I will have to talk with kevglass and some LWJGL people to figure out the problem.


This wrapper right now is more of a hack then anything else, I will be fixing it up as time comes.

I am currently using it in a game, but I haven't gotten to any parts of the game that rely on typing in-game, so this bug hasn't been much of a priority for me :D

Ill see what I can do though.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 02, 2007 12:19 am 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
I'm new to Slick and FengGui ... can someone point me what to do next? Is there a test or example? I've made (copied) wrapper class, just do not know how to use it now.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 02, 2007 11:02 pm 
Offline

Joined: Tue Oct 23, 2007 1:07 am
Posts: 33
This'll run the 'everything' demo:

Code:
package blah;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class FengGuiTest extends BasicGame {
  private FengWrapper feng; //assumed to be in package blah
 
  public FengGuiTest() {
    super("feng gui test");
  }

  public void init(GameContainer container) throws SlickException {
    feng = new FengWrapper(container);
  }

  public void render(GameContainer container, Graphics g) throws SlickException {
    feng.render(container, g);
  }
 
  public void update(GameContainer container, int delta) throws SlickException {

  }
 
  public static void main(String[] args) {
    try {
      AppGameContainer container = new AppGameContainer(new FengGuiTest());
      container.setDisplayMode(640, 480, false);
      container.start();
    } catch (SlickException e) {
      e.printStackTrace();
    }
  } 
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 3:46 pm 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
Thank you.
So I call my GUI through FengWrapper.

Does anyone know how do I consume input event so GUI picks it up but it does not proceed into the game? I have a small gui window, but if I click in it, the click will be picked up by the game also. How to block this, is there a simple way?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 10, 2007 2:46 am 
Offline

Joined: Sun Feb 11, 2007 9:25 pm
Posts: 92
That is a problem I never got around to addressing.
In fact, that is a problem I have never had to bother with in Slick at all.
You would need someway to detect that the GUI had focus, and then set isRecievingInput to false on your InputListener game classes.

I am not sure about the best way to do this, as I am currently not using FengGUI in my current project


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 10, 2007 10:32 am 
Offline

Joined: Tue Jul 24, 2007 8:43 am
Posts: 22
From what I remember of using Feng, it tells you (boolean return value) whether a GUI component was hit by a mouse click etc. All you have to do is check for the hit, if hit, don't do anything with that event in your game code.

Cheers,
Brett


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 10, 2007 11:19 am 
Offline

Joined: Sun Feb 11, 2007 9:25 pm
Posts: 92
I think he means having FengGUI eat the input events so that underlying slick elements don't think something clicked on them.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 11, 2007 1:04 pm 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
There is no way to consume an event,(although there is a Input.consumeEvent() that is just some notifier I don't understand), so I was able to resolve this from FengGUI return value if component has been hit like bjgil2 said...

It's only for mouse press now, I'll add key presses if ever need it, change in FengWrapper:

Code:
   public void mousePressed(int button, int x, int y)
   {
      boolean guiHit = desk.fireMousePressedEvent(x, container.getHeight() - y, SlickButtonToFeng(button), 1);
      if (guiHit)
          container.getInput().clearMousePressedRecord();
   }


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 11, 2007 7:26 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Input.consumeEvent() means the current event won't get sent to any other listeners after the current one.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 12, 2007 1:48 am 
Offline

Joined: Sun Feb 11, 2007 9:25 pm
Posts: 92
So, if someone adds the FengWrapper as the primary input listener, it can eat all events before anything else sees them, correct?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 12, 2007 8:14 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Thats correct. It won't stop the game detecting input based on the direct methods rather than events tho.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 20, 2008 12:41 pm 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1325
Location: Italy
i've put everything together in a zip archive, here:

http://rapidshare.com/files/116263568/SlickFengGuiTest.zip.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 33 posts ]  Go to page 1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group