primitiveGui.h
1  /*
2  Primitive Gui - simple gui form SFML 2.0
3  http://primitivegui.sourceforge.net/
4  Copyright (C) 2013 sajmon (https://sourceforge.net/users/sajmonek)
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef primitiveGui_h
21 #define primitiveGui_h
22 
23 #include <SFML\Graphics.hpp>
24 #include <sstream>
25 #include <iostream>
26 
27 class pGui;
28 class pText;
29 class pWidget;
30 class pButton;
31 class pTextEdit;
32 class pCheckBox;
33 class pSlider;
34 class pProgressBar;
35 class pWindow;
36 
37 
44 class pText : public sf::Text
45 {
46 public:
54  pText( float marginLeft, float marginRight, bool textCentered, int cutDirection );
55 
61  void setParentGeom( sf::Vector2f position, sf::Vector2f size);
62 
67  void addSymbol( sf::String& symbol );
68 
73  void setNewSymbol( sf::String& symbol );
74 
78  void removeLastSymbol( );
79 
83  void updateText( );
84 
85  sf::String fullString;
86  sf::String visibleString;
87 private:
88  sf::Vector2f parentPos;
89  sf::Vector2f parentSize;
90  float marginLeft;
91  float marginRight;
92  bool textCentered;
93  int cutDirection;//{0:zostaje końcówka tekstu, 1:zostaje początek tekstu}
94 };
95 
102 class pWidget : public sf::Drawable
103 {
104  friend class pGui;
105  friend class pButton;
106  friend class pTextEdit;
107  friend class pCheckBox;
108  friend class pSlider;
109  friend class pProgressBar;
110  friend class pWindow;
111 
112 public:
113  pWidget();
114 
122  pWidget( float x, float y, float width, float height );
123 
132  void setGeometry( float x, float y, float width, float height );
133 
138  void setVisiblity( bool visiblity );
139 
143  virtual void updateWidget() = 0;
144 
145  int widgetId;
147  bool visiblity;
148 
153  Button=0,
159  };
161 
168  };
170 
171  sf::Vector2f widgetPosition;
172  sf::Vector2f widgetSize;
173 private:
174  virtual void checkMouseEvent(sf::Vector2i&, bool) = 0;
175  virtual void symbolEntered(sf::Uint32) = 0;
176 
177  bool isActive;
178  pWindow* widgetInWindow;
179  sf::Vector2f onWindowPos;
180  sf::RectangleShape widgetShape;
181 };
182 
200 class pButton : public pWidget
201 {
202  friend pWindow;
203 public:
204  pButton();
205 
213  pButton( float x, float y, float width, float height );
214 
222  void setColors( sf::Color borderColor, sf::Color fillColor, sf::Color hoverColor, sf::Color clickedColor );
223 
228  void setText( sf::String& text );
229 
234  sf::String getText( );
235 
236  virtual void updateWidget( );
237 
238  sf::Color borderColor;
239  sf::Color fillColor;
240  sf::Color hoverColor;
241  sf::Color clickedColor;
242  sf::Color colorToDraw;
244 private:
245  virtual void checkMouseEvent(sf::Vector2i&, bool);
246  virtual void symbolEntered(sf::Uint32){};
247  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
248  void updateShape( );
249 };
250 
262 class pTextEdit : public pWidget
263 {
264 public:
265  pTextEdit( );
273  pTextEdit( float x, float y, float width, float height );
274 
281  void setColors( sf::Color borderColor, sf::Color notActiveColor, sf::Color activeColor );
282 
287  void setText( sf::String& text );
288 
293  sf::String getText( );
294 
295  virtual void updateWidget();
296 
298  sf::Color borderColor;
299  sf::Color notActiveColor;
300  sf::Color activeColor;
301  sf::Color colorToDraw;
302 private:
303  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
304  virtual void checkMouseEvent(sf::Vector2i&, bool);
305  virtual void symbolEntered(sf::Uint32);
306  void updateShape();
307 };
308 
317 class pCheckBox : public pWidget
318 {
319 public:
320  pCheckBox( );
328  pCheckBox( float x, float y, float width, float height );
329 
336  void setColors( sf::Color borderColor, sf::Color uncheckedColor, sf::Color checkedColor );
337 
342  void setChecked( bool checked );
343 
348  void setText( sf::String& text );
349 
354  sf::String getText( );
355  virtual void updateWidget( );
356 
357  bool isChecked;
358  sf::Color borderColor;
359  sf::Color uncheckedColor;
360  sf::Color checkedColor;
362 private:
363  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
364  virtual void checkMouseEvent(sf::Vector2i&, bool);
365  virtual void symbolEntered(sf::Uint32){};
366  void updateShape();
367  sf::RectangleShape checkedShape;
368 };
369 
378 class pSlider : public pWidget
379 {
380 public:
381  pSlider( );
389  pSlider( float x, float y, float width, float height );
390 
397  void setColors( sf::Color borderColor, sf::Color fillColor, sf::Color sliderColor );
398 
403  void setValue( float value );
404 
409  float getValue();
410  virtual void updateWidget();
411 
412  float value;
413  sf::Color borderColor;
414  sf::Color fillColor;
415  sf::Color sliderColor;
416 private:
417  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
418  virtual void checkMouseEvent(sf::Vector2i&, bool);
419  virtual void symbolEntered(sf::Uint32){};
420  void updateShape();
421  sf::RectangleShape sliderShape;
422 };
423 
432 class pProgressBar : public pWidget
433 {
434 public:
435  pProgressBar();
443  pProgressBar( float x, float y, float width, float height );
444 
451  void setColors( sf::Color borderColor, sf::Color fillColor, sf::Color barColor );
452 
457  void setValue( float value );
458 
463  float getValue();
464  virtual void updateWidget();
465 
466  float value;
468  sf::Color borderColor;
469  sf::Color fillColor;
470  sf::Color barColor;
471 private:
472  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
473  virtual void checkMouseEvent(sf::Vector2i&, bool){};
474  virtual void symbolEntered(sf::Uint32){};
475  void updateShape();
476  sf::RectangleShape inBar;
477 };
478 
495 class pWindow : public pWidget
496 {
497  friend class pGui;
498  friend class pWidget;
499  friend class pButton;
500  friend class pTextEdit;
501  friend class pCheckBox;
502  friend class pSlider;
503  friend class pProgressBar;
504 public:
505  pWindow();
513  pWindow( float x, float y, float width, float height );
514 
519  void addWidget( pWidget* widget );
520 
525  void setWindowTitle( sf::String& text );
526 
531  sf::String getWindowTitle( );
532 
537  void setMovable( bool movable );
538 
543  bool checkMovable();
544 
549  void setToggle( bool toggle );
550 
555  bool checkToggled();
556 
564  void setColors( sf::Color borderColor, sf::Color titleBarColor, sf::Color windowColor, sf::Color notActiveMaskColor);
565  virtual void updateWidget();
566 
567  int zValue;
569  sf::Color borderColor;
570  sf::Color titleBarColor;
571  sf::Color windowColor;
572  sf::Color notActiveMaskColor;
573 private:
574  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
575  void updateWindow(sf::Event&, const sf::Window&);
576  virtual void checkMouseEvent(sf::Vector2i&, bool){};
577  virtual void symbolEntered(sf::Uint32){};
578  void updateShape();
579  sf::Vector2i mousePosition;
580  sf::RectangleShape titleBar;
581  sf::RectangleShape notActiveMask;
582  sf::Vector2i mOldPos;
583  sf::Vector2f widgetToggledSize;
584  bool toggle;
585  bool isMovable;
586  pGui* guiHandler;
587  pButton toggleButton;
588  int widgetsCount;
589  std::vector<pWidget*> widgetsList;
590 };
591 
609 class pGui : public sf::Drawable
610 {
611  friend class pWindow;
612 public:
616  pGui();
617 
622  void addWidget( pWidget* widget );
623 
628  void addWindow( pWindow* window );
629 
635  void update( sf::Event& event, const sf::Window& window );
636 
641  void changeOnTopWindow( pWindow* window );
643 private:
644  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
645 
646  int widgetsCount;
647  std::vector<pWidget*> widgetsList;
648 
649  int windowsCount;
650  std::vector<pWindow*> windowsList;
651 
652  pWindow* windowOnTop;
653  int windowClickId;
654  sf::Vector2i mousePosition;
655 };
656 
657 #endif