2 * psychlops_figure_prototype.cpp
3 * Psychlops Standard Library (Universal)
5 * Last Modified 2005/10/05 by Kenchi HOSOKAWA
6 * (C) 2005 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
9 #include "psychlops_widgets_prototype.h"
16 bool hook(Figure *f, bool on_off, Canvas *cnvs) {
19 for(std::vector<Figure*>::iterator i=cnvs->billboard.begin(); i!=cnvs->billboard.end(); i++) {
20 if(*i==f) { return false; }
22 cnvs->billboard.push_back(f);
25 for(std::vector<Figure*>::iterator i=cnvs->billboard.begin(); i!=cnvs->billboard.end(); i++) {
26 if(*i==f) { cnvs->billboard.erase(i); return true; }
34 RectWidget::RectWidget() : Rectangle() {}
35 // RectWidget::RectWidget(const double width__, const double height__) : left(0), top(0), right(width__), bottom(height__) {}
36 // RectWidget::~RectWidget() {}
37 // RectWidget& RectWidget::set(const double width__, const double height__) {
38 // static_cast<double>(left) = 0.0;
39 // static_cast<double>(top) = 0.0;
40 // static_cast<double>(right) = width__;
41 // static_cast<double>(bottom) = height__;
44 // RectWidget& RectWidget::centering(const Point& p) { Rectangle::centering(p); return *this; }
45 // RectWidget& RectWidget::centering(const Drawable& target) { Rectangle::centering(target); return *this; }
46 // RectWidget& RectWidget::centering(const double x, const double y, const double z) { Rectangle::centering(x,y); return *this; }
47 // RectWidget& RectWidget::shift(const double x, const double y, const double z) { Rectangle::shift(x,y); return *this; }
50 Console::LetterStyle::LetterStyle() : line_height_(10) {};
51 Console::Console() : RectWidget(), changed_(false), buffer(0), last_pos_(0) {
52 fill = Color(0.2, 0.2, 0.6, 0.35);
53 stroke = Stroke(Color::white, 2, Stroke::SOLID);
54 textColor = Color::green;
56 Console::Console(const Rectangle &rect) : RectWidget(), changed_(false), buffer(0), last_pos_(0) {
57 fill.set(0.2, 0.2, 0.6, 0.35);
58 stroke = Stroke(Color::white, 2, Stroke::SOLID);
59 textColor = Color::green;
62 Console::Console(const double width, const double height) : RectWidget(), changed_(false), buffer(0), last_pos_(0) {
63 fill.set(0.2, 0.2, 0.6, 0.35);
64 stroke = Stroke(Color::white, 2, Stroke::SOLID);
65 textColor = Color::green;
69 if(buffer!=0) delete [] buffer;
73 Console & Console::set(const Rectangle &rect) {
74 if(buffer==0) buffer = new char[1024];
76 lines_.push_back(std::string());
77 lines_.at(0) = File::decodePath("%TIME_") + "\r";
78 view_.push_back(LineView(lines_.back(), 0, lines_.back().length()-1));
79 lines_.push_back(std::string());
80 view_.push_back(LineView(lines_.back(), 0, 0));
85 Console & Console::set(const double width, const double height) {
86 return set(Rectangle(width, height));
88 Console & Console::hook(bool on_off, Canvas *cnvs) {
89 Psychlops::Widgets::hook(this, on_off, cnvs);
92 Console & Console::draw(Drawable &target) { return draw(left, top, 0, target); }
93 Console & Console::draw(const Color &col, Drawable &target) { return draw(left, top, 0, target); }
94 Console & Console::draw(const Stroke &strk, Drawable &target) { return draw(left, top, 0, target); }
95 Console & Console::draw(const double x, const double y, const double z, Drawable &target) {
96 target.rect(*this, fill);
97 target.rect(*this, stroke);
98 devideStreamIntoLines();
100 for(std::deque<LineView>::iterator i=view_.begin(); i!=view_.end(); i++) {
101 Display::msg((i->target)->substr(i->begin, i->end - i->begin), x+4, y+12 + line_c*10, textColor, Letters::TEXT_ALIGN_LEFT, getWidth()-18);
106 void Console::devideStreamIntoLines() {
107 const int h_limit = ((int)getWidth()-15)/7, v_limit = (int)getHeight()/10-1;
111 //while(!strings_.eof()) {
112 strings_.getline(buffer, 1024);
113 lines_.back() += buffer;
114 char last_char = lines_.back()[lines_.back().length()-1];
115 while(lines_.back().length()-last_pos_>h_limit) {
116 last_pos_ = ((last_pos_/h_limit)+1)*h_limit;
117 view_.back().end = last_pos_-1;
118 view_.push_back(LineView(lines_.back(), last_pos_, last_pos_));
119 if( view_.size()>v_limit ) view_.pop_front();
121 view_.back().end = lines_.back().length()-1;
122 if(last_char=='\r' || last_char=='\n') {
123 lines_.push_back(std::string());
124 view_.push_back(LineView(lines_.back(), 0, 0));
126 if( view_.size()>v_limit ) view_.pop_front();
131 /* int line_c=0, line_limit = getHeight()/8-2;
132 if(last_num_lines_ > line_limit) {
133 for(int i=lines_.size()-1; i>=0; i--) {
134 line_c += lines_.at(i).lines_in_display_;
135 if(line_c>line_limit) {
136 last_begin_line_ = i+1;
145 void Console::save(std::string filename) {
146 std::ofstream f(File::decodePath(filename).c_str());
147 for(int i=0; i<lines_.size(); i++) {
153 Console::LineView::LineView() : target(0), begin(0), end(0) {}
154 Console::LineView::LineView(std::string &t, int b, int e) : target(&t), begin(b), end(e) {}
159 HotKeyWidget::HotKeyWidget(const Keyboard::Key &init_key) : key(init_key), func(0) {}
160 HotKeyWidget::~HotKeyWidget() { hook(this, false); }
161 const Point HotKeyWidget::getDatum() const { return Point(0,0,0); }
162 HotKeyWidget& HotKeyWidget::setDatum(const Point &p) { return *this; }
163 HotKeyWidget& HotKeyWidget::centering(const Point &p) { return *this; }
164 HotKeyWidget& HotKeyWidget::draw(Drawable &drawable) { return *this; }
166 PauseHotKey::PauseHotKey(const Keyboard::Key &init_key) : HotKeyWidget(init_key) {}
167 PauseHotKey& PauseHotKey::draw(Drawable &target) {
168 AppState::ThreadPriority current = AppState::getThreadPriority();
169 if(Input::get(key, Keyboard::pushed)) {
170 while(!Input::get(key, Keyboard::pushed)){
171 AppState::setThreadPriority(AppState::IDLE);
173 AppState::setThreadPriority(current);
178 ExitHotKey::ExitHotKey(const Keyboard::Key &init_key) : HotKeyWidget(init_key) {
180 ExitHotKey& ExitHotKey::draw(Drawable &target) {
181 if(Input::get(key, Keyboard::pushed)) {
187 TabStopHotKey::TabStopHotKey() : HotKeyWidget(Keyboard::tab) {}
188 TabStopHotKey::TabStopHotKey(const Keyboard::Key &init_key) : HotKeyWidget(init_key) {
191 void TabStopHotKey::set(void* target) {
196 if(Display::the_canvas!=0) Display::the_canvas->eventroot.appendTabStop__(target);
198 TabStopHotKey& TabStopHotKey::draw(Drawable &target) {
199 //StackPanel::default_stack.set(400, 1024);
200 //StackPanel::default_stack.align();
201 if(Keyboard::tab.pushed()) {
202 if(Display::the_canvas!=0)
204 if(Keyboard::shift.pressed() || Keyboard::shift_r.pressed())
206 Display::the_canvas->eventroot.prevTabStop();
208 Display::the_canvas->eventroot.nextTabStop();
214 TabStopHotKey TabStopHotKey::tabStopHotKey;
217 bool MoviePlayer::auto_play = false;
218 MoviePlayer* MoviePlayer::now_playing = 0;
219 Prototype::Thread MoviePlayer::thread;
220 Canvas *MoviePlayer::cnvs = 0;
221 Canvas::CanvasMode MoviePlayer::mode;
222 Display *MoviePlayer::disp;
224 void MoviePlayer::paraThread()
228 if(Keyboard::esc.pressed())
232 Prototype::Thread::sleep(1);
236 MoviePlayer::MoviePlayer()
240 MoviePlayer::MoviePlayer(Figures::Movie &movie)
245 MoviePlayer::~MoviePlayer() {}
247 void MoviePlayer::appendMovie(Figures::Movie &movie)
249 movies.push_back(&movie);
253 void MoviePlayer::start(Canvas::CanvasMode mode_, const Display &disp_)
258 disp = (Display*)&disp_;
261 thread.create(&play_loop_in_this_thread);
265 void MoviePlayer::start(Canvas& target)
267 start(¶Thread, target);
269 void MoviePlayer::start(void (*func)(), Canvas& target)
271 if(movies.size()!=1) return;
281 void MoviePlayer::play_loop_in_this_thread()
288 void MoviePlayer::play_loop()
292 if(!auto_play) break;
293 //now_playing->movies[0].current_frame++;
294 //now_playing->movies->current_frame %= now_playing->movies[0].frame.size();
296 now_playing->movies[0]->draw(*cnvs);
300 void MoviePlayer::stop()
305 SequentialScreenshot::SequentialScreenshot()
306 : max_shot(0), current_shot(0), rec(false), saved(false), directory("%USER_DOCUMENTS%"), filePrefix("%EXPNAME__%TIME_") {
308 SequentialScreenshot::SequentialScreenshot(const Rectangle &rect)
309 : max_shot(0), current_shot(0), rec(false), saved(false), directory("%USER_DOCUMENTS%"), filePrefix("%EXPNAME__%TIME_") {
312 SequentialScreenshot::~SequentialScreenshot() {
316 SequentialScreenshot& SequentialScreenshot::set(const Rectangle &rect) {
317 if(movie.size()==0) {
323 SequentialScreenshot& SequentialScreenshot::set(const double width, const double height) {
324 return set(Rectangle(width, height));
326 SequentialScreenshot& SequentialScreenshot::clear() {
327 if(movie.size() != 0) {
328 for(int i=0; i<movie.size(); i++) {
335 SequentialScreenshot& SequentialScreenshot::hook(bool on_off, Canvas *cnvs) {
336 Psychlops::Widgets::hook(this, on_off, cnvs);
339 SequentialScreenshot& SequentialScreenshot::record(int max_num_shots, std::string file_prefix) {
340 if(max_num_shots<0) throw new Exception("SequentialScreenshot: max_num_shots should be a positive nunmber.");
342 filePrefix = file_prefix;
343 max_shot = max_num_shots;
345 if(max_num_shots!=0) {
346 movie.reserve(max_shot);
347 for(int i=0; i<max_shot; i++) {
348 movie.push_back(new Image);
355 const SequentialScreenshot& SequentialScreenshot::stop() {
358 if(Drawable::prime_is_a_canvas()) Display::msg("END", Display::getWidth()-5, 10, Color::red, Letters::TEXT_ALIGN_RIGHT);
362 const SequentialScreenshot& SequentialScreenshot::save() {
364 for(int i=0; i<movie.size(); i++) {
366 sprintf(buf, "%u", i);
367 movie[i]->save(directory+'/'+filePrefix+"_"+buf+".png");
373 SequentialScreenshot& SequentialScreenshot::draw(Drawable &target) {
376 movie.push_back(new Image);
377 if(Drawable::prime_is_a_canvas()) dynamic_cast<Canvas *>(Drawable::prime)->to(*(movie[current_shot]), source);
378 } else if(current_shot<max_shot) {
379 if(Drawable::prime_is_a_canvas()) dynamic_cast<Canvas *>(Drawable::prime)->to(*(movie[current_shot]), source);
388 Color mean(0.5,0.4,0.4,0.5);
389 Canvas* the_canvas = dynamic_cast<Canvas *>(Drawable::prime);
391 rect.set(0,0,the_canvas->getWidth(),source.getTop()).draw(mean);
392 rect.set(0,source.getTop()+1,source.getLeft(),source.getBottom()-1).draw(mean);
393 rect.set(source.getRight(),source.getTop()+1,the_canvas->getWidth(),source.getBottom()-1).draw(mean);
394 rect.set(0,source.getBottom(),the_canvas->getWidth(),the_canvas->getHeight()).draw(mean);
397 if(Drawable::prime_is_a_canvas()) the_canvas->msg("@REC", Display::getWidth()-5, 10, Color::red, Letters::TEXT_ALIGN_RIGHT);
399 if(Keyboard::shift.pressed() && Mouse::left.pressed()) source.set(Mouse::x, Mouse::y, source.getRight(), source.getBottom());
400 if(Keyboard::shift.pressed() && Mouse::right.pressed()) source.set(source.getLeft(), source.getTop(), Mouse::x, Mouse::y);
401 if(Drawable::prime_is_a_canvas()) the_canvas->msg("||STOP", Display::getWidth()-5, 10, Color::gray, Letters::TEXT_ALIGN_RIGHT);
409 Movie::Movie(const Rectangle &rect)
410 : max_shot(0), current_shot(0), rec(false), saved(false), directory("%USER_DOCUMENTS%"), filePrefix("%EXPNAME__%TIME_") {
412 Movie::Movie(const Rectangle &rect)
413 : max_shot(0), current_shot(0), rec(false), saved(false), directory("%USER_DOCUMENTS%"), filePrefix("%EXPNAME__%TIME_") {
420 Movie& Movie::set(const Rectangle &rect);
421 Movie& Movie::set(const double width, const double height);
422 Movie& Movie::load(size_t begin, size_t end) {
426 Movie& Movie::load(const char* format, size_t begin, size_t end) {
430 Movie& Movie::clear() {
431 if(movie.size() != 0) {
432 for(int i=0; i<movie.size(); i++) {
439 Movie& Movie::hook(bool on_off, Canvas *cnvs);
440 Movie& Movie::record(int max_num_shots, std::string file_prefix);
441 Movie& Movie::seek(size_t frame);
442 Movie& Movie::stop();
443 Movie& const Movie::save(size_t begin, size_t end) {
445 for(int i=0; i<movie.size(); i++) {
447 sprintf(buf, "%u", i);
448 movie[i]->save(directory+'/'+filePrefix+"_"+buf+".png");
454 Movie& const Movie::save(const char* format, size_t begin, size_t end) {
456 for(int i=0; i<movie.size(); i++) {
458 sprintf(buf, "%u", i);
459 movie[i]->save(directory+'/'+filePrefix+"_"+buf+".png");
466 Movie& draw(Drawable &target);
467 Movie& play(Drawable &target);
472 } /* <- namespace Widgets */
473 } /* <- namespace Psycholops */