-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathcp437-table.lua
More file actions
144 lines (131 loc) · 4.41 KB
/
Copy pathcp437-table.lua
File metadata and controls
144 lines (131 loc) · 4.41 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
-- An in-game CP437 table
local dialog = require('gui.dialogs')
local gui = require('gui')
local textures = require('gui.textures')
local widgets = require('gui.widgets')
local to_pen = dfhack.pen.parse
local function get_key_pens(ch)
return {
lt=to_pen{tile=curry(textures.tp_border_thin, 1), write_to_lower=true},
t=to_pen{tile=curry(textures.tp_border_thin, 2), ch=ch, write_to_lower=true, top_of_text=true},
t_ascii=to_pen{ch=32},
rt=to_pen{tile=curry(textures.tp_border_thin, 3), write_to_lower=true},
lb=to_pen{tile=curry(textures.tp_border_thin, 15), write_to_lower=true},
b=to_pen{tile=curry(textures.tp_border_thin, 16), ch=ch, write_to_lower=true, bottom_of_text=true},
rb=to_pen{tile=curry(textures.tp_border_thin, 17), write_to_lower=true},
}
end
local function get_key_hover_pens(ch)
return {
t=to_pen{tile=curry(textures.tp_border_thin, 2), fg=COLOR_WHITE, bg=COLOR_RED, ch=ch, write_to_lower=true, top_of_text=true},
t_ascii=to_pen{fg=COLOR_WHITE, bg=COLOR_RED, ch=ch == 0 and 0 or 32},
b=to_pen{tile=curry(textures.tp_border_thin, 16), fg=COLOR_WHITE, bg=COLOR_RED, ch=ch, write_to_lower=true, bottom_of_text=true},
}
end
CPDialog = defclass(CPDialog, widgets.Window)
CPDialog.ATTRS {
frame_title='CP437 table',
frame={w=100, h=26},
}
function CPDialog:init(info)
self:addviews{
widgets.EditField{
view_id='edit',
frame={t=0, l=0},
},
widgets.Panel{
view_id='board',
frame={t=2, l=0, w=96, h=18},
},
widgets.Label{
frame={b=2, l=0},
text='Click characters or type',
},
widgets.HotkeyLabel{
frame={b=0, l=0},
key='SELECT',
label='Send text to parent',
auto_width=true,
on_activate=self:callback('submit'),
},
widgets.HotkeyLabel{
frame={b=0},
key='STRING_A000',
key_sep='',
label='Click here to Backspace',
auto_width=true,
on_activate=function() self.subviews.edit:onInput{_STRING=0} end,
},
widgets.HotkeyLabel{
frame={b=0, r=0},
key='LEAVESCREEN',
label='Cancel',
auto_width=true,
on_activate=function() self.parent_view:dismiss() end,
},
}
local board = self.subviews.board
local edit = self.subviews.edit
for ch = 0,255 do
local xoff, yoff = (ch%32) * 3, (ch//32) * 2
if not dfhack.screen.charToKey(ch) then ch = 0 end
local pens = get_key_pens(ch)
local hpens = get_key_hover_pens(ch)
local function get_top_tile()
return dfhack.screen.inGraphicsMode() and pens.t or pens.t_ascii
end
local function get_top_htile()
return dfhack.screen.inGraphicsMode() and hpens.t or hpens.t_ascii
end
board:addviews{
widgets.Label{
frame={t=yoff, l=xoff, w=3, h=2},
auto_height=false,
text={
{tile=pens.lt},
{tile=get_top_tile, htile=get_top_htile},
{tile=pens.rt},
NEWLINE,
{tile=pens.lb},
{tile=pens.b, htile=hpens.b},
{tile=pens.rb},
},
on_click=function() if ch ~= 0 then edit:insert(string.char(ch)) end end,
},
}
end
end
function CPDialog:submit()
local keys = {}
local text = self.subviews.edit.text
for i = 1,#text do
local k = dfhack.screen.charToKey(string.byte(text:sub(i, i)))
if not k then
dialog.showMessage('Error',
('Invalid character at position %d: "%s"'):
format(i, text:sub(i, i)),
COLOR_LIGHTRED)
return
end
keys[i] = k
end
local screen = self.parent_view
local parent = screen._native.parent
dfhack.screen.hideGuard(screen, function()
for i, k in pairs(keys) do
gui.simulateInput(parent, k)
end
end)
screen:dismiss()
end
CPScreen = defclass(CPScreen, gui.ZScreen)
CPScreen.ATTRS {
focus_path='cp437-table',
}
function CPScreen:init()
self:addviews{CPDialog{}}
end
function CPScreen:onDismiss()
view = nil
end
view = view and view:raise() or CPScreen{}:show()