File: status.cpp

package info (click to toggle)
golly 3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 20,176 kB
  • sloc: cpp: 72,638; ansic: 25,919; python: 7,921; sh: 4,245; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; javascript: 279; perl: 69
file content (258 lines) | stat: -rw-r--r-- 7,878 bytes parent folder | download | duplicates (3)
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
// This file is part of Golly.
// See docs/License.html for the copyright notice.

#include "bigint.h"
#include "lifealgo.h"

#include "utils.h"      // for Fatal, Beep
#include "prefs.h"      // for mindelay, maxdelay, etc
#include "algos.h"      // for algoinfo
#include "layer.h"      // for currlayer
#include "view.h"       // for nopattupdate, widescreen, PointInView, etc
#include "status.h"
#include <math.h>       // for fabs

#ifdef ANDROID_GUI
    #include "jnicalls.h"   // for UpdateStatus, GetRuleName
#endif

#ifdef WEB_GUI
    #include "webcalls.h"   // for UpdateStatus, GetRuleName
#endif

#ifdef IOS_GUI
    #import "PatternViewController.h"   // for UpdateStatus
    #import "RuleViewController.h"      // for GetRuleName
#endif

// -----------------------------------------------------------------------------

std::string status1;    // top line
std::string status2;    // middle line
std::string status3;    // bottom line

// prefixes used when widescreen is true:
const char* large_gen_prefix =   "Generation=";
const char* large_algo_prefix =  "    Algorithm=";
const char* large_rule_prefix =  "    Rule=";
const char* large_pop_prefix =   "    Population=";
const char* large_scale_prefix = "    Scale=";
const char* large_step_prefix =  "    ";
const char* large_xy_prefix =    "    XY=";

// prefixes used when widescreen is false:
const char* small_gen_prefix =   "Gen=";
const char* small_algo_prefix =  "   Algo=";
const char* small_rule_prefix =  "   Rule=";
const char* small_pop_prefix =   "   Pop=";
const char* small_scale_prefix = "   Scale=";
const char* small_step_prefix =  "   ";
const char* small_xy_prefix =    "   XY=";

static bigint currx, curry;     // cursor location in cell coords
static bool showxy = false;     // show cursor's XY location?

// -----------------------------------------------------------------------------

void UpdateStatusLines()
{
    std::string rule = currlayer->algo->getrule();

    status1 = "Pattern=";
    if (currlayer->dirty) {
        // display asterisk to indicate pattern has been modified
        status1 += "*";
    }
    status1 += currlayer->currname;
    status1 += widescreen ? large_algo_prefix : small_algo_prefix;
    status1 += GetAlgoName(currlayer->algtype);
    status1 += widescreen ? large_rule_prefix : small_rule_prefix;
    status1 += rule;

    // show rule name if one exists and is not same as rule
    // (best NOT to remove any suffix like ":T100,200" in case we allow
    // users to name "B3/S23:T100,200" as "Life on torus")
    std::string rulename = GetRuleName(rule);
    if (!rulename.empty() && rulename != rule) {
        status1 += " [";
        status1 += rulename;
        status1 += "]";
    }

    char scalestr[32];
    int mag = currlayer->view->getmag();
    if (mag < 0) {
        sprintf(scalestr, "2^%d:1", -mag);
    } else {
        sprintf(scalestr, "1:%d", 1 << mag);
    }

    char stepstr[32];
    if (currlayer->currexpo < 0) {
        // show delay in secs
        sprintf(stepstr, "Delay=%gs", (double)GetCurrentDelay() / 1000.0);
    } else {
        sprintf(stepstr, "Step=%d^%d", currlayer->currbase, currlayer->currexpo);
    }

    status2 = widescreen ? large_gen_prefix : small_gen_prefix;
    if (nopattupdate) {
        status2 += "0";
    } else {
        status2 += Stringify(currlayer->algo->getGeneration());
    }
    status2 += widescreen ? large_pop_prefix : small_pop_prefix;
    if (nopattupdate) {
        status2 += "0";
    } else {
        bigint popcount = currlayer->algo->getPopulation();
        if (popcount.sign() < 0) {
            // getPopulation returns -1 if it can't be calculated
            status2 += "?";
        } else {
            status2 += Stringify(popcount);
        }
    }
    status2 += widescreen ? large_scale_prefix : small_scale_prefix;
    status2 += scalestr;
    status2 += widescreen ? large_step_prefix : small_step_prefix;
    status2 += stepstr;
    status2 += widescreen ? large_xy_prefix : small_xy_prefix;
    #ifdef WEB_GUI
        // in web app we show the cursor's current cell location,
        // or nothing if the cursor is outside the viewport (ie. showxy is false)
        if (showxy) {
            bigint xo, yo;
            bigint xpos = currx;   xpos -= currlayer->originx;
            bigint ypos = curry;   ypos -= currlayer->originy;
            if (mathcoords) {
                // Y values increase upwards
                bigint temp = 0;
                temp -= ypos;
                ypos = temp;
            }
            status2 += Stringify(xpos);
            status2 += " ";
            status2 += Stringify(ypos);
        }
    #else
        // in iOS and Android apps we show location of the cell in middle of viewport
        status2 += Stringify(currlayer->view->x);
        status2 += " ";
        status2 += Stringify(currlayer->view->y);
    #endif
}

// -----------------------------------------------------------------------------

void ClearMessage()
{
    if (status3.length() == 0) return;    // no need to clear message

    status3.clear();
    UpdateStatus();
}

// -----------------------------------------------------------------------------

void DisplayMessage(const char* s)
{
    status3 = s;
    UpdateStatus();
}

// -----------------------------------------------------------------------------

void ErrorMessage(const char* s)
{
    Beep();
    DisplayMessage(s);
}

// -----------------------------------------------------------------------------

void SetMessage(const char* s)
{
    // set message string without displaying it
    status3 = s;
}

// -----------------------------------------------------------------------------

int GetCurrentDelay()
{
    int gendelay = mindelay * (1 << (-currlayer->currexpo - 1));
    if (gendelay > maxdelay) gendelay = maxdelay;
    return gendelay;
}

// -----------------------------------------------------------------------------

char* Stringify(const bigint& b)
{
    static char buf[32];
    char* p = buf;
    double d = b.todouble();
    if ( fabs(d) > 1000000000.0 ) {
        // use e notation for abs value > 10^9 (agrees with min & max_coord)
        sprintf(p, "%g", d);
    } else {
        // show exact value with commas inserted for readability
        if ( d < 0 ) {
            d = - d;
            *p++ = '-';
        }
        sprintf(p, "%.f", d);
        int len = (int)strlen(p);
        int commas = ((len + 2) / 3) - 1;
        int dest = len + commas;
        int src = len;
        p[dest] = 0;
        while (commas > 0) {
            p[--dest] = p[--src];
            p[--dest] = p[--src];
            p[--dest] = p[--src];
            p[--dest] = ',';
            commas--;
        }
        if ( p[-1] == '-' ) p--;
    }
    return p;
}

// -----------------------------------------------------------------------------

void CheckMouseLocation(int x, int y)
{
    // check if we need to update XY location in status bar
    bool mouse_in_grid = false;
    bigint xpos, ypos;
    if (PointInView(x, y)) {
        // get mouse location in cell coords
        pair<bigint, bigint> cellpos = currlayer->view->at(x, y);
        xpos = cellpos.first;
        ypos = cellpos.second;
        // check if xpos,ypos is inside grid (possibly bounded)
        mouse_in_grid = CellInGrid(xpos, ypos);
    }

    if (mouse_in_grid) {
        if ( xpos != currx || ypos != curry ) {
            // show new XY location
            currx = xpos;
            curry = ypos;
            showxy = true;
            UpdateStatus();
        } else if (!showxy) {
            // mouse moved from outside grid and back over currx,curry
            showxy = true;
            UpdateStatus();
        }
    } else {
        // mouse is outside grid so clear XY location if necessary
        if (showxy) {
            showxy = false;
            UpdateStatus();
        }
    }
}