aboutsummaryrefslogtreecommitdiffstats
path: root/demos/3rdparty/notepad-2/notepadCore.c
blob: ae1b4645958a01ba9c97dbd8e17642e666b5eabc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * File: notepadCore.c
 *
 * This file is a part of the Notepad demo application for ChibiOS/GFX
 * Copyright 2013, Kumar Abhishek [abhishek.kakkar@edaboard.com].
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 * 	   notice, this list of conditions and the following disclaimer in the
 * 	   documentation and/or other materials provided with the distribution.
 * 	 * The name of 'Kumar Abhishek' may not be used to endorse or promote
 *     products derived from this software without specific prior
 *     written permission.
 *
 * DISCLAIMER OF WARRANTY:
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Modified by InMarket to allow it to compile on any GFX supported operating system.
 */

#include <stdlib.h>

#include "gfx.h"

#include "notepadCore.h"
#include "notepadUIDefines.h"

#define PEN_IN_DRAWING_AREA(ev)		((ev.x >= ncoreDrawingArea->x) && (ev.x <= (ncoreDrawingArea->x + ncoreDrawingArea->width)) && \
									 (ev.y >= ncoreDrawingArea->y) && (ev.y <= (ncoreDrawingArea->y + ncoreDrawingArea->height)))

/* This is the drawing core */
static GFX_THREAD_STACK(waDrawThread, NCORE_THD_STACK_SIZE);

static gU8 					nPenWidth = 1;
static gU8 					nMode = NCORE_MODE_DRAW;

static gThread			nThd;

static GHandle					ncoreDrawingArea = NULL;
static GHandle					nStatusConsole = NULL;

static volatile gBool			doExit;

static void draw_point(gCoord x, gCoord y) {
  gColor c = ncoreDrawingArea->color;

  if (nMode == NCORE_MODE_DRAW)
	c = ncoreDrawingArea->color;
  else if (nMode == NCORE_MODE_ERASE)
	c = ncoreDrawingArea->bgcolor;

  if (nPenWidth == 1)
	gdispDrawPixel(x, y, c);
  else
	gdispFillCircle(x, y, nPenWidth, c);
}

/* Bresenham's Line Drawing Algorithm
   Modified version to draw line of variable thickness */
static void draw_line(gCoord x0, gCoord y0, gCoord x1, gCoord y1) {
  gI16 dy, dx;
  gI16 addx, addy;
  gI16 P, diff, i;
  
  if (x1 >= x0) {
	  dx = x1 - x0;
	  addx = 1;
  } else {
	  dx = x0 - x1;
	  addx = -1;
  }
  if (y1 >= y0) {
	  dy = y1 - y0;
	  addy = 1;
  } else {
	  dy = y0 - y1;
	  addy = -1;
  }

  if (dx >= dy) {
	  dy *= 2;
	  P = dy - dx;
	  diff = P - dx;

	  for(i=0; i<=dx; ++i) {
		  draw_point(x0, y0);
		  if (P < 0) {
			  P  += dy;
			  x0 += addx;
		  } else {
			  P  += diff;
			  x0 += addx;
			  y0 += addy;
		  }
	  }
  } else {
	  dx *= 2;
	  P = dx - dy;
	  diff = P - dy;

	  for(i=0; i<=dy; ++i) {
		  draw_point(x0, y0);
		  if (P < 0) {
			  P  += dx;
			  y0 += addy;
		  } else {
			  P  += diff;
			  x0 += addx;
			  y0 += addy;
		  }
	  }
  }
}

/* Core thread */
static GFX_THREAD_FUNCTION(ncoreDrawThread, msg) {

  GEventMouse ev, evPrev;
  gCoord dx, dy;

  int state = 0, dist;

  (void)msg;

  ginputGetMouseStatus(0, &evPrev);

  while (!doExit) {

	ginputGetMouseStatus(0, &ev);
	switch(state) {
	  case 0:	if (ev.meta == GMETA_MOUSE_DOWN) {
				  state = 1;
				  if (nMode == NCORE_MODE_FILL && PEN_IN_DRAWING_AREA(ev)) {
					// Set bgcolor to current color, clear the display.
					ncoreDrawingArea->bgcolor = ncoreDrawingArea->color;
					gwinClear(ncoreDrawingArea);
				  }
				}
				else
				  gfxYield();
				break;


	  case 1:   if (ev.meta == GMETA_MOUSE_UP) {
				  state = 0;
				  //gwinPrintf(nStatusConsole, "\nPen Up: (%d, %d)", ev.x, ev.y);
				  break;
				}

				dx = abs(ev.x - evPrev.x);
				dy = abs(ev.y - evPrev.y);

				dist = dx * dx + dy * dy;

				if (dist > 0)
				{
				   gdispSetClip(ncoreDrawingArea->x,
				                ncoreDrawingArea->y,
				                ncoreDrawingArea->width,
				                ncoreDrawingArea->height);

				   if (PEN_IN_DRAWING_AREA(ev)){
					// Do Interpolation
					if (dist <= 2) {
					  draw_point(ev.x, ev.y);
					}
					else if (dist <= 5) {
					  // Line drawing does not give good results for this case.
					  // So draw two pixels directly
					  draw_point(ev.x, ev.y);
					  draw_point((ev.x + evPrev.x) / 2,	(ev.y + evPrev.y) / 2);
					}
					else if (dx * dx <= MAX_DX && dy * dy <= MAX_DY) {
					  draw_line(ev.x, ev.y,	evPrev.x, evPrev.y);
					}
				  }

				  //gwinPrintf(nStatusConsole, "\nPen Down: (%d, %d)", ev.x, ev.y);
				}
				break;
	}
	evPrev = ev;
  }

  return 0;
}

/* Spawn the core thread */
void ncoreSpawnDrawThread(GHandle drawingArea, GHandle statusConsole) {

  ncoreDrawingArea = drawingArea;
  nStatusConsole = statusConsole;
  doExit = gFalse;

  nThd = gfxThreadCreate(waDrawThread,
                           sizeof(waDrawThread),
                           NCORE_THD_PRIO,
                           ncoreDrawThread,
                           NULL);

}

/* Terminate the core thread, wait for control release */
void ncoreTerminateDrawThread(void) {
  doExit = gTrue;
  gfxThreadWait(nThd);
  nThd = 0;
}

/* Get and set the pen width
 * Brush is cicular, width is pixel radius */
void ncoreSetPenWidth(gU8 penWidth) { nPenWidth = penWidth; }
gU8 ncoreGetPenWidth(void) 			{ return nPenWidth; }

/* Get and set the drawing color */
void ncoreSetPenColor(gColor penColor) { gwinSetColor(ncoreDrawingArea, penColor); }
gColor ncoreGetPenColor(void) 			{ return ncoreDrawingArea->color; }

/* Set mode */
void ncoreSetMode(gU8 mode)			{ nMode = mode; }
gU8 ncoreGetMode(void)				{ return nMode; }