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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
|
/*** /
This file is part of Golly, a Game of Life Simulator.
Copyright (C) 2011 Andrew Trevorrow and Tomas Rokicki.
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Web site: http://sourceforge.net/projects/golly
Authors: rokicki@gmail.com andrew@trevorrow.com
/ ***/
#include "wx/wxprec.h" // for compilers that support precompilation
#ifndef WX_PRECOMP
#include "wx/wx.h" // for all others include the necessary headers
#endif
#include "wx/dcbuffer.h" // for wxBufferedPaintDC
#if wxUSE_TOOLTIPS
#include "wx/tooltip.h" // for wxToolTip
#endif
#include "bigint.h"
#include "lifealgo.h"
#include "wxgolly.h" // for viewptr, statusptr, mainptr
#include "wxmain.h" // for mainptr->...
#include "wxutils.h" // for Warning, etc
#include "wxprefs.h" // for showtimeline, etc
#include "wxstatus.h" // for statusptr->...
#include "wxscript.h" // for inscript
#include "wxview.h" // for viewptr->...
#include "wxlayer.h" // for currlayer
#include "wxtimeline.h"
#ifdef __WXMSW__
// bitmaps are loaded via .rc file
#else
// bitmaps for timeline bar buttons
#include "bitmaps/record.xpm"
#include "bitmaps/stop.xpm"
#include "bitmaps/backwards.xpm"
#include "bitmaps/forwards.xpm"
#include "bitmaps/stopplay.xpm"
#include "bitmaps/deltime.xpm"
#endif
// -----------------------------------------------------------------------------
// Define timeline bar window:
enum {
// ids for bitmap buttons in timeline bar
RECORD_BUTT = 0,
STOPREC_BUTT,
BACKWARDS_BUTT,
FORWARDS_BUTT,
STOPPLAY_BUTT,
DELETE_BUTT,
NUM_BUTTONS, // must be after all buttons
ID_SLIDER, // for slider
ID_SCROLL // for scroll bar
};
// derive from wxPanel so we get current theme's background color on Windows
class TimelineBar : public wxPanel
{
public:
TimelineBar(wxWindow* parent, wxCoord xorg, wxCoord yorg, int wd, int ht);
~TimelineBar();
void AddButton(int id, const wxString& tip);
void AddSeparator();
void EnableButton(int id, bool enable);
void UpdateButtons();
void UpdateSlider();
void UpdateScrollBar();
void DisplayCurrentFrame();
// detect press and release of a bitmap button
void OnButtonDown(wxMouseEvent& event);
void OnButtonUp(wxMouseEvent& event);
void OnKillFocus(wxFocusEvent& event);
wxSlider* slider; // slider for controlling autoplay speed
wxScrollBar* framebar; // scroll bar for displaying timeline frames
// positioning data used by AddButton and AddSeparator
int ypos, xpos, smallgap, biggap;
private:
// any class wishing to process wxWidgets events must use this macro
DECLARE_EVENT_TABLE()
// event handlers
void OnPaint(wxPaintEvent& event);
void OnMouseDown(wxMouseEvent& event);
void OnButton(wxCommandEvent& event);
void OnSlider(wxScrollEvent& event);
void OnScroll(wxScrollEvent& event);
void SetTimelineFont(wxDC& dc);
void DisplayText(wxDC& dc, const wxString& s, wxCoord x, wxCoord y);
void DrawTimelineBar(wxDC& dc, int wd, int ht);
// bitmaps for normal buttons
wxBitmap normbutt[NUM_BUTTONS];
#ifdef __WXMSW__
// on Windows we need bitmaps for disabled buttons
wxBitmap disnormbutt[NUM_BUTTONS];
#endif
// remember state of buttons to avoid unnecessary updates
int buttstate[NUM_BUTTONS];
wxBitmap* timelinebitmap; // timeline bar bitmap
int timelinebitmapwd; // width of timeline bar bitmap
int timelinebitmapht; // height of timeline bar bitmap
int digitwd; // width of digit in timeline bar font
int digitht; // height of digit in timeline bar font
int textascent; // vertical adjustment used in DrawText calls
wxFont* timelinefont; // timeline bar font
};
BEGIN_EVENT_TABLE(TimelineBar, wxPanel)
EVT_PAINT ( TimelineBar::OnPaint)
EVT_LEFT_DOWN ( TimelineBar::OnMouseDown)
EVT_BUTTON (wxID_ANY, TimelineBar::OnButton)
EVT_COMMAND_SCROLL (ID_SLIDER, TimelineBar::OnSlider)
EVT_COMMAND_SCROLL (ID_SCROLL, TimelineBar::OnScroll)
END_EVENT_TABLE()
// -----------------------------------------------------------------------------
static TimelineBar* tbarptr = NULL; // global pointer to timeline bar
static int mindelpos; // minimum x position of DELETE_BUTT
const int TBARHT = 32; // height of timeline bar
const int SCROLLHT = 17; // height of scroll bar
const int PAGESIZE = 10; // scroll amount when paging
const int MINSPEED = -10; // minimum autoplay speed
const int MAXSPEED = 10; // maximum autoplay speed
// width and height of bitmap buttons
#if defined(__WXOSX_COCOA__) || defined(__WXGTK__)
const int BUTTON_WD = 28;
const int BUTTON_HT = 28;
#else
const int BUTTON_WD = 24;
const int BUTTON_HT = 24;
#endif
// timeline bar buttons (must be global to use Connect/Disconnect on Windows)
static wxBitmapButton* tlbutt[NUM_BUTTONS];
// -----------------------------------------------------------------------------
TimelineBar::TimelineBar(wxWindow* parent, wxCoord xorg, wxCoord yorg, int wd, int ht)
: wxPanel(parent, wxID_ANY, wxPoint(xorg,yorg), wxSize(wd,ht),
wxNO_FULL_REPAINT_ON_RESIZE)
{
#ifdef __WXGTK__
// avoid erasing background on GTK+
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
#endif
// init bitmaps for normal state
normbutt[RECORD_BUTT] = wxBITMAP(record);
normbutt[STOPREC_BUTT] = wxBITMAP(stop);
normbutt[BACKWARDS_BUTT] = wxBITMAP(backwards);
normbutt[FORWARDS_BUTT] = wxBITMAP(forwards);
normbutt[STOPPLAY_BUTT] = wxBITMAP(stopplay);
normbutt[DELETE_BUTT] = wxBITMAP(deltime);
#ifdef __WXMSW__
for (int i = 0; i < NUM_BUTTONS; i++) {
CreatePaleBitmap(normbutt[i], disnormbutt[i]);
}
#endif
for (int i = 0; i < NUM_BUTTONS; i++) {
buttstate[i] = 0;
}
// init position variables used by AddButton and AddSeparator
#ifdef __WXGTK__
// buttons are a different size in wxGTK
xpos = 2;
ypos = 2;
smallgap = 6;
#else
xpos = 4;
ypos = (32 - BUTTON_HT) / 2;
smallgap = 4;
#endif
biggap = 16;
// add buttons
AddButton(RECORD_BUTT, _("Start recording"));
AddSeparator();
AddButton(BACKWARDS_BUTT, _("Play backwards"));
AddButton(FORWARDS_BUTT, _("Play forwards"));
// create font for text in timeline bar and set textascent for use in DisplayText
#ifdef __WXMSW__
// use smaller, narrower font on Windows
timelinefont = wxFont::New(8, wxDEFAULT, wxNORMAL, wxNORMAL);
int major, minor;
wxGetOsVersion(&major, &minor);
if ( major > 5 || (major == 5 && minor >= 1) ) {
// 5.1+ means XP or later (Vista if major >= 6)
textascent = 11;
} else {
textascent = 10;
}
#elif defined(__WXGTK__)
// use smaller font on GTK
timelinefont = wxFont::New(8, wxMODERN, wxNORMAL, wxNORMAL);
textascent = 11;
#elif defined(__WXOSX_COCOA__)
// we need to specify facename to get Monaco instead of Courier
timelinefont = wxFont::New(10, wxMODERN, wxNORMAL, wxNORMAL, false, wxT("Monaco"));
textascent = 10;
#elif defined(__WXMAC__)
timelinefont = wxFont::New(10, wxMODERN, wxNORMAL, wxNORMAL);
textascent = 10;
#else
timelinefont = wxFont::New(10, wxMODERN, wxNORMAL, wxNORMAL);
textascent = 10;
#endif
if (timelinefont == NULL) Fatal(_("Failed to create timeline bar font!"));
wxClientDC dc(this);
SetTimelineFont(dc);
dc.GetTextExtent(_("9"), &digitwd, &digitht);
digitht -= 4;
timelinebitmap = NULL;
timelinebitmapwd = -1;
timelinebitmapht = -1;
// add slider
int x, y;
int sliderwd = 80;
#ifdef __WXMAC__
int sliderht = 15;
#else
int sliderht = 24; // for Windows and wxGTK
#endif
x = xpos + 20 - smallgap;
y = (TBARHT - (sliderht + 1)) / 2;
slider = new wxSlider(this, ID_SLIDER, 0, MINSPEED, MAXSPEED, wxPoint(x, y),
wxSize(sliderwd, sliderht),
wxSL_HORIZONTAL);
if (slider == NULL) Fatal(_("Failed to create timeline slider!"));
#ifdef __WXMAC__
slider->SetWindowVariant(wxWINDOW_VARIANT_SMALL);
slider->Move(x, y+1);
#endif
xpos = x + sliderwd;
// add scroll bar
int scrollbarwd = 60; // minimum width (ResizeTimelineBar will alter it)
#ifdef __WXMAC__
int scrollbarht = 15; // must be this height on Mac
#else
int scrollbarht = SCROLLHT;
#endif
x = xpos + 20;
y = (TBARHT - (scrollbarht + 1)) / 2;
framebar = new wxScrollBar(this, ID_SCROLL, wxPoint(x, y),
wxSize(scrollbarwd, scrollbarht),
wxSB_HORIZONTAL);
if (framebar == NULL) Fatal(_("Failed to create timeline scroll bar!"));
xpos = x + scrollbarwd + 4;
mindelpos = xpos;
AddButton(DELETE_BUTT, _("Delete timeline"));
// ResizeTimelineBar will move this button to right end of scroll bar
}
// -----------------------------------------------------------------------------
TimelineBar::~TimelineBar()
{
delete timelinefont;
delete timelinebitmap;
delete slider;
delete framebar;
}
// -----------------------------------------------------------------------------
void TimelineBar::SetTimelineFont(wxDC& dc)
{
dc.SetFont(*timelinefont);
dc.SetTextForeground(*wxBLACK);
dc.SetBrush(*wxBLACK_BRUSH);
dc.SetBackgroundMode(wxTRANSPARENT);
}
// -----------------------------------------------------------------------------
void TimelineBar::DisplayText(wxDC& dc, const wxString& s, wxCoord x, wxCoord y)
{
// DrawText's y parameter is top of text box but we pass in baseline
// so adjust by textascent which depends on platform and OS version -- yuk!
dc.DrawText(s, x, y - textascent);
}
// -----------------------------------------------------------------------------
void TimelineBar::DrawTimelineBar(wxDC& dc, int wd, int ht)
{
wxRect r = wxRect(0, 0, wd, ht);
#ifdef __WXMAC__
wxBrush brush(wxColor(202,202,202));
FillRect(dc, r, brush);
#endif
#ifdef __WXMSW__
// use theme background color on Windows
wxBrush brush(GetBackgroundColour());
FillRect(dc, r, brush);
#endif
// draw gray border line at top edge
#if defined(__WXMSW__)
dc.SetPen(*wxGREY_PEN);
#elif defined(__WXMAC__)
wxPen linepen(wxColor(140,140,140));
dc.SetPen(linepen);
#else
dc.SetPen(*wxLIGHT_GREY_PEN);
#endif
dc.DrawLine(0, 0, r.width, 0);
dc.SetPen(wxNullPen);
if (currlayer->algo->hyperCapable()) {
bool canplay = TimelineExists() && !currlayer->algo->isrecording();
tlbutt[RECORD_BUTT]->Show(true);
tlbutt[BACKWARDS_BUTT]->Show(canplay);
tlbutt[FORWARDS_BUTT]->Show(canplay);
tlbutt[DELETE_BUTT]->Show(canplay);
slider->Show(canplay);
framebar->Show(canplay);
if (currlayer->algo->isrecording()) {
// show number of frames recorded so far
SetTimelineFont(dc);
dc.SetPen(*wxBLACK_PEN);
int x = smallgap + BUTTON_WD + 10;
int y = TBARHT - 8;
wxString str;
str.Printf(_("Frames recorded: %d"), currlayer->algo->getframecount());
DisplayText(dc, str, x, y - (SCROLLHT - digitht)/2);
dc.SetPen(wxNullPen);
}
} else {
tlbutt[RECORD_BUTT]->Show(false);
tlbutt[BACKWARDS_BUTT]->Show(false);
tlbutt[FORWARDS_BUTT]->Show(false);
tlbutt[DELETE_BUTT]->Show(false);
slider->Show(false);
framebar->Show(false);
SetTimelineFont(dc);
dc.SetPen(*wxBLACK_PEN);
int x = 6;
int y = TBARHT - 8;
DisplayText(dc, _("The current algorithm does not support timelines."),
x, y - (SCROLLHT - digitht)/2);
dc.SetPen(wxNullPen);
}
}
// -----------------------------------------------------------------------------
void TimelineBar::OnPaint(wxPaintEvent& WXUNUSED(event))
{
int wd, ht;
GetClientSize(&wd, &ht);
// wd or ht might be < 1 on Windows
if (wd < 1) wd = 1;
if (ht < 1) ht = 1;
#if defined(__WXMAC__) || defined(__WXGTK__)
// windows on Mac OS X and GTK+ 2.0 are automatically buffered
wxPaintDC dc(this);
#else
// use wxWidgets buffering to avoid flicker
if (wd != timelinebitmapwd || ht != timelinebitmapht) {
// need to create a new bitmap for timeline bar
delete timelinebitmap;
timelinebitmap = new wxBitmap(wd, ht);
timelinebitmapwd = wd;
timelinebitmapht = ht;
}
if (timelinebitmap == NULL) Fatal(_("Not enough memory to render timeline bar!"));
wxBufferedPaintDC dc(this, *timelinebitmap);
#endif
if (showtimeline) DrawTimelineBar(dc, wd, ht);
}
// -----------------------------------------------------------------------------
void TimelineBar::OnMouseDown(wxMouseEvent& WXUNUSED(event))
{
// on Win/Linux we need to reset keyboard focus to viewport window
viewptr->SetFocus();
mainptr->showbanner = false;
statusptr->ClearMessage();
}
// -----------------------------------------------------------------------------
void TimelineBar::OnButton(wxCommandEvent& event)
{
#ifdef __WXMAC__
// close any open tool tip window (fixes wxMac bug?)
wxToolTip::RemoveToolTips();
#endif
mainptr->showbanner = false;
statusptr->ClearMessage();
int id = event.GetId();
int cmdid;
switch (id) {
case RECORD_BUTT: cmdid = ID_RECORD; break;
case BACKWARDS_BUTT: PlayTimeline(-1); return;
case FORWARDS_BUTT: PlayTimeline(1); return;
case DELETE_BUTT: cmdid = ID_DELTIME; break;
default: Warning(_("Unexpected button id!")); return;
}
// call MainFrame::OnMenu after OnButton finishes
wxCommandEvent cmdevt(wxEVT_COMMAND_MENU_SELECTED, cmdid);
wxPostEvent(mainptr->GetEventHandler(), cmdevt);
// avoid possible problems
viewptr->SetFocus();
}
// -----------------------------------------------------------------------------
void TimelineBar::OnSlider(wxScrollEvent& event)
{
WXTYPE type = event.GetEventType();
if (type == wxEVT_SCROLL_LINEUP) {
currlayer->tlspeed--;
if (currlayer->tlspeed < MINSPEED) currlayer->tlspeed = MINSPEED;
} else if (type == wxEVT_SCROLL_LINEDOWN) {
currlayer->tlspeed++;
if (currlayer->tlspeed > MAXSPEED) currlayer->tlspeed = MAXSPEED;
} else if (type == wxEVT_SCROLL_PAGEUP) {
currlayer->tlspeed -= PAGESIZE;
if (currlayer->tlspeed < MINSPEED) currlayer->tlspeed = MINSPEED;
} else if (type == wxEVT_SCROLL_PAGEDOWN) {
currlayer->tlspeed += PAGESIZE;
if (currlayer->tlspeed > MAXSPEED) currlayer->tlspeed = MAXSPEED;
} else if (type == wxEVT_SCROLL_THUMBTRACK) {
currlayer->tlspeed = event.GetPosition();
if (currlayer->tlspeed < MINSPEED) currlayer->tlspeed = MINSPEED;
if (currlayer->tlspeed > MAXSPEED) currlayer->tlspeed = MAXSPEED;
} else if (type == wxEVT_SCROLL_THUMBRELEASE) {
UpdateSlider();
}
#ifndef __WXMAC__
viewptr->SetFocus(); // need on Win/Linux
#endif
}
// -----------------------------------------------------------------------------
void TimelineBar::DisplayCurrentFrame()
{
currlayer->algo->gotoframe(currlayer->currframe);
// FitInView(0) would be less jerky but has the disadvantage that
// scale won't change if a pattern shrinks when going backwards
if (currlayer->autofit) viewptr->FitInView(1);
mainptr->UpdatePatternAndStatus();
}
// -----------------------------------------------------------------------------
void TimelineBar::OnScroll(wxScrollEvent& event)
{
WXTYPE type = event.GetEventType();
// best to stop autoplay if scroll bar is used
if (currlayer->autoplay != 0) {
currlayer->autoplay = 0;
mainptr->UpdateUserInterface(true);
}
if (type == wxEVT_SCROLL_LINEUP) {
currlayer->currframe--;
if (currlayer->currframe < 0) currlayer->currframe = 0;
DisplayCurrentFrame();
} else if (type == wxEVT_SCROLL_LINEDOWN) {
currlayer->currframe++;
if (currlayer->currframe >= currlayer->algo->getframecount())
currlayer->currframe = currlayer->algo->getframecount() - 1;
DisplayCurrentFrame();
} else if (type == wxEVT_SCROLL_PAGEUP) {
currlayer->currframe -= PAGESIZE;
if (currlayer->currframe < 0) currlayer->currframe = 0;
DisplayCurrentFrame();
} else if (type == wxEVT_SCROLL_PAGEDOWN) {
currlayer->currframe += PAGESIZE;
if (currlayer->currframe >= currlayer->algo->getframecount())
currlayer->currframe = currlayer->algo->getframecount() - 1;
DisplayCurrentFrame();
} else if (type == wxEVT_SCROLL_THUMBTRACK) {
currlayer->currframe = event.GetPosition();
if (currlayer->currframe < 0)
currlayer->currframe = 0;
if (currlayer->currframe >= currlayer->algo->getframecount())
currlayer->currframe = currlayer->algo->getframecount() - 1;
DisplayCurrentFrame();
} else if (type == wxEVT_SCROLL_THUMBRELEASE) {
UpdateScrollBar();
}
#ifndef __WXMAC__
viewptr->SetFocus(); // need on Win/Linux
#endif
}
// -----------------------------------------------------------------------------
void TimelineBar::OnKillFocus(wxFocusEvent& event)
{
int id = event.GetId();
tlbutt[id]->SetFocus(); // don't let button lose focus
}
// -----------------------------------------------------------------------------
void TimelineBar::OnButtonDown(wxMouseEvent& event)
{
// timeline bar button has been pressed
int id = event.GetId();
// connect a handler that keeps focus with the pressed button
tlbutt[id]->Connect(id, wxEVT_KILL_FOCUS, wxFocusEventHandler(TimelineBar::OnKillFocus));
event.Skip();
}
// -----------------------------------------------------------------------------
void TimelineBar::OnButtonUp(wxMouseEvent& event)
{
// timeline bar button has been released
int id = event.GetId();
wxPoint pt = tlbutt[id]->ScreenToClient( wxGetMousePosition() );
int wd, ht;
tlbutt[id]->GetClientSize(&wd, &ht);
wxRect r(0, 0, wd, ht);
// diconnect kill-focus handler
tlbutt[id]->Disconnect(id, wxEVT_KILL_FOCUS, wxFocusEventHandler(TimelineBar::OnKillFocus));
viewptr->SetFocus();
if (r.Contains(pt)) {
// call OnButton
wxCommandEvent buttevt(wxEVT_COMMAND_BUTTON_CLICKED, id);
buttevt.SetEventObject(tlbutt[id]);
tlbutt[id]->GetEventHandler()->ProcessEvent(buttevt);
}
}
// -----------------------------------------------------------------------------
void TimelineBar::AddButton(int id, const wxString& tip)
{
tlbutt[id] = new wxBitmapButton(this, id, normbutt[id], wxPoint(xpos,ypos),
wxSize(BUTTON_WD, BUTTON_HT));
if (tlbutt[id] == NULL) {
Fatal(_("Failed to create timeline bar button!"));
} else {
xpos += BUTTON_WD + smallgap;
tlbutt[id]->SetToolTip(tip);
#ifdef __WXMSW__
// fix problem with timeline bar buttons when generating/inscript
// due to focus being changed to viewptr
tlbutt[id]->Connect(id, wxEVT_LEFT_DOWN, wxMouseEventHandler(TimelineBar::OnButtonDown));
tlbutt[id]->Connect(id, wxEVT_LEFT_UP, wxMouseEventHandler(TimelineBar::OnButtonUp));
#endif
}
}
// -----------------------------------------------------------------------------
void TimelineBar::AddSeparator()
{
xpos += biggap - smallgap;
}
// -----------------------------------------------------------------------------
void TimelineBar::EnableButton(int id, bool enable)
{
if (enable == tlbutt[id]->IsEnabled()) return;
#ifdef __WXMSW__
tlbutt[id]->SetBitmapDisabled(disnormbutt[id]);
#endif
tlbutt[id]->Enable(enable);
}
// -----------------------------------------------------------------------------
void TimelineBar::UpdateButtons()
{
if (currlayer->algo->isrecording()) {
if (buttstate[RECORD_BUTT] != -1) {
buttstate[RECORD_BUTT] = -1;
tlbutt[RECORD_BUTT]->SetBitmapLabel(normbutt[STOPREC_BUTT]);
tlbutt[RECORD_BUTT]->SetToolTip(_("Stop recording"));
if (showtimeline) tlbutt[RECORD_BUTT]->Refresh(false);
}
} else {
if (buttstate[RECORD_BUTT] != 1) {
buttstate[RECORD_BUTT] = 1;
tlbutt[RECORD_BUTT]->SetBitmapLabel(normbutt[RECORD_BUTT]);
tlbutt[RECORD_BUTT]->SetToolTip(_("Start recording"));
if (showtimeline) tlbutt[RECORD_BUTT]->Refresh(false);
}
}
// these buttons are only shown if there is a timeline and we're not recording
// (see DrawTimelineBar)
if (TimelineExists() && !currlayer->algo->isrecording()) {
if (currlayer->autoplay == 0) {
if (buttstate[BACKWARDS_BUTT] != 1) {
buttstate[BACKWARDS_BUTT] = 1;
tlbutt[BACKWARDS_BUTT]->SetBitmapLabel(normbutt[BACKWARDS_BUTT]);
tlbutt[BACKWARDS_BUTT]->SetToolTip(_("Play backwards"));
if (showtimeline) tlbutt[BACKWARDS_BUTT]->Refresh(false);
}
if (buttstate[FORWARDS_BUTT] != 1) {
buttstate[FORWARDS_BUTT] = 1;
tlbutt[FORWARDS_BUTT]->SetBitmapLabel(normbutt[FORWARDS_BUTT]);
tlbutt[FORWARDS_BUTT]->SetToolTip(_("Play forwards"));
if (showtimeline) tlbutt[FORWARDS_BUTT]->Refresh(false);
}
} else if (currlayer->autoplay > 0) {
if (buttstate[BACKWARDS_BUTT] != 1) {
buttstate[BACKWARDS_BUTT] = 1;
tlbutt[BACKWARDS_BUTT]->SetBitmapLabel(normbutt[BACKWARDS_BUTT]);
tlbutt[BACKWARDS_BUTT]->SetToolTip(_("Play backwards"));
if (showtimeline) tlbutt[BACKWARDS_BUTT]->Refresh(false);
}
if (buttstate[FORWARDS_BUTT] != -1) {
buttstate[FORWARDS_BUTT] = -1;
tlbutt[FORWARDS_BUTT]->SetBitmapLabel(normbutt[STOPPLAY_BUTT]);
tlbutt[FORWARDS_BUTT]->SetToolTip(_("Stop playing"));
if (showtimeline) tlbutt[FORWARDS_BUTT]->Refresh(false);
}
} else { // currlayer->autoplay < 0
if (buttstate[BACKWARDS_BUTT] != -1) {
buttstate[BACKWARDS_BUTT] = -1;
tlbutt[BACKWARDS_BUTT]->SetBitmapLabel(normbutt[STOPPLAY_BUTT]);
tlbutt[BACKWARDS_BUTT]->SetToolTip(_("Stop playing"));
if (showtimeline) tlbutt[BACKWARDS_BUTT]->Refresh(false);
}
if (buttstate[FORWARDS_BUTT] != 1) {
buttstate[FORWARDS_BUTT] = 1;
tlbutt[FORWARDS_BUTT]->SetBitmapLabel(normbutt[FORWARDS_BUTT]);
tlbutt[FORWARDS_BUTT]->SetToolTip(_("Play forwards"));
if (showtimeline) tlbutt[FORWARDS_BUTT]->Refresh(false);
}
}
}
}
// -----------------------------------------------------------------------------
void TimelineBar::UpdateSlider()
{
slider->SetValue(currlayer->tlspeed);
}
// -----------------------------------------------------------------------------
void TimelineBar::UpdateScrollBar()
{
framebar->SetScrollbar(currlayer->currframe, 1,
currlayer->algo->getframecount(), PAGESIZE, true);
}
// -----------------------------------------------------------------------------
void CreateTimelineBar(wxWindow* parent)
{
// create timeline bar underneath given window
int wd, ht;
parent->GetClientSize(&wd, &ht);
tbarptr = new TimelineBar(parent, 0, ht - TBARHT, wd, TBARHT);
if (tbarptr == NULL) Fatal(_("Failed to create timeline bar!"));
tbarptr->Show(showtimeline);
}
// -----------------------------------------------------------------------------
int TimelineBarHeight() {
return (showtimeline ? TBARHT : 0);
}
// -----------------------------------------------------------------------------
void UpdateTimelineBar(bool active)
{
if (tbarptr && showtimeline && !mainptr->IsIconized()) {
if (inscript) active = false;
// may need to change bitmaps in some buttons
tbarptr->UpdateButtons();
tbarptr->EnableButton(RECORD_BUTT, active && currlayer->algo->hyperCapable());
// note that slider, scroll bar and some buttons are only shown if there is
// a timeline and we're not recording (see DrawTimelineBar)
if (TimelineExists() && !currlayer->algo->isrecording()) {
tbarptr->EnableButton(BACKWARDS_BUTT, active);
tbarptr->EnableButton(FORWARDS_BUTT, active);
tbarptr->EnableButton(DELETE_BUTT, active);
tbarptr->UpdateSlider();
tbarptr->UpdateScrollBar();
}
if (currlayer->algo->isrecording()) {
// don't refresh RECORD_BUTT (otherwise button flickers on Windows)
int wd, ht;
tbarptr->GetClientSize(&wd, &ht);
wxRect r(BUTTON_WD + tbarptr->smallgap * 2, 0, wd, ht);
tbarptr->RefreshRect(r, false);
} else {
tbarptr->Refresh(false);
}
tbarptr->Update();
}
}
// -----------------------------------------------------------------------------
void ResizeTimelineBar(int y, int wd)
{
if (tbarptr) {
tbarptr->SetSize(0, y, wd, TBARHT);
// change width of scroll bar to nearly fill timeline bar
wxRect r = tbarptr->framebar->GetRect();
r.width = wd - r.x - 20 - BUTTON_WD - 20;
tbarptr->framebar->SetSize(r);
// move DELETE_BUTT to right edge of timeline bar
r = tlbutt[DELETE_BUTT]->GetRect();
r.x = wd - 20 - BUTTON_WD;
if (r.x < mindelpos && TimelineExists()) r.x = mindelpos;
tlbutt[DELETE_BUTT]->SetSize(r);
}
}
// -----------------------------------------------------------------------------
void ToggleTimelineBar()
{
showtimeline = !showtimeline;
wxRect r = bigview->GetRect();
if (showtimeline) {
// show timeline bar underneath viewport window
r.height -= TBARHT;
ResizeTimelineBar(r.y + r.height, r.width);
} else {
// hide timeline bar
r.height += TBARHT;
}
bigview->SetSize(r);
tbarptr->Show(showtimeline); // needed on Windows
mainptr->UpdateEverything();
}
// -----------------------------------------------------------------------------
void StartStopRecording()
{
if (!inscript && currlayer->algo->hyperCapable()) {
if (currlayer->algo->isrecording()) {
// terminate GeneratePattern
mainptr->Stop();
} else {
if (currlayer->algo->isEmpty()) {
statusptr->ErrorMessage(_("There is no pattern to record."));
return;
}
if (!showtimeline) ToggleTimelineBar();
if (currlayer->algo->getframecount() == MAX_FRAME_COUNT) {
wxString msg;
msg.Printf(_("The timeline can't be extended any further (max frames = %d)."),
MAX_FRAME_COUNT);
statusptr->ErrorMessage(msg);
return;
}
// record a new timeline, or extend the existing one
if (currlayer->algo->startrecording(currlayer->currbase, currlayer->currexpo) > 0) {
if (currlayer->algo->getGeneration() == currlayer->startgen) {
// ensure the SaveStartingPattern call in DeleteTimeline will
// create a new temporary .mc file (with only one frame)
currlayer->savestart = true;
}
// temporarily disable allowundo so that GeneratePattern won't
// try to save any temporary files
bool saveundo = allowundo;
allowundo = false;
mainptr->GeneratePattern();
allowundo = saveundo;
// GeneratePattern has called currlayer->algo->stoprecording()
} else {
// can this ever happen???!!!
Warning(_("Could not start recording!"));
}
}
}
}
// -----------------------------------------------------------------------------
void DeleteTimeline()
{
if (!inscript && TimelineExists() && !currlayer->algo->isrecording()) {
if (currlayer->currframe > 0) {
// tell writeNativeFormat to only save the current frame
// so that the temporary .mc files created by SaveStartingPattern and
// RememberGenStart/Finish won't store the entire timeline
currlayer->algo->savetimelinewithframe(0);
// do stuff so user can select Reset/Undo to go back to 1st frame
currlayer->algo->gotoframe(0);
if (currlayer->autofit) viewptr->FitInView(1);
if (currlayer->algo->getGeneration() == currlayer->startgen) {
mainptr->SaveStartingPattern();
}
if (allowundo) currlayer->undoredo->RememberGenStart();
// return to the current frame
currlayer->algo->gotoframe(currlayer->currframe);
if (currlayer->autofit) viewptr->FitInView(1);
if (allowundo) currlayer->undoredo->RememberGenFinish();
// restore flag that tells writeNativeFormat to save entire timeline
currlayer->algo->savetimelinewithframe(1);
}
currlayer->algo->destroytimeline();
mainptr->UpdateUserInterface(true);
}
}
// -----------------------------------------------------------------------------
void InitTimelineFrame()
{
// the user has just loaded a .mc file with a timeline,
// so prepare to display the first frame
currlayer->algo->gotoframe(0);
currlayer->currframe = 0;
currlayer->autoplay = 0;
currlayer->tlspeed = 0;
// first frame is starting gen (needed for DeleteTimeline)
currlayer->startgen = currlayer->algo->getGeneration();
// ensure SaveStartingPattern call in DeleteTimeline will create
// a new temporary .mc file with one frame
currlayer->savestart = true;
}
// -----------------------------------------------------------------------------
bool TimelineExists()
{
// on Linux MainFrame::OnIdle is called before currlayer is set
return currlayer && currlayer->algo->getframecount() > 0;
}
// -----------------------------------------------------------------------------
bool AutoPlay()
{
// assume currlayer->algo->getframecount() > 0
if (currlayer->autoplay == 0) return false;
if (currlayer->algo->isrecording()) return false;
int frameinc = 1;
long delay = 0;
if (currlayer->tlspeed > 0) {
// skip 2^tlspeed frames
frameinc = 1 << currlayer->tlspeed;
}
if (currlayer->tlspeed < 0) {
// set delay between each frame
delay = 100 * (-currlayer->tlspeed);
if (stopwatch->Time() - currlayer->lastframe < delay) {
return true; // request another idle event
}
}
#ifdef __WXMSW__
// need to slow things down on Windows!
wxMilliSleep(20);
#endif
if (currlayer->autoplay > 0) {
// play timeline forwards
currlayer->currframe += frameinc;
if (currlayer->currframe >= currlayer->algo->getframecount() - 1) {
currlayer->currframe = currlayer->algo->getframecount() - 1;
currlayer->autoplay = -1; // reverse direction when we hit last frame
tbarptr->UpdateButtons();
}
} else {
// currlayer->autoplay < 0 so play timeline backwards
currlayer->currframe -= frameinc;
if (currlayer->currframe <= 0) {
currlayer->currframe = 0;
currlayer->autoplay = 1; // reverse direction when we hit first frame
tbarptr->UpdateButtons();
}
}
tbarptr->DisplayCurrentFrame();
tbarptr->UpdateScrollBar();
currlayer->lastframe = stopwatch->Time();
return true; // request another idle event
}
// -----------------------------------------------------------------------------
void PlayTimeline(int direction)
{
if (currlayer->algo->isrecording()) return;
if (direction > 0 && currlayer->autoplay > 0) {
currlayer->autoplay = 0;
} else if (direction < 0 && currlayer->autoplay < 0) {
currlayer->autoplay = 0;
} else {
currlayer->autoplay = direction;
}
mainptr->UpdateUserInterface(true);
}
// -----------------------------------------------------------------------------
void PlayTimelineFaster()
{
if (currlayer->algo->isrecording()) return;
if (currlayer->tlspeed < MAXSPEED) {
currlayer->tlspeed++;
if (showtimeline) tbarptr->UpdateSlider();
}
}
// -----------------------------------------------------------------------------
void PlayTimelineSlower()
{
if (currlayer->algo->isrecording()) return;
if (currlayer->tlspeed > MINSPEED) {
currlayer->tlspeed--;
if (showtimeline) tbarptr->UpdateSlider();
}
}
// -----------------------------------------------------------------------------
void ResetTimelineSpeed()
{
if (currlayer->algo->isrecording()) return;
currlayer->tlspeed = 0;
if (showtimeline) tbarptr->UpdateSlider();
}
// -----------------------------------------------------------------------------
bool TimelineIsPlaying()
{
return currlayer->autoplay != 0;
}
|