Skip to content
Snippets Groups Projects
Commit 9af71c49 authored by Edward Sabinus's avatar Edward Sabinus
Browse files

Merge branch 'develop' into 'master'

Develop

See merge request !1
parents 202c9e23 cb350468
No related branches found
No related tags found
1 merge request!1Develop
elm-stuff/*
# WWW20 Projekt ShapePicture Editor
# WWW Projekt ShapePicture Editor
Projektidee:
Website zum Erstellen von Shapes und Export als Json.
Möglichkeit zur Eingabe eigener Bilder und Jsons von Shapes.
Am besten dann wenn Andere die Möglichkeit haben das so entstandene Bild mit shapes in ihrer Website einbinden könenn
elm.json 0 → 100644
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/svg": "1.0.1",
"elm/url": "1.0.0"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
index.html 0 → 100644
This diff is collapsed.
module Manager.ModalManager exposing (..)
-- Own
import Util.Util exposing (MousePosition)
import Util.ShapeManager exposing (..)
type Modal
= NoModal
| ShowTable
| ShowInput Shape String String
| ShowShape Int
type ModalMsg
= CloseModal
-- show Modal
| OpenShowTable
| OpenShowInput
| OpenShowShape Int
-- for input / delete Shapes
| ResetShowInput
| Check String
| Input Char String
| InputPoint
-- Update
updateModal : ModalMsg -> Modal -> Modal
updateModal msg modal =
case msg of
CloseModal -> NoModal
OpenShowTable -> if (eq_String_Modal "ShowTable" modal)
then NoModal
else ShowTable
OpenShowInput -> if (eq_String_Modal "ShowInput" modal)
then NoModal
else ShowInput Empty "" ""
OpenShowShape id -> if (id <= 0)
then NoModal
else ShowShape id
ResetShowInput -> ShowInput
(deleteCoordsFromShape
(getInputShapeFromModal modal)
) "" ""
Check shape -> if (eq_String_Modal "ShowInput" modal)
then if (eq_String_Shape shape (getInputShapeFromModal modal))
then ShowInput Empty "" ""
else ShowInput (shapeDefaultConstruktor shape) "" ""
else modal
Input inputType value ->
case modal of
NoModal -> modal -- ERROR
ShowTable -> modal -- ERROR
ShowShape _ -> modal -- ERROR
ShowInput iShape ix iy -> updateInput inputType value modal "ShowInput" iShape ix iy {x = 0, y = 0}
InputPoint -> case modal of
NoModal -> modal -- ERROR
ShowTable -> modal -- ERROR
ShowShape _ -> modal -- ERROR
ShowInput iShape ix iy -> ShowInput (Polygon ((ix,iy)::(getPointListFromPolygon iShape))) "" ""
updateInput : Char -> String -> Modal -> String -> Shape -> String -> String -> MousePosition -> Modal
updateInput inputType value modal modalType iShape ix iy mouse =
case inputType of
'1' -> case iShape of
Empty -> modal -- ERROR
Circle _ b c -> modalInputConstruktor modalType (Circle value b c ) ix iy mouse
Rectangle _ b c d -> modalInputConstruktor modalType (Rectangle value b c d) ix iy mouse
Polygon _ -> modalInputConstruktor modalType iShape value iy mouse
'2' -> case iShape of
Empty -> modal -- ERROR
Circle a _ c -> modalInputConstruktor modalType (Circle a value c ) ix iy mouse
Rectangle a _ c d -> modalInputConstruktor modalType (Rectangle a value c d) ix iy mouse
Polygon _ -> modalInputConstruktor modalType iShape ix value mouse
'3' -> case iShape of
Empty -> modal -- ERROR
Circle a b _ -> modalInputConstruktor modalType (Circle a b value ) ix iy mouse
Rectangle a b _ d -> modalInputConstruktor modalType (Rectangle a b value d) ix iy mouse
Polygon _ -> modal -- ERROR
'4' -> case iShape of
Empty -> modal -- ERROR
Circle _ _ _ -> modal -- ERROR
Rectangle a b c _ -> modalInputConstruktor modalType (Rectangle a b c value) ix iy mouse
Polygon _ -> modal -- ERROR
_ -> modal -- ERROR
-- Modal Construktors
modalInputConstruktor : String -> Shape -> String -> String -> MousePosition -> Modal
modalInputConstruktor modalType shape x y mouse =
case modalType of
"ShowInput" -> ShowInput shape x y
_ -> NoModal
-- Operations on Modal
eq_String_Modal : String -> Modal -> Bool
eq_String_Modal string modal = string==(modalNameToString modal)
-- Get Methods : Parameter of Modal
getInputShapeFromModal : Modal -> Shape
getInputShapeFromModal modal =
case modal of
NoModal -> Empty
ShowTable -> Empty
ShowInput shape _ _ -> shape
ShowShape _ -> Empty
-- Transform Modal to other Types
modalNameToString : Modal -> String
modalNameToString modal =
case modal of
NoModal -> "NoModal"
ShowTable -> "ShowTable"
ShowInput _ _ _ -> "ShowInput"
ShowShape _ -> "ShowShape"
\ No newline at end of file
module Manager.StateManager exposing (..)
-- Own
import Manager.ModalManager exposing (Modal(..), eq_String_Modal, modalNameToString, getInputShapeFromModal)
import Util.ShapeManager exposing (Shape(..))
import Util.Util exposing (MousePosition)
type State
= Standby
| ActiveModal Modal
| DrawShape Shape MousePosition
-- Operations on State
eq_String_State : String -> State -> Bool
eq_String_State string state = string==(stateNameToString state)
manageState : State -> State
manageState state = if ( (eq_String_State "ActiveModal" state)
&&(eq_String_Modal "NoModal" (getModalFromState state))
)
then Standby
else state
-- Transform State to other Types
stateNameToString : State -> String
stateNameToString state =
case state of
Standby -> "Standby"
ActiveModal modal -> "ActiveModal"
DrawShape _ _ -> "DrawShape"
-- Get Methods : Parameter of State
getInputShapeFromState : State -> Shape
getInputShapeFromState state =
case state of
Standby -> Empty
ActiveModal modal -> getInputShapeFromModal modal
DrawShape shape _ -> shape
getModalFromState : State -> Modal
getModalFromState state =
case state of
Standby -> NoModal
ActiveModal modal -> modal
DrawShape _ _ -> NoModal
module Manager.UpdateManager exposing (update)
-- Browser
import Browser
import Browser.Navigation as Nav
-- Http
import Http
-- Url
import Url
-- Own
import TypeHolder exposing (Model, Msg(..))
import Manager.StateManager exposing (..)
import Manager.ModalManager exposing (..)
import Util.ShapeManager exposing (..)
import Util.Util exposing (..)
-- UPDATE
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
( updateModel msg model
, updateCmdMsg msg model
)
updateModel : Msg -> Model -> Model
updateModel msg model =
case msg of
ManageModal modalMsg value ->
let modal = getModalFromState model.state
in { model | state = manageState (ActiveModal
(case modalMsg of
CloseModal -> updateModal modalMsg modal
OpenShowInput -> updateModal modalMsg modal
OpenShowTable -> updateModal modalMsg modal
OpenShowShape _ -> updateModal modalMsg modal
ResetShowInput -> updateModal modalMsg modal
Check _ -> updateModal modalMsg modal
Input inputType _ -> updateModal (Input inputType value) modal
InputPoint -> updateModal modalMsg modal
)
)
}
ManageShapes shapesMsg ->
case shapesMsg of
DeleteShape _ -> { model | saved = updateShapes shapesMsg model.saved}
AddShape _ -> if (eq_String_Modal "ShowInput" (getModalFromState model.state))
then updateModel (ManageModal ResetShowInput "")
{ model | saved = updateShapes (AddShape (getInputShapeFromState model.state)) model.saved}
else { model | saved = updateShapes (AddShape (getInputShapeFromState model.state)) model.saved
, state = Standby
}
SvgShapeHover id -> { model | svgShapeHover = id}
OpenDrawShape shape -> { model | state = DrawShape shape {x = 0, y = 0} }
UpdateMousePos mouse -> case model.state of
Standby -> model -- ERROR
ActiveModal _ -> model -- ERROR
DrawShape iShape mOld -> { model | state = DrawShape iShape mouse }
InputDrawShape -> case model.state of
Standby -> model -- ERROR
ActiveModal _ -> model -- ERROR
DrawShape iShape mPos -> case iShape of
Empty -> model -- ERROR
Circle x y r ->
if x=="" then { model | state = DrawShape (Circle (String.fromInt mPos.x)
(String.fromInt mPos.y) r ) mPos }
else { model | saved = (Circle x y (abstand x y mPos))::model.saved
, state = Standby
}
Rectangle a b w h ->
if a==""
then { model | state = DrawShape
(Rectangle (String.fromInt mPos.x) (String.fromInt mPos.y) w h)
mPos
}
else let coords = manageRectCoords
{x= a
,y= b
,w= (abstand1D mPos.x (stringToInt a))
,h= (abstand1D mPos.y (stringToInt b))
}
in { model | saved = (Rectangle coords.x coords.y coords.w coords.h)::model.saved
, state = Standby
}
Polygon pList -> { model | state = DrawShape
(Polygon (( String.fromInt mPos.x
, String.fromInt mPos.y
)::pList
)
)
mPos
}
LoadShapes result ->
case result of
Ok "" -> { model | shapesLoadResult = Err (Http.BadBody "Empty result for LoadShapes") }
Ok "LoadExample"-> model
Ok "Fenster" -> model
Ok "T%C3%BCren" -> model --%C3%BC = ü
Ok s -> { model | shapesLoadResult = result
, saved = shapesDecoder s
}
Err e -> { model | shapesLoadResult = result }
ShapesLoaded result ->
case result of
Ok shapes-> { model | saved = shapes, shapesLoadResult = Ok "shapes"}
Err e -> { model | saved = [] , shapesLoadResult = (Err e )}
LinkClicked urlRequest -> model
UrlChanged url -> { model | url = url}
updateCmdMsg : Msg -> Model -> Cmd Msg
updateCmdMsg msg model =
case msg of
ManageModal _ _ -> Cmd.none
ManageShapes _ -> Cmd.none
SvgShapeHover _ -> Cmd.none
UpdateMousePos _ -> Cmd.none
InputDrawShape -> Cmd.none
ShapesLoaded _ -> Cmd.none
OpenDrawShape _ -> Cmd.none
LoadShapes result ->
case result of
Ok "LoadExample" ->
Http.get
{ url = "https://cors-anywhere.herokuapp.com/"
--++"https://users.informatik.uni-halle.de/~hinnebur/shapes-circ-rect-ploy.json" --don't works yet
++"https://users.informatik.uni-halle.de/~hinnebur/shapes-list-circ-rect-poly.json"
--++"https://users.informatik.uni-halle.de/~hinnebur/shapes-list-circ-rect.json"
, expect = Http.expectString LoadShapes
}
Ok s -> if (s=="Fenster" || s=="T%C3%BCren") --%C3%BC = ü
then Http.get
{ url = "https://cors-anywhere.herokuapp.com/" ++ (if (s=="Fenster") then
"https://users.informatik.uni-halle.de/~hinnebur/fenster-liste.json"else
"https://users.informatik.uni-halle.de/~hinnebur/tueren-liste.json")
, expect = Http.expectJson ShapesLoaded rectListDecoder
}
else Cmd.none
Err e -> Cmd.none
LinkClicked urlReq ->
case urlReq of
Browser.Internal url -> Nav.pushUrl model.key (Url.toString url)
Browser.External href -> Nav.load href
UrlChanged url -> updateCmdMsg (LoadShapes (Ok (Maybe.withDefault "" url.fragment))) model
\ No newline at end of file
This diff is collapsed.
module ShapePicture_Editor exposing (main)
--Browser
import Browser
import Browser.Navigation as Nav
-- Url
import Url
-- Own
import TypeHolder exposing (Model, Msg(..))
import Manager.UpdateManager exposing (update)
import Manager.ViewManager exposing (view)
import Manager.StateManager as StM
main = Browser.application
{ init = init
, subscriptions = subscriptions
, update = update
, view = documentView
, onUrlChange = UrlChanged
, onUrlRequest = LinkClicked
}
-- INIT
init : () -> Url.Url -> Nav.Key-> (Model, Cmd Msg)
init _ url key =
( Model
StM.Standby
[]
0
(Ok " Init")
url
key
, Cmd.none
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model = Sub.none
-- VIEW
documentView : Model -> Browser.Document Msg
documentView model = { title = "ShapePicture_Editor"
, body = [view model]
}
\ No newline at end of file
module TypeHolder exposing (Model, Msg(..))
-- Http
import Http
-- Browser
import Browser
import Browser.Navigation as Nav
-- Url
import Url
-- Own
import Manager.StateManager exposing (State)
import Manager.ModalManager exposing (Modal, ModalMsg)
import Util.ShapeManager exposing (Shape, ShapesMsg)
import Util.Util exposing (MousePosition)
type alias Model =
{ state : State
, saved : List (Shape)
, svgShapeHover : Int
, shapesLoadResult : (Result Http.Error String)
, url : Url.Url
, key : Nav.Key
}
type Msg
-- Manage Objects
= ManageModal ModalMsg String
| ManageShapes ShapesMsg
-- svg
| SvgShapeHover Int
-- for drawing shapes
| OpenDrawShape Shape
| UpdateMousePos MousePosition
| InputDrawShape
-- for loading shapes with http
| LoadShapes (Result Http.Error String)
| ShapesLoaded (Result Http.Error (List Shape))
-- Url
| LinkClicked Browser.UrlRequest
| UrlChanged Url.Url
module Util.ShapeManager exposing (..)
-- Json
import Json.Decode as JD
type Shape
= Empty
| Circle String String String
| Rectangle String String String String
| Polygon (List ( String, String ) )
type ShapesMsg
= DeleteShape Int
| AddShape Shape
-- Update
updateShapes : ShapesMsg -> List Shape -> List Shape
updateShapes shapesMsg shapeList =
case shapesMsg of
DeleteShape id -> List.append (List.take (id-1) shapeList)
(List.drop (id) shapeList)
AddShape shape -> shape::shapeList
-- Shape Construktors
shapeDefaultConstruktor : String -> Shape
shapeDefaultConstruktor shape =
case shape of
"Circle" -> Circle "" "" ""
"Rectangle"-> Rectangle "" "" "" ""
"Polygon" -> Polygon []
_ -> Empty
shapesDecoder : String -> List Shape
shapesDecoder s =
if (String.contains "shapes" s)
then []
else
List.concat
[ if (String.contains "circle" s)
then [ Circle (shapesDecoderList "circle" "x" s)
(shapesDecoderList "circle" "y" s)
(shapesDecoderList "circle" "r" s)
]
else []
, if (String.contains "rectangle" s)
then [ Rectangle (shapesDecoderList "rectangle" "x" s)
(shapesDecoderList "rectangle" "y" s)
(shapesDecoderList "rectangle" "width" s)
(shapesDecoderList "rectangle" "height" s)
]
else []
, if (String.contains "polygon" s)
then [ Polygon (List.map2 Tuple.pair
(shapesDecoderListPoly "x" s)
(shapesDecoderListPoly "y" s)
)
]
else []
]
-- Operations on Shapes
eq_String_Shape : String -> Shape -> Bool
eq_String_Shape string shape = string==(shapeNameToString shape)
deleteCoordsFromShape : Shape -> Shape
deleteCoordsFromShape shape =
case shape of
Empty -> Empty
Circle _ _ _ -> Circle "" "" ""
Rectangle _ _ _ _ -> Rectangle "" "" "" ""
Polygon _ -> Polygon []
-- Get Methods : Parameter of Shapes
getPointListFromPolygon : Shape -> List (String,String)
getPointListFromPolygon shape =
case shape of
Empty -> []
Circle _ _ _ -> []
Rectangle _ _ _ _ -> []
Polygon pointList -> pointList
-- Transform Shape to other Types
shapeNameToString : Shape -> String
shapeNameToString shape =
case shape of
Empty -> ""
Circle _ _ _ -> "Circle"
Rectangle _ _ _ _ -> "Rectangle"
Polygon _ -> "Polygon"
shapeNameStringToGerman : String -> String
shapeNameStringToGerman shape =
case shape of
"Circle" -> "Kreis"
"Rectangle"-> "Rechteck"
"Polygon" -> "Vieleck"
_ -> ""
shapeCoordsToString : Shape -> String
shapeCoordsToString shape =
case shape of
Empty -> ""
Circle x_center y_center radius -> x_center++","++y_center++","++radius
Rectangle x_left y_upper x_right y_lower -> x_left++","++y_upper++","++x_right++","++y_lower
Polygon pointList ->
case pointList of
[] -> ""
(x,y)::xs -> "("++x++","++y++") "++(shapeCoordsToString (Polygon xs))
shapeCoordsToHtmlCoords : Shape -> String
shapeCoordsToHtmlCoords shape =
case shape of
Empty -> ""
Circle _ _ _ -> shapeCoordsToString shape
Rectangle _ _ _ _ -> shapeCoordsToString shape
Polygon _ -> String.left ((String.length (shapeCoordsToHtmlCoordsHelper shape))-1)
(shapeCoordsToHtmlCoordsHelper shape)
shapeCoordsToHtmlCoordsHelper : Shape -> String
shapeCoordsToHtmlCoordsHelper shape =
case shape of
Empty -> ""
Circle _ _ _ -> shapeCoordsToString shape
Rectangle _ _ _ _ -> shapeCoordsToString shape
Polygon pointList ->
case pointList of
[] -> ""
(x,y)::xs -> x++","++y++","++(shapeCoordsToHtmlCoordsHelper (Polygon xs))
-- Decoder of shapes
rectListDecoder : JD.Decoder (List Shape)
rectListDecoder = JD.field "rectangle" (JD.list (JD.map4
Rectangle
(JD.field "x" JD.string)
(JD.field "y" JD.string)
(JD.field "width" JD.string)
(JD.field "height" JD.string)
)
)
shapesDecoderList : String -> String -> String -> String
shapesDecoderList shape var s =
let res = JD.decodeString
(JD.field shape (JD.list (JD.field var JD.string)))
s
in case res of
Ok stringList -> case stringList of
[] -> ""
x::[] -> x
x::xs -> x++" This List is too long"
Err e -> "ERROR"
shapesDecoderListPoly : String -> String -> List String
shapesDecoderListPoly var s =
let res = JD.decodeString
(JD.field "polygon" (JD.list (JD.field "points" (JD.list (JD.field var JD.string)))))
s
in case res of
Ok list -> case list of
[] -> []
x::[] -> x
x::xs -> x++["This List is too long"]
Err e -> ["ERROR"]
\ No newline at end of file
module Util.Util exposing (..)
-- Json
import Json.Decode as JD
-- Svg
import Svg
import Svg.Events
-- Http
import Http
-- Html
import Html exposing (Html, Attribute, node)
-- Html Elements
link : List (Attribute msg) -> List (Html msg) -> Html msg
link attributes children = node "link" attributes children
-- Working with Error
-- Transform Error to other Types
httpErrorToString : Http.Error -> String
httpErrorToString e = "Http.Error: "++
case e of
Http.BadUrl string -> "BadUrl " ++string
Http.Timeout -> "Timeout"
Http.NetworkError -> "NetworkError"
Http.BadStatus int -> "badStatus "++(String.fromInt int)
Http.BadBody string -> "BadBody " ++string
-- Math Methods
abstand1D : Int -> Int -> String
abstand1D a b = String.fromInt (a-b)
abstand : String -> String -> MousePosition -> String
abstand x y mPos = String.fromInt (round((toFloat (((stringToInt x)-mPos.x)^2+((stringToInt y)-mPos.y)^2) )^0.5))
type alias RectCoords =
{ x : String
, y : String
, w : String
, h : String
}
manageRectCoords : RectCoords -> RectCoords
manageRectCoords coords =
let x = stringToInt coords.x
y = stringToInt coords.y
w = stringToInt coords.w
h = stringToInt coords.h
in if w>0
then if h>0
then coords
else {x=String.fromInt x ,y=String.fromInt (y+h),w=String.fromInt w,h=String.fromInt -h}
else if h>0
then {x=String.fromInt (x+w),y=String.fromInt y ,w=String.fromInt -w,h=String.fromInt h}
else {x=String.fromInt (x+w),y=String.fromInt (y+h),w=String.fromInt -w,h=String.fromInt -h}
-- Transformation between elm-types
stringToInt : String -> Int
stringToInt a = Maybe.withDefault 0 (String.toInt a)
-- MouseMove
type alias MousePosition =
{ x : Int, y : Int }
onMouseMove : (MousePosition -> msg) -> Svg.Attribute msg
onMouseMove mapMousePositionToMsg =
Svg.Events.on "mousemove" (JD.map mapMousePositionToMsg offsetMousePosition)
offsetMousePosition : JD.Decoder MousePosition
offsetMousePosition =
JD.map2 MousePosition (JD.field "offsetX" JD.int)
(JD.field "offsetY" JD.int)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment