Subversion Repositories Kolibri OS

Rev

Rev 8728 | Rev 9193 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8728 leency 1
(*
2
    Copyright 2021 Anton Krotov
3
 
4
    This file is part of CEdit.
5
 
6
    CEdit 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
    CEdit 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 CEdit. If not, see .
18
*)
19
 
20
MODULE Graph;
21
 
9174 akron1 22
IMPORT SYSTEM, K := KOSAPI, Lines;
8728 leency 23
 
24
CONST
25
 
26
    modeCOPY = 0;
27
    modeNOT  = 1;
28
    modeXOR  = 2;
29
 
9174 akron1 30
    triUp* = FALSE;
31
    triDown* = TRUE;
32
    triLeft* = FALSE;
33
    triRight* = TRUE;
34
 
8728 leency 35
TYPE
36
 
37
    tFont* = POINTER TO RECORD
38
        handle*: INTEGER;
39
        height*: INTEGER;
40
        width*:  INTEGER;
41
        size:    INTEGER;
42
        name*:   ARRAY 256 OF WCHAR
43
    END;
44
 
45
    tCanvas* = POINTER TO RECORD
46
        bitmap: INTEGER;
47
        width*, height*: INTEGER;
48
        color, backColor, textColor: INTEGER;
49
        font*: tFont;
50
        mode: INTEGER
51
    END;
52
 
53
 
54
PROCEDURE DrawCanvas* (canvas: tCanvas; x, y: INTEGER);
55
BEGIN
56
    K.sysfunc7(65, canvas.bitmap, canvas.width*65536 + canvas.height, x*65536 + y, 32, 0, 0);
57
END DrawCanvas;
58
 
59
 
60
PROCEDURE SetColor* (canvas: tCanvas; color: INTEGER);
61
BEGIN
62
    canvas.color := color
63
END SetColor;
64
 
65
 
66
PROCEDURE SetTextColor* (canvas: tCanvas; color: INTEGER);
67
BEGIN
68
    canvas.textColor := color
69
END SetTextColor;
70
 
71
 
72
PROCEDURE SetBkColor* (canvas: tCanvas; color: INTEGER);
73
BEGIN
74
    canvas.backColor := color
75
END SetBkColor;
76
 
77
 
78
PROCEDURE CreateFont* (height: INTEGER; name: ARRAY OF WCHAR; attr: SET): tFont;
79
VAR
80
    font: tFont;
81
BEGIN
82
    NEW(font);
83
    font.size := MAX(MIN(height, 8), 1);
84
    font.width := font.size*8;
85
    font.height := font.size*16;
86
    DEC(font.size);
87
    font.name := name
88
    RETURN font
89
END CreateFont;
90
 
91
 
92
PROCEDURE SetFont* (canvas: tCanvas; font: tFont);
93
BEGIN
94
    canvas.font := font
95
END SetFont;
96
 
97
 
98
PROCEDURE HLine* (canvas: tCanvas; y, x1, x2: INTEGER);
99
VAR
100
    X1, X2, i: INTEGER;
101
    ptr: INTEGER;
102
    color: INTEGER;
103
BEGIN
104
    X1 := MAX(MIN(x1, x2), 0);
105
    X2 := MIN(MAX(x1, x2), canvas.width - 1);
106
    IF (0 <= y) & (y < canvas.height) THEN
107
        color := canvas.color;
108
        ptr := canvas.bitmap + y*canvas.width*4 + X1*4;
109
        FOR i := X1 TO X2 DO
110
            SYSTEM.PUT32(ptr, color);
111
            INC(ptr, 4)
112
        END
113
    END
114
END HLine;
115
 
116
 
117
PROCEDURE VLine* (canvas: tCanvas; x, y1, y2: INTEGER);
118
VAR
119
    Y1, Y2, i: INTEGER;
120
    ptr: INTEGER;
121
    color: INTEGER;
122
BEGIN
123
    Y1 := MAX(MIN(y1, y2), 0);
124
    Y2 := MIN(MAX(y1, y2), canvas.height - 1);
125
    IF (0 <= x) & (x < canvas.width) THEN
126
        color := canvas.color;
127
        ptr := canvas.bitmap + Y1*canvas.width*4 + x*4;
128
        FOR i := Y1 TO Y2 DO
129
            IF canvas.mode = modeNOT THEN
130
                SYSTEM.GET32(ptr, color);
131
                color := ORD(-BITS(color)*{0..23})
132
            ELSIF canvas.mode = modeXOR THEN
133
                SYSTEM.GET32(ptr, color);
134
                color := ORD((BITS(color)/BITS(canvas.color))*{0..23})
135
            END;
136
            SYSTEM.PUT32(ptr, color);
137
            INC(ptr, canvas.width*4)
138
        END
139
    END
140
END VLine;
141
 
142
 
143
PROCEDURE notVLine* (canvas: tCanvas; x, y1, y2: INTEGER);
144
BEGIN
145
    IF (0 <= y1) & (y1 < canvas.height) & (0 <= y2) & (y2 < canvas.height) THEN
146
        canvas.mode := modeNOT;
147
        VLine(canvas, x, y1, y2);
148
        canvas.mode := modeCOPY
149
    END
150
END notVLine;
151
 
152
 
153
PROCEDURE xorVLine* (canvas: tCanvas; x, y1, y2: INTEGER);
154
BEGIN
155
    IF (0 <= y1) & (y1 < canvas.height) & (0 <= y2) & (y2 < canvas.height) THEN
156
        canvas.mode := modeXOR;
157
        SetColor(canvas, 0FF0000H);
158
        VLine(canvas, x, y1, y2);
159
        canvas.mode := modeCOPY
160
    END
161
END xorVLine;
162
 
163
 
164
PROCEDURE DLine* (canvas: tCanvas; x1, x2, y: INTEGER; k: INTEGER);
165
VAR
166
    ptr: INTEGER;
167
    color: INTEGER;
168
    d: INTEGER;
169
BEGIN
170
    color := canvas.color;
171
    ptr := canvas.bitmap + y*canvas.width*4 + x1*4;
172
    IF k = -1 THEN
173
        d := canvas.width*4 + 4
174
    ELSIF k = 1 THEN
175
        d := 4 - canvas.width*4
176
    END;
177
    WHILE x1 <= x2 DO
178
        SYSTEM.PUT32(ptr, color);
179
        INC(ptr, d);
180
        INC(x1)
181
    END
182
END DLine;
183
 
184
 
9174 akron1 185
PROCEDURE Triangle* (canvas: tCanvas; x1, y1, x2, y2: INTEGER; orientation: BOOLEAN);
186
VAR
187
	i, a, b, d: INTEGER;
188
BEGIN
189
	d := ORD(orientation)*2 - 1;
190
	IF y1 = y2 THEN
191
		i := y1;
192
		a := MIN(x1, x2);
193
		b := MAX(x1, x2);
194
		WHILE a <= b DO
195
			HLine(canvas, i, a, b);
196
			INC(i, d);
197
			INC(a);
198
			DEC(b)
199
		END
200
	ELSIF x1 = x2 THEN
201
		i := x1;
202
		a := MIN(y1, y2);
203
		b := MAX(y1, y2);
204
		WHILE a <= b DO
205
			VLine(canvas, i, a, b);
206
			INC(i, d);
207
			INC(a);
208
			DEC(b)
209
		END
210
	END
211
END Triangle;
212
 
213
 
8728 leency 214
PROCEDURE FillRect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
215
VAR
216
    y: INTEGER;
217
BEGIN
218
    FOR y := top TO bottom DO
219
        HLine(canvas, y, left, right)
220
    END
221
END FillRect;
222
 
223
 
224
PROCEDURE Rect* (canvas: tCanvas; left, top, right, bottom: INTEGER);
225
BEGIN
226
    HLine(canvas, top, left, right);
227
    HLine(canvas, bottom, left, right);
228
    VLine(canvas, left, top, bottom);
229
    VLine(canvas, right, top, bottom)
230
END Rect;
231
 
232
 
233
PROCEDURE clear* (canvas: tCanvas);
234
VAR
235
    ptr, ptr2, w, i: INTEGER;
236
BEGIN
237
    HLine(canvas, 0, 0, canvas.width - 1);
238
    w := canvas.width*4;
239
    ptr := canvas.bitmap;
240
    ptr2 := ptr;
241
    i := canvas.height - 1;
242
    WHILE i > 0 DO
243
        INC(ptr2, w);
244
        SYSTEM.MOVE(ptr, ptr2, w);
245
        DEC(i)
246
    END
247
END clear;
248
 
249
 
250
PROCEDURE TextOut* (canvas: tCanvas; x, y: INTEGER; text: INTEGER; n: INTEGER);
251
CONST
252
    WCHAR_SIZE = 2;
253
VAR
254
    color, i: INTEGER;
9174 akron1 255
    c: WCHAR;
8728 leency 256
BEGIN
257
    IF (0 <= y) & (y <= canvas.height - canvas.font.height - 1) THEN
258
        IF x < 0 THEN
259
            i := -(x DIV canvas.font.width);
260
            INC(x, i*canvas.font.width);
261
            DEC(n, i)
262
        ELSE
263
            i := 0
264
        END;
265
        IF n > 0 THEN
266
            n := MAX(MIN(n, (canvas.width - x) DIV canvas.font.width), 0);
267
            color := canvas.color;
268
            canvas.color := canvas.backColor;
269
            FillRect(canvas, x, y, x + n*canvas.font.width, y + canvas.font.height);
270
            canvas.color := color;
9174 akron1 271
            WHILE n > 0 DO
272
                SYSTEM.GET(text + i*WCHAR_SIZE, c);
273
                IF ~Lines.isSpace(c) THEN
274
                    K.sysfunc6(4, x*65536 + y, LSL(28H + canvas.font.size, 24) + canvas.textColor, SYSTEM.ADR(c), 1, canvas.bitmap - 8)
275
                END;
8728 leency 276
                INC(x, canvas.font.width);
277
                INC(i);
278
                DEC(n)
9174 akron1 279
            END
280
            (*K.sysfunc6(4, x*65536 + y, LSL(28H + canvas.font.size, 24) + canvas.textColor, text + i*WCHAR_SIZE, n, canvas.bitmap - 8)*)
8728 leency 281
        END
282
    END
283
END TextOut;
284
 
285
 
286
PROCEDURE TextOut2* (canvas: tCanvas; x, y: INTEGER; text: ARRAY OF WCHAR; n: INTEGER);
287
BEGIN
288
    TextOut(canvas, x, y, SYSTEM.ADR(text[0]), n)
289
END TextOut2;
290
 
291
 
292
PROCEDURE CreateCanvas* (width, height: INTEGER): tCanvas;
293
VAR
294
    canvas: tCanvas;
295
BEGIN
296
    NEW(canvas);
297
    canvas.bitmap := K.malloc(width*height*4 + 8);
298
    ASSERT(canvas.bitmap # 0);
299
    SYSTEM.PUT32(canvas.bitmap, width);
300
    SYSTEM.PUT32(canvas.bitmap + 4, height);
301
    INC(canvas.bitmap, 8);
302
    canvas.width := width;
303
    canvas.height := height;
304
    canvas.mode := modeCOPY
305
    RETURN canvas
306
END CreateCanvas;
307
 
308
 
309
PROCEDURE destroy* (VAR canvas: tCanvas);
310
BEGIN
311
    IF canvas # NIL THEN
312
        canvas.bitmap := K.free(canvas.bitmap);
313
        DISPOSE(canvas)
314
    END
315
END destroy;
316
 
317
 
318
END Graph.