-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorchview.cpp
360 lines (313 loc) · 11.2 KB
/
torchview.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2017-2024 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <luz@plan44.ch>
//
// This file is part of p44lrgraphics.
//
// p44lrgraphics is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44lrgraphics is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44lrgraphics. If not, see <http://www.gnu.org/licenses/>.
//
#include "torchview.hpp"
using namespace p44;
// MARK: ===== TorchView
static ViewRegistrar r(TorchView::staticTypeName(), &TorchView::newInstance);
TorchView::TorchView() :
mNextCalculation(Never)
{
mFlameMin = 100;
mFlameMax = 220;
mFlameHeight = 1;
mSparkProbability = 2;
mSparkMin = 200;
mSparkMax = 255;
mSparkTfr = 40;
mSparkCap = 200;
mUpRad = 40;
mSideRad = 35;
mHeatCap = 0;
mHotsparkMin = 250;
mHotsparkColor = { 170, 170, 250, 255 };
mHotsparkColorInc = { 0, 0, 5, 255 };
mForegroundColor = { 180, 145, 0, 255 }; // foreground is "energy" color, which scales up with energy of the spark
mCycleTime = 25*MilliSecond;
clear();
}
TorchView::~TorchView()
{
}
void TorchView::clear()
{
stopAnimations();
// clear and resize to current size
mTorchDots.clear();
mTorchDots.resize(mContent.dy*mContent.dx, { torch_passive, 0, 0 });
}
TorchView::TorchDot& TorchView::dot(PixelPoint aPt)
{
int dotIdx = aPt.y*mContent.dx+aPt.x;
if (dotIdx>=mTorchDots.size()) clear(); // safety, resize on access outside range
return mTorchDots.at(dotIdx);
}
void TorchView::geometryChanged(PixelRect aOldFrame, PixelRect aOldContent)
{
if (aOldContent.dx!=mContent.dx || aOldContent.dy!=mContent.dy) {
clear(); // size changed: clear and resize dots vector
}
inherited::geometryChanged(aOldFrame, aOldContent);
}
void TorchView::recalculateColoring()
{
calculateGradient(256);
inherited::recalculateColoring();
}
MLMicroSeconds TorchView::step(MLMicroSeconds aPriorityUntil, MLMicroSeconds aNow)
{
MLMicroSeconds nextCall = inherited::step(aPriorityUntil, aNow);
if (mCycleTime!=Never && aNow>=mNextCalculation) {
mNextCalculation = aNow+mCycleTime;
calculateCycle();
}
updateNextCall(nextCall, aNow+mCycleTime);
return nextCall;
}
static uint16_t random(uint16_t aMinOrMax, uint16_t aMax = 0)
{
if (aMax==0) {
aMax = aMinOrMax;
aMinOrMax = 0;
}
uint32_t r = aMinOrMax;
aMax = aMax - aMinOrMax + 1;
r += rand() % aMax;
return r;
}
void TorchView::calculateCycle()
{
if (mAlpha==0) return; // don't waste cycles
PixelPoint dotpos;
// random flame
for (dotpos.y=0; dotpos.y<mFlameHeight; dotpos.y++) {
for (dotpos.x=0; dotpos.x<mContent.dx; dotpos.x++) {
TorchDot& d = dot(dotpos);
d.previous = random(mFlameMin, mFlameMax);
d.mode = torch_nop;
}
}
// random sparks in next row
dotpos.y = mFlameHeight;
for (dotpos.x=0; dotpos.x<mContent.dx; dotpos.x++) {
TorchDot& d = dot(dotpos);
if (d.mode!=torch_spark && random(100)<mSparkProbability) {
d.previous = random(mSparkMin, mSparkMax);
d.mode = torch_spark;
}
}
// let sparks fly and fade
for (dotpos.y=0; dotpos.y<mContent.dy; dotpos.y++) {
for (dotpos.x=0; dotpos.x<mContent.dx; dotpos.x++) {
TorchDot& d = dot(dotpos);
uint8_t e = d.previous;
EnergyMode m = d.mode;
switch (m) {
case torch_spark: {
// loose transfer-up-energy as long as there is any
reduce(e, mSparkTfr);
// cell above is temp spark, sucking up energy from this cell until empty
if (dotpos.y<mContent.dy-1) {
TorchDot& above = dot({ dotpos.x, dotpos.y+1 });
above.mode = torch_spark_temp;
}
break;
}
case torch_spark_temp: {
// just getting some energy from below
TorchDot& below = dot({ dotpos.x, dotpos.y-1 });
uint8_t e2 = below.previous;
if (e2<mSparkTfr) {
// cell below is exhausted, becomes passive
below.mode = torch_passive;
// gobble up rest of energy
increase(e, e2);
// loose some overall energy
e = ((int)e*mSparkCap)>>8;
// this cell becomes active spark
d.mode = torch_spark;
}
else {
increase(e, mSparkTfr);
}
break;
}
case torch_passive: {
e = ((int)e*mHeatCap)>>8;
increase(e, (
(((int)dot({dotpos.x>0 ? dotpos.x-1 : mContent.dx-1, dotpos.y}).previous+(int)dot({dotpos.x<mContent.dx-1 ? dotpos.x+1 : 0, dotpos.y}).previous)*mSideRad)>>9) +
(((int)dot({dotpos.x, dotpos.y-1}).previous*mUpRad)>>8)
);
}
default:
break;
}
d.current = e;
}
}
// transfer new
for (TorchDotVector::iterator pos=mTorchDots.begin(); pos!=mTorchDots.end(); ++pos) {
pos->previous = pos->current;
}
makeDirty();
}
#define NEWCOLORING 1
#if !NEWCOLORING
static const uint8_t energymap[32] = {0, 64, 96, 112, 128, 144, 152, 160, 168, 176, 184, 184, 192, 200, 200, 208, 208, 216, 216, 224, 224, 224, 232, 232, 232, 240, 240, 240, 240, 248, 248, 248};
#endif
PixelColor TorchView::contentColorAt(PixelPoint aPt)
{
PixelColor pix = transparent;
if (mAlpha>0 && isInContentSize(aPt)) {
TorchDot& d = dot(aPt);
int extraHeat = d.current-mHotsparkMin;
if (extraHeat>=0) {
pix = mHotsparkColor;
addToPixel(pix, dimmedPixel(mHotsparkColorInc, 255*(int)extraHeat/(255-mHotsparkMin)));
}
else {
if (d.current>0) {
#if NEWCOLORING
pix = gradientPixel(255-d.current, false);
#else
// energy to brightness is non-linear
pix.a = 255; // opaque
uint8_t eb = energymap[d.current>>3];
pix = { 10, 0, 0, 255 };
addToPixel(pix, dimmedPixel(foregroundColor, eb));
#endif
}
}
}
return pix;
}
#if ENABLE_VIEWCONFIG && !ENABLE_P44SCRIPT
// MARK: ===== view configuration
ErrorPtr TorchView::configureView(JsonObjectPtr aViewConfig)
{
JsonObjectPtr o;
ErrorPtr err = inherited::configureView(aViewConfig);
if (Error::isOK(err)) {
// flame parameters
if (aViewConfig->get("flame_min", o)) setFlameMin(o->int32Value());
if (aViewConfig->get("flame_max", o)) setFlameMax(o->int32Value());
if (aViewConfig->get("flame_height", o)) setFlameHeight(o->int32Value());
// spark generation parameters
if (aViewConfig->get("spark_probability", o)) setSparkProbability(o->int32Value());
if (aViewConfig->get("spark_min", o)) setSparkMin(o->int32Value());
if (aViewConfig->get("spark_max", o)) setSparkMax(o->int32Value());
// spark development parameters
if (aViewConfig->get("spark_tfr", o)) setSparkTfr(o->int32Value());
if (aViewConfig->get("spark_cap", o)) setSparkCap(o->int32Value());
if (aViewConfig->get("up_rad", o)) setUpRad(o->int32Value());
if (aViewConfig->get("side_rad", o)) setSideRad(o->int32Value());
if (aViewConfig->get("heat_cap", o)) setHeatCap(o->int32Value());
// hot spark coloring parameters
if (aViewConfig->get("hotspark_min", o)) setHotsparkMin(o->int32Value());
if (aViewConfig->get("hotsparkcolor", o)) setHotsparkColor(o->stringValue());
if (aViewConfig->get("hotsparkinc", o)) setHotsparkColorInc(o->stringValue());
// timing parameter
if (aViewConfig->get("cycletime", o)) setCycleTimeS(o->doubleValue());
}
return err;
}
#endif // ENABLE_VIEWCONFIG && !ENABLE_P44SCRIPT
#if ENABLE_VIEWSTATUS && !ENABLE_P44SCRIPT
JsonObjectPtr TorchView::viewStatus()
{
JsonObjectPtr status = inherited::viewStatus();
status->add("flame_min", JsonObject::newInt32(getFlameMin()));
status->add("flame_max", JsonObject::newInt32(getFlameMax()));
status->add("flame_height", JsonObject::newInt32(getFlameHeight()));
status->add("spark_probability", JsonObject::newInt32(getSparkProbability()));
status->add("spark_min", JsonObject::newInt32(getSparkMin()));
status->add("spark_max", JsonObject::newInt32(getSparkMax()));
status->add("spark_tfr", JsonObject::newInt32(getSparkTfr()));
status->add("spark_cap", JsonObject::newInt32(getSparkCap()));
status->add("up_rad", JsonObject::newInt32(getUpRad()));
status->add("side_rad", JsonObject::newInt32(getSideRad()));
status->add("heat_cap", JsonObject::newInt32(getHeatCap()));
status->add("hotspark_min", JsonObject::newInt32(getHotsparkMin()));
status->add("hotsparkcolor", JsonObject::newString(getHotsparkColor()));
status->add("hotsparkinc", JsonObject::newString(getHotsparkColorInc()));
status->add("cycletime", JsonObject::newDouble(getCycleTimeS()));
return status;
}
#endif // ENABLE_VIEWSTATUS && !ENABLE_P44SCRIPT
#if ENABLE_P44SCRIPT
using namespace P44Script;
ScriptObjPtr TorchView::newViewObj()
{
// base class with standard functionality
return new TorchViewObj(this);
}
#define ACCESSOR_CLASS TorchView
static ScriptObjPtr property_accessor(BuiltInMemberLookup& aMemberLookup, ScriptObjPtr aParentObj, ScriptObjPtr aObjToWrite, const struct BuiltinMemberDescriptor* aMemberDescriptor)
{
ACCFN_DEF
TorchViewPtr view = reinterpret_cast<ACCESSOR_CLASS*>(reinterpret_cast<TorchViewObj*>(aParentObj.get())->torch().get());
ACCFN acc = reinterpret_cast<ACCFN>(aMemberDescriptor->memberAccessInfo);
view->announceChanges(true);
ScriptObjPtr res = acc(*view, aObjToWrite);
view->announceChanges(false);
return res;
}
ACC_IMPL_INT(FlameMin)
ACC_IMPL_INT(FlameMax)
ACC_IMPL_INT(FlameHeight)
ACC_IMPL_INT(SparkProbability)
ACC_IMPL_INT(SparkMin)
ACC_IMPL_INT(SparkMax)
ACC_IMPL_INT(SparkTfr)
ACC_IMPL_INT(SparkCap)
ACC_IMPL_INT(UpRad)
ACC_IMPL_INT(SideRad)
ACC_IMPL_INT(HeatCap)
ACC_IMPL_INT(HotsparkMin)
ACC_IMPL_STR(HotsparkColor)
ACC_IMPL_STR(HotsparkColorInc)
ACC_IMPL_DBL(CycleTimeS)
static const BuiltinMemberDescriptor torchMembers[] = {
// property accessors
ACC_DECL("flame_min", numeric|lvalue, FlameMin),
ACC_DECL("flame_max", numeric|lvalue, FlameMax),
ACC_DECL("flame_height", numeric|lvalue, FlameHeight),
ACC_DECL("spark_probability", numeric|lvalue, SparkProbability),
ACC_DECL("spark_min", numeric|lvalue, SparkMin),
ACC_DECL("spark_max", numeric|lvalue, SparkMax),
ACC_DECL("spark_tfr", numeric|lvalue, SparkTfr),
ACC_DECL("spark_cap", numeric|lvalue, SparkCap),
ACC_DECL("up_rad", numeric|lvalue, UpRad),
ACC_DECL("side_rad", numeric|lvalue, SideRad),
ACC_DECL("heat_cap", numeric|lvalue, HeatCap),
ACC_DECL("hotspark_min", numeric|lvalue, HotsparkMin),
ACC_DECL("hotsparkcolor", numeric|lvalue, HotsparkColor),
ACC_DECL("hotsparkinc", numeric|lvalue, HotsparkColorInc),
ACC_DECL("cycletime", numeric|lvalue, CycleTimeS),
{ NULL } // terminator
};
static BuiltInMemberLookup* sharedTorchMemberLookupP = NULL;
TorchViewObj::TorchViewObj(P44ViewPtr aView) :
inherited(aView)
{
registerSharedLookup(sharedTorchMemberLookupP, torchMembers);
}
#endif // ENABLE_P44SCRIPT