-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxqx_grid_layer.c
167 lines (131 loc) · 3.55 KB
/
xqx_grid_layer.c
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2006-2008 Ondrej 'SanTiago' Zajicek
* Copyright (c) 2021 Cyril Hrubis <[email protected]>
*/
#include "xqx_grid_layer.h"
#define ORDER(a, b, tmp) do { if (b < a) { tmp = a ; a = b; b = tmp; } } while (0)
#define VALS 3
static const int step_table[VALS] = {2, 5, 10};
static int step_exp = 10;
static void grid_notify(void *gr_i, struct xqx_view *vw, unsigned int change)
{
struct xqx_grid *gr = gr_i;
if ((change == XQX_VLC_INIT) || (change == XQX_VLC_SCALE)) {
int64_t dx = gr->dist;
dx *= vw->scale_cx;
dx *= vw->scale_main;
dx /= vw->scale_px;
dx = abs((int) dx);
int i;
int step_base = 16;
for (i = 16; i < dx; i *= step_exp)
step_base = i;
for (i = 0; (i < VALS) && (step_base * step_table[i] < dx); i++);
gr->step = step_base * step_table[i];
}
}
static void draw_coord(gp_pixmap *pixmap, int x, int y, int align, int coord)
{
char buf[16];
buf[15] = 0;
snprintf(buf, 15, "%d", coord/16000);
gp_text(pixmap, NULL, x+1, y+1, align, 0xffffff, 0, buf);
gp_text(pixmap, NULL, x, y, align, 0, 0, buf);
}
static void dashed_vline(gp_pixmap *pixmap, gp_coord x, gp_coord y0, gp_coord y1, gp_pixel color)
{
gp_size len = y1 - y0, i;
for (i = 0; i <= len/10; i++) {
if (i%2)
gp_vline(pixmap, x, y0 + 10*(i-1), y0 + 10*i, color);
}
}
static void dashed_hline(gp_pixmap *pixmap, gp_coord x0, gp_coord x1, gp_coord y, gp_pixel color)
{
gp_size len = x1 - x0, i;
for (i = 0; i <= len/10; i++) {
if (i%2)
gp_hline(pixmap, x0 + 10*(i-1), x0 + 10*i, y, color);
}
}
static void grid_render(void *gr_i, struct xqx_view *vw, gp_pixmap *pixmap, struct xqx_rectangle *rect)
{
struct xqx_grid *gr = gr_i;
struct xqx_coordinate lc, hc;
unsigned int i;
xqx_view_pixels_to_coords(vw, rect->lx, rect->ly, &lc);
xqx_view_pixels_to_coords(vw, rect->hx, rect->hy, &hc);
lc.x /= gr->step;
lc.y /= gr->step;
hc.x /= gr->step;
hc.y /= gr->step;
ORDER(lc.x, hc.x, i);
ORDER(lc.y, hc.y, i);
lc.x--;
lc.y--;
hc.x++;
hc.y++;
gp_pixel color = gp_rgb_to_pixmap_pixel(0x22, 0x22, 0xaa, pixmap);
for (i = lc.x; i <= hc.x; i++) {
int64_t tmp = i;
tmp *= gr->step;
tmp -= vw->center.x;
tmp *= vw->scale_px;
tmp /= vw->scale_cx;
tmp /= vw->scale_main;
tmp += vw->w / 2;
if (i % 5)
dashed_vline(pixmap, tmp, rect->ly, rect->hy, color);
else
gp_vline(pixmap, tmp, rect->ly, rect->hy, color);
}
for (i = lc.y; i <= hc.y; i++) {
int64_t tmp = i;
tmp *= gr->step;
tmp -= vw->center.y;
tmp *= vw->scale_py;
tmp /= vw->scale_cy;
tmp /= vw->scale_main;
tmp += vw->h / 2;
if (i % 5)
dashed_hline(pixmap, rect->lx, rect->hx, tmp, color);
else
gp_hline(pixmap, rect->lx, rect->hx, tmp, color);
}
for (i = lc.x; i <= hc.x; i++) {
int coord = i * gr->step;
if (coord % 16000 == 0) {
int64_t tmp = coord;
tmp -= vw->center.x;
tmp *= vw->scale_px;
tmp /= vw->scale_cx;
tmp /= vw->scale_main;
tmp += vw->w / 2;
draw_coord(pixmap, (int) tmp, 1, GP_ALIGN_CENTER | GP_VALIGN_BELOW, coord);
}
}
for (i = lc.y; i <= hc.y; i++) {
int coord = i * gr->step;
if (coord % 16000 == 0) {
int64_t tmp = coord;
tmp -= vw->center.y;
tmp *= vw->scale_py;
tmp /= vw->scale_cy;
tmp /= vw->scale_main;
tmp += vw->h / 2;
draw_coord(pixmap, 1, (int) tmp, GP_ALIGN_RIGHT | GP_VALIGN_CENTER, coord);
}
}
}
struct xqx_grid *xqx_make_grid(void)
{
struct xqx_grid *gr;
gr = calloc(1, sizeof(struct xqx_grid));
if (!gr)
return NULL;
gr->common.notify_cb = grid_notify;
gr->common.render_cb = grid_render;
gr->dist = 60;
return gr;
}