-
Notifications
You must be signed in to change notification settings - Fork 0
/
veta.c
269 lines (218 loc) · 5.87 KB
/
veta.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
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
/*
* Copyright (C) 2016 Richard Jansson
*
* This file is part of Veta.
*
* Veta 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.
*
* Veta 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 Veta. If not, see <http://www.gnu.org/licenses/>.
*/
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include "veta.h"
#include "debug.h"
#include "keyboard_io.h"
#include "ui.h"
#include "conf.h"
#include "render.h"
#include "jsonconf.h"
#define CONFIG_PATH "conf.json"
cell *root_cell=NULL;
/* Configuration options */
char *symbol_file=NULL;
sym_mode_t symbol_mode;
#ifdef WINDOWS
sel_mode_t selection_mode=ZOOM;
//sel_mode_t selection_mode=HIGHLIGHT;
#else
sel_mode_t selection_mode=ZOOM;
#endif
/* end of configuration options */
void usage(char *cmd){
printf("Usage: %s \n",cmd);
printf("\t--dump-symbols [file]\t dump symbols to file\n");
printf("\t--load-symbols [file]\t load symbols from file\n");
printf("\t--zoom\t Select cells by zooming\n");
printf("\t--highlight\t Select cells by highlighting\n");
exit(1);
}
void veta_exit(){
uk_log("veta exit!");
conf_save_position(lastX,lastY);
writestate(STATE_FILE,lastX,lastY);
// FIXME: Add proper scriptability support!
system("veta_exit");
debug_exit();
}
symbol *root = NULL;
void veta_handleevent(event_t *event){
switch(event->type){
case QUIT:
uk_log("Quit");
ui_quit();
break;
case RESET:
uk_log("Reset");
clear_selection(root_cell);
veta_render();
break;
case SELECT_CELL:
uk_log("select cell %i",event->cell);
// debug_print_tree(root);
select_cell(root_cell,event->cell);
symbol *sym;
if(NULL!=(sym=get_selected_symbol(root_cell))){
clear_selection(root_cell);
if(ui2_onselect_symbol(sym)) break;
sendkey(sym->data,1,0);
}
refresh();
break;
case UNDEFINED:
uk_log("got event UNDEFINED\n");
break;
default:
uk_log("WARNING undefined event type");
break;
}
}
int symbolsloaded=0;
void veta_symbolsloaded(symbol *symbols,int n){
if(n==0) return;
symbolsloaded++;
uk_log("Symbols are loaded got %i symbols",n);
conf_save_symbols("platform_symbols.json",symbols,n);
int cellsize=conf_get_int("n_columns",CELLS_W)*conf_get_int("n_rows",CELLS_H);
root_cell=create_cells(symbols,n,cellsize,TREE_DEPTH,1);
clear_selection(root_cell);
}
int render_cell(cell *cell,void *data){
assert(cell != NULL);
assert(data != NULL);
box *ob=(box *)data;
box nb;
assert(cell);
if(cell->level>1){
if(cell->symbol->name){
rgb fg={255,255,255};
rgb bg={0,0,0};
// FIXME this should handle an arbitrary level
int is_selected=cell->selected || (cell->parent && cell->parent->selected);
if(selection_mode==HIGHLIGHT) bg=is_selected?cell->color_selected:cell->color;
else if(ui2_is_selected(cell->symbol)) bg=cell->color_selected;
else bg=cell->color;
draw_text_box(cell->symbol->name,ob->w,ob->h,ob->x0,ob->y0,fg,bg);
}else{
}
} else {
// draw_box(WIDTH,HEIGHT,0,0,0,0,0);
}
int n_cols=conf_get_int("n_columns",CELLS_W);
int n_rows=conf_get_int("n_rows",CELLS_H);
int innerWidth=(ob->w/n_cols)*0.9;
int innerHeight=(ob->h/n_rows)*0.9;
nb.w=(ob->w/n_cols)*0.9;
nb.h=(ob->h/n_rows)*0.9;
int pleft=0.1*(ob->w/n_cols);
int ptop=0.1*(ob->h/n_rows);
for(int i=0;i<cell->nchildren ;i++){
nb.x0=nb.w*(i%n_cols)+ob->x0+pleft;
nb.y0=nb.h*(i/n_cols)+ob->y0+ptop;
render_cell(cell->children[i],&nb);
}
return 0;
}
void veta_render(){
box b;
b.x0=b.y0=0;
b.w=WIDTH;
b.h=HEIGHT;
draw_box(WIDTH,HEIGHT,0,0,0,0,0);
if(selection_mode==ZOOM){
cell *deepest=get_deepest_cell(root_cell);
render_cell(deepest,&b);
}else{
render_cell(root_cell,&b);
}
render_ui2();
// FIXME: Cleanup and and add configuration options
vo_screenshot("var not used!", WIDTH, HEIGHT);
#ifdef DEBUG
rgb black=(rgb){0,0,0};
rgb white=(rgb){0xff,0xff,0xff};
draw_text_box(BUILD,650,40,20,HEIGHT-45,white,black);
#endif
}
void veta_click(int x,int y){
}
#ifdef WINDOWS
int WINAPI WinMain(HINSTANCE h,HINSTANCE hprev,LPSTR cmdline,int n){
debug_init(LOG_FILE);
uk_log("windows build: %s",BUILD);
int full_throttle=0;
nCmdShow=n;
hinstance=h;
symbol_mode=LOAD;
// FIXME temporary debug shit
#else
int main(int argc,char *argv[]){
debug_init(LOG_FILE);
conf_init(CONFIG_PATH,veta_symbolsloaded);
uk_log("build: %s",BUILD);
int full_throttle=0;
symbol_mode=LOAD;
symbol_file=SYMBOLS_FILE;
for(int i=1;i<argc;i++){
if(!strcmp("--dump-symbols",argv[i])){
symbol_mode=DUMP;
i++;
if(i>=argc) usage(argv[0]);
symbol_file=argv[i];
uk_log("Dumping to %s\n",symbol_file);
}else if(!strcmp("--load-symbols",argv[i])){
symbol_mode=LOAD;
i++;
if(i>=argc) usage(argv[0]);
symbol_file=argv[i];
}else if(!strcmp("--zoom",argv[i])){
selection_mode=ZOOM;
}else if(!strcmp("--highlight",argv[i])){
selection_mode=HIGHLIGHT;
}else if(!strcmp("--graphicsopt",argv[i])){
full_throttle=1;
}else{
usage(argv[0]);
}
}
#endif
// Set up callbacks
ui_onevent(veta_handleevent);
ui_onclick(ui2_handle_click);
ui_onrelease(ui2_handle_release);
ui_haskeymap(veta_symbolsloaded);
ui_render(veta_render);
int x,y;
conf_read_position(&x,&y);
ui_init(conf_get_int("width",WIDTH),conf_get_int("height",HEIGHT),x,y);
if(!symbolsloaded){
fprintf(stderr,"Failed to load symbols\n");
exit(1);
}
// Set up low level graphics and load the keymap
// ui_init(WIDTH,HEIGHT,st->x,st->y);
ui2_init();
ui2_add_widgets();
ui_loop(full_throttle);
return 1;
}