[go: up one dir, main page]

Menu

[r9]: / src / ulogger.pas  Maximize  Restore  History

Download this file

205 lines (178 with data), 5.0 kB

  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
{ This file is a part of Rising Force Roguelike, the roguelike game.
Copyright (C) 2010 Konovod Andrey (kipar@front.ru).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
}
unit uLogger;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics,
uRWFile;
type
{ TLogger }
TLogger = class
Lines: TStringList;
Picture, Mask: TBitmap;
// Canvas: TCanvas;
DrawRect: TRect;
Width, Height: integer;
ScrollPos: integer;
CurLine: string;
constructor Create(aRect: TRect);
destructor Destroy; override;
procedure RW;
procedure RepaintPicture;
procedure PaintStr(var X: integer; Y: integer; S: string; C: TColor);
procedure Draw(aCanvas: TCanvas);
procedure Writeln(s: string = '');
procedure InterWriteln(s: string = '');
procedure Write(s: string);
procedure ClearPicture;
procedure SetRect(aRect: TRect; NoFormUpdate: boolean = False);
end;
implementation
uses Math,
MyToken, uChoice, uMain, uGlobalConfig;
{ TLogger }
constructor TLogger.Create(aRect: TRect);
begin
inherited Create;
Lines := TStringList.Create;
CurLine := '';
Picture := TBitmap.Create;
with Picture.Canvas do
begin
// Brush.Color := clNavy;
Brush.Color := clBlack;
Font.Color := clBtnFace;
TheConfig.LogFont.SetFont(Font);
end;
Mask := TBitmap.Create;
with Mask.Canvas do
begin
Brush.Color := clWhite;
Font.Color := clBlack;
TheConfig.LogFont.SetFont(Font);
end;
SetRect(aRect, True);
ScrollPos := 0;
RepaintPicture;
end;
destructor TLogger.Destroy;
begin
Lines.Free;
Picture.Free;
Mask.Free;
inherited Destroy;
end;
procedure TLogger.RW;
begin
RWStrings(Lines);
RWBuffer(SizeOf(DrawRect), DrawRect);
RWInteger(ScrollPos);
if not IsWrite then
SetRect(DrawRect);
RWString(CurLine);
if not IsWrite then
RepaintPicture;
end;
procedure TLogger.RepaintPicture;
var
X, Y, I, N, D: integer;
S, S1: string;
C: TColor;
begin
ClearPicture;
D := TheConfig.LOGSTRINGDELTA;
N := Height div D;
Y := Height - D * min(N, Lines.Count - ScrollPos);
for I := min(N, Lines.Count - ScrollPos) downto 1 do
begin
X := 10;
S := Lines[Lines.Count - I - ScrollPos];
while S <> '' do
begin
S1 := TokenStrS(S, '{');
PaintStr(X, Y, S1, clBtnFace);
S1 := TokenStrS(S, ':');
if S1 = '' then
break;
C := StringToColor(Trim(S1));
S1 := TokenStrS(S, '}');
{ if S1 = '' then
break;}
PaintStr(X, Y, S1, C);
end;
Inc(Y, D);
end;
end;
procedure TLogger.PaintStr(var X: integer; Y: integer; S: string; C: TColor);
begin
if C = clBlack then
C := clWhite;
Picture.Canvas.Font.Color := C;
Picture.Canvas.TextOut(X, Y, S);
Mask.Canvas.TextOut(X, Y, S);
Inc(X, Picture.Canvas.TextWidth(S));
end;
procedure TLogger.Draw(aCanvas: TCanvas);
begin
aCanvas.CopyMode := cmSrcAnd;
aCanvas.CopyRect(DrawRect, Mask.Canvas, Rect(0, 0, Width, Height));
aCanvas.CopyMode := cmSrcPaint;
aCanvas.CopyRect(DrawRect, Picture.Canvas, Rect(0, 0, Width, Height));
end;
procedure TLogger.Writeln(s: string = '');
begin
Lines.Add(CurLine + s);
while Lines.Count > 100 do
Lines.Delete(0);
Lines.SaveToFile('.\debuglog.txt');
fmMain.ScrollBar1.Max := max(1, Lines.Count - Height div TheConfig.LOGSTRINGDELTA);
fmMain.ScrollBar1.Position := fmMain.ScrollBar1.Max - ScrollPos;
CurLine := '';
RepaintPicture;
end;
procedure TLogger.InterWriteln(s: string);
begin
if CurLine = '' then
Writeln(s)
else
begin
Writeln;
Write(s);
end;
end;
procedure TLogger.Write(s: string);
begin
CurLine := CurLine + s;
// RepaintPicture;
end;
procedure TLogger.ClearPicture;
begin
Picture.Canvas.FillRect(0, 0, Width, Height);
Mask.Canvas.FillRect(0, 0, Width, Height);
end;
procedure TLogger.SetRect(aRect: TRect; NoFormUpdate: boolean = False);
begin
DrawRect := aRect;
Width := aRect.Right - aRect.Left;
Height := aRect.Bottom - aRect.Top;
Picture.Width := Width;
Picture.Height := Height;
Mask.Width := Width;
Mask.Height := Height;
if not NoFormUpdate then
fmMain.UpdateLeftBar;
RepaintPicture;
end;
end.