Skip to content

Commit

Permalink
refactor to make more readable function
Browse files Browse the repository at this point in the history
Signed-off-by: Nahshon Unna-Tsameret <nahsh.ut@gmail.com>
  • Loading branch information
nunnatsa committed Apr 29, 2021
1 parent f4ffae0 commit 9fc29e4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 75 deletions.
102 changes: 57 additions & 45 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,53 +64,10 @@ func (c *Controller) do() {
return

case je := <-c.joystickEvents:
switch je {
case hat.MoveUp:
change = c.state.GoUp()

case hat.MoveLeft:
change = c.state.GoLeft()

case hat.MoveDown:
change = c.state.GoDown()

case hat.MoveRight:
change = c.state.GoRight()

case hat.Pressed:
change = c.state.Paint()
}
change = c.handleJoystickEvent(je)

case e := <-c.clientEvents:
switch data := e.(type) {
case webapp.ClientEventRegistered:
id := uint64(data)
c.registered(id)

case webapp.ClientEventReset:
if data {
change = c.state.Reset()
}

case webapp.ClientEventSetColor:
color := common.Color(data)
change = c.state.SetColor(color)

case webapp.ClientEventSetTool:
var err error
change, err = c.state.SetTool(string(data))
if err != nil {
log.Printf(err.Error())
change = nil
}

case webapp.ClientEventDownload:
ch := chan [][]common.Color(data)
ch <- c.state.GetCanvasClone()

case webapp.ClientEventUndo:
change = c.state.Undo()
}
change = c.handleWebClientEvent(e)
}

if change != nil {
Expand All @@ -119,6 +76,61 @@ func (c *Controller) do() {
}
}

func (c *Controller) handleWebClientEvent(e webapp.ClientEvent) *state.Change {
switch data := e.(type) {
case webapp.ClientEventRegistered:
id := uint64(data)
c.registered(id)

case webapp.ClientEventReset:
if data {
return c.state.Reset()
}

case webapp.ClientEventSetColor:
color := common.Color(data)
return c.state.SetColor(color)

case webapp.ClientEventSetTool:
var err error
change, err := c.state.SetTool(string(data))
if err != nil {
log.Printf(err.Error())
return nil
}
return change

case webapp.ClientEventDownload:
ch := chan [][]common.Color(data)
ch <- c.state.GetCanvasClone()

case webapp.ClientEventUndo:
return c.state.Undo()
}

return nil
}

func (c *Controller) handleJoystickEvent(je hat.Event) *state.Change {
switch je {
case hat.MoveUp:
return c.state.GoUp()

case hat.MoveLeft:
return c.state.GoLeft()

case hat.MoveDown:
return c.state.GoDown()

case hat.MoveRight:
return c.state.GoRight()

case hat.Pressed:
return c.state.Paint()
}
return nil
}

func (c *Controller) stop(signals chan os.Signal) {
c.hat.Stop()
<-c.joystickEvents // wait for the hat graceful shutdown
Expand Down
37 changes: 22 additions & 15 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const (
bucketName = "bucket"
)

const (
wightColor = common.Color(0xFFFFFF)
blackColor = common.Color(0)
backgroundColor = blackColor
)

type Canvas [][]common.Color

func (c Canvas) Clone() Canvas {
Expand Down Expand Up @@ -49,8 +55,8 @@ type State struct {
canvasWidth uint8
canvasHeight uint8
toolName string
tool tool
color common.Color
tools map[string]tool
}

func NewState(canvasWidth, canvasHeight uint8) *State {
Expand All @@ -59,12 +65,6 @@ func NewState(canvasWidth, canvasHeight uint8) *State {
canvasHeight: canvasHeight,
}

s.tools = map[string]tool{
penName: s.pen,
eraserName: s.eraser,
bucketName: s.bucket,
}

_ = s.Reset()

return s
Expand Down Expand Up @@ -95,8 +95,8 @@ func (s *State) Reset() *Change {
s.canvas = c
s.cursor = cr
s.window = win
s.color = 0xFFFFFF
s.toolName = penName
s.color = wightColor
_, _ = s.SetTool(penName)

return s.GetFullChange()
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func (s *State) GoRight() *Change {
}

func (s *State) Paint() *Change {
return s.tools[s.toolName]()
return s.tool()
}

func (s *State) setSinglePixelTool(color common.Color) *Change {
Expand All @@ -174,7 +174,7 @@ func (s *State) pen() *Change {
}

func (s *State) eraser() *Change {
return s.setSinglePixelTool(0)
return s.setSinglePixelTool(backgroundColor)
}

func (s State) getNeighbors(center Pixel) []Pixel {
Expand Down Expand Up @@ -289,14 +289,21 @@ func (s *State) SetColor(cl common.Color) *Change {
}

func (s *State) SetTool(toolName string) (*Change, error) {
if _, found := s.tools[toolName]; !found {
return nil, fmt.Errorf(`unknown tool "%s"`, toolName)
}

if toolName == s.toolName {
return nil, nil
}

switch toolName {
case penName:
s.tool = s.pen
case eraserName:
s.tool = s.eraser
case bucketName:
s.tool = s.bucket
default:
return nil, fmt.Errorf(`unknown tool "%s"`, toolName)
}

s.toolName = toolName
return &Change{
ToolName: toolName,
Expand Down
37 changes: 35 additions & 2 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func TestState_Undo(t *testing.T) {

func TestBucket(t *testing.T) {
s := NewState(8, 8)
s.canvas = [][]common.Color{
s.canvas = Canvas{
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 0},
Expand All @@ -381,7 +381,7 @@ func TestBucket(t *testing.T) {

s.cursor.Y = 4
s.cursor.X = 4
s.toolName = bucketName
_, _ = s.SetTool(bucketName)
s.color = 2
s.Paint()
expected := Canvas{
Expand Down Expand Up @@ -474,4 +474,37 @@ func TestBucket(t *testing.T) {
if !reflect.DeepEqual(expected, s.canvas) {
t.Errorf("should fill the square")
}

s.canvas = Canvas{
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
}

s.color = 6
change := s.Paint()

expected = Canvas{
{6, 6, 6, 6, 6, 6, 6, 6},
{6, 6, 6, 6, 6, 6, 6, 6},
{6, 6, 6, 6, 6, 6, 6, 6},
{6, 6, 6, 6, 6, 6, 6, 6},
{6, 6, 6, 6, 6, 6, 6, 6},
{6, 6, 6, 6, 6, 6, 6, 6},
{6, 6, 6, 6, 6, 6, 6, 6},
{6, 6, 6, 6, 6, 6, 6, 6},
}

if !reflect.DeepEqual(expected, s.canvas) {
t.Errorf("should fill the square")
}

if len(change.Pixels) != int(s.canvasWidth*s.canvasWidth) {
t.Errorf("Change is too big")
}
}
25 changes: 12 additions & 13 deletions webapp/index.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,18 @@

const cursorElement = document.getElementById(cursorID)
cursorElement.style.color = reverseColor(cursorElement.style.backgroundColor)
switch (tool) {
case "pen":
cursorElement.innerText = '+'
break
case "eraser":
cursorElement.innerText = 'x'
break
case "bucket":
cursorElement.innerText = 'o'
break
default:
cursorElement.innerText = '?'
}
cursorElement.innerText = function(tool) {
switch (tool) {
case "pen":
return '+'
case "eraser":
return 'x'
case "bucket":
return 'o'
default:
return '?'
}
}(tool)

if (tool === "pen") {
toolTypePen.checked = "checked"
Expand Down

0 comments on commit 9fc29e4

Please sign in to comment.