Last active
August 13, 2024 19:07
-
-
Save EthanRocks3322/376ede63b768bbc0557d695ce0790878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.csus.csc133; | |
import java.util.Observable; | |
import java.util.Observer; | |
import com.codename1.charts.util.ColorUtil; | |
import com.codename1.ui.Container; | |
import com.codename1.ui.Display; | |
import com.codename1.ui.Graphics; | |
import com.codename1.ui.Label; | |
import com.codename1.ui.Transform; | |
import com.codename1.ui.geom.Rectangle; | |
import com.codename1.ui.plaf.Border; | |
public class ViewMap extends Container implements Observer{ | |
private GameModel gm; | |
private GameObject selectedObject; | |
private boolean moveModeActive = false; | |
private float winLeft, winRight, winTop, winBot; | |
private Transform worldToNd, ndToScreen, VTM; | |
public ViewMap(GameModel gm) { | |
this.gm = gm; | |
this.gm.addObserver(this); | |
getAllStyles().setBgTransparency(255); | |
getAllStyles().setBgColor(ColorUtil.WHITE); | |
getAllStyles().setFgColor(ColorUtil.WHITE); | |
getStyle().setBorder(Border.createLineBorder(3,ColorUtil.rgb(255, 0, 0))); | |
//setting style of the container | |
winLeft = winBot = 0; | |
winRight = 1414; | |
winTop = 1356; | |
} | |
public void toggleMoveMode() { | |
if (selectedObject != null) { | |
moveModeActive = true; | |
} else { | |
System.out.println("Error"); | |
} | |
} | |
@Override | |
public void pointerPressed(int x, int y) { | |
super.pointerPressed(x, y); | |
x = x - getParent().getAbsoluteX(); | |
y = y - getParent().getAbsoluteY(); | |
if (moveModeActive && selectedObject != null) { | |
// Move the selected object to the clicked coordinates | |
//selectedObject.setX(x); | |
// selectedObject.setY(y); | |
moveModeActive = false; // Exit move mode after placing the object | |
repaint(); | |
return; // Skip the rest of the method to avoid deselecting | |
} | |
// Proceed with selecting or deselecting objects | |
GameObjectCollection theCollection = gm.getGameObjectCollection(); | |
IIterator iterator = theCollection.getIterator(); | |
boolean anySelected = false; | |
while (iterator.hasNext()) { | |
GameObject o = (GameObject) iterator.getNext(); | |
if (x > (o.getX() - o.getHalfSize()) && x < (o.getX() + o.getHalfSize()) && | |
y > (o.getY() - o.getHalfSize()) && y < (o.getY() + o.getHalfSize())) { | |
anySelected = true; | |
o.setSelected(true); // Select this object | |
selectedObject = o; // Keep track of the currently selected object | |
} else { | |
o.setSelected(false); | |
} | |
} | |
if (!anySelected) { | |
selectedObject = null; // No object is selected if no match was found | |
} | |
repaint(); | |
} | |
public void updateVTM() { | |
float winHeight = winTop - winBot; | |
float winWidth = winRight - winLeft; | |
VTM = Transform.makeIdentity(); | |
Transform W2ND = Transform.makeScale(1/winWidth,1/winHeight ); | |
W2ND.translate(-winLeft, -winBot); | |
Transform ND2D = Transform.makeTranslation(0, this.getHeight()); | |
ND2D.scale(this.getWidth(), -this.getHeight()); | |
VTM.concatenate(ND2D); | |
VTM.concatenate(W2ND); | |
} | |
@Override | |
public void paint(Graphics g) { | |
super.paint(g); | |
//int absX = getParent().getAbsoluteX(); | |
//int absY = getParent().getAbsoluteY(); | |
//System.out.println("parent og: X=" + absX + ", Y=" + absY); | |
//absX = 0, absY = 117 | |
Transform gXform = Transform.makeIdentity(); | |
updateVTM(); | |
gXform.concatenate(VTM); | |
g.setTransform(gXform); | |
gXform.translate( getX(), getY() ); | |
g.setTransform(gXform); | |
GameObjectCollection theCollection = gm.getGameObjectCollection(); | |
IIterator iterator1 = theCollection.getIterator(); | |
while (iterator1.hasNext()) { | |
GameObject o = (GameObject) iterator1.getNext(); | |
g.setTransform(VTM); | |
o.draw(g,getParent().getAbsoluteX() ,getParent().getAbsoluteY()); | |
if(o.isSelected()) { | |
Rectangle bBox = o.getBoundBox(); | |
g.setColor(ColorUtil.rgb(255, 0, 0)); | |
g.drawRect(bBox.getX(), bBox.getY(), o.getSize(), o.getSize()); | |
} | |
//System.out.println("im drawing " + o.getClass().getSimpleName() + o); | |
} | |
g.resetAffine(); | |
//System.out.println("im done"); | |
} | |
@Override | |
public void update(Observable o, Object arg) { | |
if (o instanceof GameModel) { | |
//((GameModel) o).display(); | |
//System.out.println("repaintingggggg"); | |
repaint(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment