summaryrefslogtreecommitdiff
path: root/Bustle/Regions.hs
blob: 1ad535c0ddfbb7c6f8bf7b1ee93642b55a846b0b (plain)
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
{-# LANGUAGE DeriveFunctor #-}
module Bustle.Regions
  (
    Stripe(..)
  , nonOverlapping
  , midpoint

  , Regions
  , translateRegions

  , RegionSelection (..)
  , regionSelectionNew
  , regionSelectionUpdate
  , regionSelectionSelect
  , regionSelectionUp
  , regionSelectionDown
  , regionSelectionFirst
  , regionSelectionLast
  )
where

import Data.Maybe (maybeToList)

import Data.List (sort)

data Stripe = Stripe { stripeTop :: Double
                     , stripeBottom :: Double
                     }
  deriving
    (Show, Eq, Ord)
type Region a = (Stripe, a)
type Regions a = [Region a]

translateRegions :: Double
                 -> Regions a
                 -> Regions a
translateRegions y = map (\(s, a) -> (translate s, a))
  where
    translate (Stripe y1 y2) = Stripe (y1 + y) (y2 + y)

-- A zipper for selected regions. rsBefore is reversed. If rsCurrent is
-- Nothing, the two lists may still both be non-empty (to keep track of roughly
-- where the user's last click was).
data RegionSelection a =
    RegionSelection { rsBefore :: Regions a
                    , rsLastClick :: Double
                    , rsCurrent :: Maybe (Region a)
                    , rsAfter :: Regions a
                    }
  deriving
    (Show, Eq, Functor)

relativeTo :: Double
           -> Stripe
           -> Ordering
relativeTo y (Stripe top bottom)
    | y < top    = LT
    | y > bottom = GT
    | otherwise  = EQ

hits :: Double
     -> Stripe
     -> Bool
hits y stripe = y `relativeTo` stripe == EQ

nonOverlapping :: [Stripe]
               -> Bool
nonOverlapping []         = True
nonOverlapping (_:[])     = True
nonOverlapping (s1:s2:ss) =
    stripeBottom s1 <= stripeTop s2 && nonOverlapping (s2:ss)

regionSelectionNew :: Regions a
                   -> RegionSelection a
regionSelectionNew rs
    | sorted /= map fst rs        = error $ "regionSelectionNew: unsorted regions"
    | not (nonOverlapping sorted) = error $ "regionSelectionNew: overlapping regions"
    | otherwise                   = RegionSelection [] 0 Nothing rs
  where
    sorted = sort (map fst rs)

regionSelectionUpdate :: Double
                      -> RegionSelection a
                      -> RegionSelection a
regionSelectionUpdate y rs = rs' { rsLastClick = y }
  where
    rs' = case rsCurrent rs of
              Just r@(s, _)
                  | y `hits` s -> rs
                  | otherwise  -> doSearch (rsBefore rs) (r:rsAfter rs)
              Nothing -> doSearch (rsBefore rs) (rsAfter rs)
    doSearch bs as =
        if y <= rsLastClick rs
          then
            let (as', result, bs') =
                    searchy y (\y' s -> y' <= stripeBottom s) as bs
            in rs { rsBefore  = bs'
                  , rsCurrent = result
                  , rsAfter   = as'
                  }
          else
            let (bs', result, as') =
                    searchy y (\y' s -> y' >= stripeTop s) bs as
            in rs { rsBefore  = bs'
                  , rsCurrent = result
                  , rsAfter   = as'
                  }

invert :: RegionSelection a
       -> RegionSelection a
invert rs = rs { rsBefore = rsAfter rs, rsAfter = rsBefore rs }

midpoint :: Stripe -> Double
midpoint (Stripe top bottom) = (top + bottom) / 2

regionSelectionUp :: RegionSelection a
                  -> RegionSelection a
regionSelectionUp rs@(RegionSelection before _lastClick current after) =
    case before of
        []     -> rs
        (b:bs) -> RegionSelection bs
                                  (midpoint (fst b))
                                  (Just b)
                                  (maybeToList current ++ after)

regionSelectionDown :: RegionSelection a
                    -> RegionSelection a
regionSelectionDown = invert . regionSelectionUp . invert

regionSelectionFirst :: RegionSelection a
                     -> RegionSelection a
regionSelectionFirst rs =
    case (reverse (rsBefore rs) ++ maybeToList (rsCurrent rs) ++ rsAfter rs) of
        []             -> rs
        (first:others) -> RegionSelection []
                                          (midpoint (fst first))
                                          (Just first)
                                          others

regionSelectionLast :: RegionSelection a
                    -> RegionSelection a
regionSelectionLast = invert . regionSelectionFirst . invert

searchy :: Double
        -> (Double -> Stripe -> Bool)
        -> Regions a
        -> Regions a
        -> (Regions a, Maybe (Region a), Regions a)
searchy y worthContinuing = go
  where
    go befores [] = (befores, Nothing, [])
    go befores afters@(a:as)
        | y `hits` fst a            = (befores, Just a, as)
        | worthContinuing y (fst a) = go (a:befores) as
        | otherwise                 = (befores, Nothing, afters)

regionSelectionSelect :: Eq a
                      => a
                      -> RegionSelection a
                      -> RegionSelection a
regionSelectionSelect x rs
    | fmap snd (rsCurrent rs) == Just x = rs
    | otherwise = case break ((== x) . snd) (rsBefore rs) of
        (ys, z:zs) -> RegionSelection { rsBefore = zs
                                      , rsCurrent = Just z
                                      , rsLastClick = midpoint (fst z)
                                      , rsAfter = reverse ys ++ rsAfter rs
                                      }
        (_, [])    -> case break ((== x) . snd) (rsAfter rs) of
            (ys, z:zs) -> RegionSelection { rsBefore = rsBefore rs ++ reverse ys
                                          , rsCurrent = Just z
                                          , rsLastClick = midpoint (fst z)
                                          , rsAfter = zs
                                          }
            (_, [])    -> rs