author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 15 Feb 2018 18:32:28 +0100 | |
changeset 13009 | 67cc3cac4c8e |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
1 |
/* |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
4 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
9 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
13 |
* GNU General Public License for more details. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
14 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
18 |
*/ |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
19 |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
package org.hedgewars.hedgeroid; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
import java.util.ArrayList; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
import java.util.Collection; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
import java.util.List; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
26 |
import org.hedgewars.hedgeroid.netplay.MessageLog; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
import android.content.Context; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
import android.text.method.LinkMovementMethod; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
import android.view.View; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
import android.view.ViewGroup; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
import android.widget.AbsListView.LayoutParams; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
import android.widget.BaseAdapter; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
import android.widget.TextView; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
/** |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
* Optimization: ListView is smart enough to try re-using the same view for an item |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
* with the same ID, but it still calls getView for those items when the list changes. |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
* Since lines with a given ID never change in our chatlog, we can avoid the effort |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
* of TextView.setText in many cases by checking if the view is already set up for the |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
* line with the right ID - but to do that, the view needs to remember the ID it's |
10017 | 42 |
* holding the text for. That's what the LoglineView does. |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
*/ |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
class LoglineView extends TextView { |
10017 | 45 |
long chatlogId = -1; |
46 |
||
47 |
public LoglineView(Context context) { |
|
48 |
super(context); |
|
49 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
50 |
} |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
51 |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
52 |
/** |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
53 |
* For performance reasons, the chatlog is implemented as ListView instead of a |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
54 |
* single TextView (although I later learned that TextView might also have |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
55 |
* facilities for efficient appending with limited backlog... oh well, this |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
56 |
* works). Every chat line is a line in the ListView, and this adapter prepares |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
57 |
* the textviews from a messagelog in an efficient way. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
58 |
*/ |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
59 |
public class ChatlogAdapter extends BaseAdapter implements MessageLog.Listener { |
10017 | 60 |
long idOffset = 0; |
61 |
private List<CharSequence> log = new ArrayList<CharSequence>(); |
|
62 |
private Context context; |
|
63 |
||
64 |
public ChatlogAdapter(Context context) { |
|
65 |
this.context = context; |
|
66 |
} |
|
67 |
||
68 |
public int getCount() { |
|
69 |
return log.size(); |
|
70 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
71 |
|
10017 | 72 |
public Object getItem(int position) { |
73 |
return log.get(position); |
|
74 |
} |
|
75 |
||
76 |
public long getItemId(int position) { |
|
77 |
return position+idOffset; |
|
78 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
79 |
|
10017 | 80 |
public boolean hasStableIds() { |
81 |
return true; |
|
82 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
83 |
|
10017 | 84 |
public void clear() { |
85 |
idOffset += log.size(); |
|
86 |
log.clear(); |
|
87 |
notifyDataSetChanged(); |
|
88 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
89 |
|
10017 | 90 |
public void lineAdded(CharSequence text) { |
91 |
log.add(text); |
|
92 |
notifyDataSetChanged(); |
|
93 |
} |
|
94 |
||
95 |
public void lineRemoved() { |
|
96 |
log.remove(0); |
|
97 |
idOffset += 1; |
|
98 |
notifyDataSetChanged(); |
|
99 |
} |
|
100 |
||
101 |
public void setLog(Collection<CharSequence> log) { |
|
102 |
idOffset += log.size(); |
|
103 |
this.log = new ArrayList<CharSequence>(log); |
|
104 |
notifyDataSetChanged(); |
|
105 |
} |
|
106 |
||
107 |
public View getView(int position, View convertView, ViewGroup parent) { |
|
108 |
LoglineView v = (LoglineView)convertView; |
|
109 |
if (v == null) { |
|
110 |
v = new LoglineView(context); |
|
111 |
v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); |
|
112 |
v.setMovementMethod(LinkMovementMethod.getInstance()); |
|
113 |
} |
|
114 |
long id = getItemId(position); |
|
115 |
if(id != v.chatlogId) { |
|
116 |
v.setText(log.get(position)); |
|
117 |
v.chatlogId = id; |
|
118 |
} |
|
119 |
return v; |
|
120 |
} |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
121 |
} |