Skip to content

Commit

Permalink
Add mouse support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ascani Carlo committed Jan 24, 2024
1 parent c23587c commit fe06f71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/Compiling.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import File.Select as Select
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Json.Decode as Decode exposing (Decoder, decodeString, field, int, map4, string, bool)
import Json.Decode as Decode exposing (Decoder, bool, decodeString, field, int, map4, string)
import Json.Encode as Encode
import Task

Expand Down Expand Up @@ -40,6 +40,7 @@ type Msg
| Load
| FileSelected File.File
| FileLoaded String
| CellClicked Int


type Dir
Expand Down Expand Up @@ -82,6 +83,9 @@ update msg model =
PressedLetter ' ' ->
( maybeSetValue model, Cmd.none )

CellClicked i ->
( maybeSetValueAt model i, Cmd.none )

PressedLetter 'j' ->
( { model | current = move Down model.current }, Cmd.none )

Expand Down Expand Up @@ -163,6 +167,7 @@ view model =
]
, tabindex 1
, id ("cell-" ++ String.fromInt i)
, onClick (CellClicked i)
]
[ span [ class "Cell-val" ] [ text (cellToText c) ]
, span [ class "Cell-index" ] [ text (String.fromInt i) ]
Expand Down Expand Up @@ -271,34 +276,40 @@ encodeCell c =
)


maybeSetValue : Model -> Model
maybeSetValue m =
maybeSetValueAt : Model -> Int -> Model
maybeSetValueAt m i =
let
currentCell =
Array.get m.current m.grid
Array.get i m.grid
in
case currentCell of
Just Available ->
occupyCell m
occupyCellAt m i

_ ->
m


occupyCell : Model -> Model
occupyCell m =
maybeSetValue : Model -> Model
maybeSetValue m =
maybeSetValueAt m m.current


occupyCellAt : Model -> Int -> Model
occupyCellAt m i =
let
withOccupiedCurrent =
Array.set m.current (Occupied (m.score + 1)) m.grid
Array.set i (Occupied (m.score + 1)) m.grid

withAvailables =
Array.indexedMap
(\i c -> cellAvailability i c m.current)
(\idx c -> cellAvailability idx c i)
withOccupiedCurrent
in
{ m
| grid = withAvailables
, score = m.score + 1
, current = i
}


Expand Down
3 changes: 3 additions & 0 deletions src/compiling.css
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,19 @@ button
.Cell.is-occupied
{
background-color: var(--white);
cursor: not-allowed;
}

.Cell.is-available
{
background-color: var(--papirus);
cursor: pointer;
}

.Cell.is-unavailable
{
background-color: var(--gray01);
cursor: not-allowed;
}

.Cell.is-current
Expand Down

0 comments on commit fe06f71

Please sign in to comment.