From 7bf35f21aa3ab6fa9a41f5cc26b1b0d600586e54 Mon Sep 17 00:00:00 2001 From: Lusito Date: Tue, 24 Jul 2012 16:54:20 +0200 Subject: [PATCH] New Widgets, Menupages, etc. --- .../gdw2/sotf/Interfaces/IUpdateListener.java | 5 + .../src/de/fhtrier/gdw2/sotf/menu/Label.java | 202 ++++++++++++++++++ .../de/fhtrier/gdw2/sotf/menu/MenuPage.java | 6 + .../src/de/fhtrier/gdw2/sotf/menu/Slider.java | 174 +++++++++++++++ .../src/de/fhtrier/gdw2/sotf/menu/Widget.java | 3 + .../gdw2/sotf/menu/pages/MenuPageOptions.java | 6 + .../gdw2/sotf/menu/pages/MenuPageRoot.java | 10 +- .../gdw2/sotf/states/MainMenuState.java | 5 + 8 files changed, 406 insertions(+), 5 deletions(-) create mode 100644 projects/SotF/src/de/fhtrier/gdw2/sotf/Interfaces/IUpdateListener.java create mode 100644 projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Label.java create mode 100644 projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Slider.java diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/Interfaces/IUpdateListener.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/Interfaces/IUpdateListener.java new file mode 100644 index 0000000..9b16230 --- /dev/null +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/Interfaces/IUpdateListener.java @@ -0,0 +1,5 @@ +package de.fhtrier.gdw2.sotf.Interfaces; + +public interface IUpdateListener { + void onUpdate(Object value); +} diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Label.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Label.java new file mode 100644 index 0000000..44134f5 --- /dev/null +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Label.java @@ -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); + } +} diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/MenuPage.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/MenuPage.java index e47a540..fe6627e 100644 --- a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/MenuPage.java +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/MenuPage.java @@ -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) { diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Slider.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Slider.java new file mode 100644 index 0000000..53be987 --- /dev/null +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Slider.java @@ -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; + } +} diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Widget.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Widget.java index 6b683f9..cd52ecb 100644 --- a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Widget.java +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/Widget.java @@ -15,6 +15,9 @@ public void keyReleased(int key, char c) { public void mouseMoved(int oldx, int oldy, int newx, int newy) { } + + public void mouseDragged(int oldx, int oldy, int newx, int newy) { + } public void mouseReleased(int button, int x, int y) { } diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageOptions.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageOptions.java index 0ef05c5..96a4130 100644 --- a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageOptions.java +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageOptions.java @@ -3,12 +3,14 @@ import org.newdawn.slick.AngelCodeFont; import org.newdawn.slick.Font; import org.newdawn.slick.GameContainer; +import org.newdawn.slick.Image; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; import de.fhtrier.gdw2.sotf.states.MainMenuState; import de.fhtrier.gdw2.sotf.Interfaces.IActionListener; import de.fhtrier.gdw2.sotf.menu.MenuPage; +import de.fhtrier.gdw2.sotf.menu.Slider; /** * Menu page: Options @@ -31,5 +33,9 @@ public void onAction() { close(); } }); + + Slider s = Slider.create(1.0f, 200, 200, 200, 30).thumbImage(new Image("res/menu/slider_thumb.png")); + + addWidget(s); } } diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageRoot.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageRoot.java index 3ed6dd6..0185767 100644 --- a/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageRoot.java +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/menu/pages/MenuPageRoot.java @@ -47,10 +47,10 @@ public MenuPageRoot(final GameContainer container, final StateBasedGame game, fi addLeftAlignedButton("Credits", x, y + h * 2, font, new MenuPageAction(menuState, credits)); addCenteredButton("Exit", 943, 710, font, - new IActionListener() { - public void onAction() { - System.exit(0); // todo - } - }); + new IActionListener() { + public void onAction() { + System.exit(0); // todo + } + }); } } diff --git a/projects/SotF/src/de/fhtrier/gdw2/sotf/states/MainMenuState.java b/projects/SotF/src/de/fhtrier/gdw2/sotf/states/MainMenuState.java index 9bf7123..c684045 100644 --- a/projects/SotF/src/de/fhtrier/gdw2/sotf/states/MainMenuState.java +++ b/projects/SotF/src/de/fhtrier/gdw2/sotf/states/MainMenuState.java @@ -79,6 +79,11 @@ public void mouseMoved(int oldx, int oldy, int newx, int newy) { if(currentPage != null) currentPage.mouseMoved(oldx, oldy, newx, newy); } + + public void mouseDragged(int oldx, int oldy, int newx, int newy) { + if(currentPage != null) + currentPage.mouseDragged(oldx, oldy, newx, newy); + } public void mouseReleased(int button, int x, int y) { if(currentPage != null)