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