| 264 | | } |
|---|
| | 294 | addHandlers!(Checkbox)(&checkbox_draw, &checkbox_best_size, &checkbox_min_size); |
|---|
| | 295 | Element[] elems = new Element[2]; |
|---|
| | 296 | Element e; |
|---|
| | 297 | |
|---|
| | 298 | e.fontColor.states[ WidgetState.Rollover ] = Color(0, 0, 0, 255); |
|---|
| | 299 | e.texColor.states[ WidgetState.Rollover ] = Color(255, 255, 255, 160); |
|---|
| | 300 | |
|---|
| | 301 | // check box |
|---|
| | 302 | e.set_texture( m_texid, [0, 54, 27, 81] ); |
|---|
| | 303 | e.set_font( m_default_font, Color(255, 255, 255, 255) ); |
|---|
| | 304 | e.fontColor.states[ WidgetState.Disabled ] = Color( 200, 200, 200, 200 ); |
|---|
| | 305 | e.texColor.states[ WidgetState.Normal ] = Color(255, 255, 255, 150); |
|---|
| | 306 | e.texColor.states[ WidgetState.Focus ] = Color(255, 255, 255, 200); |
|---|
| | 307 | e.texColor.states[ WidgetState.Depressed ] = Color(255, 255, 255, 255); |
|---|
| | 308 | elems[0] = e; |
|---|
| | 309 | |
|---|
| | 310 | // Check |
|---|
| | 311 | e.set_texture( m_texid, [27, 54, 54, 81] ); |
|---|
| | 312 | elems[1] = e; |
|---|
| | 313 | |
|---|
| | 314 | m_elements[Checkbox.classinfo] = elems; |
|---|
| | 315 | } |
|---|
| | 316 | Size checkbox_min_size(Widget widget, Size bounds) { |
|---|
| | 317 | auto w = cast(Checkbox)widget; assert(w); |
|---|
| | 318 | Element[] elem = get_widget_elements(w); |
|---|
| | 319 | Point p0 = Point(elem[0].texRect[0], elem[0].texRect[1]); |
|---|
| | 320 | Point p1 = Point(elem[0].texRect[2], elem[0].texRect[3]); |
|---|
| | 321 | p1 -= p0; |
|---|
| | 322 | Size btn = Size(lrint(p1.x/2),lrint(p1.y/2)); |
|---|
| | 323 | Size txt = elem[0].font.string_rect(w.label).size; |
|---|
| | 324 | |
|---|
| | 325 | return Size(btn.width+3+txt.width+4,max(btn.height,txt.height)); |
|---|
| | 326 | } |
|---|
| | 327 | Size checkbox_best_size(Widget widget, Size bounds) { |
|---|
| | 328 | auto w = cast(Checkbox)widget; assert(w); |
|---|
| | 329 | Size minsz = checkbox_min_size(w,bounds); |
|---|
| | 330 | return Size(max(50.,minsz.w), max(22.,minsz.h)); |
|---|
| | 331 | } |
|---|
| | 332 | void checkbox_draw(Widget widget) { |
|---|
| | 333 | auto w = cast(Checkbox)widget; assert(w); |
|---|
| | 334 | with (w) { |
|---|
| | 335 | WidgetState iState = WidgetState.Normal; |
|---|
| | 336 | if( !shown ) |
|---|
| | 337 | iState = WidgetState.Hidden; |
|---|
| | 338 | else if( !enabled ) |
|---|
| | 339 | iState = WidgetState.Disabled; |
|---|
| | 340 | else if( depressed ) |
|---|
| | 341 | iState = WidgetState.Depressed; |
|---|
| | 342 | else if( rollover ) |
|---|
| | 343 | iState = WidgetState.Rollover; |
|---|
| | 344 | else if( focused ) |
|---|
| | 345 | iState = WidgetState.Focus; |
|---|
| | 346 | |
|---|
| | 347 | float fBlendRate = ( iState == WidgetState.Depressed ) ? 0.0f : 0.8f; |
|---|
| | 348 | |
|---|
| | 349 | Element[] elem = get_widget_elements(w); |
|---|
| | 350 | elem[0].texColor.blend( iState, m_elapsedTime, fBlendRate ); |
|---|
| | 351 | elem[0].fontColor.blend( iState, m_elapsedTime, fBlendRate ); |
|---|
| | 352 | |
|---|
| | 353 | Rect btn_rect = rect; |
|---|
| | 354 | btn_rect.w = btn_rect.h; |
|---|
| | 355 | |
|---|
| | 356 | Rect txt_rect = rect; |
|---|
| | 357 | txt_rect.x += btn_rect.w + 2; |
|---|
| | 358 | txt_rect.w = elem[0].font.string_rect(label).w; |
|---|
| | 359 | |
|---|
| | 360 | draw_elem_sprite( elem[0], btn_rect ); |
|---|
| | 361 | draw_elem_text( elem[0], label, txt_rect, true ); |
|---|
| | 362 | |
|---|
| | 363 | if( !checked ) |
|---|
| | 364 | iState = WidgetState.Hidden; |
|---|
| | 365 | |
|---|
| | 366 | elem[1].texColor.blend( iState, m_elapsedTime, fBlendRate ); |
|---|
| | 367 | btn_rect.inset(1); |
|---|
| | 368 | draw_elem_sprite( elem[1], btn_rect ); |
|---|
| | 369 | } |
|---|
| | 370 | } |
|---|
| | 371 | //----TEXTFIELD--------------------------------------------------------- |
|---|
| 268 | | } |
|---|
| | 376 | addHandlers!(Slider)(&slider_draw, &slider_best_size, &slider_min_size); |
|---|
| | 377 | addEventHandlers!(Slider)(&slider_mouse_button, &slider_mouse_move); |
|---|
| | 378 | |
|---|
| | 379 | Element[] elems = new Element[2]; |
|---|
| | 380 | Element e; |
|---|
| | 381 | |
|---|
| | 382 | |
|---|
| | 383 | e.fontColor.states[ WidgetState.Rollover ] = Color(0, 0, 0, 255); |
|---|
| | 384 | e.fontColor.states[ WidgetState.Depressed ] = Color(0, 0, 0, 255); |
|---|
| | 385 | e.fontColor.states[ WidgetState.Disabled ] = Color(200, 200, 200, 200); |
|---|
| | 386 | e.set_font( m_default_font, Color(255, 255, 255, 255)); |
|---|
| | 387 | |
|---|
| | 388 | e.set_texture( m_texid, [1, 290, 280, 331] ); |
|---|
| | 389 | e.texColor.states[ WidgetState.Rollover ] = Color(255, 255, 255, 160); |
|---|
| | 390 | e.texColor.states[ WidgetState.Depressed ] = Color(150, 150, 150, 255); |
|---|
| | 391 | e.texColor.states[ WidgetState.Normal ] = Color(255, 255, 255, 150); |
|---|
| | 392 | e.texColor.states[ WidgetState.Focus ] = Color(255, 255, 255, 200); |
|---|
| | 393 | e.texColor.states[ WidgetState.Disabled ] = Color(255, 255, 255, 70); |
|---|
| | 394 | elems[0] = e; |
|---|
| | 395 | |
|---|
| | 396 | // Slider thumb |
|---|
| | 397 | e.set_texture( m_texid, [248, 55, 289, 96] ); |
|---|
| | 398 | elems[1] = e; |
|---|
| | 399 | |
|---|
| | 400 | m_elements[Slider.classinfo] = elems; |
|---|
| | 401 | |
|---|
| | 402 | } |
|---|
| | 403 | Size slider_min_size(Widget widget, Size bounds) { |
|---|
| | 404 | auto w = cast(Slider)widget; assert(w); |
|---|
| | 405 | Element[] elem = get_widget_elements(w); |
|---|
| | 406 | |
|---|
| | 407 | int scroll_thumb_w = (elem[1].texRect[2] - elem[1].texRect[0])/2; |
|---|
| | 408 | int scroll_thumb_h = (elem[1].texRect[3] - elem[1].texRect[1])/2; |
|---|
| | 409 | Size sz = Size(scroll_thumb_w+20, scroll_thumb_h+5); |
|---|
| | 410 | if (w.vertical) { |
|---|
| | 411 | return Size(sz.h,sz.w); |
|---|
| | 412 | } |
|---|
| | 413 | return sz; |
|---|
| | 414 | } |
|---|
| | 415 | Size slider_best_size(Widget widget, Size bounds) { |
|---|
| | 416 | auto w = cast(Slider)widget; assert(w); |
|---|
| | 417 | Size sz = slider_min_size(w,bounds); |
|---|
| | 418 | if (w.vertical) { |
|---|
| | 419 | sz.h = max(sz.h, 100); |
|---|
| | 420 | //sz.h = max(sz.h, bounds.h); |
|---|
| | 421 | } |
|---|
| | 422 | else { |
|---|
| | 423 | sz.w = max(sz.w, 100); |
|---|
| | 424 | //sz.w = max(sz.w, bounds.w); |
|---|
| | 425 | } |
|---|
| | 426 | return sz; |
|---|
| | 427 | } |
|---|
| | 428 | void slider_draw(Widget widget) { |
|---|
| | 429 | auto w = cast(Slider)widget; assert(w); |
|---|
| | 430 | with(w) { |
|---|
| | 431 | int dx = 0; |
|---|
| | 432 | int dy = 0; |
|---|
| | 433 | |
|---|
| | 434 | WidgetState iState = WidgetState.Normal; |
|---|
| | 435 | if( !shown ) |
|---|
| | 436 | { |
|---|
| | 437 | iState = WidgetState.Hidden; |
|---|
| | 438 | } |
|---|
| | 439 | else if( disabled ) |
|---|
| | 440 | { |
|---|
| | 441 | iState = WidgetState.Disabled; |
|---|
| | 442 | } |
|---|
| | 443 | else if( is_grabbing_mouse ) |
|---|
| | 444 | { |
|---|
| | 445 | iState = WidgetState.Depressed; |
|---|
| | 446 | dx = dy = 1; |
|---|
| | 447 | } |
|---|
| | 448 | else if( rollover ) |
|---|
| | 449 | { |
|---|
| | 450 | iState = WidgetState.Rollover; |
|---|
| | 451 | dx = dy = -1; |
|---|
| | 452 | } |
|---|
| | 453 | else if( focused ) |
|---|
| | 454 | { |
|---|
| | 455 | iState = WidgetState.Focus; |
|---|
| | 456 | } |
|---|
| | 457 | |
|---|
| | 458 | float fBlendRate = ( iState == WidgetState.Depressed ) ? 0.0f : 0.8f; |
|---|
| | 459 | |
|---|
| | 460 | Element[] elem = get_widget_elements(w); |
|---|
| | 461 | |
|---|
| | 462 | int track_height = (elem[0].texRect[3] - elem[0].texRect[1])/2; |
|---|
| | 463 | Rect track_rect = rect; |
|---|
| | 464 | if (horizontal) { |
|---|
| | 465 | track_rect.h = track_height; |
|---|
| | 466 | track_rect.y = rect.y + (rect.h-track_rect.h)/2; |
|---|
| | 467 | } else { |
|---|
| | 468 | track_rect.w = track_height; |
|---|
| | 469 | track_rect.x = rect.x + (rect.w-track_rect.w)/2; |
|---|
| | 470 | } |
|---|
| | 471 | elem[0].texColor.blend( iState, m_elapsedTime, fBlendRate ); |
|---|
| | 472 | if (horizontal) { |
|---|
| | 473 | draw_elem_sprite( elem[0], track_rect ); |
|---|
| | 474 | } |
|---|
| | 475 | else { |
|---|
| | 476 | glPushMatrix(); |
|---|
| | 477 | // Rotate rectangle |
|---|
| | 478 | Rect r = Rect(0,0,track_rect.h,track_rect.w); |
|---|
| | 479 | r.x = -r.w/2; |
|---|
| | 480 | r.y = -r.h/2; |
|---|
| | 481 | glTranslatef(rect.x+rect.w/2, rect.y+rect.h/2, 0); |
|---|
| | 482 | glRotatef(90,0,0,1); |
|---|
| | 483 | draw_elem_sprite( elem[0], r ); |
|---|
| | 484 | glPopMatrix(); |
|---|
| | 485 | } |
|---|
| | 486 | |
|---|
| | 487 | Rect thumb_rect = rect; |
|---|
| | 488 | int thumb_w = (elem[1].texRect[2] - elem[1].texRect[0])/2; |
|---|
| | 489 | int thumb_h = (elem[1].texRect[3] - elem[1].texRect[1])/2; |
|---|
| | 490 | thumb_rect.size = Size(thumb_w,thumb_h); |
|---|
| | 491 | if (thumb_rect.w > rect.w) { |
|---|
| | 492 | float shrinkby = rect.w/thumb_rect.w; |
|---|
| | 493 | thumb_rect.h *= shrinkby; |
|---|
| | 494 | thumb_rect.w = rect.w; |
|---|
| | 495 | } |
|---|
| | 496 | if (thumb_rect.h > rect.h) { |
|---|
| | 497 | float shrinkby = rect.h/thumb_rect.h; |
|---|
| | 498 | thumb_rect.w *= shrinkby; |
|---|
| | 499 | thumb_rect.h = rect.h; |
|---|
| | 500 | } |
|---|
| | 501 | elem[1].texColor.blend( iState, m_elapsedTime, fBlendRate ); |
|---|
| | 502 | if ( horizontal ) { |
|---|
| | 503 | float t = (value-value_min)/(value_max-value_min); |
|---|
| | 504 | float v = rect.x + thumb_rect.w/2 + t*(rect.w-thumb_rect.w); |
|---|
| | 505 | thumb_rect.x = v-thumb_rect.w/2; |
|---|
| | 506 | thumb_rect.y = track_rect.y + (track_rect.h-thumb_rect.h)/2; |
|---|
| | 507 | draw_elem_sprite( elem[1], thumb_rect ); |
|---|
| | 508 | } else { |
|---|
| | 509 | float t = (value-value_min)/(value_max-value_min); |
|---|
| | 510 | float v = rect.y2 - thumb_rect.w/2 - t*(rect.h-thumb_rect.h); |
|---|
| | 511 | thumb_rect = Rect(-thumb_rect.h/2,-thumb_rect.w/2, |
|---|
| | 512 | thumb_rect.h, thumb_rect.w); |
|---|
| | 513 | glPushMatrix(); |
|---|
| | 514 | glTranslatef(rect.x+rect.w/2, v, 0); |
|---|
| | 515 | glRotatef(90,0,0,1); |
|---|
| | 516 | draw_elem_sprite( elem[1], thumb_rect ); |
|---|
| | 517 | glPopMatrix(); |
|---|
| | 518 | } |
|---|
| | 519 | } |
|---|
| | 520 | } |
|---|
| | 521 | double slider_map_mouse_to_value(Slider w, Point p) |
|---|
| | 522 | { |
|---|
| | 523 | double t; |
|---|
| | 524 | Element[] elem = get_widget_elements(w); |
|---|
| | 525 | int tw = |
|---|
| | 526 | (elem[1].texRect[2] - elem[1].texRect[0])/2; |
|---|
| | 527 | tw *= 0.5; |
|---|
| | 528 | if (w.horizontal) { |
|---|
| | 529 | double trackmin = w.rect.x + tw; |
|---|
| | 530 | double trackmax = w.rect.x2 - tw; |
|---|
| | 531 | t = (p.x-trackmin)/(trackmax-trackmin); |
|---|
| | 532 | } else { |
|---|
| | 533 | double trackmin = w.rect.y + tw; |
|---|
| | 534 | double trackmax = w.rect.y2 - tw; |
|---|
| | 535 | t = 1 - (p.y-trackmin)/(trackmax-trackmin); |
|---|
| | 536 | } |
|---|
| | 537 | double v = w.value_min + t*(w.value_max - w.value_min); |
|---|
| | 538 | return v; |
|---|
| | 539 | } |
|---|
| | 540 | void slider_mouse_button(Widget widget, MouseButtonEvent ev) |
|---|
| | 541 | { |
|---|
| | 542 | auto w = cast(Slider)widget; assert(w); |
|---|
| | 543 | if (ev.is_left && ev.is_press) { w.grab_mouse(); } |
|---|
| | 544 | else if (ev.is_left && ev.is_release) { w.release_mouse(); } |
|---|
| | 545 | double v = slider_map_mouse_to_value(w, ev.p); |
|---|
| | 546 | w.m_saveValue = w.value; |
|---|
| | 547 | w.value = v; |
|---|
| | 548 | } |
|---|
| | 549 | void slider_mouse_move(Widget widget, MouseMoveEvent ev) |
|---|
| | 550 | { |
|---|
| | 551 | if (!widget.is_grabbing_mouse) { |
|---|
| | 552 | return; |
|---|
| | 553 | } |
|---|
| | 554 | auto w = cast(Slider)widget; assert(w); |
|---|
| | 555 | if (ev.left_down) { |
|---|
| | 556 | double v = slider_map_mouse_to_value(w, ev.p); |
|---|
| | 557 | w.value = v; |
|---|
| | 558 | } |
|---|
| | 559 | } |
|---|
| | 560 | //----PANEL------------------------------------------------------------- |
|---|