Mercurial > otakunoraifu
diff window/widget.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 | 5f548e5957a8 |
children | ddbcbd000206 |
line wrap: on
line diff
--- a/window/widget.cc +++ b/window/widget.cc @@ -25,10 +25,10 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include"widget.h" -#include<algorithm> -#include<map> -#include<string> +#include "widget.h" +#include <algorithm> +#include <map> +#include <string> Rect DSurfaceRenderText(TextGlyphStream::iterator start, TextGlyphStream::iterator end, const Rect& srcrect, Surface* dst, const Rect& dstrect); @@ -49,6 +49,7 @@ void SetFont(const char* font) { size_to_layout.clear(); wdefault_font = font; } + // namespace Widget { #define TimeCursor WidTimeCursor #define MouseCursor WidMouseCursor @@ -72,16 +73,21 @@ XKFont::HorizLayout* DefaultLayout(int t void PicWidget::activate(void) { } + void PicWidget::deactivate(void) { } + void PicWidget::SetRegion(const Rect& apos) { } + void PicWidget::show(void) { Pic()->show(); } + void PicWidget::hide(void) { Pic()->hide(); } + void PicWidget::show_all(void) { Pic()->show_all(); } @@ -119,6 +125,7 @@ MouseCursor::MouseCursor(Event::Containe x = 0; y = 0; container.RegisterGlobalMotionFunc(&Motionfunc, (void*)this); } + MouseCursor::MouseCursor(Event::Container& _container, PicContainer* parent, Surface* s, int x, int y, int w, int h) : Event::Video(_container), container(_container) { int sx, sy; @@ -148,37 +155,42 @@ Button::Button(Event::Container& contain show(); is_in = false; is_toggled = false; - press_func = 0; - press_pointer = 0; - drag_func = 0; - drag_pointer = 0; + press_func = NULL; + press_pointer = NULL; + drag_func = NULL; + drag_pointer = NULL; is_toggle_switch = false; } + Button::Button(Event::Container& container, PicContainer* parent, Surface* s, int _sx, int _sy, int _sdx, int _sdy, int _nptn, const Rect& r, int _z) : sx(_sx), sy(_sy), sdx(_sdx), sdy(_sdy), nptn(_nptn) ,Event::Video(container,r, _z) { SetPic(parent->create_leaf(r, 0)); Pic()->SetSurface(s, _sx, _sy); show(); is_in = false; is_toggled = false; - press_func = 0; - press_pointer = 0; - drag_func = 0; - drag_pointer = 0; + press_func = NULL; + press_pointer = NULL; + drag_func = NULL; + drag_pointer = NULL; is_toggle_switch = false; } + Button::~Button() { } + void Button::In(void) { is_in = true; if (nptn > 1) if (! is_toggled) Pic()->SetSurfacePos(sx+sdx, sy+sdy); } + void Button::Out(void) { is_in = false; if (!is_toggled) Pic()->SetSurfacePos(sx, sy); } + void Button::Toggle(bool new_toggle) { if (is_toggled == new_toggle) { return; @@ -196,6 +208,7 @@ void Button::Toggle(bool new_toggle) { Pic()->SetSurfacePos(sx, sy); } } + void Button::Press(void) { is_in = true; if (is_toggled) ; @@ -206,6 +219,7 @@ void Button::Press(void) { if (press_func) press_func(press_pointer, this); if (is_toggle_switch) Toggle(!is_toggled); } + void Button::Release(void) { if (is_toggled) ; else if (nptn > 1 && is_in) @@ -213,6 +227,7 @@ void Button::Release(void) { else if (nptn > 1) Pic()->SetSurfacePos(sx, sy); } + void Button::Drag(int x_from, int y_from, int x_to, int y_to) { if (drag_func) drag_func(x_from,y_from,x_to, y_to,drag_pointer, this); } @@ -224,16 +239,17 @@ Scale::Scale(Event::Container& _containe value(0), value_add(0), value_dragstart(0), is_vertical(_is_vertical), change_func(0), change_pointer(0) { - arrow_down = 0; - arrow_up = 0; - cursor = 0; - panel = 0; + arrow_down = NULL; + arrow_up = NULL; + cursor = NULL; + panel = NULL; Init(r_orig); } extern char* create_button(int number, int& width, int& height, int r, int g, int b); extern char* create_box(int& width, int& height, int r, int g, int b); + void Scale::Init(Rect r_orig) { int r=cursor_color.r, g=cursor_color.g, b=cursor_color.b; // 矢印 @@ -322,8 +338,8 @@ void Scale::Init(Rect r_orig) { void Scale::InitCursor(int width_ratio) { int r=cursor_color.r, g=cursor_color.g, b=cursor_color.b; - if (cursor) delete cursor; - cursor = 0; + if (cursor != NULL) delete cursor; + cursor = NULL; Rect region(0,0); if (width_ratio < 0) width_ratio = 0; else if (width_ratio > 1024) width_ratio = 1024; @@ -359,22 +375,23 @@ void Scale::InitCursor(int width_ratio) if (bar_width <= 0) value_add = max-min; else if (cursor_width == 0) value_add = 2; else value_add = scale_max*cursor_width/bar_width; - - return; } void Scale::PressArrowDown(void* pointer, Button* from) { Scale* self = (Scale*)pointer; self->SetScaleValue(self->value + self->value_add); } + void Scale::PressArrowUp(void* pointer, Button* from){ Scale* self = (Scale*)pointer; self->SetScaleValue(self->value - self->value_add); } + void Scale::PressCursor(void* pointer, Button* from){ Scale* self = (Scale*)pointer; self->value_dragstart = self->value; } + void Scale::DragCursor(int x_from, int y_from,int x, int y, void* pointer, Button* from){ Scale* self = (Scale*)pointer; int dx, w; @@ -388,6 +405,7 @@ void Scale::DragCursor(int x_from, int y if (w == 0) return; self->SetScaleValue(self->value_dragstart + dx*scale_max/w); } + int Scale::CalcValue(void) { Rect own_region = Event::Video::Region(); int x, w; @@ -403,12 +421,13 @@ int Scale::CalcValue(void) { else if (x > w) x = w; return x*scale_max/w; } + void Scale::Press(void){ int v = CalcValue(); if (v < value) SetScaleValue(value-value_add); else SetScaleValue(value+value_add); - return; } + void Scale::Motion(int x, int y){ mouse_x = x; mouse_y = y; @@ -418,8 +437,8 @@ void Scale::SetRange(int new_min, int ne min = new_min; max = new_max; SetValue(value); - return; } + void Scale::SetValue(int new_value) { if (min == max) { value = min; @@ -429,9 +448,11 @@ void Scale::SetValue(int new_value) { int scale_value = (new_value-min) * scale_max / (max-min); SetScaleValue(scale_value); } + int Scale::GetValue(void) const{ return min + value * (max-min) / scale_max; } + void Scale::SetScaleValue(int new_value) { if (new_value < 0) value = 0; else if (new_value > scale_max) value = scale_max; @@ -450,7 +471,6 @@ void Scale::SetScaleValue(int new_value) if (change_func) { (*change_func)(change_pointer, this); } - return; } TextButton::TextButton(Event::Container& container, PicContainer* parent, const char* text, int _text_size, Attribute attr, const Rect& r_orig, int _z, const Color& _fore, const Color& _pressed, const Color& _back) : @@ -462,7 +482,7 @@ TextButton::TextButton(Event::Container& // まず、テキスト領域の広さを得る Rect r(r_orig); - if (text == 0) text = ""; + if (text == NULL) text = ""; int width = r.width(); int height = r.height(); if (width == 0) width = parent->Width() - r.lx; @@ -485,12 +505,15 @@ TextButton::TextButton(Event::Container& Pic()->SetSurfaceRect(r); } - sx = 0; sy = 0; sdx = 0; sdy = r.height(); nptn = 3; + sx = 0; sy = 0; sdx = 0; + sdy = r.height(); + nptn = 3; int x = 0, y = 0; if (attribute & CENTER) x = (Pic()->Width() - gs.width()) / 2; y = (Pic()->Height() - gs.height()) / 2; + //FIXME: (back.a == NULL ?) if (back.a == 0) { // 背景なし、もしくはボタン押の状態のみ背景あり surface = root.NewSurface(r.width(), r.height()*2, ALPHA_MASK); DSurfaceFill(surface, Rect(*surface), 0, 0, 0, 0); @@ -530,11 +553,12 @@ TextButton::TextButton(Event::Container& Pic()->SetSurface(surface, 0, 0); show(); } + void TextButton::SetText(const char* text, const Color& _fore, const Color& _pressed, const Color& _back) { int width = Pic()->Width(); int height = Pic()->Height(); // まず、テキスト領域の広さを得る - if (text == 0) text = ""; + if (text == NULL) text = ""; TextGlyphStream gs = DefaultLayout(text_size)->Layout(text, width); @@ -547,8 +571,9 @@ void TextButton::SetText(const char* tex int surf_y = Pic()->SurfacePosY(); Pic()->SetSurface( (Surface*)0,0,0); root.DeleteSurface(surface); - surface = 0; + surface = NULL; + //FIXME: (back.a == NULL) ? if (back.a == 0) { // 背景なし surface = root.NewSurface(width, height*2, ALPHA_MASK); DSurfaceFill(surface, Rect(*surface), 0, 0, 0, 0); @@ -586,8 +611,7 @@ void TextButton::SetText(const char* tex TextButton::~TextButton() { if (surface) root.DeleteSurface(surface); - surface = 0; - return; + surface = NULL; } Text::Text(Event::Container& container, PicContainer* parent, const Rect& r, const Rect& text_r, int _fontsize) : @@ -622,6 +646,7 @@ Text::~Text() { void Text::SetSpeed(int new_speed) { speed = new_speed; } + void Text::SetWait(int new_wait) { if (new_wait < 0) new_wait = 100000000; wait_delay = new_wait; @@ -637,6 +662,7 @@ int Text::CalcScrollHeight(void) { if (i == line_number) i = line_number + 1; return i - line_number - 1; } + void Text::Elapsed(unsigned int current_time) { SetWakeup(current_time + 50); if (status == PREPARE) { @@ -710,7 +736,6 @@ label_wait2: if (cursor) cursor->hide(); } status = PREPARE; - return; } bool Text::Pressed(int x, int y, void* pointer) { @@ -776,7 +801,6 @@ void Text::DrawText(int& nChar) { Rect r = DSurfaceRenderText(cur_pos, it, srcrect, surface, Rect(0,0,0,0)); pictext->ReBlit(r); cur_pos = it; - return; } void Text::Scrollup(int& nChar) { @@ -811,7 +835,6 @@ void Text::Scrollup(int& nChar) { r.ty = r.by-cur_dy; DSurfaceFill(surface, r, 0, 0, 0, 0); pictext->ReBlit(); - return; } void Text::activate(void) { @@ -820,12 +843,14 @@ void Text::activate(void) { window_activated = true; if (cursor_activated && window_activated && cursor) cursor->show(); } + void Text::deactivate(void) { event.DeleteGlobalPressFunc(&Pressed, (void*)this); Event::Video::deactivate(); window_activated = false; if (cursor) cursor->hide(); } + void Text::SetCursor(TimeCursor* c) { cursor = c; if (c) { @@ -840,8 +865,9 @@ Label::Label(PicContainer* parent, const text_size(_text_size) { Rect r(r_orig); - if (text == 0) text = ""; - int width = r.width(); int height = r.height(); + if (text == NULL) text = ""; + int width = r.width(); + int height = r.height(); if (width == 0) width = parent->Width() - r.lx; TextGlyphStream gs = DefaultLayout(text_size)->Layout(text, width); @@ -871,11 +897,13 @@ Label::Label(PicContainer* parent, const Pic()->SetSurface(surface, 0, 0); show(); } + Label::~Label() { root.DeleteSurface(surface); } + void Label::SetText(const char* text) { - if (text == 0) text = ""; + if (text == NULL) text = ""; TextGlyphStream gs = DefaultLayout(text_size)->Layout(text, Pic()->Width()); DSurfaceFill(surface, Rect(*surface), 0, 0, 0, 0); int x = 0, y = 0; @@ -893,8 +921,8 @@ Dialog::Dialog(Event::Container& contain int x,y; status = WAIT; - set_func = 0; - set_pointer = 0; + set_func = NULL; + set_pointer = NULL; XKFont::HorizLayout& layout = *DefaultLayout(26); int dialog_width = parent->Width() / 2; @@ -969,12 +997,12 @@ Dialog::Dialog(Event::Container& contain show_all(); } + Dialog::~Dialog() { PicRoot& root = PicNode()->Root(); - SetPic(0); + SetPic(NULL); root.DeleteSurface(surface_btn); root.DeleteSurface(surface_diag); - return; } void Dialog::press_ok(void* pointer, Button* btn) { @@ -986,6 +1014,7 @@ void Dialog::press_ok(void* pointer, But } } } + void Dialog::press_cancel(void* pointer, Button* btn) { if (pointer) { Dialog* wid = (Dialog*)pointer; @@ -1016,12 +1045,14 @@ AnmTime::AnmTime(Event::Container& conta status = FINISHED; if (total_time == 0) total_time = 1; } + AnmTime::AnmTime(Event::Container& container, std::vector<PicBase*> _pic, int _total_time, int _all_count) : Event::Time(container), PicAnm(_pic), start_time(0), total_time(_total_time), all_count(_all_count) { status = FINISHED; if (total_time == 0) total_time = 1; } + void AnmTime::Elapsed(unsigned int current_time) { if (total_time == 0) return; if (status == FINISHED || current_time == 0) {SetWakeup(current_time+1); return;} @@ -1040,8 +1071,8 @@ void AnmTime::Elapsed(unsigned int curre Finish(); status = FINISHED; } - return; } + void AnmTime::Abort(void) { if (status == FINISHED) return; if (start_time == 0) { @@ -1053,9 +1084,11 @@ void AnmTime::Abort(void) { } status = FINISHED; } + bool AnmTime::IsEnd(void) { return status == FINISHED; } + AnmMove::AnmMove(Event::Container& container, PicBase* _pic, const Rect& _to, int total_time) : AnmTime(container, _pic, total_time), from(0,0), to(_to) { @@ -1072,14 +1105,17 @@ AnmMove::AnmMove(Event::Container& conta if (dx == 0) dx = 1; SetAllCount(dx); } + void AnmMove::Exec(int count) { Rect r(from); int dx = to.lx - from.lx; int dy = to.ty - from.ty; r.rmove(dx*count/all_count, dy*count/all_count); iterator it; - for (it=pic.begin(); it!=pic.end(); it++) (*it)->Move(r.lx, r.ty); + for (it=pic.begin(); it!=pic.end(); it++) + (*it)->Move(r.lx, r.ty); } + AnmAlpha::AnmAlpha(Event::Container& container, PicBase* _pic, int alpha_from, int alpha_to, int total_time) : AnmTime(container, _pic, total_time), from(alpha_from), to(alpha_to), alpha_r(0,0,1,1) { @@ -1092,6 +1128,7 @@ AnmAlpha::AnmAlpha(Event::Container& con if (c == 0) c = 1; SetAllCount(c); } + AnmAlpha::AnmAlpha(Event::Container& container, std::vector<PicBase*> _pic, int alpha_from, int alpha_to, int total_time) : AnmTime(container, _pic, total_time), from(alpha_from), to(alpha_to), alpha_r(0,0,1,1) { @@ -1104,15 +1141,20 @@ AnmAlpha::AnmAlpha(Event::Container& con if (c == 0) c = 1; SetAllCount(c); } + void AnmAlpha::Start(void) { iterator it; - for (it=pic.begin(); it!=pic.end(); it++) (*it)->show(); + for (it=pic.begin(); it!=pic.end(); it++) + (*it)->show(); } + void AnmAlpha::Exec(int count) { alpha = (from * (all_count-count) + (to-from) * count) / all_count; iterator it; - for (it=pic.begin(); it!=pic.end(); it++) (*it)->SetSurfaceAlpha(&alpha, alpha_r); + for (it=pic.begin(); it!=pic.end(); it++) + (*it)->SetSurfaceAlpha(&alpha, alpha_r); } + void AnmAlpha::Finish(void) { iterator it; for (it=pic.begin(); it!=pic.end(); it++) { @@ -1121,9 +1163,11 @@ void AnmAlpha::Finish(void) { (*it)->SetSurfaceAlpha(0,Rect(0,0)); } } + AnmAlphaMove::AnmAlphaMove(Event::Container& container, PicBase* _pic) : AnmTime(container, _pic, 0) { } + void AnmAlphaMove::SetPtn(void) { int total = 0; std::vector<Ptn>::iterator it; @@ -1134,6 +1178,7 @@ void AnmAlphaMove::SetPtn(void) { SetTotalTime(total); cur_count = 0; } + void AnmAlphaMove::Exec(int count) { if (ptns.empty()) return; if (cur_count != 0 && ptns[cur_count].next_tick > count) return; @@ -1161,6 +1206,7 @@ void AnmAlphaMove::Exec(int count) { else { (*p)->show(); (*p)->SetSurfaceAlpha( &(it->alpha), Rect(0,0,1,1)); } } } + void AnmAlphaMove::Finish(void) { if (ptns.empty()) return; if (cur_count >= ptns.size() - 1) return; @@ -1199,8 +1245,10 @@ void AnmPtnSolid::Exec(int count) { else alpha[i] = 0; } iterator it; - for (it=pic.begin(); it!=pic.end(); it++) (*it)->SetSurfaceAlpha(alpha, alpha_r); + for (it=pic.begin(); it!=pic.end(); it++) + (*it)->SetSurfaceAlpha(alpha, alpha_r); } + void AnmPtnAlpha::Exec(int count) { int i; int ptn_zero = count; @@ -1216,16 +1264,22 @@ void AnmPtnAlpha::Exec(int count) { void AnmPtnSolid::Start(void) { iterator it; - for (it=pic.begin(); it!=pic.end(); it++) (*it)->show(); + for (it=pic.begin(); it!=pic.end(); it++) + (*it)->show(); } + void AnmPtnSolid::Finish(void) { iterator it; - for (it=pic.begin(); it!=pic.end(); it++) (*it)->SetSurfaceAlpha(0, Rect(0,0)); + for (it=pic.begin(); it!=pic.end(); it++) + (*it)->SetSurfaceAlpha(0, Rect(0,0)); } + void AnmPtnAlpha::Start(void) { iterator it; - for (it=pic.begin(); it!=pic.end(); it++) (*it)->show(); + for (it=pic.begin(); it!=pic.end(); it++) + (*it)->show(); } + void AnmPtnAlpha::Finish(void) { iterator it; for (it=pic.begin(); it!=pic.end(); it++) (*it)->SetSurfaceAlpha(0, Rect(0,0));