forked from sam-itt/sofis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathladder-page.c
130 lines (111 loc) · 3.97 KB
/
ladder-page.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
/*
* SPDX-FileCopyrightText: 2021 Samuel Cuella <samuel.cuella@gmail.com>
*
* This file is part of SoFIS - an open source EFIS
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL_surface.h"
#include "generic-layer.h"
#include "ladder-page.h"
#include "sdl-colors.h"
#include "SDL_pcf.h"
LadderPageDescriptor *ladder_page_descriptor_init(LadderPageDescriptor *self, ScrollType direction, float page_size, float vstep, float vsubstep, LPInitFunc func)
{
self->direction = direction;
self->page_size = page_size;
self->vstep = vstep;
self->vsubstep = vsubstep;
self->offset = NAN;
self->init_page = func;
return self;
}
/*TODO: Check if ppv could be intergrated into the descriptor*/
void ladder_page_descriptor_compute_offset(LadderPageDescriptor *self, float ppv)
{
float half_split;
int leading, trailing;
float pixel_increment;
pixel_increment = ppv * self->vstep; /*How many pixels between two ruler marks*/
half_split = (pixel_increment-1)/2.0;
if(self->direction == TOP_DOWN){ /*0 is at the top of the image, increments downwards*/
leading = ceil(half_split);
self->offset = -1.0 * leading*ppv;
}else{ /*0 is at the bottom of the image, increments upwards*/
trailing = floor(half_split);
self->offset = -1.0 * trailing/ppv;
}
}
LadderPage *ladder_page_new(float start, LadderPageDescriptor *descriptor)
{
LadderPage *self;
self = calloc(1, sizeof(LadderPage));
if(self){
VERTICAL_STRIP(self)->start = start;
VERTICAL_STRIP(self)->end = NAN;
self->descriptor = descriptor;
}
return self;
}
void ladder_page_free(LadderPage *self)
{
vertical_strip_dispose(VERTICAL_STRIP(self));
free(self);
}
int ladder_page_get_index(LadderPage *self)
{
return ceil(VERTICAL_STRIP(self)->start/(self->descriptor->page_size));
}
/*TODO: inline*/
float ladder_page_resolve_value(LadderPage *self, float value)
{
VerticalStrip *strip;
bool reverse;
strip = VERTICAL_STRIP(self);
reverse = (self->descriptor->direction == BOTTUM_UP);
if(fmod(value, self->descriptor->vstep) == 0){ /*Value is a big graduation*/
float y;
value = fmod(value, fabs(round(strip->end - strip->start)) + 1);
int ngrads = value/self->descriptor->vstep;
if(!reverse)
y = self->descriptor->fei + ngrads * strip->ppv * self->descriptor->vstep;
else
y = self->descriptor->fei - ngrads * strip->ppv * self->descriptor->vstep;
return y;
}else if(self->descriptor->vsubstep != 0 && fmod(value, self->descriptor->vsubstep) == 0){ /*Value is a small graduation*/
float y;
value = fmod(value, fabs(round(strip->end - strip->start)) + 1);
int ngrads = value/self->descriptor->vsubstep;
if(!reverse)
y = self->descriptor->fei + ngrads * strip->ppv * self->descriptor->vsubstep;
else
y = self->descriptor->fei - ngrads * strip->ppv * self->descriptor->vsubstep;
return y;
}else{
return vertical_strip_resolve_value(strip, value, reverse);
}
}
/*Put markings*/
void ladder_page_etch_markings(LadderPage *self, PCF_Font *font)
{
float y;
VerticalStrip *strip;
GenericLayer *layer;
int page_index;
strip = VERTICAL_STRIP(self);
layer = GENERIC_LAYER(self);
page_index = ladder_page_get_index(self);
// printf("Page %d real range is [%f, %f]\n",page_index, strip->start, strip->end);
//
// printf("Writing indexes on %d starting at %d to %f\n",page_index, page_index*self->descriptor->page_size, strip->end);
Uint32 white = SDL_UWHITE(layer->canvas);
for(int i = page_index*self->descriptor->page_size; i <= strip->end; i += self->descriptor->vstep){
y = ladder_page_resolve_value(self, i);
PCF_FontWriteNumberAt(font,
&i, TypeInt, 0,
white, layer->canvas,
(generic_layer_w(layer)-1) - 10 - 5, y, LeftToCol | CenterOnRow);
}
}