1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using SharpDX;
|
---|
6 | using SharpDX.DirectWrite;
|
---|
7 |
|
---|
8 | namespace PackingPlanVisualizations {
|
---|
9 | public class CenteredContainer2D {
|
---|
10 | private Vector2 containerSize;
|
---|
11 | private Vector2 controlSize;
|
---|
12 | private float normalizingFactor;
|
---|
13 |
|
---|
14 | public Vector2 ContainerCenter {
|
---|
15 | get { return new Vector2(controlSize.X / 2, controlSize.Y / 2); }
|
---|
16 | }
|
---|
17 |
|
---|
18 | private struct PackingItem {
|
---|
19 | private Vector2 position;
|
---|
20 | private Vector2 size;
|
---|
21 | private string label;
|
---|
22 | public PackingItem(Vector2 size, Vector2 position, string label, CenteredContainer2D parentContainer) {
|
---|
23 | this.size = size;
|
---|
24 | this.position = position;
|
---|
25 | this.label = label;
|
---|
26 | }
|
---|
27 | public RectangleF GetRectangle(Vector2 topLeft, float normalizingFactor) {
|
---|
28 | return new RectangleF(
|
---|
29 | topLeft.X + position.X * normalizingFactor + 2,
|
---|
30 | topLeft.Y + position.Y * normalizingFactor + 2,
|
---|
31 | topLeft.X + position.X * normalizingFactor + size.X * normalizingFactor - 2,
|
---|
32 | topLeft.Y + position.Y * normalizingFactor + size.Y * normalizingFactor - 2
|
---|
33 | );
|
---|
34 | }
|
---|
35 | public string GetLabel() {
|
---|
36 | return label;
|
---|
37 | }
|
---|
38 | }
|
---|
39 | private List<PackingItem> packingItems = new List<PackingItem>();
|
---|
40 |
|
---|
41 | //public struct LabelStructure {
|
---|
42 | // private string text;
|
---|
43 | // public string Text { get { return text; } }
|
---|
44 | // private RectangleF layoutRect;
|
---|
45 | // public RectangleF LayoutRectangle { get { return layoutRect; } }
|
---|
46 | // public LabelStructure(string text, RectangleF layout) {
|
---|
47 | // this.text = text;
|
---|
48 | // this.layoutRect = layout;
|
---|
49 | // }
|
---|
50 | //}
|
---|
51 |
|
---|
52 | public CenteredContainer2D (Vector2 controlSize, Vector2 containerSize) {
|
---|
53 | this.containerSize = containerSize;
|
---|
54 | UpdateContainer(controlSize);
|
---|
55 | }
|
---|
56 | public CenteredContainer2D(float controlWidth, float controlHeight, float containerWidth, float containerHeight)
|
---|
57 | : this(new Vector2 (controlWidth, controlHeight), new Vector2(containerWidth, containerHeight)) {
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void AddItem(Vector2 size, Vector2 position, string label) {
|
---|
61 | packingItems.Add(new PackingItem (size, position, label, this));
|
---|
62 | }
|
---|
63 | public void AddItem(float width, float height, float x, float y, string label) {
|
---|
64 | AddItem(new Vector2 (width, height), new Vector2 (x, y), label);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void UpdateContainer(Vector2 controlSize) {
|
---|
68 | this.controlSize = controlSize;
|
---|
69 | if (controlSize.X / controlSize.Y > containerSize.X / containerSize.Y)
|
---|
70 | this.normalizingFactor = controlSize.Y / (containerSize.Y + 5);
|
---|
71 | else if (controlSize.X / controlSize.Y <= containerSize.X / containerSize.Y)
|
---|
72 | this.normalizingFactor = controlSize.X / (containerSize.X + 5);
|
---|
73 | }
|
---|
74 | public void UpdateContainer(float controlWidth, float controlHeight) {
|
---|
75 | UpdateContainer(new Vector2 (controlWidth, controlHeight));
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | public RectangleF GetContainerData() {
|
---|
80 | //Compute centered rectangle
|
---|
81 | SharpDX.RectangleF result = new SharpDX.RectangleF(
|
---|
82 | ContainerCenter.X - containerSize.X * normalizingFactor / 2 - 2,
|
---|
83 | ContainerCenter.Y - containerSize.Y * normalizingFactor / 2 - 2,
|
---|
84 | ContainerCenter.X + containerSize.X * normalizingFactor / 2 + 2,
|
---|
85 | ContainerCenter.Y + containerSize.Y * normalizingFactor / 2 + 2);
|
---|
86 |
|
---|
87 | return result;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public struct LabeledRectangle {
|
---|
91 | public string label;
|
---|
92 | public RectangleF rectangle;
|
---|
93 |
|
---|
94 | public LabeledRectangle(string label, RectangleF rectangle) {
|
---|
95 | this.label = label;
|
---|
96 | this.rectangle = rectangle;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | public List<LabeledRectangle> GetItemRectangles() {
|
---|
100 | List<LabeledRectangle> result = new List<LabeledRectangle> ();
|
---|
101 |
|
---|
102 | foreach (PackingItem item in packingItems) {
|
---|
103 | result.Add(new LabeledRectangle(item.GetLabel(), item.GetRectangle(new Vector2(
|
---|
104 | ContainerCenter.X - containerSize.X * normalizingFactor / 2,
|
---|
105 | ContainerCenter.Y - containerSize.Y * normalizingFactor / 2), normalizingFactor)));
|
---|
106 | }
|
---|
107 |
|
---|
108 | return result;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|