1// This file is part of SmallBASIC
2//
3// Copyright(C) 2001-2019 Chris Warren-Smith.
4//
5// This program is distributed under the terms of the GPL v2.0 or later
6// Download the GNU Public License (GPL) from www.gnu.org
7//
8
9#ifndef ANSIWIDGET_H
10#define ANSIWIDGET_H
11
12#include <config.h>
13
14#include "lib/maapi.h"
15#include "ui/strlib.h"
16#include "ui/screen.h"
17#include "ui/inputs.h"
18#include "ui/image.h"
19
20#define MAX_PENDING 250
21#define MAX_PENDING_GRAPHICS 25
22
23#define USER_SCREEN1 0
24#define USER_SCREEN2 1
25#define TEXT_SCREEN 2
26#define SOURCE_SCREEN 3
27#define CONSOLE_SCREEN 4
28#define MENU_SCREEN 5
29#define MAX_SCREENS 6
30
31using namespace strlib;
32
33struct AnsiWidget {
34 explicit AnsiWidget(int width, int height);
35 ~AnsiWidget();
36
37 void addImage(ImageDisplay &image);
38 void addInput(FormInput *input) { _back->_inputs.add(input); }
39 void clearScreen();
40 bool construct();
41 void draw();
42 void drawArc(int xc, int yc, double r, double start, double end, double aspect);
43 void drawEllipse(int xc, int yc, int rx, int ry, int fill);
44 void drawImage(ImageDisplay &image);
45 void drawOverlay(bool vscroll) { _back->drawOverlay(vscroll); }
46 void drawLine(int x1, int y1, int x2, int y2);
47 void drawRect(int x1, int y1, int x2, int y2);
48 void drawRectFilled(int x1, int y1, int x2, int y2);
49 void flush(bool force, bool vscroll=false, int maxPending = MAX_PENDING);
50 void flushNow() { if (_front) _front->drawBase(false); }
51 int getBackgroundColor() { return _back->_bg; }
52 int getCharHeight() { return _back->_charHeight; }
53 int getCharWidth() { return _back->_charWidth; }
54 int getColor() { return _back->_fg; }
55 int getFontSize() { return _fontSize; }
56 FormInput *getNextField(FormInput *field) { return _back->getNextField(field); }
57 int getPixel(int x, int y) { return _back->getPixel(x, y); }
58 int getScreenId(bool back);
59 int getScreenWidth() { return _back->_width; }
60 void getScroll(int &x, int &y) { _back->getScroll(x, y); }
61 int getHeight() { return _height; }
62 int getWidth() { return _width; }
63 int getX() { return _back->_curX; }
64 int getY() { return _back->_curY; }
65 int getMenuIndex() const { return _back->getIndex(_activeButton); }
66 bool hasActiveButton() const { return _activeButton != NULL; }
67 bool hasHover() const { return _hoverInput != NULL; }
68 bool hasMenu() const { return _back == _screens[MENU_SCREEN]; }
69 void handleMenu(bool up);
70 void insetMenuScreen(int x, int y, int w, int h);
71 void insetTextScreen(int x, int y, int w, int h);
72 bool overLabel(int x, int y) { return _back->overLabel(x, y); };
73 bool overMenu(int x, int y) { return _back->overMenu(x, y); };
74 bool pointerTouchEvent(MAEvent &event);
75 bool pointerMoveEvent(MAEvent &event);
76 void pointerReleaseEvent(MAEvent &event);
77 void print(const char *str);
78 void redraw();
79 void removeHover();
80 void removeImage(int imageId) { _back->removeImage(imageId); }
81 bool removeInput(FormInput *input) { return _back->removeInput(input); }
82 void removeInputs();
83 void resetScroll() { _back->resetScroll(); }
84 void reset();
85 void resetFont() { _back->reset(_fontSize); _back->updateFont(); }
86 void resize(int width, int height);
87 bool scroll(bool up, bool page);
88 void selectBackScreen(int screenId);
89 void selectFrontScreen(int screenId);
90 int selectScreen(int screenId, bool forceFlush=true);
91 void setColor(long color);
92 void setDirty() { _back->setDirty(); }
93 void setAutoflush(bool autoflush) { _autoflush = autoflush; }
94 void setFont(int size, bool bold, bool italic);
95 void setFontSize(int fontSize);
96 void setPixel(int x, int y, int c);
97 void setScroll(int x, int y) { _back->setScroll(x, y); }
98 void setStatus(const char *label);
99 void setTextColor(long fg, long bg);
100 void setXY(int x, int y);
101 void setScrollSize(int scrollSize);
102 int textHeight(void) { return _back->_charHeight; }
103 void updateInputs(var_p_t form, bool setv) { _back->updateInputs(form, setv); }
104
105private:
106 Screen *createScreen(int screenId);
107 bool doEscape(const char *&p, int textHeight);
108 void doSwipe(int start, bool moveDown, int distance, int maxScroll);
109 void drawActiveButton();
110 bool drawHoverLink(MAEvent &event);
111 void handleEscape(const char *&p, int textHeight);
112 bool setActiveButton(MAEvent &event, Screen *screen);
113
114 Screen *_screens[MAX_SCREENS];
115 Screen *_back; // screen being painted/written
116 Screen *_front; // screen to display
117 Screen *_focus; // screen with the active button
118 FormInput *_activeButton;
119 FormInput *_hoverInput;
120 int _width; // device screen width
121 int _height; // device screen height
122 int _fontSize; // font height based on screen size
123 int _xTouch; // touch x value
124 int _yTouch; // touch y value
125 int _xMove; // touch move x value
126 int _yMove; // touch move y value
127 int _touchTime; // last move time
128 bool _swipeExit; // last touch-down was swipe exit
129 bool _autoflush; // flush internally
130};
131
132#endif // ANSIWIDGET_H
133