OSDN Git Service

・板一覧更新の処理メッセージを追加
[gikonavigoeson/gikonavi.git] / GestureModel.pas
1 unit GestureModel;
2
3 interface
4
5 uses
6         Windows, Messages, SysUtils, Classes, Controls, StrUtils, IniFiles,
7         ActnList;
8
9 type
10         TGestureModel = class(TObject)
11         private
12                 FGestureList: TStringList;
13                 FMargin: Integer;
14                 function CheckGestureStr(Gesture: string): Boolean;
15                 function GetActionItem(ActionList: TActionList; ActionStr: string): TAction;
16                 function Get(Index: Integer): string;
17                 function GetObject(Index: Integer): TObject;
18                 function GetCount: Integer;
19         public
20                 constructor Create;
21                 destructor Destroy; override;
22                 procedure AddGesture(Gesture: string; Action: TAction);
23                 procedure DeleteGesture(Gesture: string);
24                 procedure ClearGesture;
25                 function GetGestureAction(Gesture: string): TAction;
26                 function GetActionGesture( action : TAction ) : string;
27                 property Margin: Integer read FMargin write FMargin;
28                 procedure LoadGesture(FileName: string; ActionList: TActionList);
29                 procedure SaveGesture(FileName: string);
30
31                 property Items[Index: Integer]: string read Get; default;
32                 property Objects[Index: Integer]: TObject read GetObject;
33                 property Count: Integer read GetCount;
34         end;
35
36 implementation
37
38 constructor TGestureModel.Create;
39 begin
40         inherited Create;
41         FGestureList := TStringList.Create;
42 end;
43
44 destructor TGestureModel.Destroy;
45 begin
46         FreeAndNil(FGestureList);
47         inherited Destroy;
48 end;
49
50 //\8ew\92è\82³\82ê\82½index\82Ì\83W\83F\83X\83`\83\83\81[\95\8e\9a\97ñ\82ð\95Ô\82·
51 function TGestureModel.Get(Index: Integer): string;
52 begin
53         if (Index < 0) or (Index >= FGestureList.Count) then
54                 raise Exception.Create('ERR');
55         Result := FGestureList[Index];
56 end;
57
58 //\8ew\92è\82³\82ê\82½index\82Ì\83I\83u\83W\83F\83N\83g\82ð\95Ô\82·
59 function TGestureModel.GetObject(Index: Integer): TObject;
60 begin
61         if (Index < 0) or (Index >= FGestureList.Count) then
62                 raise Exception.Create('ERR');
63         Result := FGestureList.Objects[Index];
64 end;
65
66 //\83W\83F\83X\83`\83\83\81[\82Ì\95\8e\9a\97ñ\82É\95Ï\82È\95\8e\9a\82ª\93ü\82Á\82Ä\82È\82¢\82©\83`\83F\83b\83N\82ð\8ds\82¤
67 function TGestureModel.CheckGestureStr(Gesture: string): Boolean;
68 const
69         G_STR: array[0..3] of string = ('\81ª', '\81©', '\81«', '\81¨');
70 var
71         i: Integer;
72         j: Integer;
73         c: Boolean;
74 begin
75         Result := False;
76         if Length(Gesture) = 0 then
77                 Exit;
78         for i := 0 to (Length(Gesture) div 2) - 1 do begin
79                 c := False;
80                 for j := 0 to Length(G_STR) - 1 do begin
81                         if MidStr(Gesture, i + 1, 1) = G_STR[j] then begin
82                                 c := True;
83                                 Break;
84                         end;
85                 end;
86                 if not c then
87                         Exit;
88         end;
89         Result := True;
90 end;
91
92 //\83W\83F\83X\83`\83\83\81[\83\8a\83X\83g\82Ì\83T\83C\83Y\82ð\95Ô\82·
93 function TGestureModel.GetCount: Integer;
94 begin
95         Result := FGestureList.Count;
96 end;
97
98 //\83W\83F\83X\83`\83\83\81[\82Æ\81A\82»\82ê\82É\8c\8b\82Ñ\82Â\82­\83A\83N\83V\83\87\83\93\82ð\83\8a\83X\83g\82É\93o\98^\82·\82é
99 procedure TGestureModel.AddGesture(Gesture: string; Action: TAction);
100 begin
101         if (not CheckGestureStr(Gesture)) or (Action = nil) then
102                 Exit;
103         DeleteGesture(Gesture);
104         FGestureList.AddObject(Gesture, Action);
105 end;
106
107 //\8ew\92è\82³\82ê\82½\83W\83F\83X\83`\83\83\81[\82ð\8dí\8f\9c
108 procedure TGestureModel.DeleteGesture(Gesture: string);
109 var
110         idx: Integer;
111 begin
112         idx := FGestureList.IndexOf(Gesture);
113         if idx <> -1 then
114                 FGestureList.Delete(idx);
115 end;
116
117 //\83W\83F\83X\83`\83\83\81[\83\8a\83X\83g\82ð\83N\83\8a\83A
118 procedure TGestureModel.ClearGesture;
119 begin
120         FGestureList.Clear;
121 end;
122
123 //\83W\83F\83X\83`\83\83\81[\95\8e\9a\97ñ\82©\82ç\8aY\93\96\82·\82é\83A\83N\83V\83\87\83\93\82ð\95Ô\82·
124 function TGestureModel.GetGestureAction(Gesture: string): TAction;
125 var
126         idx: Integer;
127 begin
128         Result := nil;
129         idx := FGestureList.IndexOf(Gesture);
130         if idx <> -1 then
131                 if FGestureList.Objects[idx] is TAction then
132                         Result := TAction(FGestureList.Objects[idx]);
133 end;
134
135 // \83A\83N\83V\83\87\83\93\82©\82ç\8aY\93\96\82·\82é\83W\83F\83X\83`\83\83\81[\95\8e\9a\97ñ\82ð\95Ô\82·
136 function TGestureModel.GetActionGesture( action : TAction ) : string;
137 var
138         i : Integer;
139 begin
140         Result := '';
141         for i := 0 to FGestureList.Count - 1 do begin
142                 if FGestureList.Objects[ i ] = action then begin
143                         Result := FGestureList[ i ];
144                         Break;
145                 end;
146         end;
147 end;
148
149 //ini\82©\82ç\83W\83F\83X\83`\83\83\81[\88ê\97\97\82ð\93Ç\82Ý\8d\9e\82Þ
150 procedure TGestureModel.LoadGesture(FileName: string; ActionList: TActionList);
151 var
152         ini: TMemIniFile;
153         i: Integer;
154         key: string;
155         GestureStr: string;
156         Action: TAction;
157         ActionStr: string;
158 begin
159         ini := TMemIniFile.Create(FileName);
160         try
161                 i := 0;
162                 while (True) do begin
163                         key := 'Gesture' + IntToStr(i);
164                         GestureStr := ini.ReadString('MouseGesture', key, '');
165                         key := 'Action' + IntToStr(i);
166                         ActionStr := ini.ReadString('MouseGesture', key, '');
167                         if (GestureStr = '') or (ActionStr = '') then
168                                 Break;
169                         Action := GetActionItem(ActionList, ActionStr);
170                         if Action <> nil then begin
171                                 AddGesture(GestureStr, Action);
172                         end;
173                         inc(i);
174                 end;
175                 FMargin := ini.ReadInteger('MouseGesture', 'Margin', 15);
176         finally
177                 ini.Free;
178         end;
179 end;
180
181 //\83W\83F\83X\83`\83\83\81[\88ê\97\97\82ðini\82É\8f\91\82«\8d\9e\82Þ
182 procedure TGestureModel.SaveGesture(FileName: string);
183 var
184         ini: TMemIniFile;
185         i: Integer;
186         key: string;
187 begin
188         ini := TMemIniFile.Create(FileName);
189         try
190                 ini.EraseSection('MouseGesture');
191                 for i := 0 to FGestureList.Count - 1 do begin
192                         if CheckGestureStr(FGestureList[i]) and (FGestureList.Objects[i] is TAction) then begin
193                                 key := 'Gesture' + IntToStr(i);
194                                 ini.WriteString('MouseGesture', key, FGestureList[i]);
195                                 key := 'Action' + IntToStr(i);
196                                 ini.WriteString('MouseGesture', key, TAction(FGestureList.Objects[i]).Name);
197                         end;
198                 end;
199                 ini.WriteInteger('MouseGesture', 'Margin', FMargin);
200                 ini.UpdateFile;
201         finally
202                 ini.Free;
203         end;
204 end;
205
206 //\83A\83N\83V\83\87\83\93\95\8e\9a\97ñ\82©\82ç\83A\83N\83V\83\87\83\93\82ð\95Ô\82·
207 function TGestureModel.GetActionItem(ActionList: TActionList; ActionStr: string): TAction;
208 var
209         i: Integer;
210 begin
211         Result := nil;
212         for i := 0 to ActionList.ActionCount - 1 do begin
213                 if ActionList.Actions[i].Name = ActionStr then begin
214                         if ActionList.Actions[i] is TAction then begin
215                                 Result := TAction(ActionList.Actions[i]);
216                                 Exit;
217                         end;
218                 end;
219         end;
220 end;
221
222 end.