Skip to content

Commit 406fd15

Browse files
committed
Status bar: add % as an available unit of measurement
This allows things like mouse coordinates, ruler notches, distances, layer dimensions, and selection sizes to be displayed as a percentage of total image size, which should be very helpful for alignment purposes. (A lot of random tools and UI bits were affected by this change, but all measurement conversions should now be using a central converter that standardizes things like significant digits on a per-unit basis. This greatly improves consistency and cuts a bunch of redundant unit display code.)
1 parent c14f617 commit 406fd15

File tree

7 files changed

+50
-71
lines changed

7 files changed

+50
-71
lines changed

Controls/pdRuler.ctl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ Private Sub UpdateControlLayout(Optional ByVal redrawImmediately As Boolean = Fa
390390
With imgRectCurUnit
391391

392392
Select Case m_RulerUnit
393-
393+
394394
'Pixels are the primary unit, and we can just use coordinates as-is
395395
Case mu_Pixels
396396
.Left = m_imgCoordRectF.Left
@@ -400,10 +400,10 @@ Private Sub UpdateControlLayout(Optional ByVal redrawImmediately As Boolean = Fa
400400

401401
'Other units require conversion
402402
Case Else
403-
.Left = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Left, curImgDPI)
404-
.Top = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Top, curImgDPI)
405-
.Width = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Width, curImgDPI)
406-
.Height = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Height, curImgDPI)
403+
.Left = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Left, curImgDPI, PDImages.GetActiveImage.Width)
404+
.Top = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Top, curImgDPI, PDImages.GetActiveImage.Height)
405+
.Width = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Width, curImgDPI, PDImages.GetActiveImage.Width)
406+
.Height = Units.ConvertPixelToOtherUnit(m_RulerUnit, m_imgCoordRectF.Height, curImgDPI, PDImages.GetActiveImage.Height)
407407

408408
End Select
409409

@@ -797,7 +797,7 @@ Private Function GetConvertedValue(ByVal srcValue As Double) As Long
797797
If (m_RulerUnit = mu_Pixels) Then
798798
GetConvertedValue = Int(srcValue)
799799
Else
800-
If (m_LastDPI <> 0#) Then GetConvertedValue = Int(Units.ConvertOtherUnitToPixels(m_RulerUnit, srcValue, m_LastDPI) + 0.5)
800+
If (m_LastDPI <> 0#) Then GetConvertedValue = Int(Units.ConvertOtherUnitToPixels(m_RulerUnit, srcValue, m_LastDPI, IIf(m_Orientation = pdo_Horizontal, PDImages.GetActiveImage.Width, PDImages.GetActiveImage.Height)) + 0.5)
801801
End If
802802

803803
End Function

Controls/pdStatusBar.ctl

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,7 @@ Public Sub DisplayCanvasCoordinates(ByVal xCoord As Double, ByVal yCoord As Doub
312312
'The position displayed changes based on the current measurement unit (px, in, cm)
313313
Else
314314
If PDImages.IsImageActive() Then
315-
Select Case m_UnitOfMeasurement
316-
Case mu_Pixels
317-
lblCoordinates(0).Caption = "(" & Int(xCoord) & "," & Int(yCoord) & ")"
318-
Case mu_Inches, mu_Centimeters
319-
lblCoordinates(0).Caption = "(" & Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, xCoord, PDImages.GetActiveImage.GetDPI()), "0.0##") & "," & Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, yCoord, PDImages.GetActiveImage.GetDPI()), "0.0##") & ")"
320-
Case mu_Millimeters, mu_Points, mu_Picas
321-
lblCoordinates(0).Caption = "(" & Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, xCoord, PDImages.GetActiveImage.GetDPI()), "0.0#") & "," & Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, yCoord, PDImages.GetActiveImage.GetDPI()), "0.0#") & ")"
322-
End Select
315+
lblCoordinates(0).Caption = "(" & Units.GetValueFormattedForUnit_FromPixel(m_UnitOfMeasurement, xCoord, PDImages.GetActiveImage.GetDPI(), PDImages.GetActiveImage.Width, False) & "," & Units.GetValueFormattedForUnit_FromPixel(m_UnitOfMeasurement, yCoord, PDImages.GetActiveImage.GetDPI(), PDImages.GetActiveImage.Height, False) & ")"
323316
End If
324317
End If
325318

@@ -348,32 +341,23 @@ Public Sub DisplayImageSize(ByRef srcImage As pdImage, Optional ByVal clearSize
348341
'When size IS displayed, we must also refresh the status bar (now that it dynamically aligns its contents)
349342
Else
350343

344+
Const TEXT_BETWEEN_DIMENSIONS As String = " x "
345+
351346
Dim unitWidth As Double, unitHeight As Double
352347
Dim sizeString As String
353348

354-
'Convert pixel measurements to the current unit
355-
If (m_UnitOfMeasurement <> mu_Pixels) Then
349+
'Convert pixel measurements to the current unit, then convert those to a string.
350+
' (Different measurements support different significant digits in the size readout.)
351+
If (m_UnitOfMeasurement = mu_Pixels) Or (m_UnitOfMeasurement = mu_Percent) Then
352+
unitWidth = CStr(srcImage.Width)
353+
unitHeight = CStr(srcImage.Height)
354+
sizeString = Units.GetValueFormattedForUnit_FromPixel(mu_Pixels, unitWidth) & TEXT_BETWEEN_DIMENSIONS & Units.GetValueFormattedForUnit_FromPixel(mu_Pixels, unitHeight)
355+
Else
356356
unitWidth = ConvertPixelToOtherUnit(m_UnitOfMeasurement, srcImage.Width, srcImage.GetDPI(), srcImage.Width)
357357
unitHeight = ConvertPixelToOtherUnit(m_UnitOfMeasurement, srcImage.Height, srcImage.GetDPI(), srcImage.Height)
358+
sizeString = Units.GetValueFormattedForUnit_FromPixel(m_UnitOfMeasurement, unitWidth, srcImage.GetDPI(), srcImage.Width) & TEXT_BETWEEN_DIMENSIONS & Units.GetValueFormattedForUnit_FromPixel(m_UnitOfMeasurement, unitHeight, srcImage.GetDPI(), srcImage.Height)
358359
End If
359360

360-
'Different measurements support different significant digits in the size readout
361-
Select Case m_UnitOfMeasurement
362-
363-
Case mu_Pixels
364-
sizeString = srcImage.Width & " x " & srcImage.Height
365-
366-
Case mu_Inches
367-
sizeString = Format$(unitWidth, "0.0##") & " x " & Format$(unitHeight, "0.0##")
368-
369-
Case mu_Centimeters, mu_Millimeters
370-
sizeString = Format$(unitWidth, "0.0#") & " x " & Format$(unitHeight, "0.0#")
371-
372-
Case mu_Points, mu_Picas
373-
sizeString = Format$(unitWidth, "0.0") & " x " & Format$(unitHeight, "0.0")
374-
375-
End Select
376-
377361
lblImgSize.Caption = sizeString
378362
ReflowStatusBar True
379363

@@ -407,12 +391,12 @@ Public Sub PopulateSizeUnits()
407391
cmbSizeUnit.Clear
408392

409393
Dim i As Long
410-
For i = 1 To Units.GetNumOfAvailableUnits()
411-
cmbSizeUnit.AddItem Units.GetNameOfUnit(i, True), i - 1
394+
For i = 0 To Units.GetNumOfAvailableUnits()
395+
cmbSizeUnit.AddItem Units.GetNameOfUnit(i, True), i
412396
Next i
413397

414398

415-
cmbSizeUnit.ListIndex = 0
399+
cmbSizeUnit.ListIndex = 1
416400
cmbSizeUnit.SetAutomaticRedraws True, True
417401

418402
End Sub
@@ -486,20 +470,9 @@ Public Sub SetSelectionState(ByVal newSelectionState As Boolean)
486470
Dim cString As pdString
487471
Set cString = New pdString
488472

489-
Select Case m_UnitOfMeasurement
490-
Case mu_Pixels
491-
cString.Append Int(selectRect.Width + 0.5)
492-
cString.Append LOWERCASE_X
493-
cString.Append Int(selectRect.Height + 0.5)
494-
Case mu_Inches, mu_Centimeters
495-
cString.Append Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, selectRect.Width, PDImages.GetActiveImage.GetDPI()), "0.0##")
496-
cString.Append LOWERCASE_X
497-
cString.Append Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, selectRect.Height, PDImages.GetActiveImage.GetDPI()), "0.0##")
498-
Case mu_Millimeters, mu_Points, mu_Picas
499-
cString.Append Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, selectRect.Width, PDImages.GetActiveImage.GetDPI()), "0.0#")
500-
cString.Append LOWERCASE_X
501-
cString.Append Format$(Units.ConvertPixelToOtherUnit(m_UnitOfMeasurement, selectRect.Height, PDImages.GetActiveImage.GetDPI()), "0.0#")
502-
End Select
473+
cString.Append Units.GetValueFormattedForUnit_FromPixel(m_UnitOfMeasurement, selectRect.Width, PDImages.GetActiveImage.GetDPI(), PDImages.GetActiveImage.Width, False)
474+
cString.Append LOWERCASE_X
475+
cString.Append Units.GetValueFormattedForUnit_FromPixel(m_UnitOfMeasurement, selectRect.Height, PDImages.GetActiveImage.GetDPI(), PDImages.GetActiveImage.Height, False)
503476

504477
'Also append the selection's aspect ratio, in the form X : 1
505478
If (selectRect.Height <> 0!) Then
@@ -536,10 +509,10 @@ Public Sub SetSelectionState(ByVal newSelectionState As Boolean)
536509
End Sub
537510

538511
Private Sub cmbSizeUnit_Click()
539-
m_UnitOfMeasurement = cmbSizeUnit.ListIndex + 1
512+
m_UnitOfMeasurement = cmbSizeUnit.ListIndex
540513
If PDImages.IsImageActive() Then
541514
Me.DisplayImageSize PDImages.GetActiveImage()
542-
FormMain.MainCanvas(0).NotifyRulerUnitChange cmbSizeUnit.ListIndex + 1
515+
FormMain.MainCanvas(0).NotifyRulerUnitChange cmbSizeUnit.ListIndex
543516
If (g_CurrentTool = ND_MEASURE) Then Tools_Measure.NotifyUnitChange
544517
If (g_CurrentTool = NAV_MOVE) Then Viewport.Stage4_FlipBufferAndDrawUI PDImages.GetActiveImage, FormMain.MainCanvas(0)
545518
End If

Forms/Toolpanel_Measure.frm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ Attribute VB_Exposed = False
324324
'PhotoDemon Measurement Tool Panel
325325
'Copyright 2013-2024 by Tanner Helland
326326
'Created: 11/July/18
327-
'Last updated: 09/November/21
328-
'Last update: convert to new flyout-driven UI
327+
'Last updated: 28/May/24
328+
'Last update: support for percent as a measurement unit
329329
'
330330
'PD's measurement tool is pretty straightforward: measure the distance and angle between two points,
331331
' and relay those values to the user. Can't beat that for simplicity!
@@ -416,7 +416,6 @@ Public Sub UpdateUIText()
416416

417417
Dim newUnit As PD_MeasurementUnit
418418
newUnit = FormMain.MainCanvas(0).GetRulerUnit()
419-
measurementUnitText = Units.GetNameOfUnit(newUnit, True)
420419

421420
'Ensure the display elements are visible
422421
If (Not lblValue(4).Visible) Then
@@ -429,7 +428,7 @@ Public Sub UpdateUIText()
429428
'Repeat the same steps that we used for pixels, but this time, perform an additional conversion
430429
' into the target unit space
431430
If Tools_Measure.GetDistanceInPx(measureValue) Then
432-
lblValue(4).Caption = Format$(Units.ConvertPixelToOtherUnit(newUnit, measureValue, PDImages.GetActiveImage.GetDPI), "0.0##") & " " & measurementUnitText
431+
lblValue(4).Caption = Units.GetValueFormattedForUnit_FromPixel(newUnit, measureValue, PDImages.GetActiveImage.GetDPI, PDImages.GetActiveImage.Width, True)
433432
Else
434433
lblValue(4).Caption = m_NullTextString
435434
End If
@@ -444,10 +443,10 @@ Public Sub UpdateUIText()
444443
End If
445444

446445
'Width
447-
lblValue(6).Caption = Format$(Units.ConvertPixelToOtherUnit(newUnit, Abs(firstPoint.x - secondPoint.x), PDImages.GetActiveImage.GetDPI), "0.0##") & " " & measurementUnitText
446+
lblValue(6).Caption = Units.GetValueFormattedForUnit_FromPixel(newUnit, Abs(firstPoint.x - secondPoint.x), PDImages.GetActiveImage.GetDPI, PDImages.GetActiveImage.Width, True)
448447

449448
'Height
450-
lblValue(7).Caption = Format$(Units.ConvertPixelToOtherUnit(newUnit, Abs(firstPoint.y - secondPoint.y), PDImages.GetActiveImage.GetDPI), "0.0##") & " " & measurementUnitText
449+
lblValue(7).Caption = Units.GetValueFormattedForUnit_FromPixel(newUnit, Abs(firstPoint.y - secondPoint.y), PDImages.GetActiveImage.GetDPI, PDImages.GetActiveImage.Height, True)
451450

452451
'If the current unit is "pixels", hide the extra info area
453452
Else

Modules/Drawing.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ Private Sub DrawLayerDistances_Interior(ByRef dstCanvas As pdCanvas, ByRef srcIm
11131113
PD2D.DrawLineF_FromPtF cSurface, cPen, pt1, pt2
11141114

11151115
'Calculate height text, then draw it
1116-
layerSizeAsString = Units.GetValueFormattedForUnit_FromPixel(FormMain.MainCanvas(0).GetRulerUnit(), srcLayer.GetLayerHeight(True), PDImages.GetActiveImage.GetDPI(), PDImages.GetActiveImage.Width, False)
1116+
layerSizeAsString = Units.GetValueFormattedForUnit_FromPixel(FormMain.MainCanvas(0).GetRulerUnit(), srcLayer.GetLayerHeight(True), PDImages.GetActiveImage.GetDPI(), PDImages.GetActiveImage.Height, False)
11171117
RenderTextWithBackgroundBox layerSizeAsString, cText, ptMid, cSurface, cBrush
11181118

11191119
'Restore original font color, then free the font from the target DC

Modules/MeasureTool.bas

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Attribute VB_Name = "Tools_Measure"
33
'Measure tool interface
44
'Copyright 2018-2024 by Tanner Helland
55
'Created: 11/July/17
6-
'Last updated: 13/July/17
7-
'Last update: wrap up initial build
6+
'Last updated: 28/May/14
7+
'Last update: add support for percent as a measurement unit
88
'
99
'PD's Measure tool is very straightforward. Additional details can be found in the associated form
1010
' (toolpanel_Measure).
@@ -405,8 +405,8 @@ Public Sub Rotate2ndPoint90Degrees()
405405

406406
End Sub
407407

408-
'NOTE: this code is a duplicate of the FormStraighten command string generator. Any changes here need to
409-
' also be mirrored there.
408+
'NOTE: this code is a duplicate of the FormStraighten command string generator.
409+
' Any changes here also need to be mirrored there.
410410
Public Sub StraightenImageToMatch()
411411

412412
Dim curAngle As Double

Modules/Units.bas

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,29 +200,36 @@ Public Function GetValueFormattedForUnit_FromPixel(ByVal curUnit As PD_Measureme
200200
Select Case curUnit
201201

202202
Case mu_Percent
203-
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.00%")
203+
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.0#")
204204

205205
Case mu_Pixels
206206
GetValueFormattedForUnit_FromPixel = CStr(Int(srcPixelValue + 0.5))
207207

208208
Case mu_Inches
209-
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.000")
209+
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.0##")
210210

211211
Case mu_Centimeters
212-
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.00")
212+
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.0#")
213213

214214
Case mu_Millimeters
215-
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.0")
215+
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.0#")
216216

217217
Case mu_Points
218-
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.00")
218+
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.0#")
219219

220220
Case mu_Picas
221-
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.00")
221+
GetValueFormattedForUnit_FromPixel = Format$(srcPixelValue, "0.0#")
222222

223223
End Select
224224

225-
If appendUnitAsText Then GetValueFormattedForUnit_FromPixel = GetValueFormattedForUnit_FromPixel & " " & Units.GetNameOfUnit(curUnit, True)
225+
If appendUnitAsText Then
226+
If (curUnit = mu_Percent) Then
227+
Const PERCENT_SIGN As String = "%"
228+
GetValueFormattedForUnit_FromPixel = GetValueFormattedForUnit_FromPixel & PERCENT_SIGN
229+
Else
230+
GetValueFormattedForUnit_FromPixel = GetValueFormattedForUnit_FromPixel & " " & Units.GetNameOfUnit(curUnit, True)
231+
End If
232+
End If
226233

227234
End Function
228235

PhotoDemon.vbp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ Description="PhotoDemon Photo Editor"
527527
CompatibleMode="0"
528528
MajorVer=2024
529529
MinorVer=4
530-
RevisionVer=19
530+
RevisionVer=21
531531
AutoIncrementVer=1
532532
ServerSupportFiles=0
533533
VersionComments="Copyright 2000-2024 Tanner Helland - photodemon.org"

0 commit comments

Comments
 (0)