generated from karashiiro/DalamudPluginProjectTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
SimpleWindow.cs
185 lines (169 loc) · 6.04 KB
/
SimpleWindow.cs
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
// <copyright file="SimpleWindow.cs" company="lokinmodar">
// Copyright (c) lokinmodar. All rights reserved.
// Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License license.
// </copyright>
using System;
using System.Numerics;
using System.Threading;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Windowing;
using Dalamud.Utility;
using Echoglossian.Properties;
using ImGuiNET;
namespace Echoglossian
{
internal class SimpleWindow : Window, IDisposable
{
// TODO: add window position calculations based on the current addon
// TODO: add window sizing calculations based on the current translation
// This is currently not in use yet!
private bool disposedValue;
private bool displayTranslation;
private readonly SemaphoreSlim translationSemaphore;
private string translation = string.Empty;
private volatile int currentTranslationId;
private Vector2 textDimensions = Vector2.Zero;
private Vector2 textImguiSize = Vector2.Zero;
private Vector2 textPosition = Vector2.Zero;
private Config configuration;
private ImFontPtr uiFont;
private bool fontLoaded;
/// <summary>
/// Initializes a new instance of the <see cref="SimpleWindow"/> class.
/// </summary>
/// <param name="name">Window Name.</param>
/// <param name="flags">Window Flags.</param>
/// <param name="forceMainWindow"></param>
/// <param name="translation"></param>
/// <param name="displayTranslation"></param>
/// <param name="curentTranslationId"></param>
/// <param name="textDimensions"></param>
/// <param name="textImguiSize"></param>
/// <param name="textPosition"></param>
/// <param name="uiFont"></param>
/// <param name="fontLoaded"></param>
/// <param name="translationSemaphore"></param>
public SimpleWindow(
string name,
ImGuiWindowFlags flags = ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoScrollbar |
ImGuiWindowFlags.NoScrollWithMouse | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoDecoration,
bool forceMainWindow = false,
string? translation = "",
bool displayTranslation = false,
int curentTranslationId = 0,
Vector2 textDimensions = default,
Vector2 textImguiSize = default,
Vector2 textPosition = default,
ImFontPtr uiFont = default,
bool fontLoaded = default,
SemaphoreSlim translationSemaphore = default)
: base(name, flags, forceMainWindow)
{
this.WindowName = name;
this.translation = translation;
this.displayTranslation = displayTranslation;
this.currentTranslationId = curentTranslationId;
this.textDimensions = textDimensions;
this.textImguiSize = textImguiSize;
this.textPosition = textPosition;
this.configuration = Echoglossian.PluginInterface.GetPluginConfig() as Config;
this.translationSemaphore = translationSemaphore;
this.uiFont = uiFont;
this.fontLoaded = fontLoaded;
}
public override void Draw()
{
#if DEBUG
// PluginLog.Debug("Inside DrawTranslatedDialogueWindow method!");
#endif
ImGuiHelpers.SetNextWindowPosRelativeMainViewport(new Vector2(
this.textPosition.X + (this.textDimensions.X / 2) - (this.textImguiSize.X / 2),
this.textPosition.Y - this.textImguiSize.Y - 20) + this.configuration.ImGuiWindowPosCorrection);
if (this.fontLoaded)
{
#if DEBUG
// PluginLog.Debug("Pushing font");
#endif
Echoglossian.UINewFontHandler.GeneralFontHandle.Push();
}
float size = Math.Min(
(this.textDimensions.X * this.configuration.ImGuiTalkWindowWidthMult) + (ImGui.GetStyle().WindowPadding.X * 2),
(ImGui.CalcTextSize(this.translation).X * 1.25f) + (ImGui.GetStyle().WindowPadding.X * 2));
ImGui.SetNextWindowSizeConstraints(new Vector2(size, 0), new Vector2(size, this.textDimensions.Y * this.configuration.ImGuiTalkWindowHeightMult));
ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(this.configuration.OverlayTalkTextColor, 255));
if (this.configuration.TranslateNpcNames)
{
string name = string.Empty;//GetTranslatedNpcNameForWindow();
if (!name.IsNullOrEmpty())
{
ImGui.Begin(
name,
ImGuiWindowFlags.NoNav
| ImGuiWindowFlags.NoCollapse
| ImGuiWindowFlags.AlwaysAutoResize
| ImGuiWindowFlags.NoFocusOnAppearing
| ImGuiWindowFlags.NoMouseInputs
| ImGuiWindowFlags.NoScrollbar);
}
else
{
ImGui.Begin(
"Talk translation",
ImGuiWindowFlags.NoTitleBar
| ImGuiWindowFlags.NoNav
| ImGuiWindowFlags.AlwaysAutoResize
| ImGuiWindowFlags.NoFocusOnAppearing
| ImGuiWindowFlags.NoMouseInputs
| ImGuiWindowFlags.NoScrollbar);
}
}
else
{
ImGui.Begin(
"Talk translation",
ImGuiWindowFlags.NoTitleBar
| ImGuiWindowFlags.NoNav
| ImGuiWindowFlags.AlwaysAutoResize
| ImGuiWindowFlags.NoFocusOnAppearing
| ImGuiWindowFlags.NoMouseInputs
| ImGuiWindowFlags.NoScrollbar);
}
ImGui.SetWindowFontScale(this.configuration.FontScale);
if (this.translationSemaphore.Wait(0))
{
ImGui.TextWrapped(this.translation);
this.translationSemaphore.Release();
}
else
{
ImGui.Text(Resources.WaitingForTranslation);
}
this.textImguiSize = ImGui.GetWindowSize();
ImGui.PopStyleColor(1);
ImGui.End();
if (this.fontLoaded)
{
#if DEBUG
// PluginLog.Debug("Popping font!");
#endif
Echoglossian.UINewFontHandler.GeneralFontHandle.Pop();
}
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
this.translationSemaphore.Dispose();
}
this.disposedValue = true;
}
}
public void Dispose()
{
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}