comparison window/button.cc @ 52:15a18fbe6f21

* Known bugs added to the README * Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author thib
date Sat, 18 Apr 2009 18:35:39 +0000
parents 4d7486cb20a9
children 4416cfac86ae
comparison
equal deleted inserted replaced
51:cbb301016a4e 52:15a18fbe6f21
346 static char* extract_button(int number, int& width, int& height) { 346 static char* extract_button(int number, int& width, int& height) {
347 int size; 347 int size;
348 char* data; 348 char* data;
349 349
350 switch(number) { 350 switch(number) {
351 case 0: data = buttonleft; size = buttonleft_cnt; width = 164; height = 110; break; 351 case 0:
352 case 1: data = buttonright; size = buttonright_cnt; width = 164; height = 110; break; 352 data = buttonleft;
353 case 2: data = buttonup; size = buttonup_cnt; width = 110; height = 164; break; 353 size = buttonleft_cnt;
354 case 3: data = buttondown; size = buttondown_cnt; width = 110; height = 164; break; 354 width = 164;
355 default: return 0; 355 height = 110;
356 break;
357 case 1:
358 data = buttonright;
359 size = buttonright_cnt;
360 width = 164;
361 height = 110;
362 break;
363 case 2:
364 data = buttonup;
365 size = buttonup_cnt;
366 width = 110;
367 height = 164;
368 break;
369 case 3:
370 data = buttondown;
371 size = buttondown_cnt;
372 width = 110;
373 height = 164;
374 break;
375 default:
376 return NULL;
356 } 377 }
357 378
358 char* out = new char[width*height]; 379 char* out = new char[width*height];
359 int outc = 0; 380 int outc = 0;
360 int i; 381 int i;
386 int hc, hd, l, s; // h,l,s 系。h 要素は上の c_min|mid|maxにも入る 407 int hc, hd, l, s; // h,l,s 系。h 要素は上の c_min|mid|maxにも入る
387 ButtonColor(int _r, int _g, int _b); 408 ButtonColor(int _r, int _g, int _b);
388 void SetBrightness1(int n, int c1); 409 void SetBrightness1(int n, int c1);
389 void SetBrightness(int c1, int c2, int c3, int c4, int c5); 410 void SetBrightness(int c1, int c2, int c3, int c4, int c5);
390 }; 411 };
412
391 ButtonColor::ButtonColor(int r, int g, int b) { 413 ButtonColor::ButtonColor(int r, int g, int b) {
392 is_gray = false; 414 is_gray = false;
393 if (r == g && r == b) { 415 if (r == g && r == b) {
394 is_gray = true; 416 is_gray = true;
395 l = r; 417 l = r;
434 hd = max-min; 456 hd = max-min;
435 hc = mid - min; 457 hc = mid - min;
436 if (l < 128) s = (hd*255)/(max+min); 458 if (l < 128) s = (hd*255)/(max+min);
437 else s = (hd*255) / (510-(max+min)); 459 else s = (hd*255) / (510-(max+min));
438 } 460 }
461
439 void ButtonColor::SetBrightness1(int n, int c1) { 462 void ButtonColor::SetBrightness1(int n, int c1) {
440 if (n < 1 || n > 7) return; 463 if (n < 1 || n > 7) return;
441 as[n] = 255; 464 as[n] = 255;
442 if (is_gray) { 465 if (is_gray) {
443 rs[n] = c1 * l / 256; 466 rs[n] = c1 * l / 256;
455 m1 = light*2 - m2; 478 m1 = light*2 - m2;
456 479
457 c_max[n] = m2; 480 c_max[n] = m2;
458 c_mid[n] = (hc*m2 + (hd-hc)*m1) / hd; 481 c_mid[n] = (hc*m2 + (hd-hc)*m1) / hd;
459 c_min[n] = m1; 482 c_min[n] = m1;
460 483 }
461 return; 484
462 }
463 void ButtonColor::SetBrightness(int c1, int c2, int c3, int c4, int c5) { 485 void ButtonColor::SetBrightness(int c1, int c2, int c3, int c4, int c5) {
464 rs[0] = bs[0] = gs[0] = as[0] = 0; 486 rs[0] = bs[0] = gs[0] = as[0] = 0;
465 SetBrightness1(1, c1); 487 SetBrightness1(1, c1);
466 SetBrightness1(2, c2); 488 SetBrightness1(2, c2);
467 SetBrightness1(3, c3); 489 SetBrightness1(3, c3);
468 SetBrightness1(4, c4); 490 SetBrightness1(4, c4);
469 SetBrightness1(5, c5); 491 SetBrightness1(5, c5);
470 } 492 }
471 493
472 #include"rect.h" 494 #include "rect.h"
473 #include<stdlib.h> 495 #include <stdlib.h>
474 #include<string.h> 496 #include <string.h>
475 497
476 static void draw_button(char* rdata, int width, int height, char* bdata, int bwidth, int bheight, const ButtonColor& color) { 498 static void draw_button(char* rdata, int width, int height, char* bdata, int bwidth, int bheight, const ButtonColor& color) {
477 const int* rs = color.rs; 499 const int* rs = color.rs;
478 const int* gs = color.gs; 500 const int* gs = color.gs;
479 const int* bs = color.bs; 501 const int* bs = color.bs;
481 503
482 struct ScaleData {int r,g,b,a,c;}; 504 struct ScaleData {int r,g,b,a,c;};
483 ScaleData* data = new ScaleData[width*height]; 505 ScaleData* data = new ScaleData[width*height];
484 memset(data, 0, sizeof(ScaleData)*width*height); 506 memset(data, 0, sizeof(ScaleData)*width*height);
485 507
486 int bx,by; 508 int bx, by;
487 int x=0, y=0; 509 int x = 0, y = 0;
488 int x2 = 0, y2 = 0; 510 int x2 = 0, y2 = 0;
489 int xadd = width*65536/bwidth; 511 int xadd = width*65536/bwidth;
490 int yadd = height*65536/bheight; 512 int yadd = height*65536/bheight;
491 513
492 // 元のビットマップに着色しながら縮小する 514 // 元のビットマップに着色しながら縮小する
520 rd += 4; 542 rd += 4;
521 d++; 543 d++;
522 } 544 }
523 } 545 }
524 delete[] data; 546 delete[] data;
525 return; 547 }
526 } 548
527 char* create_button(int number, int& width, int& height, int r, int g, int b) { 549 char* create_button(int number, int& width, int& height, int r, int g, int b) {
528 int bwidth, bheight; 550 int bwidth, bheight;
529 char* bdata = extract_button(number, bwidth, bheight); 551 char* bdata = extract_button(number, bwidth, bheight);
530 if (bdata == 0) return 0; 552 if (bdata == NULL) return NULL;
531 // 拡大率に合わせてwidth,heightをセット 553 // 拡大率に合わせてwidth,heightをセット
532 if (width == -1 && height == -1) width = bwidth, height = bheight; 554 if (width == -1 && height == -1) width = bwidth, height = bheight;
533 else if (width == -1) width = bwidth * height / bheight; 555 else if (width == -1) width = bwidth * height / bheight;
534 else if (height == -1) height = bheight * width / bwidth; 556 else if (height == -1) height = bheight * width / bwidth;
535 if (width > bwidth) width = bwidth; 557 if (width > bwidth) width = bwidth;
548 return rdata; 570 return rdata;
549 } 571 }
550 572
551 static void drawbox(char* buf, const Rect& region, int width, int r, int g, int b, int a) { 573 static void drawbox(char* buf, const Rect& region, int width, int r, int g, int b, int a) {
552 buf += region.ty*width*4 + region.lx*4; 574 buf += region.ty*width*4 + region.lx*4;
553 int i,j; 575 int i, j;
554 int h = region.height(); 576 int h = region.height();
555 int w = region.width(); 577 int w = region.width();
556 int col = (b&0xff) | ((g&0xff)<<8)| ((r&0xff)<<16)| ((a&0xff)<<24); 578 int col = (b&0xff) | ((g&0xff)<<8)| ((r&0xff)<<16)| ((a&0xff)<<24);
557 for (i=0; i<h; i++) { 579 for (i=0; i<h; i++) {
558 char* c = buf; 580 char* c = buf;
560 *(int*)c = col; 582 *(int*)c = col;
561 c += 4; 583 c += 4;
562 } 584 }
563 buf += width*4; 585 buf += width*4;
564 } 586 }
565 return; 587 }
566 } 588
567 static void draw_box(char* buf, int width, int height, int kage_w1, int kage_w2, const ButtonColor& color) { 589 static void draw_box(char* buf, int width, int height, int kage_w1, int kage_w2, const ButtonColor& color) {
568 const int* rs = color.rs; 590 const int* rs = color.rs;
569 const int* gs = color.gs; 591 const int* gs = color.gs;
570 const int* bs = color.bs; 592 const int* bs = color.bs;
571 const int* as = color.as; 593 const int* as = color.as;
585 } 607 }
586 if (width > 4) { 608 if (width > 4) {
587 drawbox(buf, Rect(width-kage_w2, kage_w2, width-kage_w1, height-kage_w1), width, rs[2], gs[2], bs[2], as[2]); 609 drawbox(buf, Rect(width-kage_w2, kage_w2, width-kage_w1, height-kage_w1), width, rs[2], gs[2], bs[2], as[2]);
588 } 610 }
589 } 611 }
612
590 char* create_box(int& width, int& height, int r, int g, int b) { 613 char* create_box(int& width, int& height, int r, int g, int b) {
591 ButtonColor color(r,g,b); 614 ButtonColor color(r,g,b);
592 // scale のカーソル 615 // scale のカーソル
593 if (width == -1 && height == -1) width = 32, height = 48; 616 if (width == -1 && height == -1) width = 32, height = 48;
594 else if (width == -1) width = height*3/2; 617 else if (width == -1) width = height*3/2;