From 367bc873df37869c2ceab3ef408b6adbe4b3693b Mon Sep 17 00:00:00 2001 From: Oskar Marquardt <oskar.marquardt@student.uni-halle.de> Date: Wed, 12 Jun 2024 12:36:48 +0200 Subject: [PATCH] Added Sphere Module - Added sphere editing - Moved sphere code into dedicated module --- public/main.js | 100 ++++++++++++++++++++++++++++++++++++++++++++-- src/Character.elm | 22 +++++----- src/Main.elm | 28 +++++++++++++ src/Sphere.elm | 23 +++++++++++ 4 files changed, 156 insertions(+), 17 deletions(-) create mode 100644 src/Sphere.elm diff --git a/public/main.js b/public/main.js index 4ac11cd..d102fa5 100644 --- a/public/main.js +++ b/public/main.js @@ -5161,9 +5161,10 @@ var $elm$core$Task$perform = F2( var $elm$browser$Browser$document = _Browser_document; var $author$project$Character$new = { name: 'Default Name', - spheres: { - correspondence: {dots: 0, name: 'Correspondence'} - } + spheres: _List_fromArray( + [ + {dots: 0, name: 'Correspondence'} + ]) }; var $elm$core$Platform$Cmd$batch = _Platform_batch; var $elm$core$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil); @@ -5183,6 +5184,29 @@ var $author$project$Character$changeName = F2( character, {name: newName}); }); +var $author$project$Sphere$isSphere = F2( + function (a, b) { + return _Utils_eq(a.name, b.name); + }); +var $author$project$Sphere$changeSphere = F2( + function (newSphere, sphere) { + return A2($author$project$Sphere$isSphere, sphere, newSphere) ? newSphere : sphere; + }); +var $author$project$Sphere$changeSphereInList = F2( + function (spheres, sphere) { + return A2( + $elm$core$List$map, + $author$project$Sphere$changeSphere(sphere), + spheres); + }); +var $author$project$Character$changeSphere = F2( + function (character, sphere) { + return _Utils_update( + character, + { + spheres: A2($author$project$Sphere$changeSphereInList, character.spheres, sphere) + }); + }); var $author$project$Main$modalValue = F2( function (modalType, character) { return character.name; @@ -5213,7 +5237,7 @@ var $author$project$Main$update = F2( model, {modalValue: value}), $elm$core$Platform$Cmd$none); - default: + case 'SaveModalChange': var modalType = msg.a; return _Utils_Tuple2( _Utils_update( @@ -5223,6 +5247,15 @@ var $author$project$Main$update = F2( modal: $elm$core$Maybe$Nothing }), $elm$core$Platform$Cmd$none); + default: + var sphere = msg.a; + return _Utils_Tuple2( + _Utils_update( + model, + { + character: A2($author$project$Character$changeSphere, model.character, sphere) + }), + $elm$core$Platform$Cmd$none); } }); var $author$project$Main$Name = {$: 'Name'}; @@ -5486,11 +5519,70 @@ var $author$project$Main$viewModal = function (model) { ])); } }; +var $author$project$Main$ChangeSphere = function (a) { + return {$: 'ChangeSphere', a: a}; +}; +var $author$project$Main$viewSphere = function (sphere) { + return A2( + $elm$html$Html$div, + _List_Nil, + _List_fromArray( + [ + A2( + $elm$html$Html$span, + _List_Nil, + _List_fromArray( + [ + $elm$html$Html$text(sphere.name) + ])), + A2( + $elm$html$Html$button, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('button is-small'), + $elm$html$Html$Events$onClick( + $author$project$Main$ChangeSphere( + {dots: sphere.dots - 1, name: sphere.name})) + ]), + _List_fromArray( + [ + $elm$html$Html$text('-') + ])), + A2( + $elm$html$Html$span, + _List_Nil, + _List_fromArray( + [ + $elm$html$Html$text( + $elm$core$String$fromInt(sphere.dots)) + ])), + A2( + $elm$html$Html$button, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('button is-small'), + $elm$html$Html$Events$onClick( + $author$project$Main$ChangeSphere( + {dots: sphere.dots + 1, name: sphere.name})) + ]), + _List_fromArray( + [ + $elm$html$Html$text('+') + ])) + ])); +}; +var $author$project$Main$viewSpheres = function (spheres) { + return A2( + $elm$html$Html$div, + _List_Nil, + A2($elm$core$List$map, $author$project$Main$viewSphere, spheres)); +}; var $author$project$Main$view = function (model) { return { body: _List_fromArray( [ A2($author$project$Main$viewEditableText, $author$project$Main$Name, model.character.name), + $author$project$Main$viewSpheres(model.character.spheres), $author$project$Main$viewModal(model) ]), title: 'Character Sheet' diff --git a/src/Character.elm b/src/Character.elm index 2521fb7..5053b3f 100644 --- a/src/Character.elm +++ b/src/Character.elm @@ -1,17 +1,10 @@ module Character exposing (..) -type alias Character = - { name : String - , spheres : Spheres - } +import Sphere exposing (Sphere) -type alias Spheres = - { correspondence : Sphere - } - -type alias Sphere = +type alias Character = { name : String - , dots : Int + , spheres : List Sphere } changeName : Character -> String -> Character @@ -22,13 +15,16 @@ changeName character newName = else { character | name = newName } +changeSphere : Character -> Sphere -> Character +changeSphere character sphere = + { character | spheres = Sphere.changeSphereInList character.spheres sphere } + new : Character new = { name = "Default Name" , spheres = - { correspondence = - { name = "Correspondence" + [ { name = "Correspondence" , dots = 0 } - } + ] } \ No newline at end of file diff --git a/src/Main.elm b/src/Main.elm index 443f817..f023ba1 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -5,6 +5,7 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import Character exposing (Character) +import Sphere exposing (Sphere) main : Program () Model Msg main = @@ -35,6 +36,7 @@ type Msg | CloseModal | ChangeModalValue String | SaveModalChange ModalType + | ChangeSphere Sphere type ModalType = Name @@ -66,12 +68,19 @@ update msg model = } , Cmd.none ) + ChangeSphere sphere -> + ( { model + | character = Character.changeSphere model.character sphere + } + , Cmd.none + ) view : Model -> Browser.Document Msg view model = { title = "Character Sheet" , body = [ viewEditableText Name model.character.name + , viewSpheres model.character.spheres , viewModal model ] } @@ -92,6 +101,25 @@ viewEditableText modalType content = ] ] +viewSpheres : List Sphere -> Html Msg +viewSpheres spheres = + div [] ( List.map viewSphere spheres ) + +viewSphere : Sphere -> Html Msg +viewSphere sphere = + div [] + [ span [] [ text sphere.name ] + , button + [ class "button is-small" + , onClick ( ChangeSphere { name = sphere.name, dots = sphere.dots - 1 } ) + ] [ text "-" ] + , span [] [ text ( String.fromInt sphere.dots ) ] + , button + [ class "button is-small" + , onClick ( ChangeSphere { name = sphere.name, dots = sphere.dots + 1 } ) + ] [ text "+" ] + ] + viewModal : Model -> Html Msg viewModal model = case model.modal of diff --git a/src/Sphere.elm b/src/Sphere.elm new file mode 100644 index 0000000..32a8ff9 --- /dev/null +++ b/src/Sphere.elm @@ -0,0 +1,23 @@ +module Sphere exposing (..) +import Html exposing (a) + +type alias Sphere = + { name : String + , dots : Int + } + +changeSphereInList : List Sphere -> Sphere -> List Sphere +changeSphereInList spheres sphere = + List.map ( changeSphere sphere ) spheres + +changeSphere : Sphere -> Sphere -> Sphere +changeSphere newSphere sphere = + if isSphere sphere newSphere + then + newSphere + else + sphere + +isSphere : Sphere -> Sphere -> Bool +isSphere a b = + a.name == b.name \ No newline at end of file -- GitLab