-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlignmentHelper.cs
More file actions
482 lines (422 loc) · 20 KB
/
Copy pathAlignmentHelper.cs
File metadata and controls
482 lines (422 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.VisualBasic;
using Application = Microsoft.Office.Interop.PowerPoint.Application;
using Office = Microsoft.Office.Core;
namespace decksterity
{
// Comprehensive alignment, distribution, and spacing utilities for PowerPoint shapes
// Handles both built-in Office alignment and advanced custom spacing algorithms
public static class AlignmentHelper
{
// Gets active PowerPoint application instance via COM interop
private static Application GetPowerPointApplication()
{
try
{
return (Application)Marshal.GetActiveObject("PowerPoint.Application");
}
catch
{
throw new InvalidOperationException("PowerPoint is not running or accessible.");
}
}
// Gets current active selection from PowerPoint window
private static Selection GetActiveSelection()
{
var app = GetPowerPointApplication();
if (app.ActiveWindow?.Selection != null)
{
return app.ActiveWindow.Selection;
}
throw new InvalidOperationException("No active selection in PowerPoint.");
}
// Aligns selected shapes to left edge (multiple shapes) or slide edge (single shape)
public static void AlignLeft()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignLefts, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignLefts, Office.MsoTriState.msoTrue);
}
}
// Centers selected shapes horizontally (multiple shapes) or to slide center (single shape)
public static void AlignCenter()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignCenters, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignCenters, Office.MsoTriState.msoTrue);
}
}
// Aligns selected shapes to right edge (multiple shapes) or slide edge (single shape)
public static void AlignRight()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignRights, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignRights, Office.MsoTriState.msoTrue);
}
}
// Aligns selected shapes to top edge (multiple shapes) or slide edge (single shape)
public static void AlignTop()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignTops, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignTops, Office.MsoTriState.msoTrue);
}
}
// Centers selected shapes vertically (multiple shapes) or to slide center (single shape)
public static void AlignMiddle()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignMiddles, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignMiddles, Office.MsoTriState.msoTrue);
}
}
// Aligns selected shapes to bottom edge (multiple shapes) or slide edge (single shape)
public static void AlignBottom()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignBottoms, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignBottoms, Office.MsoTriState.msoTrue);
}
}
// Distributes 3+ shapes evenly across horizontal space, centers single shape
public static void DistributeHorizontally()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 2)
{
selection.ShapeRange.Distribute(Office.MsoDistributeCmd.msoDistributeHorizontally, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignCenters, Office.MsoTriState.msoTrue);
}
}
// Distributes 3+ shapes evenly across vertical space, centers single shape
public static void DistributeVertically()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 2)
{
selection.ShapeRange.Distribute(Office.MsoDistributeCmd.msoDistributeVertically, Office.MsoTriState.msoFalse);
}
else if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 1)
{
selection.ShapeRange.Align(Office.MsoAlignCmd.msoAlignMiddles, Office.MsoTriState.msoTrue);
}
}
// Makes all selected shapes the same width as the first selected shape
public static void SameWidth()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceWidth = selection.ShapeRange[1].Width;
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Width = referenceWidth;
}
}
}
// Makes all selected shapes the same height as the first selected shape
public static void SameHeight()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceHeight = selection.ShapeRange[1].Height;
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Height = referenceHeight;
}
}
}
// Aligns all shapes to the left edge of the first selected shape
public static void PrimaryAlignLeft()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceLeft = selection.ShapeRange[1].Left;
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Left = referenceLeft;
}
}
}
// Centers all shapes horizontally on the first selected shape
public static void PrimaryAlignCenter()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceCenter = selection.ShapeRange[1].Left + (selection.ShapeRange[1].Width / 2);
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Left = referenceCenter - (selection.ShapeRange[i].Width / 2);
}
}
}
// Aligns all shapes to the right edge of the first selected shape
public static void PrimaryAlignRight()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceRight = selection.ShapeRange[1].Left + selection.ShapeRange[1].Width;
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Left = referenceRight - selection.ShapeRange[i].Width;
}
}
}
// Aligns all shapes to the top edge of the first selected shape
public static void PrimaryAlignTop()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceTop = selection.ShapeRange[1].Top;
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Top = referenceTop;
}
}
}
// Centers all shapes vertically on the first selected shape
public static void PrimaryAlignMiddle()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceMiddle = selection.ShapeRange[1].Top + (selection.ShapeRange[1].Height / 2);
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Top = referenceMiddle - (selection.ShapeRange[i].Height / 2);
}
}
}
// Aligns all shapes to the bottom edge of the first selected shape
public static void PrimaryAlignBottom()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count > 1)
{
var referenceBottom = selection.ShapeRange[1].Top + selection.ShapeRange[1].Height;
for (int i = 2; i <= selection.ShapeRange.Count; i++)
{
selection.ShapeRange[i].Top = referenceBottom - selection.ShapeRange[i].Height;
}
}
}
// Swaps the positions of exactly two selected shapes
public static void ObjectsSwapPositionCentered()
{
var selection = GetActiveSelection();
if (selection.Type == PpSelectionType.ppSelectionShapes && selection.ShapeRange.Count == 2)
{
var shape1 = selection.ShapeRange[1];
var shape2 = selection.ShapeRange[2];
var tempLeft = shape1.Left;
var tempTop = shape1.Top;
shape1.Left = shape2.Left;
shape1.Top = shape2.Top;
shape2.Left = tempLeft;
shape2.Top = tempTop;
}
}
// Advanced resize and spacing with multiple algorithms and user input
// Supports even spacing, preserve first/last, horizontal/vertical modes
public static void ResizeAndSpaceEvenly(string spacingType)
{
var selection = GetActiveSelection();
if (selection.Type != PpSelectionType.ppSelectionShapes) return;
ShapeRange shapeRange;
if (selection.HasChildShapeRange)
{
shapeRange = selection.ChildShapeRange;
}
else
{
shapeRange = selection.ShapeRange;
}
if (shapeRange.Count < 2) return;
// Step 1: Convert ShapeRange to array for sorting
var shapes = new Shape[shapeRange.Count];
for (int i = 0; i < shapeRange.Count; i++)
{
shapes[i] = shapeRange[i + 1];
}
// Step 2: Get user input for spacing value
string userInput = Interaction.InputBox(
"Enter desired spacing between objects (in points):",
"Spacing Input",
"30");
if (string.IsNullOrEmpty(userInput) || !float.TryParse(userInput, out float spaceValue))
{
MessageBox.Show("Invalid input. Please enter a numeric value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
switch (spacingType)
{
case "evenhorizontal":
ProcessEvenHorizontal(shapes, spaceValue);
break;
case "evenhorizontalpreservefirst":
ProcessEvenHorizontalPreserveFirst(shapes, spaceValue);
break;
case "evenhorizontalpreservelast":
ProcessEvenHorizontalPreserveLast(shapes, spaceValue);
break;
case "evenvertical":
ProcessEvenVertical(shapes, spaceValue);
break;
case "evenverticalpreservefirst":
ProcessEvenVerticalPreserveFirst(shapes, spaceValue);
break;
case "evenverticalpreservelast":
ProcessEvenVerticalPreserveLast(shapes, spaceValue);
break;
}
}
// Even horizontal spacing - all shapes get equal size and spacing
private static void ProcessEvenHorizontal(Shape[] shapes, float spaceWidth)
{
SortShapesByLeftPosition(shapes);
float totalWidth = shapes.Last().Left + shapes.Last().Width - shapes.First().Left;
float shapeSize = (totalWidth - (shapes.Length - 1) * spaceWidth) / shapes.Length;
shapes[0].Width = shapeSize;
for (int i = 1; i < shapes.Length; i++)
{
shapes[i].Width = shapeSize;
shapes[i].Left = shapes[0].Left + (shapeSize + spaceWidth) * i;
}
}
// Preserve first shape size, adjust others proportionally
private static void ProcessEvenHorizontalPreserveFirst(Shape[] shapes, float spaceWidth)
{
SortShapesByLeftPosition(shapes);
float totalWidth = shapes.Last().Left + shapes.Last().Width - shapes.First().Left;
float totalShapeWidth = 0;
for (int i = 1; i < shapes.Length; i++)
{
totalShapeWidth += shapes[i].Width;
}
float shapeSizeIncrease = ((totalWidth - (shapes.Length - 1) * spaceWidth) - shapes[0].Width - totalShapeWidth) / (shapes.Length - 1);
for (int i = 1; i < shapes.Length; i++)
{
shapes[i].Width += shapeSizeIncrease;
shapes[i].Left = shapes[i - 1].Left + shapes[i - 1].Width + spaceWidth;
}
}
// Preserve last shape size, adjust others proportionally
private static void ProcessEvenHorizontalPreserveLast(Shape[] shapes, float spaceWidth)
{
SortShapesByLeftPosition(shapes);
float totalWidth = shapes.Last().Left + shapes.Last().Width - shapes.First().Left;
float totalShapeWidth = 0;
for (int i = 0; i < shapes.Length - 1; i++)
{
totalShapeWidth += shapes[i].Width;
}
float shapeSizeIncrease = ((totalWidth - (shapes.Length - 1) * spaceWidth) - shapes.Last().Width - totalShapeWidth) / (shapes.Length - 1);
shapes[0].Width += shapeSizeIncrease;
for (int i = 1; i < shapes.Length - 1; i++)
{
shapes[i].Width += shapeSizeIncrease;
shapes[i].Left = shapes[i - 1].Left + shapes[i - 1].Width + spaceWidth;
}
}
// Even vertical spacing - all shapes get equal size and spacing
private static void ProcessEvenVertical(Shape[] shapes, float spaceHeight)
{
SortShapesByTopPosition(shapes);
float totalHeight = shapes.Last().Top + shapes.Last().Height - shapes.First().Top;
float shapeSize = (totalHeight - (shapes.Length - 1) * spaceHeight) / shapes.Length;
shapes[0].Height = shapeSize;
for (int i = 1; i < shapes.Length; i++)
{
shapes[i].Height = shapeSize;
shapes[i].Top = shapes[0].Top + (shapeSize + spaceHeight) * i;
}
}
// Preserve first shape size vertically, adjust others proportionally
private static void ProcessEvenVerticalPreserveFirst(Shape[] shapes, float spaceHeight)
{
SortShapesByTopPosition(shapes);
float totalHeight = shapes.Last().Top + shapes.Last().Height - shapes.First().Top;
float totalShapeHeight = 0;
for (int i = 1; i < shapes.Length; i++)
{
totalShapeHeight += shapes[i].Height;
}
float shapeSizeIncrease = ((totalHeight - (shapes.Length - 1) * spaceHeight) - shapes[0].Height - totalShapeHeight) / (shapes.Length - 1);
for (int i = 1; i < shapes.Length; i++)
{
shapes[i].Height += shapeSizeIncrease;
shapes[i].Top = shapes[i - 1].Top + shapes[i - 1].Height + spaceHeight;
}
}
// Preserve last shape size vertically, adjust others proportionally
private static void ProcessEvenVerticalPreserveLast(Shape[] shapes, float spaceHeight)
{
SortShapesByTopPosition(shapes);
float totalHeight = shapes.Last().Top + shapes.Last().Height - shapes.First().Top;
float totalShapeHeight = 0;
for (int i = 0; i < shapes.Length - 1; i++)
{
totalShapeHeight += shapes[i].Height;
}
float shapeSizeIncrease = ((totalHeight - (shapes.Length - 1) * spaceHeight) - shapes.Last().Height - totalShapeHeight) / (shapes.Length - 1);
shapes[0].Height += shapeSizeIncrease;
for (int i = 1; i < shapes.Length - 1; i++)
{
shapes[i].Height += shapeSizeIncrease;
shapes[i].Top = shapes[i - 1].Top + shapes[i - 1].Height + spaceHeight;
}
}
// Sorts shape array by left position for horizontal operations
private static void SortShapesByLeftPosition(Shape[] shapes)
{
Array.Sort(shapes, (x, y) => x.Left.CompareTo(y.Left));
}
// Sorts shape array by top position for vertical operations
private static void SortShapesByTopPosition(Shape[] shapes)
{
Array.Sort(shapes, (x, y) => x.Top.CompareTo(y.Top));
}
}
}