-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmupen-lua-ugui-ext.lua
213 lines (177 loc) · 7.73 KB
/
mupen-lua-ugui-ext.lua
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
205
206
207
208
209
210
211
212
213
-- mupen-lua-ugui-ext 2.0.0
-- https://github.com/Aurumaker72/mupen-lua-ugui
local ugui_ext = {
_VERSION = 'v2.0.0',
_URL = 'https://github.com/Aurumaker72/mupen-lua-ugui',
_DESCRIPTION = 'Extension library for mupen-lua-ugui',
_LICENSE = 'GPL-3',
}
if not ugui then
error('ugui must be present in the global scope as \'ugui\' prior to executing ugui-ext', 0)
return
end
ugui_ext.internal = {}
ugui_ext.internal.drawings = {}
ugui_ext.internal.rectangle_to_key = function(rectangle)
return rectangle.x .. rectangle.y .. rectangle.width .. rectangle.height
end
ugui_ext.internal.params_to_key = function(type, rectangle, visual_state)
return type .. visual_state .. ugui_ext.internal.rectangle_to_key(rectangle)
end
if d2d.draw_to_image then
if not UGUI_QUIET then
print('mupen-lua-ugui-ext: Using high-performance cached drawing for mupen64-rr-lua 1.1.7+')
end
ugui_ext.internal.cached_draw = function(key, rectangle, draw_callback)
if not ugui_ext.internal.drawings[key] then
ugui_ext.internal.drawings[key] = d2d.draw_to_image(rectangle.width, rectangle.height, function()
draw_callback({
x = 0,
y = 0,
width = math.floor(rectangle.width),
height = math.floor(rectangle.height),
})
end)
end
d2d.draw_image(
math.floor(rectangle.x),
math.floor(rectangle.y),
math.floor(rectangle.x + rectangle.width),
math.floor(rectangle.y + rectangle.height),
0,
0,
math.floor(rectangle.width),
math.floor(rectangle.height), 1, 0, ugui_ext.internal.drawings[key])
end
ugui_ext.free = function()
for key, value in pairs(ugui_ext.internal.drawings) do
d2d.free_image(value)
end
ugui_ext.internal.drawings = {}
end
end
if not d2d.create_render_target and not d2d.draw_to_image then
print('mupen-lua-ugui-ext: No supported cached rendering method found, falling back to uncached drawing. Performance will be affected. Please update to the latest version of mupen64-rr-lua.')
ugui_ext.internal.cached_draw = function(key, rectangle, draw_callback)
draw_callback(rectangle)
end
end
local function scale_and_center(inner, outer, max_size, adjust_even_odd)
local inner_aspect = inner.width / inner.height
local outer_aspect = outer.width / outer.height
local scale
if inner_aspect > outer_aspect then
scale = outer.width / inner.width
else
scale = outer.height / inner.height
end
if max_size then
scale = math.min(scale, max_size / inner.width, max_size / inner.height)
end
local new_width = inner.width * scale
local new_height = inner.height * scale
local new_x = outer.x + (outer.width - new_width) / 2
local new_y = outer.y + (outer.height - new_height) / 2
if adjust_even_odd then
if (inner.width % 2 == 0 and new_width % 2 ~= 0) or (inner.width % 2 ~= 0 and new_width % 2 == 0) then
new_width = new_width + 1
end
if (inner.height % 2 == 0 and new_height % 2 ~= 0) or (inner.height % 2 ~= 0 and new_height % 2 == 0) then
new_height = new_height + 1
end
end
return {
x = math.ceil(new_x),
y = math.ceil(new_y),
width = math.ceil(new_width),
height = math.ceil(new_height),
}
end
ugui_ext.apply_nineslice = function(style)
if not d2d then
print('No D2D available, falling back to unchanged standard styler to avoid performance issues')
return
end
ugui_ext.free()
local function draw_icon_placeholder(rectangle)
BreitbandGraphics.fill_rectangle(rectangle, BreitbandGraphics.colors.red)
end
local original_draw_icon = ugui.standard_styler.draw_icon
ugui.standard_styler.draw_icon = function(rectangle, color, visual_state, key)
local rectangles = style.icons[key]
if not rectangles then
original_draw_icon(rectangle, color, visual_state, key)
return
end
local rect = rectangles[visual_state]
if not rect then
draw_icon_placeholder(rectangle)
return
end
local adjusted_rect = scale_and_center(rect, rectangle, ugui.standard_styler.params.icon_size, true)
BreitbandGraphics.draw_image(adjusted_rect, rectangles[visual_state], Styles.theme().path,
BreitbandGraphics.colors.white, 'linear')
end
ugui.standard_styler.draw_raised_frame = function(control, visual_state)
local key = ugui_ext.internal.params_to_key('raised_frame', control.rectangle, visual_state)
ugui_ext.internal.cached_draw(key, control.rectangle, function(eff_rectangle)
BreitbandGraphics.draw_image_nineslice(eff_rectangle,
style.button.states[visual_state].source,
style.button.states[visual_state].center,
style.path, BreitbandGraphics.colors.white, 'nearest')
end)
end
ugui.standard_styler.draw_edit_frame = function(control, rectangle,
visual_state)
local key = ugui_ext.internal.params_to_key('edit_frame', rectangle, visual_state)
ugui_ext.internal.cached_draw(key, rectangle, function(eff_rectangle)
BreitbandGraphics.draw_image_nineslice(eff_rectangle,
style.textbox.states[visual_state].source,
style.textbox.states[visual_state].center,
style.path, BreitbandGraphics.colors.white, 'nearest')
end)
end
ugui.standard_styler.draw_list_frame = function(rectangle, visual_state)
local key = ugui_ext.internal.params_to_key('list_frame', rectangle, visual_state)
ugui_ext.internal.cached_draw(key, rectangle, function(eff_rectangle)
BreitbandGraphics.draw_image_nineslice(eff_rectangle,
style.listbox.states[visual_state].source,
style.listbox.states[visual_state].center,
style.path, BreitbandGraphics.colors.white, 'nearest')
end)
end
ugui.standard_styler.draw_list_item = function(control, item, rectangle, visual_state)
if not item then
return
end
local rect = BreitbandGraphics.inflate_rectangle(rectangle, -1)
-- bad idea to cache these
BreitbandGraphics.draw_image_nineslice(rect,
style.listbox_item.states[visual_state].source,
style.listbox_item.states[visual_state].center,
style.path, BreitbandGraphics.colors.white, 'nearest')
local text_rect = {
x = rectangle.x + 2,
y = rectangle.y,
width = rectangle.width,
height = rectangle.height,
}
ugui.standard_styler.draw_rich_text(text_rect, BreitbandGraphics.alignment.start, nil, item, ugui.standard_styler.params.listbox_item.text[visual_state], control.plaintext)
end
ugui.standard_styler.draw_scrollbar = function(container_rectangle, thumb_rectangle, visual_state)
BreitbandGraphics.draw_image(container_rectangle,
style.scrollbar_rail,
style.path, BreitbandGraphics.colors.white, 'nearest')
local key = ugui_ext.internal.params_to_key('scrollbar_thumb', thumb_rectangle, visual_state)
ugui_ext.internal.cached_draw(
key,
thumb_rectangle,
function(eff_rectangle)
BreitbandGraphics.draw_image_nineslice(eff_rectangle,
style.scrollbar_thumb.states[visual_state].source,
style.scrollbar_thumb.states[visual_state].center,
style.path, BreitbandGraphics.colors.white, 'nearest')
end)
end
end
return ugui_ext