github.com/rajveermalviya/gamen@v0.1.2-0.20220930195403-9be15877c1aa/dpi/dpi.go (about)

     1  package dpi
     2  
     3  import "golang.org/x/exp/constraints"
     4  
     5  type Position[T constraints.Integer | constraints.Float] interface {
     6  	implementsPosition()
     7  }
     8  
     9  func (LogicalPosition[T]) implementsPosition()  {}
    10  func (PhysicalPosition[T]) implementsPosition() {}
    11  
    12  type Size[T constraints.Integer | constraints.Float] interface {
    13  	ToLogical(scaleFactor float64) LogicalSize[T]
    14  	ToPhysical(scaleFactor float64) PhysicalSize[T]
    15  	implementsSize()
    16  }
    17  
    18  func (LogicalSize[T]) implementsSize()  {}
    19  func (PhysicalSize[T]) implementsSize() {}
    20  
    21  func CastSize[I, O constraints.Integer | constraints.Float](size Size[I]) Size[O] {
    22  	switch size := size.(type) {
    23  	case PhysicalSize[I]:
    24  		return PhysicalSize[O]{
    25  			Width:  O(size.Width),
    26  			Height: O(size.Height),
    27  		}
    28  
    29  	case LogicalSize[I]:
    30  		return LogicalSize[O]{
    31  			Width:  O(size.Width),
    32  			Height: O(size.Height),
    33  		}
    34  
    35  	default:
    36  		panic("unreachable")
    37  	}
    38  }