Skip to content

Commit d1dd3d7

Browse files
committed
Included the workspace project again.
1 parent e4c2d98 commit d1dd3d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3982
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package workspace;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
7+
import workspace.command.KeyCommandMap;
8+
import workspace.command.ResetPanningCommand;
9+
import workspace.command.ShadeSmoothFlatCommand;
10+
import workspace.command.ShowHideEdgesCommand;
11+
import workspace.command.ShowHideFaceNormalsCommand;
12+
import workspace.command.ShowHideGridCommand;
13+
import workspace.command.ShowHideSideBarCommand;
14+
import workspace.command.ShowHideVertexNormalsCommand;
15+
import workspace.command.ShowHideXAxisCommand;
16+
import workspace.command.ShowHideYAxisCommand;
17+
import workspace.command.ShowHideZAxisCommand;
18+
import workspace.command.WireframeCommand;
19+
import workspace.laf.LookAndFeel;
20+
import workspace.ui.UiComponent;
21+
import workspace.ui.UiEditorMenu;
22+
import workspace.ui.ViewGizmo;
23+
24+
public class Editor implements ModelListener {
25+
26+
protected List<SceneObject> sceneObjects;
27+
28+
protected UiComponent rootUi;
29+
30+
protected KeyCommandMap commands;
31+
32+
protected WorkspaceModel model;
33+
34+
protected ViewGizmo gizmo;
35+
36+
protected WorkspaceSideBarUi sideBar;
37+
38+
protected UiEditorMenu menu;
39+
40+
public Editor() {
41+
setup();
42+
}
43+
44+
private void setup() {
45+
setupLookAndFeel();
46+
initializeModel();
47+
initializeSceneObjects();
48+
initializeRootUi();
49+
createUi();
50+
initializeCommandMap();
51+
registerKeyCommands();
52+
}
53+
54+
@Override
55+
public void onModelChanged() {
56+
rootUi.setVisible(model.isUiVisible());
57+
}
58+
59+
private void initializeSceneObjects() {
60+
sceneObjects = new ArrayList<SceneObject>();
61+
}
62+
63+
private void createUi() {
64+
rootUi.add(getSideBar());
65+
rootUi.add(getGizmo());
66+
rootUi.add(getMenu());
67+
}
68+
69+
private WorkspaceSideBarUi getSideBar() {
70+
if (sideBar == null) {
71+
sideBar = new WorkspaceSideBarUi(model);
72+
}
73+
return sideBar;
74+
}
75+
76+
private ViewGizmo getGizmo() {
77+
if (gizmo == null) {
78+
gizmo = new ViewGizmo();
79+
}
80+
return gizmo;
81+
}
82+
83+
private UiEditorMenu getMenu() {
84+
if (menu == null) {
85+
menu = new UiEditorMenu();
86+
}
87+
return menu;
88+
}
89+
90+
private void initializeRootUi() {
91+
rootUi = new UiComponent();
92+
}
93+
94+
private void setupLookAndFeel() {
95+
LookAndFeel.setup();
96+
}
97+
98+
private void initializeCommandMap() {
99+
commands = new KeyCommandMap();
100+
}
101+
102+
private void initializeModel() {
103+
model = new WorkspaceModel();
104+
}
105+
106+
private void registerKeyCommands() {
107+
commands.register(new ShowHideGridCommand(model));
108+
commands.register(new ShowHideXAxisCommand(model));
109+
commands.register(new ShowHideYAxisCommand(model));
110+
commands.register(new ShowHideZAxisCommand(model));
111+
commands.register(new ShowHideSideBarCommand(model));
112+
commands.register(new ShowHideFaceNormalsCommand(model));
113+
commands.register(new ResetPanningCommand(model));
114+
commands.register(new ShowHideVertexNormalsCommand(model));
115+
commands.register(new ShowHideEdgesCommand(model));
116+
commands.register(new WireframeCommand(model));
117+
commands.register(new ShadeSmoothFlatCommand(model));
118+
}
119+
120+
private void resizeRootUi(int x, int y, int width, int height) {
121+
rootUi.setX(x);
122+
rootUi.setY(y);
123+
rootUi.setWidth(width);
124+
rootUi.setHeight(height);
125+
}
126+
127+
public void resize(int x, int y, int width, int height) {
128+
resizeRootUi(x, y, width, height);
129+
updateGizmo(width, height);
130+
}
131+
132+
public void handleMouseClicked(int x, int y) {
133+
rootUi.onMouseClicked(x, y);
134+
}
135+
136+
public void handleMouseDragged(int x, int y) {
137+
rootUi.onMouseDragged(x, y);
138+
}
139+
140+
public void handleMouseWheel(float amount) {
141+
float scale = model.getScale();
142+
scale -= amount * scale * 0.2f;
143+
model.setScale(scale);
144+
}
145+
146+
private void updateGizmo(int width, int height) {
147+
gizmo.setX(width - 80);
148+
gizmo.setY(130);
149+
}
150+
151+
public void add(UiComponent component) {
152+
rootUi.add(component);
153+
}
154+
155+
public void addSceneObject(SceneObject sceneObject) {
156+
sceneObjects.add(sceneObject);
157+
}
158+
159+
public void addAll(Collection<SceneObject> sceneObjects) {
160+
this.sceneObjects.addAll(sceneObjects);
161+
}
162+
163+
public void clearSceneObjects() {
164+
sceneObjects.clear();
165+
}
166+
167+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package workspace;
2+
3+
import math.Mathf;
4+
import math.Matrix3f;
5+
import math.Matrix4f;
6+
import math.Vector3f;
7+
import processing.core.PApplet;
8+
import processing.core.PMatrix;
9+
import processing.event.KeyEvent;
10+
11+
public class FirstPersonView {
12+
13+
private boolean enabled;
14+
private boolean left;
15+
private boolean right;
16+
private boolean forward;
17+
private boolean back;
18+
private boolean up;
19+
private boolean down;
20+
private float pitch = Mathf.PI;
21+
private float yaw = 0;
22+
private Vector3f eye = new Vector3f(-1000, 0, 1000);
23+
private float speed = 10;
24+
private PApplet context;
25+
26+
public FirstPersonView(PApplet context) {
27+
this.context = context;
28+
context.registerMethod("pre", this);
29+
context.registerMethod("keyEvent", this);
30+
}
31+
32+
public void pre() {
33+
if (!enabled)
34+
return;
35+
yaw = Mathf.map(context.mouseX, 0, context.width, Mathf.PI, -Mathf.PI);
36+
pitch = Mathf.map(context.mouseY, 0, context.height, -Mathf.PI, Mathf.PI);
37+
38+
// if (pitch > 89)
39+
// pitch = 89;
40+
// if (pitch < -89)
41+
// pitch = -89;
42+
43+
Vector3f front = new Vector3f();
44+
float x = Mathf.cos(Mathf.toRadians(yaw)) * Mathf.cos(Mathf.toRadians(pitch));
45+
float y = Mathf.sin(Mathf.toRadians(pitch));
46+
float z = Mathf.cos(Mathf.toRadians(yaw)) * Mathf.cos(Mathf.toRadians(pitch));
47+
front.set(x, y, z);
48+
49+
Vector3f velocity = new Vector3f();
50+
51+
if (left) {
52+
velocity.addLocal(-1, 0, 0);
53+
}
54+
55+
if (right) {
56+
velocity.addLocal(1, 0, 0);
57+
}
58+
59+
if (back) {
60+
velocity.addLocal(0, 0, 1);
61+
}
62+
63+
if (forward) {
64+
velocity.addLocal(0, 0, -1);
65+
}
66+
67+
velocity.multLocal(getRotationMatrix(yaw));
68+
69+
eye.addLocal(velocity.mult(speed));
70+
eye.setY(-300);
71+
}
72+
73+
public void apply() {
74+
Matrix4f m = Matrix4f.fpsViewRH(eye, pitch, yaw).transpose();
75+
PMatrix matrix = context.getMatrix();
76+
matrix.set(m.getValues());
77+
context.setMatrix(matrix);
78+
}
79+
80+
public void keyEvent(KeyEvent key) {
81+
if (key.getAction() == KeyEvent.PRESS)
82+
onKeyPressed(key.getKey());
83+
if (key.getAction() == KeyEvent.RELEASE)
84+
onKeyReleased(key.getKey());
85+
}
86+
87+
public void onKeyPressed(char key) {
88+
if (key == 'w' || key == 'W')
89+
forward = true;
90+
91+
if (key == 's' || key == 'S')
92+
back = true;
93+
94+
if (key == 'a' || key == 'A')
95+
left = true;
96+
97+
if (key == 'd' || key == 'D')
98+
right = true;
99+
100+
if (key == ' ')
101+
up = true;
102+
103+
if (key == 'c' || key == 'C')
104+
down = true;
105+
}
106+
107+
public void onKeyReleased(char key) {
108+
if (key == 'w' || key == 'W')
109+
forward = false;
110+
111+
if (key == 's' || key == 'S')
112+
back = false;
113+
114+
if (key == 'a' || key == 'A')
115+
left = false;
116+
117+
if (key == 'd' || key == 'D')
118+
right = false;
119+
120+
if (key == ' ')
121+
up = false;
122+
123+
if (key == 'c' || key == 'C')
124+
down = false;
125+
}
126+
127+
public Matrix3f getRotationMatrix(float angle) {
128+
Matrix3f m = new Matrix3f(Mathf.cos(angle), 0, Mathf.sin(angle), 0, 1, 0, -Mathf.sin(angle), 0,
129+
Mathf.cos(angle));
130+
return m;
131+
}
132+
133+
public boolean isEnabled() {
134+
return enabled;
135+
}
136+
137+
public void setEnabled(boolean enabled) {
138+
this.enabled = enabled;
139+
}
140+
141+
}

0 commit comments

Comments
 (0)