Mercurial > otakunoraifu
diff window/event.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 | ed6c21dde840 |
children | 4416cfac86ae |
line wrap: on
line diff
--- a/window/event.cc +++ b/window/event.cc @@ -25,13 +25,13 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include"SDL.h" -#include"event.h" -#include<vector> -#include<list> -#include<algorithm> -#include<iostream> -#include<sys/stat.h> +#include "SDL.h" +#include "event.h" +#include <vector> +#include <list> +#include <algorithm> +#include <iostream> +#include <sys/stat.h> using namespace std; @@ -129,17 +129,17 @@ void ContainerImplTime::Delete(Time* del iterator it = find(begin(), end(), delete_event); if (it != end()) { it->valid = false; - it->instance = 0; + it->instance = NULL; return; } it = find(new_item.begin(), new_item.end(), delete_event); if (it != end()) { it->valid = false; - it->instance = 0; + it->instance = NULL; return; } - return; } + bool ContainerImplTime::Exec(unsigned int current_time) { if (current_time == Time::NEVER_WAKE) return true; // 呼び出しまでに作製されたitemを追加 @@ -173,51 +173,51 @@ bool ContainerImplTime::Exec(unsigned in class ContainerImplVideo : private vector<Video*> { -public: - bool Exec(void); + public: + bool Exec(void); - ContainerImplVideo(void); - ~ContainerImplVideo(); - void Add(Video* item); - void Delete(Video* item); - void RegisterGlobalMotionFunc(Container::motionfunc, void* pointer); - void DeleteGlobalMotionFunc(Container::motionfunc, void* pointer); - void RegisterGlobalPressFunc(Container::motionfunc, void* pointer); - void DeleteGlobalPressFunc(Container::motionfunc, void* pointer); -private: - struct Motionfunc { - Container::motionfunc func; - void* pointer; - bool operator ==(const Motionfunc& m) const { return func == m.func && pointer == m.pointer;} - }; - list<Motionfunc> motion_vec; - list<Motionfunc> press_vec; - typedef list<Motionfunc>::iterator MotionIterator; - bool is_sorted; -public: - int button_pressed; - int button_released; - int mouse_x, mouse_y; - int new_mouse_x, new_mouse_y; -private: - void SetChanged(void); - static bool SortLess(const Video* pos1, const Video* pos2) { - return pos1 < pos2; - } - void Sort(void); - void Motion(int x, int y); // mouse motion - void Press(void); - void TakeScreenshot(void); - iterator cur_pos; - Video* cur_item; // 現在のフォーカス位置 - int cur_pressed_x, cur_pressed_y; + ContainerImplVideo(void); + ~ContainerImplVideo(); + void Add(Video* item); + void Delete(Video* item); + void RegisterGlobalMotionFunc(Container::motionfunc, void* pointer); + void DeleteGlobalMotionFunc(Container::motionfunc, void* pointer); + void RegisterGlobalPressFunc(Container::motionfunc, void* pointer); + void DeleteGlobalPressFunc(Container::motionfunc, void* pointer); + private: + struct Motionfunc { + Container::motionfunc func; + void* pointer; + bool operator ==(const Motionfunc& m) const { return func == m.func && pointer == m.pointer;} + }; + list<Motionfunc> motion_vec; + list<Motionfunc> press_vec; + typedef list<Motionfunc>::iterator MotionIterator; + bool is_sorted; + public: + int button_pressed; + int button_released; + int mouse_x, mouse_y; + int new_mouse_x, new_mouse_y; + private: + void SetChanged(void); + static bool SortLess(const Video* pos1, const Video* pos2) { + return pos1 < pos2; + } + void Sort(void); + void Motion(int x, int y); // mouse motion + void Press(void); + void TakeScreenshot(void); + iterator cur_pos; + Video* cur_item; // 現在のフォーカス位置 + int cur_pressed_x, cur_pressed_y; }; void ContainerImplVideo::SetChanged(void) { if (is_sorted) { - if (cur_item) { + if (cur_item != NULL) { cur_pos = find(begin(), end(), cur_item); - if (cur_pos == end()) cur_item = 0; + if (cur_pos == end()) cur_item = NULL; } is_sorted = false; } @@ -225,7 +225,7 @@ void ContainerImplVideo::SetChanged(void void ContainerImplVideo::Sort(void) { sort(begin(), end(), SortLess); - if (cur_item) { + if (cur_item != NULL) { cur_pos = lower_bound(begin(), end(), cur_item, SortLess); } else { cur_pos = end(); @@ -237,16 +237,19 @@ ContainerImplVideo::ContainerImplVideo(v is_sorted = false; button_pressed = 0; button_released = 0; - cur_item = 0; + cur_item = NULL; mouse_x = 0; mouse_y = 0; new_mouse_x = 0; new_mouse_y = 0; } + ContainerImplVideo::~ContainerImplVideo(void) { }; + void ContainerImplVideo::Add(Video* event) { push_back(event); SetChanged(); } + void ContainerImplVideo::Delete(Video* delete_event) { iterator it = find(begin(), end(), delete_event); if (it != end()) { @@ -264,11 +267,11 @@ void ContainerImplVideo::Delete(Video* d } if (delete_event == cur_item) { cur_pos = end(); - cur_item = 0; + cur_item = NULL; Motion(mouse_x, mouse_y); } - return; } + void ContainerImplVideo::RegisterGlobalMotionFunc(Container::motionfunc func, void* pointer) { Motionfunc f; f.func = func; @@ -277,6 +280,7 @@ void ContainerImplVideo::RegisterGlobalM motion_vec.push_back(f); } } + void ContainerImplVideo::DeleteGlobalMotionFunc(Container::motionfunc func, void* pointer) { Motionfunc f; f.func = func; @@ -284,8 +288,8 @@ void ContainerImplVideo::DeleteGlobalMot list<Motionfunc>::iterator it = find(motion_vec.begin(), motion_vec.end(), f); if (it != motion_vec.end()) motion_vec.erase(it); - return; } + void ContainerImplVideo::RegisterGlobalPressFunc(Container::motionfunc func, void* pointer) { Motionfunc f; f.func = func; @@ -294,6 +298,7 @@ void ContainerImplVideo::RegisterGlobalP press_vec.push_back(f); } } + void ContainerImplVideo::DeleteGlobalPressFunc(Container::motionfunc func, void* pointer) { Motionfunc f; f.func = func; @@ -301,8 +306,8 @@ void ContainerImplVideo::DeleteGlobalPre list<Motionfunc>::iterator it = find(press_vec.begin(), press_vec.end(), f); if (it != press_vec.end()) press_vec.erase(it); - return; } + void ContainerImplVideo::Motion(int x, int y) { mouse_x = x; mouse_y = y; MotionIterator mit; @@ -319,7 +324,7 @@ void ContainerImplVideo::Motion(int x, i if (cur_item) cur_item->Drag(cur_pressed_x, cur_pressed_y, x, y); return; } - if (cur_item) cur_item->Motion(x,y); + if (cur_item != NULL) cur_item->Motion(x,y); int z = -1; iterator z_it; iterator it; for (it = begin(); it != end(); it++) { @@ -339,7 +344,7 @@ void ContainerImplVideo::Motion(int x, i } else { if (cur_item) cur_item->Out(); cur_pos = end(); - cur_item = 0; + cur_item = NULL; } return; } @@ -361,6 +366,7 @@ void ContainerImplVideo::Press(void) { mit = mit_next; } } + void ContainerImplVideo::TakeScreenshot(void) { int n=0; char filename[1024]; @@ -372,118 +378,134 @@ void ContainerImplVideo::TakeScreenshot( } SDL_SaveBMP(SDL_GetVideoSurface(), filename); } + bool ContainerImplVideo::Exec(void) { - bool is_mouse_motion = false; int motion_x = 0, motion_y = 0; SDL_Event event; SDL_PumpEvents(); while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS) == 1) { switch(event.type) { - case SDL_QUIT: return false; // @@@ なにかやらないと - case SDL_ACTIVEEVENT: // なにもしない - // cout<<"active : gain "<<int(event.active.gain)<<", state "<<int(event.active.state)<<endl; - break; - case SDL_KEYDOWN: - if (!is_sorted) Sort(); - switch(event.key.keysym.sym) { - case SDLK_F12: - case SDLK_PRINT: - case SDLK_p: // for Zaurus - TakeScreenshot(); - break; - // Some window managers (eg enlightenment) use Alt-Enter for - // themselves, F11 is a good alternative - case SDLK_F11: - SDL_WM_ToggleFullScreen(SDL_GetVideoSurface()); + case SDL_QUIT: return false; // @@@ なにかやらないと + case SDL_ACTIVEEVENT: // なにもしない + // cout<<"active : gain "<<int(event.active.gain)<<", state "<<int(event.active.state)<<endl; break; - case SDLK_RETURN: - if (SDL_GetModState() & KMOD_ALT) { - SDL_WM_ToggleFullScreen(SDL_GetVideoSurface()); - break; + case SDL_KEYDOWN: + if (!is_sorted) Sort(); + switch(event.key.keysym.sym) { + case SDLK_F12: + case SDLK_PRINT: + case SDLK_p: // for Zaurus + TakeScreenshot(); + break; + // Some window managers (eg enlightenment) use Alt-Enter for + // themselves, F11 is a good alternative + case SDLK_F11: + SDL_WM_ToggleFullScreen(SDL_GetVideoSurface()); + break; + case SDLK_RETURN: + if (SDL_GetModState() & KMOD_ALT) { + SDL_WM_ToggleFullScreen(SDL_GetVideoSurface()); + break; + } + case SDLK_SPACE: + Press(); + break; + case SDLK_TAB: // move to next widget + if (cur_pos != end()) cur_pos++; + if (cur_pos == end()) cur_pos = begin(); + if (cur_pos != end()) { + cur_item = *cur_pos; + cur_item->In(); + } else { + cur_item = NULL; + } + break; + case SDLK_LEFT: if (cur_pos != end()) (*cur_pos)->KeyLeft(); break; + case SDLK_RIGHT:if (cur_pos != end()) (*cur_pos)->KeyRight(); break; + case SDLK_LSHIFT: case SDLK_RSHIFT: button_pressed |= (1<<KEY_SHIFT); break; + case SDLK_ESCAPE: button_pressed |= (1<<MOUSE_RIGHT); break; /* for Zaurus */ + case SDLK_s: save_req = true; break; + case SDLK_l: load_req = true; break; + case SDLK_g: grpdump_req = true; break; + case SDLK_a: pressAreq = true; break; + case SDLK_d: pressDreq = true; break; + case SDLK_f: pressFreq = true; break; } - case SDLK_SPACE: - Press(); break; - case SDLK_TAB: // move to next widget - if (cur_pos != end()) cur_pos++; - if (cur_pos == end()) cur_pos = begin(); - if (cur_pos != end()) { - cur_item = *cur_pos; - cur_item->In(); - } else { - cur_item = 0; + case SDL_KEYUP: + // cout << "keyup which "<<int(event.key.which)<<", sym "<<int(event.key.keysym.sym)<<endl; + switch(event.key.keysym.sym) { + case SDLK_RETURN: case SDLK_SPACE: + if (cur_item) cur_item->Release(); + case SDLK_LSHIFT: case SDLK_RSHIFT: button_pressed &= ~(1<<KEY_SHIFT); button_released |= 1<<KEY_SHIFT; break; + case SDLK_ESCAPE: button_pressed &= ~(1<<MOUSE_RIGHT); button_released |= 1<<MOUSE_RIGHT; break; /* for Zaurus */ } break; - case SDLK_LEFT: if (cur_pos != end()) (*cur_pos)->KeyLeft(); break; - case SDLK_RIGHT:if (cur_pos != end()) (*cur_pos)->KeyRight(); break; - case SDLK_LSHIFT: case SDLK_RSHIFT: button_pressed |= (1<<KEY_SHIFT); break; - case SDLK_ESCAPE: button_pressed |= (1<<MOUSE_RIGHT); break; /* for Zaurus */ - case SDLK_s: save_req = true; break; - case SDLK_l: load_req = true; break; - case SDLK_g: grpdump_req = true; break; - case SDLK_a: pressAreq = true; break; - case SDLK_d: pressDreq = true; break; - case SDLK_f: pressFreq = true; break; - } - break; - case SDL_KEYUP: - // cout << "keyup which "<<int(event.key.which)<<", sym "<<int(event.key.keysym.sym)<<endl; - switch(event.key.keysym.sym) { - case SDLK_RETURN: case SDLK_SPACE: - if (cur_item) cur_item->Release(); - case SDLK_LSHIFT: case SDLK_RSHIFT: button_pressed &= ~(1<<KEY_SHIFT); button_released |= 1<<KEY_SHIFT; break; - case SDLK_ESCAPE: button_pressed &= ~(1<<MOUSE_RIGHT); button_released |= 1<<MOUSE_RIGHT; break; /* for Zaurus */ - } - break; - case SDL_MOUSEMOTION: - motion_x = event.motion.x; - motion_y = event.motion.y; - is_mouse_motion = true; - // Motion(event.motion.x, event.motion.y); - // cout<< "motion which "<<int(event.motion.which)<< - // "x "<<event.motion.x << "y "<<event.motion.y<<endl; - break; - case SDL_MOUSEBUTTONUP: - if (event.button.button == 1) { - Motion(event.button.x, event.button.y); - is_mouse_motion = false; - if (cur_item) cur_item->Release(); - } - switch(event.button.button) { - case 1: button_pressed &= ~(1<<MOUSE_LEFT); button_released |= 1<<MOUSE_LEFT; break; - case 2: button_pressed &= ~(1<<MOUSE_MIDDLE); button_released |= 1<<MOUSE_MIDDLE; break; - case 3: button_pressed &= ~(1<<MOUSE_RIGHT); button_released |= 1<<MOUSE_RIGHT; break; - case 4: button_pressed &= ~(1<<MOUSE_UP); button_released |= 1<<MOUSE_UP; break; - case 5: button_pressed &= ~(1<<MOUSE_DOWN); button_released |= 1<<MOUSE_DOWN; break; - } - break; - case SDL_MOUSEBUTTONDOWN: - if (event.button.button == 1) { - Motion(event.button.x, event.button.y); - is_mouse_motion = false; - Press(); - } - switch(event.button.button) { - case 1: button_pressed |= (1<<MOUSE_LEFT); break; - case 2: button_pressed |= (1<<MOUSE_MIDDLE); break; - case 3: button_pressed |= (1<<MOUSE_RIGHT); break; - case 4: button_pressed |= (1<<MOUSE_UP); break; - case 5: button_pressed |= (1<<MOUSE_DOWN); break; - } - // cout << "mouse which "<<int(event.button.which)<<"button "<<int(event.button.button)<< - // "state "<<int(event.button.state)<<"x "<<event.button.x << "y "<<event.button.y<<endl; - break; - case SDL_VIDEOEXPOSE: // redraw の必要がある? - // cout<<"expose."<<endl; - break; + case SDL_MOUSEMOTION: + motion_x = event.motion.x; + motion_y = event.motion.y; + is_mouse_motion = true; + // Motion(event.motion.x, event.motion.y); + // cout<< "motion which "<<int(event.motion.which)<< + // "x "<<event.motion.x << "y "<<event.motion.y<<endl; + break; + case SDL_MOUSEBUTTONUP: + if (event.button.button == 1) { + Motion(event.button.x, event.button.y); + is_mouse_motion = false; + if (cur_item) cur_item->Release(); + } + switch(event.button.button) { + case 1: + button_pressed &= ~(1<<MOUSE_LEFT); + button_released |= 1<<MOUSE_LEFT; + break; + case 2: + button_pressed &= ~(1<<MOUSE_MIDDLE); + button_released |= 1<<MOUSE_MIDDLE; + break; + case 3: + button_pressed &= ~(1<<MOUSE_RIGHT); + button_released |= 1<<MOUSE_RIGHT; + break; + case 4: + button_pressed &= ~(1<<MOUSE_UP); + button_released |= 1<<MOUSE_UP; + break; + case 5: + button_pressed &= ~(1<<MOUSE_DOWN); + button_released |= 1<<MOUSE_DOWN; + break; + } + break; + case SDL_MOUSEBUTTONDOWN: + if (event.button.button == 1) { + Motion(event.button.x, event.button.y); + is_mouse_motion = false; + Press(); + } + switch(event.button.button) { + case 1: button_pressed |= (1<<MOUSE_LEFT); break; + case 2: button_pressed |= (1<<MOUSE_MIDDLE); break; + case 3: button_pressed |= (1<<MOUSE_RIGHT); break; + case 4: button_pressed |= (1<<MOUSE_UP); break; + case 5: button_pressed |= (1<<MOUSE_DOWN); break; + } + // cout << "mouse which "<<int(event.button.which)<<"button "<<int(event.button.button)<< + // "state "<<int(event.button.state)<<"x "<<event.button.x << "y "<<event.button.y<<endl; + break; + case SDL_VIDEOEXPOSE: // redraw の必要がある? + // cout<<"expose."<<endl; + break; } } // Motion 呼び出しは一回だけ if (is_mouse_motion) Motion(motion_x, motion_y); return true; -}; +} + /* Impl: struct Event::Container */ Container::Container(void) { pimpl_video = new ContainerImplVideo; @@ -495,37 +517,47 @@ Container::Container(void) { } button_pressed = 0; current_time = 0; - int i; for (i=0; i<BUTTON_MAX; i++) button_presscount[i] = 0; - return; + int i; + for (i=0; i<BUTTON_MAX; i++) button_presscount[i] = 0; } + Container::~Container(void) { delete pimpl_video; delete pimpl_time; } + void Container::Add(Video* item) { pimpl_video->Add(item); } + void Container::Delete(Video* item) { pimpl_video->Delete(item); } + void Container::Add(Time* item) { pimpl_time->Add(item); } + void Container::Delete(Time* item) { pimpl_time->Delete(item); } + void Container::RegisterGlobalMotionFunc(Container::motionfunc f, void* pointer) { pimpl_video->RegisterGlobalMotionFunc(f, pointer); } + void Container::DeleteGlobalMotionFunc(Container::motionfunc f, void* pointer) { pimpl_video->DeleteGlobalMotionFunc(f, pointer); } + void Container::RegisterGlobalPressFunc(Container::motionfunc f, void* pointer) { pimpl_video->RegisterGlobalPressFunc(f, pointer); } + void Container::DeleteGlobalPressFunc(Container::motionfunc f, void* pointer) { pimpl_video->DeleteGlobalPressFunc(f, pointer); } + bool Container::Exec(unsigned int time) { current_time = time; bool ret = true; @@ -553,6 +585,7 @@ bool Container::pressed(int mask) { if (mask < 0 || mask >= BUTTON_MAX) return 0; return (button_pressed & (1<<mask)) != 0; } + bool Container::presscount(int mask) { if (mask < 0 || mask >= BUTTON_MAX) return 0; int count = button_presscount[mask]; @@ -560,7 +593,7 @@ bool Container::presscount(int mask) { return count; } -}; /* end of namespace Container */ +} /* end of namespace Container */ // 問題: // z 軸と xy 軸の相互干渉;高速化