Skip to content

Commit 9df62f9

Browse files
authored
Merge pull request #28 from yokoro13/feature-terminalview
TerminalViewの実装
2 parents b6d323c + bca35a9 commit 9df62f9

12 files changed

Lines changed: 797 additions & 792 deletions

File tree

-4 Bytes
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<uses-permission android:name="android.permission.BLUETOOTH"/>
88
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
99
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
10+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
1011

1112
<application
1213
android:hardwareAccelerated="true"
@@ -16,9 +17,12 @@
1617
android:roundIcon="@mipmap/ic_launcher_round"
1718
android:supportsRtl="true"
1819
android:theme="@style/AppTheme">
19-
<activity android:name="com.i14yokoro.mldpterminal.MainActivity"
20-
android:screenOrientation="portrait"
21-
android:windowSoftInputMode="adjustResize">
20+
<activity
21+
android:name="com.i14yokoro.mldpterminal.MainActivity"
22+
android:screenOrientation="portrait"
23+
android:resizeableActivity="true"
24+
25+
android:windowSoftInputMode="adjustResize">
2226
<intent-filter>
2327
<action android:name="android.intent.action.MAIN" />
2428

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.i14yokoro.mldpterminal
22

3+
import com.i14yokoro.mldpterminal.terminalview.TerminalView.Cursor
34

45
/**
56
* ANSIのエスケープシーケンスと同じ動作をする
@@ -10,124 +11,123 @@ class EscapeSequence
1011
*/
1112
internal constructor(private val termBuffer: TerminalBuffer) {
1213

13-
private val nonBreakingSpace = Typography.nbsp
14-
15-
private val currentRow: Int
16-
get() = termBuffer.cursorY + termBuffer.topRow
14+
private val space = ' '
1715

1816
/**
1917
* @param n 移動量
2018
*/
21-
fun moveRight(n: Int) {
22-
moveCursorX(n)
19+
fun moveRight(cursor: Cursor, n: Int) {
20+
moveCursorX(cursor, n)
2321
}
2422

2523
/**
2624
* @param n 移動量
2725
*/
28-
fun moveLeft(n: Int) {
29-
moveCursorX(-n)
26+
fun moveLeft(cursor: Cursor, n: Int) {
27+
moveCursorX(cursor, -n)
3028
}
3129

3230
/**
3331
* @param n 移動量
3432
*/
35-
fun moveUp(n: Int) {
36-
moveCursorY(-n)
37-
termBuffer.currentRow -= n
33+
fun moveUp(cursor: Cursor, n: Int) {
34+
termBuffer.currentRow -= if (cursor.y - n > 0) n else cursor.y
35+
moveCursorY(cursor, -n)
3836
}
3937

4038
/**
4139
* @param n 移動量
4240
*/
43-
fun moveDown(n: Int) {
44-
if(termBuffer.cursorY + n >= termBuffer.screenColumnSize){
45-
termBuffer.currentRow += termBuffer.cursorY + n - termBuffer.screenColumnSize
46-
} else {
47-
termBuffer.currentRow += n
48-
}
49-
if(termBuffer.currentRow >= termBuffer.totalColumns) {
50-
for(i in 0 .. termBuffer.currentRow - termBuffer.totalColumns) {
41+
fun moveDown(cursor: Cursor, n: Int) {
42+
termBuffer.currentRow += if (cursor.y + n < termBuffer.screenRowSize) n else (termBuffer.screenRowSize - cursor.y - 1)
43+
moveCursorY(cursor, n)
44+
45+
if(termBuffer.topRow + cursor.y >= termBuffer.totalLines) {
46+
for(i in 0 .. termBuffer.topRow + cursor.y - termBuffer.totalLines) {
5147
termBuffer.addRow()
5248
}
5349
}
54-
55-
moveCursorY(n)
5650
}
5751

5852
/**
5953
* @param n 移動量
6054
*/
61-
fun moveUpToRowLead(n: Int) {
62-
termBuffer.cursorX = 0
63-
moveUp(n)
55+
fun moveUpToRowLead(cursor: Cursor, n: Int) {
56+
cursor.x = 0
57+
moveUp(cursor, n)
6458
}
6559

6660
/**
6761
* @param n 移動量
6862
*/
69-
fun moveDownToRowLead(n: Int) {
70-
termBuffer.cursorX = 0
71-
moveDown(n)
63+
fun moveDownToRowLead(cursor: Cursor, n: Int) {
64+
cursor.x = 0
65+
moveDown(cursor, n)
7266
}
7367

7468
/**
7569
* @param n 移動量
7670
*/
77-
fun moveCursor(n: Int) {
78-
termBuffer.cursorX = 0
79-
moveRight(n-1)
71+
fun moveCursor(cursor: Cursor, n: Int) {
72+
cursor.x = 0
73+
moveRight(cursor, n-1)
8074
}
8175

8276
/**
8377
* @param n 縦の移動量(0 < n)
8478
* @param m 横の移動量(0 < m)
8579
*/
86-
fun moveCursor(n: Int, m: Int) { //n,mは1~
87-
termBuffer.cursorY = 0
88-
termBuffer.cursorX = 0
89-
moveDown(n-1)
90-
moveRight(m-1)
80+
fun moveCursor(cursor: Cursor, n: Int, m: Int) { //n,mは1~
81+
if(n-1 > cursor.y) {
82+
termBuffer.currentRow -= cursor.y
83+
cursor.y = 0
84+
moveDown(cursor, n-1)
85+
} else {
86+
termBuffer.currentRow -= cursor.y - (n-1)
87+
cursor.y = n-1
88+
}
89+
90+
cursor.x = m-1
9191
}
9292

9393
/**
9494
* 画面消去
9595
*/
96-
private fun clearDisplay() {
97-
for (x in termBuffer.cursorX until termBuffer.screenRowSize){
98-
termBuffer.setText(x, currentRow, nonBreakingSpace)
96+
private fun clearDisplay(cursor: Cursor) {
97+
for (x in cursor.x until termBuffer.screenColumnSize){
98+
termBuffer.setText(x, termBuffer.topRow + cursor.y, space)
9999
}
100-
for (y in termBuffer.cursorY+1 until termBuffer.displayedLines) {
101-
for (x in 0 until termBuffer.screenRowSize){
102-
termBuffer.setText(x, y, nonBreakingSpace)
100+
for (y in cursor.y+1 until termBuffer.displayedLines) {
101+
for (x in 0 until termBuffer.screenColumnSize){
102+
termBuffer.setText(x, y, space)
103103
}
104104
}
105105
}
106106

107107
/**
108108
* @param n 画面消去の方法
109109
*/
110-
fun clearDisplay(n: Int) {
110+
fun clearDisplay(cursor: Cursor, n: Int) {
111111
//一番下までスクロールして消去ぽい
112112
if (n == 0) { //カーソルより後ろにある画面上の文字を消す
113-
clearDisplay()
113+
clearDisplay(cursor)
114114
}
115115

116116
if (n == 1) { //カーソルより前にある画面上の文字を消す
117-
for (y in termBuffer.topRow .. currentRow) {
118-
for (x in 0 until termBuffer.screenRowSize) {
119-
termBuffer.setText(x, y, nonBreakingSpace)
120-
if (y == currentRow && x == termBuffer.cursorX) {
117+
for (y in termBuffer.topRow .. termBuffer.topRow + cursor.y) {
118+
for (x in 0 until termBuffer.screenColumnSize) {
119+
if (y == termBuffer.topRow + cursor.y && x == cursor.x) {
121120
break
122121
}
122+
termBuffer.setText(x, y, space)
123123
}
124124
}
125125
}
126126

127127
if (n == 2) { //全消去
128128
for (y in termBuffer.topRow until termBuffer.displayedLines + termBuffer.topRow) {
129-
for (x in 0 until termBuffer.screenRowSize) {
130-
termBuffer.setText(x, y, nonBreakingSpace)
129+
for (x in 0 until termBuffer.screenColumnSize) {
130+
termBuffer.setText(x, y, space)
131131
}
132132
}
133133
}
@@ -136,32 +136,31 @@ internal constructor(private val termBuffer: TerminalBuffer) {
136136
/**
137137
* @param n 行削除の方法
138138
*/
139-
fun clearRow(n: Int) {
139+
fun clearRow(cursor: Cursor, n: Int) {
140140
if (n == 0) { //カーソル以降にある文字を消す
141-
for (x in termBuffer.cursorX until termBuffer.screenRowSize) {
142-
termBuffer.setText(x, currentRow, nonBreakingSpace)
141+
for (x in cursor.x until termBuffer.screenColumnSize) {
142+
termBuffer.setText(x, termBuffer.topRow + cursor.y, space)
143143
}
144144
}
145145

146146
if (n == 1) { //カーソル以前にある文字を消す
147-
for (x in 0 until termBuffer.cursorX) {
148-
termBuffer.setText(x, currentRow, nonBreakingSpace)
147+
for (x in 0 until cursor.x) {
148+
termBuffer.setText(x, termBuffer.topRow + cursor.y, space)
149149
}
150150
}
151151

152152
if (n == 2) { //全消去
153-
for (x in 0 until termBuffer.screenRowSize) {
154-
termBuffer.setText(x, currentRow, nonBreakingSpace)
153+
for (x in 0 until termBuffer.screenColumnSize) {
154+
termBuffer.setText(x, termBuffer.topRow + cursor.y, space)
155155
}
156-
157156
}
158157
}
159158

160159
/**
161160
* @param n 移動する量
162161
*/
163162
fun scrollNext(n: Int) {
164-
if (termBuffer.topRow + n > termBuffer.totalColumns) return //一番したに空白追加??
163+
if (termBuffer.topRow + n > termBuffer.totalLines) return //一番したに空白追加??
165164
termBuffer.topRow = termBuffer.topRow + n
166165
}
167166

@@ -194,15 +193,15 @@ internal constructor(private val termBuffer: TerminalBuffer) {
194193
/**
195194
* @param x カーソルをx移動させる
196195
*/
197-
private fun moveCursorX(x: Int) {
198-
termBuffer.cursorX += x
196+
private fun moveCursorX(cursor: Cursor, x: Int) {
197+
cursor.x += x
199198
}
200199

201200
/**
202201
* @param y カーソルをy移動させる
203202
*/
204-
private fun moveCursorY(y: Int) {
205-
termBuffer.cursorY += y
203+
private fun moveCursorY(cursor: Cursor, y: Int) {
204+
cursor.y += y
206205
}
207206

208207
}

0 commit comments

Comments
 (0)