Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 18, 2013 9:29 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Absolute Layout
PostPosted: Tue Sep 20, 2011 5:09 pm 
Offline

Joined: Tue Sep 20, 2011 5:03 pm
Posts: 3
Is there any examples of creating a Widget, that places its children relative to self or am I just stupid?
I am trying to make a fixed-size widget/layout.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2011 7:19 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
Just subclass Widget and override it's layout() method. In there you can place all children like you want. See the GameUIDemo for details.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject: ok, got it working:
PostPosted: Tue Sep 20, 2011 9:03 pm 
Offline

Joined: Tue Sep 20, 2011 5:03 pm
Posts: 3
Thanks Matthias!
libgdx Vector2 one can replace with other container:
Code:
package com.killallhumans.ui.widgets;

import java.util.HashMap;

import com.badlogic.gdx.math.Vector2;

import de.matthiasmann.twl.Widget;

public class AbsoluteLayout extends Widget {
   private HashMap<Integer, Vector2> offsets = new HashMap<Integer, Vector2>();

   @Override
   protected void layout() {
      super.layout();
      int baseX = getX();
      int baseY = getY();
      int x = 0;
      int y = 0;
      int w = getWidth();
      int h = getHeight();
      //System.out.println("" + baseX + ", " + baseY);
      //System.out.println("w=" + w + ", h=" + h);
      for (int i = 0; i < getNumChildren(); i++) {
         Vector2 offset = offsets.get(i);
         if (offset != null) {
            x = (int) (offset.x) + baseX;
            y = (int) (offset.y) + baseY;
         } else {
            x = 0;
            y = 0;
         }
         getChild(i).setPosition(x, y);
         if (w < x + getChild(i).getWidth()) {
            w = x + getChild(i).getWidth();
         }
         if (h < y + getChild(i).getHeight()) {
            h = y + getChild(i).getHeight();
         }
      }
      setSize(w, h);

      //System.out.println("" + getWidth() + "x" + getHeight());
   }

   public void addAt(Widget child, int x, int y) {
      int index = getNumChildren();
      offsets.put(index, new Vector2(x, y));
      add(child);
   }
}


Top
 Profile  
 
PostPosted: Wed Sep 21, 2011 8:20 pm 
Offline

Joined: Tue Sep 20, 2011 5:03 pm
Posts: 3
Here you can move your widget and specify for children desired width and height, and resize layout itself
Code:

package com.wtf.ui.widgets;

import java.util.HashMap;

import com.badlogic.gdx.math.Vector2;

import de.matthiasmann.twl.Event;
import de.matthiasmann.twl.Widget;

public class RelativeLayout extends Widget implements Editable{
   private static final int treshold = 10;
   private boolean followMouse = false;
   private int mouseX;
   private int mouseY;
   private boolean isEditable = false;
   private boolean isResizeX;
   private boolean isResizeY;

   private HashMap<Integer, Vector2> offsets = new HashMap<Integer, Vector2>();
   private HashMap<Integer, Vector2> sizes = new HashMap<Integer, Vector2>();

   @Override
   protected void layout() {
      super.layout();
      int baseX = getX();
      int baseY = getY();
      int x = 0;
      int y = 0;
      int w = getWidth();
      int h = getHeight();
      Widget child;
      int cw;// child's width
      int ch;// child's height
      // System.out.println("" + baseX + ", " + baseY);
      // System.out.println("w=" + w + ", h=" + h);
      for (int i = 0; i < getNumChildren(); i++) {
         Vector2 offset = offsets.get(i);
         Vector2 size = sizes.get(i);
         if (offset != null) {
            x = (int) (offset.x) + baseX;
            y = (int) (offset.y) + baseY;
         } else {
            x = 0;
            y = 0;
         }
         child = getChild(i);
         child.setPosition(x, y);
         cw = child.getWidth();
         if (size.x > 0) {
            cw = (int) size.x;
         } else if (cw != child.getPreferredWidth()) {
            cw = child.getPreferredWidth();
         }

         ch = child.getHeight();
         if (size.y > 0) {
            ch = (int) size.y;
         } else if (ch != child.getPreferredHeight()) {
            ch = child.getPreferredHeight();
         }
         if (cw != child.getWidth() || ch != child.getHeight()) {
            child.setSize(cw, ch);
            size.x = cw;
            size.y = ch;
            sizes.put(i, size);
         }
         if (w < x + cw) {
            w = x + cw;
         }
         if (h < y + ch) {
            h = y + ch;
         }
      }
      setSize(w, h);

      // System.out.println("" + getWidth() + "x" + getHeight());
   }

   public void setEditable(Boolean editable) {
      this.isEditable = editable;
   }

   @Override
   protected boolean handleEvent(Event evt) {
      if (this.isEditable == false) {
         return super.handleEvent(evt);
      }
      if (evt.getType() == Event.Type.MOUSE_BTNDOWN) {
         this.followMouse = true;
         this.mouseX = evt.getMouseX();
         this.mouseY = evt.getMouseY();
         this.isResizeX = this.mouseX > (getX() + getWidth() - treshold);
         this.isResizeY = this.mouseY > (getY() + getHeight() - treshold);
         // System.out.println("-------- " + (this.mouseX) + " " +
         // (this.mouseY));
         return true;
      } else if (evt.getType() == Event.Type.MOUSE_DRAGGED && this.followMouse) {
         if (this.isResizeX) {
            setSize(Math.max(1, getWidth() + evt.getMouseX() - this.mouseX), getHeight());
         } else if (this.isResizeY) {
            setSize(getWidth(), Math.max(1, getHeight() + evt.getMouseY() - this.mouseY));
         } else {
            setPosition(getX() + evt.getMouseX() - this.mouseX, getY() + evt.getMouseY() - this.mouseY);
         }

         this.mouseX = evt.getMouseX();
         this.mouseY = evt.getMouseY();
         return true;
      }
      if (evt.isMouseDragEnd() && this.followMouse) {
         this.followMouse = false;
         System.out.println("xy wh:" + (getX() - getParent().getX()) + ", " + (getY() - getParent().getY()) + ", " + getWidth() + ", " + getHeight());
      }
      if (evt.isMouseEvent()) {
         return true;
      } else {
         this.followMouse = false;
         return super.handleEvent(evt);
      }
   }
   public void addAt(Widget child, int x, int y) {
      addAt(child, x, y, -1, -1);
   }

   public void addAt(Widget child, int x, int y, int w, int h) {
      int index = getNumChildren();
      offsets.put(index, new Vector2(x, y));
      sizes.put(index, new Vector2(w, h));
      super.add(child);
   }

   @Override
   public void add(Widget child) {
      System.out.println("WARNING: RelativeLayout.add, use addAt instead.");
      addAt(child, 0, 0);
   }
}

and Editable:
Code:
public interface Editable {
   public void  setEditable(Boolean editable);
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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