Fix button not updating bug

This commit is contained in:
2022-06-14 18:47:11 +01:00
parent dafbd8a2da
commit 0957c79e58
10 changed files with 51 additions and 67 deletions
+11 -41
View File
@@ -32,12 +32,13 @@ makeButton col eff = defaultButton
drawSwitch :: Color -> Color -> Button -> SPic
drawSwitch col1 col2 bt
| _btState bt == BtOff = flick $ pi/4
| _btState bt == BtOff
= flick $ pi/4
| otherwise = flick (negate (pi/4))
where
flick a = ( mconcat
[ colorSH col1 . translateSHz 10 . upperPrismPoly 10 $ rectNSEW (-2) (-5) 10 (-10)
, colorSH col2 . translateSH (V3 0 (-2) 15) . rotateSH a . upperPrismPoly 2 $ rectNSEW 10 0 2 (-2)
[ colorSH col1 . upperPrismPoly 20 $ rectNSEW (-2) (-5) 10 (-10)
, colorSH col2 . translateSH (V3 0 (-2) 20) . rotateSH a . upperPrismPoly 2 $ rectNSEW 10 0 2 (-2)
]
, mempty)
@@ -64,50 +65,19 @@ makeSwitchSPic dswitch effOn effOff = defaultButton
, _btState = BtOff
}
where
flipSwitch b w = switchEffect b . soundFromGeneral (LeverSound 0) (bpos b) click1S Nothing $ w
flipSwitch b = switchEffect b . soundFromGeneral (LeverSound 0) (bpos b) click1S Nothing
bpos b w = _btPos $ _buttons w IM.! _btID b
switchEffect b = case _btState b of
BtOff -> effOn . over buttons (IM.adjust turnOn (_btID b))
BtOn -> effOff . over buttons (IM.adjust turnOff (_btID b))
BtOff -> effOn . (buttons . ix (_btID b) %~ turnOn )
BtOn -> effOff . (buttons . ix (_btID b) %~ turnOff)
_ -> error "Trying to switch a button with no label"
turnOn :: Button -> Button
turnOn bt = bt
{ _btState = BtOn
, _btText = "SWITCH\\"
}
turnOff :: Button -> Button
turnOff bt = bt
{ _btState = BtOff
, _btText = "SWITCH/"
}
turnOn = (btState .~ BtOn ) . (btText .~ "SWITCH\\")
turnOff = (btState .~ BtOff) . (btText .~ "SWITCH/")
makeSwitch
makeSwitch
:: Color
-> Color
-> (World -> World) -- ^ Switch on effect
-> (World -> World) -- ^ Switch off effect
-> Button
makeSwitch col1 col2 effOn effOff = defaultButton
{ _btPict = drawSwitch col1 col2
, _btEvent = flipSwitch
, _btText = "SWITCH/"
, _btState = BtOff
}
where
flipSwitch b w = switchEffect b . soundFromGeneral (LeverSound 0) (bpos b) click1S Nothing $ w
bpos b w = _btPos $ _buttons w IM.! _btID b
switchEffect b = case _btState b of
BtOff -> effOn . over buttons (IM.adjust turnOn (_btID b))
BtOn -> effOff . over buttons (IM.adjust turnOff (_btID b))
_ -> error "Trying to switch a button with no label"
turnOn :: Button -> Button
turnOn bt = bt
{ _btState = BtOn
, _btText = "SWITCH\\"
}
turnOff :: Button -> Button
turnOff bt = bt
{ _btState = BtOff
, _btText = "SWITCH/"
}
makeSwitch col1 col2 f1 = (btColor .~ col1) . makeSwitchSPic (drawSwitch col1 col2) f1