summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Melcher <fltk@matthiasm.com>2018-03-10 00:46:12 +0000
committerMatthias Melcher <fltk@matthiasm.com>2018-03-10 00:46:12 +0000
commitc0cbf0fbde0ac33d3743a6d50bf0fa9f6664a008 (patch)
tree8fb9b3e1db8bfef8052deb549af986023fc86915 /src
parentdc2fb581b76ea84ebd70a7ced1291dd26b40fc71 (diff)
Android: crude graphics clipping
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12726 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/drivers/Android/Fl_Android_Graphics_Driver.H72
-rw-r--r--src/drivers/Android/Fl_Android_Graphics_Driver.cxx23
-rw-r--r--src/drivers/Android/Fl_Android_Graphics_Driver_region.cxx263
-rw-r--r--src/drivers/Android/README.txt12
5 files changed, 306 insertions, 65 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f8e7159b4..072fe0604 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -303,6 +303,7 @@ elseif (ANDROID)
drivers/Android/Fl_Android_Window_Driver.cxx
drivers/Android/Fl_Android_Image_Surface_Driver.cxx
drivers/Android/Fl_Android_Graphics_Driver.cxx
+ drivers/Android/Fl_Android_Graphics_Driver_region.cxx
)
set (DRIVER_HEADER_FILES
drivers/Android/Fl_Android_Application.H
diff --git a/src/drivers/Android/Fl_Android_Graphics_Driver.H b/src/drivers/Android/Fl_Android_Graphics_Driver.H
index 210190376..a55e39400 100644
--- a/src/drivers/Android/Fl_Android_Graphics_Driver.H
+++ b/src/drivers/Android/Fl_Android_Graphics_Driver.H
@@ -26,66 +26,24 @@
#define FL_ANDROID_GRAPHICS_DRIVER_H
#include <FL/Fl_Graphics_Driver.H>
+#include <FL/Fl_Rect.H>
#include <limits.h>
-#if 0
-// clipping
-// 1: rect
-// Draw only the part of any element that fits inside the rect
-// 2: region
-// A rect that contains the entire region
-// A sorted list of rows that contains fitting parts of the region
-// A sorted list of rects for each row
-
-class AFl_Rect
-{
- int pX1, pY1, pX2, pY2;
-public:
- const int int_max = INT_MAX;
- const int int_min = INT_MIN;
- AFl_Rect(int x1, y1, x2, y2) : pX1(x1), pY1(y1), pX2(x2), pY2(y2) { }
- AFl_Rect() : pX1(int_min), pY1(int_min), pX2(int_max), pY2(int_max) { }
- void x1(int x) { pX1 = x; }
- void y1(int y) { pY1 = y; }
- void x2(int x) { pX2 = x; }
- void y2(int y) { pY2 = y; }
- void w(int w) { pX2 = x1+w; }
- void h(int h) { pY2 = y1+w; }
- int x1() { return pX1; }
- int y1() { return pY1; }
- int x2() { return pX2; }
- int y2() { return pY2; }
- int w() { return pX2-pX1; }
- int h() { return pY2-pY1; }
-};
-class AFl_Row
+struct Fl_Clip_Rect
{
- int nRect, NRect;
- AFl_Rect *pRect;
- AFl_rect pBounds;
-public:
- AFl_Row(int y1, int y2) : nRect(0), NRect(0), pRect(0), pY1(y1), pY2(y2) { }
- ~AFl_Row() { /* TODO: delete all rects */ }
- void y1(int y) { pY1 = y; }
- void y2(int y) { pY2 = y; }
- AFl_Rect *first(int &i) { i=0; return (pRect && nRect>i) ? nRect[i] : 0L; }
- AFl_Rect *next(int &i) { i++; return (pRect && nRect>i) ? nRect[i] : 0L; }
- void add(AFl_Rect *r) { /* TODO: implement! */ }
- void subtract(AFl_Rect *r) { /* TODO: implement! */ }
- void clear() { /* TODO: implement! */ }
+ Fl_Clip_Rect() : pRect() {}
+ Fl_Clip_Rect(int x, int y, int w, int h) : pRect(x, y, w, h) {}
+ int x() { return pRect.x(); }
+ int y() { return pRect.y(); }
+ int w() { return pRect.w(); }
+ int h() { return pRect.h(); }
+ int intersect_with(Fl_Clip_Rect *r);
+ static int min(int a, int b) { return (a<b) ? a : b; }
+ static int max(int a, int b) { return (a>b) ? a : b; }
+ Fl_Rect pRect;
};
-class AFl_Region
-{
- int nRow, NRow;
- AFl_Row *pRow;
- AFl_rect pBounds;
-public:
- AFl_Region() : nRow(0), NRow(0), pRow(0) { }
- ~AFl_Region() { /* TODO: delete all rows */ }
-}
-#endif
/**
\brief The Windows-specific graphics driver class.
@@ -164,6 +122,7 @@ protected:
void focus_rect(int x, int y, int w, int h);
#endif
void rectf_unscaled(float x, float y, float w, float h);
+ void rectf_unclipped(float x, float y, float w, float h);
#if 0
virtual void line_unscaled(float x, float y, float x1, float y1);
virtual void line_unscaled(float x, float y, float x1, float y1, float x2, float y2);
@@ -175,6 +134,7 @@ protected:
virtual void loop_unscaled(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3);
virtual void polygon_unscaled(float x0, float y0, float x1, float y1, float x2, float y2);
virtual void polygon_unscaled(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3);
+#endif
// --- clipping
void push_clip(int x, int y, int w, int h);
int clip_box(int x, int y, int w, int h, int &X, int &Y, int &W, int &H);
@@ -182,6 +142,10 @@ protected:
void push_no_clip();
void pop_clip();
void restore_clip();
+ void clip_region(Fl_Region r);
+ Fl_Region clip_region();
+ static Fl_Clip_Rect pClipRect;
+#if 0
virtual Fl_Region scale_clip(float f);
// --- implementation is in src/fl_vertex.cxx which includes src/cfg_gfx/xxx_rect.cxx
void begin_complex_polygon();
diff --git a/src/drivers/Android/Fl_Android_Graphics_Driver.cxx b/src/drivers/Android/Fl_Android_Graphics_Driver.cxx
index c9780c8d8..1df52462a 100644
--- a/src/drivers/Android/Fl_Android_Graphics_Driver.cxx
+++ b/src/drivers/Android/Fl_Android_Graphics_Driver.cxx
@@ -1,7 +1,7 @@
//
// "$Id$"
//
-// Rectangle drawing routines for the Fast Light Tool Kit (FLTK).
+// Graphics routines for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2018 by Bill Spitzak and others.
//
@@ -20,18 +20,11 @@
#include "../../config_lib.h"
#include "Fl_Android_Application.H"
#include "Fl_Android_Graphics_Driver.H"
+#include "Fl_Android_Screen_Driver.H"
#include <FL/Fl.H>
#include <FL/platform.H>
#include <errno.h>
-#include "Fl_Android_Screen_Driver.H"
-#include <android/log.h>
-
-#define LOG_TAG "FLTK"
-#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
-#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
-#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
-
/*
* By linking this module, the following static method will instantiate the
@@ -60,8 +53,16 @@ static uint16_t make565(Fl_Color crgba)
((crgba >>11) & 0x001f) );
}
-
void Fl_Android_Graphics_Driver::rectf_unscaled(float x, float y, float w, float h) {
+ Fl_Android_Application::log_w("rectf %g %g %g %g", x, y, w, h);
+ Fl_Clip_Rect r(x, y, w, h);
+ if (r.intersect_with(&pClipRect)) {
+ rectf_unclipped(r.x(), r.y(), r.w(), r.h());
+ }
+}
+
+void Fl_Android_Graphics_Driver::rectf_unclipped(float x, float y, float w, float h) {
+ Fl_Android_Application::log_w("rectf unclipped %g %g %g %g", x, y, w, h);
if (w<=0 || h<=0) return;
// TODO: clip the rectangle to the window outline
@@ -147,7 +148,7 @@ if (once==0) {
once = 1;
FILE *f = fopen("/system/fonts/DroidSans.ttf", "rb");
if (f==NULL) {
- LOGE("ERROR reading font %d!", errno);
+ Fl_Android_Application::log_e("ERROR reading font %d!", errno);
return 0;
}
fread(ttf_buffer, 1, 1<<25, f);
diff --git a/src/drivers/Android/Fl_Android_Graphics_Driver_region.cxx b/src/drivers/Android/Fl_Android_Graphics_Driver_region.cxx
new file mode 100644
index 000000000..107a6e708
--- /dev/null
+++ b/src/drivers/Android/Fl_Android_Graphics_Driver_region.cxx
@@ -0,0 +1,263 @@
+//
+// "$Id$"
+//
+// Clipping region routines for the Fast Light Tool Kit (FLTK).
+//
+// Copyright 1998-2018 by Bill Spitzak and others.
+//
+// This library is free software. Distribution and use rights are outlined in
+// the file "COPYING" which should have been included with this file. If this
+// file is missing or damaged, see the license at:
+//
+// http://www.fltk.org/COPYING.php
+//
+// Please report all bugs and problems on the following page:
+//
+// http://www.fltk.org/str.php
+//
+
+
+#include "../../config_lib.h"
+#include "Fl_Android_Graphics_Driver.H"
+#include "Fl_Android_Application.H"
+#include <FL/platform.H>
+
+
+Fl_Clip_Rect Fl_Android_Graphics_Driver::pClipRect;
+
+
+// return 0 for empty, 1 for same, 2 if intersecting
+int Fl_Clip_Rect::intersect_with(Fl_Clip_Rect *a)
+{
+ if (pRect.is_empty()) {
+ return 0;
+ }
+ if (a->pRect.is_empty()) {
+ pRect.clear();
+ return 0;
+ }
+ int x = max(pRect.x(), a->pRect.x());
+ int y = max(pRect.y(), a->pRect.y());
+ int r = min(pRect.r(), a->pRect.r());
+ int b = min(pRect.b(), a->pRect.b());
+ int w = r-x;
+ int h = b-y;
+ if (pRect.equals(x, y, w, h)) {
+ return 1;
+ }
+ pRect.set(x, y, w, h);
+ if ( (pRect.w()<=0) || (pRect.h()<=0) ) {
+ pRect.clear();
+ return 0;
+ }
+ return 2;
+}
+
+
+
+void Fl_Android_Graphics_Driver::clip_region(Fl_Region r)
+{
+ Fl_Region oldr = rstack[rstackptr];
+ if (oldr)
+ ::free(oldr);
+ rstack[rstackptr] = r;
+ restore_clip();
+}
+
+Fl_Region Fl_Android_Graphics_Driver::clip_region()
+{
+ return rstack[rstackptr];
+}
+
+void Fl_Android_Graphics_Driver::restore_clip()
+{
+ fl_clip_state_number++;
+ Fl_Window *win = Fl_Window::current();
+ Fl_Clip_Rect a(0, 0, win->w(), win->h());
+
+ Fl_Region b = rstack[rstackptr];
+ if (b) {
+ // FIXME: scaling!
+ a.intersect_with(b);
+ }
+ pClipRect = a;
+}
+
+void Fl_Android_Graphics_Driver::push_clip(int x, int y, int w, int h)
+{
+ Fl_Region r;
+ if (w > 0 && h > 0) {
+ r = new Fl_Clip_Rect(x,y,w,h);
+ Fl_Region current = rstack[rstackptr];
+ if (current) {
+ r->intersect_with(current);
+ }
+ } else { // make empty clip region:
+ r = new Fl_Clip_Rect();
+ }
+ if (rstackptr < region_stack_max) rstack[++rstackptr] = r;
+ else Fl::warning("Fl_Android_Graphics_Driver::push_clip: clip stack overflow!\n");
+ restore_clip();
+}
+
+void Fl_Android_Graphics_Driver::push_no_clip()
+{
+ if (rstackptr < region_stack_max) rstack[++rstackptr] = 0;
+ else Fl::warning("Fl_Android_Graphics_Driver::push_no_clip: clip stack overflow!\n");
+ restore_clip();
+}
+
+void Fl_Android_Graphics_Driver::pop_clip()
+{
+ if (rstackptr > 0) {
+ Fl_Region oldr = rstack[rstackptr--];
+ if (oldr)
+ ::free(oldr);
+ } else Fl::warning("Fl_Android_Graphics_Driver::pop_clip: clip stack underflow!\n");
+ restore_clip();
+}
+
+/*
+ Intersects the rectangle with the current clip region and returns the
+ bounding box of the result.
+
+ Returns non-zero if the resulting rectangle is different to the original.
+ This can be used to limit the necessary drawing to a rectangle.
+ \p W and \p H are set to zero if the rectangle is completely outside the region.
+ \param[in] x,y,w,h position and size of rectangle
+ \param[out] X,Y,W,H position and size of resulting bounding box.
+ \returns Non-zero if the resulting rectangle is different to the original.
+ */
+int Fl_Android_Graphics_Driver::clip_box(int x, int y, int w, int h, int& X, int& Y, int& W, int& H)
+{
+ Fl_Region r = rstack[rstackptr];
+ if (!r) {
+ X = x; Y = y; W = w; H = h;
+ return 0;
+ }
+
+ Fl_Clip_Rect a(x, y, w, h);
+ int ret = a.intersect_with(r); // return 0 for empty, 1 for same, 2 if intersecting
+ X = a.x();
+ Y = a.y();
+ W = a.w();
+ H = a.h();
+
+ return (ret!=1);
+}
+
+/*
+ Does the rectangle intersect the current clip region?
+ \param[in] x,y,w,h position and size of rectangle
+ \returns non-zero if any of the rectangle intersects the current clip
+ region. If this returns 0 you don't have to draw the object.
+
+ \note
+ Under X this returns 2 if the rectangle is partially clipped,
+ and 1 if it is entirely inside the clip region.
+ */
+int Fl_Android_Graphics_Driver::not_clipped(int x, int y, int w, int h) {
+ if (x+w <= 0 || y+h <= 0) return 0;
+ Fl_Region r = rstack[rstackptr];
+ if (!r) return 1;
+
+ Fl_Clip_Rect a(x, y, w, h); // return 0 for empty, 1 for same, 2 if intersecting
+ return a.intersect_with(r);
+}
+
+#if 0
+
+// --- clipping
+
+int Fl_GDI_Graphics_Driver::clip_box(int x, int y, int w, int h, int& X, int& Y, int& W, int& H){
+ X = x; Y = y; W = w; H = h;
+ Fl_Region r = rstack[rstackptr];
+ if (!r) return 0;
+ // The win32 API makes no distinction between partial and complete
+ // intersection, so we have to check for partial intersection ourselves.
+ // However, given that the regions may be composite, we have to do
+ // some voodoo stuff...
+ Fl_Region rr = XRectangleRegion(x,y,w,h);
+ Fl_Region temp = CreateRectRgn(0,0,0,0);
+ int ret;
+ if (CombineRgn(temp, rr, r, RGN_AND) == NULLREGION) { // disjoint
+ W = H = 0;
+ ret = 2;
+ } else if (EqualRgn(temp, rr)) { // complete
+ ret = 0;
+ } else { // partial intersection
+ RECT rect;
+ GetRgnBox(temp, &rect);
+ if (Fl_Surface_Device::surface() != Fl_Display_Device::display_device()) { // if print context, convert coords from device to logical
+ POINT pt[2] = { {rect.left, rect.top}, {rect.right, rect.bottom} };
+ DPtoLP(gc_, pt, 2);
+ X = pt[0].x; Y = pt[0].y; W = pt[1].x - X; H = pt[1].y - Y;
+ }
+ else {
+ X = rect.left; Y = rect.top; W = rect.right - X; H = rect.bottom - Y;
+ }
+ ret = 1;
+ }
+ DeleteObject(temp);
+ DeleteObject(rr);
+ return ret;
+}
+
+int Fl_GDI_Graphics_Driver::not_clipped(int x, int y, int w, int h) {
+ if (x+w <= 0 || y+h <= 0) return 0;
+ Fl_Region r = rstack[rstackptr];
+ if (!r) return 1;
+ RECT rect;
+ if (Fl_Surface_Device::surface() != Fl_Display_Device::display_device()) { // in case of print context, convert coords from logical to device
+ POINT pt[2] = { {x, y}, {x + w, y + h} };
+ LPtoDP(gc_, pt, 2);
+ rect.left = pt[0].x; rect.top = pt[0].y; rect.right = pt[1].x; rect.bottom = pt[1].y;
+ } else {
+ rect.left = x; rect.top = y; rect.right = x+w; rect.bottom = y+h;
+ }
+ return RectInRegion(r,&rect);
+}
+
+
+#endif
+
+
+/*
+ - Pushes an empty clip region onto the stack so nothing will be clipped.
+virtual void push_no_clip() {}
+ - Intersects the current clip region with a rectangle and pushes this new region onto the stack.
+virtual void push_clip(int x, int y, int w, int h) {}
+ - Restores the previous clip region.
+virtual void pop_clip() {}
+ - Does the rectangle intersect the current clip region? 0=no, 1=all, 2=partially
+virtual int not_clipped(int x, int y, int w, int h) {return 1;}
+ - Intersects the rectangle with the current clip region and returns the bounding box of the result.
+ Returns 1 if rect changed, W and H are 0 if there is no rect
+virtual int clip_box(int x, int y, int w, int h, int &X, int &Y, int &W, int &H) {return 0;}
+ - Undoes any clobbering of clip done by your program
+virtual void restore_clip();
+
+virtual Fl_Region clip_region(); // has default implementation
+virtual void clip_region(Fl_Region r); // has default implementation
+
+ fl_push_clip(x,y,w,h) -> driver
+ fl_pop_clip() -> driver
+ fl_push_no_clip() -> driver
+ fl_not_clipped(int x, int y, int w, int h) -> driver
+ fl_clip_box(int x , int y, int w, int h, int& X, int& Y, int& W, int& H) -> driver
+ fl_restore_clip() -> driver
+
+ fl_clip_region(Fl_Region r) -> driver
+ fl_clip_region() -> driver
+
+virtual Fl_Region scale_clip(float f) { return 0; }
+void unscale_clip(Fl_Region r);
+
+
+ */
+
+
+
+//
+// End of "$Id$".
+//
diff --git a/src/drivers/Android/README.txt b/src/drivers/Android/README.txt
new file mode 100644
index 000000000..650c07f68
--- /dev/null
+++ b/src/drivers/Android/README.txt
@@ -0,0 +1,12 @@
+
+
+WonkoBook:Android matt$ svn ps svn:keywords "author date id revision" Fl_Android_Application.*
+property 'svn:keywords' set on 'Fl_Android_Application.cpp'
+property 'svn:keywords' set on 'Fl_Android_Application.h'
+WonkoBook:Android matt$ svn pg svn:eol-style Fl_Font.H
+native
+WonkoBook:Android matt$ svn ps svn:eol-style "native" Fl_Android_Application.*
+property 'svn:eol-style' set on 'Fl_Android_Application.cpp'
+property 'svn:eol-style' set on 'Fl_Android_Application.h'
+
+