ucmd_view.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /* view: image viewer built on the img library and the ngui toolkit.
  2. *
  3. * Usage: view <file>. The command owns a canvas window, reads the whole file
  4. * through the kernel file service, decodes it into an exact-size malloc arena,
  5. * and draws it with fit/1:1/zoom/pan support. */
  6. #include <stdlib.h>
  7. #include "ucmd_lib.h"
  8. #include "ngui.h"
  9. #include "img.h"
  10. #include "img_file.h"
  11. #define VIEW_FILE_CAP (IMG_FILE_MAX + 1u)
  12. #define VIEW_HEAP_MAX (32u * 1024u * 1024u)
  13. #define VIEW_TOOLBAR_H 28
  14. #define VIEW_STATUS_H 18
  15. #define VIEW_BAR_H (VIEW_TOOLBAR_H + VIEW_STATUS_H)
  16. #define VIEW_ID_FIT 1
  17. #define VIEW_ID_ONE 2
  18. #define VIEW_ID_OUT 3
  19. #define VIEW_ID_IN 4
  20. #define VIEW_BG NGUI_RGB(24, 26, 32)
  21. #define VIEW_VIEW_BG NGUI_RGB(16, 17, 21)
  22. #define VIEW_CHK1 NGUI_RGB(46, 48, 54)
  23. #define VIEW_CHK2 NGUI_RGB(58, 60, 67)
  24. struct viewer {
  25. struct ngui ui;
  26. struct img_image img;
  27. struct img_mem mem;
  28. unsigned char *file;
  29. unsigned char *arena;
  30. unsigned int file_size;
  31. unsigned int iw, ih;
  32. unsigned int format;
  33. unsigned int zoom; /* 16.16 when fit == 0 */
  34. int fit;
  35. int off_x, off_y; /* destination top-left inside the viewport */
  36. int dragging;
  37. int drag_x, drag_y;
  38. int have_img;
  39. int loading;
  40. int error;
  41. unsigned int gif_frames, gif_index, gif_delay, gif_next_ms;
  42. int last_w, last_h;
  43. char err[64];
  44. char name[20];
  45. char title[20];
  46. int dirty;
  47. };
  48. static struct viewer v;
  49. static unsigned int s_len(const char *s)
  50. {
  51. unsigned int n = 0;
  52. while (s && s[n])
  53. n++;
  54. return n;
  55. }
  56. static void s_copy(char *d, const char *s, unsigned int cap)
  57. {
  58. unsigned int i = 0;
  59. if (!cap)
  60. return;
  61. while (s && s[i] && i + 1 < cap) {
  62. d[i] = s[i];
  63. i++;
  64. }
  65. d[i] = '\0';
  66. }
  67. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  68. {
  69. while (s && *s && *n + 1 < cap)
  70. d[(*n)++] = *s++;
  71. d[*n] = '\0';
  72. }
  73. static void s_append_uint(char *d, unsigned int *n, unsigned int v,
  74. unsigned int cap)
  75. {
  76. char tmp[12];
  77. unsigned int i = 0, j;
  78. do {
  79. tmp[i++] = (char)('0' + v % 10);
  80. v /= 10;
  81. } while (v && i < sizeof(tmp));
  82. for (j = 0; j < i && *n + 1 < cap; j++)
  83. d[(*n)++] = tmp[i - 1 - j];
  84. d[*n] = '\0';
  85. }
  86. static void base_name(const char *path, char *out, unsigned int cap)
  87. {
  88. unsigned int n = 0, i, start = 0;
  89. if (!path)
  90. path = "";
  91. for (i = 0; path[i]; i++)
  92. if (path[i] == '/')
  93. start = i + 1;
  94. for (i = start; path[i] && n + 1 < cap; i++)
  95. out[n++] = path[i];
  96. out[n] = '\0';
  97. if (!n)
  98. s_copy(out, "image", cap);
  99. }
  100. struct ngui_rect view_body(struct ngui *ui)
  101. {
  102. struct ngui_rect r = {0, 0, ui->w, ui->h};
  103. struct ngui_rect top, bottom;
  104. top = ngui_cut_top(&r, ui->h >= 140 ? VIEW_TOOLBAR_H : 22);
  105. bottom = ngui_cut_bottom(&r, ui->h >= 140 ? VIEW_STATUS_H : 14);
  106. (void)top;
  107. (void)bottom;
  108. return r;
  109. }
  110. static unsigned int view_scale(struct ngui *ui)
  111. {
  112. struct ngui_rect b = view_body(ui);
  113. unsigned int s;
  114. if (!v.have_img || b.w <= 0 || b.h <= 0)
  115. return 65536;
  116. if (!v.fit)
  117. return v.zoom ? v.zoom : 65536;
  118. s = img_fit_scale(v.iw, v.ih, (unsigned int)b.w, (unsigned int)b.h);
  119. if (s > 65536)
  120. s = 65536; /* don't enlarge a smaller image */
  121. return s ? s : 256;
  122. }
  123. static void clamp_view(struct ngui *ui, unsigned int scale)
  124. {
  125. struct ngui_rect b = view_body(ui);
  126. unsigned int sw, sh;
  127. if (!v.have_img) {
  128. v.off_x = v.off_y = 0;
  129. return;
  130. }
  131. sw = (unsigned int)(((unsigned long long)v.iw * scale) >> 16);
  132. sh = (unsigned int)(((unsigned long long)v.ih * scale) >> 16);
  133. if (sw < 1) sw = 1;
  134. if (sh < 1) sh = 1;
  135. if (sw <= (unsigned int)b.w)
  136. v.off_x = ((int)b.w - (int)sw) / 2;
  137. else {
  138. if (v.off_x > 0) v.off_x = 0;
  139. if (v.off_x < (int)b.w - (int)sw)
  140. v.off_x = (int)b.w - (int)sw;
  141. }
  142. if (sh <= (unsigned int)b.h)
  143. v.off_y = ((int)b.h - (int)sh) / 2;
  144. else {
  145. if (v.off_y > 0) v.off_y = 0;
  146. if (v.off_y < (int)b.h - (int)sh)
  147. v.off_y = (int)b.h - (int)sh;
  148. }
  149. if (v.fit) {
  150. v.off_x = ((int)b.w - (int)sw) / 2;
  151. v.off_y = ((int)b.h - (int)sh) / 2;
  152. }
  153. }
  154. static void view_set_scale(struct ngui *ui, unsigned int scale)
  155. {
  156. struct ngui_rect b = view_body(ui);
  157. unsigned int old = view_scale(ui);
  158. unsigned int sx, sy;
  159. int dx, dy;
  160. if (!v.have_img || b.w <= 0 || b.h <= 0)
  161. return;
  162. if (scale < 256) scale = 256;
  163. if (scale > 8u * 65536u) scale = 8u * 65536u;
  164. dx = b.w / 2 - v.off_x;
  165. dy = b.h / 2 - v.off_y;
  166. sx = ((unsigned int)dx << 16) / old;
  167. sy = ((unsigned int)dy << 16) / old;
  168. v.fit = 0;
  169. v.zoom = scale;
  170. v.off_x = (int)(b.w / 2 - (int)(((unsigned long long)sx * scale) >> 16));
  171. v.off_y = (int)(b.h / 2 - (int)(((unsigned long long)sy * scale) >> 16));
  172. clamp_view(ui, scale);
  173. }
  174. static void view_fit(void)
  175. {
  176. v.fit = 1;
  177. }
  178. static void view_one(struct ngui *ui)
  179. {
  180. struct ngui_rect b = view_body(ui);
  181. unsigned int sw;
  182. v.fit = 0;
  183. v.zoom = 65536;
  184. sw = v.iw;
  185. v.off_x = ((int)b.w - (int)sw) / 2;
  186. v.off_y = ((int)b.h - (int)v.ih) / 2;
  187. clamp_view(ui, v.zoom);
  188. }
  189. static void view_zoom_step(struct ngui *ui, int in)
  190. {
  191. unsigned int s = view_scale(ui);
  192. if (in)
  193. s = s + s / 4;
  194. else
  195. s = s - s / 5;
  196. view_set_scale(ui, s);
  197. }
  198. static void format_status(char *buf, unsigned int cap)
  199. {
  200. unsigned int n = 0;
  201. buf[0] = '\0';
  202. if (v.have_img) {
  203. unsigned int scale = view_scale(&v.ui);
  204. s_append(buf, &n, img_format_name(v.format), cap);
  205. s_append(buf, &n, " ", cap);
  206. s_append_uint(buf, &n, v.iw, cap);
  207. s_append(buf, &n, "x", cap);
  208. s_append_uint(buf, &n, v.ih, cap);
  209. s_append(buf, &n, " ", cap);
  210. if (v.file_size >= 1024 * 1024) {
  211. s_append_uint(buf, &n, v.file_size / (1024 * 1024), cap);
  212. s_append(buf, &n, " MB", cap);
  213. } else {
  214. s_append_uint(buf, &n, (v.file_size + 1023) / 1024, cap);
  215. s_append(buf, &n, " KB", cap);
  216. }
  217. s_append(buf, &n, " ", cap);
  218. if (v.fit)
  219. s_append(buf, &n, "fit ", cap);
  220. s_append_uint(buf, &n, (scale * 100u) >> 16, cap);
  221. s_append(buf, &n, "%", cap);
  222. } else {
  223. s_append(buf, &n, "view ", cap);
  224. s_append(buf, &n, v.name, cap);
  225. }
  226. }
  227. static void draw_message(struct ngui *ui, struct ngui_rect body,
  228. const char *msg)
  229. {
  230. int tw = (int)ngui_text_width_ui(ui, msg);
  231. int x = body.x + (body.w - tw) / 2;
  232. int y = body.y + body.h / 2 - 8;
  233. if (x < body.x + 6)
  234. x = body.x + 6;
  235. ngui_text(ui, x, y, msg, ui->theme.text);
  236. if (!v.loading) {
  237. const char *hint = "Esc closes";
  238. int hw = (int)ngui_text_width_ui(ui, hint);
  239. ngui_text(ui, body.x + (body.w - hw) / 2, y + 24, hint,
  240. ui->theme.text_dim);
  241. }
  242. }
  243. static void draw_toolbar(struct ngui *ui, struct ngui_rect r)
  244. {
  245. struct ngui_rect b;
  246. int x = r.x + 6;
  247. int by = r.y + (r.h - 20) / 2;
  248. int bw;
  249. ngui_fill_rect(ui, r, ui->theme.panel);
  250. ngui_fill_rect(ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  251. ui->theme.border);
  252. bw = 40;
  253. b = (struct ngui_rect){x, by, bw, 20};
  254. if (ngui_button(ui, VIEW_ID_FIT, b, "Fit")) {
  255. view_fit();
  256. v.dirty = 1;
  257. }
  258. x += bw + 5;
  259. b = (struct ngui_rect){x, by, 40, 20};
  260. if (ngui_button(ui, VIEW_ID_ONE, b, "1:1")) {
  261. view_one(ui);
  262. v.dirty = 1;
  263. }
  264. x += 45;
  265. b = (struct ngui_rect){x, by, 24, 20};
  266. if (ngui_button(ui, VIEW_ID_OUT, b, "-")) {
  267. view_zoom_step(ui, 0);
  268. v.dirty = 1;
  269. }
  270. x += 29;
  271. b = (struct ngui_rect){x, by, 24, 20};
  272. if (ngui_button(ui, VIEW_ID_IN, b, "+")) {
  273. view_zoom_step(ui, 1);
  274. v.dirty = 1;
  275. }
  276. x += 32;
  277. if (v.have_img && x + 70 < r.x + r.w) {
  278. char z[16];
  279. unsigned int n = 0;
  280. z[0] = '\0';
  281. s_append_uint(z, &n, (view_scale(ui) * 100u) >> 16, sizeof(z));
  282. s_append(z, &n, "%", sizeof(z));
  283. ngui_text(ui, x, r.y + (r.h - 16) / 2, z, ui->theme.text_dim);
  284. }
  285. }
  286. static void draw(struct ngui *ui)
  287. {
  288. struct ngui_rect all = {0, 0, ui->w, ui->h};
  289. struct ngui_rect r = all;
  290. struct ngui_rect toolbar, status, body;
  291. char line[96];
  292. toolbar = ngui_cut_top(&r, ui->h >= 140 ? VIEW_TOOLBAR_H : 22);
  293. status = ngui_cut_bottom(&r, ui->h >= 140 ? VIEW_STATUS_H : 14);
  294. body = r;
  295. ngui_fill_rect(ui, all, VIEW_BG);
  296. draw_toolbar(ui, toolbar);
  297. ngui_fill_rect(ui, status, ui->theme.panel);
  298. format_status(line, sizeof(line));
  299. ngui_text_ellipsis(ui, status, status.x + 6,
  300. status.y + (status.h - 16) / 2, line,
  301. ui->theme.text_dim);
  302. if (body.w <= 0 || body.h <= 0)
  303. return;
  304. ngui_fill_rect(ui, body, VIEW_VIEW_BG);
  305. if (!v.have_img) {
  306. if (v.error)
  307. draw_message(ui, body, v.err);
  308. else {
  309. char msg[48];
  310. s_copy(msg, "Loading ", sizeof(msg));
  311. {
  312. unsigned int n = s_len(msg);
  313. s_append(msg, &n, v.name, sizeof(msg));
  314. s_append(msg, &n, "...", sizeof(msg));
  315. }
  316. draw_message(ui, body, msg);
  317. }
  318. return;
  319. }
  320. {
  321. unsigned int scale = view_scale(ui);
  322. unsigned int sw =
  323. (unsigned int)(((unsigned long long)v.iw * scale) >> 16);
  324. unsigned int sh =
  325. (unsigned int)(((unsigned long long)v.ih * scale) >> 16);
  326. struct img_blit blit;
  327. if (!sw) sw = 1;
  328. if (!sh) sh = 1;
  329. clamp_view(ui, scale);
  330. blit.dst = ui->px;
  331. blit.dst_w = ui->w;
  332. blit.dst_h = ui->h;
  333. blit.clip_x = body.x;
  334. blit.clip_y = body.y;
  335. blit.clip_w = body.w;
  336. blit.clip_h = body.h;
  337. blit.x = body.x + v.off_x;
  338. blit.y = body.y + v.off_y;
  339. blit.scale = scale;
  340. blit.bg = VIEW_CHK1;
  341. blit.bg2 = VIEW_CHK2;
  342. blit.flags = IMG_BLIT_CHECKER;
  343. if (v.dragging)
  344. blit.flags |= IMG_BLIT_FAST;
  345. if (v.img.alpha)
  346. img_draw(&v.img, &blit);
  347. else {
  348. blit.flags &= ~IMG_BLIT_CHECKER;
  349. img_draw(&v.img, &blit);
  350. }
  351. (void)sw;
  352. (void)sh;
  353. }
  354. }
  355. static int create_window(unsigned int base_w, unsigned int base_h,
  356. const char *title, struct win_info *wi)
  357. {
  358. unsigned int w = base_w, h = base_h;
  359. if (w < 320) w = 320;
  360. if (h < 200) h = 200;
  361. if (w > 960) w = 960;
  362. if (h > 640) h = 640;
  363. for (;;) {
  364. if (u_win_create(w, h + VIEW_BAR_H, title, wi) == 0)
  365. return 0;
  366. if (w <= 240 && h <= 160)
  367. break;
  368. w = w * 4 / 5;
  369. h = h * 4 / 5;
  370. if (w < 240) w = 240;
  371. if (h < 160) h = 160;
  372. }
  373. return -1;
  374. }
  375. static void error_exit(const char *msg)
  376. {
  377. struct win_info wi;
  378. struct ngui ui;
  379. int quit = 0;
  380. if (u_win_create(480, 240 + VIEW_BAR_H, "view", &wi) != 0) {
  381. u_puts("view: ");
  382. u_puts(msg);
  383. u_puts("\n");
  384. u_exit(1);
  385. }
  386. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  387. ngui_font_attach(&ui);
  388. v.error = 1;
  389. s_copy(v.err, msg, sizeof(v.err));
  390. v.have_img = 0;
  391. for (;;) {
  392. if (!ngui_begin(&ui)) {
  393. u_sleep_ms(10);
  394. continue;
  395. }
  396. {
  397. int k = ngui_key(&ui);
  398. if (k == NGUI_KEY_ESC || k == 'q' || k == 0x03)
  399. quit = 1;
  400. }
  401. draw(&ui);
  402. ngui_end(&ui);
  403. if (quit)
  404. break;
  405. u_sleep_ms(10);
  406. }
  407. u_win_close();
  408. u_exit(1);
  409. }
  410. void _start(const char *arg)
  411. {
  412. struct img_info info;
  413. struct win_info wi;
  414. struct img_options opt = {0};
  415. struct sys_stat st;
  416. unsigned int n, fcap, est;
  417. int r;
  418. if (!arg || !arg[0]) {
  419. u_puts("usage: view <file>\n");
  420. u_exit(1);
  421. }
  422. base_name(arg, v.name, sizeof(v.name));
  423. s_copy(v.title, "view ", sizeof(v.title));
  424. {
  425. unsigned int tn = s_len(v.title);
  426. s_append(v.title, &tn, v.name, sizeof(v.title));
  427. }
  428. fcap = VIEW_FILE_CAP;
  429. if (u_stat(arg, &st) == 0 && !st.is_dir && st.size > 0 &&
  430. st.size < VIEW_FILE_CAP)
  431. fcap = st.size + 1u;
  432. v.file = (unsigned char *)malloc(fcap);
  433. if (!v.file)
  434. error_exit("Not enough memory for the image");
  435. n = (unsigned int)img_read_file(arg, v.file, fcap);
  436. if ((int)n < 0) {
  437. if ((int)n == IMG_ERR_TOOBIG)
  438. error_exit("Image file is larger than 4 MB");
  439. error_exit(img_error_string((int)n));
  440. }
  441. v.file_size = n;
  442. r = img_sniff(v.file, n, &info);
  443. if (r != 0)
  444. error_exit(img_error_string(r));
  445. est = img_estimate(&info);
  446. if (est == 0xFFFFFFFFu || est > VIEW_HEAP_MAX ||
  447. (unsigned long long)n + est > VIEW_HEAP_MAX)
  448. error_exit("Image needs more than 32 MB");
  449. v.arena = (unsigned char *)malloc(est);
  450. if (!v.arena)
  451. error_exit("Not enough memory for the image");
  452. img_mem_init(&v.mem, v.arena, est);
  453. v.iw = info.width;
  454. v.ih = info.height;
  455. v.format = info.format;
  456. if (create_window(info.width, info.height, v.title, &wi) != 0) {
  457. u_puts("view: no graphics window\n");
  458. u_exit(1);
  459. }
  460. ngui_init(&v.ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  461. ngui_font_attach(&v.ui);
  462. v.fit = 1;
  463. v.zoom = 65536;
  464. v.loading = 1;
  465. v.dirty = 1;
  466. v.last_w = (int)wi.width;
  467. v.last_h = (int)wi.height;
  468. {
  469. struct ngui *ui = &v.ui;
  470. /* Draw the loading frame once; the input loop starts after decode. */
  471. ngui_begin(ui);
  472. draw(ui);
  473. ngui_end(ui);
  474. }
  475. opt.max_pixels = 0;
  476. r = img_decode(v.file, n, &opt, &v.mem, &v.img);
  477. v.loading = 0;
  478. if (r != 0) {
  479. s_copy(v.err, img_error_string(r), sizeof(v.err));
  480. v.error = 1;
  481. v.have_img = 0;
  482. } else {
  483. v.have_img = 1;
  484. v.error = 0;
  485. v.iw = v.img.w;
  486. v.ih = v.img.h;
  487. v.format = v.img.format;
  488. v.fit = 1;
  489. }
  490. if (v.have_img && v.format == IMG_FMT_GIF) {
  491. unsigned int frames = 0, delay = 100;
  492. int ha = 0;
  493. if (img_gif_frames(v.file, v.file_size, &frames, &delay, &ha) == 0 &&
  494. frames > 1) {
  495. v.gif_frames = frames;
  496. v.gif_index = 0;
  497. v.gif_delay = delay;
  498. v.gif_next_ms = u_ticks_ms() + delay;
  499. }
  500. }
  501. v.dirty = 1;
  502. for (;;) {
  503. struct ngui *ui = &v.ui;
  504. struct ngui_rect body = view_body(ui);
  505. int close = 0;
  506. int k;
  507. unsigned int now = u_ticks_ms();
  508. if (v.gif_frames > 1 &&
  509. (int)(now - v.gif_next_ms) >= 0) {
  510. unsigned int d = v.gif_delay;
  511. v.gif_index = (v.gif_index + 1) % v.gif_frames;
  512. if (img_decode_gif_frame(v.file, v.file_size,
  513. v.gif_index, &v.mem, &v.img,
  514. &d) == 0) {
  515. if (d < 20) d = 20;
  516. v.gif_delay = d;
  517. v.dirty = 1;
  518. } else {
  519. v.gif_frames = 0;
  520. }
  521. v.gif_next_ms = u_ticks_ms() + v.gif_delay;
  522. }
  523. ngui_begin(ui);
  524. if (ui->w != v.last_w || ui->h != v.last_h) {
  525. /* size changed; redraw at the new dimensions */
  526. v.last_w = ui->w;
  527. v.last_h = ui->h;
  528. v.dirty = 1;
  529. }
  530. while ((k = ngui_key(ui)) != 0) {
  531. if (k == NGUI_KEY_ESC || k == 'q' || k == 0x03) {
  532. close = 1;
  533. break;
  534. } else if (k == 'f' || k == '0') {
  535. view_fit();
  536. v.dirty = 1;
  537. } else if (k == '1') {
  538. view_one(ui);
  539. v.dirty = 1;
  540. } else if (k == '+' || k == '=') {
  541. view_zoom_step(ui, 1);
  542. v.dirty = 1;
  543. } else if (k == '-' || k == '_') {
  544. view_zoom_step(ui, 0);
  545. v.dirty = 1;
  546. } else if (k == NGUI_KEY_LEFT || k == NGUI_KEY_RIGHT ||
  547. k == NGUI_KEY_UP || k == NGUI_KEY_DOWN) {
  548. if (v.have_img) {
  549. if (v.fit)
  550. view_set_scale(ui, view_scale(ui));
  551. if (k == NGUI_KEY_LEFT)
  552. v.off_x += 32;
  553. if (k == NGUI_KEY_RIGHT)
  554. v.off_x -= 32;
  555. if (k == NGUI_KEY_UP)
  556. v.off_y += 32;
  557. if (k == NGUI_KEY_DOWN)
  558. v.off_y -= 32;
  559. clamp_view(ui, view_scale(ui));
  560. v.dirty = 1;
  561. }
  562. }
  563. }
  564. if (close)
  565. break;
  566. if (ui->pressed && ui->press_x >= body.x && ui->press_y >= body.y &&
  567. ui->press_x < body.x + body.w && ui->press_y < body.y + body.h &&
  568. v.have_img) {
  569. v.dragging = 1;
  570. v.drag_x = ui->mouse_x;
  571. v.drag_y = ui->mouse_y;
  572. v.dirty = 1;
  573. }
  574. if (v.dragging) {
  575. if (!(ui->buttons & 1) || ui->released) {
  576. v.dragging = 0;
  577. v.dirty = 1;
  578. } else {
  579. int dx = ui->mouse_x - v.drag_x;
  580. int dy = ui->mouse_y - v.drag_y;
  581. if (dx || dy) {
  582. v.off_x += dx;
  583. v.off_y += dy;
  584. v.drag_x = ui->mouse_x;
  585. v.drag_y = ui->mouse_y;
  586. clamp_view(ui, view_scale(ui));
  587. v.dirty = 1;
  588. }
  589. }
  590. }
  591. if (ui->pressed || ui->released)
  592. v.dirty = 1;
  593. if (v.dirty) {
  594. v.dirty = 0;
  595. draw(ui);
  596. ngui_end_ex(ui, 1);
  597. } else {
  598. ngui_end_ex(ui, 0);
  599. }
  600. u_sleep_ms(10);
  601. }
  602. u_win_close();
  603. u_exit(0);
  604. }