From 6f594ad00a07365eec68e16d338151dde23bb648 Mon Sep 17 00:00:00 2001 From: pixel Date: Thu, 1 Dec 2005 13:48:12 +0000 Subject: Lots of fixes and adds in mogltk: -) fixed a compilation bug in the engine.h file (widget cyclic dependancy) -) fixed font system so to have unsigned chars instead of chars -) updated the shaper system so to have different fonts to print -) updated the widgets with: -) new methods: center, set_viewport, delete_me -) Label now has a font -) MsgBox now has a font -) InputText widget added -) InputDialog message box added -) fixed a bug in the engine causing unwanted mouse move events -) fixed a bug in the various lists of widgets (.clear != .empty) --- include/engine.h | 6 +- include/font.h | 8 +-- include/shape.h | 7 ++- include/widgets.h | 53 ++++++++++++++-- lib/engine.cc | 8 +-- lib/font.cc | 10 +-- lib/shape.cc | 24 ++++---- lib/widgets.cc | 178 ++++++++++++++++++++++++++++++++++++++++++++++++------ 8 files changed, 240 insertions(+), 54 deletions(-) diff --git a/include/engine.h b/include/engine.h index 73d12dd..9d597b2 100644 --- a/include/engine.h +++ b/include/engine.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: engine.h,v 1.20 2004-11-27 21:48:01 pixel Exp $ */ +/* $Id: engine.h,v 1.21 2005-12-01 13:48:12 pixel Exp $ */ #ifndef __ENGINE_H__ #define __ENGINE_H__ @@ -27,9 +27,9 @@ #include #include #include -#include namespace mogltk { + class widget; class engine : public Base { public: class keyevent : public Base { @@ -98,4 +98,6 @@ namespace mogltk { }; }; +#include + #endif diff --git a/include/font.h b/include/font.h index 9530a37..6cf0e93 100644 --- a/include/font.h +++ b/include/font.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: font.h,v 1.10 2004-11-27 21:48:01 pixel Exp $ */ +/* $Id: font.h,v 1.11 2005-12-01 13:48:12 pixel Exp $ */ #ifndef __FONT_H__ #define __FONT_H__ @@ -40,8 +40,8 @@ namespace mogltk { void putcursor(int, int); void putentry(Uint16, ColorP = WHITE); void putentryontex(texture *, Uint16, ColorP = WHITE); - void drawchar(char, ColorP = WHITE); - void drawcharontex(texture *, char, ColorP = WHITE); + void drawchar(unsigned char, ColorP = WHITE); + void drawcharontex(texture *, unsigned char, ColorP = WHITE); void newline(void); int printf(const ugly_string &, ...); int printf(const char *, ...); @@ -58,7 +58,7 @@ namespace mogltk { void setcolor(ColorP); void setshadow(int); void setwspace(int); - int findchar(char) const; + int findchar(unsigned char) const; int singletextsize(const String &) const; protected: diff --git a/include/shape.h b/include/shape.h index 72feba2..b5c3d91 100644 --- a/include/shape.h +++ b/include/shape.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: shape.h,v 1.16 2004-11-27 21:48:01 pixel Exp $ */ +/* $Id: shape.h,v 1.17 2005-12-01 13:48:12 pixel Exp $ */ #ifndef __SHAPE_H__ #define __SHAPE_H__ @@ -27,6 +27,7 @@ #include #include #include +#include namespace mogltk { typedef enum { @@ -143,13 +144,13 @@ namespace mogltk { ColorP shade2 = DOS_GRAY); virtual void text(int x, int y, const String &, ColorP textcolor = DOS_HIGH_WHITE, - align_t align = LEFT); + align_t align = LEFT, mogltk::font * = mogltk::SystemFont); virtual void text3d(int x, int y, const String &, ColorP textcolor = DOS_BLACK, ColorP shade1 = DOS_HIGH_WHITE, ColorP shade2 = DOS_GRAY, align_t align = LEFT, - bool bevel = false); + bool bevel = false, mogltk::font * = mogltk::SystemFont); virtual void button(int x1, int y1, int x2, int y2, const String &, bool bevel = false, ColorP front = DOS_WHITE, diff --git a/include/widgets.h b/include/widgets.h index bf1cab1..ae29cee 100644 --- a/include/widgets.h +++ b/include/widgets.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: widgets.h,v 1.11 2005-06-20 22:43:35 pixel Exp $ */ +/* $Id: widgets.h,v 1.12 2005-12-01 13:48:12 pixel Exp $ */ #ifndef __WIDGETS_H__ #define __WIDGETS_H__ @@ -25,6 +25,7 @@ #include #include #include +#include namespace mogltk { enum event_t { @@ -77,6 +78,9 @@ namespace mogltk { virtual void SetEnabled(bool); void MoveOnTop(); static void check_timed_events(); + void center(bool cx = true, bool cy = true); + void set_viewport(); + void delete_me(); protected: widget(widget * father, int x, int y, int sx, int sy, int type, String name, shape *); virtual void draw(); @@ -179,11 +183,12 @@ namespace mogltk { */ class Label : public widget { public: - Label(shape *, widget * father, int x, int y, int w, int h, const String & caption, const ColorP & color = BLACK); + Label(shape *, widget * father, int x, int y, int w, int h, const String & caption, const ColorP & color = BLACK, mogltk::font * = mogltk::SystemFont); protected: virtual void draw(); private: ColorP color; + mogltk::font * label_font; }; /*! @@ -212,10 +217,12 @@ namespace mogltk { The MsgBox is a SmartBoxClose which holds a text, as well as a 'Ok' button. */ - class MsgBox : public SmartBoxClose { + class MsgBox : public SmartBox { public: - MsgBox(shape *, widget * father, const String & caption, const String & text); + MsgBox(shape *, widget * father, const String & caption, const String & text, mogltk::font * = mogltk::SystemFont); virtual ~MsgBox(); + private: + mogltk::font * msgbox_font; }; /*! @@ -318,7 +325,45 @@ namespace mogltk { std::vector nodes; int cur_x; int selected; + ContextMenu * subselected; + }; + + class InputText : public widget { + public: + InputText(shape *, widget * father, int x, int y); + virtual ~InputText(); + virtual void draw(); + const String & GetText() const; + void SetText(const String &); + private: + class inputtext_keyevent : public mogltk::engine::keyevent { + public: + inputtext_keyevent(InputText *); + virtual void down(SDL_keysym k); + virtual void up(SDL_keysym k); + private: + InputText * father; + } * it_keyevent; + String text; }; + + /*! + The InputDialog is a kind of MsgBox which holds an InputText as well as a 'Ok' button. + It also contains an action so that the text can be readed from where the dialog + is created. + */ + class InputDialog : public SmartBox { + public: + InputDialog(action *, shape *, widget * father, const String & caption, const String & text, mogltk::font * = mogltk::SystemFont); + virtual ~InputDialog(); + String GetText(); + void do_action(); + private: + InputText * it; + action * a; + mogltk::font * InputDialog_font; + }; + }; }; diff --git a/lib/engine.cc b/lib/engine.cc index d9e479f..65f3c55 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: engine.cc,v 1.28 2004-11-27 21:48:03 pixel Exp $ */ +/* $Id: engine.cc,v 1.29 2005-12-01 13:48:12 pixel Exp $ */ #include #include @@ -280,11 +280,11 @@ void mogltk::engine::pollevents() throw (GeneralException) { } break; case SDL_MOUSEMOTION: + if ((event.motion.x == mx) && (event.motion.y == my)) + break; printm(M_INFO, "Mouse slept over the screen - (%i, %i)\n", event.motion.x, event.motion.y); if (locked) { - if (!((event.motion.x == mx) && (event.motion.y == my))) { - SDL_WarpMouse(mx, my); - } + SDL_WarpMouse(mx, my); } if (cursorvisible) hastoreturn = true; diff --git a/lib/font.cc b/lib/font.cc index 07ade78..8527d15 100644 --- a/lib/font.cc +++ b/lib/font.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: font.cc,v 1.16 2004-11-27 21:48:03 pixel Exp $ */ +/* $Id: font.cc,v 1.17 2005-12-01 13:48:12 pixel Exp $ */ #include #include @@ -306,7 +306,7 @@ void mogltk::font::putentryontex(texture * t, Uint16 entry, ColorP c) { cx += sizes[entry] + wspace; } -void mogltk::font::drawchar(char ch, ColorP c) { +void mogltk::font::drawchar(unsigned char ch, ColorP c) { Uint16 * p; int i; @@ -318,7 +318,7 @@ void mogltk::font::drawchar(char ch, ColorP c) { } } -void mogltk::font::drawcharontex(texture * t, char ch, ColorP c) { +void mogltk::font::drawcharontex(texture * t, unsigned char ch, ColorP c) { Uint16 * p; int i; @@ -330,7 +330,7 @@ void mogltk::font::drawcharontex(texture * t, char ch, ColorP c) { } } -int mogltk::font::findchar(char ch) const { +int mogltk::font::findchar(unsigned char ch) const { Uint16 * p; int i; @@ -424,7 +424,7 @@ mogltk::rect mogltk::font::size(const ugly_string & m, va_list ap) { w = 0; } } else { - w += sizes[findchar(*p)] + wspace; + w += sizes[findchar(*((unsigned char*)p))] + wspace; } } diff --git a/lib/shape.cc b/lib/shape.cc index a7ed137..7a3312a 100644 --- a/lib/shape.cc +++ b/lib/shape.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: shape.cc,v 1.19 2004-11-27 21:48:03 pixel Exp $ */ +/* $Id: shape.cc,v 1.20 2005-12-01 13:48:12 pixel Exp $ */ #include #include @@ -831,30 +831,30 @@ void mogltk::shape::window(int x1, int y1, int x2, int y2, const String & title, LEAVE; } -void mogltk::shape::text(int x, int y, const String & text, ColorP textcolor, align_t align) { - int tsize = SystemFont->singletextsize(text); +void mogltk::shape::text(int x, int y, const String & text, ColorP textcolor, align_t align, mogltk::font * _font) { + int tsize = _font->singletextsize(text); switch (align) { case LEFT: - SystemFont->putcursor(x, y); + _font->putcursor(x, y); break; case CENTER: - SystemFont->putcursor(x - (tsize / 2), y); + _font->putcursor(x - (tsize / 2), y); break; case RIGHT: - SystemFont->putcursor(x - tsize, y); + _font->putcursor(x - tsize, y); break; } - SystemFont->setcolor(textcolor); - SystemFont->setshadow(0); - SystemFont->printf(text); + _font->setcolor(textcolor); + _font->setshadow(0); + _font->printf("%s", text.to_charp()); } void mogltk::shape::text3d(int x, int y, const String & atext, ColorP textcolor, ColorP shade1, ColorP shade2, - align_t align, bool bevel) { + align_t align, bool bevel, mogltk::font * _font) { ENTER; - SystemFont->setwspace(1); + _font->setwspace(1); if (!bevel) { text(x - 1, y - 1, atext, shade2, align); text(x + 1, y + 1, atext, shade1, align); @@ -863,7 +863,7 @@ void mogltk::shape::text3d(int x, int y, const String & atext, ColorP textcolor, text(x + 1, y + 1, atext, shade2, align); } text(x, y, atext, textcolor, align); - SystemFont->setwspace(0); + _font->setwspace(0); LEAVE; } diff --git a/lib/widgets.cc b/lib/widgets.cc index 94cbd73..246a182 100644 --- a/lib/widgets.cc +++ b/lib/widgets.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: widgets.cc,v 1.14 2005-06-20 22:43:35 pixel Exp $ */ +/* $Id: widgets.cc,v 1.15 2005-12-01 13:48:12 pixel Exp $ */ #include #include @@ -39,6 +39,7 @@ mogltk::widget * dragged_widget = 0; std::vector out_move, out_click; std::vector mogltk::widget::timed_events; +std::vector to_delete; class widget_mouse_event : public mogltk::engine::mouseevent { @@ -107,7 +108,7 @@ void widget_mouse_event::move(SDL_MouseMotionEvent m) { } } - out_move_stack.empty(); + out_move_stack.clear(); } void widget_mouse_event::action(SDL_MouseButtonEvent b) { @@ -137,7 +138,7 @@ void widget_mouse_event::action(SDL_MouseButtonEvent b) { } } - out_click_stack.empty(); + out_click_stack.clear(); } else { mouse_down = false; @@ -168,7 +169,7 @@ void widget_mouse_event::action(SDL_MouseButtonEvent b) { } } - out_click_stack.empty(); + out_click_stack.clear(); if ((SDL_GetTicks() - old_click) < 500) { printm(M_INFO, "mouse_dbl_click\n"); @@ -382,14 +383,21 @@ void mogltk::widget::fulldraw() { } void mogltk::widget::idraw() { - int x1, y1, x2, y2; - if (next) next->idraw(); if (!visible) return; + set_viewport(); + draw(); + + if (child) + child->idraw(); +} + +void mogltk::widget::set_viewport() { + int x1, y1, x2, y2; x1 = MAX(GetAX(), father->GetAX()); y1 = MAX(GetAY(), father->GetAY()); @@ -397,11 +405,6 @@ void mogltk::widget::idraw() { y2 = MIN(GetAY2(), father->GetAY2()); engine::base_o->changeviewport(x1, y1, x2 - x1, y2 - y1); - - draw(); - - if (child) - child->idraw(); } bool mogltk::widget::inside(int xe, int ye) { @@ -587,6 +590,28 @@ void mogltk::widget::set_absolute_timed_event(Uint32 t) { timed_events.push_back(timed_event(this, t)); } +void mogltk::widget::center(bool cx, bool cy) { + rect tr, fr; + tr.x = GetX(); + tr.y = GetY(); + tr.w = GetW(); + tr.h = GetH(); + fr.w = Father()->GetW(); + fr.h = Father()->GetH(); + + if (cx) + tr.x = fr.w / 2 - tr.w / 2; + + if (cy) + tr.y = fr.h / 2 - tr.h / 2; + + move(tr.x, tr.y); +} + +void mogltk::widget::delete_me() { + to_delete.push_back(this); +} + /* * Predefined widgets. @@ -743,20 +768,18 @@ class MessageBoxAction : public mogltk::widgets::action { mogltk::widget * parent; }; -mogltk::widgets::MsgBox::MsgBox(shape * sh, mogltk::widget * father, const String & caption, const String & text) : SmartBoxClose(sh, father, 0, 0, 0, 0, caption) { - rect size = SystemFont->size(text); +mogltk::widgets::MsgBox::MsgBox(shape * sh, mogltk::widget * father, const String & caption, const String & text, mogltk::font * _font) : SmartBox(sh, father, 0, 0, 0, 0, caption), msgbox_font(_font) { + rect size = msgbox_font->size(text); rect lsize = size; size.w += 12; size.h += 60; - size.x = (father->GetW() - size.w) / 2; - size.y = (father->GetH() - size.h) / 2; resize(size.w, size.h); - move(size.x, size.y); + center(); new Button(new MessageBoxAction(this), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok"); - new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK); + new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK, msgbox_font); set_exclusive(father); } @@ -770,12 +793,12 @@ mogltk::widgets::MsgBox::~MsgBox() { * A simple Label * *****************/ -mogltk::widgets::Label::Label(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption, const ColorP & _color) : widget(father, x, y, w, h, 0, "Label", sh), color(_color) { +mogltk::widgets::Label::Label(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption, const ColorP & _color, mogltk::font * _font) : widget(father, x, y, w, h, 0, "Label", sh), color(_color), label_font(_font) { caption = _caption; } void mogltk::widgets::Label::draw() { - Shaper()->text(GetAX(), GetAY(), caption, color); + Shaper()->text(GetAX(), GetAY(), caption, color, LEFT, label_font); } @@ -1089,7 +1112,7 @@ void mogltk::widgets::Menu::node::SetEnabled(bool _enabled) { enabled = _enabled; } -mogltk::widgets::Menu::Menu(shape * sh, mogltk::widget * father) : widget(father, 0, 0, 0, 0, 0, "Menu", sh), cur_x(0), selected(-1) { +mogltk::widgets::Menu::Menu(shape * sh, mogltk::widget * father) : widget(father, 0, 0, 0, 0, 0, "Menu", sh), cur_x(0), selected(-1), subselected(0) { rect r = father->GetDrawRect(); move(r.x, r.y); resize(r.w, 16); @@ -1190,6 +1213,116 @@ bool mogltk::widgets::Menu::process_event(int mx, int my, mogltk::event_t event) return false; } +/********************** +* The InputText entry * +**********************/ + +mogltk::widgets::InputText::InputText(shape * sh, mogltk::widget * father, int _x, int _y) : widget(father, _x, _y, 80, 20, 0, "InputText", sh), it_keyevent(0) { + it_keyevent = new mogltk::widgets::InputText::inputtext_keyevent(this); +} + +mogltk::widgets::InputText::~InputText() { + delete it_keyevent; +} + +mogltk::widgets::InputText::inputtext_keyevent::inputtext_keyevent(mogltk::widgets::InputText * _father) : father(_father) { +} + +void mogltk::widgets::InputText::inputtext_keyevent::up(SDL_keysym k) { +} + +void mogltk::widgets::InputText::inputtext_keyevent::down(SDL_keysym k) { + if (father->GetVisible()) { + switch (k.sym) { + case SDLK_BACKSPACE: + if (father->GetText().strlen() == 1) { + father->SetText(""); + } else if (father->GetText().strlen()) { + father->SetText(father->GetText().extract(0, father->GetText().strlen() - 2)); + } + break; + case SDLK_RETURN: + break; + default: + if (k.unicode) { + if (((k.unicode >= 'a') && (k.unicode <= 'z')) || + ((k.unicode >= 'A') && (k.unicode <= 'Z')) || + ((k.unicode >= '0') && (k.unicode <= '9'))) + father->SetText(father->GetText() + ((char) k.unicode)); + } + } + } else { + if (old_handler) + old_handler->down(k); + } +} + +const String & mogltk::widgets::InputText::GetText() const{ + return text; +} + +void mogltk::widgets::InputText::SetText(const String & _text) { + text = _text; +} + +void mogltk::widgets::InputText::draw() { + int rx; + rect r = SystemFont->size(text); + + rx = r.w + GetAX() + 2; + + Shaper()->box3d(GetAX(), GetAY(), GetAX2(), GetAY2(), DOS_WHITE, DOS_HIGH_WHITE, DOS_GRAY, 2, true); + Shaper()->text(MIN(rx, GetAX2() - 2), GetAY() + 2, text, BLACK, RIGHT); +} + + +/************************ +* The InputDialog child * +************************/ + +class InputDialogAction : public mogltk::widgets::action { + public: + InputDialogAction(mogltk::widgets::InputDialog * _parent) : parent(_parent) { } + virtual void do_action(mogltk::widget *) { + parent->do_action(); + delete parent; + delete this; + } + private: + mogltk::widgets::InputDialog * parent; +}; + +mogltk::widgets::InputDialog::InputDialog(mogltk::widgets::action * _a, shape * sh, mogltk::widget * father, const String & caption, const String & text, mogltk::font * _font) : SmartBox(sh, father, 0, 0, 0, 0, caption), a(_a), InputDialog_font(_font) { + rect size = InputDialog_font->size(text); + rect lsize = size; + + size.w += 12; + size.h += 90; + + resize(size.w, size.h); + center(); + + new Button(new InputDialogAction(this), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok"); + it = new InputText(sh, this, 10, size.h - 60); + it->resize(size.w - 20, 20); + new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK, InputDialog_font); + + set_exclusive(father); +} + +mogltk::widgets::InputDialog::~InputDialog() { + unset_exclusive(Father()); +} + +String mogltk::widgets::InputDialog::GetText() { + return it->GetText(); +} + +void mogltk::widgets::InputDialog::do_action() { + if (a) + a->do_action(this); +} + /* * The mainloop widget thing. @@ -1215,6 +1348,11 @@ void mogltk::widget::mainloop() { engine::base_o->Flip(); widget::check_timed_events(); + + for (std::vector::iterator i = to_delete.begin(); i != to_delete.end(); i++) { + delete *i; + } + to_delete.clear(); } delete mouse; -- cgit v1.2.3