diff --git a/GameEngine/src/main/java/gameEngine/entites/GameObject.java b/GameEngine/src/main/java/gameEngine/entites/GameObject.java index a8f3a7b..6f1f3ff 100644 --- a/GameEngine/src/main/java/gameEngine/entites/GameObject.java +++ b/GameEngine/src/main/java/gameEngine/entites/GameObject.java @@ -1,7 +1,9 @@ package gameEngine.entites; +import gameEngine.entites.gameComponents.EditorButton; import gameEngine.entites.gameComponents.GameComponent; import gameEngine.entites.gameComponents.Mesh; +import gameEngine.entites.gameComponents.TextMesh; import gameEngine.scenes.GameScene; import gameEngine.views.Window; @@ -46,10 +48,16 @@ public void updateComponents(boolean isEditorScene) { for (GameComponent component : gameComponents) { - if (isEditorScene) { + if (isEditorScene) { //Editorシーンでも機能するコンポーネント if (component instanceof Mesh) { component.update(); } + else if (component instanceof TextMesh) { + component.update(); + } + else if(component instanceof EditorButton){ + component.update(); + } } else { component.update(); } diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/Button.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Button.java index 44237df..55b1702 100644 --- a/GameEngine/src/main/java/gameEngine/entites/gameComponents/Button.java +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Button.java @@ -1,13 +1,26 @@ package gameEngine.entites.gameComponents; import gameEngine.entites.Entity; +import gameEngine.entites.GameObject; import gameEngine.input.Input; import gameEngine.input.MouseInput; +import gameEngine.views.Color; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.util.ArrayList; +import java.util.List; public class Button extends GameComponent { private Entity entity; private Mesh mesh; + private List onClickListeners = new ArrayList<>(); + private boolean isPressed = false; + + public Color normalColor = new Color(1f,1f,1f,1f); + public Color pressedColor = new Color(0.5f, 0.5f, 0.5f, 1f); + public Button(Entity entity, Mesh mesh) { this.entity = entity; this.mesh = mesh; @@ -22,6 +35,8 @@ } public void update() { + + GameObject gameObject = (GameObject) entity; float mouseX = MouseInput.getX(); float mouseY = MouseInput.getY(); @@ -30,9 +45,27 @@ float width = mesh.getDisplayedWidth(); float height = mesh.getDisplayedHeight(); - // マウスがボタンの範囲内にあるかどうかをチェック - if (Input.GetMouseButtonUp(0) && mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) { - System.out.println("Button clicked! Mouse X: " + mouseX + ", Mouse Y: " + mouseY); + if (Input.GetMouseButtonDown(0) && mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) { + isPressed = true; // ボタン上でMouseDownした場合のみ押下状態にする + gameObject.getComponent(Mesh.class).setColor(pressedColor); + } + + if (Input.GetMouseButtonUp(0)) { + gameObject.getComponent(Mesh.class).setColor(normalColor); + if (isPressed && mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) { + System.out.println("Button pressed! Mouse X: " + mouseX + ", Mouse Y: " + mouseY); + for (Runnable listener : onClickListeners) { + listener.run(); + System.out.println(listener); + } + } + isPressed = false; + } + + } + + public void addListener(Runnable listener) { + onClickListeners.add(listener); } } diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/EditorButton.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/EditorButton.java new file mode 100644 index 0000000..792ca2e --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/EditorButton.java @@ -0,0 +1,15 @@ +package gameEngine.entites.gameComponents; + +import gameEngine.entites.Entity; + +// +// Editor時に動くButton(通常ButtonはGame実行時のみ動く) +// + +public class EditorButton extends Button{ + + public EditorButton(Entity entity, Mesh mesh) { + super(entity, mesh); + } + +} diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/Mesh.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Mesh.java index bf21be6..59bc2b5 100644 --- a/GameEngine/src/main/java/gameEngine/entites/gameComponents/Mesh.java +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/Mesh.java @@ -2,6 +2,7 @@ import gameEngine.entites.Entity; import gameEngine.entites.GameObject; +import gameEngine.views.Color; import gameEngine.views.Texture; import gameEngine.views.Window; import static org.lwjgl.opengl.GL11.*; @@ -15,6 +16,7 @@ private final MeshType type; private Texture texture; // スプライトのテクスチャを保持 private final Entity parent; // 親のオブジェクトを持つ + private Color color = new Color(1f, 1f, 1f, 1f); private int spriteWidth = 64; // スプライト幅(今後指定可能にする) private int spriteHeight = 64; // スプライト高さ(今後指定可能にする) @@ -57,6 +59,16 @@ renderSprite(); // スプライトの描画 } } + + /** + * メッシュのカラーを変更 + * + */ + public void setColor(Color color){ + this.color = color; + renderSprite(); + } + /** * スプライトの最終的な表示幅を取得 * @return 表示幅(スケールとZスケールを考慮したサイズ) @@ -88,14 +100,6 @@ } } - public int getSpriteWidth() { - return spriteWidth; - } - - public int getSpriteHeight() { - return spriteHeight; - } - private void updateSpriteDimensions() { if (texture != null) { this.spriteWidth = texture.getWidth(); @@ -110,37 +114,28 @@ private void renderSprite() { // 2Dの描画モードに切り替え - glMatrixMode(GL_PROJECTION);// 投影行列の設定モードに切り替え - glPushMatrix(); // 現在の投影行列を保存 - glLoadIdentity(); // 単位行列をロードして投影行列をリセット + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); - // ピクセル単位の座標系に設定 (画面左上が原点、画面サイズはWindowの幅と高さ) - /* - x軸: 左が0、右がWindow.get().width(ウィンドウの幅) - y軸: 上が0、下がWindow.get().height(ウィンドウの高さ) - z軸: -1000から1000の範囲で設定 - */ + // ピクセル単位の座標系に設定 glOrtho(0, Window.get().width, Window.get().height, 0, -1000, 1000); - // モデルビュー行列に切り替えて、描画する位置を決める - glMatrixMode(GL_MODELVIEW); // モデルビュー行列の設定モードに切り替え - glPushMatrix(); // 現在のモデルビュー行列を保存 - glLoadIdentity(); // 単位行列をロードしてモデルビュー行列をリセット + // モデルビュー行列に切り替えて描画する位置を決める + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); - // テクスチャをバインド (テクスチャIDをOpenGLに設定) + // テクスチャをバインド glBindTexture(GL_TEXTURE_2D, texture.getId()); // 親のGameObjectの座標と回転を取得 float x = parent.transform.position.x; float y = parent.transform.position.y; float z = parent.transform.position.z; - float rotationX = parent.transform.rotation.x; - float rotationY = parent.transform.rotation.y; float rotationZ = parent.transform.rotation.z; float scaleX = parent.transform.scale.x; float scaleY = parent.transform.scale.y; - float scaleZ = parent.transform.scale.z; - float zScale; if (z >= 0) { zScale = 1.0f + (z * 0.1f); @@ -148,35 +143,32 @@ zScale = 1.0f / (1.0f + Math.abs(z) * 0.1f); } - // 回転の適用 - glTranslatef(x + spriteWidth / 2.0f, y + spriteHeight / 2.0f, z); // 回転中心をスプライトの中心に - glRotatef(rotationX, 1, 0, 0); // X軸周りの回転 - glRotatef(rotationY, 0, 1, 0); // Y軸周りの回転 + // 左上基準でスケーリング + glTranslatef(x, y, z); + glScalef(scaleX * zScale, scaleY * zScale, 1.0f); // スケールを適用 glRotatef(rotationZ, 0, 0, 1); // Z軸周りの回転 - glScalef(scaleX * zScale, scaleY * zScale, scaleZ); - glTranslatef(-(x + spriteWidth / 2.0f), -(y + spriteHeight / 2.0f), z); // 元の位置に戻す - // テクスチャの描画 + // テクスチャの描画設定 glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glColor4f(1f, 1f, 1f, 1f); + glColor4f(color.r, color.g, color.b, color.a); - // 四角形を描画 (テクスチャ座標と画面座標を対応付け) - glBegin(GL_QUADS); // 四角形を描画する - glTexCoord2f(0, 0); glVertex3f(x, y, z); // 左上の頂点 (テクスチャ座標0,0と画面座標) - glTexCoord2f(1, 0); glVertex3f(x + spriteWidth, y, z); // 右上の頂点 (テクスチャ座標1,0と画面座標) - glTexCoord2f(1, 1); glVertex3f(x + spriteWidth, y + spriteHeight, z); // 右下の頂点 (テクスチャ座標1,1と画面座標) - glTexCoord2f(0, 1); glVertex3f(x, y + spriteHeight, z); // 左下の頂点 (テクスチャ座標0,1と画面座標) - glEnd(); // 四角形の描画を終了 + // 四角形を描画 + glBegin(GL_QUADS); + glTexCoord2f(0, 0); glVertex3f(0, 0, 0); // 左上の頂点 + glTexCoord2f(1, 0); glVertex3f(spriteWidth, 0, 0); // 右上の頂点 + glTexCoord2f(1, 1); glVertex3f(spriteWidth, spriteHeight, 0); // 右下の頂点 + glTexCoord2f(0, 1); glVertex3f(0, spriteHeight, 0); // 左下の頂点 + glEnd(); // テクスチャの使用を終了 - glDisable(GL_TEXTURE_2D); // テクスチャの無効化 - glBindTexture(GL_TEXTURE_2D, 0); // テクスチャのバインドを解除 + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); - // 行列を元に戻す (PushしたものをPopで復元) - glPopMatrix(); // モデルビュー行列を元に戻す - glMatrixMode(GL_PROJECTION); // 投影行列の設定モードに切り替え - glPopMatrix(); // 投影行列を元に戻す + // 行列を元に戻す + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); } } \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java index a12310a..8503ae8 100644 --- a/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java @@ -2,10 +2,9 @@ import gameEngine.entites.Entity; import gameEngine.entites.GameObject; import gameEngine.input.*; +import gameEngine.views.Color; import gameEngine.views.Window; -import java.awt.*; - import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; @@ -42,7 +41,7 @@ if(Input.GetKeyDown(GLFW_KEY_U)){ GameObject gameObject = (GameObject) entity; TextMesh textmesh = gameObject.getComponent(TextMesh.class); - textmesh.setColor(Color.RED); + textmesh.setColor(new Color(1f,0f,1f,1f)); } if (Input.GetKey(GLFW_KEY_W)) { diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/TextMesh.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/TextMesh.java index 0907f40..ae5d244 100644 --- a/GameEngine/src/main/java/gameEngine/entites/gameComponents/TextMesh.java +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/TextMesh.java @@ -15,19 +15,17 @@ private Entity parent; // 親のオブジェクトを持つ public String text; public int textSize; - public java.awt.Color color; + private Color color = new Color(0f,0f,0f,1f); private int spriteWidth = 1; // サイズとは無関係 private int spriteHeight = 1; // サイズとは無関係 - // コンストラクタ - public TextMesh(Entity parent, String text, int textSize, java.awt.Color color) { + public TextMesh(Entity parent, String text, int textSize) { this.parent = parent; this.text = text; this.textSize = textSize; - this.color = color; - Font font = new Font(new java.awt.Font(SANS_SERIF, PLAIN, textSize),true, color); + Font font = new Font(new java.awt.Font(SANS_SERIF, PLAIN, textSize),true); fontTexture = font.getTexture(); fontGlyphs = font.getGlyphs(); fontHeight = font.getFontHeight(); @@ -37,7 +35,6 @@ this.parent = newParent; this.text = original.text; this.textSize = original.textSize; - this.color = original.color; this.fontGlyphs = original.fontGlyphs; this.fontTexture = original.fontTexture; this.fontHeight = original.fontHeight; @@ -72,19 +69,15 @@ if(newTextSize < 3 || newTextSize > 127) return; this.textSize = newTextSize; - Font font = new Font(new java.awt.Font(SANS_SERIF, PLAIN, textSize), true, color); + Font font = new Font(new java.awt.Font(SANS_SERIF, PLAIN, textSize), true); this.fontTexture = font.getTexture(); this.fontGlyphs = font.getGlyphs(); this.fontHeight = font.getFontHeight(); renderText(); } - public void setColor(java.awt.Color newColor){ - this.color = newColor; - Font font = new Font(new java.awt.Font(SANS_SERIF, PLAIN, textSize), true, color); - this.fontTexture = font.getTexture(); - this.fontGlyphs = font.getGlyphs(); - this.fontHeight = font.getFontHeight(); + public void setColor(Color color){ + this.color = color; renderText(); } @@ -127,7 +120,7 @@ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //ここまで文字の背景の透明化 - glColor4f(1f, 1f, 1f, 1f); + glColor4f(color.r, color.g, color.b, color.a); // Text drawing logic float drawX = x; diff --git a/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java b/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java index f615f02..4af24ef 100644 --- a/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java +++ b/GameEngine/src/main/java/gameEngine/scenes/EditorScene.java @@ -10,6 +10,7 @@ public EditorScene(){ System.out.println("Active Editor scene"); glClearColor(1, 1, 1, 0); + addEditor(); } public EditorScene(HashMap entities){ this.entities = entities; diff --git a/GameEngine/src/main/java/gameEngine/scenes/Scene.java b/GameEngine/src/main/java/gameEngine/scenes/Scene.java index d1b002d..620fb67 100644 --- a/GameEngine/src/main/java/gameEngine/scenes/Scene.java +++ b/GameEngine/src/main/java/gameEngine/scenes/Scene.java @@ -10,8 +10,6 @@ import gameEngine.views.Window; import java.awt.event.KeyEvent; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; @@ -52,29 +50,59 @@ public void addNewObject() { enqueueTask(() -> { + addNewGameObject(); + }); + } + + public void addEditor(){ + enqueueTask(() -> { + addEditorFrameObject(); addNewButtonObject(); addNewTextObject(); }); } - private void addNewButtonObject(){ + private GameObject createGameObject(){ int entitiesLength = entities.size(); String newId = Integer.toString(entitiesLength); - GameObject button = new GameObject(newId); - addEntity(newId, button); + GameObject gameObject = new GameObject(newId); + addEntity(newId, gameObject); + return gameObject; + } + + private void addEditorFrameObject(){ + GameObject editorFrame = createGameObject(); + editorFrame.addComponent(new Mesh(editorFrame, Mesh.MeshType.SPRITE, "GameEngine/resources/EditorFrame.png")); + editorFrame.transform.setPosition(0, 0, 0); + editorFrame.transform.setScale(Window.get().width,0.8f,1); + editorFrame.setName("EditorFrame"); + } + + + private void addNewButtonObject(){ + GameObject button = createGameObject(); button.addComponent(new Mesh(button, Mesh.MeshType.SPRITE, "GameEngine/resources/button.png")); - button.addComponent(new Button(button, button.getComponent(Mesh.class))); - button.transform.setScale(1,1,1); - button.setName("NewEntity" + newId); + button.addComponent(new EditorButton(button, button.getComponent(Mesh.class))); + button.transform.setPosition((float) Window.get().width / 2 - button.getComponent(Mesh.class).getDisplayedWidth()/2 , 5f, 0); + button.transform.setScale(1.4f,0.6f,1); + button.setName("NewEntity" + button.getId()); + + button.getComponent(Button.class).addListener(this::changeSceneStart); } private void addNewTextObject(){ - int entitiesLength = entities.size(); - String newId = Integer.toString(entitiesLength); - GameObject text = new GameObject(newId); - addEntity(newId, text); - text.addComponent(new TextMesh(text, "Hello World",32, java.awt.Color.BLACK)); - text.setName("NewEntity" + newId); + GameObject text = createGameObject(); + text.addComponent(new TextMesh(text, "Play",32)); + text.transform.setPosition(580, 3f, 0); + text.setName("NewEntity" + text.getId()); + } + + private void addNewGameObject(){ + GameObject object = createGameObject(); + object.addComponent(new Mesh(object, Mesh.MeshType.SPRITE, "GameEngine/resources/0.png")); + object.transform.setPosition((float) Window.get().width / 2 - object.getComponent(Mesh.class).getDisplayedWidth()/2 , + (float) Window.get().height / 2 - object.getComponent(Mesh.class).getDisplayedHeight()/2, 0); + object.addComponent(new MoveImage(object)); } /** @@ -146,6 +174,9 @@ }); } + private void changeSceneStart(){ + if(!changingScene) changingScene = true; + } //シーン切り替え時の演出 void changeScene(int scene, float dt){ diff --git a/GameEngine/src/main/java/gameEngine/views/Font.java b/GameEngine/src/main/java/gameEngine/views/Font.java index ee5720c..3dac45b 100644 --- a/GameEngine/src/main/java/gameEngine/views/Font.java +++ b/GameEngine/src/main/java/gameEngine/views/Font.java @@ -40,12 +40,12 @@ * @param font The AWT Font * @param antiAlias Wheter the font should be antialiased or not */ - public Font(java.awt.Font font, boolean antiAlias, Color color) { + public Font(java.awt.Font font, boolean antiAlias) { glyphs = new HashMap<>(); - createFontTexture(font, antiAlias, color); + createFontTexture(font, antiAlias); } - private void createFontTexture(java.awt.Font font, boolean antiAlias, Color color) { + private void createFontTexture(java.awt.Font font, boolean antiAlias) { texture = new Texture(); /* Loop through the characters to get charWidth and charHeight */ int imageWidth = 0; @@ -58,7 +58,7 @@ continue; } char c = (char) i; - BufferedImage ch = createCharImage(font, c, antiAlias, color); + BufferedImage ch = createCharImage(font, c, antiAlias); if (ch == null) { /* If char image is null that font does not contain the char */ continue; @@ -84,7 +84,7 @@ continue; } char c = (char) i; - BufferedImage charImage = createCharImage(font, c, antiAlias, color); + BufferedImage charImage = createCharImage(font, c, antiAlias); if (charImage == null) { /* If char image is null that font does not contain the char */ continue; @@ -146,7 +146,7 @@ * @param antiAlias Wheter the char should be antialiased or not * @return Char image */ - private BufferedImage createCharImage(java.awt.Font font, char c, boolean antiAlias, Color color) { + private BufferedImage createCharImage(java.awt.Font font, char c, boolean antiAlias) { /* Creating temporary image to extract character size */ BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); @@ -174,7 +174,7 @@ } g.setFont(font); - g.setPaint(color); //色指定部分 + //g.setPaint(Color.BLACK); g.drawString(String.valueOf(c), 0, metrics.getAscent()); g.dispose(); return image;