-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate-computed.lua
More file actions
150 lines (121 loc) · 3.9 KB
/
state-computed.lua
File metadata and controls
150 lines (121 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
local ennui = require("ennui")
local State = ennui.State
local Size = ennui.Size
local StackPanel = ennui.Widgets.Stackpanel
local HorizontalStackPanel = ennui.Widgets.Horizontalstackpanel
local Text = ennui.Widgets.Text
local Slider = ennui.Widgets.Slider
local Rectangle = ennui.Widgets.Rectangle
local Window = ennui.Widgets.Window
-- Single reactive value that drives every derived display.
local state = State({
celsius = 20
})
local celsiusBinding = state:bind("celsius")
local fahrenheit = celsiusBinding:map(function(c) return c * 9 / 5 + 32 end)
local kelvin = celsiusBinding:map(function(c) return c + 273.15 end)
local celsiusLabel = celsiusBinding:format("%.1f °C")
local fahrenheitLabel = fahrenheit:format("%.1f °F")
local kelvinLabel = kelvin:format("%.1f K")
local feelLabel = celsiusBinding:map(function(c)
if c < 0 then
return "Freezing"
elseif c < 10 then
return "Cold"
elseif c < 20 then
return "Cool"
elseif c < 30 then
return "Warm"
else
return "Hot"
end
end)
local barWidthComputed = celsiusBinding:map(function(c)
return Size.percent(math.max(0, math.min(1, (c + 40) / 140)))
end)
local barColorComputed = celsiusBinding:map(function(c)
-- TODO: interpolate colour
if c < 0 then
return { 0.3, 0.4, 1.0, 1 }
elseif c < 20 then
return { 0.3, 0.7, 0.9, 1 }
elseif c < 30 then
return { 1.0, 0.7, 0.2, 1 }
else
return { 1.0, 0.3, 0.1, 1 }
end
end)
local host = ennui.Widgets.Host():setSize(love.graphics.getDimensions())
local window = Window("Computed Properties")
:setSize(440, Size.auto())
:setPosition(80, 60)
local panel = StackPanel()
:setSpacing(14)
:setPadding(14)
:setSize(Size.fill(), Size.auto())
local slider = Slider(-40, 100, 20)
:setSize(Size.fill(), 24)
:setFillColor(0.4, 0.5, 0.9)
slider:watch("value", function(val)
state.props.celsius = val
end)
local rangeRow = HorizontalStackPanel()
:setSize(Size.fill(), Size.auto())
rangeRow:addChild(Text("-40 °C"):setColor(0.5, 0.5, 0.5))
rangeRow:addChild(
Text("100 °C")
:setColor(0.5, 0.5, 0.5)
:setTextHorizontalAlignment("right")
:setSize(Size.fill(), Size.auto())
)
local function TemperatureCard(title, labelComputed, r, g, b)
local cardStack = StackPanel()
:setSize(Size.fill(), Size.auto())
:setSpacing(4)
cardStack:addChild(
Text(title)
:setColor(r, g, b)
:setTextHorizontalAlignment("center")
)
cardStack:addChild(
Text()
:setColor(1, 1, 1)
:setTextHorizontalAlignment("center")
:bindTo("text", labelComputed)
)
return cardStack
end
local displayRow = HorizontalStackPanel()
:setSpacing(10)
:setSize(Size.fill(), Size.auto())
displayRow:addChild(TemperatureCard("Celsius", celsiusLabel, 1.0, 0.75, 0.3))
displayRow:addChild(TemperatureCard("Fahrenheit", fahrenheitLabel, 0.4, 0.8, 1.0))
displayRow:addChild(TemperatureCard("Kelvin", kelvinLabel, 0.6, 1.0, 0.6))
local temperatureBar = Rectangle()
:setSize(Size.fill(), 22)
:setColor(0.12, 0.12, 0.18)
:setRadius(4)
local temperatureBarFill = Rectangle()
:setPreferredWidth(barWidthComputed)
:setPreferredHeight(Size.fill())
:setRadius(4)
:setHorizontalAlignment("left")
:bindTo("color", barColorComputed)
temperatureBar:addChild(temperatureBarFill)
local feelRow = HorizontalStackPanel()
:setSpacing(8)
:setSize(Size.fill(), Size.auto())
feelRow:addChild(Text("Feel:"):setColor(0.55, 0.55, 0.55))
feelRow:addChild(
Text()
:setColor(1, 1, 1)
:bindTo("text", feelLabel)
)
panel:addChild(slider)
panel:addChild(rangeRow)
panel:addChild(displayRow)
panel:addChild(temperatureBar)
panel:addChild(feelRow)
window:setContent(panel)
host:addChild(window)
return host