forked from AthenaADP/MHW-ASS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreviewImage.h
297 lines (248 loc) · 8.76 KB
/
PreviewImage.h
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#pragma once
#include "Common.h"
#include "Armor.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace MHWASS
{
public ref class PreviewImage : public System::Windows::Forms::Form
{
Bitmap^ image;
String^ path;
bool female;
int current_zoom;
Control^ parent_control;
public:
static int zoom = 100;
PreviewImage( const bool female )
: female( female )
{
InitializeComponent();
this->FormBorderStyle = Windows::Forms::FormBorderStyle::SizableToolWindow;
this->MouseWheel += gcnew MouseEventHandler( this, &PreviewImage::OnMouseWheelInput );
this->pictureBox1->MouseWheel += gcnew MouseEventHandler( this, &PreviewImage::OnMouseWheelInput );
current_zoom = zoom;
if( female )
path = L"Data/Images/f_";
else
path = L"Data/Images/m_";
String^ base_filename = path + L"base.png";
if( IO::File::Exists( base_filename ) )
{
image = gcnew Bitmap( base_filename );
this->ClientSize = System::Drawing::Size( image->Width * zoom / 200, image->Height * zoom / 200 );
}
}
void SetLocation( Control^ parent_control )
{
this->parent_control = parent_control;
UpdateLocation();
}
#pragma warning( disable: 4677 )
Bitmap^ SetData( List_t< Armor^ >% armors ) { return SetData( armors, nullptr ); }
Bitmap^ SetData( List_t< Armor^ >% armors, Bitmap^ cached_image )
{
if( !image )
return nullptr;
if( cached_image )
{
pictureBox1->Image = cached_image;
return cached_image;
}
Drawing::Rectangle r( 0, 0, image->Width, image->Height );
Imaging::BitmapData^ bd = image->LockBits( r, Imaging::ImageLockMode::ReadWrite, Imaging::PixelFormat::Format32bppArgb );
array< int >^ pix = gcnew array< int >( image->Width * image->Height );
Runtime::InteropServices::Marshal::Copy( bd->Scan0, pix, 0, pix->Length );
array< Byte >^ head_info = LoadImagePack( L"head" );
array< Byte >^ body_info = LoadImagePack( L"body" );
array< Byte >^ arms_info = LoadImagePack( L"arms" );
array< Byte >^ waist_info = LoadImagePack( L"waist" );
array< Byte >^ legs_info = LoadImagePack( L"legs" );
MergeImage( head_info, pix, armors[ (int)Armor::ArmorType::HEAD ], true );
MergeImage( body_info, pix, armors[ (int)Armor::ArmorType::BODY ], true );
MergeImage( waist_info, pix, armors[ (int)Armor::ArmorType::WAIST ], true );
MergeImage( legs_info, pix, armors[ (int)Armor::ArmorType::LEGS ], false );
if( !IsGammothMail( armors[ (int)Armor::ArmorType::BODY ] ) )
MergeImage( body_info, pix, armors[ (int)Armor::ArmorType::BODY ], false );
MergeImage( waist_info, pix, armors[ (int)Armor::ArmorType::WAIST ], false );
MergeImage( arms_info, pix, armors[ (int)Armor::ArmorType::ARMS ], false );
if( IsGammothMail( armors[ (int)Armor::ArmorType::BODY ] ) )
MergeImage( body_info, pix, armors[ (int)Armor::ArmorType::BODY ], false );
MergeImage( head_info, pix, armors[ (int)Armor::ArmorType::HEAD ], false );
Runtime::InteropServices::Marshal::Copy( pix, 0, bd->Scan0, pix->Length );
image->UnlockBits( bd );
pictureBox1->Image = image;
return image;
}
bool CanShow()
{
return !!image;
}
protected:
void UpdateLocation()
{
if( !parent_control )
return;
Screen^ screen = Screen::FromControl( this );
if( parent_control->Location.Y + this->Size.Height >= screen->WorkingArea.Bottom )
this->Location = Point( parent_control->Location.X + parent_control->Size.Width, Math::Max( 0, screen->WorkingArea.Bottom - this->Size.Height ) );
else
this->Location = Point( parent_control->Location.X + parent_control->Size.Width, parent_control->Location.Y );
}
void OnMouseWheelInput( System::Object^ sender, MouseEventArgs^ e )
{
const int dz = 5 * e->Delta / 120;
const int new_zoom = Math::Max( 50, Math::Min( 200, current_zoom + dz ) );
if( new_zoom == current_zoom )
return;
current_zoom = new_zoom;
this->ClientSize = System::Drawing::Size( image->Width * current_zoom / 200, image->Height * current_zoom / 200 );
UpdateLocation();
}
bool IsGammothMail( Armor^ armor )
{
if( !armor )
return false;
if( female )
{
return armor->female_image == 135 ||
armor->female_image == 136 ||
armor->female_image == 401 ||
armor->female_image == 402;
}
else
{
return false;
}
}
unsigned ReadUInt( array< Byte >^ data, const unsigned offset )
{
unsigned n = int(data[offset]) << 24;
n |= int(data[offset + 1]) << 16;
n |= int(data[offset + 2]) << 8;
n |= int(data[offset + 3]);
return n;
}
unsigned short ReadUShort( array< Byte >^ data, const unsigned offset )
{
unsigned short n = (unsigned short)data[offset] << 8;
n |= (unsigned short)data[offset + 1];
return n;
}
array< Byte >^ LoadImagePack( String^ subpath )
{
String^ filename = path + subpath + L".pak";
if( IO::File::Exists( filename ) )
return IO::File::ReadAllBytes( filename );
else
return nullptr;
}
Bitmap^ LoadPackedImage( array< Byte >^ data, const unsigned index, int% dx, int% dy, const bool need_alpha )
{
const unsigned images_offset = ReadUInt( data, 0 );
const unsigned index_offset = index * 4;
if( index_offset + 4 > images_offset )
return nullptr;
const unsigned offset = ReadUInt( data, index_offset );
if( offset == 0xffffffff )
return nullptr;
dx = ReadUShort( data, images_offset + offset );
dy = ReadUShort( data, images_offset + offset + 2 );
if( dx & 0x8000 )
{
dx = dx & 0x7fff;
}
else if( need_alpha )
return nullptr;
return gcnew Bitmap( gcnew IO::MemoryStream( data, images_offset + offset + 4, data->Length - offset - images_offset - 4, false, true ) );
}
void MergeImage( array< Byte >^ data, array< int >^ pix, Armor^ armor, bool use_mid_alpha_only )
{
if( !armor || !data )
return;
const int index = female ? armor->female_image : armor->male_image;
if( index < 1 )
return;
int dx, dy;
Bitmap^ b = LoadPackedImage( data, index, dx, dy, use_mid_alpha_only );
if( !b )
return;
Drawing::Rectangle r( 0, 0, b->Width, b->Height );
Imaging::BitmapData^ bd = b->LockBits( r, Imaging::ImageLockMode::ReadWrite, Imaging::PixelFormat::Format32bppArgb );
array< int >^ bpix = gcnew array< int >( b->Width * b->Height );
Runtime::InteropServices::Marshal::Copy( bd->Scan0, bpix, 0, bpix->Length );
b->UnlockBits( bd );
for( int x = 0; x < b->Width; ++x )
{
for( int y = 0; y < b->Height; ++y )
{
const int c = bpix[ x + b->Width * y ];
const unsigned char alpha = (c >> 24) & 0xff;
if( alpha == 0 )
continue;
if( ( alpha < 255 ) == use_mid_alpha_only )
{
pix[ x + dx + image->Width * (y + dy) ] = c | 0xff000000;
}
}
}
}
~PreviewImage()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
this->pictureBox1->Location = System::Drawing::Point(0, 0);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(190, 500);
this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// PreviewImage
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(190, 500);
this->ControlBox = false;
this->Controls->Add(this->pictureBox1);
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = L"PreviewImage";
this->ShowIcon = false;
this->ShowInTaskbar = false;
this->TopMost = true;
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
};
}