Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
GuiButtons in Menuscreen and much more!
Browse files Browse the repository at this point in the history
  • Loading branch information
CiroZDP committed Sep 13, 2024
1 parent 23e3be6 commit 1dc6aff
Show file tree
Hide file tree
Showing 35 changed files with 326 additions and 154 deletions.
Binary file removed opcraft/resources/music/calm1.ogg
Binary file not shown.
Binary file removed opcraft/resources/music/calm2.ogg
Binary file not shown.
Binary file removed opcraft/resources/music/calm3.ogg
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file removed opcraft/resources/newmusic/hal1.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/hal2.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/hal3.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/hal4.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/nuance1.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/nuance2.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/piano1.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/piano2.ogg
Binary file not shown.
Binary file removed opcraft/resources/newmusic/piano3.ogg
Binary file not shown.
31 changes: 28 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.op</groupId>
<groupId>net.opencraft</groupId>
<artifactId>OpenCraft</artifactId>
<version>24r11</version>
<version>24r12</version>
<name>OpenCraft</name>
<description>An exact copy of Minecraft.</description>
<description>A rougly calculated clone of Minecraft.</description>

<properties>
<java.version>8</java.version>
Expand Down Expand Up @@ -109,6 +109,31 @@
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>net.opencraft.OpenCraft</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/opencraft/GameSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
*/
public class GameSettings {

public static final String OFFICIAL_WEBPAGE = "https://opencraftstudios.github.io";
public static final String ONLINE_LANGSHEET = "https://raw.githubusercontent.com/OpenCraft-Studios/piston-data/main/langsheet.csv";

public static String DEF_CONFIG;
public static boolean LEGACY_CONFIG = false;

Expand Down
42 changes: 24 additions & 18 deletions src/main/java/net/opencraft/Locales.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package net.opencraft;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.net.URI;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import java.util.*;
import java.util.function.Consumer;

import lombok.Getter;
import net.opencraft.spectoland.SpectoError;
import net.opencraft.util.Files;

public class Locales {
public final class Locales {

private static final Map<String, String> ENGLISH = new HashMap<>();
private static final Map<String, String> GALICIAN = new HashMap<>();
Expand All @@ -22,7 +18,9 @@ public class Locales {
private static final Map<String, String> ITALIAN = new HashMap<>();
private static final Map<String, String> CATALAN = new HashMap<>();
private static final Map<String, String> PORTUGUESE = new HashMap<>();


@Getter
private static final List<Consumer<Locale>> listeners = new ArrayList<>();
private static Locale locale = Locales.getLocale();

public static class Loader {
Expand Down Expand Up @@ -79,7 +77,7 @@ private static Optional<InputStream> getCSVOnline() {
Optional<InputStream> opstream = Optional.empty();

try {
URI csvURI = new URI(GameSettings.ONLINE_LANGSHEET);
URI csvURI = new URI(SharedConstants.ONLINE_LANGSHEET);
InputStream in = csvURI.toURL().openStream();

opstream = Optional.ofNullable(in);
Expand Down Expand Up @@ -116,6 +114,14 @@ public static String getGenericName(Locale locale) {

public static void setLocale(Locale locale) {
Locales.locale = locale;
callListeners();
}

private static void callListeners() {
for (int i = 0; i < listeners.size(); i++) {
Consumer<Locale> listener = listeners.get(i);
listener.accept(locale);
}
}

public static Locale getLocale() {
Expand All @@ -124,22 +130,22 @@ public static Locale getLocale() {

public static String translate(String resource, final Locale locale) {
final String _def = "???";
final String lname = getGenericName(locale);
final String lname = getGenericName(locale).toLowerCase();

resource = resource.trim().toLowerCase();
Map<String, String> translations = ENGLISH;

if (lname.equalsIgnoreCase("Spanish"))
if (lname.equals("spanish"))
translations = SPANISH;
else if (lname.equalsIgnoreCase("Italian"))
else if (lname.equals("italian"))
translations = ITALIAN;
else if (lname.equalsIgnoreCase("French"))
else if (lname.equals("french"))
translations = FRENCH;
else if (lname.equalsIgnoreCase("Galician"))
else if (lname.equals("galician"))
translations = GALICIAN;
else if (lname.equalsIgnoreCase("Catalan"))
else if (lname.equals("catalan"))
translations = CATALAN;
else if (lname.equalsIgnoreCase("Portuguese"))
else if (lname.equals("portuguese"))
translations = PORTUGUESE;

return translations.getOrDefault(resource, _def);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/opencraft/SharedConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

public class SharedConstants {

public static final String VERSION_STRING = "24r11";
public static final String TECHNICAL_NAME = "System Update 3";
public static final String VERSION_STRING = "24r12";
public static final String TECHNICAL_NAME = "HAECATOMBE";
public static final String OFFICIAL_WEBPAGE = "https://github.com/OpenCraft-Studios/";
public static final String ONLINE_LANGSHEET = "https://raw.githubusercontent.com/OpenCraft-Studios/piston-data/main/langsheet.csv";


private SharedConstants() {}

Expand Down
27 changes: 23 additions & 4 deletions src/main/java/net/opencraft/renderer/gui/GuiArrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import org.joml.Vector2f;
import net.opencraft.renderer.gui.interfaces.HighlightCalculator;

public class GuiArrow extends GuiElement implements IState, IUpdateable, IHighlight {
public class GuiArrow extends GuiElement
implements IState, IUpdateable, IHighlight {

public static final int ARROW_LEFT = 0b01,
ARROW_RIGHT = 0b00,
Expand Down Expand Up @@ -43,7 +44,20 @@ public int getState() {
return state;
}

@Override
public void setState(int state) {
if (getState() == state)
return;

highlighted = (state >> 1) == 1;
right = (state & 1) == 1;
this.update();
}

public void setDirection(int direction) {
if (direction == ARROW_RIGHT && right)
return;

this.right = direction == ARROW_RIGHT;
this.update();
}
Expand All @@ -69,13 +83,18 @@ public void update() {
}

@Override
public void mouseEntered(Vector2f pos, Vector2f delta) {
public void mouseEntered(int x, int y, int dx, int dy) {
setHighlighted(true);
}

@Override
public void mouseExited(Vector2f pos, Vector2f delta) {
public void mouseExited(int x, int y, int dx, int dy) {
setHighlighted(false);
}

@Override
public void setHighlighted(HighlightCalculator calc) {
setHighlighted(calc.shouldHighlight(getBounds()));
}

}
88 changes: 79 additions & 9 deletions src/main/java/net/opencraft/renderer/gui/GuiButton.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,61 @@
package net.opencraft.renderer.gui;

import static net.opencraft.Locales.*;
import static net.opencraft.OpenCraft.*;

import java.awt.Graphics2D;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Objects;

public class GuiButton extends GuiElement implements IUpdateable, IState, IHighlight{
import net.opencraft.renderer.gui.interfaces.HighlightCalculator;
import net.opencraft.util.FontRenderer;

public static final int BUTTON_DISABLED = 0,
BUTTON_NORMAL = 1,
public class GuiButton extends GuiElement
implements IUpdateable, IState, IHighlight, IEnabled {

public static final int BUTTON_DISABLED = 0,
BUTTON_NORMAL = 1,
BUTTON_HIGHLIGHTED = 2;

private BufferedImage tex = null;
private boolean highlighted;
private int state = BUTTON_NORMAL;

private FontRenderer font;
private String text;

public GuiButton(String text, FontRenderer font) {
setText(text);
setFont(font);
}

public GuiButton(FontRenderer font) {
this("", font);
}

public void setFont(FontRenderer font) {
this.font = Objects.requireNonNull(font);
}

@Override
public void draw(Graphics2D g2d) {
if (tex == null)
this.update();

// Draw button
g2d.drawImage(tex, x, y, width, height, null);

// Draw text
font.color(isHighlighted() ? 0xFFFFA0 : 0xFFFFFF);
font.size(16);
FontMetrics metrix = font.metrics(g2d);

int textX = (width - metrix.stringWidth(text)) / 2 + x;
int textY = (height - metrix.getHeight()) / 2 + metrix.getAscent() + y;

if (isEnabled())
font.drawShadow(g2d, text, textX, textY);
else
font.draw(g2d, text, textX, textY, 0xA0A0A0);
}

@Override
Expand All @@ -28,18 +65,51 @@ public void update() {

@Override
public int getState() {
return BUTTON_NORMAL;
return state;
}

@Override
public boolean isHighlighted() {
return highlighted;
return state == BUTTON_HIGHLIGHTED;
}

@Override
public void setHighlighted(boolean h) {
this.highlighted = h;
public void setHighlighted(boolean highlighted) {
if (!isEnabled())
return;
if (state == BUTTON_HIGHLIGHTED && highlighted)
return;

setState(highlighted ? BUTTON_HIGHLIGHTED : BUTTON_NORMAL);
}

@Override
public boolean isEnabled() {
return state != BUTTON_DISABLED;
}

@Override
public void setState(int state) {
this.state = state;
this.update();
}

public void setText(String text) {
this.text = Objects.requireNonNull(text);
}

@Override
public void setHighlighted(HighlightCalculator calc) {
setHighlighted(calc.shouldHighlight(getBounds()));
}

@Override
public void setEnabled(boolean enabled) {
setState(enabled ? BUTTON_NORMAL : BUTTON_DISABLED);
}

public void setTranslatedText(String component) {
this.text = translate(component);
}

}
17 changes: 12 additions & 5 deletions src/main/java/net/opencraft/renderer/gui/GuiElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.awt.*;

import org.joml.Vector2f;

import lombok.Getter;
import lombok.Setter;

Expand All @@ -16,13 +14,13 @@ public abstract class GuiElement {

/* Override-able event methods */

public void mouseClicked(Vector2f pos, int button) {
public void mouseClicked(int x, int y, int button) {
}

public void mouseEntered(Vector2f pos, Vector2f delta) {
public void mouseEntered(int x, int y, int dx, int dy) {
}

public void mouseExited(Vector2f pos, Vector2f delta) {
public void mouseExited(int x, int y, int dx, int dy) {
}

/* Setters & Getters */
Expand Down Expand Up @@ -62,4 +60,13 @@ public void scale(float scalar) {
this.height *= scalar;
}

public void setBounds(int x, int y, int width, int height) {
setSize(width, height);
setLocation(x, y);
}

public void setBounds(Rectangle bounds) {
setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
}

}
8 changes: 8 additions & 0 deletions src/main/java/net/opencraft/renderer/gui/IEnabled.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.opencraft.renderer.gui;

public interface IEnabled {

boolean isEnabled();
void setEnabled(boolean enabled);

}
3 changes: 3 additions & 0 deletions src/main/java/net/opencraft/renderer/gui/IHighlight.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.opencraft.renderer.gui;

import net.opencraft.renderer.gui.interfaces.HighlightCalculator;

public interface IHighlight {

boolean isHighlighted();
void setHighlighted(boolean h);
void setHighlighted(HighlightCalculator calc);

}
Loading

0 comments on commit 1dc6aff

Please sign in to comment.