Newer
Older
// Author: Valeriy Onuchin & Fons Rademakers 04/10/98
///////////////////////////////////////////////////////////////////
// Animated Text with cool wave effect.
//
// ROOT implementation of the hello world example borrowed
// from the Qt hello world example.
//
// To run this example do the following:
// $ root
// root [0] gSystem.Load("Hello")
// root [1] Hello h
// <enjoy>
// root [2] .q
//
// Other ROOT fun examples: Tetris, Aclock ...
//
// Begin_Html
// <img src="http://emcal06.rhic.bnl.gov/~onuchin/root/gif/hello_clock.gif">
// End_Html
//
///////////////////////////////////////////////////////////////////
#include <TROOT.h>
#include <TSystem.h>
#include "Hello.h"
ClassImp(Hello)
TChar::TChar(char ch, Coord_t x, Coord_t y) : TText(x, y, "")
{
// Create single character text.
}
Float_t TChar::GetWidth()
{
// Return character width
UInt_t w,h;
if (!TVirtualPad::Pad()) return 0;
Float_t wh = (Float_t)gPad->XtoAbsPixel(gPad->GetX2());
GetTextExtent(w, h, (char*)GetTitle());
return w/wh;
}
Hello::Hello(const char *text) : TTimer(40, kTRUE)
{
// Hello constructor
TChar *ch;
fList = new TList();
if (!TVirtualPad::Pad())
fPad = new TCanvas("Hello:canvas","ROOT says Hello!",200,200,400,200);
else
fPad = (TPad*)gPad;
while(text[fI]) { // create list of characters
ch = new TChar(text[fI]);
ch->SetTextFont(72); // times-bold-r-normal
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
ch->SetTextSize(0.3);
ch->Modify();
fI++;
fList->Add(ch);
}
Draw(); // append this to current pad
Paint(); // calculate new coordinates of chars and paint it
gSystem->AddTimer(this); // start timer= start animation
}
Hello::~Hello()
{
// Clean up hello.
fList->Delete();
delete fList;
}
void Hello::ExecuteEvent(Int_t event, Int_t, Int_t)
{
// Actions when mouse clicked.
if (event == kButton1Up) {
TTimer::Remove();
}
}
Float_t Hello::GetWidth()
{
// Return text width
TChar *ch;
TIter nextin(fList);
Float_t width = 0;
while ((ch = (TChar*)nextin()))
width = width + ch->GetWidth();
return width;
}
void Hello::Paint(Option_t *)
{
// Paint text
static int sin_tbl[16] = {
0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38
};
TChar *ch;
TIter nextin(fList);
Float_t width = GetWidth();
Coord_t xnext = (1-width)/2.; // draw text in the center of the pad
Coord_t y;
Coord_t y0 = 0.5;
fI = (fI+1) & 15;
int i = 0;
while ((ch = (TChar*)nextin())) {
int i16 = (fI+i) & 15;
y = y0 - (Coord_t)sin_tbl[i16]/1500.; // sin-wave coordinates
ch->SetY(y);
ch->SetX(xnext);
ch->SetTextColor(i16+200); // let's use x3d colors
i++;
xnext = xnext + ch->GetWidth(); // x position of next character
ch->Paint(""); // paint character
}
}
void Hello::Print(Option_t * opt) const
{
fList->Print(opt);
}
void Hello::ls(Option_t *opt) const
{
fList->ls(opt);
}