Remove Coord typedef, rename related functions

This commit is contained in:
Francisco Vallarino 2020-10-08 11:54:36 -03:00
parent c97a58bda8
commit 73c58bc015
10 changed files with 84 additions and 86 deletions

View File

@ -15,10 +15,10 @@ An easy to use, Elm inspired, GUI library for native applications.
- Be able to run in mobile
- Support Vulkan/Metal
### Limitations
### Current limitations
- Multi-platform features depend on what SDL already provides
- Text input capabilities are basic, supporting only left to right at the moment
- Text input capabilities are basic, supporting only left to right editing at the moment
### These are not objectives for this project

View File

@ -7,31 +7,29 @@ import qualified Data.Sequence as Seq
type PathStep = Int
type Path = Seq PathStep
type Coord = Double
type Factor = Double
data Point = Point {
_pX :: !Coord,
_pY :: !Coord
_pX :: !Double,
_pY :: !Double
} deriving (Show, Eq)
instance Default Point where
def = Point 0 0
data Size = Size {
_sW :: !Coord,
_sH :: !Coord
_sW :: !Double,
_sH :: !Double
} deriving (Show, Eq)
instance Default Size where
def = Size 0 0
data Rect = Rect {
_rX :: !Coord,
_rY :: !Coord,
_rW :: !Coord,
_rH :: !Coord
_rX :: !Double,
_rY :: !Double,
_rW :: !Double,
_rH :: !Double
} deriving (Show, Eq)
instance Default Rect where
@ -46,18 +44,18 @@ pointInRect (Point px py) rect = coordInRectH px rect && coordInRectY py rect
addPoint :: Point -> Point -> Point
addPoint (Point x1 y1) (Point x2 y2) = Point (x1 + x2) (y1 + y2)
coordInRectH :: Coord -> Rect -> Bool
coordInRectH :: Double -> Rect -> Bool
coordInRectH px (Rect x y w h) = px >= x && px < x + w
coordInRectY :: Coord -> Rect -> Bool
coordInRectY :: Double -> Rect -> Bool
coordInRectY py (Rect x y w h) = py >= y && py < y + h
addToSize :: Size -> Coord -> Coord -> Size
addToSize :: Size -> Double -> Double -> Size
addToSize (Size w h) w2 h2 = Size nw nh where
nw = max 0 $ w + w2
nh = max 0 $ h + h2
subtractFromSize :: Size -> Coord -> Coord -> Size
subtractFromSize :: Size -> Double -> Double -> Size
subtractFromSize (Size w h) w2 h2 = Size nw nh where
nw = max 0 $ w - w2
nh = max 0 $ h - h2
@ -73,14 +71,14 @@ rectInRectV :: Rect -> Rect -> Bool
rectInRectV (Rect x1 y1 w1 h1) (Rect x2 y2 w2 h2) =
y1 >= y2 && y1 + h1 <= y2 + h2
addToRect :: Rect -> Coord -> Coord -> Coord -> Coord -> Rect
addToRect :: Rect -> Double -> Double -> Double -> Double -> Rect
addToRect (Rect x y w h) l r t b = Rect nx ny nw nh where
nx = x - l
ny = y - t
nw = max 0 $ w + l + r
nh = max 0 $ h + t + b
subtractFromRect :: Rect -> Coord -> Coord -> Coord -> Coord -> Rect
subtractFromRect :: Rect -> Double -> Double -> Double -> Double -> Rect
subtractFromRect (Rect x y w h) l r t b = Rect nx ny nw nh where
nx = x + l
ny = y + t

View File

@ -24,22 +24,22 @@ instance FlexHeight StyleState where
flexHeight h = def & L.sizeReqH ?~ FlexSize h 1
instance BoundedWidth StyleState where
boundedWidth w1 w2 = def & L.sizeReqW ?~ BoundedSize w1 w2 1
boundedWidth w1 w2 = def & L.sizeReqW ?~ RangeSize w1 w2 1
instance BoundedHeight StyleState where
boundedHeight h1 h2 = def & L.sizeReqH ?~ BoundedSize h1 h2 1
boundedHeight h1 h2 = def & L.sizeReqH ?~ RangeSize h1 h2 1
instance MinWidth StyleState where
minWidth w = def & L.sizeReqW ?~ BoundedSize w (2 * w) 1
minWidth w = def & L.sizeReqW ?~ RangeSize w (2 * w) 1
instance MinHeight StyleState where
minHeight h = def & L.sizeReqH ?~ BoundedSize h (2 * h) 1
minHeight h = def & L.sizeReqH ?~ RangeSize h (2 * h) 1
instance MaxWidth StyleState where
maxWidth w = def & L.sizeReqW ?~ BoundedSize 0 w 1
maxWidth w = def & L.sizeReqW ?~ RangeSize 0 w 1
instance MaxHeight StyleState where
maxHeight h = def & L.sizeReqH ?~ BoundedSize 0 h 1
maxHeight h = def & L.sizeReqH ?~ RangeSize 0 h 1
instance BgColor StyleState where
bgColor col = def & L.bgColor ?~ col

View File

@ -50,9 +50,9 @@ instance Monoid ThemeState where
-- | Basic styling attributes
data SizeReq
= FixedSize Coord
| FlexSize Coord Factor
| BoundedSize Coord Coord Factor
= FixedSize Double
| FlexSize Double Factor
| RangeSize Double Double Factor
deriving (Eq, Show)
instance Default SizeReq where

View File

@ -130,8 +130,8 @@ makeBox config = widget where
Rect vx vy vw vh = viewport
Rect rx ry rw rh = renderArea
child = Seq.index children 0
contentW = getMaxReqCoord $ _wiSizeReqW child
contentH = getMaxReqCoord $ _wiSizeReqH child
contentW = getMaxSizeReq $ _wiSizeReqW child
contentH = getMaxSizeReq $ _wiSizeReqH child
vpChild = Rect vx vy (min vw contentW) (min vh contentH)
raChild = Rect rx ry (min rw contentW) (min rh contentH)
ah = fromMaybe ACenter (_boxAlignH config)

View File

@ -253,7 +253,7 @@ makeDropdown widgetData items makeMain makeRow config state = widget where
resize wenv viewport renderArea children widgetInst = resized where
area = case Seq.lookup 0 children of
Just child -> (oViewport, oRenderArea) where
reqHeight = getMinReqCoord . _wiSizeReqH $ child
reqHeight = getMinSizeReq . _wiSizeReqH $ child
maxHeight = min reqHeight 150
oViewport = viewport {
_rY = _rY viewport + _rH viewport,

View File

@ -33,7 +33,7 @@ makeFixedGrid isHorizontal = widget where
nReqs = length vchildren
vreqsW = _wiSizeReqW <$> vchildren
vreqsH = _wiSizeReqH <$> vchildren
fixedReqs reqs = Seq.filter isFixedReq reqs
fixedReqs reqs = Seq.filter isFixedSizeReq reqs
fixedW = nReqs > 0 && Seq.length (fixedReqs vreqsW) == nReqs
fixedH = nReqs > 0 && Seq.length (fixedReqs vreqsH) == nReqs
factor = 1
@ -45,10 +45,10 @@ makeFixedGrid isHorizontal = widget where
| otherwise = fromIntegral (length vchildren)
width
| Seq.null vchildren = 0
| otherwise = wMul * (maximum . fmap getMaxReqCoord) vreqsW
| otherwise = wMul * (maximum . fmap getMaxSizeReq) vreqsW
height
| Seq.null vchildren = 0
| otherwise = hMul * (maximum . fmap getMaxReqCoord) vreqsH
| otherwise = hMul * (maximum . fmap getMaxSizeReq) vreqsH
newSizeReqW
| not isHorizontal && fixedW = FixedSize width
| otherwise = FlexSize width factor

View File

@ -273,8 +273,8 @@ makeScroll config state = widget where
getSizeReq wenv widgetInst children = sizeReq where
child = Seq.index children 0
w = getMinReqCoord $ _wiSizeReqW child
h = getMinReqCoord $ _wiSizeReqH child
w = getMinSizeReq $ _wiSizeReqW child
h = getMinSizeReq $ _wiSizeReqH child
factor = 1
sizeReq = (FlexSize w factor, FlexSize h factor)
@ -285,8 +285,8 @@ makeScroll config state = widget where
dy = _sstDeltaY state
child = Seq.index (_wiChildren widgetInst) 0
childWidth2 = getMinReqCoord $ _wiSizeReqW child
childHeight2 = getMinReqCoord $ _wiSizeReqH child
childWidth2 = getMinSizeReq $ _wiSizeReqW child
childHeight2 = getMinSizeReq $ _wiSizeReqH child
areaW = max w childWidth2
areaH = max h childHeight2

View File

@ -49,17 +49,17 @@ makeStack isHorizontal = widget where
newSizeReqW
| isVertical && Seq.null flexW = FixedSize (maximum fixedW)
| isVertical && Seq.null fixedW = FlexSize (maximum flexW) factW
| isVertical = BoundedSize (maximum fixedW) tmaxW factW
| isVertical = RangeSize (maximum fixedW) tmaxW factW
| Seq.null flexW = FixedSize (sum fixedW)
| Seq.null fixedW = FlexSize (sum flexW) factW
| otherwise = BoundedSize (sum fixedW) tsumW factW
| otherwise = RangeSize (sum fixedW) tsumW factW
newSizeReqH
| isHorizontal && Seq.null flexH = FixedSize (maximum fixedH)
| isHorizontal && Seq.null fixedH = FlexSize (maximum flexH) factH
| isHorizontal = BoundedSize (maximum fixedH) tmaxH factH
| isHorizontal = RangeSize (maximum fixedH) tmaxH factH
| Seq.null flexH = FixedSize (sum fixedH)
| Seq.null fixedH = FlexSize (sum flexH) factH
| otherwise = BoundedSize (sum fixedH) tsumH factH
| otherwise = RangeSize (sum fixedH) tsumH factH
resize wenv viewport renderArea children widgetInst = resized where
Rect l t w h = renderArea
@ -89,7 +89,7 @@ makeStack isHorizontal = widget where
mainSize = case mainReqSelector child of
FixedSize sz -> sz
FlexSize sz factor -> (1 + fExtra * factor) * sz
BoundedSize sz1 sz2 factor -> sz1 + (1 + fExtra * factor) * (sz2 - sz1)
RangeSize sz1 sz2 factor -> sz1 + (1 + fExtra * factor) * (sz2 - sz1)
hRect = Rect offset t mainSize h
vRect = Rect l offset w mainSize
result
@ -112,7 +112,7 @@ makeStack isHorizontal = widget where
calcDimensions vchildren useFactor = (maxW, sumW, maxH, sumH) where
getReqSize
| useFactor = getReqFactored
| otherwise = getMinReqCoord
| otherwise = getMinSizeReq
vreqsW = _wiSizeReqW <$> vchildren
vreqsH = _wiSizeReqH <$> vchildren
sumW = (sum . fmap getReqSize) vreqsW
@ -140,28 +140,28 @@ makeStack isHorizontal = widget where
| isHorizontal = _rW
| otherwise = _rH
getFixedSize :: SizeReq -> Coord
getFixedSize :: SizeReq -> Double
getFixedSize (FixedSize c) = c
getFixedSize (FlexSize c _) = 0
getFixedSize (BoundedSize c1 _ _) = c1
getFixedSize (RangeSize c1 _ _) = c1
getFlexSize :: Bool -> SizeReq -> Coord
getFlexSize :: Bool -> SizeReq -> Double
getFlexSize useFactor req = coord where
factor
| useFactor = getReqFactor req
| useFactor = getFactorReq req
| otherwise = 1
coord = case req of
FixedSize c -> 0
FlexSize c _ -> c * factor
BoundedSize c1 c2 _ -> (c2 - c1) * factor
RangeSize c1 c2 _ -> (c2 - c1) * factor
getFactorAvg :: Seq SizeReq -> Double
getFactorAvg reqs
| Seq.null flexReqs = 1
| otherwise = sum (fmap getReqFactor flexReqs) / flexCount
| otherwise = sum (fmap getFactorReq flexReqs) / flexCount
where
flexReqs = Seq.filter (not . isFixedReq) reqs
flexReqs = Seq.filter (not . isFixedSizeReq) reqs
flexCount = fromIntegral (Seq.length flexReqs)
getReqFactored :: SizeReq -> Coord
getReqFactored req = getReqFactor req * getMinReqCoord req
getReqFactored :: SizeReq -> Double
getReqFactored req = getFactorReq req * getMinSizeReq req

View File

@ -1,13 +1,13 @@
module Monomer.Widgets.Util.Base (
handleSizeReqStyle,
handleStyleChange,
isFixedReq,
isFlexReq,
isBoundedReq,
getMinReqCoord,
getMaxReqCoord,
getReqFactor,
modifyReqCoord
isFixedSizeReq,
isFlexSizeReq,
isBoundedSizeReq,
getMinSizeReq,
getMaxSizeReq,
getFactorReq,
modifySizeReq
) where
import Data.Default
@ -57,37 +57,37 @@ handleSizeReqStyle style (reqW, reqH) = (newReqW, newReqH) where
addOuterSizeReq :: StyleState -> (SizeReq, SizeReq) -> (SizeReq, SizeReq)
addOuterSizeReq style (reqW, reqH) = (newReqW, newReqH) where
Size w h = addOuterSize style def
newReqW = modifyReqCoord reqW (+w)
newReqH = modifyReqCoord reqH (+h)
newReqW = modifySizeReq reqW (+w)
newReqH = modifySizeReq reqH (+h)
isFixedReq :: SizeReq -> Bool
isFixedReq FixedSize{} = True
isFixedReq _ = False
isFixedSizeReq :: SizeReq -> Bool
isFixedSizeReq FixedSize{} = True
isFixedSizeReq _ = False
isFlexReq :: SizeReq -> Bool
isFlexReq FlexSize{} = True
isFlexReq _ = False
isFlexSizeReq :: SizeReq -> Bool
isFlexSizeReq FlexSize{} = True
isFlexSizeReq _ = False
isBoundedReq :: SizeReq -> Bool
isBoundedReq BoundedSize{} = True
isBoundedReq _ = False
isBoundedSizeReq :: SizeReq -> Bool
isBoundedSizeReq RangeSize{} = True
isBoundedSizeReq _ = False
getMinReqCoord :: SizeReq -> Coord
getMinReqCoord (FixedSize c) = c
getMinReqCoord (FlexSize c _) = c
getMinReqCoord (BoundedSize c1 c2 _) = c1
getMinSizeReq :: SizeReq -> Double
getMinSizeReq (FixedSize c) = c
getMinSizeReq (FlexSize c _) = c
getMinSizeReq (RangeSize c1 c2 _) = c1
getMaxReqCoord :: SizeReq -> Coord
getMaxReqCoord (FixedSize c) = c
getMaxReqCoord (FlexSize c _) = c
getMaxReqCoord (BoundedSize c1 c2 _) = c2
getMaxSizeReq :: SizeReq -> Double
getMaxSizeReq (FixedSize c) = c
getMaxSizeReq (FlexSize c _) = c
getMaxSizeReq (RangeSize c1 c2 _) = c2
getReqFactor :: SizeReq -> Factor
getReqFactor (FixedSize _) = 1
getReqFactor (FlexSize _ f) = f
getReqFactor (BoundedSize _ _ f) = f
getFactorReq :: SizeReq -> Factor
getFactorReq (FixedSize _) = 1
getFactorReq (FlexSize _ f) = f
getFactorReq (RangeSize _ _ f) = f
modifyReqCoord :: SizeReq -> (Coord -> Coord) -> SizeReq
modifyReqCoord (FixedSize c) f = FixedSize (f c)
modifyReqCoord (FlexSize c factor) f = FlexSize (f c) factor
modifyReqCoord (BoundedSize c1 c2 factor) f = BoundedSize (f c1) (f c2) factor
modifySizeReq :: SizeReq -> (Double -> Double) -> SizeReq
modifySizeReq (FixedSize c) f = FixedSize (f c)
modifySizeReq (FlexSize c factor) f = FlexSize (f c) factor
modifySizeReq (RangeSize c1 c2 factor) f = RangeSize (f c1) (f c2) factor