interact.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <unistd.h>
  2. #include <grass/gis.h>
  3. #include <grass/display.h>
  4. #include "graphics.h"
  5. #include "colors.h"
  6. static int save_colors(char *, char *, struct Colors *);
  7. static int yes(char *, char *);
  8. #define XSCALE 2.0
  9. #define YSCALE 2.0
  10. #define WRITE_STATUS \
  11. Write_status(cur_red, cur_grn, cur_blu, shift_incr, at_cat, hi_mode)
  12. #define WRITE_CATS Write_cats(categories, at_cat)
  13. int
  14. interact(struct Categories *categories, struct Colors *colors, char *name,
  15. char *mapset)
  16. {
  17. char buffer[128];
  18. CELL at_cat, tmp;
  19. int hi_mode;
  20. int hi_save_mode;
  21. int shift_incr;
  22. int cur_char;
  23. int red_hi;
  24. int grn_hi;
  25. int blu_hi;
  26. int cur_red;
  27. int cur_grn;
  28. int cur_blu;
  29. int colors_changed;
  30. int quit;
  31. set_signals();
  32. red_hi = 0;
  33. grn_hi = 0;
  34. blu_hi = 0;
  35. hi_mode = 0;
  36. hi_save_mode = 0;
  37. colors_changed = 0;
  38. Rast_set_c_null_value(&at_cat, 1);
  39. Rast_get_c_color(&at_cat, &cur_red, &cur_grn, &cur_blu, colors);
  40. shift_incr = 10;
  41. Initialize_curses();
  42. WRITE_CATS;
  43. Write_menu();
  44. WRITE_STATUS;
  45. /* mark_category(at_cat, 1) ; */
  46. while (1) {
  47. R_flush();
  48. cur_char = getchar() & 0177;
  49. sprintf(buffer, " %c", cur_char);
  50. Write_message(2, buffer);
  51. switch (cur_char) {
  52. case '*':
  53. Replot_screen();
  54. break;
  55. case 'Q':
  56. quit = 1;
  57. if (colors_changed) {
  58. if (yes("Colors changed", "Save the changes? (y/n)"))
  59. quit = save_colors(name, mapset, colors);
  60. else
  61. quit = yes("Quit anyway? (y/n)", "");
  62. }
  63. if (quit) {
  64. Clear_message();
  65. Write_message(2, "Bye ");
  66. G_sleep(2);
  67. Close_curses();
  68. return (0);
  69. }
  70. break;
  71. case 'D':
  72. case 'U':
  73. case 'd':
  74. case 'u':
  75. if (hi_mode && !hi_save_mode) {
  76. Rast_get_c_color(&at_cat, &cur_red, &cur_grn, &cur_blu, colors);
  77. }
  78. /* tmark_category(at_cat, 0) ; */
  79. if (Rast_is_c_null_value(&at_cat))
  80. tmp = 0;
  81. else
  82. tmp = at_cat + 1;
  83. switch (cur_char) {
  84. case 'd':
  85. tmp++;
  86. break;
  87. case 'u':
  88. tmp += categories->num + 1;
  89. break;
  90. case 'D':
  91. tmp += 10;
  92. break;
  93. case 'U':
  94. if (categories->num > 10)
  95. tmp += categories->num - 9;
  96. else
  97. tmp += categories->num - (categories->num - 1);
  98. break;
  99. }
  100. tmp = tmp % (categories->num + 2); /* changed 11/99 M.N. */
  101. if (!tmp)
  102. Rast_set_c_null_value(&at_cat, 1);
  103. else
  104. at_cat = tmp - 1;
  105. if (hi_mode) {
  106. cur_red = red_hi;
  107. cur_grn = grn_hi;
  108. cur_blu = blu_hi;
  109. if (hi_save_mode) {
  110. Rast_set_c_color(at_cat, cur_red, cur_grn, cur_blu, colors);
  111. colors_changed = 1;
  112. }
  113. }
  114. else {
  115. Rast_get_c_color(&at_cat, &cur_red, &cur_grn, &cur_blu, colors);
  116. }
  117. WRITE_CATS;
  118. WRITE_STATUS;
  119. /* mark_category(at_cat, 1) ; */
  120. break;
  121. case 'r':
  122. case 'R':
  123. case 'g':
  124. case 'G':
  125. case 'b':
  126. case 'B':
  127. if (hi_mode) {
  128. switch (cur_char) {
  129. case 'r':
  130. red_hi = shift_color(red_hi, -shift_incr);
  131. break;
  132. case 'R':
  133. red_hi = shift_color(red_hi, shift_incr);
  134. break;
  135. case 'g':
  136. grn_hi = shift_color(grn_hi, -shift_incr);
  137. break;
  138. case 'G':
  139. grn_hi = shift_color(grn_hi, shift_incr);
  140. break;
  141. case 'b':
  142. blu_hi = shift_color(blu_hi, -shift_incr);
  143. break;
  144. case 'B':
  145. blu_hi = shift_color(blu_hi, shift_incr);
  146. break;
  147. }
  148. cur_red = red_hi;
  149. cur_grn = grn_hi;
  150. cur_blu = blu_hi;
  151. if (hi_save_mode) {
  152. Rast_set_c_color(at_cat, cur_red, cur_grn, cur_blu, colors);
  153. colors_changed = 1;
  154. }
  155. }
  156. else {
  157. Rast_get_c_color(&at_cat, &cur_red, &cur_grn, &cur_blu, colors);
  158. switch (cur_char) {
  159. case 'r':
  160. cur_red = shift_color(cur_red, -shift_incr);
  161. break;
  162. case 'R':
  163. cur_red = shift_color(cur_red, shift_incr);
  164. break;
  165. case 'g':
  166. cur_grn = shift_color(cur_grn, -shift_incr);
  167. break;
  168. case 'G':
  169. cur_grn = shift_color(cur_grn, shift_incr);
  170. break;
  171. case 'b':
  172. cur_blu = shift_color(cur_blu, -shift_incr);
  173. break;
  174. case 'B':
  175. cur_blu = shift_color(cur_blu, shift_incr);
  176. break;
  177. }
  178. Rast_set_c_color(at_cat, cur_red, cur_grn, cur_blu, colors);
  179. colors_changed = 1;
  180. }
  181. WRITE_STATUS;
  182. break;
  183. case 'i':
  184. shift_incr = shift_color(shift_incr, -1);
  185. WRITE_STATUS;
  186. break;
  187. case 'I':
  188. shift_incr = shift_color(shift_incr, 1);
  189. WRITE_STATUS;
  190. break;
  191. case '+':
  192. Rast_shift_c_colors(1, colors);
  193. if (hi_mode) {
  194. cur_red = red_hi;
  195. cur_grn = grn_hi;
  196. cur_blu = blu_hi;
  197. if (hi_save_mode)
  198. Rast_set_c_color(at_cat, cur_red, cur_grn, cur_blu, colors);
  199. }
  200. colors_changed = 1;
  201. WRITE_STATUS;
  202. break;
  203. case '-':
  204. Rast_shift_c_colors(-1, colors);
  205. if (hi_mode) {
  206. cur_red = red_hi;
  207. cur_grn = grn_hi;
  208. cur_blu = blu_hi;
  209. if (hi_save_mode)
  210. Rast_set_c_color(at_cat, cur_red, cur_grn, cur_blu, colors);
  211. }
  212. colors_changed = 1;
  213. WRITE_STATUS;
  214. break;
  215. case 'c': /* Writeout color lookup table */
  216. colors_changed = 0;
  217. save_colors(name, mapset, colors);
  218. break;
  219. case 't':
  220. Clear_message();
  221. Write_message(1, "toggling new color table...");
  222. table_toggle(name, mapset, colors);
  223. /*Clear_message() ; */
  224. if (hi_mode) {
  225. cur_red = red_hi;
  226. cur_grn = grn_hi;
  227. cur_blu = blu_hi;
  228. if (hi_save_mode)
  229. Rast_set_c_color(at_cat, cur_red, cur_grn, cur_blu, colors);
  230. }
  231. colors_changed = 1;
  232. break;
  233. case 'h':
  234. case 'H':
  235. if (hi_mode) {
  236. Rast_get_c_color(&at_cat, &cur_red, &cur_grn, &cur_blu, colors);
  237. hi_mode = 0;
  238. hi_save_mode = 0;
  239. }
  240. else {
  241. cur_red = red_hi;
  242. cur_grn = grn_hi;
  243. cur_blu = blu_hi;
  244. hi_mode = 1;
  245. if (cur_char == 'H') {
  246. Rast_set_c_color(at_cat, cur_red, cur_grn, cur_blu, colors);
  247. hi_save_mode = 1;
  248. colors_changed = 1;
  249. }
  250. }
  251. WRITE_STATUS;
  252. break;
  253. default:
  254. sprintf(buffer, " %c - Unknown Command", cur_char);
  255. Write_message(2, buffer);
  256. break;
  257. }
  258. }
  259. }
  260. int shift_color(int colr, int shift)
  261. {
  262. colr = colr + shift;
  263. if (colr < 0)
  264. colr = 0;
  265. if (colr > 255)
  266. colr = 255;
  267. return (colr);
  268. }
  269. static int yes(char *msg1, char *msg2)
  270. {
  271. int c;
  272. Clear_message();
  273. Write_message(1, msg1);
  274. Write_message(2, msg2);
  275. while (1) {
  276. c = getchar() & 0177;
  277. switch (c) {
  278. case 'y':
  279. case 'Y':
  280. Clear_message();
  281. return 1;
  282. case 'n':
  283. case 'N':
  284. Clear_message();
  285. return 0;
  286. }
  287. putchar('\7');
  288. }
  289. }
  290. static int save_colors(char *name, char *mapset, struct Colors *colors)
  291. {
  292. Clear_message();
  293. Write_message(2, "Writing color table ");
  294. Rast_write_colors(name, mapset, colors);
  295. Clear_message();
  296. return 1;
  297. }