Skip to content

Commit 8c56502

Browse files
you can draw ellilpse now
1 parent 11038af commit 8c56502

File tree

8 files changed

+42
-18
lines changed

8 files changed

+42
-18
lines changed

TODO.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ DONE-support label and text area
241241
-make BoxLabelHandler generic to shapes
242242
-make BoxTextHandler generic to shapes
243243
-finish ShapeModifyHandler when you do the above 2 thing
244-
-connect fully and deprecate BoxLabel/TextHandler
244+
IGNORE-refactor ShapeDef please...
245+
DONE-connect fully and deprecate BoxLabel/TextHandler
245246
-you can still keep BoxHandler for now as it does multi-select for you
247+
-make ellipse drawer
246248
-add UTs
247-
IGNORE-refactor ShapeDef please...
248249
-make basic shape renderer that just fills all intersecting lines with fill character or whatever
249250

250251
:: shape tool spec

src/Potato/Flow/Controller/Goat.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,22 @@ goatState_hasLocalPreview = owlPFWorkspace_hasLocalPreview . _goatState_workspac
182182
makeHandlerFromNewTool :: GoatState -> Tool -> SomePotatoHandler
183183
makeHandlerFromNewTool GoatState{..} = \case
184184
Tool_Box -> SomePotatoHandler $ def { _boxHandler_creation = BoxCreationType_Box }
185+
-- TODO this should just work, need to update UTs
186+
--Tool_Box -> SomePotatoHandler $ (def { _shapeCreationHandler_shapeType = ShapeType_Box } )
185187
Tool_Line -> SomePotatoHandler $ def { _autoLineHandler_isCreation = True }
186188
Tool_Select -> makeHandlerFromSelection _goatState_canvasSelection
187189
Tool_Text -> SomePotatoHandler $ def { _boxHandler_creation = BoxCreationType_Text }
188190
Tool_TextArea -> SomePotatoHandler $ def { _boxHandler_creation = BoxCreationType_TextArea }
189191
Tool_Pan -> SomePotatoHandler $ (def :: PanHandler)
190-
Tool_Shape -> SomePotatoHandler $ (def { _shapeCreationHandler_shapeType = ShapeType_Box } )
192+
Tool_Shape -> SomePotatoHandler $ (def { _shapeCreationHandler_shapeType = ShapeType_Ellipse } )
191193

192194

193195
-- TODO rename to makeHandlerFromCanvasSelection
194196
makeHandlerFromSelection :: CanvasSelection -> SomePotatoHandler
195197
makeHandlerFromSelection selection = case computeSelectionType selection of
196198
SMTBox -> SomePotatoHandler $ (def :: BoxHandler)
199+
-- TODO this should just work, need to update UTs
200+
--SMTBox -> SomePotatoHandler $ (def :: ShapeModifyHandler)
197201
SMTBoxText -> SomePotatoHandler $ (def :: BoxHandler)
198202
SMTLine -> SomePotatoHandler $ (def :: AutoLineHandler)
199203
SMTTextArea -> SomePotatoHandler $ (def :: BoxHandler)

src/Potato/Flow/Controller/Manipulator/Box.hs

+11-5
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ instance PotatoHandler BoxHandler where
518518

519519

520520
-- TODO move this to a more appropriate place
521-
data ShapeType = ShapeType_Unknown | ShapeType_Box deriving (Show, Eq)
521+
data ShapeType = ShapeType_Unknown | ShapeType_Box | ShapeType_Ellipse deriving (Show, Eq)
522522

523523

524524

@@ -600,12 +600,14 @@ instance PotatoHandler ShapeCreationHandler where
600600

601601
mdd = makeDragDeltaBox _shapeCreationHandler_handle rmd
602602

603-
shapeDef = case _shapeCreationHandler_shapeType of
604-
ShapeType_Box -> boxShapeDef
603+
someShapeDef = case _shapeCreationHandler_shapeType of
604+
ShapeType_Box -> SomeShapeDef boxShapeDef
605+
ShapeType_Ellipse -> SomeShapeDef ellipseShapeDef
605606
ShapeType_Unknown -> error "attempting to use ShapeCreationHandler with ShapeType_Unknown"
606607

607-
mop = Just $ makeAddEltLlama _potatoHandlerInput_pFState newEltPos $
608-
shapeType_to_owlItem _potatoHandlerInput_potatoDefaultParameters (canonicalLBox_from_lBox $ LBox _mouseDrag_from dragDelta) shapeDef
608+
mop = case someShapeDef of
609+
SomeShapeDef shapeDef -> Just $ makeAddEltLlama _potatoHandlerInput_pFState newEltPos $
610+
shapeType_to_owlItem _potatoHandlerInput_potatoDefaultParameters (canonicalLBox_from_lBox $ LBox _mouseDrag_from dragDelta) shapeDef
609611

610612
newbh = bh {
611613
_shapeCreationHandler_undoFirst = True
@@ -686,6 +688,7 @@ shapeModifyHandlerFromSelection :: CanvasSelection -> ShapeModifyHandler
686688
shapeModifyHandlerFromSelection cs = r where
687689
(shapeType, _) = case superOwl_toSElt_hack <$> selectionToMaybeFirstSuperOwl cs of
688690
Just (SEltBox sbox) -> (ShapeType_Box, _shapeDef_impl boxShapeDef sbox)
691+
Just (SEltEllipse sellipse) -> (ShapeType_Ellipse, _shapeDef_impl ellipseShapeDef sellipse)
689692
_ -> (ShapeType_Unknown, emptyShapeImpl)
690693
r = def {
691694
_shapeModifyHandler_shapeType = shapeType
@@ -701,8 +704,11 @@ instance PotatoHandler ShapeModifyHandler where
701704
pHandlerName _ = handlerName_shapeModify
702705
pHandleMouse bh@ShapeModifyHandler {..} phi@PotatoHandlerInput {..} rmd@(RelMouseDrag MouseDrag {..}) = let
703706
selt = superOwl_toSElt_hack $ selectionToFirstSuperOwl _potatoHandlerInput_canvasSelection
707+
708+
-- TODO we should combine ShapeImpl with ShapeDef and add type param to ShapeModifyHandler so we don't have to do this weirdness
704709
(shapeDef, shapeImpl) = case (_shapeModifyHandler_shapeType, selt) of
705710
(ShapeType_Box, SEltBox sbox) -> (boxShapeDef, _shapeDef_impl boxShapeDef sbox)
711+
(ShapeType_Ellipse, SEltEllipse sellipse) -> (boxShapeDef, _shapeDef_impl ellipseShapeDef sellipse)
706712
(x, y) -> error ("attempting to use ShapeModifyHandler with (" <> show x <> ", " <> show y <> ")")
707713

708714
in case _mouseDrag_state of

src/Potato/Flow/Controller/Manipulator/Select.hs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ doesOwlSubItemIntersectBox ot rcache lbox sowl = case superOwl_owlSubItem sowl o
4545
Just (OwlItemCache_Line lar _) -> lar
4646
_ -> assert False (sSimpleLineNewRenderFnComputeCache ot sline)
4747
r = lineAnchorsForRender_doesIntersectBox anchors lbox
48+
OwlSubItemEllipse x -> does_lBox_intersect_include_zero_area lbox (_sEllipse_box x)
4849
_ -> False
4950

5051

src/Potato/Flow/Methods/SEltMethods.hs

+16-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ doesSEltIntersectBox_DEPRECATED lbox selt = case selt of
5858
SEltFolderEnd -> False
5959
SEltBox x -> does_lBox_intersect_include_zero_area lbox (_sBox_box x)
6060
SEltTextArea x -> does_lBox_intersect_include_zero_area lbox (_sTextArea_box x)
61+
-- TODO you could use the isInEllipse function here instead
62+
SEltEllipse x -> does_lBox_intersect_include_zero_area lbox (_sEllipse_box x)
6163
-- TODO this is wrong, do it correctly...
6264
-- we use does_lBox_intersect since it's impossible for a SAutoLine to have zero sized box
6365
SEltLine sline -> does_lBox_intersect lbox (fromJust $ getSEltBox_naive (SEltLine sline))
@@ -207,9 +209,18 @@ sEllipse_drawer sellipse = SEltDrawer {
207209
, _sEltDrawer_maxCharWidth = 1
208210
}
209211

210-
-- TODO proper rendering
212+
isInEllipse :: LBox -> XY -> Bool
213+
isInEllipse (LBox (V2 x y) (V2 w h)) (V2 px py) = isInEllipse (px, py)
214+
where
215+
(width, height) = (w, h)
216+
centerX = x + w `div` 2
217+
centerY = y + h `div` 2
218+
a = fromIntegral w / 2
219+
b = fromIntegral h / 2
220+
isInEllipse (i, j) = ((fromIntegral (i - centerX) / a) ^ 2 + (fromIntegral (j - centerY) / b) ^ 2) <= 1
221+
211222
sEllipse_renderFn :: forall a. (HasOwlTree a) => SEllipse -> a -> XY -> Maybe PChar
212-
sEllipse_renderFn sellipse ot (V2 x y) = if does_lBox_contains_XY (_sEllipse_box sellipse) (V2 x y)
223+
sEllipse_renderFn sellipse ot (V2 x y) = if isInEllipse (_sEllipse_box sellipse) (V2 x y)
213224
then Just '#'
214225
else Nothing
215226

@@ -283,6 +294,9 @@ modify_sElt_with_cBoundingBox isDo selt cbb@CBoundingBox {..} = case selt of
283294
SEltTextArea stext -> SEltTextArea $ stext {
284295
_sTextArea_box = modifyDelta isDo (_sTextArea_box stext) _cBoundingBox_deltaBox
285296
}
297+
SEltEllipse sellipse -> SEltEllipse $ sellipse {
298+
_sEllipse_box = modifyDelta isDo (_sEllipse_box sellipse) _cBoundingBox_deltaBox
299+
}
286300
x -> x
287301

288302
modify_sElt_with_cSuperStyle :: Bool -> SElt -> CSuperStyle -> SElt

src/Potato/Flow/Methods/Shape.hs

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import Data.Default
1919
import qualified Text.Show
2020

2121

22-
-- TODO probably just combine this with ShapeImpl and make ShapeModifyHandler take an `o` parameter
22+
data SomeShapeDef = forall o. () => SomeShapeDef (ShapeDef o)
23+
24+
-- TODO probably just combine this with ShapeImpl
2325
data ShapeDef o = ShapeDef {
2426
_shapeDef_name :: Text
2527
, _shapeDef_create :: PotatoDefaultParameters -> LBox -> OwlItem
@@ -29,11 +31,6 @@ data ShapeDef o = ShapeDef {
2931
, _shapeDef_textImpl :: TextImpl o
3032
}
3133

32-
33-
34-
35-
36-
3734
data ShapeImpl = ShapeImpl {
3835
_shapeImpl_updateFromLBox :: REltId -> LBox -> Llama
3936
, _shapeImpl_toLBox :: LBox

src/Potato/Flow/Methods/Types.hs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ getSEltBox_naive selt = case selt of
8383
r = make_lBox_from_XYlist $ (_sAutoLine_start x) : (_sAutoLine_end x) : (_sAutoLine_start x + 1) : (_sAutoLine_end x + 1) : midpoints
8484

8585
SEltTextArea x -> Just $ canonicalLBox_from_lBox_ $ _sTextArea_box x
86+
SEltEllipse x -> Just $ canonicalLBox_from_lBox_ $ _sEllipse_box x
8687

8788
getSEltLabelBox :: SEltLabel -> Maybe LBox
8889
getSEltLabelBox (SEltLabel _ x) = getSEltBox_naive x

test/Potato/Flow/Controller/Manipulator/ShapeSpec.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ drawShape (x, y, sx, sy) = do
2828
canvasMouseUp (x+sx, y+sy)
2929
let
3030
f sowl = case _superOwl_elt sowl of
31-
OwlItem _ (OwlSubItemBox _) -> Nothing
32-
xx -> Just ("expected box, got " <> show xx)
31+
OwlItem _ (OwlSubItemEllipse _) -> Nothing
32+
xx -> Just ("expected ellipse, got " <> show xx)
3333
verifySelectionIsAndOnlyIs "shape is selected" f
3434

3535
basic_test :: Spec

0 commit comments

Comments
 (0)