Update StateVars only when its widget reports a change (#42)

This commit is contained in:
Alexander Bondarenko 2021-03-12 14:03:54 +03:00 committed by GitHub
parent ebd5286e1c
commit b0337eb084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -163,6 +163,8 @@ module DearImGui
where where
-- base -- base
import Control.Monad
( when )
import Data.Bool import Data.Bool
import Data.Coerce import Data.Coerce
( coerce ) ( coerce )
@ -412,8 +414,9 @@ checkbox label ref = liftIO do
changed <- withCString label \labelPtr -> changed <- withCString label \labelPtr ->
(0 /=) <$> [C.exp| bool { Checkbox($(char* labelPtr), $(bool* boolPtr)) } |] (0 /=) <$> [C.exp| bool { Checkbox($(char* labelPtr), $(bool* boolPtr)) } |]
newValue <- peek boolPtr when changed do
ref $=! (newValue == 1) newValue <- peek boolPtr
ref $=! (newValue == 1)
return changed return changed
@ -484,8 +487,9 @@ dragFloat desc ref speed minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { DragFloat( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { DragFloat( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |]
newValue <- peek floatPtr when changed do
ref $=! realToFrac newValue newValue <- peek floatPtr
ref $=! realToFrac newValue
return changed return changed
where where
@ -503,8 +507,9 @@ dragFloat2 desc ref speed minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { DragFloat2( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { DragFloat2( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |]
[x', y'] <- peekArray 2 floatPtr when changed do
ref $=! (realToFrac x', realToFrac y') [x', y'] <- peekArray 2 floatPtr
ref $=! (realToFrac x', realToFrac y')
return changed return changed
where where
@ -522,8 +527,9 @@ dragFloat3 desc ref speed minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { DragFloat3( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { DragFloat3( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |]
[x', y', z'] <- peekArray 3 floatPtr when changed do
ref $=! (realToFrac x', realToFrac y', realToFrac z') [x', y', z'] <- peekArray 3 floatPtr
ref $=! (realToFrac x', realToFrac y', realToFrac z')
return changed return changed
where where
@ -541,8 +547,9 @@ dragFloat4 desc ref speed minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { DragFloat4( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { DragFloat4( $(char* descPtr), $(float *floatPtr), $(float speed'), $(float min'), $(float max')) } |]
[x', y', z', u'] <- peekArray 4 floatPtr when changed do
ref $=! (realToFrac x', realToFrac y', realToFrac z', realToFrac u') [x', y', z', u'] <- peekArray 4 floatPtr
ref $=! (realToFrac x', realToFrac y', realToFrac z', realToFrac u')
return changed return changed
where where
@ -560,8 +567,9 @@ sliderFloat desc ref minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { SliderFloat( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { SliderFloat( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |]
newValue <- peek floatPtr when changed do
ref $=! realToFrac newValue newValue <- peek floatPtr
ref $=! realToFrac newValue
return changed return changed
where where
@ -578,8 +586,9 @@ sliderFloat2 desc ref minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { SliderFloat2( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { SliderFloat2( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |]
[x', y'] <- peekArray 2 floatPtr when changed do
ref $=! (realToFrac x', realToFrac y') [x', y'] <- peekArray 2 floatPtr
ref $=! (realToFrac x', realToFrac y')
return changed return changed
where where
@ -596,8 +605,9 @@ sliderFloat3 desc ref minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { SliderFloat3( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { SliderFloat3( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |]
[x', y', z'] <- peekArray 3 floatPtr when changed do
ref $=! (realToFrac x', realToFrac y', realToFrac z') [x', y', z'] <- peekArray 3 floatPtr
ref $=! (realToFrac x', realToFrac y', realToFrac z')
return changed return changed
where where
@ -614,8 +624,9 @@ sliderFloat4 desc ref minValue maxValue = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { SliderFloat4( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |] (0 /=) <$> [C.exp| bool { SliderFloat4( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |]
[x', y', z', u'] <- peekArray 4 floatPtr when changed do
ref $=! (realToFrac x', realToFrac y', realToFrac z', realToFrac u') [x', y', z', u'] <- peekArray 4 floatPtr
ref $=! (realToFrac x', realToFrac y', realToFrac z', realToFrac u')
return changed return changed
where where
@ -648,8 +659,9 @@ colorPicker3 desc ref = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /= ) <$> [C.exp| bool { ColorPicker3( $(char* descPtr), $(float *refPtr) ) } |] (0 /= ) <$> [C.exp| bool { ColorPicker3( $(char* descPtr), $(float *refPtr) ) } |]
[x', y', z'] <- peekArray 3 refPtr when changed do
ref $=! ImVec3 (realToFrac x') (realToFrac y') (realToFrac z') [x', y', z'] <- peekArray 3 refPtr
ref $=! ImVec3 (realToFrac x') (realToFrac y') (realToFrac z')
return changed return changed
@ -664,8 +676,9 @@ colorButton desc ref = liftIO do
changed <- withCString desc \descPtr -> changed <- withCString desc \descPtr ->
(0 /=) <$> [C.exp| bool { ColorButton( $(char* descPtr), *$(ImVec4 *refPtr) ) } |] (0 /=) <$> [C.exp| bool { ColorButton( $(char* descPtr), *$(ImVec4 *refPtr) ) } |]
newValue <- peek refPtr when changed do
ref $=! newValue newValue <- peek refPtr
ref $=! newValue
return changed return changed