Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.fhtrier.gdw2.sotf.Interfaces;

public interface IUpdateListener {
void onUpdate(Object value);
}
202 changes: 202 additions & 0 deletions projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Label.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package de.fhtrier.gdw2.sotf.menu;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

import de.fhtrier.gdw2.sotf.Interfaces.IActionListener;

/**
* Text label a menu page
*
* @author Lusito
* @todo file paths need to be adapted when resource loader is ready.
*/
public class Label extends Widget {
/** The font to write the message with */
private Font font;
/** The bounding rect */
public Rectangle rect = new Rectangle(0, 0, 0, 0);
/** Text to display */
public String text = "";
public Color color;
public Image image;
public Align align = Align.LEFT;

public enum Align {
LEFT,
RIGHT,
CENTER
}

private Label() {
}

public void init(GameContainer container) throws SlickException {
if(font == null)
font = new AngelCodeFont("res/fonts/verdana_46.fnt","res/fonts/verdana_46_0.tga");
if(color == null)
color = Color.white;
}

public void render(Graphics g) {
g.setFont(font);
float w = font.getWidth(text);
float h = font.getHeight(text);
if(color != null) {
g.setColor(color);
switch(align) {
case LEFT:
g.drawString(text, rect.getX(), rect.getY());
break;
case RIGHT:
g.drawString(text, rect.getMaxX() - w, rect.getMaxY() - h);
break;
case CENTER:
g.drawString(text, rect.getCenterX() - w/2, rect.getCenterY() - h/2);
break;
}
}
if(image != null) {
image.draw(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
}

@Override
public boolean contains(int x, int y) {
return rect.contains(x, y);
}

/**
* Change the text to display
*
* @param value the new value
* @return this
*/
public Label text(String value) {
text = value;
return this;
}

/**
* Change the position of this label
*
* @param x the offset from left in pixels
* @param y the offset from top in pixels
* @return this
*/
public Label position(float x, float y) {
rect.setLocation(x, y);
return this;
}

/**
* Change the size of this label
*
* @param width in pixels
* @param width in pixels
* @return this
*/
public Label size(float width, float height) {
rect.setSize(width, height);
return this;
}

/**
* Change the font of the text
*
* @param value the new value
* @return this
*/
public Label font(Font value) {
font = value;
return this;
}

/**
* Change the align of the text
*
* @param value the new value
* @return this
*/
public Label align(Align value) {
align = value;
return this;
}

/**
* Change the text color
*
* @param value the new value
* @return this
*/
public Label color(Color value) {
color = value;
return this;
}

/**
* Change the image
*
* @param value the new value
* @return this
*/
public Label image(Image value) {
image = value;
return this;
}

/**
* Clone this label
*
* @return A new Label with the same values as this
*/
public Label clone() {
Label b = Label.create(text, rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());

b.font = font;
b.color = color;

return b;
}


/**
* Create a new label
*
* @param text the text to display
* @param x offset from left in pixels
* @param y offset from top in pixels
* @param width in pixels
* @param height in pixels
* @return the new label
*/
public static Label create(String text, float x, float y, float width, float height) {
Label label = new Label();
label.text(text);
label.position(x,y);
label.size(width, height);
return label;
}

/**
* Shows how to create and init a label
*/
private void demo() {
int x=0,y=0,w=0,h=0;
Label b = Label.create("Hello", x, y, w, h)
.font(font)
.color(Color.gray)
.image(null);
Label c = b.clone()
.text("World")
.position(x,y)
.size(w,h);
}
}
6 changes: 6 additions & 0 deletions projects/SotF/src/de/fhtrier/gdw2/sotf/menu/MenuPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public void mouseMoved(int oldx, int oldy, int newx, int newy) {
w.mouseMoved(oldx, oldy, newx, newy);
}
}

public void mouseDragged(int oldx, int oldy, int newx, int newy) {
for(Widget w: widgets) {
w.mouseDragged(oldx, oldy, newx, newy);
}
}

public void mouseReleased(int button, int x, int y) {
for(Widget w: widgets) {
Expand Down
174 changes: 174 additions & 0 deletions projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Slider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package de.fhtrier.gdw2.sotf.menu;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

import de.fhtrier.gdw2.sotf.Interfaces.IUpdateListener;

/**
* Text & image button for a menu page
*
* @author Lusito
* @todo file paths need to be adapted when resource loader is ready.
*/
public class Slider extends Widget {
/** The bounding rect */
public Rectangle rect = new Rectangle(0, 0, 0, 0);
/** An image for the thumb */
public Image thumbImage;
/** An update listener */
public IUpdateListener listener;
/** The percent value position of the thumb (0.0f-1.0f) */
public float value = 0.0f;

public boolean pressed;

private Slider() {
}

public void init(GameContainer container) throws SlickException {
}

public void render(Graphics g) {
if(thumbImage != null) {
thumbImage.draw(rect.getX() + value*rect.getWidth() - thumbImage.getWidth()/2, rect.getY());
}
g.drawRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}

@Override
public boolean contains(int x, int y) {
return rect.contains(x, y);
}

@Override
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
if(pressed) {
onMove(newx);
}
}

@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
if(pressed) {
onMove(newx);
}
}

@Override
public void mouseReleased(int button, int x, int y) {
if(pressed) {
onMove(x);
pressed = false;
}
}

@Override
public void mousePressed(int button, int x, int y) {
if(contains(x,y)) {
onMove(x);
pressed = true;
}
}

private void onMove(int mouseX) {
// update value
value = Math.max(0.0f, Math.min(1.0f, (mouseX - rect.getX()) / (float)rect.getWidth()));
if(listener != null) {
listener.onUpdate(new Float(value));
}
}

/**
* Change the value (0.0f-1.0f) of the slider
*
* @param value the new value
* @return this
*/
public Slider value(float value) {
this.value = value;
return this;
}

/**
* Change the position of this button
*
* @param x the offset from left in pixels
* @param y the offset from top in pixels
* @return this
*/
public Slider position(float x, float y) {
rect.setLocation(x, y);
return this;
}

/**
* Change the size of this button
*
* @param width in pixels
* @param width in pixels
* @return this
*/
public Slider size(float width, float height) {
rect.setSize(width, height);
return this;
}


/**
* Change the thumb image
*
* @param value the new value
* @return this
*/
public Slider thumbImage(Image value) {
thumbImage = value;
return this;
}

/**
* Change the update listener for when the slider changes
*
* @param value the new value
* @return this
*/
public Slider action(IUpdateListener value) {
listener = value;
return this;
}

/**
* Clone this button
*
* @return A new Slider with the same values as this
*/
public Slider clone() {
Slider b = Slider.create(value, rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());

b.thumbImage = thumbImage;

return b;
}


/**
* Create a new button
*
* @param text the text to display
* @param x offset from left in pixels
* @param y offset from top in pixels
* @param width in pixels
* @param height in pixels
* @return the new button
*/
public static Slider create(float value, float x, float y, float width, float height) {
Slider button = new Slider();
button.value(value);
button.position(x,y);
button.size(width, height);
return button;
}
}
Loading