1 | using System;
|
---|
2 | using SharpDX;
|
---|
3 | using SharpDX.Toolkit;
|
---|
4 | using SharpDX.Toolkit.Graphics;
|
---|
5 | using SharpDX.Toolkit.Input;
|
---|
6 | using System.Windows.Forms;
|
---|
7 |
|
---|
8 | using ButtonStateDX = SharpDX.Toolkit.Input.ButtonState;
|
---|
9 | using SharpDX.Direct3D11;
|
---|
10 |
|
---|
11 | namespace PackingPlanVisualizations {
|
---|
12 | public class PackingGame : Game {
|
---|
13 |
|
---|
14 |
|
---|
15 | #region Global Private Variables
|
---|
16 | private GraphicsDeviceManager graphicsDeviceManager;
|
---|
17 | private PackingPlan3D control;
|
---|
18 | private Matrix worldMatrix;
|
---|
19 | private Matrix viewMatrix;
|
---|
20 | private Matrix projectionMatrix;
|
---|
21 | private float zoom = 50;
|
---|
22 | private int selectedItemIndex = -1;
|
---|
23 |
|
---|
24 | private BasicEffect basicEffect;
|
---|
25 | private Color backgroundColor { get; set; }
|
---|
26 |
|
---|
27 | private CenteredContainer container;
|
---|
28 | private MouseManager Mouse;
|
---|
29 | #endregion Global Private Variables
|
---|
30 |
|
---|
31 | protected override void Dispose(bool disposeManagedResources) {
|
---|
32 | control.disposedByGame = true;
|
---|
33 | base.Dispose(disposeManagedResources);
|
---|
34 | control.disposedByGame = false;
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | public PackingGame(PackingPlan3D control)
|
---|
39 | : base() {
|
---|
40 | this.control = control;
|
---|
41 | graphicsDeviceManager = new GraphicsDeviceManager(this);
|
---|
42 | graphicsDeviceManager.PreferMultiSampling = true;
|
---|
43 | graphicsDeviceManager.PreparingDeviceSettings +=
|
---|
44 | (object sender, PreparingDeviceSettingsEventArgs e) => {
|
---|
45 | e.GraphicsDeviceInformation.PresentationParameters.MultiSampleCount = MSAALevel.X4;
|
---|
46 | return;
|
---|
47 | };
|
---|
48 | var controlColor = Color.White;//System.Drawing.SystemColors.Control;
|
---|
49 | backgroundColor = new Color (controlColor.R,controlColor.G, controlColor.B, controlColor.A);
|
---|
50 |
|
---|
51 | Mouse = new MouseManager(this);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void SetSize(int width, int height) {
|
---|
55 |
|
---|
56 | graphicsDeviceManager.PreferredBackBufferHeight = height;
|
---|
57 | graphicsDeviceManager.PreferredBackBufferWidth = width;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | public void InitializeContainer(BasicCuboidShape containerShape) {
|
---|
63 | container = new CenteredContainer(containerShape);
|
---|
64 | }
|
---|
65 | public void InitializeContainer(float width, float height, float depth) {
|
---|
66 | InitializeContainer(new BasicCuboidShape(new Vector3(width, height, depth), -1));
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void AddItemToContainer(float width, float height, float depth, float x, float y, float z, int index, int material) {
|
---|
70 | container.AddPackingItem(new Vector3(width, height, depth), new Vector3(x, y, z), index, material);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void SelectItem(int itemIndex) {
|
---|
74 | selectedItemIndex = itemIndex;
|
---|
75 | }
|
---|
76 | public void UnselectItem() {
|
---|
77 | selectedItemIndex = -1;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | protected override void Initialize() {
|
---|
82 | base.Initialize();
|
---|
83 | initializeWorld();
|
---|
84 | previousMouseState = Mouse.GetState();
|
---|
85 | Console.WriteLine("Initial bounds:" + Window.ClientBounds);
|
---|
86 | this.Window.IsMouseVisible = true;
|
---|
87 | //this.Window.ClientSizeChanged += (a, b) => {
|
---|
88 | // Console.WriteLine("Resized..");
|
---|
89 | // this.Tick();
|
---|
90 | //};
|
---|
91 | }
|
---|
92 | private void initializeWorld() {
|
---|
93 | #region Matrix-Initialization
|
---|
94 | worldMatrix = Matrix.Identity;
|
---|
95 |
|
---|
96 | viewMatrix = Matrix.LookAtLH(
|
---|
97 | new Vector3(0, 10, zoom),
|
---|
98 | Vector3.Zero,
|
---|
99 | Vector3.UnitY);
|
---|
100 |
|
---|
101 | projectionMatrix = Matrix.PerspectiveFovLH(
|
---|
102 | (float)Math.PI / 6.0f,
|
---|
103 | (float)GraphicsDevice.BackBuffer.Width /
|
---|
104 | (float)GraphicsDevice.BackBuffer.Height,
|
---|
105 | 1.0f, 1000.0f);
|
---|
106 | #endregion Matrix-Initialization
|
---|
107 |
|
---|
108 | #region Effect and Color
|
---|
109 | //Effect initialization
|
---|
110 | basicEffect = new BasicEffect(GraphicsDevice);
|
---|
111 | basicEffect.World = worldMatrix;
|
---|
112 | basicEffect.View = viewMatrix;
|
---|
113 | basicEffect.Projection = projectionMatrix;
|
---|
114 |
|
---|
115 | //Primitive color
|
---|
116 | basicEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f);
|
---|
117 | basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
|
---|
118 | basicEffect.SpecularColor = new Vector3(0.25f, 0.25f, 0.25f);
|
---|
119 | basicEffect.SpecularPower = 5.0f;
|
---|
120 | basicEffect.Alpha = 1.0f;
|
---|
121 | basicEffect.VertexColorEnabled = true;
|
---|
122 | #endregion Effect-World and Color
|
---|
123 |
|
---|
124 | #region World lighting
|
---|
125 | basicEffect.LightingEnabled = true;
|
---|
126 |
|
---|
127 | basicEffect.PreferPerPixelLighting = true;
|
---|
128 |
|
---|
129 | if (basicEffect.LightingEnabled) {
|
---|
130 | basicEffect.DirectionalLight0.Enabled = true; // enable each light individually
|
---|
131 | if (basicEffect.DirectionalLight0.Enabled) {
|
---|
132 | // x direction
|
---|
133 | basicEffect.DirectionalLight0.DiffuseColor = new Vector3(1, 1, 1); // range is 0 to 1
|
---|
134 | basicEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-1, 0, 0));
|
---|
135 | // points from the light to the origin of the scene
|
---|
136 | basicEffect.DirectionalLight0.SpecularColor = Vector3.One;
|
---|
137 | }
|
---|
138 |
|
---|
139 | basicEffect.DirectionalLight1.Enabled = true;
|
---|
140 | if (basicEffect.DirectionalLight1.Enabled) {
|
---|
141 | // y direction
|
---|
142 | basicEffect.DirectionalLight1.DiffuseColor = new Vector3(1, 1, 1);
|
---|
143 | basicEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(0, -1, 0));
|
---|
144 | basicEffect.DirectionalLight1.SpecularColor = Vector3.One;
|
---|
145 | }
|
---|
146 |
|
---|
147 | basicEffect.DirectionalLight2.Enabled = true;
|
---|
148 | if (basicEffect.DirectionalLight2.Enabled) {
|
---|
149 | // z direction
|
---|
150 | basicEffect.DirectionalLight2.DiffuseColor = new Vector3(1, 1, 1);
|
---|
151 | basicEffect.DirectionalLight2.Direction = Vector3.Normalize(new Vector3(0, 0, -1));
|
---|
152 | basicEffect.DirectionalLight2.SpecularColor = Vector3.One;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | #endregion World lighting
|
---|
156 |
|
---|
157 | #region Transparency
|
---|
158 |
|
---|
159 | //basicEffect.GraphicsDevice.RenderState.AlphaBlendEnable = true;
|
---|
160 | //basicEffect.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
|
---|
161 | //basicEffect.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
|
---|
162 |
|
---|
163 | BlendStateDescription blendStateDescription = new SharpDX.Direct3D11.BlendStateDescription();
|
---|
164 | blendStateDescription.RenderTarget[0].IsBlendEnabled = true;
|
---|
165 | blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
|
---|
166 | blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
|
---|
167 | blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add;
|
---|
168 | blendStateDescription.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
|
---|
169 | blendStateDescription.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
|
---|
170 | blendStateDescription.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
|
---|
171 | blendStateDescription.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
|
---|
172 | SharpDX.Toolkit.Graphics.BlendState blendState = SharpDX.Toolkit.Graphics.BlendState.New(GraphicsDevice, blendStateDescription);
|
---|
173 | GraphicsDevice.SetBlendState(blendState);
|
---|
174 |
|
---|
175 | #endregion
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | protected override void LoadContent() {
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected override void Draw(GameTime gameTime) {
|
---|
184 | GraphicsDevice.Clear(backgroundColor);
|
---|
185 |
|
---|
186 | worldMatrix =
|
---|
187 | Matrix.RotationY(MathUtil.DegreesToRadians(-currentViewAngle.X / 4)) *
|
---|
188 | Matrix.RotationX(MathUtil.DegreesToRadians(currentViewAngle.Y / 4));
|
---|
189 | basicEffect.World = worldMatrix;
|
---|
190 |
|
---|
191 | viewMatrix = Matrix.LookAtLH(
|
---|
192 | new Vector3(0, zoom, zoom),
|
---|
193 | Vector3.Zero,
|
---|
194 | Vector3.UnitY);
|
---|
195 | basicEffect.View = viewMatrix;
|
---|
196 |
|
---|
197 | foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) {
|
---|
198 | pass.Apply();
|
---|
199 |
|
---|
200 | if (container != null) {
|
---|
201 | if (container.PackingItems.Count == 0)
|
---|
202 | container.Container.RenderShapeTrianglesAndLines(GraphicsDevice);
|
---|
203 | else {
|
---|
204 | container.Container.RenderShapeLines(GraphicsDevice, new Color(0, 0, 0));
|
---|
205 | var selectedItem = container.PackingItems.Find(x => x.ShapeID == selectedItemIndex);
|
---|
206 | foreach (BasicCuboidShape item in container.PackingItems) {
|
---|
207 | if (selectedItem == null || selectedItemIndex == item.ShapeID) {
|
---|
208 | item.RenderShapeLines(GraphicsDevice);
|
---|
209 | item.RenderShapeTriangles(GraphicsDevice);
|
---|
210 | } else
|
---|
211 | item.RenderShapeLines(GraphicsDevice, new Color(0,0,0));
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | base.Draw(gameTime);
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 |
|
---|
222 |
|
---|
223 |
|
---|
224 |
|
---|
225 | protected override void Update(GameTime gameTime) {
|
---|
226 | computeMouseHandling();
|
---|
227 | base.Update(gameTime);
|
---|
228 | }
|
---|
229 | #region Mouse-Handling Variables
|
---|
230 | private MouseState previousMouseState;
|
---|
231 | private Vector2 mousePositionOnBtnDown;
|
---|
232 | private Vector2 viewAngleOnBtnRelease;
|
---|
233 | private Vector2 currentViewAngle = new Vector2(0, 0);
|
---|
234 | #endregion Mouse-Handling Variables
|
---|
235 | private void computeMouseHandling() {
|
---|
236 | MouseState mouseState = Mouse.GetState();
|
---|
237 |
|
---|
238 | computeLeftMouseBtnHandling(mouseState);
|
---|
239 | computeMouseWheelHandling(mouseState);
|
---|
240 |
|
---|
241 | previousMouseState = mouseState;
|
---|
242 | }
|
---|
243 | private void computeLeftMouseBtnHandling(MouseState mouseState) {
|
---|
244 | //Left btn released
|
---|
245 | if (mouseState.Left.Equals(ButtonStateDX.Released) && mouseState.Left.Equals(previousMouseState.Left)) {
|
---|
246 | //Console.WriteLine("Released");
|
---|
247 | }
|
---|
248 |
|
---|
249 | //Left btn pressed
|
---|
250 | else if (mouseState.Left.Equals(ButtonStateDX.Pressed) && mouseState.Left.Equals(previousMouseState.Left)) {
|
---|
251 | currentViewAngle = new Vector2(
|
---|
252 | viewAngleOnBtnRelease.X + mouseState.X - mousePositionOnBtnDown.X,
|
---|
253 | viewAngleOnBtnRelease.Y + mouseState.Y - mousePositionOnBtnDown.Y);
|
---|
254 |
|
---|
255 | //angle += 0.5f;
|
---|
256 | //Console.WriteLine("Pressed");
|
---|
257 | //Console.WriteLine(currentViewAngle.ToString());
|
---|
258 | }
|
---|
259 |
|
---|
260 | //Left btn freshly pressed ==> ONLICK-EVENT
|
---|
261 | else if (mouseState.Left.Equals(ButtonStateDX.Pressed) && !previousMouseState.Left.Equals(ButtonStateDX.Pressed)) {
|
---|
262 | Console.WriteLine("OnClick");
|
---|
263 | PackingPlan3D native = (PackingPlan3D)Window.NativeWindow;
|
---|
264 | Console.WriteLine("Window: " + Window.ClientBounds.ToString());
|
---|
265 | Console.WriteLine("native: " + native.ClientSize.ToString());
|
---|
266 | Console.WriteLine("native.parent: " + native.Parent.ClientSize.ToString());
|
---|
267 |
|
---|
268 | Console.WriteLine(native.ParentForm.ToString());
|
---|
269 | Console.WriteLine(Window.Name);
|
---|
270 | Console.WriteLine(this.IsActive);
|
---|
271 | mousePositionOnBtnDown = new Vector2(mouseState.X, mouseState.Y);
|
---|
272 | }
|
---|
273 |
|
---|
274 | //Left btn freshly released ==> ONRELEASE-EVENT
|
---|
275 | else if (mouseState.Left.Equals(ButtonStateDX.Released) && !previousMouseState.Left.Equals(ButtonStateDX.Released)) {
|
---|
276 | Console.WriteLine("OnRelease");
|
---|
277 | viewAngleOnBtnRelease = currentViewAngle;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | private void computeMouseWheelHandling(MouseState mouseState) {
|
---|
281 | int prev = previousMouseState.WheelDelta;
|
---|
282 | int curr = mouseState.WheelDelta;
|
---|
283 | if (curr < prev && zoom > 1) {
|
---|
284 | zoom++;
|
---|
285 | Console.WriteLine(zoom);
|
---|
286 | } else if (curr > prev && zoom < 300) {
|
---|
287 | zoom--;
|
---|
288 | Console.WriteLine(zoom);
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | }
|
---|
293 | }
|
---|